-import errno, metadata, os, stat, struct, tempfile
-from bup import xstat
+from __future__ import absolute_import, print_function
+import errno, os, stat, struct, tempfile
+
+from bup import compat, 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)
-EMPTY_SHA = '\0'*20
-FAKE_SHA = '\x01'*20
+EMPTY_SHA = b'\0' * 20
+FAKE_SHA = b'\x01' * 20
-INDEX_HDR = 'BUPI\0\0\0\7'
+INDEX_HDR = b'BUPI\0\0\0\7'
# Time values are handled as integer nanoseconds since the epoch in
# memory, but are written as xstat/metadata timespecs. This behavior
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
self.children_n = 0
def __repr__(self):
- return ("(%s,0x%04x,%d,%d,%d,%d,%d,%d,%s/%s,0x%04x,%d,0x%08x/%d)"
+ return ("(%r,0x%04x,%d,%d,%d,%d,%d,%d,%s/%s,0x%04x,%d,0x%08x/%d)"
% (self.name, self.dev, self.ino, self.nlink,
self.ctime, self.mtime, self.atime,
self.size, self.mode, self.gitmode,
def __eq__(self, other):
return self._cmp(other) == 0
- def __ne__():
+ def __ne__(self, other):
return self._cmp(other) != 0
def __lt__(self, other):
def __gt__(self, other):
return self._cmp(other) > 0
- def __le__():
+ def __le__(self, other):
return self._cmp(other) <= 0
- def __ge__():
+ def __ge__(self, other):
return self._cmp(other) >= 0
def write(self, f):
- f.write(self.basename + '\0' + self.packed())
+ f.write(self.basename + b'\0' + self.packed())
class NewEntry(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))
def iter(self, name=None, wantrecurse=None):
dname = name
- if dname and not dname.endswith('/'):
- dname += '/'
+ if dname and not dname.endswith(b'/'):
+ dname += b'/'
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):
- eon = self._m.find('\0', ofs)
+ for i in range(self.children_n):
+ eon = self._m.find(b'\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
or child.name.startswith(dname)
- or child.name.endswith('/') and dname.startswith(child.name)):
+ or child.name.endswith(b'/') and dname.startswith(child.name)):
if not wantrecurse or wantrecurse(child):
for e in child.iter(name=name, wantrecurse=wantrecurse):
yield e
class Reader:
def __init__(self, filename):
self.filename = filename
- self.m = ''
+ self.m = b''
self.writable = False
self.count = 0
f = None
try:
- f = open(filename, 'r+')
+ f = open(filename, 'rb+')
except IOError as e:
if e.errno == errno.ENOENT:
pass
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()
def forward_iter(self):
ofs = len(INDEX_HDR)
while ofs+ENTLEN <= len(self.m)-FOOTLEN:
- eon = self.m.find('\0', ofs)
+ eon = self.m.find(b'\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)]
yield ExistingEntry(None, basename, basename, self.m, eon+1)
ofs = eon + 1 + ENTLEN
def iter(self, name=None, wantrecurse=None):
if len(self.m) > len(INDEX_HDR)+ENTLEN:
dname = name
- if dname and not dname.endswith('/'):
- dname += '/'
- root = ExistingEntry(None, '/', '/',
+ if dname and not dname.endswith(b'/'):
+ dname += b'/'
+ root = ExistingEntry(None, b'/', b'/',
self.m, len(self.m)-FOOTLEN-ENTLEN)
for sub in root.iter(name=name, wantrecurse=wantrecurse):
yield sub
# in an odd way and depends on a terminating '/' to indicate directories.
def pathsplit(p):
"""Split a path into a list of elements of the file system hierarchy."""
- l = p.split('/')
- l = [i+'/' for i in l[:-1]] + l[-1:]
- if l[-1] == '':
+ l = p.split(b'/')
+ l = [i + b'/' for i in l[:-1]] + l[-1:]
+ if l[-1] == b'':
l.pop() # extra blank caused by terminating '/'
return l
self.metastore = metastore
self.tmax = tmax
(dir,name) = os.path.split(filename)
- (ffd,self.tmpname) = tempfile.mkstemp('.tmp', filename, dir)
+ ffd, self.tmpname = tempfile.mkstemp(b'.tmp', filename, dir)
self.f = os.fdopen(ffd, 'wb', 65536)
self.f.write(INDEX_HDR)
self.metastore, self.tmax)
def add(self, name, st, meta_ofs, hashgen = None):
- endswith = name.endswith('/')
+ endswith = name.endswith(b'/')
ename = pathsplit(name)
basename = ename[-1]
#log('add: %r %r\n' % (basename, name))
for p in paths:
rp = _slashappend_or_add_error(resolve_parent(p), 'reduce_paths')
if rp:
- xpaths.append((rp, slashappend(p) if rp.endswith('/') else p))
+ xpaths.append((rp, slashappend(p) if rp.endswith(b'/') else p))
xpaths.sort()
paths = []
prev = None
for (rp, p) in xpaths:
if prev and (prev == rp
- or (prev.endswith('/') and rp.startswith(prev))):
+ or (prev.endswith(b'/') and rp.startswith(prev))):
continue # already superceded by previous path
paths.append((rp, p))
prev = rp