Skip to content

Commit e6f6f8f

Browse files
committed
fixes the duplicate key bug when finding the same fil multiple times
1 parent e6188d6 commit e6f6f8f

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

Infrastructure/Database/FindingsQuery.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public function create(): void {
2929
detection VARCHAR(128) NOT NULL,
3030
sha256 VARCHAR(64) NOT NULL,
3131
request_id VARCHAR(256) NOT NULL,
32+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
33+
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
3234
UNIQUE KEY file_path (file_path)
3335
)' . $charset_collate . ';';
3436

@@ -68,14 +70,38 @@ public function table_exists(): bool {
6870
return false;
6971
}
7072

73+
private function exits( string $file_path ): bool {
74+
global $wpdb;
75+
76+
if (!$this->table_exists()) {
77+
return false;
78+
}
79+
return $wpdb->get_var(
80+
$wpdb->prepare('SELECT COUNT(*) FROM %i WHERE file_path = %s', $this->get_table_name(), $file_path)
81+
) > 0;
82+
}
83+
7184
public function add( DetectedFile $detected_file ): void {
7285
global $wpdb;
86+
assert($wpdb instanceof wpdb);
7387

7488
if (! $this->table_exists()) {
7589
return;
7690
}
7791

7892
try {
93+
if ($this->exits($detected_file->path)) {
94+
$wpdb->update(
95+
$this->get_table_name(),
96+
array(
97+
'detection' => $detected_file->detection,
98+
'sha256' => $detected_file->sha256,
99+
'request_id' => $detected_file->request_id
100+
),
101+
array( 'file_path' => $detected_file->path )
102+
);
103+
return;
104+
}
79105
$wpdb->insert(
80106
$this->get_table_name(),
81107
array(
@@ -121,7 +147,7 @@ public function get_all(): array {
121147
return array();
122148
}
123149
return $wpdb->get_results(
124-
$wpdb->prepare('SELECT file_path, detection, sha256, request_id FROM %i', $this->get_table_name()),
150+
$wpdb->prepare('SELECT file_path, detection, sha256, request_id, updated_at FROM %i', $this->get_table_name()),
125151
ARRAY_A
126152
);
127153
}

PluginPage/Findings/FindingsMenuPage.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ public function findings_list(): void {
135135
<th scope="col" id="title_file" class="manage-column column-title column-primary">
136136
File
137137
</th>
138+
<th scope="col" id="title_file" class="manage-column column-title column-primary">
139+
Last seen
140+
</th>
138141
<th scope="col" id="title_detection" class="manage-column column-title column-primary">
139142
Detection
140143
</th>
@@ -168,6 +171,11 @@ public function findings_list(): void {
168171
echo esc_html($finding['file_path']);
169172
?>
170173
</td>
174+
<td>
175+
<?php
176+
echo esc_html($finding['updated_at']);
177+
?>
178+
</td>
171179
<td>
172180
<?php
173181
echo esc_html($finding['detection']);

PluginPage/FullScan/FullScanMenuPage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ public function full_scan(): void {
246246
if ($file_path->isDir()) {
247247
continue;
248248
}
249-
// For testing purposes, we only scan files with eicar in the name
249+
// // For testing purposes, we only scan files with eicar in the name
250250
// if (str_contains($file_path->getPathname(), "eicar") === false) {
251251
// continue;
252252
// }

Readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ While the released code is hosted on the WordPress svn, we develop the plugin on
5858

5959
= 2.1.0 =
6060
* bugfix: [full scan runs in loop](https://github.com/GDATASoftwareAG/wordpress-gdata-antivirus/issues/37)
61+
* bugifx: fails on duplicate key when detecting the same file twice
6162

6263
= 2.0.9 =
6364
* bugfix: reconnect on long running scans

0 commit comments

Comments
 (0)