2 from bup import _helpers
3 from bup.helpers import *
5 BLOB_MAX = 8192*4 # 8192 is the "typical" blob size for bupsplit
6 BLOB_READ_SIZE = 1024*1024
8 progress_callback = None
11 GIT_MODE_FILE = 0100644
12 GIT_MODE_TREE = 040000
13 GIT_MODE_SYMLINK = 0120000
14 assert(GIT_MODE_TREE != 40000) # 0xxx should be treated as octal
16 # The purpose of this type of buffer is to avoid copying on peek(), get(),
17 # and eat(). We do copy the buffer contents on put(), but that should
18 # be ok if we always only put() large amounts of data at a time.
26 self.data = buffer(self.data, self.start) + s
29 def peek(self, count):
30 return buffer(self.data, self.start, count)
36 v = buffer(self.data, self.start, count)
41 return len(self.data) - self.start
44 def readfile_iter(files, progress=None):
45 for filenum,f in enumerate(files):
50 progress(filenum, len(b))
51 b = f.read(BLOB_READ_SIZE)
53 # Warning: ofs == 0 means 'done with the whole file'
54 # This will only happen here when the file is empty
61 def _splitbuf(buf, basebits, fanbits):
63 b = buf.peek(buf.used())
64 (ofs, bits) = _helpers.splitbuf(b)
70 level = (bits-basebits)//fanbits # integer division
72 yield buffer(b, 0, ofs), level
75 while buf.used() >= BLOB_MAX:
77 yield buf.get(BLOB_MAX), 0
80 def _hashsplit_iter(files, progress):
81 assert(BLOB_READ_SIZE > BLOB_MAX)
82 basebits = _helpers.blobbits()
83 fanbits = int(math.log(fanout or 128, 2))
85 for inblock in readfile_iter(files, progress):
87 for buf_and_level in _splitbuf(buf, basebits, fanbits):
90 yield buf.get(buf.used()), 0
93 def _hashsplit_iter_keep_boundaries(files, progress):
94 for real_filenum,f in enumerate(files):
96 def prog(filenum, nbytes):
97 # the inner _hashsplit_iter doesn't know the real file count,
98 # so we'll replace it here.
99 return progress(real_filenum, nbytes)
102 for buf_and_level in _hashsplit_iter([f], progress=prog):
106 def hashsplit_iter(files, keep_boundaries, progress):
108 return _hashsplit_iter_keep_boundaries(files, progress)
110 return _hashsplit_iter(files, progress)
114 def split_to_blobs(makeblob, files, keep_boundaries, progress):
116 for (blob, level) in hashsplit_iter(files, keep_boundaries, progress):
118 total_split += len(blob)
119 if progress_callback:
120 progress_callback(len(blob))
121 yield (sha, len(blob), level)
124 def _make_shalist(l):
127 total = sum(size for mode,sha,size, in l)
128 vlen = len('%x' % total)
130 for (mode, sha, size) in l:
131 shalist.append((mode, '%0*x' % (vlen,ofs), sha))
134 return (shalist, total)
137 def _squish(maketree, stacks, n):
139 while i < n or len(stacks[i]) >= MAX_PER_TREE:
140 while len(stacks) <= i+1:
142 if len(stacks[i]) == 1:
143 stacks[i+1] += stacks[i]
145 (shalist, size) = _make_shalist(stacks[i])
146 tree = maketree(shalist)
147 stacks[i+1].append((GIT_MODE_TREE, tree, size))
152 def split_to_shalist(makeblob, maketree, files,
153 keep_boundaries, progress=None):
154 sl = split_to_blobs(makeblob, files, keep_boundaries, progress)
158 for (sha,size,level) in sl:
159 shal.append((GIT_MODE_FILE, sha, size))
160 return _make_shalist(shal)[0]
163 for (sha,size,level) in sl:
164 stacks[0].append((GIT_MODE_FILE, sha, size))
165 _squish(maketree, stacks, level)
166 #log('stacks: %r\n' % [len(i) for i in stacks])
167 _squish(maketree, stacks, len(stacks)-1)
168 #log('stacks: %r\n' % [len(i) for i in stacks])
169 return _make_shalist(stacks[-1])[0]
172 def split_to_blob_or_tree(makeblob, maketree, files,
173 keep_boundaries, progress=None):
174 shalist = list(split_to_shalist(makeblob, maketree,
175 files, keep_boundaries, progress))
176 if len(shalist) == 1:
177 return (shalist[0][0], shalist[0][2])
178 elif len(shalist) == 0:
179 return (GIT_MODE_FILE, makeblob(''))
181 return (GIT_MODE_TREE, maketree(shalist))
184 def open_noatime(name):
185 fd = _helpers.open_noatime(name)
187 return os.fdopen(fd, 'rb', 1024*1024)
196 def fadvise_done(f, ofs):
198 if ofs > 0 and hasattr(f, 'fileno'):
199 _helpers.fadvise_done(f.fileno(), ofs)