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

Hello Wise Monks!

I'm using a Perl application to download files from my server every 5 minutes. My question is, what happens if my Perl application downloads a file that is being modified at the time of download?

Thanks in advance for your wisdom.

Gorby

Replies are listed 'Best First'.
Re: Downloading using FTP
by Abigail-II (Bishop) on May 10, 2004 at 09:01 UTC
    You probably get a damaged file, but to be sure, you need to know how your FTP server deals with such a case. I don't know any FTP server that does, but it is technically possible to write an FTP server that doesn't serve a file that is open for writing. Or an FTP server could first try to lock a file - if the modifying program locks before updating as well, you're sure the downloaded file isn't mangled.

    However, none of this can be influenced by the downloading program. The client doesn't know about files being modified.

    Abigail

Re: Downloading using FTP
by tachyon (Chancellor) on May 10, 2004 at 09:46 UTC

    The file will probably be corrupt. To solve such issues one option is to make a copy of a file to be downloaded in a safe and atomic manner, then download the copy. For example you might run a cron job to do the copy using flock locally on the server. Copying takes a finite quantum of time so you would typically use a rename strategy to make a file 'appear' in a given dir in an atomic fashion and avoid the race condition which occurs while you are writing the copy.

    cheers

    tachyon

Re: Downloading using FTP
by nimdokk (Vicar) on May 10, 2004 at 11:58 UTC
    What you could also do would be (if its within your control) have the application that is creating the file touch some sort of control file that would indicate when it is done writing the file. Your FTP could then look for the presence of that file and assume that the file you want is finished being written. Just another way to do it, as someone else mentioned, you can also have your script check the file size to make sure it has stopped growing.

    What happens if something needs to write into a file after you have started downloading it but are not yet done downloading the file?

Re: Downloading using FTP
by exussum0 (Vicar) on May 10, 2004 at 11:07 UTC
Re: Downloading using FTP
by TwistedGreen (Sexton) on May 10, 2004 at 09:00 UTC
    Check out flock for more information on this. flock will try to lock a file. It will fail if it can't, meaning the file is in use or otherwise inaccessible.

    -peter

    Edit: As Abigail-II pointed out, this won't really help you over FTP. I should've read your question more thoroughly, but flock is a place to start.
      Check out flock for more information on this. flock will try to lock a file.
      Unfortunally, you can't lock a file over FTP. Technically, you can write an FTP server that flocks files, but that's done at the server side, not the client side.

      Abigail