2 from bup import _helpers
3 from bup.helpers import *
9 progress_callback = None
10 max_pack_size = 1000*1000*1000 # larger packs will slow down pruning
11 max_pack_objects = 200*1000 # cache memory usage is about 83 bytes per object
21 self.data = buffer(self.data, self.start) + s
24 def peek(self, count):
25 return buffer(self.data, self.start, count)
31 v = buffer(self.data, self.start, count)
36 return len(self.data) - self.start
40 b = buf.peek(buf.used())
41 (ofs, bits) = _helpers.splitbuf(b)
44 return (buffer(b, 0, ofs), bits)
52 fadvise_done(f, max(0, ofs - 1024*1024))
61 def drainbuf(buf, finalize):
63 (blob, bits) = splitbuf(buf)
68 if buf.used() > BLOB_MAX:
70 yield (buf.get(buf.used()), 0)
71 elif finalize and buf.used():
72 yield (buf.get(buf.used()), 0)
75 def _hashsplit_iter(files):
76 assert(BLOB_HWM > BLOB_MAX)
80 for i in drainbuf(buf, finalize=False):
82 while buf.used() < BLOB_HWM:
86 for i in drainbuf(buf, finalize=True):
92 def _hashsplit_iter_keep_boundaries(files):
94 for i in _hashsplit_iter([f]):
98 def hashsplit_iter(files, keep_boundaries):
100 return _hashsplit_iter_keep_boundaries(files)
102 return _hashsplit_iter(files)
106 def _split_to_blobs(w, files, keep_boundaries):
108 for (blob, bits) in hashsplit_iter(files, keep_boundaries):
109 sha = w.new_blob(blob)
110 total_split += len(blob)
111 if w.outbytes >= max_pack_size or w.count >= max_pack_objects:
113 if progress_callback:
114 progress_callback(len(blob))
115 yield (sha, len(blob), bits)
118 def _make_shalist(l):
121 for (mode, sha, size) in l:
122 shalist.append((mode, '%016x' % ofs, sha))
125 return (shalist, total)
128 def _squish(w, stacks, n):
130 while i<n or len(stacks[i]) > MAX_PER_TREE:
131 while len(stacks) <= i+1:
133 if len(stacks[i]) == 1:
134 stacks[i+1] += stacks[i]
136 (shalist, size) = _make_shalist(stacks[i])
137 tree = w.new_tree(shalist)
138 stacks[i+1].append(('40000', tree, size))
143 def split_to_shalist(w, files, keep_boundaries):
144 sl = _split_to_blobs(w, files, keep_boundaries)
147 for (sha,size,bits) in sl:
148 shal.append(('100644', sha, size))
149 return _make_shalist(shal)[0]
151 base_bits = _helpers.blobbits()
152 fanout_bits = int(math.log(fanout, 2))
154 assert(n >= base_bits)
155 return (n - base_bits)/fanout_bits
157 for (sha,size,bits) in sl:
159 stacks[0].append(('100644', sha, size))
161 _squish(w, stacks, bits_to_idx(bits))
162 #log('stacks: %r\n' % [len(i) for i in stacks])
163 _squish(w, stacks, len(stacks)-1)
164 #log('stacks: %r\n' % [len(i) for i in stacks])
165 return _make_shalist(stacks[-1])[0]
168 def split_to_blob_or_tree(w, files, keep_boundaries):
169 shalist = list(split_to_shalist(w, files, keep_boundaries))
170 if len(shalist) == 1:
171 return (shalist[0][0], shalist[0][2])
172 elif len(shalist) == 0:
173 return ('100644', w.new_blob(''))
175 return ('40000', w.new_tree(shalist))
178 def open_noatime(name):
179 fd = _helpers.open_noatime(name)
181 return os.fdopen(fd, 'rb', 1024*1024)
190 def fadvise_done(f, ofs):
193 _helpers.fadvise_done(f.fileno(), ofs)