tags:

views:

45

answers:

2

In my project I have a file uploading feature. Files are uploaded via FTP. I need to configure a listener that will check for new files and invoke a script only when file uploading is finished. Because if I run this script immediately after detecting the new file, it can start to process file that is not completely uploaded, which will cause an error. Can anybody tell if this is possible on LINUX and how can I do this?

+5  A: 

I'd try using inotify, event code IN_CLOSE_WRITE.

Joril
+1 , but its worth mentioning that some ftp servers create a hidden temporary file to receive data until the xfer is finished. I.e. `.foo-ftpupload-2837948723` in the same upload path as the target file , so you'll need to make sure your event loop can deal with that and not trigger the task until the file has actually been renamed.
Tim Post
@Tim Post Good point.vsftp for example does not do this
Pete
Thank you for suggestions. I tried inotify, but I found that it fires IN_CLOSE_WRITE in situation when file is not completely uploaded, but connection is lost (for example if I stop uploading or close FTP client). So I believe there is no way of detecting file COMPLETELY uploaded rather than using HTTP. Is this correct?
duke84
+1  A: 

Have a look at inotify

It doesn't automatically watch sub directories though, so if you need to monitor many ftp accounts (or the FTP client wants to create a sub dir and upload there) you'll need to handle this yourself.

Pete
Correct. A breadth-first search (similar to `ftw()`, but breadth-first) is recommended for whatever daemon is doing the watching so no sub directories are missed as it starts.
Tim Post