Ftp – Ensure that a via ftp uploaded file is complete

daemonftp

I want to write a script that runs on a server and looks for a configuration file every x seconds and starts or kills other scripts depending on the data in the configuration file. My question now is: When somebody uploads this configuration file via ftp could it be that my script just gets a part of this configuration data if the upload is not jet finished. How can I ensure that I never get a broken (= not yet complete uploaded) configuration file?

Best Answer

It may well be possible to get a broken file, especially if they're quite large. If they're really small (a few lines) it's not very likely. You have to think about what happens if you read it while a new one is being uploaded, or if something interrupts the upload.

One way of dealing with this kind of thing is to upload the file to a temporary name, then rename it to replace the real one. That way the real one is never incomplete.

While you can do this from the client, it's a bit inconvenient if the uploads are manual. (great if they're automatic though).

Quite a few FTP servers will take care of this for you, though.

For example ProFTPD has the configuration option HiddenStores:

http://www.proftpd.org/docs/directives/linked/config_ref_HiddenStores.html

NcFTPd has something similar:

http://www.ncftp.com/ncftpd/doc/config/g/upload-tmp-then-rename.html

Have a look through the manual for whichever FTP server you're using.