From 8f41404f74e51deb54c04b97c1320095ee81345f Mon Sep 17 00:00:00 2001 From: Joshua Olaoye Date: Sat, 26 Oct 2024 08:53:27 -0500 Subject: [PATCH 01/13] build: Add config for rust implementation --- file-shuffler/shuffler-in-rust/.gitignore | 1 + file-shuffler/shuffler-in-rust/Cargo.toml | 6 ++++++ file-shuffler/shuffler-in-rust/src/main.rs | 3 +++ 3 files changed, 10 insertions(+) create mode 100644 file-shuffler/shuffler-in-rust/.gitignore create mode 100644 file-shuffler/shuffler-in-rust/Cargo.toml create mode 100644 file-shuffler/shuffler-in-rust/src/main.rs diff --git a/file-shuffler/shuffler-in-rust/.gitignore b/file-shuffler/shuffler-in-rust/.gitignore new file mode 100644 index 0000000..c41cc9e --- /dev/null +++ b/file-shuffler/shuffler-in-rust/.gitignore @@ -0,0 +1 @@ +/target \ No newline at end of file diff --git a/file-shuffler/shuffler-in-rust/Cargo.toml b/file-shuffler/shuffler-in-rust/Cargo.toml new file mode 100644 index 0000000..d69e671 --- /dev/null +++ b/file-shuffler/shuffler-in-rust/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "shuffler-in-rust" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/file-shuffler/shuffler-in-rust/src/main.rs b/file-shuffler/shuffler-in-rust/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/file-shuffler/shuffler-in-rust/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} From 013347d7d180058d4cd7f028971ffaf8db5119f5 Mon Sep 17 00:00:00 2001 From: Joshua Olaoye Date: Sat, 26 Oct 2024 08:55:20 -0500 Subject: [PATCH 02/13] chore: Add dependencies --- file-shuffler/shuffler-in-rust/Cargo.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/file-shuffler/shuffler-in-rust/Cargo.toml b/file-shuffler/shuffler-in-rust/Cargo.toml index d69e671..a30c6da 100644 --- a/file-shuffler/shuffler-in-rust/Cargo.toml +++ b/file-shuffler/shuffler-in-rust/Cargo.toml @@ -4,3 +4,6 @@ version = "0.1.0" edition = "2021" [dependencies] +walkdir = "2.3" +rand = "0.8" +filetime = "0.2" \ No newline at end of file From ba4d52ea7b1ec414b9ead02056b579d2a2247d49 Mon Sep 17 00:00:00 2001 From: Joshua Olaoye Date: Sat, 26 Oct 2024 08:59:07 -0500 Subject: [PATCH 03/13] feature: Add implementation for file shuffler --- file-shuffler/shuffler-in-rust/src/main.rs | 221 ++++++++++++++++++++- 1 file changed, 219 insertions(+), 2 deletions(-) diff --git a/file-shuffler/shuffler-in-rust/src/main.rs b/file-shuffler/shuffler-in-rust/src/main.rs index e7a11a9..bfcfd61 100644 --- a/file-shuffler/shuffler-in-rust/src/main.rs +++ b/file-shuffler/shuffler-in-rust/src/main.rs @@ -1,3 +1,220 @@ -fn main() { - println!("Hello, world!"); +use std::env; +use std::fs; +use std::io; +use std::path::{Path, PathBuf}; +use rand::Rng; +use filetime::{FileTime, set_file_times}; +use std::time::{SystemTime, UNIX_EPOCH, Duration}; +use std::thread; +use std::str::FromStr; +use std::collections::HashSet; + +// Enum class for the time interval +#[derive(Debug)] +enum Interval { + Never, + EveryWeek, + Every30Seconds, +} + + + +impl FromStr for Interval { + type Err = (); + + fn from_str(s: &str) -> Result { + match s { + "0" => Ok(Interval::Never), + "1" => Ok(Interval::EveryWeek), + "2" => Ok(Interval::Every30Seconds), + _ => Err(()), + } + } +} + +/// Generates a random timestamp within the last 10 days. +fn random_timestamp() -> SystemTime { + let now = SystemTime::now(); + let ten_days = Duration::from_secs(10 * 24 * 60 * 60); // 10 days in seconds + let random_offset = rand::thread_rng().gen_range(0..=ten_days.as_secs()); + now - Duration::from_secs(random_offset) +} + +/// Renames files in a given directory sequentially +fn rename_files_in_directory(dir: &Path) -> io::Result<()> { + let mut used_numbers = HashSet::new(); + let mut rng = rand::thread_rng(); + + // Create a temporary directory + let temp_dir = dir.join("temp"); + fs::create_dir_all(&temp_dir)?; + + // Count the number of files in the directory + let n = fs::read_dir(dir)?.count(); + + for entry in fs::read_dir(dir)? { + let entry = entry?; + let path = entry.path(); + + // Check for hidden files and delete them (.DS_Store on Mac) + if path.file_name().unwrap().to_str().unwrap().starts_with('.') { + fs::remove_file(&path)?; + continue; // Skip processing further for this entry + } + + if path.is_file() { + // Generate a unique random number within the range of 1 to n + let mut random_number; + let mut new_path; + loop { + random_number = rng.gen_range(1..=n); + new_path = temp_dir.join(format!("file_{}.txt", random_number)); + if !used_numbers.contains(&random_number) && !new_path.exists() { + used_numbers.insert(random_number); + break; + } + } + + // Move the file to the temporary directory with the new name + fs::rename(&path, &new_path)?; + + // Uncomment and implement the timestamp update if needed + let random_time = FileTime::from_system_time(random_timestamp()); + set_file_times(&new_path, random_time, random_time)?; + } + } + + // Move files back from the temporary directory to the original directory + for entry in fs::read_dir(&temp_dir)? { + let entry = entry?; + let temp_path = entry.path(); + let file_name = temp_path.file_name().unwrap(); + let final_path = dir.join(file_name); + fs::rename(&temp_path, &final_path)?; + } + + // Remove the temporary directory + fs::remove_dir(&temp_dir)?; + + // FIXME: One off error on mac + println!("Renamed {:?} file(s) in: {:?}", n, dir); + Ok(()) +} + +/// Determines if a directory contains subdirectories. +fn has_subdirectories(dir: &Path) -> bool { + fs::read_dir(dir) + .map(|mut entries| entries.any(|e| e.map(|e| e.path().is_dir()).unwrap_or(false))) + .unwrap_or(false) +} + +/// Moves files from a directory to its parent and deletes the directory. +fn move_files_to_parent_and_delete(dir: &Path) -> io::Result<()> { + let parent_dir = dir.parent().unwrap(); + for entry in fs::read_dir(dir)? { + let entry = entry?; + let path = entry.path(); + + if path.is_file() { + let file_name = path.file_name().unwrap(); + let mut new_path = parent_dir.join(file_name); + + // Check for name conflicts and generate a unique name if necessary + let mut counter = 1; + while new_path.exists() { + let new_file_name = format!("{}_copy{}", + file_name.to_string_lossy(), // Original file name + counter // Append counter + ); + new_path = parent_dir.join(new_file_name); + counter += 1; + } + + // Move the file to the parent directory + fs::rename(&path, &new_path)?; + } + } + + // Delete the now-empty directory + fs::remove_dir(dir)?; + Ok(()) +} + +/// Recursively traverses directories and renames files +fn process_directory_recursive(dir: &Path, root_dir: &Path) -> io::Result<()> { + + // Process only the immediate subdirectories + for entry in fs::read_dir(dir)? { + let entry = entry?; + let path = entry.path(); + + // Process only directories + if path.is_dir() { + process_directory_recursive(&path, &root_dir)?; + } + } + + // After processing subdirectories, check if the current directory is now a leaf + if dir != root_dir && !has_subdirectories(dir) { + if dir.parent() == Some(root_dir) { + rename_files_in_directory(dir)?; + return Ok(()) + } + move_files_to_parent_and_delete(dir)?; + } + + Ok(()) +} + +fn is_at_least_two_levels_deep(dir: &Path) -> bool { + if let Ok(entries) = fs::read_dir(dir) { + for entry in entries { + if let Ok(entry) = entry { + let path = entry.path(); + if path.is_dir() { + return true; + } + } + } + } + false +} + +fn main() -> io::Result<()> { + // Get the source directory and interval from command-line arguments + let args: Vec = env::args().collect(); + if args.len() != 3 { + eprintln!("Usage: {} ", args[0]); + eprintln!("Interval: 0 for never, 1 for every week, 2 for every 30 seconds"); + std::process::exit(1); + } + + let source_dir = PathBuf::from(&args[1]); + let interval: Interval = args[2].parse().unwrap_or(Interval::Never); + + if !source_dir.exists() || !source_dir.is_dir() { + eprintln!("Source directory not found or not a directory."); + std::process::exit(1); + } + + if !is_at_least_two_levels_deep(&source_dir) { + eprintln!("Source directory is not at least 2 levels deep."); + std::process::exit(1); + } + + match interval { + Interval::Never => { + process_directory_recursive(&source_dir, &source_dir)?; + } + Interval::EveryWeek => loop { + process_directory_recursive(&source_dir, &source_dir)?; + thread::sleep(Duration::from_secs(7 * 24 * 60 * 60)); // Sleep for 1 week + }, + Interval::Every30Seconds => loop { + process_directory_recursive(&source_dir, &source_dir)?; + thread::sleep(Duration::from_secs(30)); // Sleep for 30 seconds + }, + } + + Ok(()) } From 41b6b30b788318132aed0b5267df174bcbc3938e Mon Sep 17 00:00:00 2001 From: Joshua Olaoye Date: Sat, 26 Oct 2024 09:02:39 -0500 Subject: [PATCH 04/13] chore: Add Cargo.lock for dependency version consistency --- file-shuffler/shuffler-in-rust/Cargo.lock | 281 ++++++++++++++++++++++ 1 file changed, 281 insertions(+) create mode 100644 file-shuffler/shuffler-in-rust/Cargo.lock diff --git a/file-shuffler/shuffler-in-rust/Cargo.lock b/file-shuffler/shuffler-in-rust/Cargo.lock new file mode 100644 index 0000000..7c4718d --- /dev/null +++ b/file-shuffler/shuffler-in-rust/Cargo.lock @@ -0,0 +1,281 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "bitflags" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "filetime" +version = "0.2.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35c0522e981e68cbfa8c3f978441a5f34b30b96e146b33cd3359176b50fe8586" +dependencies = [ + "cfg-if", + "libc", + "libredox", + "windows-sys", +] + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.161" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1" + +[[package]] +name = "libredox" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" +dependencies = [ + "bitflags", + "libc", + "redox_syscall", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "proc-macro2" +version = "1.0.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "redox_syscall" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b6dfecf2c74bce2466cabf93f6664d6998a69eb21e39f4207930065b27b771f" +dependencies = [ + "bitflags", +] + +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "shuffler-in-rust" +version = "0.1.0" +dependencies = [ + "filetime", + "rand", + "walkdir", +] + +[[package]] +name = "syn" +version = "2.0.85" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5023162dfcd14ef8f32034d8bcd4cc5ddc61ef7a247c024a33e24e1f24d21b56" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" + +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "winapi-util" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "byteorder", + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] From f07fe989e02ddcf00c7f6efa419387bf84a0f806 Mon Sep 17 00:00:00 2001 From: Joshua Olaoye Date: Sat, 26 Oct 2024 10:18:13 -0500 Subject: [PATCH 05/13] chore: Add chrono library for date and time manipulation --- file-shuffler/shuffler-in-rust/Cargo.lock | 213 ++++++++++++++++++++++ file-shuffler/shuffler-in-rust/Cargo.toml | 4 +- 2 files changed, 216 insertions(+), 1 deletion(-) diff --git a/file-shuffler/shuffler-in-rust/Cargo.lock b/file-shuffler/shuffler-in-rust/Cargo.lock index 7c4718d..9e9ad46 100644 --- a/file-shuffler/shuffler-in-rust/Cargo.lock +++ b/file-shuffler/shuffler-in-rust/Cargo.lock @@ -2,24 +2,80 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + [[package]] name = "bitflags" version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" +[[package]] +name = "bumpalo" +version = "3.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" + [[package]] name = "byteorder" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" +[[package]] +name = "cc" +version = "1.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2e7962b54006dcfcc61cb72735f4d89bb97061dd6a7ed882ec6b8ee53714c6f" +dependencies = [ + "shlex", +] + [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "js-sys", + "num-traits", + "wasm-bindgen", + "windows-targets", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + [[package]] name = "filetime" version = "0.2.25" @@ -43,6 +99,38 @@ dependencies = [ "wasi", ] +[[package]] +name = "iana-time-zone" +version = "0.1.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "js-sys" +version = "0.3.72" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a88f1bda2bd75b0452a14784937d796722fdebfe50df998aeb3f0b7603019a9" +dependencies = [ + "wasm-bindgen", +] + [[package]] name = "libc" version = "0.2.161" @@ -60,6 +148,27 @@ dependencies = [ "redox_syscall", ] +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" + [[package]] name = "ppv-lite86" version = "0.2.20" @@ -135,12 +244,20 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + [[package]] name = "shuffler-in-rust" version = "0.1.0" dependencies = [ + "chrono", "filetime", "rand", + "terminal_size", "walkdir", ] @@ -155,6 +272,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "terminal_size" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "633c1a546cee861a1a6d0dc69ebeca693bf4296661ba7852b9d21d159e0506df" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "unicode-ident" version = "1.0.13" @@ -177,6 +304,77 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wasm-bindgen" +version = "0.2.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "128d1e363af62632b8eb57219c8fd7877144af57558fb2ef0368d0087bddeb2e" +dependencies = [ + "cfg-if", + "once_cell", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb6dd4d3ca0ddffd1dd1c9c04f94b868c37ff5fac97c30b97cff2d74fce3a358" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e79384be7f8f5a9dd5d7167216f022090cf1f9ec128e6e6a482a2cb5c5422c56" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26c6ab57572f7a24a4985830b120de1594465e5d500f24afe89e16b4e833ef68" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65fc09f10666a9f147042251e0dda9c18f166ff7de300607007e96bdebc1068d" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + [[package]] name = "winapi-util" version = "0.1.9" @@ -186,6 +384,21 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-sys" version = "0.59.0" diff --git a/file-shuffler/shuffler-in-rust/Cargo.toml b/file-shuffler/shuffler-in-rust/Cargo.toml index a30c6da..4ff92f2 100644 --- a/file-shuffler/shuffler-in-rust/Cargo.toml +++ b/file-shuffler/shuffler-in-rust/Cargo.toml @@ -6,4 +6,6 @@ edition = "2021" [dependencies] walkdir = "2.3" rand = "0.8" -filetime = "0.2" \ No newline at end of file +filetime = "0.2" +terminal_size = "0.1" +chrono = "0.4" \ No newline at end of file From 7b58b62b6c116dbadf43386d9fe015fab73d32e6 Mon Sep 17 00:00:00 2001 From: Joshua Olaoye Date: Sat, 26 Oct 2024 10:29:37 -0500 Subject: [PATCH 06/13] fix: Display file names and timestamps before and after changes --- file-shuffler/shuffler-in-rust/src/main.rs | 70 +++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/file-shuffler/shuffler-in-rust/src/main.rs b/file-shuffler/shuffler-in-rust/src/main.rs index bfcfd61..8334613 100644 --- a/file-shuffler/shuffler-in-rust/src/main.rs +++ b/file-shuffler/shuffler-in-rust/src/main.rs @@ -8,6 +8,8 @@ use std::time::{SystemTime, UNIX_EPOCH, Duration}; use std::thread; use std::str::FromStr; use std::collections::HashSet; +use terminal_size::{Width, terminal_size}; +use chrono::{DateTime, FixedOffset, TimeZone, LocalResult}; // Enum class for the time interval #[derive(Debug)] @@ -18,7 +20,6 @@ enum Interval { } - impl FromStr for Interval { type Err = (); @@ -63,6 +64,11 @@ fn rename_files_in_directory(dir: &Path) -> io::Result<()> { } if path.is_file() { + // Get last modified timestamp + let timestamp = get_formatted_modified_time(&path)?; + + println!("| Currently modifying file\n| File name: {:?}\n| Unix timestamp: {:?}", path, timestamp); + println!("•"); // Generate a unique random number within the range of 1 to n let mut random_number; let mut new_path; @@ -81,6 +87,10 @@ fn rename_files_in_directory(dir: &Path) -> io::Result<()> { // Uncomment and implement the timestamp update if needed let random_time = FileTime::from_system_time(random_timestamp()); set_file_times(&new_path, random_time, random_time)?; + + let modified_timestamp = get_formatted_modified_time(&new_path)?; + + println!("| Modified file\n| New file name: {:?}\n| Modified unix timestamp: {:?}\n", new_path, modified_timestamp); } } @@ -98,6 +108,7 @@ fn rename_files_in_directory(dir: &Path) -> io::Result<()> { // FIXME: One off error on mac println!("Renamed {:?} file(s) in: {:?}", n, dir); + print_dashes(true); Ok(()) } @@ -157,6 +168,8 @@ fn process_directory_recursive(dir: &Path, root_dir: &Path) -> io::Result<()> { // After processing subdirectories, check if the current directory is now a leaf if dir != root_dir && !has_subdirectories(dir) { if dir.parent() == Some(root_dir) { + println!("Currently processing {:?}", dir); + print_dashes(false); rename_files_in_directory(dir)?; return Ok(()) } @@ -180,6 +193,61 @@ fn is_at_least_two_levels_deep(dir: &Path) -> bool { false } +fn print_dashes(add_new_line: bool) { + let width = if let Some((Width(w), _)) = terminal_size() { + w as usize + } else { + 80 // Default width if terminal size can't be determined + }; + + println!("{}", "-".repeat(width)); + + if add_new_line { + println!(); // Print an additional new line if specified + } +} + +/// Function to get the formatted last modified time of a file +fn get_formatted_modified_time(path: &PathBuf) -> std::io::Result { + let metadata = fs::metadata(path)?; + let modified_time = metadata.modified()?; + + let timestamp = modified_time_to_unix(&modified_time)?; + let formatted_date = format_timestamp(timestamp); + + Ok(formatted_date) +} + +/// Function to convert SystemTime to UNIX timestamp +fn modified_time_to_unix(modified_time: &SystemTime) -> std::io::Result { + let duration_since_epoch = modified_time.duration_since(UNIX_EPOCH) + .expect("Time went backwards"); + Ok(duration_since_epoch.as_secs()) +} + +/// Function to format UNIX timestamp to a human-readable string +fn format_timestamp(timestamp: u64) -> String { + // Define a fixed offset for CDT (UTC-5) + let fixed_offset = FixedOffset::west_opt(5 * 3600).expect("Invalid offset"); // 5 hours behind UTC + + // Convert the timestamp to DateTime + let datetime: LocalResult> = fixed_offset.timestamp_opt(timestamp as i64, 0); + + // Extract the DateTime + match extract_datetime(datetime) { + Some(dt) => dt.format("%B %d, %Y at %I:%M %p").to_string(), + None => "Invalid date".to_string(), // Handle the None case as needed + } +} + +fn extract_datetime(local_result: LocalResult>) -> Option> { + match local_result { + LocalResult::Single(dt) => Some(dt), + LocalResult::Ambiguous(dt1, _) => Some(dt1), // You can choose which one to return or handle both + LocalResult::None => None, + } +} + fn main() -> io::Result<()> { // Get the source directory and interval from command-line arguments let args: Vec = env::args().collect(); From 1ce0dfc20e9896197b97966478cea37cc0a7effc Mon Sep 17 00:00:00 2001 From: Joshua Olaoye Date: Sat, 26 Oct 2024 10:33:55 -0500 Subject: [PATCH 07/13] chore: Update output format --- file-shuffler/shuffler-in-rust/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/file-shuffler/shuffler-in-rust/src/main.rs b/file-shuffler/shuffler-in-rust/src/main.rs index 8334613..99e65e6 100644 --- a/file-shuffler/shuffler-in-rust/src/main.rs +++ b/file-shuffler/shuffler-in-rust/src/main.rs @@ -168,8 +168,8 @@ fn process_directory_recursive(dir: &Path, root_dir: &Path) -> io::Result<()> { // After processing subdirectories, check if the current directory is now a leaf if dir != root_dir && !has_subdirectories(dir) { if dir.parent() == Some(root_dir) { - println!("Currently processing {:?}", dir); print_dashes(false); + println!("Currently processing {:?}\n", dir); rename_files_in_directory(dir)?; return Ok(()) } From 13fae18616a3fe7c7461f7d2791afac16495c979 Mon Sep 17 00:00:00 2001 From: Joshua Olaoye Date: Sat, 26 Oct 2024 10:44:40 -0500 Subject: [PATCH 08/13] chore: Update output format --- file-shuffler/shuffler-in-rust/src/main.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/file-shuffler/shuffler-in-rust/src/main.rs b/file-shuffler/shuffler-in-rust/src/main.rs index 99e65e6..f902469 100644 --- a/file-shuffler/shuffler-in-rust/src/main.rs +++ b/file-shuffler/shuffler-in-rust/src/main.rs @@ -67,7 +67,7 @@ fn rename_files_in_directory(dir: &Path) -> io::Result<()> { // Get last modified timestamp let timestamp = get_formatted_modified_time(&path)?; - println!("| Currently modifying file\n| File name: {:?}\n| Unix timestamp: {:?}", path, timestamp); + println!("| Original file\n| File name: {:?}\n| Timestamp: {:?}", path, timestamp); println!("•"); // Generate a unique random number within the range of 1 to n let mut random_number; @@ -90,7 +90,7 @@ fn rename_files_in_directory(dir: &Path) -> io::Result<()> { let modified_timestamp = get_formatted_modified_time(&new_path)?; - println!("| Modified file\n| New file name: {:?}\n| Modified unix timestamp: {:?}\n", new_path, modified_timestamp); + println!("| Modified file\n| File name: {:?}\n| Timestamp: {:?}\n", new_path, modified_timestamp); } } @@ -286,3 +286,10 @@ fn main() -> io::Result<()> { Ok(()) } + + +// Cargo +// Cargo run