Jeremy Hinegardner
4/30/2009 5:14:00 AM
On Wed, Apr 29, 2009 at 01:32:24AM +0900, Joel VanderWerf wrote:
> Greg Willits wrote:
>> I want to use Ruby to periodically move files out of an FTP upload
>> folder to another location. Before I move any one file, I need to know
>> that it is not in the middle of being uploaded.
>> Other than doing something really barbaric like check the file size,
>> wait some time period and check the size again, I can't figure out how
>> Ruby would know if a file not in use by Ruby itself is "busy."
>
> As long as the place you are moving the file to is on the same partition,
> why can't you just move it and let the upload proceed? If the writer has an
> open file handle, it will keep writing to the same file. Of course, if the
> writer opens a new file handle each time data comes in, then this won't
> work. And maybe you don't want new writes after the file is moved, because
> of your application logic. So probably Robert Klemme's suggestion to use
> fuser is apt.
A few other options:
1) If you control the ftp upload, simply upload to a temporary name, upon
completion of the upload, submit an ftp rename/move command to rename the
temprorary name to the final name.
On the directory monitoring side, only look for files with the the final name
pattern. Since a mv is generally an atomic operation (so long as the move
is on the same file system) then this is probably the best solution.
2) Use the transfer log of your ftp server and monitor that for when a file up
load is complete. This assumes that the appropriate log message is written
after the uploaded file is complete, and that you are logging.
3) See if your ftp server has a 'post upload hook' or some method of executing a
predefined script upon upload completion. A quick google on this one turned
up PureFTP has as '--with-uploadscript' compilation option that allows
something like this.
enjoy,
-jeremy
--
========================================================================
Jeremy Hinegardner jeremy@hinegardner.org