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.helpers import (add_error, debug1, handle_ctrl_c, log, progress, qprogress,
16 bup bloom [options...]
18 ruin ruin the specified bloom file (clearing the bitfield)
19 f,force ignore existing bloom file and regenerate it from scratch
20 o,output= output bloom filename (default: auto)
21 d,dir= input directory to look for idx files (default: auto)
22 k,hashes= number of hash functions to use (4 or 5) (default: auto)
23 c,check= check the given .idx file against the bloom filter
27 def ruin_bloom(bloomfilename):
28 rbloomfilename = git.repo_rel(bloomfilename)
29 if not os.path.exists(bloomfilename):
30 log("%s\n" % bloomfilename)
31 add_error("bloom: %s not found to ruin\n" % rbloomfilename)
33 b = bloom.ShaBloom(bloomfilename, readwrite=True, expected=1)
34 b.map[16:16+2**b.bits] = '\0' * 2**b.bits
37 def check_bloom(path, bloomfilename, idx):
38 rbloomfilename = git.repo_rel(bloomfilename)
39 ridx = git.repo_rel(idx)
40 if not os.path.exists(bloomfilename):
41 log("bloom: %s: does not exist.\n" % rbloomfilename)
43 b = bloom.ShaBloom(bloomfilename)
45 add_error("bloom: %r is invalid.\n" % rbloomfilename)
47 base = os.path.basename(idx)
48 if base not in b.idxnames:
49 log("bloom: %s does not contain the idx.\n" % rbloomfilename)
52 idx = os.path.join(path, idx)
53 log("bloom: bloom file: %s\n" % rbloomfilename)
54 log("bloom: checking %s\n" % ridx)
55 for objsha in git.open_idx(idx):
56 if not b.exists(objsha):
57 add_error("bloom: ERROR: object %s missing"
58 % str(objsha).encode('hex'))
62 def do_bloom(path, outfilename):
65 if os.path.exists(outfilename) and not opt.force:
66 b = bloom.ShaBloom(outfilename)
68 debug1("bloom: Existing invalid bloom found, regenerating.\n")
75 for i,name in enumerate(glob.glob('%s/*.idx' % path)):
76 progress('bloom: counting: %d\r' % i)
77 ix = git.open_idx(name)
78 ixbase = os.path.basename(name)
79 if b and (ixbase in b.idxnames):
85 total = add_count + rest_count
88 debug1("bloom: nothing to do.\n")
92 if len(b) != rest_count:
93 debug1("bloom: size %d != idx total %d, regenerating\n"
94 % (len(b), rest_count))
96 elif (b.bits < bloom.MAX_BLOOM_BITS and
97 b.pfalse_positive(add_count) > bloom.MAX_PFALSE_POSITIVE):
98 debug1("bloom: regenerating: adding %d entries gives "
99 "%.2f%% false positives.\n"
100 % (add_count, b.pfalse_positive(add_count)))
103 b = bloom.ShaBloom(outfilename, readwrite=True, expected=add_count)
104 if not b: # Need all idxs to build from scratch
106 add_count += rest_count
110 msg = b is None and 'creating from' or 'adding'
111 if not _first: _first = path
112 dirprefix = (_first != path) and git.repo_rel(path)+': ' or ''
113 progress('bloom: %s%s %d file%s (%d object%s).\r'
115 len(add), len(add)!=1 and 's' or '',
116 add_count, add_count!=1 and 's' or ''))
120 tfname = os.path.join(path, 'bup.tmp.bloom')
121 b = bloom.create(tfname, expected=add_count, k=opt.k)
125 ix = git.open_idx(name)
126 qprogress('bloom: writing %.2f%% (%d/%d objects)\r'
127 % (icount*100.0/add_count, icount, add_count))
132 # Currently, there's an open file object for tfname inside b.
133 # Make sure it's closed before rename.
137 os.rename(tfname, outfilename)
142 o = options.Options(optspec)
143 (opt, flags, extra) = o.parse(sys.argv[1:])
146 o.fatal('no positional parameters expected')
148 git.check_repo_or_die()
150 if not opt.check and opt.k and opt.k not in (4,5):
151 o.fatal('only k values of 4 and 5 are supported')
153 paths = opt.dir and [opt.dir] or git.all_packdirs()
155 debug1('bloom: scanning %s\n' % path)
156 outfilename = opt.output or os.path.join(path, 'bup.bloom')
158 check_bloom(path, outfilename, opt.check)
160 ruin_bloom(outfilename)
162 do_bloom(path, outfilename)
165 log('WARNING: %d errors encountered during bloom.\n' % len(saved_errors))
168 log('All tests passed.\n')