3 bup_python="$(dirname "$0")/bup-python" || exit $?
4 exec "$bup_python" "$0" ${1+"$@"}
7 import sys, glob, tempfile
8 from bup import options, git, bloom
9 from bup.helpers import *
12 bup bloom [options...]
14 ruin ruin the specified bloom file (clearing the bitfield)
15 f,force ignore existing bloom file and regenerate it from scratch
16 o,output= output bloom filename (default: auto)
17 d,dir= input directory to look for idx files (default: auto)
18 k,hashes= number of hash functions to use (4 or 5) (default: auto)
19 c,check= check the given .idx file against the bloom filter
23 def ruin_bloom(bloomfilename):
24 rbloomfilename = git.repo_rel(bloomfilename)
25 if not os.path.exists(bloomfilename):
26 log("%s\n" % bloomfilename)
27 add_error("bloom: %s not found to ruin\n" % rbloomfilename)
29 b = bloom.ShaBloom(bloomfilename, readwrite=True, expected=1)
30 b.map[16:16+2**b.bits] = '\0' * 2**b.bits
33 def check_bloom(path, bloomfilename, idx):
34 rbloomfilename = git.repo_rel(bloomfilename)
35 ridx = git.repo_rel(idx)
36 if not os.path.exists(bloomfilename):
37 log("bloom: %s: does not exist.\n" % rbloomfilename)
39 b = bloom.ShaBloom(bloomfilename)
41 add_error("bloom: %r is invalid.\n" % rbloomfilename)
43 base = os.path.basename(idx)
44 if base not in b.idxnames:
45 log("bloom: %s does not contain the idx.\n" % rbloomfilename)
48 idx = os.path.join(path, idx)
49 log("bloom: bloom file: %s\n" % rbloomfilename)
50 log("bloom: checking %s\n" % ridx)
51 for objsha in git.open_idx(idx):
52 if not b.exists(objsha):
53 add_error("bloom: ERROR: object %s missing"
54 % str(objsha).encode('hex'))
58 def do_bloom(path, outfilename):
61 if os.path.exists(outfilename) and not opt.force:
62 b = bloom.ShaBloom(outfilename)
64 debug1("bloom: Existing invalid bloom found, regenerating.\n")
71 for i,name in enumerate(glob.glob('%s/*.idx' % path)):
72 progress('bloom: counting: %d\r' % i)
73 ix = git.open_idx(name)
74 ixbase = os.path.basename(name)
75 if b and (ixbase in b.idxnames):
81 total = add_count + rest_count
84 debug1("bloom: nothing to do.\n")
88 if len(b) != rest_count:
89 debug1("bloom: size %d != idx total %d, regenerating\n"
90 % (len(b), rest_count))
92 elif (b.bits < bloom.MAX_BLOOM_BITS and
93 b.pfalse_positive(add_count) > bloom.MAX_PFALSE_POSITIVE):
94 debug1("bloom: regenerating: adding %d entries gives "
95 "%.2f%% false positives.\n"
96 % (add_count, b.pfalse_positive(add_count)))
99 b = bloom.ShaBloom(outfilename, readwrite=True, expected=add_count)
100 if not b: # Need all idxs to build from scratch
102 add_count += rest_count
106 msg = b is None and 'creating from' or 'adding'
107 if not _first: _first = path
108 dirprefix = (_first != path) and git.repo_rel(path)+': ' or ''
109 progress('bloom: %s%s %d file%s (%d object%s).\n'
111 len(add), len(add)!=1 and 's' or '',
112 add_count, add_count!=1 and 's' or ''))
116 tfname = os.path.join(path, 'bup.tmp.bloom')
117 b = bloom.create(tfname, expected=add_count, k=opt.k)
121 ix = git.open_idx(name)
122 qprogress('bloom: writing %.2f%% (%d/%d objects)\r'
123 % (icount*100.0/add_count, icount, add_count))
128 # Currently, there's an open file object for tfname inside b.
129 # Make sure it's closed before rename.
133 os.rename(tfname, outfilename)
138 o = options.Options(optspec)
139 (opt, flags, extra) = o.parse(sys.argv[1:])
142 o.fatal('no positional parameters expected')
144 git.check_repo_or_die()
146 if not opt.check and opt.k and opt.k not in (4,5):
147 o.fatal('only k values of 4 and 5 are supported')
149 paths = opt.dir and [opt.dir] or git.all_packdirs()
151 debug1('bloom: scanning %s\n' % path)
152 outfilename = opt.output or os.path.join(path, 'bup.bloom')
154 check_bloom(path, outfilename, opt.check)
156 ruin_bloom(outfilename)
158 do_bloom(path, outfilename)
161 log('WARNING: %d errors encountered during bloom.\n' % len(saved_errors))
164 log('All tests passed.\n')