]> arthur.barton.de Git - bup.git/blob - lib/bup/drecurse.py
cmd/index: catch exception for paths that don't exist.
[bup.git] / lib / bup / drecurse.py
1 import stat
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):
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             try:
55                 OsFile(name).fchdir()
56             except OSError, e:
57                 add_error('%s: %s' % (prepend, e))
58             else:
59                 for i in _recursive_dirlist(prepend=prepend+name, xdev=xdev):
60                     yield i
61                 os.chdir('..')
62         yield (prepend + name, pst)
63
64
65 def recursive_dirlist(paths, xdev):
66     startdir = OsFile('.')
67     try:
68         assert(type(paths) != type(''))
69         for path in paths:
70             try:
71                 pst = os.lstat(path)
72                 if stat.S_ISLNK(pst.st_mode):
73                     yield (path, pst)
74                     continue
75             except OSError, e:
76                 add_error('recursive_dirlist: %s' % e)
77                 continue
78             try:
79                 pfile = OsFile(path)
80             except OSError, e:
81                 add_error(e)
82                 continue
83             pst = pfile.stat()
84             if xdev:
85                 xdev = pst.st_dev
86             else:
87                 xdev = None
88             if stat.S_ISDIR(pst.st_mode):
89                 pfile.fchdir()
90                 prepend = os.path.join(path, '')
91                 for i in _recursive_dirlist(prepend=prepend, xdev=xdev):
92                     yield i
93                 startdir.fchdir()
94             else:
95                 prepend = path
96             yield (prepend,pst)
97     except:
98         try:
99             startdir.fchdir()
100         except:
101             pass
102         raise