3 # https://sourceware.org/bugzilla/show_bug.cgi?id=26034
4 export "BUP_ARGV_0"="$0"
7 export "BUP_ARGV_${arg_i}"="$arg"
11 # Here to end of preamble replaced during install
12 bup_python="$(dirname "$0")/bup-python" || exit $?
13 exec "$bup_python" "$0"
17 from __future__ import absolute_import
18 import glob, os, sys, tempfile
20 from bup import compat, options, git, bloom
21 from bup.compat import argv_bytes, hexstr
22 from bup.helpers import (add_error, debug1, handle_ctrl_c, log, progress, qprogress,
24 from bup.io import path_msg
28 bup bloom [options...]
30 ruin ruin the specified bloom file (clearing the bitfield)
31 f,force ignore existing bloom file and regenerate it from scratch
32 o,output= output bloom filename (default: auto)
33 d,dir= input directory to look for idx files (default: auto)
34 k,hashes= number of hash functions to use (4 or 5) (default: auto)
35 c,check= check the given .idx file against the bloom filter
39 def ruin_bloom(bloomfilename):
40 rbloomfilename = git.repo_rel(bloomfilename)
41 if not os.path.exists(bloomfilename):
42 log(path_msg(bloomfilename) + '\n')
43 add_error('bloom: %s not found to ruin\n' % path_msg(rbloomfilename))
45 b = bloom.ShaBloom(bloomfilename, readwrite=True, expected=1)
46 b.map[16 : 16 + 2**b.bits] = b'\0' * 2**b.bits
49 def check_bloom(path, bloomfilename, idx):
50 rbloomfilename = git.repo_rel(bloomfilename)
51 ridx = git.repo_rel(idx)
52 if not os.path.exists(bloomfilename):
53 log('bloom: %s: does not exist.\n' % path_msg(rbloomfilename))
55 b = bloom.ShaBloom(bloomfilename)
57 add_error('bloom: %r is invalid.\n' % path_msg(rbloomfilename))
59 base = os.path.basename(idx)
60 if base not in b.idxnames:
61 log('bloom: %s does not contain the idx.\n' % path_msg(rbloomfilename))
64 idx = os.path.join(path, idx)
65 log('bloom: bloom file: %s\n' % path_msg(rbloomfilename))
66 log('bloom: checking %s\n' % path_msg(ridx))
67 for objsha in git.open_idx(idx):
68 if not b.exists(objsha):
69 add_error('bloom: ERROR: object %s missing' % hexstr(objsha))
73 def do_bloom(path, outfilename, k):
75 assert k in (None, 4, 5)
77 if os.path.exists(outfilename) and not opt.force:
78 b = bloom.ShaBloom(outfilename)
80 debug1("bloom: Existing invalid bloom found, regenerating.\n")
87 for i, name in enumerate(glob.glob(b'%s/*.idx' % path)):
88 progress('bloom: counting: %d\r' % i)
89 ix = git.open_idx(name)
90 ixbase = os.path.basename(name)
91 if b and (ixbase in b.idxnames):
99 debug1("bloom: nothing to do.\n")
103 if len(b) != rest_count:
104 debug1("bloom: size %d != idx total %d, regenerating\n"
105 % (len(b), rest_count))
107 elif k is not None and k != b.k:
108 debug1("bloom: new k %d != existing k %d, regenerating\n"
111 elif (b.bits < bloom.MAX_BLOOM_BITS[b.k] and
112 b.pfalse_positive(add_count) > bloom.MAX_PFALSE_POSITIVE):
113 debug1("bloom: regenerating: adding %d entries gives "
114 "%.2f%% false positives.\n"
115 % (add_count, b.pfalse_positive(add_count)))
118 b = bloom.ShaBloom(outfilename, readwrite=True, expected=add_count)
119 if not b: # Need all idxs to build from scratch
121 add_count += rest_count
125 msg = b is None and 'creating from' or 'adding'
126 if not _first: _first = path
127 dirprefix = (_first != path) and git.repo_rel(path) + b': ' or b''
128 progress('bloom: %s%s %d file%s (%d object%s).\r'
129 % (path_msg(dirprefix), msg,
130 len(add), len(add)!=1 and 's' or '',
131 add_count, add_count!=1 and 's' or ''))
135 tfname = os.path.join(path, b'bup.tmp.bloom')
136 b = bloom.create(tfname, expected=add_count, k=k)
140 ix = git.open_idx(name)
141 qprogress('bloom: writing %.2f%% (%d/%d objects)\r'
142 % (icount*100.0/add_count, icount, add_count))
147 # Currently, there's an open file object for tfname inside b.
148 # Make sure it's closed before rename.
152 os.rename(tfname, outfilename)
157 o = options.Options(optspec)
158 opt, flags, extra = o.parse(compat.argv[1:])
161 o.fatal('no positional parameters expected')
163 if not opt.check and opt.k and opt.k not in (4,5):
164 o.fatal('only k values of 4 and 5 are supported')
167 opt.check = argv_bytes(opt.check)
169 git.check_repo_or_die()
171 output = argv_bytes(opt.output) if opt.output else None
172 paths = opt.dir and [argv_bytes(opt.dir)] or git.all_packdirs()
174 debug1('bloom: scanning %s\n' % path_msg(path))
175 outfilename = output or os.path.join(path, b'bup.bloom')
177 check_bloom(path, outfilename, opt.check)
179 ruin_bloom(outfilename)
181 do_bloom(path, outfilename, opt.k)
184 log('WARNING: %d errors encountered during bloom.\n' % len(saved_errors))
187 log('All tests passed.\n')