From 8076999d688278338a4f78bb3868960bb076ad05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9da=20Housni=20Alaoui?= Date: Tue, 15 Sep 2015 15:55:51 +0200 Subject: [PATCH] #23 Digest computation on packed 200 jars should only consider jar content --- .../com/threerings/getdown/data/Resource.java | 44 ++++++++++++++----- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/threerings/getdown/data/Resource.java b/src/main/java/com/threerings/getdown/data/Resource.java index 7d9fac1..a11f34a 100644 --- a/src/main/java/com/threerings/getdown/data/Resource.java +++ b/src/main/java/com/threerings/getdown/data/Resource.java @@ -5,10 +5,7 @@ package com.threerings.getdown.data; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; import java.net.URL; import java.security.MessageDigest; import java.util.Collections; @@ -16,12 +13,14 @@ import java.util.Comparator; import java.util.List; import java.util.jar.JarEntry; import java.util.jar.JarFile; +import java.util.jar.JarInputStream; import com.samskivert.io.StreamUtil; import com.samskivert.util.StringUtil; import com.threerings.getdown.util.FileUtil; import com.threerings.getdown.util.ProgressObserver; +import sun.misc.IOUtils; import static com.threerings.getdown.Log.log; @@ -42,8 +41,8 @@ public class Resource _marker = new File(lpath + "v"); _unpack = unpack; - _isJar = lpath.endsWith(".jar"); - _isPacked200Jar = (lpath.endsWith(".jar.pack") || lpath.endsWith(".jar.pack.gz")); + _isJar = isJar(lpath); + _isPacked200Jar = isPacked200Jar(lpath); if (_unpack && _isJar) { _unpacked = _local.getParentFile(); } else if(_unpack && _isPacked200Jar) { @@ -218,6 +217,14 @@ public class Resource return _path; } + private static boolean isJar(String path){ + return path.endsWith(".jar"); + } + + private static boolean isPacked200Jar(String path){ + return path.endsWith(".jar.pack") || path.endsWith(".jar.pack.gz"); + } + /** * Computes the MD5 hash of the supplied file. */ @@ -229,12 +236,25 @@ public class Resource byte[] buffer = new byte[DIGEST_BUFFER_SIZE]; int read; + boolean isJar = isJar(target.getPath()); + boolean isPacked200Jar = isPacked200Jar(target.getPath()); + // if this is a jar file, we need to compute the digest in a // timestamp and file order agnostic manner to properly correlate // jardiff patched jars with their unpatched originals - if (target.getPath().endsWith(".jar")) { - JarFile jar = new JarFile(target); + if(isJar || isPacked200Jar){ + File tmpJarFile = null; + JarFile jar = null; try { + // if this is a compressed jar file, we need to uncompress it to compute the jar file digest + if(isPacked200Jar){ + tmpJarFile = new File(target.getPath() + ".tmp"); + FileUtil.unpackPacked200Jar(target, tmpJarFile); + jar = new JarFile(tmpJarFile); + } else{ + jar = new JarFile(target); + } + List entries = Collections.list(jar.entries()); Collections.sort(entries, ENTRY_COMP); @@ -261,12 +281,16 @@ public class Resource } finally { try { - jar.close(); + if(jar != null){ + jar.close(); + } } catch (IOException ioe) { log.warning("Error closing jar [path=" + target + ", error=" + ioe + "]."); } + if(tmpJarFile != null){ + tmpJarFile.delete(); + } } - } else { long totalSize = target.length(), position = 0L; FileInputStream fin = null;