Skip to content

Commit 30b193e

Browse files
committed
local file checksumming
1 parent 5f753ba commit 30b193e

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

src/main/java/com/falsepattern/lib/internal/impl/dependencies/DependencyLoaderImpl.java

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,19 @@ private boolean tryLoadingExistingFile() {
275275
if (!file.exists()) {
276276
return false;
277277
}
278+
try {
279+
val status = validateChecksum(file);
280+
if (status == ChecksumStatus.FAILED) {
281+
return false;
282+
} else if (status == ChecksumStatus.MISSING) {
283+
log.debug("Library {} is missing checksum data! Either it was manually deleted, " +
284+
"or the source repo didn't have it in the first place", artifactLogName);
285+
}
286+
} catch (IOException e) {
287+
log.error("Failed to execute validation check for " + artifactLogName, e);
288+
checkedDelete(file);
289+
return false;
290+
}
278291
try {
279292
addToClasspath(file);
280293
loadedLibraries.put(artifact, preferredVersion);
@@ -360,22 +373,36 @@ private ChecksumStatus validateChecksum(String url) throws IOException {
360373
success.set(true);
361374
});
362375
if (success.get()) {
363-
val fileHash = hash(checksumType, file);
364-
val referenceHash = new String(Files.readAllBytes(checksumFile.toPath()));
365-
if (!fileHash.equals(referenceHash)) {
366-
log.error("Failed {} checksum validation for {}. Retrying download...", checksumType,
367-
artifactLogName);
368-
checkedDelete(file);
369-
checkedDelete(checksumFile);
370-
return ChecksumStatus.FAILED;
371-
}
372-
log.debug("Successfully validated {} checksum for {}", checksumType, artifactLogName);
373-
return ChecksumStatus.OK;
376+
return getChecksumStatus(file, checksumType, checksumFile);
374377
}
375378
}
376379
return ChecksumStatus.MISSING;
377380
}
378381

382+
private ChecksumStatus validateChecksum(File file) throws IOException {
383+
for (val checksumType : CHECKSUM_TYPES) {
384+
val checksumFile = new File(libDir, jarName + "." + checksumType);
385+
log.debug("Attempting to read {} checksum from file...", checksumType);
386+
if (checksumFile.exists()) {
387+
return getChecksumStatus(file, checksumType, checksumFile);
388+
}
389+
}
390+
return ChecksumStatus.MISSING;
391+
}
392+
393+
private ChecksumStatus getChecksumStatus(File file, String checksumType, File checksumFile) throws IOException {
394+
val fileHash = hash(checksumType, file);
395+
val referenceHash = new String(Files.readAllBytes(checksumFile.toPath()));
396+
if (!fileHash.equals(referenceHash)) {
397+
log.error("Failed {} checksum validation for {}.", checksumType, artifactLogName);
398+
checkedDelete(file);
399+
checkedDelete(checksumFile);
400+
return ChecksumStatus.FAILED;
401+
}
402+
log.debug("Successfully validated {} checksum for {}.", checksumType, artifactLogName);
403+
return ChecksumStatus.OK;
404+
}
405+
379406
private enum ChecksumStatus {
380407
OK,
381408
FAILED,

0 commit comments

Comments
 (0)