]> arthur.barton.de Git - bup.git/blob - lib/cmd/bloom-cmd.py
Bypass Python 3 glibc argv problems by routing args through env
[bup.git] / lib / cmd / bloom-cmd.py
1 #!/bin/sh
2 """": # -*-python-*-
3 # https://sourceware.org/bugzilla/show_bug.cgi?id=26034
4 export "BUP_ARGV_0"="$0"
5 arg_i=1
6 for arg in "$@"; do
7     export "BUP_ARGV_${arg_i}"="$arg"
8     shift
9     arg_i=$((arg_i + 1))
10 done
11 # Here to end of preamble replaced during install
12 bup_python="$(dirname "$0")/bup-python" || exit $?
13 exec "$bup_python" "$0"
14 """
15 # end of bup preamble
16
17 from __future__ import absolute_import
18 import glob, os, sys, tempfile
19
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,
23                          saved_errors)
24 from bup.io import path_msg
25
26
27 optspec = """
28 bup bloom [options...]
29 --
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
36 """
37
38
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))
44         return
45     b = bloom.ShaBloom(bloomfilename, readwrite=True, expected=1)
46     b.map[16 : 16 + 2**b.bits] = b'\0' * 2**b.bits
47
48
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))
54         return
55     b = bloom.ShaBloom(bloomfilename)
56     if not b.valid():
57         add_error('bloom: %r is invalid.\n' % path_msg(rbloomfilename))
58         return
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))
62         return
63     if base == idx:
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))
70
71
72 _first = None
73 def do_bloom(path, outfilename, k):
74     global _first
75     assert k in (None, 4, 5)
76     b = None
77     if os.path.exists(outfilename) and not opt.force:
78         b = bloom.ShaBloom(outfilename)
79         if not b.valid():
80             debug1("bloom: Existing invalid bloom found, regenerating.\n")
81             b = None
82
83     add = []
84     rest = []
85     add_count = 0
86     rest_count = 0
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):
92             rest.append(name)
93             rest_count += len(ix)
94         else:
95             add.append(name)
96             add_count += len(ix)
97
98     if not add:
99         debug1("bloom: nothing to do.\n")
100         return
101
102     if b:
103         if len(b) != rest_count:
104             debug1("bloom: size %d != idx total %d, regenerating\n"
105                    % (len(b), rest_count))
106             b = None
107         elif k is not None and k != b.k:
108             debug1("bloom: new k %d != existing k %d, regenerating\n"
109                    % (k, b.k))
110             b = None
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)))
116             b = None
117         else:
118             b = bloom.ShaBloom(outfilename, readwrite=True, expected=add_count)
119     if not b: # Need all idxs to build from scratch
120         add += rest
121         add_count += rest_count
122     del rest
123     del rest_count
124
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 ''))
132
133     tfname = None
134     if b is None:
135         tfname = os.path.join(path, b'bup.tmp.bloom')
136         b = bloom.create(tfname, expected=add_count, k=k)
137     count = 0
138     icount = 0
139     for name in add:
140         ix = git.open_idx(name)
141         qprogress('bloom: writing %.2f%% (%d/%d objects)\r' 
142                   % (icount*100.0/add_count, icount, add_count))
143         b.add_idx(ix)
144         count += 1
145         icount += len(ix)
146
147     # Currently, there's an open file object for tfname inside b.
148     # Make sure it's closed before rename.
149     b.close()
150
151     if tfname:
152         os.rename(tfname, outfilename)
153
154
155 handle_ctrl_c()
156
157 o = options.Options(optspec)
158 opt, flags, extra = o.parse(compat.argv[1:])
159
160 if extra:
161     o.fatal('no positional parameters expected')
162
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')
165
166 if opt.check:
167     opt.check = argv_bytes(opt.check)
168
169 git.check_repo_or_die()
170
171 output = argv_bytes(opt.output) if opt.output else None
172 paths = opt.dir and [argv_bytes(opt.dir)] or git.all_packdirs()
173 for path in paths:
174     debug1('bloom: scanning %s\n' % path_msg(path))
175     outfilename = output or os.path.join(path, b'bup.bloom')
176     if opt.check:
177         check_bloom(path, outfilename, opt.check)
178     elif opt.ruin:
179         ruin_bloom(outfilename)
180     else:
181         do_bloom(path, outfilename, opt.k)
182
183 if saved_errors:
184     log('WARNING: %d errors encountered during bloom.\n' % len(saved_errors))
185     sys.exit(1)
186 elif opt.check:
187     log('All tests passed.\n')