Skip to content

Commit 9179628

Browse files
committed
StoreCleaner deletes files on exit
Fixes #2745
1 parent 4f75a78 commit 9179628

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

CHANGELOG.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,18 +174,21 @@ Version 4.x is JDK17 LTS bytecode compatible, with Docker and JUnit / direct Jav
174174
* Features and fixes
175175
* Return correct error body on invalid ranges (fixes #2732)
176176
* Accept unquoted etags in if-match/if-none-match headers (fixes #2665)
177+
* StoreCleaner deletes files on exit (fixes #2745)
177178
* Refactorings
178179
* Drop commons-lang3 dependency and replace its usages with core Java (fixes #2735)
179180
* Version updates (deliverable dependencies)
180181
* Bump spring-boot.version from 3.5.6 to 3.5.7
181182
* Bump aws-v2.version from 2.33.12 to 2.37.2
182183
* Bump aws.version from 1.12.791 to 1.12.793
183184
* Bump alpine from 3.22.1 to 3.22.2 in /docker
184-
* Bump org.apache.commons:commons-lang3 from 3.18.0 to 3.19.0
185+
* Bump commons-codec:commons-codec from 1.19.0 to 1.20.0
186+
* Bump commons-io:commons-io from 2.20.0 to 2.21.0
185187
* Version updates (build dependencies)
186188
* Bump kotlin.version from 2.2.20 to 2.2.21
187189
* Bump aws.sdk.kotlin:s3-jvm from 1.5.41 to 1.5.73
188-
* Bump digital.pragmatech.testing:spring-test-profiler from 0.0.12 to 0.0.13
190+
* Bump io.fabric8:docker-maven-plugin from 0.46.0 to 0.47.0
191+
* Bump digital.pragmatech.testing:spring-test-profiler from 0.0.12 to 0.0.14
189192
* Bump org.mockito.kotlin:mockito-kotlin from 6.0.0 to 6.1.0
190193
* Bump org.xmlunit:xmlunit-assertj3 from 2.10.4 to 2.11.0
191194
* Bump org.codehaus.mojo:exec-maven-plugin from 3.5.1 to 3.6.2
@@ -200,6 +203,8 @@ Version 4.x is JDK17 LTS bytecode compatible, with Docker and JUnit / direct Jav
200203
* Bump ossf/scorecard-action from 2.4.2 to 2.4.3
201204
* Bump actions/stale from 10.0.0 to 10.1.0
202205
* Bump actions/upload-artifact from 4.6.2 to 5.0.0
206+
* Bump step-security/harden-runner from 2.13.1 to 2.13.2
207+
* Bump docker/setup-qemu-action from 3.6.0 to 3.7.0
203208

204209
## 4.9.1
205210
Version 4.x is JDK17 LTS bytecode compatible, with Docker and JUnit / direct Java integration.

server/src/main/java/com/adobe/testing/s3mock/store/StoreCleaner.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@
1616

1717
package com.adobe.testing.s3mock.store;
1818

19-
import static org.apache.commons.io.FileUtils.cleanDirectory;
20-
2119
import java.io.File;
22-
import org.springframework.beans.factory.DisposableBean;
20+
import java.io.IOException;
21+
import java.nio.file.Files;
22+
import java.nio.file.Path;
23+
import java.util.Comparator;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
26+
import org.springframework.boot.CommandLineRunner;
2327

24-
public class StoreCleaner implements DisposableBean {
28+
public class StoreCleaner implements CommandLineRunner {
2529

30+
private static final Logger LOG = LoggerFactory.getLogger(StoreCleaner.class);
2631
private final File rootFolder;
2732
private final boolean retainFilesOnExit;
2833

@@ -32,9 +37,20 @@ public StoreCleaner(File rootFolder, boolean retainFilesOnExit) {
3237
}
3338

3439
@Override
35-
public void destroy() throws Exception {
36-
if (!retainFilesOnExit && rootFolder.exists()) {
37-
cleanDirectory(rootFolder);
38-
}
40+
public void run(String... args) {
41+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
42+
try {
43+
LOG.info("Calling StoreCleaner destroy() with retainFilesOnExit={}", retainFilesOnExit);
44+
if (!retainFilesOnExit && rootFolder.exists()) {
45+
Files.walk(rootFolder.toPath())
46+
.sorted(Comparator.reverseOrder())
47+
.map(Path::toFile)
48+
.forEach(File::delete);
49+
LOG.info("Directory {} cleaned up via shutdown hook.", rootFolder);
50+
}
51+
} catch (IOException e) {
52+
LOG.error("Error cleaning up directory {}", rootFolder, e);
53+
}
54+
}));
3955
}
4056
}

0 commit comments

Comments
 (0)