2 import sys, os, glob, subprocess
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:
107 presult = par2_generate(base)
109 print '%s par2 create: failed (%d)' % (last, presult)
116 assert(opt.generate and (not par2_ok or par2_exists))
117 debug(' skipped: par2 file already generated.\n')
122 bup fsck [options...] [filenames...]
124 r,repair attempt to repair errors using par2 (dangerous!)
125 g,generate generate auto-repair information using par2
126 v,verbose increase verbosity (can be used more than once)
127 quick just check pack sha1sum, don't use git verify-pack
128 j,jobs= run 'n' jobs in parallel
129 par2-ok immediately return 0 if par2 is ok, 1 if not
130 disable-par2 ignore par2 even if it is available
132 o = options.Options('bup fsck', optspec)
133 (opt, flags, extra) = o.parse(sys.argv[1:])
138 sys.exit(0) # 'true' in sh
144 git.check_repo_or_die()
147 debug('fsck: No filenames given: checking all packs.\n')
148 extra = glob.glob(git.repo('objects/pack/*.pack'))
154 if name.endswith('.pack'):
156 elif name.endswith('.idx'):
158 elif name.endswith('.par2'):
160 elif os.path.exists(name + '.pack'):
163 raise Exception('%s is not a pack file!' % name)
164 (dir,last) = os.path.split(base)
165 par2_exists = os.path.exists(base + '.par2')
166 if par2_exists and os.stat(base + '.par2').st_size == 0:
169 debug('fsck: checking %s (%s)\n'
170 % (last, par2_ok and par2_exists and 'par2' or 'git'))
172 progress('fsck (%d/%d)\r' % (count, len(extra)))
175 nc = do_pack(base, last)
179 while len(outstanding) >= opt.jobs:
182 if pid in outstanding:
191 sys.exit(do_pack(base, last))
193 log('exception: %r\n' % e)
196 while len(outstanding):
199 if pid in outstanding:
204 progress('fsck (%d/%d)\r' % (count, len(extra)))
206 if not opt.verbose and istty: