-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify.py
More file actions
executable file
·280 lines (213 loc) · 7.69 KB
/
Copy pathverify.py
File metadata and controls
executable file
·280 lines (213 loc) · 7.69 KB
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
#!/usr/bin/env python3
# pylint: disable=logging-fstring-interpolation
"""
Snapshot and compare directories, e.g., to confirm that they're identical.
"""
import argparse
import hashlib
import json
import locale
import logging
import os
import sqlite3
import stat
import sys
from pathlib import Path
from typing import Any, Dict, NamedTuple, Tuple, Union
locale.setlocale(locale.LC_ALL, "") # for formatting numbers
BUFFER_SIZE = 16 * 2**20 # 16 MiB
SimpleJSON = Union[int, float, str, Dict[str, Any]]
class FileInfo(NamedTuple):
path: str
mode: int
uid: int
gid: int
mtime: Union[int, float]
size: int
digest: str
def make_digest(path: Path, mode: int) -> str:
"""
Returns the SHA-1 hash for the given path.
"""
hasher = hashlib.sha1() # same algorithm as xfer.py # nosec blacklist
if stat.S_ISREG(mode):
with open(path, "rb") as fp:
while True:
data = fp.read(BUFFER_SIZE)
if not data:
break
hasher.update(data)
elif stat.S_ISLNK(mode):
hasher.update(bytes(os.readlink(path), "utf-8"))
return hasher.hexdigest()
def println(tag: str, data: SimpleJSON, file=sys.stdout) -> None:
print(f"{tag} {json.dumps(data)}", file=file)
def parseln(line: str) -> Tuple[str, SimpleJSON]:
tag, data = line.split(" ", maxsplit=1)
return tag, json.loads(data)
# --------------------------------------------------------------------------
def do_hash(args: argparse.Namespace) -> None:
bytes_last_reported = 0
bytes_seen = 0
dirs_seen = 0
files_seen = 0
out_file = sys.stdout
if args.output:
out_file = open(args.output, mode="w", encoding="utf-8")
println("ROOT", args.root, file=out_file)
for (dirpath, dirnames, filenames) in os.walk(args.root):
dirnames[:] = sorted(dirnames)
filenames[:] = sorted(filenames)
dirpath = Path(dirpath)
for p in dirnames + filenames:
entry = dirpath / p
info = os.lstat(entry)
digest = make_digest(entry, info.st_mode)
info_struct = FileInfo(
path=os.fspath(entry),
mode=info.st_mode,
uid=info.st_uid,
gid=info.st_gid,
mtime=info.st_mtime,
size=info.st_size,
digest=digest,
)
if stat.S_ISDIR(info.st_mode):
println("DIR", info_struct._asdict(), file=out_file)
dirs_seen += 1
else:
println("FILE", info_struct._asdict(), file=out_file)
files_seen += 1
bytes_seen += info_struct.size
if bytes_seen - bytes_last_reported >= 128 * 2**20: # 128 MiB
logging.info(
f"Directories: {dirs_seen:n}; Files: {files_seen:n}; Bytes: {bytes_seen:n}"
)
bytes_last_reported = bytes_seen
logging.info(
f"Directories: {dirs_seen:n}; Files: {files_seen:n}; Bytes: {bytes_seen:n}"
)
if args.output:
out_file.close()
def do_convert(args: argparse.Namespace) -> None:
db = sqlite3.connect(args.destination)
db.execute(
"""
CREATE TABLE data(
path TEXT PRIMARY KEY,
mode INT,
uid INT,
gid INT,
mtime REAL,
size INT,
digest TEXT
)
""" # STRICT # available starting with SQLite 3.37
)
rows_seen = 0
with open(args.source, encoding="utf-8") as fp:
line = fp.readline()
root = Path(parseln(line)[1]) # type: ignore[arg-type]
while True:
line = fp.readline()
if not line:
break
info = FileInfo(**parseln(line)[1]) # type: ignore[arg-type]
path = Path(info.path)
info = info._replace(path=os.fspath(path.relative_to(root)))
db.execute("INSERT INTO data VALUES (?, ?, ?, ?, ?, ?, ?)", info)
rows_seen += 1
if rows_seen % 100000 == 0:
db.commit()
logging.info(f"Rows seen: {rows_seen}")
db.commit()
logging.info(f"Rows seen: {rows_seen}")
db.close()
def do_compare(args: argparse.Namespace) -> None:
db = sqlite3.connect(getattr(args, "destination-db"))
out_file = sys.stdout
if args.output:
out_file = open(args.output, mode="w", encoding="utf-8")
rows_seen = 0
with open(getattr(args, "source-list"), encoding="utf-8") as fp:
line = fp.readline()
root = Path(parseln(line)[1]) # type: ignore[arg-type]
while True:
line = fp.readline()
if not line:
break
tag, data = parseln(line)
info = FileInfo(**data) # type: ignore[arg-type]
path = Path(info.path)
info = info._replace(path=os.fspath(path.relative_to(root)))
cursor = db.execute(
"SELECT * FROM data WHERE path = ?", (info.path,)
)
rows = cursor.fetchall()
if not rows:
println("MISSING", info.path, file=out_file)
elif len(rows) > 1:
raise RuntimeError(f"Primary key failure: {info.path!r}")
else:
new_info = FileInfo(*rows[0])
if info == new_info:
println("OK", info.path, file=out_file)
else:
diff = []
for field in info._fields:
if getattr(info, field) != getattr(new_info, field):
diff.append(field)
if not (tag == "DIR" and diff == ["size"]):
println(
"MISMATCH",
{"path": info.path, "kind": tag, "diff": diff},
file=out_file,
)
rows_seen += 1
if rows_seen % 100000 == 0:
logging.info(f"Rows seen: {rows_seen}")
if args.output:
out_file.close()
db.close()
# --------------------------------------------------------------------------
def init_logging() -> None:
logging.basicConfig(
format="[%(asctime)s] %(levelname)s %(message)s",
level=logging.ERROR,
stream=sys.stderr,
)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", action="store_true")
parser.set_defaults(func=None)
parser.set_defaults(verbose=False)
subparsers = parser.add_subparsers()
hash_parser = subparsers.add_parser("hash")
hash_parser.add_argument("-o", "--output", metavar="file")
hash_parser.add_argument("root")
hash_parser.set_defaults(func=do_hash)
convert_parser = subparsers.add_parser("convert")
convert_parser.add_argument("source")
convert_parser.add_argument("destination")
convert_parser.set_defaults(func=do_convert)
compare_parser = subparsers.add_parser("compare")
compare_parser.add_argument("-o", "--output", metavar="file")
compare_parser.add_argument("source-list")
compare_parser.add_argument("destination-db")
compare_parser.set_defaults(func=do_compare)
return parser.parse_args()
def main() -> None:
args = parse_args()
if args.func:
if args.verbose:
logging.getLogger().setLevel(logging.DEBUG)
args.func(args)
else:
raise RuntimeError("No action specified on the command line")
if __name__ == "__main__":
try:
init_logging()
main()
except Exception: # pylint: disable=broad-except
logging.exception("Uncaught exception")
sys.exit(1)