]> arthur.barton.de Git - bup.git/blob - lib/bup/drecurse.py
14bb7212a9ecb3e5466f8236ed9674a9420d2332
[bup.git] / lib / bup / drecurse.py
1
2 import stat, os
3
4 from bup.helpers import add_error, should_rx_exclude_path, debug1, resolve_parent
5 import bup.xstat as xstat
6
7
8 try:
9     O_LARGEFILE = os.O_LARGEFILE
10 except AttributeError:
11     O_LARGEFILE = 0
12 try:
13     O_NOFOLLOW = os.O_NOFOLLOW
14 except AttributeError:
15     O_NOFOLLOW = 0
16
17
18 # the use of fchdir() and lstat() is for two reasons:
19 #  - help out the kernel by not making it repeatedly look up the absolute path
20 #  - avoid race conditions caused by doing listdir() on a changing symlink
21 class OsFile:
22     def __init__(self, path):
23         self.fd = None
24         self.fd = os.open(path, os.O_RDONLY|O_LARGEFILE|O_NOFOLLOW|os.O_NDELAY)
25         
26     def __del__(self):
27         if self.fd:
28             fd = self.fd
29             self.fd = None
30             os.close(fd)
31
32     def fchdir(self):
33         os.fchdir(self.fd)
34
35     def stat(self):
36         return xstat.fstat(self.fd)
37
38
39 _IFMT = stat.S_IFMT(0xffffffff)  # avoid function call in inner loop
40 def _dirlist():
41     l = []
42     for n in os.listdir('.'):
43         try:
44             st = xstat.lstat(n)
45         except OSError as e:
46             add_error(Exception('%s: %s' % (resolve_parent(n), str(e))))
47             continue
48         if (st.st_mode & _IFMT) == stat.S_IFDIR:
49             n += '/'
50         l.append((n,st))
51     l.sort(reverse=True)
52     return l
53
54
55 def _recursive_dirlist(prepend, xdev, bup_dir=None,
56                        excluded_paths=None,
57                        exclude_rxs=None,
58                        xdev_exceptions=frozenset()):
59     for (name,pst) in _dirlist():
60         path = prepend + name
61         if excluded_paths:
62             if os.path.normpath(path) in excluded_paths:
63                 debug1('Skipping %r: excluded.\n' % path)
64                 continue
65         if exclude_rxs and should_rx_exclude_path(path, exclude_rxs):
66             continue
67         if name.endswith('/'):
68             if bup_dir != None:
69                 if os.path.normpath(path) == bup_dir:
70                     debug1('Skipping BUP_DIR.\n')
71                     continue
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' % path)
75             else:
76                 try:
77                     OsFile(name).fchdir()
78                 except OSError as e:
79                     add_error('%s: %s' % (prepend, e))
80                 else:
81                     for i in _recursive_dirlist(prepend=prepend+name, xdev=xdev,
82                                                 bup_dir=bup_dir,
83                                                 excluded_paths=excluded_paths,
84                                                 exclude_rxs=exclude_rxs,
85                                                 xdev_exceptions=xdev_exceptions):
86                         yield i
87                     os.chdir('..')
88         yield (path, pst)
89
90
91 def recursive_dirlist(paths, xdev, bup_dir=None,
92                       excluded_paths=None,
93                       exclude_rxs=None,
94                       xdev_exceptions=frozenset()):
95     startdir = OsFile('.')
96     try:
97         assert(type(paths) != type(''))
98         for path in paths:
99             try:
100                 pst = xstat.lstat(path)
101                 if stat.S_ISLNK(pst.st_mode):
102                     yield (path, pst)
103                     continue
104             except OSError as e:
105                 add_error('recursive_dirlist: %s' % e)
106                 continue
107             try:
108                 pfile = OsFile(path)
109             except OSError as e:
110                 add_error(e)
111                 continue
112             pst = pfile.stat()
113             if xdev:
114                 xdev = pst.st_dev
115             else:
116                 xdev = None
117             if stat.S_ISDIR(pst.st_mode):
118                 pfile.fchdir()
119                 prepend = os.path.join(path, '')
120                 for i in _recursive_dirlist(prepend=prepend, xdev=xdev,
121                                             bup_dir=bup_dir,
122                                             excluded_paths=excluded_paths,
123                                             exclude_rxs=exclude_rxs,
124                                             xdev_exceptions=xdev_exceptions):
125                     yield i
126                 startdir.fchdir()
127             else:
128                 prepend = path
129             yield (prepend,pst)
130     except:
131         try:
132             startdir.fchdir()
133         except:
134             pass
135         raise