2 from __future__ import absolute_import
5 from bup.compat import str_type
6 from bup.helpers import add_error, should_rx_exclude_path, debug1, resolve_parent
7 from bup.io import path_msg
8 import bup.xstat as xstat
12 O_LARGEFILE = os.O_LARGEFILE
13 except AttributeError:
16 O_NOFOLLOW = os.O_NOFOLLOW
17 except AttributeError:
21 # the use of fchdir() and lstat() is for two reasons:
22 # - help out the kernel by not making it repeatedly look up the absolute path
23 # - avoid race conditions caused by doing listdir() on a changing symlink
25 def __init__(self, path):
27 self.fd = os.open(path, os.O_RDONLY|O_LARGEFILE|O_NOFOLLOW|os.O_NDELAY)
39 return xstat.fstat(self.fd)
43 for n in os.listdir(b'.'):
47 add_error(Exception('%s: %s' % (resolve_parent(n), str(e))))
49 if stat.S_ISDIR(st.st_mode):
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_msg(path))
65 if exclude_rxs and should_rx_exclude_path(path, exclude_rxs):
67 if name.endswith(b'/'):
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'
80 add_error('%s: %s' % (prepend, e))
82 for i in _recursive_dirlist(prepend=prepend+name, xdev=xdev,
84 excluded_paths=excluded_paths,
85 exclude_rxs=exclude_rxs,
86 xdev_exceptions=xdev_exceptions):
92 def recursive_dirlist(paths, xdev, bup_dir=None,
95 xdev_exceptions=frozenset()):
96 startdir = OsFile(b'.')
98 assert not isinstance(paths, str_type)
101 pst = xstat.lstat(path)
102 if stat.S_ISLNK(pst.st_mode):
106 add_error('recursive_dirlist: %s' % e)
118 if stat.S_ISDIR(pst.st_mode):
120 prepend = os.path.join(path, b'')
121 for i in _recursive_dirlist(prepend=prepend, xdev=xdev,
123 excluded_paths=excluded_paths,
124 exclude_rxs=exclude_rxs,
125 xdev_exceptions=xdev_exceptions):