]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/commitdiff
Changes to fileutil.
authorDaira Hopwood <daira@jacaranda.org>
Fri, 10 Jul 2015 03:35:27 +0000 (04:35 +0100)
committerDaira Hopwood <daira@jacaranda.org>
Fri, 10 Jul 2015 03:35:27 +0000 (04:35 +0100)
Signed-off-by: Daira Hopwood <daira@jacaranda.org>
src/allmydata/node.py
src/allmydata/util/fileutil.py

index 9ebb3787c022d0d42e070fed535e5be53337ee24..8d1ca15e233ae35070043f0b363f9451f859b530 100644 (file)
@@ -308,11 +308,11 @@ class Node(service.MultiService):
             fileutil.write(privpath, value, mode="")
         return value
 
-    def write_config(self, name, value, mode="w"):
+    def write_config(self, name, value, mode=""):
         """Write a string to a config file."""
         fn = os.path.join(self.basedir, name)
         try:
-            fileutil.write(fn, value, mode)
+            fileutil.write(fn, value, mode=mode)
         except EnvironmentError, e:
             self.log("Unable to write config file '%s'" % fn)
             self.log(e)
index f4ab13393ed95b3de20fd547e093289c61c7b1af..beb1d23291359a1f5782197682242ec55edfc44c 100644 (file)
@@ -2,7 +2,7 @@
 Futz with files like a pro.
 """
 
-import sys, exceptions, os, stat, tempfile, time, binascii
+import errno, sys, exceptions, os, re, stat, tempfile, time, binascii
 
 from twisted.python import log
 
@@ -174,10 +174,12 @@ def rm_dir(dirname):
             else:
                 remove(fullname)
         os.rmdir(dirname)
-    except Exception, le:
-        # Ignore "No such file or directory"
-        if (not isinstance(le, OSError)) or le.args[0] != 2:
+    except EnvironmentError, le:
+        # Ignore "No such file or directory", collect any other exception.
+        if (le.args[0] != 2 and le.args[0] != 3) or (le.args[0] != errno.ENOENT):
             excs.append(le)
+    except Exception, le:
+        excs.append(le)
 
     # Okay, now we've recursively removed everything, ignoring any "No
     # such file or directory" errors, and collecting any other errors.
@@ -189,13 +191,39 @@ def rm_dir(dirname):
             raise OSError, "Failed to remove dir for unknown reason."
         raise OSError, excs
 
-
 def remove_if_possible(f):
     try:
         remove(f)
     except:
         pass
 
+def rmdir_if_empty(path):
+    """ Remove the directory if it is empty. """
+    try:
+        os.rmdir(path)
+    except OSError, e:
+        if e.errno != errno.ENOTEMPTY:
+            raise
+
+
+ASCII = re.compile(r'^[\x00-\x7F]*$')
+
+def listdir(path, filter=ASCII):
+    try:
+        children = os.listdir(path)
+    except OSError, e:
+        if e.errno != errno.ENOENT:
+            raise
+        return []
+    else:
+        return [str(child) for child in children if filter.match(child)]
+
+def open_or_create(fname, binarymode=True):
+    try:
+        return open(fname, binarymode and "r+b" or "r+")
+    except EnvironmentError:
+        return open(fname, binarymode and "w+b" or "w+")
+
 def du(basedir):
     size = 0
 
@@ -221,8 +249,8 @@ def write_atomically(target, contents, mode="b"):
         f.close()
     move_into_place(target+".tmp", target)
 
-def write(path, data, mode="wb"):
-    wf = open(path, mode)
+def write(path, data, mode="b"):
+    wf = open(path, "w"+mode)
     try:
         wf.write(data)
     finally:
@@ -542,8 +570,8 @@ def get_used_space(path):
         # [in] 512-byte units." It is also defined that way on MacOS X. Python does
         # not set the attribute on Windows.
         #
-        # We consider platforms that define st_blocks but give it a wrong value, or
-        # measure it in a unit other than 512 bytes, to be broken. See also
+        # This code relies on the underlying platform to either define st_blocks in
+        # units of 512 bytes or else leave st_blocks undefined. See also
         # <http://bugs.python.org/issue12350>.
 
         if hasattr(s, 'st_blocks'):