diff --git a/coreutils/src/commands/ln.rs b/coreutils/src/commands/ln.rs index 5de84f0..b09b757 100644 --- a/coreutils/src/commands/ln.rs +++ b/coreutils/src/commands/ln.rs @@ -1,10 +1,6 @@ use boxutils::args::ArgParser; use boxutils::commands::Command; use std::fs; -#[cfg(unix)] -use std::os::unix::fs::symlink; -#[cfg(windows)] -use std::os::windows::fs::{symlink_dir, symlink_file}; use std::path::Path; pub struct Ln; @@ -58,28 +54,13 @@ impl Command for Ln { } if args.get_flag("-s") { - #[cfg(unix)] - { - if let Err(e) = symlink(&to_be_linked, &destination) { - eprintln!("ln: failed to create symlink: {}", e); - } - } + let symlink_result = boxutils::cross::fs::symlink(to_be_linked, destination); - #[cfg(windows)] - { - let target_metadata = fs::metadata(&to_be_linked).unwrap(); - let symlink_result = if target_metadata.is_dir() { - symlink_dir(&to_be_linked, &destination) - } else { - symlink_file(&to_be_linked, &destination) - }; - - if let Err(e) = symlink_result { - eprintln!("ln: failed to create symlink: {}", e); - } + if let Err(e) = symlink_result { + eprintln!("ln: failed to create symlink: {}", e); } } else { - if let Err(e) = fs::hard_link(&to_be_linked, &destination) { + if let Err(e) = boxutils::cross::fs::hard_link(to_be_linked, destination) { eprintln!("ln: failed to create hard link: {}", e); } } diff --git a/utils/src/cross/fs.rs b/utils/src/cross/fs.rs new file mode 100644 index 0000000..af15cd7 --- /dev/null +++ b/utils/src/cross/fs.rs @@ -0,0 +1,28 @@ +use std::fs; + +#[cfg(unix)] +use std::os::unix::fs::symlink as unix_symlink; +#[cfg(windows)] +use std::os::windows::fs::{symlink_dir, symlink_file}; + +#[cfg(windows)] +pub fn symlink(to_be_linked: String, destination: String) -> std::io::Result<()> { + let target_metadata = fs::metadata(&to_be_linked)?; + if target_metadata.is_dir() { + symlink_dir(&to_be_linked, &destination) + } else { + symlink_file(&to_be_linked, &destination) + }?; + Ok(()) +} + +#[cfg(unix)] +pub fn symlink(to_be_linked: String, destination: String) -> std::io::Result<()> { + unix_symlink(to_be_linked, destination)?; + Ok(()) +} + +pub fn hard_link(to_be_linked: String, destination: String) -> std::io::Result<()> { + fs::hard_link(to_be_linked, destination)?; + Ok(()) +} diff --git a/utils/src/cross/mod.rs b/utils/src/cross/mod.rs new file mode 100644 index 0000000..57f1fee --- /dev/null +++ b/utils/src/cross/mod.rs @@ -0,0 +1 @@ +pub mod fs; \ No newline at end of file diff --git a/utils/src/lib.rs b/utils/src/lib.rs index 2cfa076..7be51ae 100644 --- a/utils/src/lib.rs +++ b/utils/src/lib.rs @@ -3,3 +3,4 @@ pub mod commands; pub mod encoding; pub mod input; pub mod registry; +pub mod cross; \ No newline at end of file