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

I write a cron job to download a DVD iso file (about 2G) from a ftp site every day. But the creat of the iso file need a lot of time so I need to check the file is integrated. The meothod I use is that check the size of the file and sleep 10s then check the size of the file secondly. If the size of the file is not changed, the iso file is integrated. I feel my method is not elegant. Is there better ways to solve the program?
Thanks!

Replies are listed 'Best First'.
Re: Insure the file is not be wringting
by Corion (Patriarch) on Jan 02, 2006 at 10:30 UTC

    Your method is one way. I would take a different way and have the .iso creation process also create a second file, .iso.ok, which tells you that the .iso has been created completely. Another way would be to create the .iso in a second directory and then rename it into the pickup directory.

Re: Insure the file is not be wringting
by tirwhan (Abbot) on Jan 02, 2006 at 10:54 UTC

    If you also have an ssh or telnet login to the remote site you could use the UNIX lsof command to see whether the file is open for writing by a specific process. For example, say your file is /tmp/test.iso, you could run

    lsof /tmp/test.iso
    Which would give something like this:
    COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME isowrite 12533 root 3w REG 254,1 0 235 /tmp/test.iso

    The FD entry shows you that the file is opened for writing by the isowrite process.

    Not the most elegant method either (Corions solution is better, but requires that you have some control over the iso-creating process), but more reliable than checking for size change.


    A computer is a state machine. Threads are for people who can't program state machines. -- Alan Cox
Re: Insure the file is not be wringting
by graff (Chancellor) on Jan 02, 2006 at 19:06 UTC
    Bear in mind that it is possible for file data to change without the file size changing; for example, some data fields in the initial "root blocks" of the iso file might be filled in as a final step, after the full extent of data files have been written. So rather than checking file size, it might be better to check the modification time (using  -M $isofilename).

    But as others have pointed out, the best solution is to make a slight change in the process that creates the iso file: the directory or name of the file being created should be different from the directory or name used to download the file; just add a step after the "mkisofs" or whatever, to rename/move the completed file so that the download process will only find it after it's finished.