lumia

Archive checksum manager
git clone git://lumidify.org/git/lumia.git
Log | Files | Refs

commit 5731b0ab242ae2a798fa9b0380d76f7fa7b9c927
parent a964a391d65fce7963a55c9964bbbe6a5520a2ff
Author: lumidify <nobody@lumidify.org>
Date:   Mon, 23 Mar 2020 15:56:26 +0100

Cleanup; switch to using system mv command

Diffstat:
Mlumia.pl | 46+++++++++++++---------------------------------
1 file changed, 13 insertions(+), 33 deletions(-)

diff --git a/lumia.pl b/lumia.pl @@ -6,7 +6,6 @@ # FIXME: handle rm, etc. on .lumidify* files # FIXME: ignore all except for a certain file/folder # FIXME: store modified date and checksum filed with changed date -# FIXME: sha256 works with leading zeroes, maybe switch? # FIXME: allow different hash types # FIXME: don't write anything if cksum fails (will create malformed line) # FIXME: add option to just check dir structure or maybe check if everything exists @@ -14,16 +13,10 @@ use strict; use warnings; -use File::Spec::Functions qw(catfile catdir splitpath splitdir abs2rel); +use File::Spec::Functions qw(catfile abs2rel); use File::Basename qw(basename dirname); -use File::Copy qw(move copy); -use File::Path qw(remove_tree make_path); +use File::Path qw(remove_tree); use String::ShellQuote; -use Cwd qw(realpath); -use POSIX qw(SIGINT); -use Data::Dumper; -use Scalar::Util qw(looks_like_number); -use Getopt::Long; # the file used to store checksums for files my $CKSUM_FILE = ".lumidify_archive_cksums"; @@ -512,6 +505,7 @@ sub sort_by_dir { return \%sorted_files; } +# FIXME: handle different cases like move_files # copies the $src files to $dst and updates the checksums in $dst # $src: list of source paths # $dst: destination directory or file (in latter case only one src is allowed) @@ -560,14 +554,7 @@ sub copy_files { write_cksums $dst_dir, $dst_cksums, $files_touched, $dirs_touched; } -# return whether the two paths are the same -sub cmp_path { - my ($src, $dst) = @_; - my $src_real = realpath $src; - return defined $src_real && $src_real eq realpath $dst; -} - -# move a file from $src to $dst, prompting for confirmation if $dst already exists +# move a file (or directory) from $src to $dst, prompting for confirmation if $dst already exists; # automatically appends the basename of $src to $dst if $dst is a directory sub move_file { my ($src, $dst) = @_; @@ -575,15 +562,15 @@ sub move_file { $dst .= "/" . basename($src); } if (-e $dst) { - warn "WARNING: \"$dst\" exists already. Do you want to replace it?"; + 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") { - return "Not moving \"$src\" to \"$dst\"."; + warn "Not moving \"$src\" to \"$dst\"\n"; + return 1; } } - move($src, $dst) or return "ERROR: can't move \"$src\" to \"$dst\": $!"; - return 0; + return system("mv", $src, $dst); } # move all files/directories in $src_files from $src_dir to $dst_dir ($src_files @@ -597,10 +584,6 @@ sub move_from_same_dir { my $dirs_touched = 0; foreach my $src_file (@$src_files) { my $fullpath = "$src_dir/$src_file"; - if (cmp_path($fullpath, $dst_dir)) { - warn "ERROR: can't move \"$fullpath\" into \"$dst_dir\" (same dir)\n"; - next; - } my $tmp_dirs_touched = 0; my $tmp_files_touched = 0; if (-d $fullpath) { @@ -608,10 +591,9 @@ sub move_from_same_dir { } else { $tmp_files_touched = 1; } - if (my $err = move_file($fullpath, $dst_dir)) { - warn "$err\n"; - next; - } + + next if move_file($fullpath, $dst_dir); + # need to be able to check if the path is a directory # before actually moving it $dirs_touched ||= $tmp_dirs_touched; @@ -648,10 +630,8 @@ sub move_rename { return if !defined $dst_cksums; } - if (my $err = move_file($src, $dst)) { - warn "$err\n"; - return; - } + return if move_file($src, $dst); + my $files_touched = 0; my $dirs_touched = 0; if (-d $src) {