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

Hi all,

I am looking for the best way for checking when a tar file has finished his filling before open it. The Tar is created by another process. I need to find all tar files and untar all but only if the file has ended his creation.

I am using this way:

... while (1) { ... for my $tgz ( <*.tgz> ) { # wait 1 min for safety if Tar file would not finished the fill next unless time - (stat($tgz))[9] > 60; my @file = Archive::Tar->extract_archive($tgz, 1); ...

advices for a best way?

Thanks!

  • Comment on Best way for testing the end of the filling of a Tar file before opening it.
  • Download Code

Replies are listed 'Best First'.
Re: Best way for testing the end of the filling of a Tar file before opening it.
by roboticus (Chancellor) on Feb 06, 2014 at 14:05 UTC

    dirac:

    The strategy we use is to create the tar file with a temporary name (e.g. with an extension of .tmptgz). Then when the tar file is completed, we rename it into the final name. Processes that consume the tar file look only for files whose name matches the final name template.

    Occasionally, we'll have to work with processes that don't use that strategy. In those cases you can either wait for the last modified date to be old enough that "it should be complete now", or you could dredge through lsof output to see if anyone has the file open.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Best way for testing the end of the filling of a Tar file before opening it.
by BrowserUk (Patriarch) on Feb 07, 2014 at 00:33 UTC

    I think I would do something like this:

    while( my $file = getNextFilename() ) { my $size = -s $file; sleep 1; next if -s( $file ) != $size; ## Pick it up next time around. ## process complete files }

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Best way for testing the end of the filling of a Tar file before opening it.
by oiskuu (Hermit) on Feb 06, 2014 at 23:58 UTC

    You could try some advisory locking, flock or a lockfile, but the canonical solution is to rename a temp. file once it is complete.

    The other process might create the archive in its build directory, then move it to queue directory. Unpacker process would periodically process the queue.

Re: Best way for testing the end of the filling of a Tar file before opening it.
by jellisii2 (Hermit) on Feb 06, 2014 at 20:09 UTC
    Does extract_archive work if the archive is still being written to? If so, you can always daemonize the process and just check (stat($file))[9] and see if it's older than your current lap. 7 (size) would work too, assuming the archive gets written all at once. stat