]> arthur.barton.de Git - bup.git/blob - lib/bup/drecurse.py
Excludes BUP_DIR from index.
[bup.git] / lib / bup / drecurse.py
1 import stat, os
2 from bup.helpers import *
3
4 try:
5     O_LARGEFILE = os.O_LARGEFILE
6 except AttributeError:
7     O_LARGEFILE = 0
8
9
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
13 class OsFile:
14     def __init__(self, path):
15         self.fd = None
16         self.fd = os.open(path, 
17                           os.O_RDONLY|O_LARGEFILE|os.O_NOFOLLOW|os.O_NDELAY)
18         
19     def __del__(self):
20         if self.fd:
21             fd = self.fd
22             self.fd = None
23             os.close(fd)
24
25     def fchdir(self):
26         os.fchdir(self.fd)
27
28     def stat(self):
29         return os.fstat(self.fd)
30
31
32 _IFMT = stat.S_IFMT(0xffffffff)  # avoid function call in inner loop
33 def _dirlist():
34     l = []
35     for n in os.listdir('.'):
36         try:
37             st = os.lstat(n)
38         except OSError, e:
39             add_error(Exception('%s: %s' % (realpath(n), str(e))))
40             continue
41         if (st.st_mode & _IFMT) == stat.S_IFDIR:
42             n += '/'
43         l.append((n,st))
44     l.sort(reverse=True)
45     return l
46
47
48 def _recursive_dirlist(prepend, xdev, bup_dir=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))
53                 continue
54             if bup_dir != None:
55                 if os.path.normpath(prepend+name) == bup_dir:
56                     log('Skipping BUP_DIR.\n')
57                     continue
58             try:
59                 OsFile(name).fchdir()
60             except OSError, e:
61                 add_error('%s: %s' % (prepend, e))
62             else:
63                 for i in _recursive_dirlist(prepend=prepend+name, xdev=xdev,
64                                             bup_dir=bup_dir):
65                     yield i
66                 os.chdir('..')
67         yield (prepend + name, pst)
68
69
70 def recursive_dirlist(paths, xdev, bup_dir=None):
71     startdir = OsFile('.')
72     try:
73         assert(type(paths) != type(''))
74         for path in paths:
75             try:
76                 pst = os.lstat(path)
77                 if stat.S_ISLNK(pst.st_mode):
78                     yield (path, pst)
79                     continue
80             except OSError, e:
81                 add_error('recursive_dirlist: %s' % e)
82                 continue
83             try:
84                 pfile = OsFile(path)
85             except OSError, e:
86                 add_error(e)
87                 continue
88             pst = pfile.stat()
89             if xdev:
90                 xdev = pst.st_dev
91             else:
92                 xdev = None
93             if stat.S_ISDIR(pst.st_mode):
94                 pfile.fchdir()
95                 prepend = os.path.join(path, '')
96                 for i in _recursive_dirlist(prepend=prepend, xdev=xdev,
97                                             bup_dir=bup_dir):
98                     yield i
99                 startdir.fchdir()
100             else:
101                 prepend = path
102             yield (prepend,pst)
103     except:
104         try:
105             startdir.fchdir()
106         except:
107             pass
108         raise