471,355 Members | 1,632 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,355 software developers and data experts.

Drop folder and race conditions

I have a need to implement a drop folder upload mechanism for secure uploading
of files to a server. At first glance this appears that it would be an easy
application to write. Then I begin to think about the race conditions that
exist between the process that will wake up to upload the files and the fact
that the user can add additional files to the drop folder at any point in time.
I would like to clear out files/folders after they have been processed but it is
possible that while the background process was uploading files, the user added
additional files/folders to the drop folder that make the deletion of folders
impossible. This project has become significantly more complex than it appeared
at first. Anyone out there have any "sage" advice on how to tackle this beast?

Thanks in advance for any pointers.

Regards,
Larry Bates
Oct 9 '07 #1
1 1414
On Tue, 09 Oct 2007 07:05:57 -0500, Larry Bates wrote:
I have a need to implement a drop folder upload mechanism for secure
uploading of files to a server. At first glance this appears that it
would be an easy application to write. Then I begin to think about the
race conditions that exist between the process that will wake up to
upload the files and the fact that the user can add additional files to
the drop folder at any point in time. I would like to clear out
files/folders after they have been processed but it is possible that
while the background process was uploading files, the user added
additional files/folders to the drop folder that make the deletion of
folders impossible. This project has become significantly more complex
than it appeared at first. Anyone out there have any "sage" advice on
how to tackle this beast?

Off the top of my head...

Recursively copy the contents of each folder to the server, deepest
first, deleting each file as it's copied. Then delete the folder as soon
as you've emptied it (but naturally not the top-level drop folder). If it
happens to *not* be empty (because the user has added additional files to
it), just catch the error and ignore it. In another few minutes, you'll
try again, copying the newly added files and then delete the folder.

Because each file is being deleted as soon as it is copied, you'll won't
build up an ever-increasing collection of files. At worst, you may have a
collection of folders -- but unless you're fighting a hostile process
that can add folders quicker than you can delete them, you'll eventually
win.

Maybe you need a heuristic that says "if the number of 'directory not
empty' errors keeps increasing, assume we're dealing with a hostile
process and take extra steps". Say, lock the drop folder for five minutes
while you flush it.

Another possibility is that before you start copying from the folders,
you change their permissions to prohibit the user adding extra files into
them. That way, nothing can be added to them once you start copying, and
you can be sure that you can delete them once empty.
--
Steven
Oct 9 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by GIMME | last post: by
18 posts views Thread by Urs Vogel | last post: by
2 posts views Thread by manuelg | last post: by
9 posts views Thread by Keith G Hicks | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.