@@ -280,66 +280,62 @@ private void processFile(File file, Scan scanInstance) {
280280 org .pwss .file_integrity_scanner .msr .domain .model .entities .file .File fileEntity ;
281281 boolean fileInDatabase = fileService .existsByPath (file .getPath ());
282282
283- try {
284- HashForFilesOutput computedHashes = fileHashComputer .computeHashes (file );
285-
286- MonitoredDirectory mDirectory = scanInstance .getMonitoredDirectory ();
287-
288- if (fileInDatabase ) {
289- // Fetch existing entity and update fields
290- fileEntity = fileService .findByPath (file .getPath ());
291- fileEntity .setSize (file .length ());
292- OffsetDateTime lastModified = Instant .ofEpochMilli (file .lastModified ())
293- .atOffset (ZoneOffset .UTC );
294- fileEntity .setMtime (lastModified );
295- log .debug ("Updating existing file in DB: {}" , fileEntity .getPath ());
296- } else {
297- // Create new entity
298- fileEntity = new org .pwss .file_integrity_scanner .msr .domain .model .entities .file .File ();
299- fileEntity .setPath (file .getPath ());
300- fileEntity .setBasename (file .getName ());
301- fileEntity .setDirectory (file .getParent ());
302- fileEntity .setSize (file .length ());
303- OffsetDateTime lastModified = Instant .ofEpochMilli (file .lastModified ())
304- .atOffset (ZoneOffset .UTC );
305- fileEntity .setMtime (lastModified );
306- log .debug ("Adding new file to DB: {}" , fileEntity .getPath ());
307- }
283+ HashForFilesOutput computedHashes = fileHashComputer .computeHashes (file );
284+
285+ MonitoredDirectory mDirectory = scanInstance .getMonitoredDirectory ();
286+
287+ if (fileInDatabase ) {
288+ // Fetch existing entity and update fields
289+ fileEntity = fileService .findByPath (file .getPath ());
290+ fileEntity .setSize (file .length ());
291+ OffsetDateTime lastModified = Instant .ofEpochMilli (file .lastModified ())
292+ .atOffset (ZoneOffset .UTC );
293+ fileEntity .setMtime (lastModified );
294+ log .debug ("Updating existing file in DB: {}" , fileEntity .getPath ());
295+ } else {
296+ // Create new entity
297+ fileEntity = new org .pwss .file_integrity_scanner .msr .domain .model .entities .file .File ();
298+ fileEntity .setPath (file .getPath ());
299+ fileEntity .setBasename (file .getName ());
300+ fileEntity .setDirectory (file .getParent ());
301+ fileEntity .setSize (file .length ());
302+ OffsetDateTime lastModified = Instant .ofEpochMilli (file .lastModified ())
303+ .atOffset (ZoneOffset .UTC );
304+ fileEntity .setMtime (lastModified );
305+ log .debug ("Adding new file to DB: {}" , fileEntity .getPath ());
306+ }
308307
309- fileService .save (fileEntity );
310-
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- }
308+ fileService .save (fileEntity );
309+
310+ Checksum checksum = new Checksum ();
311+ checksum .setChecksumSha256 (computedHashes .sha256 ());
312+ checksum .setChecksumSha3 (computedHashes .sha3 ());
313+ checksum .setChecksumBlake2b (computedHashes .blake2 ());
314+ checksum .setFile (fileEntity );
315+ checksumService .save (checksum );
316+
317+ // If the baseline is established, check if the file has changed
318+ if (monitoredDirectoryService .isBaseLineEstablished (mDirectory )) {
319+ List <Checksum > dbChecksums = checksumService .findByFile (fileEntity );
320+ if (!dbChecksums .isEmpty ()) {
321+ // TODO: Maybe not getFirst but let's discuss this later
322+ Checksum oldChecksum = dbChecksums .getFirst ();
323+ if (fileHashComputer .compareHashes (oldChecksum , checksum )) {
324+ log .info ("File {} has not changed since last scan ✅" , fileEntity .getPath ());
330325 } else {
331- log .info ("No existing checksum found for file from prior scans {}" , fileEntity .getPath ());
326+ log .warn ("File {} has changed since last scan ⚠️" , fileEntity .getPath ());
327+ // TODO: Figure out what to do with changed files, add some notes to scan summary?
332328 }
329+ } else {
330+ log .info ("No existing checksum found for file from prior scans {}" , fileEntity .getPath ());
333331 }
334-
335- ScanSummary scanSummary = new ScanSummary ();
336- scanSummary .setFile (fileEntity );
337- scanSummary .setScan (scanInstance );
338- scanSummary .setChecksum (checksum );
339- scanSummaryService .save (scanSummary );
340- } catch (OutOfMemoryError memoryError ) {
341- log .warn ("Out of memory error while processing file: {}. Skipping file." , file .getPath ());
342332 }
333+
334+ ScanSummary scanSummary = new ScanSummary ();
335+ scanSummary .setFile (fileEntity );
336+ scanSummary .setScan (scanInstance );
337+ scanSummary .setChecksum (checksum );
338+ scanSummaryService .save (scanSummary );
343339 }
344340
345341 @ Override
0 commit comments