2 from __future__ import absolute_import
5 from bup.helpers import add_error, should_rx_exclude_path, debug1, resolve_parent
6 from bup.io import path_msg
7 import bup.xstat as xstat
11 O_LARGEFILE = os.O_LARGEFILE
12 except AttributeError:
15 O_NOFOLLOW = os.O_NOFOLLOW
16 except AttributeError:
20 # the use of fchdir() and lstat() is for two reasons:
21 # - help out the kernel by not making it repeatedly look up the absolute path
22 # - avoid race conditions caused by doing listdir() on a changing symlink
24 def __init__(self, path):
26 self.fd = os.open(path, os.O_RDONLY|O_LARGEFILE|O_NOFOLLOW|os.O_NDELAY)
38 return xstat.fstat(self.fd)
42 for n in os.listdir(b'.'):
46 add_error(Exception('%s: %s' % (resolve_parent(n), str(e))))
48 if stat.S_ISDIR(st.st_mode):
54 def _recursive_dirlist(prepend, xdev, bup_dir=None,
57 xdev_exceptions=frozenset()):
58 for (name,pst) in _dirlist():
61 if os.path.normpath(path) in excluded_paths:
62 debug1('Skipping %r: excluded.\n' % path_msg(path))
64 if exclude_rxs and should_rx_exclude_path(path, exclude_rxs):
66 if name.endswith(b'/'):
68 if os.path.normpath(path) == bup_dir:
69 debug1('Skipping BUP_DIR.\n')
71 if xdev != None and pst.st_dev != xdev \
72 and path not in xdev_exceptions:
73 debug1('Skipping contents of %r: different filesystem.\n'
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(b'.')
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, b'')
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):