in reply to How to sync a UNC path to a USB memory stick in Windows?
Been a while. The Briefcase isn't quite working well. The Briefcase is designed to be a 2-way sync and it also seems to be reporting some files with identical modification times as both having been updated separately.
The main person who coordinates the source files is also leery of one of the users accidentally modifying the files on his stick. While the execs aren't supposed to be able to write back to the source stuff, she's a little leery of it as the permissions are tricky.
As a result, I've gone ahead and re-written something from scratch. I use File::Compare to be sure that the files are indeed different or identical. I eschew using the modificaiton date because this more cumbersome process will detect corruption in the mirrored data. However that's a lot of overhead. Is there another way I could do it at least as quickly and with the same robustness? For example I could check modification time FIRST (some files presumably would fail this quicker test and thus the contents comparison would be moot), and that way I would only be comparing the files that didn't really change, probably (which would still be the majority). Also, would something like an MD5 sum be a good way to check difference without reading every single one into memory (ie be 99.9% as robust but faster)? Here's my code so far:
Thoughts? T.use strict; use File::Copy; use File::Find; use File::Compare; my $source="G:\\contingency\\"; my $dest="D:\\"; (-r $source) || die "Error: $source not readable\n"; (-e $dest) || die "Error: $dest (memory stick) does not exist--is you +r memory stick attached?\n"; # for the convenience of &wanted calls, including -eval statements: use vars qw/*name *dir *prune/; *name = *File::Find::name; *dir = *File::Find::dir; *prune = *File::Find::prune; sub wanted{}; sub wanted2{}; my $newdirs; my $newfiles; my $changedfiles; my $unchangedfiles; my $orphans; #Freshen up memory stick my $status1=find(\&wanted,$source); #..and then take care of orphans my $status1=finddepth(\&wanted2,$dest); my $totalfiles=$newdirs+$newfiles+$changedfiles+$unchangedfiles+$orpha +ns; print "Summary report: \n"; print "$totalfiles scanned.\n"; print "$newdirs new directories found and created.\n"; print "$newfiles new files found and copied.\n"; print "$changedfiles updated files copied.\n"; print "$unchangedfiles unchanged files ignored.\n"; print "$orphans orphans deleted from memory stick.\n"; sub wanted{ $dest=$name; $dest =~ s/G:/D:/; #sleep 3; if (-d $name){ print "$name\n"; if ( not -e $dest){ print "Making directory $dest: \n"; mkdir $dest; ++$newdirs; } } else{ print "comparing $name to: \n$dest\n....\n"; my $status=compare($name, $dest); #print "status of compare is $status.\n"; unless (!$status){ print "copying $name to memory stick \n($dest)\n......\n"; my $cstatus=copy($name, $dest); #print "status of copy is $cstatus.\n"; #copy returns 1 on success ($cstatus) || die "Copy failed with status $cstatus: $!\n"; if ($status==-1){ print "File is new on contingency site since last update.\n(compar +e returned $status: $!\n"; ++$newfiles; }else{ print "Version of file on memory stick was different(stale or alte +red).\n"; ++$changedfiles; } }else{ print "files are identical.\n"; ++$unchangedfiles; } } } sub wanted2{ my $status=1; $source=$name; $source =~ s/D:/G:/; if ( not -e $source){ ++$orphans; print "$name is an orphan"; if (-d $name){ $status=rmdir $name; }else{ $status=unlink $name; } #rmidr returns true on success; unlink returns number of files del +eted (should be 1) ($status) || warn "WARNING: Status of removal of $name is $status + : $!\n"; } }
_________________________________________________________________________________
I like computer programming because it's like Legos for the mind.
|
|---|