2 from __future__ import absolute_import
5 from bup.helpers import add_error, should_rx_exclude_path, debug1, resolve_parent
6 import bup.xstat as xstat
10 O_LARGEFILE = os.O_LARGEFILE
11 except AttributeError:
14 O_NOFOLLOW = os.O_NOFOLLOW
15 except AttributeError:
19 # the use of fchdir() and lstat() is for two reasons:
20 # - help out the kernel by not making it repeatedly look up the absolute path
21 # - avoid race conditions caused by doing listdir() on a changing symlink
23 def __init__(self, path):
25 self.fd = os.open(path, os.O_RDONLY|O_LARGEFILE|O_NOFOLLOW|os.O_NDELAY)
37 return xstat.fstat(self.fd)
40 _IFMT = stat.S_IFMT(0xffffffff) # avoid function call in inner loop
43 for n in os.listdir('.'):
47 add_error(Exception('%s: %s' % (resolve_parent(n), str(e))))
49 if (st.st_mode & _IFMT) == stat.S_IFDIR:
56 def _recursive_dirlist(prepend, xdev, bup_dir=None,
59 xdev_exceptions=frozenset()):
60 for (name,pst) in _dirlist():
63 if os.path.normpath(path) in excluded_paths:
64 debug1('Skipping %r: excluded.\n' % path)
66 if exclude_rxs and should_rx_exclude_path(path, exclude_rxs):
68 if name.endswith('/'):
70 if os.path.normpath(path) == bup_dir:
71 debug1('Skipping BUP_DIR.\n')
73 if xdev != None and pst.st_dev != xdev \
74 and path not in xdev_exceptions:
75 debug1('Skipping contents of %r: different filesystem.\n' % path)
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('.')
98 assert(type(paths) != 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, '')
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):