diff options
author | Kamiyaa <jeff.no.zhao@gmail.com> | 2020-05-26 17:01:40 -0400 |
---|---|---|
committer | Kamiyaa <jeff.no.zhao@gmail.com> | 2020-05-26 17:01:40 -0400 |
commit | 0a18ffbf9242f47a7c3a0bb56a9f84804ee7dcf5 (patch) | |
tree | 4a523ad75791171adea121a0acce200f73cb9ca4 /src/commands | |
parent | 95bc120687723a8c9e525b7448c5fd71235a329e (diff) |
fix cut/copy symlinks
Diffstat (limited to 'src/commands')
-rw-r--r-- | src/commands/file_ops/paste_copy.rs | 13 | ||||
-rw-r--r-- | src/commands/file_ops/paste_cut.rs | 31 |
2 files changed, 28 insertions, 16 deletions
diff --git a/src/commands/file_ops/paste_copy.rs b/src/commands/file_ops/paste_copy.rs index 46bafbc..5fad0e4 100644 --- a/src/commands/file_ops/paste_copy.rs +++ b/src/commands/file_ops/paste_copy.rs @@ -15,9 +15,8 @@ pub fn recursive_copy(dest: &Path, src: &Path, options: &Options) -> std::io::Re dest_buf.push(s); } rename_filename_conflict(&mut dest_buf); - if !src.is_dir() { - std::fs::copy(src, dest_buf) - } else { + let file_type = fs::symlink_metadata(src)?.file_type(); + if file_type.is_dir() { fs::create_dir(dest_buf.as_path())?; let mut total = 0; for entry in fs::read_dir(src)? { @@ -26,6 +25,14 @@ pub fn recursive_copy(dest: &Path, src: &Path, options: &Options) -> std::io::Re total += recursive_copy(dest_buf.as_path(), entry_path.as_path(), options)?; } Ok(total) + } else if file_type.is_file() { + fs::copy(src, dest_buf) + } else if file_type.is_symlink() { + let link_path = fs::read_link(src)?; + std::os::unix::fs::symlink(link_path, dest_buf)?; + Ok(0) + } else { + Ok(0) } } diff --git a/src/commands/file_ops/paste_cut.rs b/src/commands/file_ops/paste_cut.rs index 4fa171d..6ae1569 100644 --- a/src/commands/file_ops/paste_cut.rs +++ b/src/commands/file_ops/paste_cut.rs @@ -15,22 +15,14 @@ pub fn recursive_cut(dest: &Path, src: &Path, options: &Options) -> std::io::Res dest_buf.push(s); } rename_filename_conflict(&mut dest_buf); - if !src.is_dir() { - let metadata = src.metadata()?; - if fs::rename(src, dest_buf.as_path()).is_err() { - fs::copy(src, dest_buf.as_path())?; - fs::remove_file(src)?; - } - Ok(metadata.len()) - } else { + let metadata = fs::symlink_metadata(src)?; + let file_type = metadata.file_type(); + if file_type.is_dir() { match fs::rename(src, dest_buf.as_path()) { - Ok(_) => { - let metadata = dest_buf.metadata()?; - Ok(metadata.len()) - } + Ok(_) => Ok(metadata.len()), Err(_) => { - fs::create_dir(dest_buf.as_path())?; let mut total = 0; + fs::create_dir(dest_buf.as_path())?; for entry in fs::read_dir(src)? { let entry = entry?; let entry_path = entry.path(); @@ -40,6 +32,19 @@ pub fn recursive_cut(dest: &Path, src: &Path, options: &Options) -> std::io::Res Ok(total) } } + } else if file_type.is_file() { + if fs::rename(src, dest_buf.as_path()).is_err() { + fs::copy(src, dest_buf.as_path())?; + fs::remove_file(src)?; + } + Ok(metadata.len()) + } else if file_type.is_symlink() { + let link_path = fs::read_link(src)?; + std::os::unix::fs::symlink(link_path, dest_buf)?; + fs::remove_file(src)?; + Ok(metadata.len()) + } else { + Ok(0) } } |