2 import sys, os, glob, subprocess, time
3 from bup import options, git
4 from bup.helpers import *
7 nullf = open('/dev/null')
14 # at least in python 2.5, using "stdout=2" or "stdout=sys.stderr" below
15 # doesn't actually work, because subprocess closes fd #2 right before
16 # execing for some reason. So we work around it by duplicating the fd
18 fd = os.dup(2) # copy stderr
20 p = subprocess.Popen(argv, stdout=fd, close_fds=False)
29 p = subprocess.Popen(['par2', '--help'],
30 stdout=nullf, stderr=nullf, stdin=nullf)
33 log('fsck: warning: par2 not found; disabling recovery features.\n')
38 if opt.verbose >= lvl:
46 def par2_generate(base):
47 return run(['par2', 'create', '-n1', '-c200'] + parv(2)
48 + ['--', base, base+'.pack', base+'.idx'])
50 def par2_verify(base):
51 return run(['par2', 'verify'] + parv(3) + ['--', base])
53 def par2_repair(base):
54 return run(['par2', 'repair'] + parv(2) + ['--', base])
56 def quick_verify(base):
57 f = open(base + '.pack', 'rb')
60 assert(len(wantsum) == 20)
63 for b in chunkyreader(f, os.fstat(f.fileno()).st_size - 20):
65 if sum.digest() != wantsum:
66 raise ValueError('expected %r, got %r' % (wantsum.encode('hex'),
75 debug('error: %s\n' % e)
79 return run(['git', 'verify-pack', '--', base])
82 def do_pack(base, last):
84 if par2_ok and par2_exists and (opt.repair or not opt.generate):
85 vresult = par2_verify(base)
88 rresult = par2_repair(base)
90 print '%s par2 repair: failed (%d)' % (last, rresult)
93 print '%s par2 repair: succeeded (0)' % last
96 print '%s par2 verify: failed (%d)' % (last, vresult)
100 elif not opt.generate or (par2_ok and not par2_exists):
101 gresult = git_verify(base)
103 print '%s git verify: failed (%d)' % (last, gresult)
106 if par2_ok and opt.generate:
110 assert(opt.generate and (not par2_ok or par2_exists))
111 debug(' skipped: par2 file already generated.\n')
116 bup fsck [options...] [filenames...]
118 r,repair attempt to repair errors using par2 (dangerous!)
119 g,generate generate auto-repair information using par2
120 v,verbose increase verbosity (can be used more than once)
121 quick just check pack sha1sum, don't use git verify-pack
122 j,jobs= run 'n' jobs in parallel
123 par2-ok immediately return 0 if par2 is ok, 1 if not
124 disable-par2 ignore par2 even if it is available
126 o = options.Options('bup fsck', optspec)
127 (opt, flags, extra) = o.parse(sys.argv[1:])
132 sys.exit(0) # 'true' in sh
138 git.check_repo_or_die()
141 debug('fsck: No filenames given: checking all packs.\n')
142 extra = glob.glob(git.repo('objects/pack/*.pack'))
148 if name.endswith('.pack'):
150 elif name.endswith('.idx'):
152 elif name.endswith('.par2'):
154 elif os.path.exists(name + '.pack'):
157 raise Exception('%s is not a pack file!' % name)
158 (dir,last) = os.path.split(base)
159 par2_exists = os.path.exists(base + '.par2')
160 if par2_exists and os.stat(base + '.par2').st_size == 0:
163 debug('fsck: checking %s (%s)\n'
164 % (last, par2_ok and par2_exists and 'par2' or 'git'))
166 progress('fsck (%d/%d)\r' % (count, len(extra)))
169 nc = do_pack(base, last)
173 while len(outstanding) >= opt.jobs:
176 if pid in outstanding:
185 sys.exit(do_pack(base, last))
187 log('exception: %r\n' % e)
190 while len(outstanding):
193 if pid in outstanding:
198 progress('fsck (%d/%d)\r' % (count, len(extra)))
200 if not opt.verbose and istty: