feat: the start of cross for boxutils

This commit is contained in:
Teesh 2025-03-30 11:36:28 +03:00
parent f4260b668e
commit 675c4c77c3
4 changed files with 34 additions and 23 deletions

View file

@ -1,10 +1,6 @@
use boxutils::args::ArgParser; use boxutils::args::ArgParser;
use boxutils::commands::Command; use boxutils::commands::Command;
use std::fs; 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; use std::path::Path;
pub struct Ln; pub struct Ln;
@ -58,28 +54,13 @@ impl Command for Ln {
} }
if args.get_flag("-s") { if args.get_flag("-s") {
#[cfg(unix)] let symlink_result = boxutils::cross::fs::symlink(to_be_linked, destination);
{
if let Err(e) = symlink(&to_be_linked, &destination) {
eprintln!("ln: failed to create symlink: {}", e);
}
}
#[cfg(windows)] if let Err(e) = symlink_result {
{ eprintln!("ln: failed to create symlink: {}", e);
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);
}
} }
} else { } 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); eprintln!("ln: failed to create hard link: {}", e);
} }
} }

28
utils/src/cross/fs.rs Normal file
View file

@ -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(())
}

1
utils/src/cross/mod.rs Normal file
View file

@ -0,0 +1 @@
pub mod fs;

View file

@ -3,3 +3,4 @@ pub mod commands;
pub mod encoding; pub mod encoding;
pub mod input; pub mod input;
pub mod registry; pub mod registry;
pub mod cross;