From 18cc9e0e68cecdda3e249769bfbaa1c757167cfb Mon Sep 17 00:00:00 2001 From: LSmyrnaios Date: Wed, 4 Oct 2023 16:08:38 +0300 Subject: [PATCH] - Improve error-handling in file-compression. - Update dependencies. --- build.gradle | 8 ++++---- .../eu/openaire/urls_worker/util/FilesCompressor.java | 11 ++++++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/build.gradle b/build.gradle index 4f4cde8..edcb1a3 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ plugins { - id 'org.springframework.boot' version '2.7.15' + id 'org.springframework.boot' version '2.7.16' id 'io.spring.dependency-management' version '1.1.3' id 'java' } @@ -44,11 +44,11 @@ dependencies { // https://mvnrepository.com/artifact/com.google.code.gson/gson implementation 'com.google.code.gson:gson:2.10.1' - implementation("org.apache.commons:commons-compress:1.23.0") { + implementation("org.apache.commons:commons-compress:1.24.0") { exclude group: 'com.github.luben', module: 'zstd-jni' } - implementation 'com.github.luben:zstd-jni:1.5.5-5' // Even though this is part of the above dependency, the Apache commons rarely updates it, while the zstd team makes improvements very often. - // Also, for compressing, we strangely need it, otherwise it does not work. + implementation 'com.github.luben:zstd-jni:1.5.5-6' // Even though this is part of the above dependency, the Apache commons rarely updates it, while the zstd team makes improvements very often. + // Also, for compressing, we strangely need it to be explicitly declared independently, otherwise it does not work. testImplementation 'org.springframework.security:spring-security-test' testImplementation "org.springframework.boot:spring-boot-starter-test" diff --git a/src/main/java/eu/openaire/urls_worker/util/FilesCompressor.java b/src/main/java/eu/openaire/urls_worker/util/FilesCompressor.java index 4a32e40..edd57b1 100644 --- a/src/main/java/eu/openaire/urls_worker/util/FilesCompressor.java +++ b/src/main/java/eu/openaire/urls_worker/util/FilesCompressor.java @@ -12,6 +12,7 @@ import java.io.BufferedOutputStream; import java.io.File; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.NoSuchFileException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; @@ -85,7 +86,9 @@ public class FilesCompressor { } } - if ( numTarredFiles != filesToTar.size() ) + if ( numTarredFiles == 0 ) { + throw new RuntimeException("None of the requested (" + filesToTar.size() + ") could be tarred, for assignments_" + assignmentsCounter + ", batch_" + tarBatchCounter); + } else if ( numTarredFiles != filesToTar.size() ) logger.warn("The number of \"numTarredFiles\" (" + numTarredFiles + ") is different from the number of files requested to be tarred (" + filesToTar.size() + "), for assignments_" + assignmentsCounter + ", batch_" + tarBatchCounter); // Still, some files may have been tarred, so we move on. It's up to the Controller, to handle such case. @@ -98,8 +101,7 @@ public class FilesCompressor { boolean shouldCloseEntry = false; // Useful in order to know if we should close the entry (an Exception may appear when initializing the stream, and so we should not try to close it). Path fullFileNamePath = Paths.get(baseDir + fileName); - try ( BufferedInputStream fis = new BufferedInputStream(Files.newInputStream(fullFileNamePath), bufferSize) ) - { + try ( BufferedInputStream fis = new BufferedInputStream(Files.newInputStream(fullFileNamePath), bufferSize) ) { TarArchiveEntry entry = new TarArchiveEntry(fileName); entry.setSize(Files.size(fullFileNamePath)); // Yes, tar requires that we set the size beforehand.. taos.putArchiveEntry(entry); @@ -109,6 +111,9 @@ public class FilesCompressor { while ( (readByte = fis.read()) != -1 ) { taos.write(readByte); } + } catch (NoSuchFileException nsfe) { + logger.error("NoSuchFileException: " + nsfe.getMessage()); + return false; } catch (Exception e) { logger.error("", e); return false;