[PATCH v2] Allow empty foldernames

Sebastian Spaeth Sebastian at SSpaeth.de
Wed Aug 17 15:11:00 BST 2011


Empty foldernames (as they could be created through nametrans) were
failing as the uidvalidity and status files names as determined by
folder/Base.py:getfolderbasename() lead to invalid file names ''.

Fix this by handling empty file names and translating them to '.' which
leads to the special file name 'dot'. (this special value existed before
and was not invented by this patch)

Signed-off-by: Sebastian Spaeth <Sebastian at SSpaeth.de>
---
I actually propose this for inclusion. It has the typo fixed that Vlad
noticed.

 offlineimap/folder/Base.py              |   14 +++++++++-----
 offlineimap/folder/LocalStatus.py       |    2 +-
 offlineimap/folder/LocalStatusSQLite.py |    2 +-
 offlineimap/repository/LocalStatus.py   |   15 +++++++++++----
 4 files changed, 22 insertions(+), 11 deletions(-)

diff --git a/offlineimap/folder/Base.py b/offlineimap/folder/Base.py
index 3da0de1..d73d112 100644
--- a/offlineimap/folder/Base.py
+++ b/offlineimap/folder/Base.py
@@ -72,11 +72,15 @@ class BaseFolder(object):
             return self.getname()
 
     def getfolderbasename(self):
-        foldername = self.getname()
-        foldername = foldername.replace(self.repository.getsep(), '.')
-        foldername = re.sub('/\.$', '/dot', foldername)
-        foldername = re.sub('^\.$', 'dot', foldername)
-        return foldername
+        """Return base file name of file to store Status/UID info in"""
+        if not self.name:
+            basename = '.'
+        else: #avoid directory hierarchies and file names such as '/'
+            basename = self.name.replace('/', '.')
+        # replace with literal 'dot' if final path name is '.' as '.' is
+        # an invalid file name.
+        basename = re.sub('(^|\/)\.$','\\1dot', basename)
+        return basename
 
     def isuidvalidityok(self):
         """Does the cached UID match the real UID
diff --git a/offlineimap/folder/LocalStatus.py b/offlineimap/folder/LocalStatus.py
index 5113345..375777e 100644
--- a/offlineimap/folder/LocalStatus.py
+++ b/offlineimap/folder/LocalStatus.py
@@ -28,7 +28,7 @@ class LocalStatusFolder(BaseFolder):
         self.root = root
         self.sep = '.'
         self.config = config
-        self.filename = repository.getfolderfilename(name)
+        self.filename = os.path.join(root, self.getfolderbasename())
         self.messagelist = {}
         self.repository = repository
         self.savelock = threading.Lock()
diff --git a/offlineimap/folder/LocalStatusSQLite.py b/offlineimap/folder/LocalStatusSQLite.py
index a57992b..77cdff3 100644
--- a/offlineimap/folder/LocalStatusSQLite.py
+++ b/offlineimap/folder/LocalStatusSQLite.py
@@ -115,7 +115,7 @@ class LocalStatusSQLiteFolder(LocalStatusFolder):
             plaintextfilename = os.path.join(
                 self.repository.account.getaccountmeta(),
                 'LocalStatus',
-                re.sub('(^|\/)\.$','\\1dot', self.name))
+                self.getfolderbasename(self.name))
             # MIGRATE from plaintext if needed
             if os.path.exists(plaintextfilename):
                 self.ui._msg('Migrating LocalStatus cache from plain text '
diff --git a/offlineimap/repository/LocalStatus.py b/offlineimap/repository/LocalStatus.py
index 50dfdc8..a392dcf 100644
--- a/offlineimap/repository/LocalStatus.py
+++ b/offlineimap/repository/LocalStatus.py
@@ -50,10 +50,17 @@ class LocalStatusRepository(BaseRepository):
         return '.'
 
     def getfolderfilename(self, foldername):
-        """Return the full path of the status file"""
-        # replace with 'dot' if final path name is '.'
-        foldername = re.sub('(^|\/)\.$','\\1dot', foldername)
-        return os.path.join(self.directory, foldername)
+        """Return the full path of the status file
+
+        This mimics the path that Folder().getfolderbasename() would return"""
+        if not foldername:
+            basename = '.'
+        else: #avoid directory hierarchies and file names such as '/'
+            basename = foldername.replace('/', '.')
+        # replace with literal 'dot' if final path name is '.' as '.' is
+        # an invalid file name.
+        basename = re.sub('(^|\/)\.$','\\1dot', basename)
+        return os.path.join(self.directory, basename)
 
     def makefolder(self, foldername):
         """Create a LocalStatus Folder
-- 
1.7.4.1





More information about the OfflineIMAP-project mailing list