2 from __future__ import absolute_import
10 should_rx_exclude_path)
11 from bup.io import path_msg
12 import bup.xstat as xstat
14 # the use of fchdir() and lstat() is for two reasons:
15 # - help out the kernel by not making it repeatedly look up the absolute path
16 # - avoid race conditions caused by doing listdir() on a changing symlink
19 O_LARGEFILE = os.O_LARGEFILE
20 except AttributeError:
23 O_NOFOLLOW = os.O_NOFOLLOW
24 except AttributeError:
28 def finalized_fd(path):
29 fd = os.open(path, os.O_RDONLY|O_LARGEFILE|O_NOFOLLOW|os.O_NDELAY)
30 return finalized(fd, lambda x: os.close(x))
35 for n in os.listdir(b'.'):
39 add_error(Exception('%s: %s' % (resolve_parent(n), str(e))))
41 if stat.S_ISDIR(st.st_mode):
47 def _recursive_dirlist(prepend, xdev, bup_dir=None,
50 xdev_exceptions=frozenset()):
51 for (name,pst) in _dirlist():
54 if os.path.normpath(path) in excluded_paths:
55 debug1('Skipping %r: excluded.\n' % path_msg(path))
57 if exclude_rxs and should_rx_exclude_path(path, exclude_rxs):
59 if name.endswith(b'/'):
61 if os.path.normpath(path) == bup_dir:
62 debug1('Skipping BUP_DIR.\n')
64 if xdev != None and pst.st_dev != xdev \
65 and path not in xdev_exceptions:
66 debug1('Skipping contents of %r: different filesystem.\n'
70 with finalized_fd(name) as fd:
73 add_error('%s: %s' % (prepend, e))
75 for i in _recursive_dirlist(prepend=prepend+name, xdev=xdev,
77 excluded_paths=excluded_paths,
78 exclude_rxs=exclude_rxs,
79 xdev_exceptions=xdev_exceptions):
85 def recursive_dirlist(paths, xdev, bup_dir=None,
88 xdev_exceptions=frozenset()):
89 with finalized_fd(b'.') as startdir:
91 assert not isinstance(paths, str)
94 pst = xstat.lstat(path)
95 if stat.S_ISLNK(pst.st_mode):
99 add_error('recursive_dirlist: %s' % e)
102 opened_pfile = finalized_fd(path)
106 with opened_pfile as pfile:
107 pst = xstat.fstat(pfile)
112 if stat.S_ISDIR(pst.st_mode):
114 prepend = os.path.join(path, b'')
115 for i in _recursive_dirlist(prepend=prepend, xdev=xdev,
117 excluded_paths=excluded_paths,
118 exclude_rxs=exclude_rxs,
119 xdev_exceptions=xdev_exceptions):