-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashcheck.py
373 lines (313 loc) · 13.3 KB
/
hashcheck.py
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import hashlib
import argparse
import sqlite3
from datetime import datetime
import os
import signal
import sys
from filedate.Utils import Copy
class destination_file():
def __init__(self, destdir):
self.destdir = destdir
self.sourcepath = None
self.destpath = None
self.destfile = None
def open(self, filepath):
self.sourcepath = filepath
(drive, pathandfile) = os.path.splitdrive(filepath)
(path, file) = os.path.split(pathandfile)
if not os.path.isdir(self.destdir + path):
os.makedirs(self.destdir + path)
self.destpath = self.destdir + pathandfile
self.destfile = open(self.destpath, "wb")
def write(self, data):
self.destfile.write(data)
def close(self):
self.destfile.close()
Copy(self.sourcepath, self.destpath).all()
self.destfile = None
def parse_args():
parser = argparse.ArgumentParser(description="Version 1.0.2")
mode_group = parser.add_mutually_exclusive_group(required=True)
mode_group.add_argument("-g", "--generate", help="Generate hashes for new files in specified file/directory", action='store_true')
mode_group.add_argument("-c", "--check", help="Check stored hashes for specified file/directory (always recursively)", action='store_true')
mode_group.add_argument("-e", "--enumerate", help="List files not present in DB", action='store_true')
mode_group.add_argument("-m", "--missing", help="Only check for missing files (always recursively)", action='store_true')
mode_group.add_argument("-p", "--prune", help="Prune missing files from DB (always recursively)", action='store_true')
parser.add_argument("-r", "--recursive", help="Recursive search", action='store_true')
parser.add_argument("-u", "--update", help="Update existing hashes", required=False, action='store_true')
parser.add_argument("-t", "--test-run", help="Test run", action='store_true')
parser.add_argument('-v', '--verbose', action='count', default=0, help="verbose output (repeat for increased verbosity)")
parser.add_argument("-d", "--database", help="Specify database file", required=False, default="hashes.sqlite")
parser.add_argument("-o", "--outfile", help="Output to file", required=False)
parser.add_argument("-s", "--session", help="Session number", required=False, type=int)
parser.add_argument("--db-path", help="Path in DB", required=False)
parser.add_argument("--fs-path", help="Path in filesystem", required=False)
parser.add_argument("--path-conv-to", help="Convert slashes in paths to (u/w)", required=False)
parser.add_argument("--copy-to", help="Copy hashed files to destination", required=False)
parser.add_argument("path", help="Path")
args = parser.parse_args()
if args.recursive and not (args.generate or args.enumerate):
output("--recursive only available with --generate or --enumerate")
sys.exit(1)
if args.update and not args.generate:
output("--update only available with --generate")
sys.exit(1)
if args.session and not args.generate:
output("--session only available with --generate")
sys.exit(1)
if bool(args.db_path != None) ^ bool(args.fs_path != None):
output("Path substitution needs both sides!")
sys.exit(1)
if args.path_conv_to != None:
if args.db_path == None:
output("Path conversion only useful with path substitution!")
sys.exit(1)
args.path_conv_to = args.path_conv_to.lower()
if args.path_conv_to != "u" and args.path_conv_to !="w" :
output("Path conversion to [w]indows or [u]nix")
sys.exit(1)
if args.copy_to != None:
if not (args.generate or args.check):
output("Copy only supported while generating or checking!")
sys.exit(1)
if os.path.abspath(args.path) in os.path.abspath(args.copy_to):
output("Copy destination can't be inside the source folder!")
sys.exit(1)
return args
def output(string, to_stdout = 0, to_file = None):
if args.verbose >= to_stdout:
print(string)
if to_file != None and args.verbose >= to_file and outfile != None:
print(string, file=outfile)
def getFileList(path, recursive):
output("Listing files and folders...")
filelist = []
if os.path.isfile(path):
filelist = [path]
elif os.path.isdir(path):
for (dirpath, dirnames, filenames) in os.walk(path):
for f in filenames:
filelist.append(os.path.join(dirpath, f))
if not recursive:
break
else:
output("Invalid path! {}".format(path))
terminate(2)
return filelist
def getFilter(path):
if os.path.isfile(path):
filter = path
elif os.path.isdir(path):
if(path[-1] != os.sep):
filter = path + os.sep + "%"
else:
filter = path + "%"
else:
filter = "%"
return filter
def getSubset(abspath, new, recursive):
filelist = getFileList(abspath, recursive)
filter = getFilter(abspath)
crsr = mem_db.cursor()
crsr.execute("SELECT filename FROM hashes WHERE filename LIKE ?", (filter,))
dbresults = crsr.fetchall()
try:
dblist = list(zip(*dbresults))[0]
except IndexError:
dblist = []
if new:
files = list(set(filelist) - set(dblist))
else:
files = list(set(dblist) - set(filelist))
return sorted(files)
def hash_file(filepath):
if destfile:
destfile.open(filepath)
try:
with open(filepath,"rb") as f:
file_hash = hashlib.sha256()
chunk = f.read(1048576)
while chunk:
file_hash.update(chunk)
if destfile:
destfile.write(chunk)
chunk = f.read(1048576)
f.close()
if destfile:
destfile.close()
return file_hash.hexdigest()
except (PermissionError, OSError):
output("Unable to open file {}".format(filepath), 0, 0)
def generate_hashes(filelist, update):
lastsave = datetime.now()
crsr = mem_db.cursor()
prevdir = ""
crsr.execute("SELECT filename, sha256 FROM hashes")
dbresults = crsr.fetchall()
try:
dblist = list(zip(*dbresults))[0]
except IndexError:
dblist = []
if update:
try:
hashlist = list(zip(*dbresults))[1]
except IndexError:
hashlist = []
for f in filelist:
timediff = datetime.now() - lastsave
if timediff.total_seconds() > 300:
save_db()
if outfile != None:
outfile.flush()
lastsave = datetime.now()
dir = os.path.dirname(f)
if dir != prevdir:
prevdir = dir
output("Processing folder {}".format(prevdir))
if f not in dblist:
if not args.test_run:
output("Hashing {}".format(f), 1, 2)
if os.path.isfile(f):
hash = hash_file(f)
if hash != None:
createdstr = datetime.fromtimestamp(os.path.getctime(f))
modifiedstr = datetime.fromtimestamp(os.path.getmtime(f))
mem_db.execute("INSERT INTO hashes VALUES (NULL, ?, ?, ?, ?, ?, ?, ?)", (f, hash, os.path.getsize(f), createdstr, modifiedstr, datetime.utcnow(), args.session))
mem_db.commit()
else:
output("File was deleted: {}".format(f), 0, 0)
else:
output("Hashing skipped {}".format(f), 0, 0)
else:
if update:
index = dblist.index(f)
oldhash = hashlist[index]
if os.path.isfile(f):
hash = hash_file(f)
if hash != oldhash:
if not args.test_run:
output("Updating file {}".format(f), 0, 0)
createdstr = datetime.fromtimestamp(os.path.getctime(f))
modifiedstr = datetime.fromtimestamp(os.path.getmtime(f))
mem_db.execute("UPDATE hashes SET sha256=?, filesize=?, creation_date=?, modified_date=?, timestamp=?, session=? WHERE filename=?", (hash, os.path.getsize(f), createdstr, modifiedstr, datetime.utcnow(), args.session, f))
mem_db.commit()
else:
output("Update skipped: {}".format(f), 0, 0)
else:
output("Hash already correct: {}".format(f), 1, 2)
else:
output("File was deleted: {}".format(f), 0, 0)
def check_hashes(filter):
prevdir = ""
lastsave = datetime.now()
crsr = mem_db.cursor()
for row in crsr.execute("SELECT * FROM hashes WHERE filename LIKE ?", (filter,)):
filename = row[1]
stored_hash = row[2]
timediff = datetime.now() - lastsave
if timediff.total_seconds() > 300 and outfile != None:
outfile.flush()
lastsave = datetime.now()
if args.verbose > 0:
dir = os.path.dirname(filename)
if dir != prevdir:
prevdir = dir
output("Processing folder {}".format(prevdir))
output("Checking {}".format(filename), 2, 3)
if os.path.isfile(filename):
hash = hash_file(filename)
if(hash != stored_hash):
output("Hash mismatch for {}".format(filename), 0, 0)
output("Hash OK for {}".format(filename), 2, 3)
else:
output("File missing: {}".format(filename), 0, 0)
def prune_db(abspath):
filelist = getSubset(abspath, False, True)
output("Pruning DB...")
mem_db.execute("DELETE FROM hashes WHERE filename in ({seq})".format(seq=','.join(['?']*len(filelist))), filelist)
mem_db.commit()
def save_db():
if not args.test_run:
output("Saving DB...")
mem_db.commit()
mem_db.backup(db)
db.commit()
def terminate(exitcode):
if args.generate or args.prune:
save_db()
db.close()
mem_db.close()
sys.exit(exitcode)
def exit_handler(signum, frame):
output("Cancelled, exiting...")
terminate(1)
if __name__ == "__main__" :
sys.stdout.reconfigure(encoding='utf-8')
signal.signal(signal.SIGINT, exit_handler)
args = parse_args()
if args.outfile:
try:
outfile = open(args.outfile, "w", encoding="utf-8")
except:
output("Unable to open output file", 0)
sys.exit(1)
else:
outfile = None
if args.copy_to != None and os.path.isdir(os.path.abspath(args.copy_to)):
destpath = os.path.abspath(args.copy_to)
destfile = destination_file(destpath)
else:
destpath = None
destfile = None
if args.session == None:
args.session = 1
mem_db = sqlite3.connect(":memory:")
try:
db = sqlite3.connect(args.database)
db.execute("CREATE TABLE IF NOT EXISTS hashes(id INTEGER PRIMARY KEY, filename TEXT NOT NULL, sha256 TEXT NOT NULL, filesize INTEGER, creation_date TEXT, modified_date TEXT, timestamp TEXT, session INTEGER)")
except sqlite3.DatabaseError:
output("Invalid DB file")
sys.exit(2)
db.commit()
db.backup(mem_db)
start = datetime.now()
if args.db_path != None and args.fs_path != None and not args.generate and not args.prune:
mem_db.execute("UPDATE hashes SET filename = REPLACE(filename, ?, ?) WHERE filename LIKE ?", (args.db_path, args.fs_path, "%"+args.db_path+"%"))
mem_db.commit()
if args.path_conv_to != None:
if args.path_conv_to == "w":
fromChar = "/"
toChar = "\\"
else:
fromChar = "\\"
toChar = "/"
mem_db.execute("UPDATE hashes SET filename = REPLACE(filename, ?, ?)", (fromChar, toChar))
mem_db.commit()
abspath = os.path.abspath(args.path)
if args.generate:
if not args.update:
filelist = getSubset(abspath, True, args.recursive)
else:
filelist = getFileList(abspath, args.recursive)
generate_hashes(filelist, args.update)
elif args.check:
filter = getFilter(abspath)
check_hashes(filter)
elif args.prune:
if os.path.exists(abspath):
prune_db(abspath)
else:
output("Invalid path! {}".format(abspath))
elif args.enumerate or args.missing:
if args.enumerate:
text = "New file:"
new = True
elif args.missing:
text = "File missing:"
new = False
files = getSubset(abspath, new, args.recursive or args.missing)
for f in files:
output ("{} {}".format(text, f), 0, 0)
output ("Time: {}".format (datetime.now()-start), 0, 0)
terminate(0)