AlexandreG has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I'm using SimpleZip to backup all my files/folders in a directory. I'm using the following code that working fine:

find( sub { eval {$zip->add($_, Name => $File::Find::name) or warn "Cannot add '$_' to zip file: $SimpleZipError\n" ;}; warn $@ if $@ }, $data);

The problem is as follow, this is how I understand it: When Find go through the folder it create the first pass for all files, then adding the files one by one in the Zip file. We have large files that will take time to be added to the Zip file, if during time the large file is getting add to the zip file, one of the temporary file get remove by another process, when it's time to add this remove file to the zip, all other files will failed. then I'm getting the following error:

Example:

Cannot add 'File1' to zip file: File 'File_that_Was_Remove' does not exist

Cannot add 'File2' to zip file: File 'File_that_Was_Remove' does not exist

Cannot add 'File3' to zip file: File 'File_that_Was_Remove' does not exist

It's like if the $_ value is correct but it report the error that it cannot find the file $File::Find::name (stuck on the file that was removed), then the script end. Only the files from the FIND order was added up to the file that was removed.

Hope that make sense.

I would like to skip the deleted file in the FIND order when it try to add the deleted file in the SimpleZip.

Thanks,

Alex

Replies are listed 'Best First'.
Re: SimpleZip If file doesn't exist
by haukex (Archbishop) on Aug 30, 2018 at 21:35 UTC

    You could add an extra -e check right before you add the file, although the race condition will still exist. The better way to handle this would be to not be working in a directory at the same time you're zipping it, or using some other mechanism to lock, or perhaps exclude these temporary files from being considered for zipping in the first place... anyway, here's an (untested) idea for a "quick fix" that might help with the symptoms you describe:

    find( sub { return unless -e $File::Find::name; eval { $zip->add($_, Name => $File::Find::name) or warn "Cannot add '$_' to zip file: $SimpleZipError\n"; 1 } or warn $@||'unknown error'; }, $data);

    Note I'm using the "safer" form of checking eval for errors, see Bug in eval in pre-5.14

Re: SimpleZip If file doesn't exist
by TheloniusMonk (Sexton) on Aug 31, 2018 at 11:42 UTC
    Dare I say it? If you need modules anyway, why not existing utilities instead? What about using tar jcf directory.tar.bz2 directory/ on linux or 7zip on Windows to back up your files? Or is it that the solution has to be in portable code?

    Update: My apologies: this won't work for linux, because tar also crashes when encountering the missing files it was given as input.

Re: SimpleZip If file doesn't exist
by AlexandreG (Initiate) on Aug 31, 2018 at 18:12 UTC

    Thank you for your help, the following is working fine:

    find( sub { eval {if (-e $File::Find::name) {$zip->add($_, Name => $File::Find::name) or warn "Cannot add '$_' to zip file: $SimpleZipError\n" ;}}; warn $@ if $@ }, $data);

    Alex