Skip to content

Commit

Permalink
Fix eclipse-tycho#829 - Support maven --strict-checksums option
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Läubrich <laeubi@laeubi-soft.de>
  • Loading branch information
laeubi committed Apr 2, 2022
1 parent 433104c commit 73cd19c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Contributors:
* SAP AG - initial API and implementation
* Christoph Läubrich - Bug 564363 - add access to reactor projects
* Issue #829 - Support maven --strict-checksums option
*******************************************************************************/
package org.eclipse.tycho.core.shared;

Expand All @@ -27,6 +28,10 @@
*/
public interface MavenContext {

enum ChecksumPolicy {
LAX, STRICT;
}

public File getLocalRepositoryRoot();

public MavenLogger getLogger();
Expand All @@ -41,6 +46,8 @@ public interface MavenContext {
*/
boolean isUpdateSnapshots();

ChecksumPolicy getChecksumsMode();

/**
* Session-global properties merged from (in order of precedence)
* <ol>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,9 @@ public Stream<MavenRepositoryLocation> getMavenRepositoryLocations() {
return Stream.empty();
}

@Override
public ChecksumPolicy getChecksumsMode() {
return ChecksumPolicy.LAX;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.eclipse.tycho.PackagingType;
import org.eclipse.tycho.TychoConstants;
import org.eclipse.tycho.core.shared.MavenContext;
import org.eclipse.tycho.core.shared.MavenContext.ChecksumPolicy;
import org.eclipse.tycho.core.shared.MavenLogger;
import org.eclipse.tycho.core.shared.MultiLineLogger;
import org.eclipse.tycho.repository.p2base.artifact.provider.IRawArtifactFileProvider;
Expand Down Expand Up @@ -233,7 +234,8 @@ protected boolean makeOneFormatLocallyAvailable(IArtifactKey key)
if (artifactFile != null && artifactFile.isFile()) {
//check if only properties has changed...
GAVArtifactDescriptor descriptor = newLocalDescriptor(key);
if (fileMatchesProperties(artifactFile, descriptor.getProperties())) {
if (fileMatchesProperties(artifactFile, descriptor.getProperties(),
mavenContext.getChecksumsMode() == ChecksumPolicy.STRICT)) {
localArtifactRepository.internalAddDescriptor(descriptor);
localArtifactRepository.save();
return true;
Expand All @@ -251,29 +253,47 @@ protected boolean makeOneFormatLocallyAvailable(IArtifactKey key)
}
}

private boolean fileMatchesProperties(File file, Map<String, String> properties) {
private boolean fileMatchesProperties(File file, Map<String, String> properties, boolean logFailure) {
String downloadSize = properties.get("download.size");
if (downloadSize != null && !downloadSize.equals(String.valueOf(file.length()))) {
//early break out...
if (logFailure) {
mavenContext.getLogger().warn("File size for " + file.getAbsolutePath()
+ " does not match, attempting to download file again...");
}
return false;
}
String sha256 = properties.get("download.checksum.sha-256");
if (sha256 != null) {
try (FileInputStream stream = new FileInputStream(file)) {
String fileSha256 = DigestUtils.sha256Hex(stream);
return fileSha256.equalsIgnoreCase(sha256);
if (fileSha256.equalsIgnoreCase(sha256)) {
return true;
}
} catch (IOException e) {
return false;
mavenContext.getLogger().debug("Computing hash sum failed, assume file is corrupted (" + e + ")");
}
if (logFailure) {
mavenContext.getLogger().warn("sha-256 checksum for " + file.getAbsolutePath()
+ " does not match, attempting to download file again...");
}
return false;
}
String md5 = properties.get("download.checksum.md5");
if (md5 != null) {
try (FileInputStream stream = new FileInputStream(file)) {
String fileMd5 = DigestUtils.md5Hex(stream);
return fileMd5.equalsIgnoreCase(md5);
if (fileMd5.equalsIgnoreCase(md5)) {
return true;
}
} catch (IOException e) {
return false;
mavenContext.getLogger().debug("Computing hash sum failed, assume file is corrupted (" + e + ")");
}
if (logFailure) {
mavenContext.getLogger().warn("md5 checksum for " + file.getAbsolutePath()
+ " does not match, attempting to download file again...");
}
return false;
}
return false;
}
Expand Down Expand Up @@ -375,6 +395,15 @@ public boolean isFileAlreadyAvailable(IArtifactKey artifactKey) {
IArtifactDescriptor[] localDescriptors = localArtifactRepository.getArtifactDescriptors(artifactKey);
GAVArtifactDescriptor localDescriptor = (GAVArtifactDescriptor) findCanonicalDescriptor(
localDescriptors);
if (mavenContext.getChecksumsMode() == ChecksumPolicy.STRICT && remoteDescriptor != null
&& localDescriptor != null) {
File artifactFile = localArtifactRepository.getArtifactFile(localDescriptor);
if (artifactFile != null && artifactFile.isFile()) {
if (!fileMatchesProperties(artifactFile, remoteDescriptor.getProperties(), false)) {
return false;
}
}
}
if (remoteDescriptor != null && localDescriptor != null && localDescriptor.getMavenCoordinates()
.getGroupId().startsWith(TychoConstants.P2_GROUPID_PREFIX)) {
Map<String, String> remoteProperties = getProperties(remoteDescriptor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* SAP AG - initial API and implementation
* Christoph Läubrich - Bug 564363 - Make ReactorProject available in MavenContext
* Issue #797 - Implement a caching P2 transport
* Issue #829 - Support maven --strict-checksums option
*******************************************************************************/
package org.eclipse.tycho.osgi.configuration;

Expand All @@ -26,6 +27,7 @@

import org.apache.maven.artifact.handler.ArtifactHandler;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.LegacySupport;
import org.apache.maven.project.MavenProject;
Expand All @@ -39,6 +41,7 @@
import org.eclipse.tycho.core.osgitools.DefaultReactorProject;
import org.eclipse.tycho.core.resolver.shared.MavenRepositoryLocation;
import org.eclipse.tycho.core.shared.MavenContext;
import org.eclipse.tycho.core.shared.MavenContext.ChecksumPolicy;
import org.eclipse.tycho.core.shared.MavenContextImpl;
import org.eclipse.tycho.osgi.adapters.MavenLoggerAdapter;
import org.eclipse.tycho.p2maven.repository.P2ArtifactRepositoryLayout;
Expand Down Expand Up @@ -70,6 +73,12 @@ public void afterFrameworkStarted(EmbeddedEquinox framework) {
return null;
}
}).filter(Objects::nonNull).collect(Collectors.toUnmodifiableList());
ChecksumPolicy checksumPolicy;
if (MavenExecutionRequest.CHECKSUM_POLICY_FAIL.equals(session.getRequest().getGlobalChecksumPolicy())) {
checksumPolicy = ChecksumPolicy.STRICT;
} else {
checksumPolicy = ChecksumPolicy.LAX;
}
MavenContextImpl mavenContext = new MavenContextImpl(localRepoRoot, session.isOffline(), mavenLogger,
globalProps) {

Expand All @@ -93,6 +102,11 @@ public Stream<MavenRepositoryLocation> getMavenRepositoryLocations() {
return repositoryLocations.stream();
}

@Override
public ChecksumPolicy getChecksumsMode() {
return checksumPolicy;
}

};
for (MavenProject project : session.getProjects()) {
mavenContext.addProject(DefaultReactorProject.adapt(project));
Expand Down

0 comments on commit 73cd19c

Please sign in to comment.