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?
相关问题
- Is shmid returned by shmget() unique across proces
- how to get running process information in java?
- Error building gcc 4.8.3 from source: libstdc++.so
- Why should we check WIFEXITED after wait in order
- Null-terminated string, opening file for reading
I have used the HiddenStores feature of Proftpd. It keeps in-transit files hidden by prefixing them with a .in.filename.ext until they have finished uploading. Your process can then safely list the directories for completed files.
http://www.proftpd.org/docs/directives/linked/config_ref_HiddenStores.html
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.
I was looking for the same thing and stumbled on pureftpd which has an upload script feature. Sounds like exactly what was needed. Found the details here: http://www.linuxbyexamples.net/2012/10/config-ftp-server-trigger-upload-file-to-call-external-script.html
Apache "Mina" ftp server (java) might be able to do what you want, including detecting a failed upload, as mentioned here
Quote:
Ftplet overview here, including response codes.
The afterCommand method signature:
You'd check
reply.getCode()
in your overriden method. You should subclassDefaultFtplet
rather than implementingFtplet
interface from scratch.Note that
DefaultFtplet::afterCommand
shows how to detect what client command is being responded to. You can check forSTOR
orSTOU
and reply-code426
or551
to detect failed uploads.However, this may not detect an upload intentionally terminated by the client, if the client app decides to treat the transfer as though the file was just shorter than it is. In the case of a unintentionally broken connection, I think the reply-code check will work. A test could be to kill the client app, or bring down the client machine's network interface.
To handle successful uploads (your original question), you can look for the success reply-code instead, ie
226
.I'd try using inotify, event code IN_CLOSE_WRITE.