Skip to content

Commit

Permalink
maven artifacts resolution: mark bad jars corrupted but don't delete …
Browse files Browse the repository at this point in the history
…them

Bad JARs are moved to file-name.jar.corruptedXXXX instead of deletion which allows
to inspect bad ZIP contents if needed.

Should be revised when IDEA-269182 is enabled.

GitOrigin-RevId: c31746e6f09a670ebcfac5ff7f329f2df233ae19
  • Loading branch information
shchuko authored and intellij-monorepo-bot committed Jan 30, 2023
1 parent e98c328 commit 02dac8c
Showing 1 changed file with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.zip.ZipFile;

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

class StrictLocalRepositoryManager implements LocalRepositoryManager {
private static final Logger LOG = LoggerFactory.getLogger(StrictLocalRepositoryManager.class);
private static final boolean STRICT_VALIDATION = "true".equals(System.getProperty("org.jetbrains.idea.maven.aether.strictValidation"));
Expand All @@ -32,6 +35,12 @@ public LocalArtifactResult find(RepositorySystemSession session, LocalArtifactRe
return result;
}

/**
* Checks whether {@code archive} file is valid ZIP. If ZIP is invalid, renames the file adding {@code .corruptedXXXXX} suffix.
*
* @param archive ZIP archive file
* @return true if valid, false if not
*/
boolean isValidArchive(File archive) {
if (!archive.exists()) return false;
// TODO: to be revised after IDEA-269182 is implemented
Expand All @@ -41,16 +50,25 @@ boolean isValidArchive(File archive) {
entriesCount = zip.size();
}
catch (IOException e) {
LOG.warn("Unable to read a number of entries in " + archive, e);
/* Short exception message is enough */
LOG.warn("Unable to read a number of entries in " + archive + ": " + e.getMessage());
entriesCount = 0;
}
if (entriesCount <= 0) {
LOG.warn(archive + " is probably corrupted, deleting");
LOG.warn(archive + " is probably corrupted, marking as corrupted");
try {
Files.deleteIfExists(archive.toPath());
Path archiveAsPath = archive.toPath();
if (Files.exists(archiveAsPath)) {
String prefix = archiveAsPath.getFileName() + ".corrupted";
String suffix = "";
Path corruptedArchivePath = Files.createTempFile(archiveAsPath.getParent(), prefix, suffix);
Files.move(archiveAsPath, corruptedArchivePath, REPLACE_EXISTING);

LOG.warn(archive + " is moved to " + corruptedArchivePath);
}
}
catch (IOException e) {
throw new RuntimeException("Unable to delete " + archive, e);
throw new RuntimeException("Unable to move " + archive, e);
}
return false;
}
Expand Down

0 comments on commit 02dac8c

Please sign in to comment.