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, par2_exists):
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 action_result = 'failed'
91 log('%s par2 repair: failed (%d)\n' % (last, rresult))
94 action_result = 'repaired'
95 log('%s par2 repair: succeeded (0)\n' % last)
98 action_result = 'failed'
99 log('%s par2 verify: failed (%d)\n' % (last, vresult))
103 elif not opt.generate or (par2_ok and not par2_exists):
104 gresult = git_verify(base)
106 action_result = 'failed'
107 log('%s git verify: failed (%d)\n' % (last, gresult))
110 if par2_ok and opt.generate:
111 presult = par2_generate(base)
113 action_result = 'failed'
114 log('%s par2 create: failed (%d)\n' % (last, presult))
117 action_result = 'generated'
121 assert(opt.generate and (not par2_ok or par2_exists))
122 action_result = 'exists' if par2_exists else 'skipped'
124 print last, action_result
129 bup fsck [options...] [filenames...]
131 r,repair attempt to repair errors using par2 (dangerous!)
132 g,generate generate auto-repair information using par2
133 v,verbose increase verbosity (can be used more than once)
134 quick just check pack sha1sum, don't use git verify-pack
135 j,jobs= run 'n' jobs in parallel
136 par2-ok immediately return 0 if par2 is ok, 1 if not
137 disable-par2 ignore par2 even if it is available
139 o = options.Options(optspec)
140 (opt, flags, extra) = o.parse(sys.argv[1:])
145 sys.exit(0) # 'true' in sh
151 git.check_repo_or_die()
154 debug('fsck: No filenames given: checking all packs.\n')
155 extra = glob.glob(git.repo('objects/pack/*.pack'))
161 if name.endswith('.pack'):
163 elif name.endswith('.idx'):
165 elif name.endswith('.par2'):
167 elif os.path.exists(name + '.pack'):
170 raise Exception('%s is not a pack file!' % name)
171 (dir,last) = os.path.split(base)
172 par2_exists = os.path.exists(base + '.par2')
173 if par2_exists and os.stat(base + '.par2').st_size == 0:
176 debug('fsck: checking %s (%s)\n'
177 % (last, par2_ok and par2_exists and 'par2' or 'git'))
179 progress('fsck (%d/%d)\r' % (count, len(extra)))
182 nc = do_pack(base, last, par2_exists)
186 while len(outstanding) >= opt.jobs:
189 if pid in outstanding:
198 sys.exit(do_pack(base, last, par2_exists))
200 log('exception: %r\n' % e)
203 while len(outstanding):
206 if pid in outstanding:
211 progress('fsck (%d/%d)\r' % (count, len(extra)))
214 debug('fsck done. \n')