-
Notifications
You must be signed in to change notification settings - Fork 1
/
holes
executable file
·344 lines (308 loc) · 11.5 KB
/
holes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#! /usr/bin/env python
"""
holes - encode / decode files with "holes" to pass
through regular cp/tar
"""
import argparse
from itertools import groupby
import os
import signal
import sys
def format_bytes(nbytes):
"format bytes-read or bytes-written in %d.%dMB form"
if nbytes <= 1024:
return '%d bytes' % nbytes
kbytes = nbytes // 1024
if kbytes <= 1024:
return '%d kiB' % kbytes
mbytes = kbytes // 1024
kbytes -= mbytes * 1024
kfrac = ('%.3f' % (kbytes / 1024.0))[1:3]
return '%d%s MiB' % (mbytes, kfrac)
def request_info(signum, frame): # pylint: disable=unused-argument
"signal handler for SIGINFO"
Gvars.set_siginfo(True)
class GlobalVars(object):
"global variables"
def __init__(self):
self._debug = False
self._siginfo = False
self._bytes_read = 0
self._bytes_written = 0
self._hole_bytes = 0
signal.signal(signal.SIGINFO, request_info)
def set_debug(self, flag):
"update debug flag"
self._debug = flag
def set_siginfo(self, flag):
"update siginfo flag"
self._siginfo = flag
def debug(self, fmt, *args):
"print debug info if debug flag set"
if not self._debug:
return
if len(args):
print >> sys.stderr, fmt % args
else:
print >> sys.stderr, fmt
def inc_bytes_read(self, nbytes):
"update bytes-read"
self._bytes_read += nbytes
def inc_bytes_written(self, nbytes, as_hole = False):
"update bytes-written"
self._bytes_written += nbytes
if as_hole:
self._hole_bytes += nbytes
def siginfo(self):
"print ^T output if requested and good time to do so"
if self._siginfo:
if self._hole_bytes:
print >> sys.stderr, \
'%s bytes read, %s written (%s of those as holes)' % (
format_bytes(self._bytes_read),
format_bytes(self._bytes_written),
format_bytes(self._hole_bytes))
else:
print >> sys.stderr, '%s bytes read, %s written' % (
format_bytes(self._bytes_read),
format_bytes(self._bytes_written))
self._siginfo = False
Gvars = GlobalVars() # pylint: disable=invalid-name
class DecodeError(Exception):
"indicates problem with encoded file"
pass
def zfile_encode(in_arg, out_arg):
"""
Encode a 'zero file', ie, a file with potential holes.
"""
_zfile_xcode(in_arg, out_arg, _zfile_encode)
def zfile_decode(in_arg, out_arg):
"""
Decode a 'zero file', ie, a file with potential holes.
"""
_zfile_xcode(in_arg, out_arg, _zfile_decode)
def _zfile_xcode(in_arg, out_arg, doit):
"""
Read from in_arg, write en- or de-coded data to out_arg. If
in_arg or out_arg is an integer it's an open file descriptor,
if it has a .fileno/.read/.write method it's a stream.
"""
ifd = -1
do_i_close = False
try:
ifd = in_arg.fileno()
except AttributeError:
if isinstance(in_arg, int):
ifd = in_arg
else:
do_i_close = True
ifd = os.open(in_arg, os.O_RDONLY)
ostream = None
do_o_close = False
try:
if hasattr(out_arg, 'read') and hasattr(out_arg, 'write'):
ostream = out_arg
elif isinstance(out_arg, int):
ostream = os.fdopen(out_arg)
else:
do_o_close = True
ostream = open(out_arg, 'w')
return doit(ifd, ostream)
finally:
if do_i_close and ifd >= 0:
os.close(ifd)
if do_o_close and ostream != None:
ostream.close()
def _zfile_encode(ifd, ostream):
"""
Guts of encoding a zero-counted file.
Note: we ignore the underlying file system I/O size when
encoding, since we may be writing the file on a different file
system block size. We just look for runs of zero bytes and
count them.
"""
def enc_zero_bytes(ostream, nbytes):
"""
Short runs are encoded by stuffing an extra \0 at the front,
long runs are encoded as \0<count>\0.
"""
if nbytes < 1024:
ostream.write('\0' * (nbytes + 1))
Gvars.inc_bytes_written(nbytes + 1)
else:
Gvars.debug('encode %d zeros', nbytes)
as_str = '\0' + str(nbytes) + '\0'
ostream.write(as_str)
Gvars.inc_bytes_written(len(as_str))
zeros = 0
blksize = os.fstat(ifd).st_blksize
#blksize = 1024*1024
while True:
blk = os.read(ifd, blksize)
if blk == b'':
break
Gvars.inc_bytes_read(len(blk))
Gvars.siginfo()
for is_zero, subblock in groupby(blk, lambda x: x == b'\0'):
subblock = ''.join(subblock)
if is_zero:
zeros += len(subblock)
#Gvars.debug('more zeros: now %d' % zeros)
continue
if zeros:
enc_zero_bytes(ostream, zeros)
zeros = 0
ostream.write(subblock)
Gvars.inc_bytes_written(len(subblock))
if zeros:
Gvars.debug('final encode %d zeros', zeros)
enc_zero_bytes(ostream, zeros)
def _zfile_decode(ifd, ostream):
# pylint: disable=too-many-branches,too-many-statements
# this does need refactoring though
"""
Guts of decoding a zero-counted file.
"""
def do_output(ostream, skip_count, zero_bytes, subblock):
"""
Write subblock, perhaps after seeking forward (to perhaps
create a hole) or after writing some literal '\0' bytes.
"""
if skip_count:
if zero_bytes:
raise DecodeError('internal error')
Gvars.debug('skip forward %d bytes', skip_count)
ostream.seek(skip_count, 1)
Gvars.inc_bytes_written(skip_count, True)
skip_count = 0
elif zero_bytes:
#Gvars.debug('write %d zero bytes', (zero_bytes - 1))
ostream.write((zero_bytes - 1) * '\0')
Gvars.inc_bytes_written(zero_bytes - 1)
ostream.write(subblock)
Gvars.inc_bytes_written(len(subblock))
blksize = os.fstat(ifd).st_blksize
#blksize = 1024*1024
decode_state = 0
stringized_count = None
skip_count = 0
zero_bytes = 0
while True:
blk = os.read(ifd, blksize)
if blk == b'':
break
Gvars.inc_bytes_read(len(blk))
Gvars.siginfo()
for is_zero, subblock in groupby(blk, lambda x: x == b'\0'):
subblock = ''.join(subblock)
if is_zero:
zero_bytes += len(subblock)
continue
# Got a run of N b'\0' characters (maybe none)
# followed by a block of non-'\0' text.
#
# Short runs of '\0's are encoded by stuffing an extra
# '\0', so we'll have two or more '\0's. Long runs are
# encoded as '\0'<count>'\0'.
#
# Note that we might have already gotten '\0'<count>
# here, in which case decode_state will be 1.
#
# The encoder never "short-counts" a long run, i.e.,
# if we get something like '\0' '1024' '\0' the next
# character in the input stream is by definition not '\0'.
# So, if decode_state > 0, zero_bytes must be
# either 0 (we're at the '24' in '\0' '10' '24' '\0') or
# 1 (we got the '\0' and the '1024' and the last '\0',
# and now subblock contains whatever comes after the
# '\0' and hence zero_bytes==1).
if decode_state == 0:
if zero_bytes == 1:
decode_state = 1
stringized_count = subblock
if not stringized_count.isdigit():
raise DecodeError('bad zero-byte-count %r' %
stringized_count)
zero_bytes = 0
else:
do_output(ostream, skip_count, zero_bytes, subblock)
skip_count = 0
zero_bytes = 0
continue
if decode_state == 1:
if zero_bytes:
if zero_bytes > 1:
raise DecodeError('zero-byte-count %r '
'followed by %d zero-bytes -- should be 1' %
(stringized_count, zero_bytes))
# zero_bytes==1 - we've finished the skip_count
skip_count = int(stringized_count)
stringized_count = None
decode_state = 0
do_output(ostream, skip_count, 0, subblock)
skip_count = 0
zero_bytes = 0
continue
# this is the '\0' '10' '24' '\0' case -- we have
# not yet seen the final '\0'; accumulate the '24'
stringized_count += subblock
if not stringized_count.isdigit():
raise DecodeError('bad zero-byte-count %r' %
stringized_count)
continue
raise DecodeError('internal error, decode_state=%d' %
decode_state)
if decode_state == 1:
if zero_bytes != 1:
raise DecodeError('zero-byte-count %r '
'followed by %d zero-bytes -- should be 1' %
(stringized_count, zero_bytes))
skip_count = int(stringized_count)
stringized_count = None
decode_state = 0
if decode_state != 0:
raise DecodeError('internal error, decode_state=%d' % decode_state)
# zero_bytes encodes 1 more than the actual number needed
# (and is 1 if we changed decode_state from 1 to 0 above,
# and should be 0 now) -- so subtract 1 if > 0.
if zero_bytes:
zero_bytes -= 1
if skip_count or zero_bytes:
Gvars.debug('decode: final skip_count=%d, zero_bytes=%d',
skip_count, zero_bytes)
skip_count += zero_bytes
zero_bytes = 0
Gvars.debug('decode: final ftruncate forward %d', skip_count)
ostream.flush()
os.ftruncate(ostream.fileno(), ostream.tell() + skip_count)
Gvars.inc_bytes_written(skip_count, True)
# probably don't need the last seek...
ostream.seek(skip_count, 1)
def main():
"the usual main"
parser = argparse.ArgumentParser(prog = 'holes', description =
'holes - read or write an encoding for files with possible holes')
parser.add_argument('-D', '--debug', action = 'store_true',
help = 'turn on internal debug')
group = parser.add_mutually_exclusive_group(required = True)
group.add_argument('-d', '--decode', action = 'store_true',
help = 'decode a zero-hole-encoded file')
group.add_argument('-e', '--encode', dest = 'decode',
action = 'store_false',
help = 'encode a zero-hole-encoded file')
parser.add_argument('input', nargs = '?', default=sys.stdin,
help = 'specify input file (default=stdin)')
parser.add_argument('output', nargs = '?', default=sys.stdout,
help = 'specify output file (default=stdout)')
args = parser.parse_args()
Gvars.set_debug(args.debug)
if args.decode:
zfile_decode(args.input, args.output)
else:
zfile_encode(args.input, args.output)
return 0
if __name__ == '__main__':
try:
sys.exit(main())
except KeyboardInterrupt:
sys.exit('\nInterrupted')