From a10b993845dc1883ce91bad0b945db96acf75030 Mon Sep 17 00:00:00 2001 From: Pascal Rapicault Date: Tue, 21 Oct 2008 00:22:45 +0000 Subject: Bug 222870 - Enabled MD5 processing step --- .../META-INF/MANIFEST.MF | 5 +- .../plugin.xml | 5 -- .../artifact/processors/verifier/MD5Verifier.java | 83 ---------------------- .../p2/artifact/processors/md5/messages.properties | 3 + .../simple/SimpleArtifactRepository.java | 4 +- .../equinox/spi/p2/publisher/PublisherHelper.java | 43 +++++++++++ .../META-INF/MANIFEST.MF | 3 +- 7 files changed, 51 insertions(+), 95 deletions(-) delete mode 100644 bundles/org.eclipse.equinox.p2.artifact.processors/src/org/eclipse/equinox/internal/p2/artifact/processors/verifier/MD5Verifier.java create mode 100644 bundles/org.eclipse.equinox.p2.artifact.repository/src/org/eclipse/equinox/internal/p2/artifact/processors/md5/messages.properties diff --git a/bundles/org.eclipse.equinox.p2.artifact.processors/META-INF/MANIFEST.MF b/bundles/org.eclipse.equinox.p2.artifact.processors/META-INF/MANIFEST.MF index b07bc2d51..d93837da5 100644 --- a/bundles/org.eclipse.equinox.p2.artifact.processors/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.equinox.p2.artifact.processors/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %pluginName Bundle-SymbolicName: org.eclipse.equinox.p2.artifact.processors;singleton:=true -Bundle-Version: 1.0.0.qualifier +Bundle-Version: 1.0.100.qualifier Bundle-Localization: plugin Bundle-RequiredExecutionEnvironment: CDC-1.1/Foundation-1.1, J2SE-1.4 @@ -25,7 +25,6 @@ Import-Package: ie.wombat.jbdiff, Bundle-Vendor: %providerName Export-Package: org.eclipse.equinox.internal.p2.artifact.processors;x-internal:=true, org.eclipse.equinox.internal.p2.artifact.processors.jardelta;x-internal:=true, - org.eclipse.equinox.internal.p2.artifact.processors.jbdiff;x-internal:=true, - org.eclipse.equinox.internal.p2.artifact.processors.verifier;x-internal:=true + org.eclipse.equinox.internal.p2.artifact.processors.jbdiff;x-internal:=true Bundle-Activator: org.eclipse.equinox.internal.p2.artifact.processors.Activator Eclipse-LazyStart: true diff --git a/bundles/org.eclipse.equinox.p2.artifact.processors/plugin.xml b/bundles/org.eclipse.equinox.p2.artifact.processors/plugin.xml index 94a700302..2b083618c 100644 --- a/bundles/org.eclipse.equinox.p2.artifact.processors/plugin.xml +++ b/bundles/org.eclipse.equinox.p2.artifact.processors/plugin.xml @@ -16,9 +16,4 @@ id="org.eclipse.equinox.p2.processing.JBPatchZipStep"> - - - diff --git a/bundles/org.eclipse.equinox.p2.artifact.processors/src/org/eclipse/equinox/internal/p2/artifact/processors/verifier/MD5Verifier.java b/bundles/org.eclipse.equinox.p2.artifact.processors/src/org/eclipse/equinox/internal/p2/artifact/processors/verifier/MD5Verifier.java deleted file mode 100644 index 11161de66..000000000 --- a/bundles/org.eclipse.equinox.p2.artifact.processors/src/org/eclipse/equinox/internal/p2/artifact/processors/verifier/MD5Verifier.java +++ /dev/null @@ -1,83 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2007 compeople AG and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * compeople AG (Stefan Liebig) - initial API and implementation - * IBM Corporation - ongoing development - *******************************************************************************/ -package org.eclipse.equinox.internal.p2.artifact.processors.verifier; - -import java.io.IOException; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.Status; -import org.eclipse.equinox.internal.p2.artifact.processors.Activator; -import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactDescriptor; -import org.eclipse.equinox.internal.provisional.p2.artifact.repository.processing.ProcessingStep; -import org.eclipse.equinox.internal.provisional.p2.artifact.repository.processing.ProcessingStepDescriptor; - -public class MD5Verifier extends ProcessingStep { - - protected String md5Test; - private MessageDigest md5; - - public MD5Verifier() { - super(); - } - - public MD5Verifier(String md5Test) { - super(); - this.md5Test = md5Test; - basicInitialize(null); - } - - public void initialize(ProcessingStepDescriptor descriptor, IArtifactDescriptor context) { - super.initialize(descriptor, context); - String data = descriptor.getData(); - if (data.equals("download")) - md5Test = context.getProperty(IArtifactDescriptor.DOWNLOAD_MD5); - else if (data.equals("artifact")) - md5Test = context.getProperty(IArtifactDescriptor.ARTIFACT_MD5); - else - md5Test = data; - basicInitialize(descriptor); - } - - private void basicInitialize(ProcessingStepDescriptor descriptor) { - int code = (descriptor == null) ? IStatus.ERROR : descriptor.isRequired() ? IStatus.ERROR : IStatus.INFO; - if (md5Test == null || md5Test.length() != 32) - setStatus(new Status(code, Activator.ID, "MD5 value not available or incorrect size")); - try { - md5 = MessageDigest.getInstance("MD5"); - } catch (NoSuchAlgorithmException e) { - setStatus(new Status(code, Activator.ID, "Could not create MD5 algorithm", e)); - } - } - - public void write(int b) throws IOException { - if (b != -1) - md5.update((byte) b); - getDestination().write(b); - } - - public void close() throws IOException { - super.close(); - - byte[] digest = md5.digest(); - StringBuffer buf = new StringBuffer(); - for (int i = 0; i < digest.length; i++) { - if ((digest[i] & 0xFF) < 0x10) - buf.append('0'); - buf.append(Integer.toHexString(digest[i] & 0xFF)); - } - - // if the hashes don't line up set the status to error. - if (!buf.toString().equals(md5Test)) - setStatus(new Status(IStatus.ERROR, Activator.ID, "Error processing stream. MD5 hash is not as expected.")); - } -} diff --git a/bundles/org.eclipse.equinox.p2.artifact.repository/src/org/eclipse/equinox/internal/p2/artifact/processors/md5/messages.properties b/bundles/org.eclipse.equinox.p2.artifact.repository/src/org/eclipse/equinox/internal/p2/artifact/processors/md5/messages.properties new file mode 100644 index 000000000..426b8a009 --- /dev/null +++ b/bundles/org.eclipse.equinox.p2.artifact.repository/src/org/eclipse/equinox/internal/p2/artifact/processors/md5/messages.properties @@ -0,0 +1,3 @@ +Error_invalid_hash=MD5 value not available or incorrect size, {0}. +Error_MD5_unavailable=Could not create MD5 algorithm. +Error_unexpected_hash=Error while processing stream. MD5 hash is not as expected. Expected: {0} and found {1}. \ No newline at end of file diff --git a/bundles/org.eclipse.equinox.p2.artifact.repository/src/org/eclipse/equinox/internal/p2/artifact/repository/simple/SimpleArtifactRepository.java b/bundles/org.eclipse.equinox.p2.artifact.repository/src/org/eclipse/equinox/internal/p2/artifact/repository/simple/SimpleArtifactRepository.java index 6da8b5499..4a815e948 100644 --- a/bundles/org.eclipse.equinox.p2.artifact.repository/src/org/eclipse/equinox/internal/p2/artifact/repository/simple/SimpleArtifactRepository.java +++ b/bundles/org.eclipse.equinox.p2.artifact.repository/src/org/eclipse/equinox/internal/p2/artifact/repository/simple/SimpleArtifactRepository.java @@ -35,7 +35,7 @@ public class SimpleArtifactRepository extends AbstractArtifactRepository impleme */ public static final boolean MIRRORS_ENABLED = !"false".equals(Activator.getContext().getProperty("eclipse.p2.mirrors")); //$NON-NLS-1$//$NON-NLS-2$ - public static final boolean enableMD5 = !"false".equals(Activator.getContext().getProperty("eclipse.noMD5Check")); //$NON-NLS-1$//$NON-NLS-2$ + public static final boolean MD5_CHECK_ENABLED = !"false".equals(Activator.getContext().getProperty("eclipse.p2.MD5Check")); //$NON-NLS-1$//$NON-NLS-2$ /** * The key for a integer property controls the maximum number @@ -327,7 +327,7 @@ public class SimpleArtifactRepository extends AbstractArtifactRepository impleme ArrayList steps = new ArrayList(); if (IArtifactDescriptor.TYPE_ZIP.equals(descriptor.getProperty(IArtifactDescriptor.DOWNLOAD_CONTENTTYPE))) steps.add(new ZipVerifierStep()); - if (enableMD5 && descriptor.getProperty(IArtifactDescriptor.DOWNLOAD_MD5) != null) + if (MD5_CHECK_ENABLED && descriptor.getProperty(IArtifactDescriptor.DOWNLOAD_MD5) != null) steps.add(new MD5Verifier(descriptor.getProperty(IArtifactDescriptor.DOWNLOAD_MD5))); // Add steps here if needed if (steps.isEmpty()) diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/spi/p2/publisher/PublisherHelper.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/spi/p2/publisher/PublisherHelper.java index 3a0ba7f1a..d17fb31fe 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/spi/p2/publisher/PublisherHelper.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/spi/p2/publisher/PublisherHelper.java @@ -15,6 +15,8 @@ package org.eclipse.equinox.spi.p2.publisher; import java.io.*; import java.net.URI; import java.net.URISyntaxException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.util.*; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; @@ -143,9 +145,50 @@ public class PublisherHelper { // TODO - this is wrong but I'm testing a work-around for bug 205842 result.setProperty(IArtifactDescriptor.DOWNLOAD_SIZE, Long.toString(pathOnDisk.length())); } + String md5 = computeMD5(pathOnDisk); + if (md5 != null) + result.setProperty(IArtifactDescriptor.DOWNLOAD_MD5, md5); return result; } + private static String computeMD5(File file) { + if (file == null || file.isDirectory() || !file.exists()) + return null; + MessageDigest md5Checker; + try { + md5Checker = MessageDigest.getInstance("MD5"); //$NON-NLS-1$ + } catch (NoSuchAlgorithmException e) { + return null; + } + InputStream fis = null; + try { + fis = new BufferedInputStream(new FileInputStream(file)); + int read = -1; + while ((read = fis.read()) != -1) { + md5Checker.update((byte) read); + } + byte[] digest = md5Checker.digest(); + StringBuffer buf = new StringBuffer(); + for (int i = 0; i < digest.length; i++) { + if ((digest[i] & 0xFF) < 0x10) + buf.append('0'); + buf.append(Integer.toHexString(digest[i] & 0xFF)); + } + return buf.toString(); + } catch (FileNotFoundException e) { + return null; + } catch (IOException e) { + return null; + } finally { + if (fis != null) + try { + fis.close(); + } catch (IOException e) { + // ignore + } + } + } + /** * @deprecated moved to AbstractPublishingAction */ diff --git a/bundles/org.eclipse.equinox.p2.tests.optimizers/META-INF/MANIFEST.MF b/bundles/org.eclipse.equinox.p2.tests.optimizers/META-INF/MANIFEST.MF index b9600237f..d5ebaecff 100644 --- a/bundles/org.eclipse.equinox.p2.tests.optimizers/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.equinox.p2.tests.optimizers/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: p2 Optimizers Test Plug-in Bundle-SymbolicName: org.eclipse.equinox.p2.tests.optimizers;singleton:=true -Bundle-Version: 1.0.0.qualifier +Bundle-Version: 1.0.100.qualifier Bundle-RequiredExecutionEnvironment: J2SE-1.4 Require-Bundle: org.eclipse.equinox.common;bundle-version="3.4.0", org.junit;bundle-version="3.8.2" @@ -17,7 +17,6 @@ Import-Package: ie.wombat.jbdiff, org.eclipse.equinox.internal.p2.artifact.processors.jardelta, org.eclipse.equinox.internal.p2.artifact.processors.jbdiff, org.eclipse.equinox.internal.p2.artifact.processors.pack200, - org.eclipse.equinox.internal.p2.artifact.processors.verifier, org.eclipse.equinox.internal.p2.artifact.repository, org.eclipse.equinox.internal.p2.core.helpers, org.eclipse.equinox.internal.p2.metadata, -- cgit v1.2.3