3 bup_python="$(dirname "$0")/bup-python" || exit $?
4 exec "$bup_python" "$0" ${1+"$@"}
8 from __future__ import absolute_import
9 import glob, os, sys, tempfile
11 from bup import options, git, bloom
12 from bup.compat import argv_bytes, hexstr
13 from bup.helpers import (add_error, debug1, handle_ctrl_c, log, progress, qprogress,
15 from bup.io import path_msg
19 bup bloom [options...]
21 ruin ruin the specified bloom file (clearing the bitfield)
22 f,force ignore existing bloom file and regenerate it from scratch
23 o,output= output bloom filename (default: auto)
24 d,dir= input directory to look for idx files (default: auto)
25 k,hashes= number of hash functions to use (4 or 5) (default: auto)
26 c,check= check the given .idx file against the bloom filter
30 def ruin_bloom(bloomfilename):
31 rbloomfilename = git.repo_rel(bloomfilename)
32 if not os.path.exists(bloomfilename):
33 log(path_msg(bloomfilename) + '\n')
34 add_error('bloom: %s not found to ruin\n' % path_msg(rbloomfilename))
36 b = bloom.ShaBloom(bloomfilename, readwrite=True, expected=1)
37 b.map[16 : 16 + 2**b.bits] = b'\0' * 2**b.bits
40 def check_bloom(path, bloomfilename, idx):
41 rbloomfilename = git.repo_rel(bloomfilename)
42 ridx = git.repo_rel(idx)
43 if not os.path.exists(bloomfilename):
44 log('bloom: %s: does not exist.\n' % path_msg(rbloomfilename))
46 b = bloom.ShaBloom(bloomfilename)
48 add_error('bloom: %r is invalid.\n' % path_msg(rbloomfilename))
50 base = os.path.basename(idx)
51 if base not in b.idxnames:
52 log('bloom: %s does not contain the idx.\n' % path_msg(rbloomfilename))
55 idx = os.path.join(path, idx)
56 log('bloom: bloom file: %s\n' % path_msg(rbloomfilename))
57 log('bloom: checking %s\n' % path_msg(ridx))
58 for objsha in git.open_idx(idx):
59 if not b.exists(objsha):
60 add_error('bloom: ERROR: object %s missing' % hexstr(objsha))
64 def do_bloom(path, outfilename, k):
66 assert k in (None, 4, 5)
68 if os.path.exists(outfilename) and not opt.force:
69 b = bloom.ShaBloom(outfilename)
71 debug1("bloom: Existing invalid bloom found, regenerating.\n")
78 for i, name in enumerate(glob.glob(b'%s/*.idx' % path)):
79 progress('bloom: counting: %d\r' % i)
80 ix = git.open_idx(name)
81 ixbase = os.path.basename(name)
82 if b and (ixbase in b.idxnames):
90 debug1("bloom: nothing to do.\n")
94 if len(b) != rest_count:
95 debug1("bloom: size %d != idx total %d, regenerating\n"
96 % (len(b), rest_count))
98 elif k is not None and k != b.k:
99 debug1("bloom: new k %d != existing k %d, regenerating\n"
102 elif (b.bits < bloom.MAX_BLOOM_BITS[b.k] and
103 b.pfalse_positive(add_count) > bloom.MAX_PFALSE_POSITIVE):
104 debug1("bloom: regenerating: adding %d entries gives "
105 "%.2f%% false positives.\n"
106 % (add_count, b.pfalse_positive(add_count)))
109 b = bloom.ShaBloom(outfilename, readwrite=True, expected=add_count)
110 if not b: # Need all idxs to build from scratch
112 add_count += rest_count
116 msg = b is None and 'creating from' or 'adding'
117 if not _first: _first = path
118 dirprefix = (_first != path) and git.repo_rel(path) + b': ' or b''
119 progress('bloom: %s%s %d file%s (%d object%s).\r'
120 % (path_msg(dirprefix), msg,
121 len(add), len(add)!=1 and 's' or '',
122 add_count, add_count!=1 and 's' or ''))
126 tfname = os.path.join(path, b'bup.tmp.bloom')
127 b = bloom.create(tfname, expected=add_count, k=k)
131 ix = git.open_idx(name)
132 qprogress('bloom: writing %.2f%% (%d/%d objects)\r'
133 % (icount*100.0/add_count, icount, add_count))
138 # Currently, there's an open file object for tfname inside b.
139 # Make sure it's closed before rename.
143 os.rename(tfname, outfilename)
148 o = options.Options(optspec)
149 (opt, flags, extra) = o.parse(sys.argv[1:])
152 o.fatal('no positional parameters expected')
154 if not opt.check and opt.k and opt.k not in (4,5):
155 o.fatal('only k values of 4 and 5 are supported')
158 opt.check = argv_bytes(opt.check)
160 git.check_repo_or_die()
162 output = argv_bytes(opt.output) if opt.output else None
163 paths = opt.dir and [argv_bytes(opt.dir)] or git.all_packdirs()
165 debug1('bloom: scanning %s\n' % path_msg(path))
166 outfilename = output or os.path.join(path, b'bup.bloom')
168 check_bloom(path, outfilename, opt.check)
170 ruin_bloom(outfilename)
172 do_bloom(path, outfilename, opt.k)
175 log('WARNING: %d errors encountered during bloom.\n' % len(saved_errors))
178 log('All tests passed.\n')