From d628d5f503a838a22ac9d60db86591ae847df4fc Mon Sep 17 00:00:00 2001
From: Brian Warner <warner@lothar.com>
Date: Sat, 15 Sep 2007 14:34:04 -0700
Subject: [PATCH] storage: remove the leftover incoming/XYZ/ directory when
 we're done with it

---
 src/allmydata/storage.py           |  6 ++++++
 src/allmydata/test/test_storage.py | 11 +++++++++++
 2 files changed, 17 insertions(+)

diff --git a/src/allmydata/storage.py b/src/allmydata/storage.py
index a185c64c..afd6e07b 100644
--- a/src/allmydata/storage.py
+++ b/src/allmydata/storage.py
@@ -195,6 +195,12 @@ class BucketWriter(Referenceable):
         filelen = os.stat(self.finalhome)[stat.ST_SIZE]
         self.ss.bucket_writer_closed(self, filelen)
 
+        # if we were the last share to be moved, remove the incoming/
+        # directory that was our parent
+        parentdir = os.path.split(self.incominghome)[0]
+        if not os.listdir(parentdir):
+            os.rmdir(parentdir)
+
 
 class BucketReader(Referenceable):
     implements(RIBucketReader)
diff --git a/src/allmydata/test/test_storage.py b/src/allmydata/test/test_storage.py
index c9b4d30e..61948c7e 100644
--- a/src/allmydata/test/test_storage.py
+++ b/src/allmydata/test/test_storage.py
@@ -202,6 +202,17 @@ class Server(unittest.TestCase):
                                           renew_secret, cancel_secret,
                                           sharenums, size, Referenceable())
 
+    def test_remove_incoming(self):
+        ss = self.create("test_remove_incoming")
+        already, writers = self.allocate(ss, "vid", range(3), 10)
+        for i,wb in writers.items():
+            wb.remote_write(0, "%10d" % i)
+            wb.remote_close()
+        incomingdir = os.path.join(self.workdir("test_remove_incoming"),
+                                   "shares", "incoming")
+        leftover_dirs = os.listdir(incomingdir)
+        self.failUnlessEqual(leftover_dirs, [])
+
     def test_allocate(self):
         ss = self.create("test_allocate")
 
-- 
2.45.2