how to lock a file in ftp site

M

muttu2244

hi all

am trying to write some information into the file, which is located in
ftp, and this file can be updated by number of people, but if at all i
download a file from the ftp to my local machine, update it and then
upload it back to ftp, and at the same time if some one else downloads
the same file for the modification, then the data will be overwritten.

so is there a way in Python script where i can lock the file, so that
no one updates it until i release the lock.

Or is there a way where i can directly update the file from the ftp
itself, i mean without downloading it to my local machine.

Thanks in advance for the help

regards
yogi
 
A

andy

hi all

am trying to write some information into the file, which is located in
ftp, and this file can be updated by number of people, but if at all i
download a file from the ftp to my local machine, update it and then
upload it back to ftp, and at the same time if some one else downloads
the same file for the modification, then the data will be overwritten.

so is there a way in Python script where i can lock the file, so that
no one updates it until i release the lock.

Or is there a way where i can directly update the file from the ftp
itself, i mean without downloading it to my local machine.

Thanks in advance for the help

regards
yogi
I've come across this problem - instead of uploading the file to it's

intended directory, upload it to a temp directory, with a unique name
(say uuuuuyyyymmddhhmmss where uuuuu is username and the rest are
obvious) and then *rename* it to it's correct name in the real
destination directory. Do the move in a try... except block and if it
fails, take evasive action.

In FTP a rename (i.e. move) is usually an atomic action but an upload is
not. You have an unavoidable race condition here trying to lock a file
(even if ftp allowed it) but with the method described above, you can
avoid it, but get the same effect.

Hope that helps!
-andyj
 
B

Ben Hutchings

hi all

am trying to write some information into the file, which is located in
ftp, and this file can be updated by number of people, but if at all i
download a file from the ftp to my local machine, update it and then
upload it back to ftp, and at the same time if some one else downloads
the same file for the modification, then the data will be overwritten.

so is there a way in Python script where i can lock the file, so that
no one updates it until i release the lock.

A common means of cooperative file locking is to use the existence of
a second file as a lock indicator. I'm not sure that can be done over
FTP, though, because I think when you write to such a file you can't
tell whether it existed previously (which would mean someone else
owned the lock).
Or is there a way where i can directly update the file from the ftp
itself, i mean without downloading it to my local machine.

Thanks in advance for the help

You should probably switch from FTP to a version control system, such
as CVS or Subversion. They don't normally use locking, but they do
detect conflicting changes and stop you from overwriting other
people's changes.
 
S

Steve Holden

Ben said:
A common means of cooperative file locking is to use the existence of
a second file as a lock indicator. I'm not sure that can be done over
FTP, though, because I think when you write to such a file you can't
tell whether it existed previously (which would mean someone else
owned the lock).
Well of course you could place a lock on the lock file before you tested
for its presence by creating another file ...
You should probably switch from FTP to a version control system, such
as CVS or Subversion. They don't normally use locking, but they do
detect conflicting changes and stop you from overwriting other
people's changes.
I'm not sure a version control system is right for the OP's needs, but
clearly unadorned FTP isn't going to hack it either.

This being the Python, of course, it would be relatively simple to
design a protocol that would provide the required functionality and then
build clients and servers for that protocol. But we don't know whether
that's an option yet.

regards
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top