2 from bup.helpers import *
5 O_LARGEFILE = os.O_LARGEFILE
10 # the use of fchdir() and lstat() is for two reasons:
11 # - help out the kernel by not making it repeatedly look up the absolute path
12 # - avoid race conditions caused by doing listdir() on a changing symlink
14 def __init__(self, path):
16 self.fd = os.open(path,
17 os.O_RDONLY|O_LARGEFILE|os.O_NOFOLLOW|os.O_NDELAY)
29 return os.fstat(self.fd)
32 _IFMT = stat.S_IFMT(0xffffffff) # avoid function call in inner loop
35 for n in os.listdir('.'):
39 add_error(Exception('%s: %s' % (realpath(n), str(e))))
41 if (st.st_mode & _IFMT) == stat.S_IFDIR:
48 def _recursive_dirlist(prepend, xdev, bup_dir=None, excluded_paths=None):
49 for (name,pst) in _dirlist():
50 if name.endswith('/'):
51 if xdev != None and pst.st_dev != xdev:
52 log('Skipping %r: different filesystem.\n' % (prepend+name))
55 if os.path.normpath(prepend+name) == bup_dir:
56 log('Skipping BUP_DIR.\n')
59 if os.path.normpath(prepend+name) in excluded_paths:
60 log('Skipping %r: excluded.\n' % (prepend+name))
65 add_error('%s: %s' % (prepend, e))
67 for i in _recursive_dirlist(prepend=prepend+name, xdev=xdev,
69 excluded_paths=excluded_paths):
72 yield (prepend + name, pst)
75 def recursive_dirlist(paths, xdev, bup_dir=None, excluded_paths=None):
76 startdir = OsFile('.')
78 assert(type(paths) != type(''))
82 if stat.S_ISLNK(pst.st_mode):
86 add_error('recursive_dirlist: %s' % e)
98 if stat.S_ISDIR(pst.st_mode):
100 prepend = os.path.join(path, '')
101 for i in _recursive_dirlist(prepend=prepend, xdev=xdev,
103 excluded_paths=excluded_paths):
116 def parse_excludes(flags):
120 (option, parameter) = flag
121 if option == '--exclude':
122 excluded_paths.append(realpath(parameter))
124 if option == '--exclude-from':
127 f = open(realpath(parameter))
128 for exclude_path in f.readlines():
129 excluded_paths.append(realpath(exclude_path.strip()))
131 log("warning: couldn't read %s" % parameter)
135 return excluded_paths