]> arthur.barton.de Git - bup.git/blob - lib/cmd/damage-cmd.py
Bypass Python 3 glibc argv problems by routing args through env
[bup.git] / lib / cmd / damage-cmd.py
1 #!/bin/sh
2 """": # -*-python-*-
3 # https://sourceware.org/bugzilla/show_bug.cgi?id=26034
4 export "BUP_ARGV_0"="$0"
5 arg_i=1
6 for arg in "$@"; do
7     export "BUP_ARGV_${arg_i}"="$arg"
8     shift
9     arg_i=$((arg_i + 1))
10 done
11 # Here to end of preamble replaced during install
12 bup_python="$(dirname "$0")/bup-python" || exit $?
13 exec "$bup_python" "$0"
14 """
15 # end of bup preamble
16
17 from __future__ import absolute_import
18 import sys, os, random
19
20 from bup import compat, options
21 from bup.compat import argv_bytes, bytes_from_uint, range
22 from bup.helpers import log
23 from bup.io import path_msg
24
25
26 def randblock(n):
27     return b''.join(bytes_from_uint(random.randrange(0,256)) for i in range(n))
28
29
30 optspec = """
31 bup damage [-n count] [-s maxsize] [-S seed] <filenames...>
32 --
33    WARNING: THIS COMMAND IS EXTREMELY DANGEROUS
34 n,num=   number of blocks to damage
35 s,size=  maximum size of each damaged block
36 percent= maximum size of each damaged block (as a percent of entire file)
37 equal    spread damage evenly throughout the file
38 S,seed=  random number seed (for repeatable tests)
39 """
40 o = options.Options(optspec)
41 opt, flags, extra = o.parse(compat.argv[1:])
42
43 if not extra:
44     o.fatal('filenames expected')
45
46 if opt.seed != None:
47     random.seed(opt.seed)
48
49 for name in extra:
50     name = argv_bytes(name)
51     log('Damaging "%s"...\n' % path_msg(name))
52     with open(name, 'r+b') as f:
53         st = os.fstat(f.fileno())
54         size = st.st_size
55         if opt.percent or opt.size:
56             ms1 = int(float(opt.percent or 0)/100.0*size) or size
57             ms2 = opt.size or size
58             maxsize = min(ms1, ms2)
59         else:
60             maxsize = 1
61         chunks = opt.num or 10
62         chunksize = size // chunks
63         for r in range(chunks):
64             sz = random.randrange(1, maxsize+1)
65             if sz > size:
66                 sz = size
67             if opt.equal:
68                 ofs = r*chunksize
69             else:
70                 ofs = random.randrange(0, size - sz + 1)
71             log('  %6d bytes at %d\n' % (sz, ofs))
72             f.seek(ofs)
73             f.write(randblock(sz))