I would recommend renaming both files
before you start. If the ftp server is actively
writing data to the files, they will continue
writing to the files. If the ftp client
starts sending a new file after the rename(),
it won't overwrite the renamed files.
You can then loop for some reasonable amount
of time, and if the mtime doesn't change, you
can guess that the ftp server isn't writing
to them anymore.
Something like:
if ( -f $control_file && -f $data_file ) {
rename($data_file,$data_file. $$);
rename($control_file,$control_file . $$);
} else {
exit;
}
while (1) {
$mtime=(stat($data_file . $$))[9];
# drop out of the loop if the file hasn't
# changed in 5 minutes (perhaps longer
# for a wan connection ?)
last if (time > $mtime + 300);
sleep(30);
}
#process the files
This still leaves a small race condition between
the two rename(s) in which the control file might
have been overwritten. (a real small timeslice, but
a timeslice nonetheless...)
You mentioned you didn't have control over the process,
but if you ever do get control, here's two ideas:
- Have the ftp client send the files as file.tmp, then
use the "rename file.tmp file" ftp command when the file is
sent.
- Lacking that, install an ftp server that does something
similar. proftpd has
a configuration parameter called HiddenStor that does this.
Update: yep..I missed a sleep in the lower loop,
which would burn lots of resources...fixed
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|