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 hexstr
13 from bup.helpers import (add_error, debug1, handle_ctrl_c, log, progress, qprogress,
17 bup bloom [options...]
19 ruin ruin the specified bloom file (clearing the bitfield)
20 f,force ignore existing bloom file and regenerate it from scratch
21 o,output= output bloom filename (default: auto)
22 d,dir= input directory to look for idx files (default: auto)
23 k,hashes= number of hash functions to use (4 or 5) (default: auto)
24 c,check= check the given .idx file against the bloom filter
28 def ruin_bloom(bloomfilename):
29 rbloomfilename = git.repo_rel(bloomfilename)
30 if not os.path.exists(bloomfilename):
31 log("%s\n" % bloomfilename)
32 add_error("bloom: %s not found to ruin\n" % rbloomfilename)
34 b = bloom.ShaBloom(bloomfilename, readwrite=True, expected=1)
35 b.map[16:16+2**b.bits] = '\0' * 2**b.bits
38 def check_bloom(path, bloomfilename, idx):
39 rbloomfilename = git.repo_rel(bloomfilename)
40 ridx = git.repo_rel(idx)
41 if not os.path.exists(bloomfilename):
42 log("bloom: %s: does not exist.\n" % rbloomfilename)
44 b = bloom.ShaBloom(bloomfilename)
46 add_error("bloom: %r is invalid.\n" % rbloomfilename)
48 base = os.path.basename(idx)
49 if base not in b.idxnames:
50 log("bloom: %s does not contain the idx.\n" % rbloomfilename)
53 idx = os.path.join(path, idx)
54 log("bloom: bloom file: %s\n" % rbloomfilename)
55 log("bloom: checking %s\n" % ridx)
56 for objsha in git.open_idx(idx):
57 if not b.exists(objsha):
58 add_error('bloom: ERROR: object %s missing' % hexstr(objsha))
62 def do_bloom(path, outfilename, k):
64 assert k in (None, 4, 5)
66 if os.path.exists(outfilename) and not opt.force:
67 b = bloom.ShaBloom(outfilename)
69 debug1("bloom: Existing invalid bloom found, regenerating.\n")
76 for i,name in enumerate(glob.glob('%s/*.idx' % path)):
77 progress('bloom: counting: %d\r' % i)
78 ix = git.open_idx(name)
79 ixbase = os.path.basename(name)
80 if b and (ixbase in b.idxnames):
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 k is not None and k != b.k:
97 debug1("bloom: new k %d != existing k %d, regenerating\n"
100 elif (b.bits < bloom.MAX_BLOOM_BITS[b.k] and
101 b.pfalse_positive(add_count) > bloom.MAX_PFALSE_POSITIVE):
102 debug1("bloom: regenerating: adding %d entries gives "
103 "%.2f%% false positives.\n"
104 % (add_count, b.pfalse_positive(add_count)))
107 b = bloom.ShaBloom(outfilename, readwrite=True, expected=add_count)
108 if not b: # Need all idxs to build from scratch
110 add_count += rest_count
114 msg = b is None and 'creating from' or 'adding'
115 if not _first: _first = path
116 dirprefix = (_first != path) and git.repo_rel(path)+': ' or ''
117 progress('bloom: %s%s %d file%s (%d object%s).\r'
119 len(add), len(add)!=1 and 's' or '',
120 add_count, add_count!=1 and 's' or ''))
124 tfname = os.path.join(path, 'bup.tmp.bloom')
125 b = bloom.create(tfname, expected=add_count, k=k)
129 ix = git.open_idx(name)
130 qprogress('bloom: writing %.2f%% (%d/%d objects)\r'
131 % (icount*100.0/add_count, icount, add_count))
136 # Currently, there's an open file object for tfname inside b.
137 # Make sure it's closed before rename.
141 os.rename(tfname, outfilename)
146 o = options.Options(optspec)
147 (opt, flags, extra) = o.parse(sys.argv[1:])
150 o.fatal('no positional parameters expected')
152 git.check_repo_or_die()
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')
157 paths = opt.dir and [opt.dir] or git.all_packdirs()
159 debug1('bloom: scanning %s\n' % path)
160 outfilename = opt.output or os.path.join(path, 'bup.bloom')
162 check_bloom(path, outfilename, opt.check)
164 ruin_bloom(outfilename)
166 do_bloom(path, outfilename, opt.k)
169 log('WARNING: %d errors encountered during bloom.\n' % len(saved_errors))
172 log('All tests passed.\n')