Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature-49: TmpFile Clean-up #50

Merged
merged 5 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,12 @@
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<version>3.1.2</version>
<!-- Adjust forkCount to speed up tests if desired -->
<!-- <configuration>
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
</configuration> -->
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1025,9 +1025,26 @@ protected ObjectInfo putObject(
// Generate tmp file and write to it
logFileHashStore.debug("FileHashStore.putObject - Generating tmpFile");
File tmpFile = generateTmpFile("tmp", OBJECT_TMP_FILE_DIRECTORY);
Map<String, String> hexDigests = writeToTmpFileAndGenerateChecksums(
tmpFile, object, additionalAlgorithm, checksumAlgorithm
);
Map<String, String> hexDigests;
try {
hexDigests = writeToTmpFileAndGenerateChecksums(
tmpFile, object, additionalAlgorithm, checksumAlgorithm
);
} catch (Exception ge) {
// If the process to write to the tmpFile is interrupted for any reason,
// we will delete the tmpFile.
boolean deleteStatus = tmpFile.delete();
String errMsg =
"FileHashStore.putObject - Unexpected Exception while storing object for: " + pid;
if (deleteStatus) {
errMsg = errMsg + ". Deleting temp file: " + tmpFile + ". Aborting request.";
} else {
errMsg = errMsg + ". Failed to delete temp file: " + tmpFile
+ ". Aborting request.";
}
logFileHashStore.error(errMsg);
throw new IOException(errMsg);
}
long storedObjFileSize = Files.size(Paths.get(tmpFile.toString()));

// Validate object if checksum and checksum algorithm is passed
Expand Down Expand Up @@ -1277,6 +1294,7 @@ protected File generateTmpFile(String prefix, Path directory) throws IOException
logFileHashStore.trace(
"FileHashStore.generateTmpFile - tmpFile generated: " + newFile.getAbsolutePath()
);
newFile.deleteOnExit();
return newFile;

} catch (IOException ioe) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public static void checkForEmptyString(String string, String argument, String me
/**
* Checks whether a given long integer is greater than 0
*
* @param object Object to check
* @param longInt Object to check
* @param method Calling method
* @throws IllegalArgumentException If longInt is less than 0
*/
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/dataone/hashstore/HashStoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void isHashStore() {
* Check that getHashStore throws exception when classPackage is null
*/
@Test
public void hashStore_classPackageNull() throws Exception {
public void hashStore_classPackageNull() {
assertThrows(HashStoreFactoryException.class, () -> {
Properties storeProperties = new Properties();
storeProperties.setProperty("storePath", "/test");
Expand All @@ -88,7 +88,7 @@ public void hashStore_classPackageNull() throws Exception {
* Check that getHashStore throws exception when classPackage is not found
*/
@Test
public void hashStore_classPackageNotFound() throws Exception {
public void hashStore_classPackageNotFound() {
assertThrows(HashStoreFactoryException.class, () -> {
String classPackage = "org.dataone.hashstore.filehashstore.AnotherHashStore";

Expand All @@ -110,7 +110,7 @@ public void hashStore_classPackageNotFound() throws Exception {
* Check that getHashStore throws exception when storeProperties is null
*/
@Test
public void hashStore_nullStoreProperties() throws Exception {
public void hashStore_nullStoreProperties() {
assertThrows(HashStoreFactoryException.class, () -> {
String classPackage = "org.dataone.hashstore.filehashstore.FileHashStore";
hashStore = HashStoreFactory.getHashStore(classPackage, null);
Expand Down
Loading