]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/index.py
Remove inefficient (or will be) uses of buffer
[bup.git] / lib / bup / index.py
index a570784da81c5c2a7e6b30cb2922fe30975d40e7..a03a2dd9edaa135586d270b18ba4445526de1ce4 100644 (file)
@@ -1,7 +1,10 @@
-import errno, metadata, os, stat, struct, tempfile
 
-from bup import xstat
-from bup._helpers import UINT_MAX
+from __future__ import absolute_import
+import errno, os, stat, struct, tempfile
+
+from bup import metadata, xstat
+from bup._helpers import UINT_MAX, bytescmp
+from bup.compat import range
 from bup.helpers import (add_error, log, merge_iter, mmap_readwrite,
                          progress, qprogress, resolve_parent, slashappend)
 
@@ -162,8 +165,10 @@ def _golevel(level, f, ename, newentry, metastore, tmax):
 
 class Entry:
     def __init__(self, basename, name, meta_ofs, tmax):
-        self.basename = str(basename)
-        self.name = str(name)
+        assert basename is None or type(basename) == bytes
+        assert name is None or type(name) == bytes
+        self.basename = basename
+        self.name = name
         self.meta_ofs = meta_ofs
         self.tmax = tmax
         self.children_ofs = 0
@@ -195,13 +200,34 @@ class Entry:
             log('pack error: %s (%r)\n' % (e, self))
             raise
 
-    def from_stat(self, st, meta_ofs, tstart, check_device=True):
-        old = (self.dev if check_device else 0,
-               self.ino, self.nlink, self.ctime, self.mtime,
-               self.size, self.flags & IX_EXISTS)
-        new = (st.st_dev if check_device else 0,
-               st.st_ino, st.st_nlink, st.st_ctime, st.st_mtime,
-               st.st_size, IX_EXISTS)
+    def stale(self, st, tstart, check_device=True):
+        if self.size != st.st_size:
+            return True
+        if self.mtime != st.st_mtime:
+            return True
+        if self.sha == EMPTY_SHA:
+            return True
+        if not self.gitmode:
+            return True
+        if self.ctime != st.st_ctime:
+            return True
+        if self.ino != st.st_ino:
+            return True
+        if self.nlink != st.st_nlink:
+            return True
+        if not (self.flags & IX_EXISTS):
+            return True
+        if check_device and (self.dev != st.st_dev):
+            return True
+        # Check that the ctime's "second" is at or after tstart's.
+        ctime_sec_in_ns = xstat.fstime_floor_secs(st.st_ctime) * 10**9
+        if ctime_sec_in_ns >= tstart:
+            return True
+        return False
+
+    def update_from_stat(self, st, meta_ofs):
+        # Should only be called when the entry is stale(), and
+        # invalidate() should almost certainly be called afterward.
         self.dev = st.st_dev
         self.ino = st.st_ino
         self.nlink = st.st_nlink
@@ -212,13 +238,8 @@ class Entry:
         self.mode = st.st_mode
         self.flags |= IX_EXISTS
         self.meta_ofs = meta_ofs
-        # Check that the ctime's "second" is at or after tstart's.
-        ctime_sec_in_ns = xstat.fstime_floor_secs(st.st_ctime) * 10**9
-        if ctime_sec_in_ns >= tstart or old != new \
-              or self.sha == EMPTY_SHA or not self.gitmode:
-            self.invalidate()
         self._fixup()
-        
+
     def _fixup(self):
         self.mtime = self._fixup_time(self.mtime)
         self.ctime = self._fixup_time(self.ctime)
@@ -263,10 +284,36 @@ class Entry:
     def is_fake(self):
         return not self.ctime
 
-    def __cmp__(a, b):
-        return (cmp(b.name, a.name)
-                or cmp(a.is_valid(), b.is_valid())
-                or cmp(a.is_fake(), b.is_fake()))
+    def _cmp(self, other):
+        # Note reversed name ordering
+        bc = bytescmp(other.name, self.name)
+        if bc != 0:
+            return bc
+        vc = self.is_valid() - other.is_valid()
+        if vc != 0:
+            return vc
+        fc = self.is_fake() - other.is_fake()
+        if fc != 0:
+            return fc
+        return 0
+
+    def __eq__(self, other):
+        return self._cmp(other) == 0
+
+    def __ne__():
+        return self._cmp(other) != 0
+
+    def __lt__(self, other):
+        return self._cmp(other) < 0
+
+    def __gt__(self, other):
+        return self._cmp(other) > 0
+
+    def __le__():
+        return self._cmp(other) <= 0
+
+    def __ge__():
+        return self._cmp(other) >= 0
 
     def write(self, f):
         f.write(self.basename + '\0' + self.packed())
@@ -303,7 +350,7 @@ class ExistingEntry(Entry):
          self.ctime, ctime_ns, self.mtime, mtime_ns, self.atime, atime_ns,
          self.size, self.mode, self.gitmode, self.sha,
          self.flags, self.children_ofs, self.children_n, self.meta_ofs
-         ) = struct.unpack(INDEX_SIG, str(buffer(m, ofs, ENTLEN)))
+         ) = struct.unpack(INDEX_SIG, m[ofs : ofs + ENTLEN])
         self.atime = xstat.timespec_to_nsecs((self.atime, atime_ns))
         self.mtime = xstat.timespec_to_nsecs((self.mtime, mtime_ns))
         self.ctime = xstat.timespec_to_nsecs((self.ctime, ctime_ns))
@@ -339,12 +386,12 @@ class ExistingEntry(Entry):
         ofs = self.children_ofs
         assert(ofs <= len(self._m))
         assert(self.children_n <= UINT_MAX)  # i.e. python struct 'I'
-        for i in xrange(self.children_n):
+        for i in range(self.children_n):
             eon = self._m.find('\0', ofs)
             assert(eon >= 0)
             assert(eon >= ofs)
             assert(eon > ofs)
-            basename = str(buffer(self._m, ofs, eon-ofs))
+            basename = self._m[ofs : ofs + (eon - ofs)]
             child = ExistingEntry(self, basename, self.name + basename,
                                   self._m, eon+1)
             if (not dname
@@ -386,7 +433,8 @@ class Reader:
                     self.m = mmap_readwrite(f)
                     self.writable = True
                     self.count = struct.unpack(FOOTER_SIG,
-                          str(buffer(self.m, st.st_size-FOOTLEN, FOOTLEN)))[0]
+                                               self.m[st.st_size - FOOTLEN
+                                                      : st.st_size])[0]
 
     def __del__(self):
         self.close()
@@ -401,7 +449,7 @@ class Reader:
             assert(eon >= 0)
             assert(eon >= ofs)
             assert(eon > ofs)
-            basename = str(buffer(self.m, ofs, eon-ofs))
+            basename = self.m[ofs : ofs + (eon - ofs)]
             yield ExistingEntry(None, basename, basename, self.m, eon+1)
             ofs = eon + 1 + ENTLEN