feat: the start of cross for boxutils
This commit is contained in:
parent
f4260b668e
commit
675c4c77c3
4 changed files with 34 additions and 23 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
#[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)
|
||||
};
|
||||
let symlink_result = boxutils::cross::fs::symlink(to_be_linked, destination);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
28
utils/src/cross/fs.rs
Normal file
28
utils/src/cross/fs.rs
Normal 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
1
utils/src/cross/mod.rs
Normal file
|
|
@ -0,0 +1 @@
|
|||
pub mod fs;
|
||||
|
|
@ -3,3 +3,4 @@ pub mod commands;
|
|||
pub mod encoding;
|
||||
pub mod input;
|
||||
pub mod registry;
|
||||
pub mod cross;
|
||||
Loading…
Add table
Add a link
Reference in a new issue