commit 041b675684a7f17f2fb7ae14af0be4d53ee3aba9
parent 5731b0ab242ae2a798fa9b0380d76f7fa7b9c927
Author: lumidify <nobody@lumidify.org>
Date:   Mon, 23 Mar 2020 16:48:55 +0100
Modify copy_files to copy each source file/dir individually
Diffstat:
6 files changed, 39 insertions(+), 16 deletions(-)
diff --git a/lumia.pl b/lumia.pl
@@ -505,6 +505,27 @@ sub sort_by_dir {
 	return \%sorted_files;
 }
 
+# check if $dst exists and prompt the user whether it should be overwritten
+# returns 0 if it can be overwritten or doesn't exist, 1 if it shouldn't be overwritten
+sub prompt_overwrite {
+	my $dst = shift;
+	if (-e $dst) {
+		print STDERR "WARNING: \"$dst\" exists already. Do you want to replace it? (y/n) ";
+		my $choice = "";
+		while ($choice ne "y" && $choice ne "n") {
+			$choice = <STDIN>;
+			chomp $choice;
+		}
+		if ($choice eq "n") {
+			warn "Not overwriting \"$dst\"\n";
+			return 1;
+		} else {
+			return 0;
+		}
+	}
+	return 0;
+}
+
 # FIXME: handle different cases like move_files
 # copies the $src files to $dst and updates the checksums in $dst
 # $src: list of source paths
@@ -516,6 +537,8 @@ sub copy_files {
 		$dst_dir = dirname $dst;
 	}
 	my $diff_name = 0;
+	# check if the file/dir is getting a different name or
+	# just being copied into a different directory
 	if (!-d $dst && !-d $src->[0]) {
 		$diff_name = 1;
 	}
@@ -524,9 +547,6 @@ sub copy_files {
 	}
 	my $dst_cksums = read_cksums $dst_dir;
 	return if !defined $dst_cksums;
-	if (system("cp", "-aiv", @$src, $dst)) {
-		die "ERROR while copying files\n";
-	}
 	my $src_sorted = sort_by_dir(@$src);
 	my $files_touched = 0;
 	my $dirs_touched = 0;
@@ -535,11 +555,17 @@ sub copy_files {
 		next if !defined $src_cksums;
 		foreach my $src_file (@{$src_sorted->{$src_dir}}) {
 			my $src_path = "$src_dir/$src_file";
+
+			my $dst_path = $diff_name ? $dst : "$dst_dir/$src_file";
+			next if prompt_overwrite($dst_path);
+			next if system("cp", "-av", $src_path, $dst);
+
 			if (-d $src_path) {
 				$dirs_touched = 1;
 			} else {
 				$files_touched = 1;
 			}
+
 			if (exists $src_cksums->{$src_file}) {
 				if ($diff_name) {
 					$dst_cksums->{basename $dst} = $src_cksums->{$src_file};
@@ -561,15 +587,7 @@ sub move_file {
 	if (-d $dst) {
 		$dst .= "/" . basename($src);
 	}
-	if (-e $dst) {
-		print STDERR "WARNING: \"$dst\" exists already. Do you want to replace it? (y/n) ";
-		my $choice = <STDIN>;
-		chomp $choice;
-		if ($choice ne "y" && $choice ne "Y") {
-			warn "Not moving \"$src\" to \"$dst\"\n";
-			return 1;
-		}
-	}
+	return 1 if prompt_overwrite($dst);
 	return system("mv", $src, $dst);
 }
 
@@ -837,9 +855,10 @@ if ($ARGV[0] eq "mv") {
 	remove_old_files $dir;
 } elsif ($ARGV[0] eq "check") {
 	if ($#ARGV < 1) {
-		die "At least one path required";
+		check_files ".";
+	} else {
+		check_files @ARGV[1..$#ARGV];
 	}
-	check_files @ARGV[1..$#ARGV];
 } elsif ($ARGV[0] eq "clean") {
 	my $dir = ".";
 	if ($#ARGV > 0) {
diff --git a/test/.lumidify_archive_cksums b/test/.lumidify_archive_cksums
@@ -1,2 +1,4 @@
+2418082923 2 "bob1"
+2418082923 2 "bob3"
+4294967295 0 "bob2"
 4294967295 0 "fred2"
-4294967295 0 "bob1"
diff --git a/test/.lumidify_archive_cksums.cksum b/test/.lumidify_archive_cksums.cksum
@@ -1,2 +1,2 @@
-2507213385 41 ".lumidify_archive_cksums"
 3971863640 21 ".lumidify_archive_dirs"
+1556400735 81 ".lumidify_archive_cksums"
diff --git a/test/bob1 b/test/bob1
@@ -0,0 +1 @@
+a
diff --git a/test/bob1 b/test/bob2
diff --git a/test/bob3 b/test/bob3
@@ -0,0 +1 @@
+a