diff --git a/core/src/main/java/com/threerings/getdown/data/Resource.java b/core/src/main/java/com/threerings/getdown/data/Resource.java index e8f8f1d..78ca844 100644 --- a/core/src/main/java/com/threerings/getdown/data/Resource.java +++ b/core/src/main/java/com/threerings/getdown/data/Resource.java @@ -62,17 +62,18 @@ public class Resource implements Comparable byte[] buffer = new byte[DIGEST_BUFFER_SIZE]; int read; - boolean isJar = isJar(target.getPath()); - boolean isPacked200Jar = isPacked200Jar(target.getPath()); + boolean isJar = isJar(target); + boolean isPacked200Jar = isPacked200Jar(target); // if this is a jar, 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 (isJar || isPacked200Jar){ + File tmpJarFile = null; ZipFile jar = null; try { // if this is a compressed jar file, uncompress it to compute the jar file digest if (isPacked200Jar){ - File tmpJarFile = new File(target.getPath() + ".tmp"); + tmpJarFile = new File(target.getPath() + ".tmp"); tmpJarFile.deleteOnExit(); jar = FileUtil.unpackPacked200Jar(target, tmpJarFile); } else{ @@ -109,6 +110,9 @@ public class Resource implements Comparable log.warning("Error closing jar", "path", target, "jar", jar, "error", ioe); } } + if (tmpJarFile != null) { + FileUtil.deleteHarder(tmpJarFile); + } } } else { @@ -133,12 +137,11 @@ public class Resource implements Comparable _remote = remote; _local = local; _localNew = new File(local.toString() + "_new"); - String lpath = _local.getPath(); - _marker = new File(lpath + "v"); + _marker = new File(_local.getPath() + "v"); _attrs = attrs; - _isJar = isJar(lpath); - _isPacked200Jar = isPacked200Jar(lpath); + _isJar = isJar(local); + _isPacked200Jar = isPacked200Jar(local); boolean unpack = attrs.contains(Attr.UNPACK); if (unpack && _isJar) { _unpacked = _local.getParentFile(); @@ -339,7 +342,11 @@ public class Resource implements Comparable @Override public boolean equals (Object other) { - return other instanceof Resource && _path.equals(((Resource) other)._path); + if (other instanceof Resource) { + return _path.equals(((Resource)other)._path); + } else { + return false; + } } @Override public int hashCode () @@ -360,13 +367,16 @@ public class Resource implements Comparable } } - protected static boolean isJar (String path) + protected static boolean isJar (File file) { - return path.endsWith(".jar") || path.endsWith(".jar_new") || path.endsWith(".zip"); + String path = file.getName(); + return path.endsWith(".jar") || path.endsWith(".jar_new") || + path.endsWith(".zip") || path.endsWith(".zip_new"); } - protected static boolean isPacked200Jar (String path) + protected static boolean isPacked200Jar (File file) { + String path = file.getName(); return path.endsWith(".jar.pack") || path.endsWith(".jar.pack_new") || path.endsWith(".jar.pack.gz")|| path.endsWith(".jar.pack.gz_new"); } diff --git a/core/src/main/java/com/threerings/getdown/tools/Differ.java b/core/src/main/java/com/threerings/getdown/tools/Differ.java index 31a0f0a..97a7d29 100644 --- a/core/src/main/java/com/threerings/getdown/tools/Differ.java +++ b/core/src/main/java/com/threerings/getdown/tools/Differ.java @@ -88,8 +88,8 @@ public class Differ } } - protected static void createPatch(File patch, ArrayList orsrcs, - ArrayList nrsrcs, boolean verbose) + protected void createPatch (File patch, ArrayList orsrcs, + ArrayList nrsrcs, boolean verbose) throws IOException { int version = Digest.VERSION; @@ -165,7 +165,8 @@ public class Differ } } - protected static File rebuildJar(File target) throws IOException + protected File rebuildJar (File target) + throws IOException { File temp = File.createTempFile("differ", "jar"); try (ZipFile jar = new ZipFile(target); @@ -189,7 +190,8 @@ public class Differ return temp; } - protected static void jarDiff(File ofile, File nfile, ZipOutputStream jout) throws IOException + protected void jarDiff (File ofile, File nfile, ZipOutputStream jout) + throws IOException { JarDiff.createPatch(ofile.getPath(), nfile.getPath(), jout, false); } @@ -197,7 +199,8 @@ public class Differ public static void main (String[] args) { if (args.length < 2) { - System.err.println("Usage: Differ [-verbose] new_vers_dir old_vers_dir"); + System.err.println( + "Usage: Differ [-verbose] new_vers_dir old_vers_dir"); System.exit(255); } Differ differ = new Differ(); diff --git a/core/src/main/java/com/threerings/getdown/tools/JarDiff.java b/core/src/main/java/com/threerings/getdown/tools/JarDiff.java index 90ddf0c..734fc9c 100644 --- a/core/src/main/java/com/threerings/getdown/tools/JarDiff.java +++ b/core/src/main/java/com/threerings/getdown/tools/JarDiff.java @@ -74,16 +74,18 @@ public class JarDiff implements JarDiffCodes private static boolean _debug; /** - * Creates a patch from the two passed in files, writing the result to {@code os}. + * Creates a patch from the two passed in files, writing the result to os. */ public static void createPatch (String oldPath, String newPath, OutputStream os, boolean minimal) throws IOException { - try (JarFile2 oldJar = new JarFile2(oldPath); JarFile2 newJar = new JarFile2(newPath)) { - final Map moved = new HashMap<>(); - final Set implicit = new HashSet<>(); - final Set moveSrc = new HashSet<>(); - final Set newEntries = new HashSet<>(); + try (JarFile2 oldJar = new JarFile2(oldPath); + JarFile2 newJar = new JarFile2(newPath)) { + + HashMap moved = new HashMap<>(); + HashSet implicit = new HashSet<>(); + HashSet moveSrc = new HashSet<>(); + HashSet newEntries = new HashSet<>(); // FIRST PASS // Go through the entries in new jar and @@ -128,6 +130,7 @@ public class JarDiff implements JarDiffCodes // for backward compatibility if (_debug) { + System.out.println("NEW: "+ newname); } newEntries.add(newname); @@ -141,9 +144,12 @@ public class JarDiff implements JarDiffCodes } // Check if this disables an implicit 'move ' if (implicit.contains(oldname) && minimal) { + if (_debug) { System.err.println("implicit.remove " + oldname); + System.err.println("moved.put " + oldname + " " + oldname); + } implicit.remove(oldname); moved.put(oldname, oldname); @@ -155,7 +161,7 @@ public class JarDiff implements JarDiffCodes // SECOND PASS: = - - // - - final List deleted = new ArrayList<>(); + ArrayList deleted = new ArrayList<>(); for (ZipEntry oldEntry : oldJar) { String oldName = oldEntry.getName(); if (!implicit.contains(oldName) && !moveSrc.contains(oldName) @@ -201,11 +207,12 @@ public class JarDiff implements JarDiffCodes } /** - * Writes the index file out to {@code jos}. - * {@code oldEntries} gives the names of the files that were removed, - * {@code movedMap} maps from the new name to the old name. + * Writes the index file out to jos. + * oldEntries gives the names of the files that were removed, + * movedMap maps from the new name to the old name. */ - private static void createIndex (ZipOutputStream jos, List oldEntries, Map movedMap) + private static void createIndex (ZipOutputStream jos, List oldEntries, + Map movedMap) throws IOException { StringWriter writer = new StringWriter(); @@ -236,7 +243,7 @@ public class JarDiff implements JarDiffCodes jos.write(bytes, 0, bytes.length); } - protected static void writeEscapedString (Writer writer, String string) + protected static Writer writeEscapedString (Writer writer, String string) throws IOException { int index = 0; @@ -262,6 +269,7 @@ public class JarDiff implements JarDiffCodes writer.write(string); } + return writer; } private static void writeEntry (ZipOutputStream jos, ZipEntry entry, JarFile2 file) @@ -380,7 +388,7 @@ public class JarDiff implements JarDiffCodes public String hasSameContent (JarFile2 file, ZipEntry entry) throws IOException { String thisName = null; - Long crcL = entry.getCrc(); + Long crcL = Long.valueOf(entry.getCrc()); // check if this jar contains files with the passed in entry's crc if (_crcToEntryMap.containsKey(crcL)) { // get the Linked List with files with the crc @@ -415,7 +423,7 @@ public class JarDiff implements JarDiffCodes while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); long crc = entry.getCrc(); - Long crcL = crc; + Long crcL = Long.valueOf(crc); if (_debug) { System.out.println("\t" + entry.getName() + " CRC " + crc); } @@ -432,7 +440,7 @@ public class JarDiff implements JarDiffCodes } else { // create a new entry in the hashmap for the new key - LinkedList ll = new LinkedList(); + LinkedList ll = new LinkedList<>(); ll.add(entry); _crcToEntryMap.put(crcL, ll); } diff --git a/core/src/main/java/com/threerings/getdown/tools/JarDiffPatcher.java b/core/src/main/java/com/threerings/getdown/tools/JarDiffPatcher.java index af42173..859d73e 100644 --- a/core/src/main/java/com/threerings/getdown/tools/JarDiffPatcher.java +++ b/core/src/main/java/com/threerings/getdown/tools/JarDiffPatcher.java @@ -41,22 +41,22 @@ public class JarDiffPatcher implements JarDiffCodes * * @throws IOException if any problem occurs during patching. */ - public static void patchJar(String jarPath, String diffPath, File target, ProgressObserver observer) + public void patchJar (String jarPath, String diffPath, File target, ProgressObserver observer) throws IOException { File oldFile = new File(jarPath), diffFile = new File(diffPath); try (ZipFile oldJar = new ZipFile(oldFile); ZipFile jarDiff = new ZipFile(diffFile); ZipOutputStream jos = new ZipOutputStream(new FileOutputStream(target))) { - final Set ignoreSet = new HashSet<>(); - final Map renameMap = new HashMap<>(); + Set ignoreSet = new HashSet<>(); + Map renameMap = new HashMap<>(); determineNameMapping(jarDiff, ignoreSet, renameMap); // get all keys in renameMap String[] keys = renameMap.keySet().toArray(new String[renameMap.size()]); // Files to implicit move - final Set oldjarNames = new HashSet<>(); + Set oldjarNames = new HashSet<>(); Enumeration oldEntries = oldJar.entries(); if (oldEntries != null) { while (oldEntries.hasMoreElements()) { @@ -111,7 +111,8 @@ public class JarDiffPatcher implements JarDiffCodes // Get source ZipEntry ZipEntry oldEntry = oldJar.getEntry(oldName); if (oldEntry == null) { - throw new IOException("error.badmove: " + MOVE_COMMAND + oldName + " " + newName); + String moveCmd = MOVE_COMMAND + oldName + " " + newName; + throw new IOException("error.badmove: " + moveCmd); } // Create dest ZipEntry @@ -156,14 +157,14 @@ public class JarDiffPatcher implements JarDiffCodes } } - protected static void updateObserver(ProgressObserver observer, double currentSize, double size) + protected void updateObserver (ProgressObserver observer, double currentSize, double size) { if (observer != null) { observer.progress((int)(100*currentSize/size)); } } - protected static void determineNameMapping( + protected void determineNameMapping ( ZipFile jarDiff, Set ignoreSet, Map renameMap) throws IOException { @@ -172,7 +173,8 @@ public class JarDiffPatcher implements JarDiffCodes throw new IOException("error.noindex"); } - LineNumberReader indexReader = new LineNumberReader(new InputStreamReader(is, UTF_8)); + LineNumberReader indexReader = + new LineNumberReader(new InputStreamReader(is, UTF_8)); String line = indexReader.readLine(); if (line == null || !line.equals(VERSION_HEADER)) { throw new IOException("jardiff.error.badheader: " + line); @@ -207,11 +209,12 @@ public class JarDiffPatcher implements JarDiffCodes } } - protected static List getSubpaths(String path) + protected List getSubpaths (String path) { int index = 0; int length = path.length(); - final List sub = new ArrayList<>(); + ArrayList sub = new ArrayList<>(); + while (index < length) { while (index < length && Character.isWhitespace (path.charAt(index))) { @@ -251,7 +254,7 @@ public class JarDiffPatcher implements JarDiffCodes return sub; } - protected static void writeEntry(ZipOutputStream jos, ZipEntry entry, ZipFile file) + protected void writeEntry (ZipOutputStream jos, ZipEntry entry, ZipFile file) throws IOException { try (InputStream data = file.getInputStream(entry)) { @@ -259,7 +262,7 @@ public class JarDiffPatcher implements JarDiffCodes } } - protected static void writeEntry(ZipOutputStream jos, ZipEntry entry, InputStream data) + protected void writeEntry (ZipOutputStream jos, ZipEntry entry, InputStream data) throws IOException { jos.putNextEntry(new ZipEntry(entry.getName())); diff --git a/core/src/main/java/com/threerings/getdown/tools/Patcher.java b/core/src/main/java/com/threerings/getdown/tools/Patcher.java index 922ebb0..fc39426 100644 --- a/core/src/main/java/com/threerings/getdown/tools/Patcher.java +++ b/core/src/main/java/com/threerings/getdown/tools/Patcher.java @@ -121,7 +121,8 @@ public class Patcher } } - protected void patchFile (ZipFile file, ZipEntry entry, File appdir, String path) + protected void patchFile (ZipFile file, ZipEntry entry, + File appdir, String path) { File target = new File(appdir, path); File patch = new File(appdir, entry.getName()); @@ -154,7 +155,7 @@ public class Patcher // now apply the patch to create the new target file patcher = new JarDiffPatcher(); - JarDiffPatcher.patchJar(otarget.getPath(), patch.getPath(), target, obs); + patcher.patchJar(otarget.getPath(), patch.getPath(), target, obs); } catch (IOException ioe) { if (patcher == null) { diff --git a/core/src/main/java/com/threerings/getdown/util/FileUtil.java b/core/src/main/java/com/threerings/getdown/util/FileUtil.java index 08f4ac4..06ac41c 100644 --- a/core/src/main/java/com/threerings/getdown/util/FileUtil.java +++ b/core/src/main/java/com/threerings/getdown/util/FileUtil.java @@ -120,9 +120,7 @@ public final class FileUtil { List lines = new ArrayList<>(); try (BufferedReader bin = new BufferedReader(in)) { - for (String line; (line = bin.readLine()) != null;) { - lines.add(line); - } + for (String line = null; (line = bin.readLine()) != null; lines.add(line)) {} } return lines; } @@ -142,9 +140,8 @@ public final class FileUtil File efile = new File(target, entry.getName()); if (efile.exists()) { for (File f : efile.listFiles()) { - if (!f.isDirectory()) { - f.delete(); - } + if (!f.isDirectory()) + f.delete(); } } }