]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/drecurse.py
Add support for "bup index --exclude-rx <pattern> ...".
[bup.git] / lib / bup / drecurse.py
index c3daaa8be0a126003e3c34cf020176c1a99fcf69..694c403594d1aec45c6979629c20314fd4c948ea 100644 (file)
@@ -1,10 +1,15 @@
-import stat, heapq
+import stat, os
 from bup.helpers import *
+import bup.xstat as xstat
 
 try:
     O_LARGEFILE = os.O_LARGEFILE
 except AttributeError:
     O_LARGEFILE = 0
+try:
+    O_NOFOLLOW = os.O_NOFOLLOW
+except AttributeError:
+    O_NOFOLLOW = 0
 
 
 # the use of fchdir() and lstat() is for two reasons:
@@ -13,7 +18,7 @@ except AttributeError:
 class OsFile:
     def __init__(self, path):
         self.fd = None
-        self.fd = os.open(path, os.O_RDONLY|O_LARGEFILE|os.O_NOFOLLOW)
+        self.fd = os.open(path, os.O_RDONLY|O_LARGEFILE|O_NOFOLLOW|os.O_NDELAY)
         
     def __del__(self):
         if self.fd:
@@ -25,7 +30,7 @@ class OsFile:
         os.fchdir(self.fd)
 
     def stat(self):
-        return os.fstat(self.fd)
+        return xstat.fstat(self.fd)
 
 
 _IFMT = stat.S_IFMT(0xffffffff)  # avoid function call in inner loop
@@ -33,7 +38,7 @@ def _dirlist():
     l = []
     for n in os.listdir('.'):
         try:
-            st = os.lstat(n)
+            st = xstat.lstat(n)
         except OSError, e:
             add_error(Exception('%s: %s' % (realpath(n), str(e))))
             continue
@@ -44,35 +49,52 @@ def _dirlist():
     return l
 
 
-def _recursive_dirlist(prepend, xdev):
+def _recursive_dirlist(prepend, xdev, bup_dir=None,
+                       excluded_paths=None,
+                       exclude_rxs=None):
     for (name,pst) in _dirlist():
+        path = prepend + name
+        if excluded_paths:
+            if os.path.normpath(path) in excluded_paths:
+                debug1('Skipping %r: excluded.\n' % path)
+                continue
+        if exclude_rxs and should_rx_exclude_path(path, exclude_rxs):
+            continue
         if name.endswith('/'):
             if xdev != None and pst.st_dev != xdev:
-                log('Skipping %r: different filesystem.\n' % (prepend+name))
+                debug1('Skipping %r: different filesystem.\n' % (prepend+name))
                 continue
+            if bup_dir != None:
+                if os.path.normpath(prepend+name) == bup_dir:
+                    debug1('Skipping BUP_DIR.\n')
+                    continue
             try:
                 OsFile(name).fchdir()
             except OSError, e:
                 add_error('%s: %s' % (prepend, e))
             else:
-                for i in _recursive_dirlist(prepend=prepend+name, xdev=xdev):
+                for i in _recursive_dirlist(prepend=prepend+name, xdev=xdev,
+                                            bup_dir=bup_dir,
+                                            excluded_paths=excluded_paths,
+                                            exclude_rxs=exclude_rxs):
                     yield i
                 os.chdir('..')
         yield (prepend + name, pst)
 
 
-def recursive_dirlist(paths, xdev):
+def recursive_dirlist(paths, xdev, bup_dir=None, excluded_paths=None,
+                      exclude_rxs=None):
     startdir = OsFile('.')
     try:
         assert(type(paths) != type(''))
         for path in paths:
             try:
-                pst = os.lstat(path)
+                pst = xstat.lstat(path)
                 if stat.S_ISLNK(pst.st_mode):
                     yield (path, pst)
                     continue
             except OSError, e:
-                add_error(e)
+                add_error('recursive_dirlist: %s' % e)
                 continue
             try:
                 pfile = OsFile(path)
@@ -87,7 +109,10 @@ def recursive_dirlist(paths, xdev):
             if stat.S_ISDIR(pst.st_mode):
                 pfile.fchdir()
                 prepend = os.path.join(path, '')
-                for i in _recursive_dirlist(prepend=prepend, xdev=xdev):
+                for i in _recursive_dirlist(prepend=prepend, xdev=xdev,
+                                            bup_dir=bup_dir,
+                                            excluded_paths=excluded_paths,
+                                            exclude_rxs=exclude_rxs):
                     yield i
                 startdir.fchdir()
             else: