2 import sys, os, glob, subprocess, time, sha
8 nullf = open('/dev/null')
15 # at least in python 2.5, using "stdout=2" or "stdout=sys.stderr" below
16 # doesn't actually work, because subprocess closes fd #2 right before
17 # execing for some reason. So we work around it by duplicating the fd
19 fd = os.dup(2) # copy stderr
21 p = subprocess.Popen(argv, stdout=fd, close_fds=False)
30 p = subprocess.Popen(['par2', '--help'],
31 stdout=nullf, stderr=nullf, stdin=nullf)
34 log('fsck: warning: par2 not found; disabling recovery features.\n')
39 if opt.verbose >= lvl:
47 def par2_generate(base):
48 return run(['par2', 'create', '-n1', '-c200'] + parv(2)
49 + ['--', base, base+'.pack', base+'.idx'])
51 def par2_verify(base):
52 return run(['par2', 'verify'] + parv(3) + ['--', base])
54 def par2_repair(base):
55 return run(['par2', 'repair'] + parv(2) + ['--', base])
57 def quick_verify(base):
58 f = open(base + '.pack', 'rb')
61 assert(len(wantsum) == 20)
64 for b in chunkyreader(f, os.fstat(f.fileno()).st_size - 20):
66 if sum.digest() != wantsum:
67 raise ValueError('expected %r, got %r' % (wantsum.encode('hex'),
76 debug('error: %s\n' % e)
80 return run(['git', 'verify-pack', '--', base])
83 def do_pack(base, last):
85 if par2_ok and par2_exists and (opt.repair or not opt.generate):
86 vresult = par2_verify(base)
89 rresult = par2_repair(base)
91 print '%s par2 repair: failed (%d)' % (last, rresult)
94 print '%s par2 repair: succeeded (0)' % last
97 print '%s par2 verify: failed (%d)' % (last, vresult)
101 elif not opt.generate or (par2_ok and not par2_exists):
102 gresult = git_verify(base)
104 print '%s git verify: failed (%d)' % (last, gresult)
107 if par2_ok and opt.generate:
111 assert(opt.generate and (not par2_ok or par2_exists))
112 debug(' skipped: par2 file already generated.\n')
117 bup fsck [options...] [filenames...]
119 r,repair attempt to repair errors using par2 (dangerous!)
120 g,generate generate auto-repair information using par2
121 v,verbose increase verbosity (can be used more than once)
122 quick just check pack sha1sum, don't use git verify-pack
123 j,jobs= run 'n' jobs in parallel
124 par2-ok immediately return 0 if par2 is ok, 1 if not
125 disable-par2 ignore par2 even if it is available
127 o = options.Options('bup fsck', optspec)
128 (opt, flags, extra) = o.parse(sys.argv[1:])
133 sys.exit(0) # 'true' in sh
139 git.check_repo_or_die()
142 debug('fsck: No filenames given: checking all packs.\n')
143 extra = glob.glob(git.repo('objects/pack/*.pack'))
149 if name.endswith('.pack'):
151 elif name.endswith('.idx'):
153 elif name.endswith('.par2'):
155 elif os.path.exists(name + '.pack'):
158 raise Exception('%s is not a pack file!' % name)
159 (dir,last) = os.path.split(base)
160 par2_exists = os.path.exists(base + '.par2')
161 if par2_exists and os.stat(base + '.par2').st_size == 0:
164 debug('fsck: checking %s (%s)\n'
165 % (last, par2_ok and par2_exists and 'par2' or 'git'))
166 if not opt.verbose and istty:
167 log('fsck (%d/%d)\r' % (count, len(extra)))
170 nc = do_pack(base, last)
174 while len(outstanding) >= opt.jobs:
177 if pid in outstanding:
186 sys.exit(do_pack(base, last))
188 log('exception: %r\n' % e)
191 while len(outstanding):
194 if pid in outstanding:
198 if not opt.verbose and istty:
199 log('fsck (%d/%d)\r' % (count, len(extra)))
201 if not opt.verbose and istty: