Skip to content

Commit c7b2083

Browse files
committed
try to copy the file if doesn't exist
1 parent 4ba5f52 commit c7b2083

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

cp-arch-test.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
set -e
2-
rm -rf dir
2+
3+
4+
#rm -rf a b c d; mkdir a b c d; ln -s ../c a; ln -s ../c b;
5+
#cp -RL a b d
6+
#./target/debug/coreutils cp cp -RL a b d
7+
# cp: error: Option 'dereference' not yet implemented.
8+
#rm -rf a b c d
9+
10+
rm -rf dir dir-copy-archive-rust
311
mkdir dir
412
echo "a" > dir/1
513
echo "b" > dir/2

src/uu/cp/src/cp.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,7 @@ fn copy(sources: &[Source], target: &Target, options: &Options) -> CopyResult<()
825825
let mut non_fatal_errors = false;
826826
let mut seen_sources = HashSet::with_capacity(sources.len());
827827
for source in sources {
828+
println!("processing {}", source.display());
828829
if seen_sources.contains(source) {
829830
show_warning!("source '{}' specified more than once", source.display());
830831
} else {
@@ -914,6 +915,11 @@ fn adjust_canonicalization<'a>(p: &'a Path) -> Cow<'a, Path> {
914915
}
915916
}
916917

918+
919+
fn is_symlink(path: &Path) -> bool {
920+
fs::symlink_metadata(path).unwrap().file_type().is_symlink()
921+
}
922+
917923
/// Read the contents of the directory `root` and recursively copy the
918924
/// contents to `target`.
919925
///
@@ -945,7 +951,7 @@ fn copy_directory(root: &Path, target: &Target, options: &Options) -> CopyResult
945951
#[cfg(any(windows, target_os = "redox"))]
946952
let mut hard_links: Vec<(String, u64)> = vec![];
947953

948-
for path in WalkDir::new(root) {
954+
for path in WalkDir::new(root).sort_by(|a, b| is_symlink(a.path()).cmp(&is_symlink(b.path()))) {
949955
let p = or_continue!(path);
950956
let is_symlink = fs::symlink_metadata(p.path())?.file_type().is_symlink();
951957
let path = if options.no_dereference && is_symlink {
@@ -957,7 +963,7 @@ fn copy_directory(root: &Path, target: &Target, options: &Options) -> CopyResult
957963
} else {
958964
or_continue!(p.path().canonicalize())
959965
};
960-
966+
println!("processing 2 {}", path.display());
961967
let local_to_root_parent = match root_parent {
962968
Some(parent) => {
963969
#[cfg(windows)]
@@ -1067,17 +1073,16 @@ fn copy_attribute(source: &Path, dest: &Path, attribute: &Attribute) -> CopyResu
10671073

10681074
#[cfg(not(windows))]
10691075
fn symlink_file(source: &Path, dest: &Path, context: &str) -> CopyResult<()> {
1070-
println!("dans symlink: {:?} / {:?} / {:?}", source.display(), dest.display(), context);
1076+
Ok(std::os::unix::fs::symlink(source, dest).context(context)?)
1077+
/*println!("dans symlink: {:?} / {:?} / {:?}", source.display(), dest.display(), context);
10711078
match std::os::unix::fs::symlink(source, dest).context(context) {
10721079
Ok(data) => {
1073-
println!("c'est ok");
10741080
Ok(())
10751081
}
10761082
Err(err) => {
1077-
println!("ignore error");
10781083
Ok(())
10791084
}
1080-
}
1085+
}*/
10811086
//Ok(std::os::unix::fs::symlink(source, dest).context(context)?)
10821087
}
10831088

@@ -1243,7 +1248,7 @@ fn copy_helper(source: &Path, dest: &Path, options: &Options) -> CopyResult<()>
12431248
} else if options.no_dereference && fs::symlink_metadata(&source)?.file_type().is_symlink() {
12441249

12451250
// Here, we will copy the symlink itself (actually, just recreate it)
1246-
let link = fs::read_link(&source)?;
1251+
let link_target = fs::read_link(&source)?;
12471252

12481253
let dest: Cow<'_, Path> = if dest.is_dir() {
12491254
match source.file_name() {
@@ -1257,8 +1262,20 @@ fn copy_helper(source: &Path, dest: &Path, options: &Options) -> CopyResult<()>
12571262
} else {
12581263
dest.into()
12591264
};
1260-
// le pb est que le symlink doit toujours etre cree
1261-
symlink_file(&link, &dest, &*context_for(&link, &dest))?;
1265+
// Here, when we are in this mode, we should copy the file too
1266+
// and add it to the list
1267+
1268+
println!("source {}", source.display());
1269+
println!("dest {}", dest.display());
1270+
println!("link_target {}", link_target.display());
1271+
1272+
if ! link_target.exists() {
1273+
println!("copy le fichier origin {} {}", link_target.display(), dest.display());
1274+
// The target of the symlink doesn't exist, let's copy it before
1275+
fs::copy(&link_target, &dest).context(&*context_for(&link_target.clone(), &dest))?;
1276+
// TODO: make sure we don't copy it twice
1277+
}
1278+
symlink_file(&link_target, &dest, &*context_for(&link_target, &dest))?;
12621279
} else {
12631280
fs::copy(source, dest).context(&*context_for(source, dest))?;
12641281
}

0 commit comments

Comments
 (0)