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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.