-
Notifications
You must be signed in to change notification settings - Fork 0
/
holes
executable file
·76 lines (58 loc) · 1.96 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
#!/usr/bin/env python
import os
import sys
import re
not_null_re = re.compile('[^\0]')
NULL_CHAR = '_'
FILL_CHAR = 'X'
def sizeof_fmt(num, suffix='B'):
for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']:
if abs(num) < 1024.0:
return "%3.1f%s%s" % (num, unit, suffix)
num /= 1024.0
return "%.1f%s%s" % (num, 'Yi', suffix)
def main(filenames, options=None):
if not filenames:
sys.stderr.write('usage: {} FILE...\n'.format(
os.path.basename(sys.argv[0])))
sys.exit(1)
for filename in filenames:
filename, count, total, size = count_filled(filename, print_dots=True)
print('{!r}: {}% of {}'.format(filename, 100.0 * count / total,
sizeof_fmt(size)))
def count_filled(filename, blocksize=None, print_dots=False):
count = 0
total = 0
file_size = os.stat(filename).st_size
if print_dots:
sys.stdout.write('{!r}: '.format(filename))
if blocksize is None:
if print_dots:
blocksize = file_size / 1660
else:
blocksize = 4096
null_block = '\0' * blocksize
with open(filename, 'r') as fh:
while True:
block = fh.read(blocksize)
if not block:
break
total += 1
# if not_null_re.search(block):
if ((len(block) == len(null_block) and block != null_block) or
(len(block) != len(null_block)
and block.count('\0') == len(block))):
count += 1
if print_dots:
sys.stdout.write(FILL_CHAR)
sys.stdout.flush()
else:
if print_dots:
sys.stdout.write(NULL_CHAR)
sys.stdout.flush()
if print_dots:
sys.stdout.write('\n')
sys.stdout.flush()
return (filename, count, total, file_size)
if __name__ == '__main__':
main(sys.argv[1:])