Skip to content

Commit 39ec16d

Browse files
committed
Delete cached archive in case of error
1 parent c7d6f63 commit 39ec16d

File tree

1 file changed

+27
-14
lines changed
  • code/tool-support/tool-support-commons/src/main/java/io/projectenv/core/toolsupport/commons/commands

1 file changed

+27
-14
lines changed

code/tool-support/tool-support-commons/src/main/java/io/projectenv/core/toolsupport/commons/commands/ExtractArchiveStep.java

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,10 @@ public ExtractArchiveStep(String rawArchiveUri) {
2929

3030
@Override
3131
public LocalToolInstallationDetails executeInstallStep(File installationRoot, LocalToolInstallationDetails intermediateInstallationDetails) throws LocalToolInstallationStepException {
32-
try {
33-
Path localArchivePath = downloadArchive();
34-
extractArchive(localArchivePath, installationRoot);
32+
Path localArchivePath = downloadArchive();
33+
extractArchive(localArchivePath, installationRoot);
3534

36-
return intermediateInstallationDetails;
37-
} catch (IOException e) {
38-
throw new LocalToolInstallationStepException("failed to extract archive from URI " + rawArchiveUri, e);
39-
}
35+
return intermediateInstallationDetails;
4036
}
4137

4238
@Override
@@ -49,7 +45,7 @@ public void updateChecksum(MessageDigest digest) {
4945
digest.update(rawArchiveUri.getBytes(StandardCharsets.UTF_8));
5046
}
5147

52-
private Path downloadArchive() throws IOException {
48+
private Path downloadArchive() throws LocalToolInstallationStepException {
5349
Path archiveCachePath = getArchiveCachePath();
5450
if (Files.exists(archiveCachePath)) {
5551
ProcessOutput.writeDebugMessage("using cached archive from {0}", archiveCachePath);
@@ -65,13 +61,17 @@ private Path downloadArchive() throws IOException {
6561
ProcessOutput.writeDebugMessage("cached archive at {0}", archiveCachePath);
6662
return archiveCachePath;
6763
} catch (IOException e) {
68-
Files.deleteIfExists(archiveCachePath);
69-
throw e;
64+
deleteIfExists(archiveCachePath);
65+
throw new LocalToolInstallationStepException("failed to download archive from URI " + rawArchiveUri, e);
7066
}
7167
}
7268

73-
private Path getArchiveCachePath() throws IOException {
74-
return getCacheDirectory().resolve(FilenameUtils.getName(rawArchiveUri));
69+
private Path getArchiveCachePath() throws LocalToolInstallationStepException {
70+
try {
71+
return getCacheDirectory().resolve(FilenameUtils.getName(rawArchiveUri));
72+
} catch (IOException e) {
73+
throw new LocalToolInstallationStepException("failed to resolve archive cache path for URI " + rawArchiveUri, e);
74+
}
7575
}
7676

7777
private Path getCacheDirectory() throws IOException {
@@ -82,8 +82,21 @@ private Path getCacheDirectory() throws IOException {
8282
});
8383
}
8484

85-
private void extractArchive(Path localArchivePath, File installationRoot) throws IOException {
86-
ArchiveExtractorFactory.createArchiveExtractor().extractArchive(localArchivePath.toFile(), installationRoot);
85+
private void extractArchive(Path localArchivePath, File installationRoot) throws LocalToolInstallationStepException {
86+
try {
87+
ArchiveExtractorFactory.createArchiveExtractor().extractArchive(localArchivePath.toFile(), installationRoot);
88+
} catch (IOException e) {
89+
deleteIfExists(localArchivePath);
90+
throw new LocalToolInstallationStepException("failed to extract archive from URI " + rawArchiveUri, e);
91+
}
92+
}
93+
94+
private void deleteIfExists(Path path) {
95+
try {
96+
Files.deleteIfExists(path);
97+
} catch (IOException ignored) {
98+
99+
}
87100
}
88101

89102
}

0 commit comments

Comments
 (0)