2 from __future__ import absolute_import, print_function
3 import errno, os, stat, struct, tempfile
5 from bup import compat, metadata, xstat
6 from bup._helpers import UINT_MAX, bytescmp
7 from bup.compat import range
8 from bup.helpers import (add_error, log, merge_iter, mmap_readwrite,
9 progress, qprogress, resolve_parent, slashappend)
11 EMPTY_SHA = b'\0' * 20
12 FAKE_SHA = b'\x01' * 20
14 INDEX_HDR = b'BUPI\0\0\0\7'
16 # Time values are handled as integer nanoseconds since the epoch in
17 # memory, but are written as xstat/metadata timespecs. This behavior
18 # matches the existing metadata/xstat/.bupm code.
20 # Record times (mtime, ctime, atime) as xstat/metadata timespecs, and
21 # store all of the times in the index so they won't interfere with the
22 # forthcoming metadata cache.
27 'qQ' # ctime_s, ctime_ns
28 'qQ' # mtime_s, mtime_ns
29 'qQ' # atime_s, atime_ns
39 ENTLEN = struct.calcsize(INDEX_SIG)
41 FOOTLEN = struct.calcsize(FOOTER_SIG)
43 IX_EXISTS = 0x8000 # file exists on filesystem
44 IX_HASHVALID = 0x4000 # the stored sha1 matches the filesystem
45 IX_SHAMISSING = 0x2000 # the stored sha1 object doesn't seem to exist
47 class Error(Exception):
51 class MetaStoreReader:
52 def __init__(self, filename):
54 self._file = open(filename, 'rb')
64 def metadata_at(self, ofs):
66 return metadata.Metadata.read(self._file)
69 class MetaStoreWriter:
70 # For now, we just append to the file, and try to handle any
71 # truncation or corruption somewhat sensibly.
73 def __init__(self, filename):
74 # Map metadata hashes to bupindex.meta offsets.
76 self._filename = filename
78 # FIXME: see how slow this is; does it matter?
79 m_file = open(filename, 'ab+')
84 m = metadata.Metadata.read(m_file)
86 m_encoded = m.encode()
87 self._offsets[m_encoded] = m_off
89 m = metadata.Metadata.read(m_file)
93 log('index metadata in %r appears to be corrupt' % filename)
97 self._file = open(filename, 'ab')
108 def store(self, metadata):
109 meta_encoded = metadata.encode(include_path=False)
110 ofs = self._offsets.get(meta_encoded)
113 ofs = self._file.tell()
114 self._file.write(meta_encoded)
115 self._offsets[meta_encoded] = ofs
120 def __init__(self, ename, parent):
127 (ofs,n) = (f.tell(), len(self.list))
129 count = len(self.list)
130 #log('popping %r with %d entries\n'
131 # % (''.join(self.ename), count))
135 self.parent.count += count + self.count
139 def _golevel(level, f, ename, newentry, metastore, tmax):
140 # close nodes back up the tree
142 default_meta_ofs = metastore.store(metadata.Metadata())
143 while ename[:len(level.ename)] != level.ename:
144 n = BlankNewEntry(level.ename[-1], default_meta_ofs, tmax)
146 (n.children_ofs,n.children_n) = level.write(f)
147 level.parent.list.append(n)
150 # create nodes down the tree
151 while len(level.ename) < len(ename):
152 level = Level(ename[:len(level.ename)+1], level)
154 # are we in precisely the right place?
155 assert(ename == level.ename)
157 BlankNewEntry(ename and level.ename[-1] or None, default_meta_ofs, tmax)
158 (n.children_ofs,n.children_n) = level.write(f)
160 level.parent.list.append(n)
167 def __init__(self, basename, name, meta_ofs, tmax):
168 assert basename is None or type(basename) == bytes
169 assert name is None or type(name) == bytes
170 self.basename = basename
172 self.meta_ofs = meta_ofs
174 self.children_ofs = 0
178 return ("(%r,0x%04x,%d,%d,%d,%d,%d,%d,%s/%s,0x%04x,%d,0x%08x/%d)"
179 % (self.name, self.dev, self.ino, self.nlink,
180 self.ctime, self.mtime, self.atime,
181 self.size, self.mode, self.gitmode,
182 self.flags, self.meta_ofs,
183 self.children_ofs, self.children_n))
187 ctime = xstat.nsecs_to_timespec(self.ctime)
188 mtime = xstat.nsecs_to_timespec(self.mtime)
189 atime = xstat.nsecs_to_timespec(self.atime)
190 return struct.pack(INDEX_SIG,
191 self.dev, self.ino, self.nlink,
195 self.size, self.mode,
196 self.gitmode, self.sha, self.flags,
197 self.children_ofs, self.children_n,
199 except (DeprecationWarning, struct.error) as e:
200 log('pack error: %s (%r)\n' % (e, self))
203 def stale(self, st, check_device=True):
204 if self.size != st.st_size:
206 if self.mtime != st.st_mtime:
208 if self.sha == EMPTY_SHA:
212 if self.ctime != st.st_ctime:
214 if self.ino != st.st_ino:
216 if self.nlink != st.st_nlink:
218 if not (self.flags & IX_EXISTS):
220 if check_device and (self.dev != st.st_dev):
224 def update_from_stat(self, st, meta_ofs):
225 # Should only be called when the entry is stale(), and
226 # invalidate() should almost certainly be called afterward.
229 self.nlink = st.st_nlink
230 self.ctime = st.st_ctime
231 self.mtime = st.st_mtime
232 self.atime = st.st_atime
233 self.size = st.st_size
234 self.mode = st.st_mode
235 self.flags |= IX_EXISTS
236 self.meta_ofs = meta_ofs
240 self.mtime = self._fixup_time(self.mtime)
241 self.ctime = self._fixup_time(self.ctime)
243 def _fixup_time(self, t):
244 if self.tmax != None and t > self.tmax:
250 f = IX_HASHVALID|IX_EXISTS
251 return (self.flags & f) == f
253 def invalidate(self):
254 self.flags &= ~IX_HASHVALID
256 def validate(self, gitmode, sha):
259 assert(gitmode+0 == gitmode)
260 self.gitmode = gitmode
262 self.flags |= IX_HASHVALID|IX_EXISTS
265 return not self.is_deleted()
267 def sha_missing(self):
268 return (self.flags & IX_SHAMISSING) or not (self.flags & IX_HASHVALID)
270 def is_deleted(self):
271 return (self.flags & IX_EXISTS) == 0
273 def set_deleted(self):
274 if self.flags & IX_EXISTS:
275 self.flags &= ~(IX_EXISTS | IX_HASHVALID)
278 return not self.is_fake()
281 return not self.ctime
283 def _cmp(self, other):
284 # Note reversed name ordering
285 bc = bytescmp(other.name, self.name)
288 vc = self.is_valid() - other.is_valid()
291 fc = self.is_fake() - other.is_fake()
296 def __eq__(self, other):
297 return self._cmp(other) == 0
299 def __ne__(self, other):
300 return self._cmp(other) != 0
302 def __lt__(self, other):
303 return self._cmp(other) < 0
305 def __gt__(self, other):
306 return self._cmp(other) > 0
308 def __le__(self, other):
309 return self._cmp(other) <= 0
311 def __ge__(self, other):
312 return self._cmp(other) >= 0
315 f.write(self.basename + b'\0' + self.packed())
318 class NewEntry(Entry):
319 def __init__(self, basename, name, tmax, dev, ino, nlink,
321 size, mode, gitmode, sha, flags, meta_ofs,
322 children_ofs, children_n):
323 Entry.__init__(self, basename, name, meta_ofs, tmax)
324 (self.dev, self.ino, self.nlink, self.ctime, self.mtime, self.atime,
325 self.size, self.mode, self.gitmode, self.sha,
326 self.flags, self.children_ofs, self.children_n
327 ) = (dev, ino, nlink, ctime, mtime, atime,
328 size, mode, gitmode, sha, flags, children_ofs, children_n)
332 class BlankNewEntry(NewEntry):
333 def __init__(self, basename, meta_ofs, tmax):
334 NewEntry.__init__(self, basename, basename, tmax,
335 0, 0, 0, 0, 0, 0, 0, 0,
336 0, EMPTY_SHA, 0, meta_ofs, 0, 0)
339 class ExistingEntry(Entry):
340 def __init__(self, parent, basename, name, m, ofs):
341 Entry.__init__(self, basename, name, None, None)
345 (self.dev, self.ino, self.nlink,
346 self.ctime, ctime_ns, self.mtime, mtime_ns, self.atime, atime_ns,
347 self.size, self.mode, self.gitmode, self.sha,
348 self.flags, self.children_ofs, self.children_n, self.meta_ofs
349 ) = struct.unpack(INDEX_SIG, m[ofs : ofs + ENTLEN])
350 self.atime = xstat.timespec_to_nsecs((self.atime, atime_ns))
351 self.mtime = xstat.timespec_to_nsecs((self.mtime, mtime_ns))
352 self.ctime = xstat.timespec_to_nsecs((self.ctime, ctime_ns))
354 # effectively, we don't bother messing with IX_SHAMISSING if
355 # not IX_HASHVALID, since it's redundant, and repacking is more
356 # expensive than not repacking.
357 # This is implemented by having sha_missing() check IX_HASHVALID too.
358 def set_sha_missing(self, val):
360 oldval = self.sha_missing() and 1 or 0
362 flag = val and IX_SHAMISSING or 0
363 newflags = (self.flags & (~IX_SHAMISSING)) | flag
364 self.flags = newflags
367 def unset_sha_missing(self, flag):
368 if self.flags & IX_SHAMISSING:
369 self.flags &= ~IX_SHAMISSING
373 self._m[self._ofs:self._ofs+ENTLEN] = self.packed()
374 if self.parent and not self.is_valid():
375 self.parent.invalidate()
378 def iter(self, name=None, wantrecurse=None):
380 if dname and not dname.endswith(b'/'):
382 ofs = self.children_ofs
383 assert(ofs <= len(self._m))
384 assert(self.children_n <= UINT_MAX) # i.e. python struct 'I'
385 for i in range(self.children_n):
386 eon = self._m.find(b'\0', ofs)
390 basename = self._m[ofs : ofs + (eon - ofs)]
391 child = ExistingEntry(self, basename, self.name + basename,
394 or child.name.startswith(dname)
395 or child.name.endswith(b'/') and dname.startswith(child.name)):
396 if not wantrecurse or wantrecurse(child):
397 for e in child.iter(name=name, wantrecurse=wantrecurse):
399 if not name or child.name == name or child.name.startswith(dname):
401 ofs = eon + 1 + ENTLEN
408 def __init__(self, filename):
409 self.filename = filename
411 self.writable = False
415 f = open(filename, 'rb+')
417 if e.errno == errno.ENOENT:
422 b = f.read(len(INDEX_HDR))
424 log('warning: %s: header: expected %r, got %r\n'
425 % (filename, INDEX_HDR, b))
427 st = os.fstat(f.fileno())
429 self.m = mmap_readwrite(f)
431 self.count = struct.unpack(FOOTER_SIG,
432 self.m[st.st_size - FOOTLEN
439 return int(self.count)
441 def forward_iter(self):
443 while ofs+ENTLEN <= len(self.m)-FOOTLEN:
444 eon = self.m.find(b'\0', ofs)
448 basename = self.m[ofs : ofs + (eon - ofs)]
449 yield ExistingEntry(None, basename, basename, self.m, eon+1)
450 ofs = eon + 1 + ENTLEN
452 def iter(self, name=None, wantrecurse=None):
453 if len(self.m) > len(INDEX_HDR)+ENTLEN:
455 if dname and not dname.endswith(b'/'):
457 root = ExistingEntry(None, b'/', b'/',
458 self.m, len(self.m)-FOOTLEN-ENTLEN)
459 for sub in root.iter(name=name, wantrecurse=wantrecurse):
461 if not dname or dname == root.name:
467 def find(self, name):
468 return next((e for e in self.iter(name, wantrecurse=lambda x : True)
476 if self.writable and self.m:
481 if self.writable and self.m:
484 self.writable = False
486 def filter(self, prefixes, wantrecurse=None):
487 for (rp, path) in reduce_paths(prefixes):
489 for e in self.iter(rp, wantrecurse=wantrecurse):
491 assert(e.name.startswith(rp))
492 name = path + e.name[len(rp):]
495 # Always return at least the top for each prefix.
496 # Otherwise something like "save x/y" will produce
497 # nothing if x is up to date.
500 name = path + pe.name[len(rp):]
503 # FIXME: this function isn't very generic, because it splits the filename
504 # in an odd way and depends on a terminating '/' to indicate directories.
506 """Split a path into a list of elements of the file system hierarchy."""
508 l = [i + b'/' for i in l[:-1]] + l[-1:]
510 l.pop() # extra blank caused by terminating '/'
515 def __init__(self, filename, metastore, tmax):
516 self.rootlevel = self.level = Level([], None)
521 self.filename = filename = resolve_parent(filename)
522 self.metastore = metastore
524 (dir,name) = os.path.split(filename)
525 ffd, self.tmpname = tempfile.mkstemp(b'.tmp', filename, dir)
526 self.f = os.fdopen(ffd, 'wb', 65536)
527 self.f.write(INDEX_HDR)
537 os.unlink(self.tmpname)
541 self.level = _golevel(self.level, self.f, [], None,
542 self.metastore, self.tmax)
543 self.count = self.rootlevel.count
546 self.f.write(struct.pack(FOOTER_SIG, self.count))
548 assert(self.level == None)
556 os.rename(self.tmpname, self.filename)
558 def _add(self, ename, entry):
559 if self.lastfile and self.lastfile <= ename:
560 raise Error('%r must come before %r'
561 % (''.join(ename), ''.join(self.lastfile)))
562 self.lastfile = ename
563 self.level = _golevel(self.level, self.f, ename, entry,
564 self.metastore, self.tmax)
566 def add(self, name, st, meta_ofs, hashgen = None):
567 endswith = name.endswith(b'/')
568 ename = pathsplit(name)
570 #log('add: %r %r\n' % (basename, name))
574 (gitmode, sha) = hashgen(name)
575 flags |= IX_HASHVALID
577 (gitmode, sha) = (0, EMPTY_SHA)
579 isdir = stat.S_ISDIR(st.st_mode)
580 assert(isdir == endswith)
581 e = NewEntry(basename, name, self.tmax,
582 st.st_dev, st.st_ino, st.st_nlink,
583 st.st_ctime, st.st_mtime, st.st_atime,
584 st.st_size, st.st_mode, gitmode, sha, flags,
588 meta_ofs = self.metastore.store(metadata.Metadata())
589 e = BlankNewEntry(basename, meta_ofs, self.tmax)
595 def add_ixentry(self, e):
596 e.children_ofs = e.children_n = 0
597 self._add(pathsplit(e.name), e)
599 def new_reader(self):
601 return Reader(self.tmpname)
604 def _slashappend_or_add_error(p, caller):
605 """Return p, after ensuring it has a single trailing slash if it names
606 a directory, unless there's an OSError, in which case, call
607 add_error() and return None."""
611 add_error('%s: %s' % (caller, e))
614 if stat.S_ISDIR(st.st_mode):
615 return slashappend(p)
619 def unique_resolved_paths(paths):
620 "Return a collection of unique resolved paths."
621 rps = (_slashappend_or_add_error(resolve_parent(p), 'unique_resolved_paths')
623 return frozenset((x for x in rps if x is not None))
626 def reduce_paths(paths):
629 rp = _slashappend_or_add_error(resolve_parent(p), 'reduce_paths')
631 xpaths.append((rp, slashappend(p) if rp.endswith(b'/') else p))
636 for (rp, p) in xpaths:
637 if prev and (prev == rp
638 or (prev.endswith(b'/') and rp.startswith(prev))):
639 continue # already superceded by previous path
640 paths.append((rp, p))
642 paths.sort(reverse=True)
647 def pfunc(count, total):
648 qprogress('bup: merging indexes (%d/%d)\r' % (count, total))
649 def pfinal(count, total):
650 progress('bup: merging indexes (%d/%d), done.\n' % (count, total))
651 return merge_iter(iters, 1024, pfunc, pfinal, key='name')