@@ -233,26 +233,34 @@ def insert_file(file, detection, src, conn):
233
233
cursor .execute ("SET @file_last = LAST_INSERT_ID()" )
234
234
235
235
236
- def insert_filechecksum (file , checktype , conn ):
236
+ def insert_filechecksum (file , checktype , file_id , conn ):
237
237
if checktype not in file :
238
238
return
239
239
240
240
checksum = file [checktype ]
241
241
checksize , checktype , checksum = get_checksum_props (checktype , checksum )
242
242
243
- query = f"INSERT INTO filechecksum (file, checksize, checktype, checksum) VALUES (@file_last, '{ checksize } ', '{ checktype } ', '{ checksum } ')"
243
+ query = "INSERT INTO filechecksum (file, checksize, checktype, checksum) VALUES (%s, %s, %s, %s)"
244
+ with conn .cursor () as cursor :
245
+ cursor .execute (query , (file_id , checksize , checktype , checksum ))
246
+
247
+ add_all_equal_checksums (checksize , checktype , checksum , file_id , conn )
248
+
249
+
250
+ def add_all_equal_checksums (checksize , checktype , checksum , file_id , conn ):
251
+ """
252
+ We can update all the checksums when file size is less than the checksum size type, as all checksums are equal in that case.
253
+ """
244
254
with conn .cursor () as cursor :
245
- cursor .execute (query )
246
255
if "md5" not in checktype :
247
256
return
248
-
249
257
size_name = "size"
250
258
if checktype [- 1 ] == "r" :
251
259
size_name += "-rd"
252
260
if checktype [- 1 ] == "s" :
253
261
size_name += "-d"
254
262
255
- cursor .execute (f"SELECT `{ size_name } ` FROM file WHERE id = @file_last " )
263
+ cursor .execute (f"SELECT `{ size_name } ` FROM file WHERE id = { file_id } " )
256
264
result = cursor .fetchone ()
257
265
if not result :
258
266
return
@@ -281,9 +289,10 @@ def insert_filechecksum(file, checktype, conn):
281
289
checksum_size = exploded .pop ()
282
290
checksum_type = "-" .join (exploded )
283
291
284
- query = "INSERT INTO filechecksum (file, checksize, checktype, checksum) VALUES (@file_last, %s, %s, %s)"
285
- with conn .cursor () as cursor :
286
- cursor .execute (query , (checksum_size , checksum_type , checksum ))
292
+ query = "INSERT INTO filechecksum (file, checksize, checktype, checksum) VALUES (%s, %s, %s, %s)"
293
+ cursor .execute (
294
+ query , (file_id , checksum_size , checksum_type , checksum )
295
+ )
287
296
288
297
289
298
def delete_filesets (conn ):
@@ -558,9 +567,13 @@ def db_insert(data_arr, username=None, skiplog=False):
558
567
559
568
for file in unique_files :
560
569
insert_file (file , detection , src , conn )
570
+ file_id = None
571
+ with conn .cursor () as cursor :
572
+ cursor .execute ("SELECT @file_last AS file_id" )
573
+ file_id = cursor .fetchone ()["file_id" ]
561
574
for key , value in file .items ():
562
575
if key not in ["name" , "size" , "size-r" , "size-rd" , "sha1" , "crc" ]:
563
- insert_filechecksum (file , key , conn )
576
+ insert_filechecksum (file , key , file_id , conn )
564
577
565
578
if detection :
566
579
conn .cursor ().execute (
@@ -1070,7 +1083,6 @@ def set_perform_match(
1070
1083
with conn .cursor () as cursor :
1071
1084
if len (candidate_filesets ) == 1 :
1072
1085
matched_fileset_id = candidate_filesets [0 ]
1073
-
1074
1086
cursor .execute (
1075
1087
"SELECT status FROM fileset WHERE id = %s" , (matched_fileset_id ,)
1076
1088
)
@@ -1123,6 +1135,7 @@ def set_perform_match(
1123
1135
1124
1136
elif len (candidate_filesets ) > 1 :
1125
1137
found_match = False
1138
+
1126
1139
for candidate_fileset in candidate_filesets :
1127
1140
(is_match , _ ) = is_full_checksum_match (candidate_fileset , fileset , conn )
1128
1141
if is_match :
@@ -1579,7 +1592,7 @@ def populate_file(fileset, fileset_id, conn, detection):
1579
1592
1580
1593
for key , value in file .items ():
1581
1594
if key not in ["name" , "size" , "size-r" , "size-rd" , "sha1" , "crc" ]:
1582
- insert_filechecksum (file , key , conn )
1595
+ insert_filechecksum (file , key , file_id , conn )
1583
1596
if value in target_files_dict and not file_exists :
1584
1597
cursor .execute (
1585
1598
f"SELECT detection_type FROM file WHERE id = { target_files_dict [value ]['id' ]} "
@@ -1683,7 +1696,10 @@ def set_populate_file(fileset, fileset_id, conn, detection):
1683
1696
cursor .execute ("SET @file_last = LAST_INSERT_ID()" )
1684
1697
cursor .execute ("SELECT @file_last AS file_id" )
1685
1698
1686
- insert_filechecksum (file , "md5" , conn )
1699
+ cursor .execute ("SELECT @file_last AS file_id" )
1700
+ file_id = cursor .fetchone ()["file_id" ]
1701
+
1702
+ insert_filechecksum (file , "md5" , file_id , conn )
1687
1703
1688
1704
else :
1689
1705
query = """
@@ -1701,6 +1717,7 @@ def set_populate_file(fileset, fileset_id, conn, detection):
1701
1717
candidate_files [filename .lower ()][0 ],
1702
1718
),
1703
1719
)
1720
+
1704
1721
query = """
1705
1722
INSERT INTO filechecksum (file, checksize, checktype, checksum)
1706
1723
VALUES (%s, %s, %s, %s)
@@ -1714,6 +1731,14 @@ def set_populate_file(fileset, fileset_id, conn, detection):
1714
1731
checksum ,
1715
1732
),
1716
1733
)
1734
+
1735
+ add_all_equal_checksums (
1736
+ checksize ,
1737
+ checktype ,
1738
+ checksum ,
1739
+ candidate_files [filename .lower ()][0 ],
1740
+ conn ,
1741
+ )
1717
1742
seen_detection_files .add ((filename .lower (), file ["size" ]))
1718
1743
1719
1744
@@ -1745,9 +1770,13 @@ def insert_new_fileset(
1745
1770
if fileset_id :
1746
1771
for file in fileset ["rom" ]:
1747
1772
insert_file (file , detection , src , conn )
1773
+ file_id = None
1774
+ with conn .cursor () as cursor :
1775
+ cursor .execute ("SELECT @file_last AS file_id" )
1776
+ file_id = cursor .fetchone ()["file_id" ]
1748
1777
for key , value in file .items ():
1749
1778
if key not in ["name" , "size" , "size-r" , "size-rd" , "sha1" , "crc" ]:
1750
- insert_filechecksum (file , key , conn )
1779
+ insert_filechecksum (file , key , file_id , conn )
1751
1780
return (fileset_id , existing )
1752
1781
1753
1782
0 commit comments