4 from bup.helpers import add_error, should_rx_exclude_path, debug1, resolve_parent
5 import bup.xstat as xstat
9 O_LARGEFILE = os.O_LARGEFILE
10 except AttributeError:
13 O_NOFOLLOW = os.O_NOFOLLOW
14 except AttributeError:
18 # the use of fchdir() and lstat() is for two reasons:
19 # - help out the kernel by not making it repeatedly look up the absolute path
20 # - avoid race conditions caused by doing listdir() on a changing symlink
22 def __init__(self, path):
24 self.fd = os.open(path, os.O_RDONLY|O_LARGEFILE|O_NOFOLLOW|os.O_NDELAY)
36 return xstat.fstat(self.fd)
39 _IFMT = stat.S_IFMT(0xffffffff) # avoid function call in inner loop
42 for n in os.listdir('.'):
46 add_error(Exception('%s: %s' % (resolve_parent(n), str(e))))
48 if (st.st_mode & _IFMT) == stat.S_IFDIR:
55 def _recursive_dirlist(prepend, xdev, bup_dir=None,
58 xdev_exceptions=frozenset()):
59 for (name,pst) in _dirlist():
62 if os.path.normpath(path) in excluded_paths:
63 debug1('Skipping %r: excluded.\n' % path)
65 if exclude_rxs and should_rx_exclude_path(path, exclude_rxs):
67 if name.endswith('/'):
69 if os.path.normpath(path) == bup_dir:
70 debug1('Skipping BUP_DIR.\n')
72 if xdev != None and pst.st_dev != xdev \
73 and path not in xdev_exceptions:
74 debug1('Skipping contents of %r: different filesystem.\n' % path)
79 add_error('%s: %s' % (prepend, e))
81 for i in _recursive_dirlist(prepend=prepend+name, xdev=xdev,
83 excluded_paths=excluded_paths,
84 exclude_rxs=exclude_rxs,
85 xdev_exceptions=xdev_exceptions):
91 def recursive_dirlist(paths, xdev, bup_dir=None,
94 xdev_exceptions=frozenset()):
95 startdir = OsFile('.')
97 assert(type(paths) != type(''))
100 pst = xstat.lstat(path)
101 if stat.S_ISLNK(pst.st_mode):
105 add_error('recursive_dirlist: %s' % e)
117 if stat.S_ISDIR(pst.st_mode):
119 prepend = os.path.join(path, '')
120 for i in _recursive_dirlist(prepend=prepend, xdev=xdev,
122 excluded_paths=excluded_paths,
123 exclude_rxs=exclude_rxs,
124 xdev_exceptions=xdev_exceptions):