[CalendarServer-changes] [10180] CalendarServer/branches/release/CalendarServer-4.3-dev/txdav

source_changes at macosforge.org source_changes at macosforge.org
Mon Dec 17 13:02:48 PST 2012


Revision: 10180
          http://trac.calendarserver.org//changeset/10180
Author:   cdaboo at apple.com
Date:     2012-12-17 13:02:48 -0800 (Mon, 17 Dec 2012)
Log Message:
-----------
Make sure temp attachment files are cleaned up when a txn is aborted.

Modified Paths:
--------------
    CalendarServer/branches/release/CalendarServer-4.3-dev/txdav/caldav/datastore/file.py
    CalendarServer/branches/release/CalendarServer-4.3-dev/txdav/caldav/datastore/sql.py
    CalendarServer/branches/release/CalendarServer-4.3-dev/txdav/caldav/datastore/test/common.py
    CalendarServer/branches/release/CalendarServer-4.3-dev/txdav/common/datastore/file.py

Modified: CalendarServer/branches/release/CalendarServer-4.3-dev/txdav/caldav/datastore/file.py
===================================================================
--- CalendarServer/branches/release/CalendarServer-4.3-dev/txdav/caldav/datastore/file.py	2012-12-17 17:50:55 UTC (rev 10179)
+++ CalendarServer/branches/release/CalendarServer-4.3-dev/txdav/caldav/datastore/file.py	2012-12-17 21:02:48 UTC (rev 10180)
@@ -731,7 +731,22 @@
         self._path = self._attachment._path.temporarySibling()
         self._file = self._path.open("w")
 
+        self._txn.postAbort(self.aborted)
 
+
+    @property
+    def _txn(self):
+        return self._attachment._txn
+
+
+    def aborted(self):
+        """
+        Transaction aborted - clean up temp files.
+        """
+        if self._path.exists():
+            self._path.remove()
+
+
     def write(self, data):
         # FIXME: multiple chunks
         self._file.write(data)
@@ -784,6 +799,11 @@
         self._dropboxPath = dropboxPath
 
 
+    @property
+    def _txn(self):
+        return self._calendarObject._txn
+
+
     def name(self):
         return self._name
 

Modified: CalendarServer/branches/release/CalendarServer-4.3-dev/txdav/caldav/datastore/sql.py
===================================================================
--- CalendarServer/branches/release/CalendarServer-4.3-dev/txdav/caldav/datastore/sql.py	2012-12-17 17:50:55 UTC (rev 10179)
+++ CalendarServer/branches/release/CalendarServer-4.3-dev/txdav/caldav/datastore/sql.py	2012-12-17 21:02:48 UTC (rev 10180)
@@ -1358,7 +1358,9 @@
         self._hash = hashlib.md5()
         self._creating = creating
 
+        self._txn.postAbort(self.aborted)
 
+
     def _temporaryFile(self):
         """
         Returns a (file descriptor, absolute path) tuple for a temporary file within
@@ -1378,6 +1380,14 @@
         return self._attachment._txn
 
 
+    def aborted(self):
+        """
+        Transaction aborted - clean up temp files.
+        """
+        if self._path.exists():
+            self._path.remove()
+
+
     def write(self, data):
         if isinstance(data, buffer):
             data = str(data)

Modified: CalendarServer/branches/release/CalendarServer-4.3-dev/txdav/caldav/datastore/test/common.py
===================================================================
--- CalendarServer/branches/release/CalendarServer-4.3-dev/txdav/caldav/datastore/test/common.py	2012-12-17 17:50:55 UTC (rev 10179)
+++ CalendarServer/branches/release/CalendarServer-4.3-dev/txdav/caldav/datastore/test/common.py	2012-12-17 21:02:48 UTC (rev 10180)
@@ -20,8 +20,9 @@
 """
 
 from StringIO import StringIO
+import os
 
-from twisted.internet.defer import Deferred, inlineCallbacks, returnValue,\
+from twisted.internet.defer import Deferred, inlineCallbacks, returnValue, \
     maybeDeferred
 from twisted.internet.protocol import Protocol
 from twisted.python import hashlib
@@ -811,12 +812,12 @@
         """
         Test Calendar._countComponentTypes to make sure correct counts are returned.
         """
-        
+
         tests = (
             ("calendar_1", (("VEVENT", 3),)),
             ("calendar_2", (("VEVENT", 3), ("VTODO", 2))),
         )
-        
+
         for calname, results in tests:
             testalendar = yield (yield self.transactionUnderTest().calendarHomeWithUID(
                 "home_splits")).calendarWithName(calname)
@@ -1451,7 +1452,7 @@
             set(c.name() for c in calendars),
             set(home1_calendarNames)
         )
-        
+
         for c in calendars:
             self.assertTrue(c.properties() is not None)
 
@@ -1705,7 +1706,7 @@
             self.assertNotEquals(event1_text, event1_text_withDifferentSubject)
             newComponent = VComponent.fromString(event1_text_withDifferentSubject)
             yield obj.setComponent(newComponent)
-    
+
             # Putting everything into a separate transaction to account for any
             # caching that may take place.
             yield self.commit()
@@ -1996,6 +1997,37 @@
 
 
     @inlineCallbacks
+    def test_attachmentTemporaryFileCleanup(self):
+        """
+        L{IAttachmentStream} object cleans-up its temporary file on txn abort.
+        """
+        obj = yield self.calendarObjectUnderTest()
+        attachment = yield obj.createAttachmentWithName(
+            "new.attachment",
+        )
+        t = attachment.store(MimeType("text", "x-fixture"))
+
+        temp = t._path.path
+
+        yield self.abort()
+
+        self.assertFalse(os.path.exists(temp))
+
+        obj = yield self.calendarObjectUnderTest()
+        attachment = yield obj.createAttachmentWithName(
+            "new.attachment",
+        )
+        t = attachment.store(MimeType("text", "x-fixture"))
+
+        temp = t._path.path
+        os.remove(temp)
+
+        yield self.abort()
+
+        self.assertFalse(os.path.exists(temp))
+
+
+    @inlineCallbacks
     def test_quotaAllowedBytes(self):
         """
         L{ICalendarHome.quotaAllowedBytes} should return the configuration value
@@ -2047,7 +2079,7 @@
         that fails with L{QuotaExceeded}.
         """
         home = yield self.homeUnderTest()
-        attachment = yield getit() 
+        attachment = yield getit()
         t = attachment.store(MimeType("text", "x-fixture"))
         sample = "all work and no play makes jack a dull boy"
         chunk = (sample * (home.quotaAllowedBytes() / len(sample)))

Modified: CalendarServer/branches/release/CalendarServer-4.3-dev/txdav/common/datastore/file.py
===================================================================
--- CalendarServer/branches/release/CalendarServer-4.3-dev/txdav/common/datastore/file.py	2012-12-17 17:50:55 UTC (rev 10179)
+++ CalendarServer/branches/release/CalendarServer-4.3-dev/txdav/common/datastore/file.py	2012-12-17 21:02:48 UTC (rev 10180)
@@ -1015,6 +1015,12 @@
     def __repr__(self):
         return "<%s: %s>" % (self.__class__.__name__, self._path.path)
 
+
+    @property
+    def _txn(self):
+        return self._transaction
+
+
     def transaction(self):
         return self._transaction
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20121217/e6c2adc2/attachment.html>


More information about the calendarserver-changes mailing list