@@ -53,13 +53,16 @@ public class ScanServiceImpl extends BaseService<ScanRepository> implements Scan
5353 private final FileHashComputer fileHashComputer ;
5454
5555 private final org .slf4j .Logger log ;
56-
5756 private final DateTimeFormatter timeAndDateStringForLogFormat ;
57+
58+ // Map to hold active scan tasks, keyed by directory path
5859 private final ConcurrentMap <String , ScanTaskState > activeScanTasks ;
60+
5961 // Flag to indicate if an ongoing scan should be stopped
6062 private boolean stopRequested = false ;
6163
62- private final int SCAN_TASK_MONITOR_DELAY = 5000 ; // Delay in milliseconds for monitoring scan tasks
64+ // Delay in milliseconds for monitoring scan tasks
65+ private final int SCAN_TASK_MONITOR_DELAY = 5000 ;
6366
6467 @ Autowired
6568 public ScanServiceImpl (ScanRepository repository ,
@@ -282,22 +285,14 @@ private void processFile(File file, Scan scanInstance) {
282285
283286 MonitoredDirectory mDirectory = scanInstance .getMonitoredDirectory ();
284287
285- if (monitoredDirectoryService .isBaseLineEstablished (mDirectory )) {
286-
287- // Compare current hash to previous hash (Ticket #46)
288-
289- } else {
290- // Scan without comparing hashes for this Monitored Directory
291- }
292-
293288 if (fileInDatabase ) {
294289 // Fetch existing entity and update fields
295290 fileEntity = fileService .findByPath (file .getPath ());
296291 fileEntity .setSize (file .length ());
297292 OffsetDateTime lastModified = Instant .ofEpochMilli (file .lastModified ())
298293 .atOffset (ZoneOffset .UTC );
299294 fileEntity .setMtime (lastModified );
300- log .info ("Updating existing file in DB: {}" , fileEntity .getPath ());
295+ log .debug ("Updating existing file in DB: {}" , fileEntity .getPath ());
301296 } else {
302297 // Create new entity
303298 fileEntity = new org .pwss .file_integrity_scanner .msr .domain .model .entities .file .File ();
@@ -308,22 +303,39 @@ private void processFile(File file, Scan scanInstance) {
308303 OffsetDateTime lastModified = Instant .ofEpochMilli (file .lastModified ())
309304 .atOffset (ZoneOffset .UTC );
310305 fileEntity .setMtime (lastModified );
311- log .info ("Adding new file to DB: {}" , fileEntity .getPath ());
306+ log .debug ("Adding new file to DB: {}" , fileEntity .getPath ());
312307 }
313308
314309 fileService .save (fileEntity );
315310
316- Checksum checksums = new Checksum ();
317- checksums .setChecksumSha256 (computedHashes .sha256 ());
318- checksums .setChecksumSha3 (computedHashes .sha3 ());
319- checksums .setChecksumBlake2b (computedHashes .blake2 ());
320- checksums .setFile (fileEntity );
321- checksumService .save (checksums );
311+ Checksum checksum = new Checksum ();
312+ checksum .setChecksumSha256 (computedHashes .sha256 ());
313+ checksum .setChecksumSha3 (computedHashes .sha3 ());
314+ checksum .setChecksumBlake2b (computedHashes .blake2 ());
315+ checksum .setFile (fileEntity );
316+ checksumService .save (checksum );
317+
318+ // If the baseline is established, check if the file has changed
319+ if (monitoredDirectoryService .isBaseLineEstablished (mDirectory )) {
320+ List <Checksum > dbChecksums = checksumService .findByFile (fileEntity );
321+ if (!dbChecksums .isEmpty ()) {
322+ // TODO: Maybe not getFirst but let's discuss this later
323+ Checksum oldChecksum = dbChecksums .getFirst ();
324+ if (fileHashComputer .compareHashes (oldChecksum , checksum )) {
325+ log .info ("File {} has not changed since last scan ✅" , fileEntity .getPath ());
326+ } else {
327+ log .warn ("File {} has changed since last scan ⚠️" , fileEntity .getPath ());
328+ // TODO: Figure out what to do with changed files, add some notes to scan summary?
329+ }
330+ } else {
331+ log .info ("No existing checksum found for file from prior scans {}" , fileEntity .getPath ());
332+ }
333+ }
322334
323335 ScanSummary scanSummary = new ScanSummary ();
324336 scanSummary .setFile (fileEntity );
325337 scanSummary .setScan (scanInstance );
326- scanSummary .setChecksum (checksums );
338+ scanSummary .setChecksum (checksum );
327339 scanSummaryService .save (scanSummary );
328340 } catch (OutOfMemoryError memoryError ) {
329341 log .warn ("Out of memory error while processing file: {}. Skipping file." , file .getPath ());
0 commit comments