1 #!/usr/bin/env python2.5
2 import sys, re, errno, stat, tempfile, struct, mmap
6 INDEX_SIG = '!IIIIIQ20sH'
7 ENTLEN = struct.calcsize(INDEX_SIG)
14 def __init__(self, path):
16 self.fd = os.open(path, os.O_RDONLY|os.O_LARGEFILE|os.O_NOFOLLOW)
17 #self.st = os.fstat(self.fd)
30 def __init__(self, name, m, ofs):
34 (self.dev, self.ctime, self.mtime, self.uid, self.gid,
36 self.flags) = struct.unpack(INDEX_SIG, buffer(m, ofs, ENTLEN))
39 return ("(%s,0x%04x,%d,%d,%d,%d,%d,0x%04x)"
40 % (self.name, self.dev,
41 self.ctime, self.mtime, self.uid, self.gid,
42 self.size, self.flags))
45 return struct.pack(INDEX_SIG, self.dev, self.ctime, self.mtime,
46 self.uid, self.gid, self.size, self.sha,
50 self._m[self._ofs:self._ofs+ENTLEN] = self.pack()
52 def from_stat(self, st):
53 old = (self.dev, self.ctime, self.mtime,
54 self.uid, self.gid, self.size)
55 new = (st.st_dev, int(st.st_ctime), int(st.st_mtime),
56 st.st_uid, st.st_gid, st.st_size)
58 self.ctime = int(st.st_ctime)
59 self.mtime = int(st.st_mtime)
62 self.size = st.st_size
63 self.flags |= IX_EXISTS
65 self.flags &= ~IX_HASHVALID
72 def __init__(self, filename):
73 self.filename = filename
78 f = open(filename, 'r+')
80 if e.errno == errno.ENOENT:
85 st = os.fstat(f.fileno())
87 self.m = mmap.mmap(f.fileno(), 0,
88 mmap.MAP_SHARED, mmap.PROT_READ|mmap.PROT_WRITE)
89 f.close() # map will persist beyond file close
94 while ofs < len(self.m):
95 eon = self.m.find('\0', ofs)
97 yield IxEntry(buffer(self.m, ofs, eon-ofs),
99 ofs = eon + 1 + ENTLEN
106 def ix_encode(st, sha, flags):
107 return struct.pack(INDEX_SIG, st.st_dev, int(st.st_ctime),
108 int(st.st_mtime), st.st_uid, st.st_gid,
109 st.st_size, sha, flags)
113 def __init__(self, filename):
117 self.filename = filename = os.path.realpath(filename)
118 (dir,name) = os.path.split(filename)
119 (ffd,self.tmpname) = tempfile.mkstemp('.tmp', filename, dir)
120 self.f = os.fdopen(ffd, 'wb', 65536)
130 os.unlink(self.tmpname)
137 os.rename(self.tmpname, self.filename)
139 def add(self, name, st):
140 #log('ADDING %r\n' % name)
142 assert(cmp(self.lastfile, name) > 0) # reverse order only
144 data = name + '\0' + ix_encode(st, '\0'*20, IX_EXISTS|IX_HASHVALID)
147 def add_ixentry(self, e):
149 assert(cmp(self.lastfile, e.name) > 0) # reverse order only
150 self.lastfile = e.name
151 data = e.name + '\0' + e.pack()
154 def new_reader(self):
156 return IndexReader(self.tmpname)
161 saved_errors.append(e)
165 # the use of fchdir() and lstat() are for two reasons:
166 # - help out the kernel by not making it repeatedly look up the absolute path
167 # - avoid race conditions caused by doing listdir() on a changing symlink
168 def handle_path(ri, wi, dir, name, pst):
171 #log('handle_path(%r,%r)\n' % (dir, name))
172 if stat.S_ISDIR(pst.st_mode):
173 if opt.verbose == 1: # log dirs only
174 sys.stdout.write('%s\n' % path)
177 OsFile(name).fchdir()
179 add_error(Exception('in %s: %s' % (dir, str(e))))
184 #log('* %r: %r\n' % (name, ld))
186 add_error(Exception('in %s: %s' % (path, str(e))))
193 add_error(Exception('in %s: %s' % (path, str(e))))
195 if stat.S_ISDIR(st.st_mode):
198 for p,st in reversed(sorted(lds)):
199 dirty += handle_path(ri, wi, path, p, st)
202 #log('endloop: ri.cur:%r path:%r\n' % (ri.cur.name, path))
203 while ri.cur and ri.cur.name > path:
204 #log('ricur:%r path:%r\n' % (ri.cur, path))
205 if dir and ri.cur.name.startswith(dir):
206 #log(' --- deleting\n')
207 ri.cur.flags &= ~(IX_EXISTS | IX_HASHVALID)
211 if ri.cur and ri.cur.name == path:
212 dirty += ri.cur.from_stat(pst)
214 #log(' --- updating %r\n' % path)
220 if opt.verbose > 1: # all files, not just dirs
221 sys.stdout.write('%s\n' % path)
229 except StopIteration:
233 def merge_indexes(out, r1, r2):
240 if e1 and (not e2 or e2.name < e1.name):
241 if e1.flags & IX_EXISTS:
244 elif e2 and (not e1 or e1.name < e2.name):
245 if e2.flags & IX_EXISTS:
248 elif e1.name == e2.name:
249 assert(0) # duplicate name? should never happen anymore.
250 if e2.flags & IX_EXISTS:
257 def __init__(self, l):
264 self.cur = self.i.next()
265 except StopIteration:
270 def update_index(path):
271 ri = IndexReader('index')
272 wi = IndexWriter('index')
273 rpath = os.path.realpath(path)
278 (dir, name) = os.path.split(rpath)
279 if dir and dir[-1] != '/':
281 if stat.S_ISDIR(st.st_mode) and rpath[-1] != '/':
283 rig = MergeGetter(ri)
285 dirty = handle_path(rig, wi, dir, name, st)
287 # make sure all the parents of the updated path exist and are invalidated
290 (rpath, junk) = os.path.split(rpath)
297 while rig.cur and rig.cur.name > p:
298 #log('FINISHING: %r path=%r d=%r\n' % (rig.cur.name, p, dirty))
300 if rig.cur and rig.cur.name == p:
302 rig.cur.flags &= ~IX_HASHVALID
305 wi.add(p, os.lstat(p))
311 mi = IndexWriter('index')
312 merge_indexes(mi, ri, wi.new_reader())
318 bup index [-v] <filenames...>
320 v,verbose increase log output (can be used more than once)
322 o = options.Options('bup index', optspec)
323 (opt, flags, extra) = o.parse(sys.argv[1:])
328 for ent in IndexReader('index'):
329 if not ent.flags & IX_EXISTS:
330 print 'D ' + ent.name
331 elif not ent.flags & IX_HASHVALID:
332 print 'M ' + ent.name
338 log('WARNING: %d errors encountered.\n' % len(saved_errors))