From d5be3e6086b2a8b6beeab74e1008df809b8b7f7b Mon Sep 17 00:00:00 2001
From: Zooko O'Whielacronx <zooko@zooko.com>
Date: Fri, 30 Mar 2007 18:01:56 -0700
Subject: [PATCH] fix BucketWriter to not create a finalhome until it is
 complete, and to clean up the empty $VERIFIERID dir under incoming/ when it
 moves the last share out of it

---
 src/allmydata/storageserver.py | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/src/allmydata/storageserver.py b/src/allmydata/storageserver.py
index 426b8aec..0579c88d 100644
--- a/src/allmydata/storageserver.py
+++ b/src/allmydata/storageserver.py
@@ -10,7 +10,7 @@ from allmydata.util import bencode, fileutil, idlib
 from allmydata.util.assertutil import _assert, precondition
 
 # store/
-# store/incoming # temp dirs named $VERIFIERID/$SHARENUM that will be moved to store/ on success
+# store/incoming # temp dirs named $VERIFIERID/$SHARENUM which will be moved to store/$VERIFIERID/$SHARENUM on success
 # store/$VERIFIERID
 # store/$VERIFIERID/$SHARENUM
 # store/$VERIFIERID/$SHARENUM/blocksize
@@ -30,7 +30,6 @@ class BucketWriter(Referenceable):
         self.blocksize = blocksize
         self.closed = False
         fileutil.make_dirs(incominghome)
-        fileutil.make_dirs(finalhome)
         self._write_file('blocksize', str(blocksize))
 
     def _write_file(self, fname, data):
@@ -56,7 +55,14 @@ class BucketWriter(Referenceable):
     def remote_close(self):
         precondition(not self.closed)
         # TODO assert or check the completeness and consistency of the data that has been written
+        fileutil.make_dirs(os.path.dirname(self.finalhome))
         fileutil.rename(self.incominghome, self.finalhome)
+        try:
+            os.rmdir(os.path.dirname(self.incominghome))
+        except OSError:
+            # Perhaps the directory wasn't empty.  In any case, ignore the error.
+            pass
+            
         self.closed = True
 
 def str2l(s):
@@ -103,8 +109,7 @@ class StorageServer(service.MultiService, Referenceable):
         alreadygot = set()
         bucketwriters = {} # k: shnum, v: BucketWriter
         for shnum in sharenums:
-            incominghome = os.path.join(self.incomingdir,
-                                        idlib.b2a(verifierid) +  "%d"%shnum)
+            incominghome = os.path.join(self.incomingdir, idlib.b2a(verifierid), "%d"%shnum)
             finalhome = os.path.join(self.storedir, idlib.b2a(verifierid), "%d"%shnum)
             if os.path.exists(incominghome) or os.path.exists(finalhome):
                 alreadygot.add(shnum)
-- 
2.45.2