SFTP w/ proper atomic put and locking

John Arbash Meinel john at arbash-meinel.com
Sat Nov 5 08:56:10 GMT 2005


The current implementation of SFTPTransport did not handle file locking,
or putting atomically. Since this is about to be used by a bunch of
people, who are going to be collaborating with it, we need at least a
little bit of safety that we won't clobber someone else's changes, or
have the network connection die in the middle of a push, and have the
remote tree broken.

So I did 2 things, first, I 'put' into temporary files, and then rename
them into place.

Second, I open a file with O_EXCL which is .bzr/branch-lock.write-lock,
it is a cheesy way to implement "Transport.lock_write()", but I think it
is something that will work for us.
If SFTPTransport tries to lock the file, but finds the lock is already
there, it will throw a LockError exception, which generally prevents
further changes to that branch.

This may not be perfect always, and if the network connection dies in
the middle it will probably leave the tree in a locked state (and leave
a bogus temporary file or two lying around somewhere)
But I believe just deleting the branch-lock.write-lock file will return
the tree to its pristine state.

Probably we should add a (hidden) command for "bzr unlock LOCATION?"
where the default is the parent of the current branch.
But it is 3:45am, and I need to get some sleep. :)

Anyway, the tree is available here:
http://bzr.arbash-meinel.com/branches/bzr/sftp-fix/

I also am attaching the current patch.

John
=:->
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: sftp-fix.patch
Url: https://lists.ubuntu.com/archives/bazaar/attachments/20051105/6abb903b/attachment.diff 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 249 bytes
Desc: OpenPGP digital signature
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20051105/6abb903b/attachment.pgp 


More information about the bazaar mailing list