]> arthur.barton.de Git - bup.git/blob - lib/bup/cmd/bloom.py
git: remove all_packdirs()
[bup.git] / lib / bup / cmd / bloom.py
1
2 from __future__ import absolute_import
3
4 import os, glob, tempfile
5
6 from bup import compat, options, git, bloom
7 from bup.compat import argv_bytes, hexstr
8 from bup.helpers import (add_error, debug1, handle_ctrl_c, log, progress, qprogress,
9                          saved_errors)
10 from bup.io import path_msg
11
12
13 optspec = """
14 bup bloom [options...]
15 --
16 ruin       ruin the specified bloom file (clearing the bitfield)
17 f,force    ignore existing bloom file and regenerate it from scratch
18 o,output=  output bloom filename (default: auto)
19 d,dir=     input directory to look for idx files (default: auto)
20 k,hashes=  number of hash functions to use (4 or 5) (default: auto)
21 c,check=   check the given .idx file against the bloom filter
22 """
23
24
25 def ruin_bloom(bloomfilename):
26     rbloomfilename = git.repo_rel(bloomfilename)
27     if not os.path.exists(bloomfilename):
28         log(path_msg(bloomfilename) + '\n')
29         add_error('bloom: %s not found to ruin\n' % path_msg(rbloomfilename))
30         return
31     b = bloom.ShaBloom(bloomfilename, readwrite=True, expected=1)
32     b.map[16 : 16 + 2**b.bits] = b'\0' * 2**b.bits
33
34
35 def check_bloom(path, bloomfilename, idx):
36     rbloomfilename = git.repo_rel(bloomfilename)
37     ridx = git.repo_rel(idx)
38     if not os.path.exists(bloomfilename):
39         log('bloom: %s: does not exist.\n' % path_msg(rbloomfilename))
40         return
41     b = bloom.ShaBloom(bloomfilename)
42     if not b.valid():
43         add_error('bloom: %r is invalid.\n' % path_msg(rbloomfilename))
44         return
45     base = os.path.basename(idx)
46     if base not in b.idxnames:
47         log('bloom: %s does not contain the idx.\n' % path_msg(rbloomfilename))
48         return
49     if base == idx:
50         idx = os.path.join(path, idx)
51     log('bloom: bloom file: %s\n' % path_msg(rbloomfilename))
52     log('bloom:   checking %s\n' % path_msg(ridx))
53     for objsha in git.open_idx(idx):
54         if not b.exists(objsha):
55             add_error('bloom: ERROR: object %s missing' % hexstr(objsha))
56
57
58 _first = None
59 def do_bloom(path, outfilename, k, force):
60     global _first
61     assert k in (None, 4, 5)
62     b = None
63     if os.path.exists(outfilename) and not force:
64         b = bloom.ShaBloom(outfilename)
65         if not b.valid():
66             debug1("bloom: Existing invalid bloom found, regenerating.\n")
67             b = None
68
69     add = []
70     rest = []
71     add_count = 0
72     rest_count = 0
73     for i, name in enumerate(glob.glob(b'%s/*.idx' % path)):
74         progress('bloom: counting: %d\r' % i)
75         ix = git.open_idx(name)
76         ixbase = os.path.basename(name)
77         if b and (ixbase in b.idxnames):
78             rest.append(name)
79             rest_count += len(ix)
80         else:
81             add.append(name)
82             add_count += len(ix)
83
84     if not add:
85         debug1("bloom: nothing to do.\n")
86         return
87
88     if b:
89         if len(b) != rest_count:
90             debug1("bloom: size %d != idx total %d, regenerating\n"
91                    % (len(b), rest_count))
92             b = None
93         elif k is not None and k != b.k:
94             debug1("bloom: new k %d != existing k %d, regenerating\n"
95                    % (k, b.k))
96             b = None
97         elif (b.bits < bloom.MAX_BLOOM_BITS[b.k] and
98               b.pfalse_positive(add_count) > bloom.MAX_PFALSE_POSITIVE):
99             debug1("bloom: regenerating: adding %d entries gives "
100                    "%.2f%% false positives.\n"
101                    % (add_count, b.pfalse_positive(add_count)))
102             b = None
103         else:
104             b = bloom.ShaBloom(outfilename, readwrite=True, expected=add_count)
105     if not b: # Need all idxs to build from scratch
106         add += rest
107         add_count += rest_count
108     del rest
109     del rest_count
110
111     msg = b is None and 'creating from' or 'adding'
112     if not _first: _first = path
113     dirprefix = (_first != path) and git.repo_rel(path) + b': ' or b''
114     progress('bloom: %s%s %d file%s (%d object%s).\r'
115         % (path_msg(dirprefix), msg,
116            len(add), len(add)!=1 and 's' or '',
117            add_count, add_count!=1 and 's' or ''))
118
119     tfname = None
120     if b is None:
121         tfname = os.path.join(path, b'bup.tmp.bloom')
122         b = bloom.create(tfname, expected=add_count, k=k)
123     count = 0
124     icount = 0
125     for name in add:
126         ix = git.open_idx(name)
127         qprogress('bloom: writing %.2f%% (%d/%d objects)\r' 
128                   % (icount*100.0/add_count, icount, add_count))
129         b.add_idx(ix)
130         count += 1
131         icount += len(ix)
132
133     # Currently, there's an open file object for tfname inside b.
134     # Make sure it's closed before rename.
135     b.close()
136
137     if tfname:
138         os.rename(tfname, outfilename)
139
140
141 def main(argv):
142     o = options.Options(optspec)
143     opt, flags, extra = o.parse_bytes(argv[1:])
144
145     if extra:
146         o.fatal('no positional parameters expected')
147
148     if not opt.check and opt.k and opt.k not in (4,5):
149         o.fatal('only k values of 4 and 5 are supported')
150
151     if opt.check:
152         opt.check = argv_bytes(opt.check)
153
154     git.check_repo_or_die()
155
156     output = argv_bytes(opt.output) if opt.output else None
157     path = opt.dir and argv_bytes(opt.dir) or git.repo(b'objects/pack')
158     debug1('bloom: scanning %s\n' % path_msg(path))
159     outfilename = output or os.path.join(path, b'bup.bloom')
160     if opt.check:
161         check_bloom(path, outfilename, opt.check)
162     elif opt.ruin:
163         ruin_bloom(outfilename)
164     else:
165         do_bloom(path, outfilename, opt.k, opt.force)
166
167     if saved_errors:
168         log('WARNING: %d errors encountered during bloom.\n' % len(saved_errors))
169         sys.exit(1)
170     elif opt.check:
171         log('All tests passed.\n')