-
Notifications
You must be signed in to change notification settings - Fork 4
/
holes
executable file
·182 lines (139 loc) · 4.14 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
#!/usr/bin/env python3
import io
import math
import optparse
import os
import sys
from dataclasses import dataclass
from typing import Optional
NULL_CHAR = '_'
FILL_CHAR = 'X'
MAX_BLOCK_SIZE = 100 * 1024 * 1024
class HolesError(Exception):
pass
def sizeof_fmt(num, suffix='B') -> str:
if num == 0:
return "0B"
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() -> int:
usage = '''%prog [options] FILE...
Find runs of null bytes in files, print the percentage of blocks that are
filled with content vs empty. By default, the blocks will be dynamically sized
based on the file's total size and the width of the output terminal. Pass -b to
use a fixed block size instead.
'''.rstrip()
p = optparse.OptionParser(usage=usage)
p.add_option('-q', '--quiet', dest='verbose', action='store_false',
help="Silence visualization", default=True)
p.add_option('-b', '--blocksize', dest='blocksize', metavar='BYTES',
type='int', help='Size of blocks to examine')
opts, args = p.parse_args()
filenames = args
if not filenames:
p.print_help()
return 1
try:
for filename in filenames:
print_file_info(
filename=filename,
print_dots=opts.verbose,
blocksize=opts.blocksize,
verbose=opts.verbose,
)
except HolesError as err:
sys.stderr.write(f"\n\terror: {err}\n")
return 2
return 0
def print_file_info(
filename: str,
blocksize: Optional[int] = None,
print_dots: bool = True,
verbose: bool = False,
) -> None:
sys.stdout.write(filename)
res = count_filled(
filename=filename,
blocksize=blocksize,
print_dots=print_dots,
)
# technically / 0 is NaN, but 0% makes intuitive sense
frac = res.count / res.total if res.total > 0 else 0
out = f"\t{frac:.0%} full of {sizeof_fmt(res.file_size)}"
print(out)
@dataclass
class CountResult:
filename: str
count: int
total: int
file_size: int
block_size: int
def count_filled(
filename: str,
blocksize: Optional[int] = None,
print_dots: bool = True,
print_blocksize: bool = True,
) -> CountResult:
count = 0
total = 0
fh = io.FileIO(filename, 'rb')
# Seek to end to determine file size.
# This method works on block devices, unlike stat.
file_size = fh.seek(0, os.SEEK_END)
fh.seek(0)
if blocksize is None:
if sys.stdout.isatty():
max_blocks = os.get_terminal_size().columns - 9
else:
max_blocks = 71
if max_blocks < 1:
max_blocks = 1
blocksize = math.ceil(file_size / max_blocks)
blocksize = min(blocksize, MAX_BLOCK_SIZE)
null_comparison = memoryview(b'\0' * blocksize)
buf = bytearray(blocksize)
if print_blocksize:
if print_dots:
sys.stdout.write("\n\t")
else:
sys.stdout.write("\t")
sys.stdout.write("bs=" + sizeof_fmt(blocksize))
if print_dots:
sys.stdout.write("\n\t")
sys.stdout.flush()
while True:
try:
size = fh.readinto(buf)
except OSError as err:
pos = fh.tell()
raise HolesError(f"{err} at fd offset={pos}") from err
if size == 0:
break
total += 1
if buf.startswith(null_comparison[:size], 0, size):
if print_dots:
sys.stdout.write(NULL_CHAR)
sys.stdout.flush()
else:
count += 1
if print_dots:
sys.stdout.write(FILL_CHAR)
sys.stdout.flush()
if print_dots:
sys.stdout.write('\n')
sys.stdout.flush()
return CountResult(
filename=filename,
count=count,
total=total,
file_size=file_size,
block_size=blocksize,
)
if __name__ == '__main__':
try:
sys.exit(main())
except KeyboardInterrupt:
sys.exit(130)