This repository has been archived by the owner on Apr 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dup.py
1025 lines (673 loc) · 27.2 KB
/
dup.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import io, os, sys
import hashlib, binascii
import time
import sqlite3
import signal
import utils
from colorama import Fore, Back, Style
from colorama import init
#
# Some constants
#
FMT_STR_CONSIDERING_DIR = "Considering " + Fore.LIGHTGREEN_EX + Style.DIM + "{}" + Fore.RESET + Style.RESET_ALL + " (master:{}, protected:{})..."
FMT_STR_COMPLETED_DIR = "Completed directory lookup for " + Fore.LIGHTGREEN_EX + Style.DIM + "{}" + Fore.RESET + Style.RESET_ALL
#
# 1. Discovering files
# 2. Calculating quick hash (on the first 8192 bytes only)
# 3. Calculating complete hash for duplicate candidates
#
# REMEMBER : Trust No-One. Filter and sanitize all fields and entries
#
#
# ---> Some inits
#
algo = utils.hash_algo
db = utils.db_name
filelist = utils.filelist_name
restart = False
last_step = None
last_id = None
def exit_handler(signum, frame):
print()
print("Normal exit from KeyboardInterrupt (CTRL+C)")
utils.checkpoint_db(cnx, last_step, last_id, commit = True)
exit(0)
#
# ====================================================================
# File hash calculation (single file)
# ====================================================================
#
def file_hash_calc(filename, algo_name, pre_hash=True):
"""
Returns the hash of a file. You can choose any hash function that your Phython environment supports.
It can be used for a full hash computation, or only a pre-hash (much quicker with large files). Pre-hash
computes the hash only on the first bytes. As we are looking for duplicates, two files with a different
pre-hash are... different! No need to compute the full hash.
On the contrary, if the pre-hash is the same for some files, we calculate the full hash (with the entire
file content) and then we could decide if files are duplicates or not.
Args:
filename (text): Absolute path for the file
algo_name(text): Name of the hash algo we use ("md5", "sha1", and even "crc32")
pre_hash (boolean): Indicates if we compute a pre-hash or not
Returns:
h (text): The hash value (hexa text)
t (int): execution time (in seconds)
.. Example:: file_hash_calc("test.txt", "md5")
"""
# Start time
chrono = utils.Chrono()
chrono.start()
#
# ---> Depending on the selected algo, let's calculate the hash
#
if (algo_name != "crc32"):
# Here we have the 'hashlib' hashes
# ---
hl = getattr(hashlib, algo_name)()
#
# We calculate here only the first bytes for the pre_hash (to speed up the calculation and alors to avoid MemoryError with big files)
#
# For complete hash, we need to split the reading.
#
with open(filename,'rb') as f:
if (pre_hash):
#
# Pre-hash => Only on the first bytes
#
hl.update(f.read(io.DEFAULT_BUFFER_SIZE))
else:
#
# Complete hash => split the file
#
while True:
#
# We use the read only the size of the block to avoid
# heavy ram usage. The file content is buffered.
#
data = f.read(io.DEFAULT_BUFFER_SIZE)
if not data:
# if we don't have any more data to read, stop.
break
# we partially calculate the hash
hl.update(data)
#
# We have exceptions: the 'shake' hashes need one argument
# Else, for all other hashes, we don't need any argument adnd we
# can calculate the hash with the hexdigest() method.
#
if (algo_name == "shake_128"):
h = hl.hexdigest(128)
elif (algo_name == "shake_256"):
h = hl.hexdigest(256)
else:
h = hl.hexdigest()
else:
#
# Here we have the 'binascii' hashes (CRC32). Included in an other library.
#
with open(filename,'rb') as f:
h = binascii.crc32(f.read(8192))
# End time
chrono.stop()
#
# ---> We return both hash and execution time (for info)
#
return h, chrono.elapsed()
#
# ====================================================================
# Database connexion
# ====================================================================
#
def db_connect(db, restart = False):
"""
Connects to the file which will be used for the sqlite3 database. If needed (= if you start a new search or if you've asked for a restart manually),
a new database will be created, else you just get the pointer to the file.
If the database exists, we look into to get the state of the last call of the script, to manage the restart option, else we create a new database.
Args:
db (text): Name of the file used for storing sqlite3 database
restart (boolean): (Optional) Indicates if your restart the process from the beginning or not
Returns:
cnx (sqlite3.Connection): Connection object (bound to the file _filename_)
"""
cnx = sqlite3.connect(db)
#
# ---> Did we ask for a restart or not?
#
if not restart:
# We didn't, so we continue the process. Does the table 'params' exist?
res = cnx.execute("SELECT count(*) FROM sqlite_master WHERE type ='table' AND name ='params';").fetchone()
if (res[0] == 1):
# Yes, so we look for the status. If the status is empty, no step has been executed
# so we have to recreate the database to start on clean basis.
step = get_status(cnx)
if (step == None):
# No step in the database => Let's recreate the DB
cnx.close()
cnx = db_create(db)
else:
# The script is in progress
pass
else:
# No, there's no existing database, so we create one
cnx = db_create(db)
else:
#
# 'restart' is passed in args. So we drop & recreate the database
#
cnx = db_create(db)
return cnx
#
# ====================================================================
# Database creation
# ====================================================================
#
def db_create(db):
"""
Creates the database tables (unconditionnally).
Args:
db (text): Name of the file used for storing sqlite3 database
Returns:
cnx (sqlite3.Connection): Connection object (bound to the file _filename_)
"""
cnx = sqlite3.connect(db)
#cnx = sqlite3.connect(':memory:') ==> in-memory for sqlite3 is not really faster
print("Creating database...")
#
# ---> Dropping old tables
#
try:
# Drop all tables
cnx.execute("DROP TABLE filelist")
cnx.execute("DROP TABLE params")
print("Old database deleted.")
except sqlite3.OperationalError:
# Exception means no old database
print("No old database.")
#
# ---> Let's create the table used for storing files information
#
cnx.execute("CREATE TABLE filelist (\
fid INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, \
hash CHAR(256), \
pre_hash CHAR(256), \
path VARCHAR(4096), \
name TINYTEXT, \
original_path VARCHAR(4096), \
size BIGINT, \
marked_for_deletion BOOL, \
protected BOOL, \
access_denied BOOL, \
master BOOL, \
has_duplicate BOOL, \
os_errno TINYINT, \
os_strerror TEXT, \
trashed BOOL, \
delete_error TINYINT) \
")
# ---> Some useful indexes to speed up the processing
cnx.execute("CREATE INDEX index_filepath ON filelist (path, name)")
cnx.execute("CREATE INDEX index_hash ON filelist (hash)")
cnx.execute("CREATE INDEX index_pre_hash ON filelist (pre_hash)")
#
# ---> Params table used to store infomation about the process and restart steps.
#
cnx.execute("CREATE TABLE params(\
key TINYTEXT,\
value TEXT)\
")
# --- > Default values
cnx.execute("INSERT INTO params VALUES ('last_step','')")
cnx.execute("INSERT INTO params VALUES ('last_id','')")
cnx.execute("INSERT INTO params VALUES ('last_path','')")
cnx.execute("INSERT INTO params VALUES ('last_file','')")
cnx.commit()
return cnx
#
# ====================================================================
# Get state of last call in the params table
# ====================================================================
#
def get_status(cnx):
"""
Gets the status of the last process, if it has beend stored, to handle a restart if needed.
Args:
cnx (sqlite3.Connection): Connection object
Returns:
value (text): Name of the last executed process
"""
res = cnx.execute("SELECT value FROM params WHERE key='last_step'").fetchone()
#
# ---> Get last step
#
if (res != None):
if (res[0] == ''):
res= None
else:
res = res[0]
#
# ---> Get last ID (fid or hash)
#
lid = cnx.execute("SELECT value FROM params WHERE key='last_id'").fetchone()
if (lid != None):
if (lid[0] == ''):
lid= None
else:
lid = lid[0]
return res, lid
#
# ====================================================================
# Set state of last (=current) step executed in the params table
# ====================================================================
#
def checkpoint_db(cnx, last_step, last_id = None, commit = False):
"""
Sets the status of the current process by storing the name of the last well-executed step.
Args:
cnx (sqlite3.Connection): Connection object
last_step (text): Name of the last well-executed step
Returns:
nothing
"""
cnx.execute("UPDATE params SET value=? WHERE key='last_step'", (last_step,))
cnx.execute("UPDATE params SET value=? WHERE key='last_id'", (last_id,))
if (commit):
cnx.commit()
return
#
# ====================================================================
# Directory calculation (for all files in many directories)
# ====================================================================
#
def directories_lookup(cnx, basepath_list):
"""
Args:
cnx (sqlite3.Connection): Connection object
basepath (text): Array of file paths we will look into.
Returns:
t (time): The execution time of this function
nb_to_process (int): The number of files we have to process
"""
t_elaps = 0.0
# We look for the directories already looked up
completed_dir = []
res = cnx.execute("SELECT ALL value FROM params WHERE key = 'completed_dir'")
for dir_name in res:
completed_dir.append(dir_name[0])
print(FMT_STR_COMPLETED_DIR.format(dir_name[0]))
# Loop over directories
for basepath in basepath_list:
line = basepath.rstrip("\n").split(";")
if line[0] != '':
p_master, p_protected, path = line
# If the directory has been completed, we skip it. Else, we restart the lookup from the beginning.
if (path not in completed_dir):
# Restart point here. We delete what has been done for this directory (for it has not been completed)
cnx.execute("DELETE FROM filelist WHERE original_path=?", (path,))
cnx.commit()
print(FMT_STR_CONSIDERING_DIR.format(path, bool(int(p_master)), bool(int(p_protected))))
t = directory_lookup(cnx, path, p_master, p_protected)
t_elaps += t
# Returning nb of files to process in the table. Should be the same as nb...
r = cnx.execute("SELECT COUNT(*) FROM filelist")
nb_to_process = r.fetchone()[0]
return t_elaps, nb_to_process
#
# ====================================================================
# Directory calculation (for all files in one directory)
# ====================================================================
#
def directory_lookup(cnx, basepath, master, protected):
"""
Looks (hierarchically) for all files within the folder structure, and stores the path and the
name of each file. No file access is made (to save time).
Args:
cnx (sqlite3.Connection): Connection object
basepath (text): Array of file paths we will look into.
Returns:
t (time): The execution time of this function
"""
global last_step, last_id
# Start time
chrono = utils.Chrono()
chrono.start()
# Nb of files init
nb = 0
# Filepath init
if (basepath == ""):
basepath = "."
#
# ---> Files discovering. Thanks to Python, we just need to call an existing function...
#
for root, _, files in os.walk(basepath, topdown=True):
#
# We just look for files, we don't process the directories
#
for name in files:
# Hey, we got one (file)!
nb = nb + 1
cnx.execute("INSERT INTO filelist(path, name, access_denied, original_path, master, protected)\
VALUES (?, ?, ?, ?, ?, ?)",(root, name, False, basepath, master, protected))
# Checkpoint
last_step = "directory_lookup"
last_id = "in progress"
# Displaying progression and commit (occasionnaly)
if ((nb % 100) == 0):
print("Discovering #{} files ({:.2f} sec)".format(nb, chrono.elapsed()), end="\r", flush=True)
if ((nb % 1000) == 0):
cnx.commit()
"""
except PermissionError:
cnx.execute("INSERT INTO filelist(pre_hash, path, name, access_denied)\
VALUES (?, ?, ?, ?)",("?", root, name, True))
except OSError as ose:
cnx.execute("INSERT INTO filelist(pre_hash, path, name, os_errno, os_strerror)\
VALUES (?, ?, ?, ?, ?)",("?", root, name, ose.errno, ose.strerror))
"""
#
# ---> Last commit
#
utils.checkpoint_db(cnx, "directory_lookup", basepath, commit = True)
# End time
chrono.stop()
return chrono.elapsed()
#
# ====================================================================
# File list pre hash calculation (for all files)
# ====================================================================
#
def filelist_pre_hash(cnx, algo):
"""
Calculates a "pre-hash" that is a hash calculated on the first bytes of the file.
Listen to me well: hash calculation can be long for... long files. Here we make a first *selection*
where we eliminate files without duplicates. As a matter of fact, if the hash of the first
bytes are not equals, it means that the files are... not equals for sure!
On the other hand, files with the same pre-hash need further investigation and we'll
calculate in the next step the complete hash, but only for duplicate candidates. Then
we avoid unnecessary long calculation, especially for big files.
Though you can use any system-known hash function, we suggest to use only **md5** for
pre-hashing, because it has enough entropy for our usage, and it's quicker in most situations.
Better algos will have better result, but here we only want some file duplicate candidate selection.
Args:
cnx (sqlite3.Connection): Connection object
algo (text): Name of the hash algo to use.
Returns:
t (time): The execution time of this function
"""
global last_step, last_id
# Start time
chrono = utils.Chrono()
chrono.start()
#
# ---> Main loop (on all files)
#
res = cnx.execute("select count(fid) from filelist;")
nb_total = res.fetchone()[0]
if (last_step != "filelist_pre_hash"):
nb = 0
r = cnx.execute("SELECT * FROM filelist ORDER BY fid")
else:
res = cnx.execute("SELECT count(fid) FROM filelist WHERE fid <=? ORDER BY fid", (last_id,))
nb = res.fetchone()[0]
r = cnx.execute("SELECT * FROM filelist WHERE fid >? ORDER BY fid", (last_id,))
print("Restart from fid {}".format(last_id))
for row in r:
# Let's get the filepath of this element
fid = row[0]
filepath = os.path.join(row[3], row[4])
try:
file_stats = os.stat(filepath)
file_size = file_stats.st_size
h, _ = file_hash_calc(filepath, algo)
cnx.execute("UPDATE filelist SET size = ?, pre_hash = ? WHERE fid = (?)", (file_size, h, fid))
# Checkpoint
last_step = "filelist_pre_hash"
last_id = fid
except PermissionError as pe:
#
# Here we have an existing file but we have no right permission on it. Bad strike!
#
cnx.execute("UPDATE filelist SET size = ?, access_denied = ? WHERE fid = (?)", (file_size, True, fid))
except OSError as ose:
#
# Worst: we have an OS Error while retrieving file information or during hash calculation
#
# Example: We'll get an #22 error with an OneDrive file stored only in the cloud and not present on disk
#
cnx.execute("UPDATE filelist SET os_errno = ?, os_strerror=? WHERE fid = (?)", (ose.errno, ose.strerror, fid))
nb = nb + 1
# Displaying progression and commit (occasionnaly)
if ((nb % 100) == 0):
perc = (nb / nb_total) * 100
print("Quick hash computing #{} files ({:.2f}%), {:.2f} sec".format(nb, perc, chrono.elapsed()), end="\r", flush=True)
if ((nb % 1000) == 0):
utils.checkpoint_db(cnx, "filelist_pre_hash", fid, commit = True)
#
# ---> Last commit
#
utils.checkpoint_db(cnx, "filelist_pre_hash", "all", commit = True)
# End time
chrono.stop()
return chrono.elapsed()
#
# ====================================================================
# Finding pre-duplicates (files with the same pre_hash). We need to calculate the full hash for all of them
# ====================================================================
#
def pre_duplicates_rehash(cnx):
"""
Recalculates full hash for duplicate candidates (files having the same pre-hash). You can use here
the hash function you want but consider "md5" as the best ratio entropy/execution time. The updates
are made directly in the database.
Args:
cnx (sqlite3.Connection): Connection object
Returns:
t (time): The execution time of this function
"""
global last_step, last_id
# Start time
chrono = utils.Chrono()
chrono.start()
#
# ---> Selection of pre_hashes present more than once
#
res = cnx.execute("SELECT count(fid) FROM filelist WHERE pre_hash IN (SELECT pre_hash FROM filelist GROUP BY pre_hash HAVING COUNT(pre_hash) > 1 ORDER BY pre_hash);")
nb_total = res.fetchone()[0]
if (last_step != "pre_duplicates_rehash"):
# Here we start from the beginning and read all the files in DB
nb = 0
res = cnx.execute("SELECT pre_hash FROM filelist GROUP BY pre_hash HAVING COUNT(pre_hash) > 1 ORDER BY pre_hash")
else:
# Checkpoint/restart : we restart from last updated record (fid)
res = cnx.execute("SELECT count(fid) FROM filelist WHERE hash NOT NULL")
nb = res.fetchone()[0]
# Restart point
res = cnx.execute("SELECT pre_hash FROM filelist WHERE pre_hash >? GROUP BY pre_hash HAVING COUNT(pre_hash) > 1 ORDER BY pre_hash", (last_id,))
print("Restart from pre_hash {}".format(last_id))
# Progression
last_d = 0
last_m = 0
# Loop...
for h in res:
#
# We look for all files that have the selected pre_hash
#
hash = h[0]
r = cnx.execute("SELECT * FROM filelist WHERE pre_hash = ?", (hash, ))
for row in r:
# Here we need to go a bit further: having the same pre_hash
# does not mean that files are identical; we need to calculate the complete hash
fid = row[0]
filepath = os.path.join(row[3], row[4])
full_hash, _ = file_hash_calc(filepath, "md5", False)
cnx.execute("UPDATE filelist set hash = ? WHERE fid = (?)", (full_hash, fid))
# Checkpoint
last_step = "pre_duplicates_rehash"
last_id = hash
nb = nb + 1
# Displaying progression and commit (occasionnaly, but only when we get a new hash)
d = nb // 100
m = nb // 1000
if (d != last_d):
last_d = d
perc = (nb / nb_total) * 100
print("Rehashing duplicate candidates #{} ({:.2f}%), {:.2f} sec".format(nb, perc, chrono.elapsed()), end="\r", flush=True)
if (m != last_m):
last_m = m
utils.checkpoint_db(cnx, "pre_duplicates_rehash", fid, commit = True)
#
# ---> Last commit
#
utils.checkpoint_db(cnx, "pre_duplicates_rehash", "all", commit = True)
# End time
chrono.stop()
return chrono.elapsed(), nb
#
# ====================================================================
# Selecting "true" duplicates (= having the same hash ;)
# ====================================================================
#
def duplicates_update(cnx):
"""
Selects the *real* duplicates (= files having the same *full* hash), and marks them into the DB.
Args:
cnx (sqlite3.Connection): Connection object
Returns:
t (time): The execution time of this function
nb (int): The number of files having duplicates
size (int): The size of all files that have duplicates
"""
global last_step, last_id
# Start time
chrono = utils.Chrono()
chrono.start()
#
# ---> All-in-one SQL query (updating the files in the selection of duplicates)
#
# The query has 2 parts:
# . The UPDATE that marks the 'has_duplicate' of all files whose hash is in the selected ones;
# . The SELECT used in the 'IN' clause where we select (complete) hashes present more than once.
#
cnx.execute("UPDATE filelist SET has_duplicate = True WHERE hash IN \
(SELECT hash FROM filelist WHERE hash NOT NULL GROUP BY hash HAVING COUNT(hash) > 1 ORDER BY hash)")
#
# ---> Last commit
#
# Checkpoint
last_step = "pre_duplicates_rehash"
last_id = "all"
utils.checkpoint_db(cnx, "duplicates_update", "all", commit = True)
# Results...
nb , size = duplicates_select(cnx)
# End time
chrono.stop()
return chrono.elapsed(), nb, size
#
# ====================================================================
# Selecting "true" duplicates (= having the same hash ;)
# ====================================================================
#
def duplicates_select(cnx):
"""
Returns infos about files having duplicates in the database.
Args:
cnx (sqlite3.Connection): Connection object
Returns:
nb (int): The number of files having duplicates
size (int): The size of all files that have duplicates
"""
# Nb of files that have duplicates
# ---
r = cnx.execute("SELECT COUNT(*) FROM filelist WHERE has_duplicate = True")
nb = r.fetchone()[0]
# Size of all files that are duplicated
# ---
r = cnx.execute("SELECT SUM(size) FROM filelist WHERE has_duplicate = True")
size = r.fetchone()[0]
return nb, size
#
# ====================================================================
#
# Main part
#
# ====================================================================
#
def main():
# Colorama init
init()
#
# ---> Check for 'restart' argument
#
arguments = utils.check_arguments(sys.argv)
if ("restart" in arguments):
restart = True
else:
restart = False
#
# ---> Catch the exit signal to commit the database with last checkpoint
#
signal.signal(signal.SIGINT, exit_handler)
#
# ---> Read the directory files list
#
with open(filelist, "r") as f:
basepath = f.readlines()
#print(basepath)
print("Default blocksize for this system is {} bytes.".format(io.DEFAULT_BUFFER_SIZE))
#
# ---> DB connection
#
cnx = db_connect(db, restart)
last_step, last_id = get_status(cnx)
print("Last step: {}, last ID: {}".format(last_step, last_id))
next_step = False
# Looking for files
# ---
if (last_step == None) | ((last_step == "directory_lookup") & (last_id == "in progress")):
t, nb = directories_lookup(cnx, basepath)
print("Files lookup duration: {:.2f} sec for {} files.".format(t, nb))
next_step = True
else:
print("Files lookup already done.")
# Calculating pre hash (quick hash on first bytes)
# ---
if (next_step |
((last_step == "directory_lookup") & (last_id == "all"))|
((last_step == "filelist_pre_hash") & (last_id != "all"))):
t = filelist_pre_hash(cnx, 'md5')
print("Pre-hash calculation duration: {:.2f} sec. ".format(t))
next_step = True
else:
print("Pre-hash calculation already done.")
# Calculate size of all files
# ---
res = cnx.execute("select sum(size) FROM filelist")
size = res.fetchone()[0]
print("Size of all files: {}".format(utils.humanbytes(size)))
# Recomputing hashes for duplicates candidates
# ---
if (next_step |
((last_step == "filelist_pre_hash") & (last_id == "all")) |
((last_step == "pre_duplicates_rehash") & (last_id != "all"))):
t, nb = pre_duplicates_rehash(cnx)
print("Pre-duplicates rehashing duration: {:.2f} sec. for {} records.".format(t, nb))
next_step = True
else:
print("Pre-duplicates rehashing already done.")
# Dealing with duplicates
# ---
if (next_step | (last_step == "pre_duplicates_rehash")):
t, nb_dup, size_dup = duplicates_update(cnx)