revert non-related changes, small fixes

This commit is contained in:
sergiorussia
2019-05-05 17:24:38 +03:00
parent c17882f432
commit 4973da5e45
6 changed files with 73 additions and 51 deletions
@@ -62,17 +62,18 @@ public class Resource implements Comparable<Resource>
byte[] buffer = new byte[DIGEST_BUFFER_SIZE]; byte[] buffer = new byte[DIGEST_BUFFER_SIZE];
int read; int read;
boolean isJar = isJar(target.getPath()); boolean isJar = isJar(target);
boolean isPacked200Jar = isPacked200Jar(target.getPath()); boolean isPacked200Jar = isPacked200Jar(target);
// if this is a jar, we need to compute the digest in a "timestamp and file order" agnostic // 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 // manner to properly correlate jardiff patched jars with their unpatched originals
if (isJar || isPacked200Jar){ if (isJar || isPacked200Jar){
File tmpJarFile = null;
ZipFile jar = null; ZipFile jar = null;
try { try {
// if this is a compressed jar file, uncompress it to compute the jar file digest // if this is a compressed jar file, uncompress it to compute the jar file digest
if (isPacked200Jar){ if (isPacked200Jar){
File tmpJarFile = new File(target.getPath() + ".tmp"); tmpJarFile = new File(target.getPath() + ".tmp");
tmpJarFile.deleteOnExit(); tmpJarFile.deleteOnExit();
jar = FileUtil.unpackPacked200Jar(target, tmpJarFile); jar = FileUtil.unpackPacked200Jar(target, tmpJarFile);
} else{ } else{
@@ -109,6 +110,9 @@ public class Resource implements Comparable<Resource>
log.warning("Error closing jar", "path", target, "jar", jar, "error", ioe); log.warning("Error closing jar", "path", target, "jar", jar, "error", ioe);
} }
} }
if (tmpJarFile != null) {
FileUtil.deleteHarder(tmpJarFile);
}
} }
} else { } else {
@@ -133,12 +137,11 @@ public class Resource implements Comparable<Resource>
_remote = remote; _remote = remote;
_local = local; _local = local;
_localNew = new File(local.toString() + "_new"); _localNew = new File(local.toString() + "_new");
String lpath = _local.getPath(); _marker = new File(_local.getPath() + "v");
_marker = new File(lpath + "v");
_attrs = attrs; _attrs = attrs;
_isJar = isJar(lpath); _isJar = isJar(local);
_isPacked200Jar = isPacked200Jar(lpath); _isPacked200Jar = isPacked200Jar(local);
boolean unpack = attrs.contains(Attr.UNPACK); boolean unpack = attrs.contains(Attr.UNPACK);
if (unpack && _isJar) { if (unpack && _isJar) {
_unpacked = _local.getParentFile(); _unpacked = _local.getParentFile();
@@ -339,7 +342,11 @@ public class Resource implements Comparable<Resource>
@Override public boolean equals (Object other) @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 () @Override public int hashCode ()
@@ -360,13 +367,16 @@ public class Resource implements Comparable<Resource>
} }
} }
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") || return path.endsWith(".jar.pack") || path.endsWith(".jar.pack_new") ||
path.endsWith(".jar.pack.gz")|| path.endsWith(".jar.pack.gz_new"); path.endsWith(".jar.pack.gz")|| path.endsWith(".jar.pack.gz_new");
} }
@@ -88,8 +88,8 @@ public class Differ
} }
} }
protected static void createPatch(File patch, ArrayList<Resource> orsrcs, protected void createPatch (File patch, ArrayList<Resource> orsrcs,
ArrayList<Resource> nrsrcs, boolean verbose) ArrayList<Resource> nrsrcs, boolean verbose)
throws IOException throws IOException
{ {
int version = Digest.VERSION; 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"); File temp = File.createTempFile("differ", "jar");
try (ZipFile jar = new ZipFile(target); try (ZipFile jar = new ZipFile(target);
@@ -189,7 +190,8 @@ public class Differ
return temp; 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); JarDiff.createPatch(ofile.getPath(), nfile.getPath(), jout, false);
} }
@@ -197,7 +199,8 @@ public class Differ
public static void main (String[] args) public static void main (String[] args)
{ {
if (args.length < 2) { 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); System.exit(255);
} }
Differ differ = new Differ(); Differ differ = new Differ();
@@ -74,16 +74,18 @@ public class JarDiff implements JarDiffCodes
private static boolean _debug; 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 <code>os</code>.
*/ */
public static void createPatch (String oldPath, String newPath, public static void createPatch (String oldPath, String newPath,
OutputStream os, boolean minimal) throws IOException OutputStream os, boolean minimal) throws IOException
{ {
try (JarFile2 oldJar = new JarFile2(oldPath); JarFile2 newJar = new JarFile2(newPath)) { try (JarFile2 oldJar = new JarFile2(oldPath);
final Map<String, String> moved = new HashMap<>(); JarFile2 newJar = new JarFile2(newPath)) {
final Set<String> implicit = new HashSet<>();
final Set<String> moveSrc = new HashSet<>(); HashMap<String,String> moved = new HashMap<>();
final Set<String> newEntries = new HashSet<>(); HashSet<String> implicit = new HashSet<>();
HashSet<String> moveSrc = new HashSet<>();
HashSet<String> newEntries = new HashSet<>();
// FIRST PASS // FIRST PASS
// Go through the entries in new jar and // Go through the entries in new jar and
@@ -128,6 +130,7 @@ public class JarDiff implements JarDiffCodes
// for backward compatibility // for backward compatibility
if (_debug) { if (_debug) {
System.out.println("NEW: "+ newname); System.out.println("NEW: "+ newname);
} }
newEntries.add(newname); newEntries.add(newname);
@@ -141,9 +144,12 @@ public class JarDiff implements JarDiffCodes
} }
// Check if this disables an implicit 'move <oldname> <oldname>' // Check if this disables an implicit 'move <oldname> <oldname>'
if (implicit.contains(oldname) && minimal) { if (implicit.contains(oldname) && minimal) {
if (_debug) { if (_debug) {
System.err.println("implicit.remove " + oldname); System.err.println("implicit.remove " + oldname);
System.err.println("moved.put " + oldname + " " + oldname); System.err.println("moved.put " + oldname + " " + oldname);
} }
implicit.remove(oldname); implicit.remove(oldname);
moved.put(oldname, oldname); moved.put(oldname, oldname);
@@ -155,7 +161,7 @@ public class JarDiff implements JarDiffCodes
// SECOND PASS: <deleted files> = <oldjarnames> - <implicitmoves> - // SECOND PASS: <deleted files> = <oldjarnames> - <implicitmoves> -
// <source of move commands> - <new or modified entries> // <source of move commands> - <new or modified entries>
final List<String> deleted = new ArrayList<>(); ArrayList<String> deleted = new ArrayList<>();
for (ZipEntry oldEntry : oldJar) { for (ZipEntry oldEntry : oldJar) {
String oldName = oldEntry.getName(); String oldName = oldEntry.getName();
if (!implicit.contains(oldName) && !moveSrc.contains(oldName) if (!implicit.contains(oldName) && !moveSrc.contains(oldName)
@@ -201,11 +207,12 @@ public class JarDiff implements JarDiffCodes
} }
/** /**
* Writes the index file out to {@code jos}. * Writes the index file out to <code>jos</code>.
* {@code oldEntries} gives the names of the files that were removed, * <code>oldEntries</code> gives the names of the files that were removed,
* {@code movedMap} maps from the new name to the old name. * <code>movedMap</code> maps from the new name to the old name.
*/ */
private static void createIndex (ZipOutputStream jos, List<String> oldEntries, Map<String,String> movedMap) private static void createIndex (ZipOutputStream jos, List<String> oldEntries,
Map<String,String> movedMap)
throws IOException throws IOException
{ {
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
@@ -236,7 +243,7 @@ public class JarDiff implements JarDiffCodes
jos.write(bytes, 0, bytes.length); jos.write(bytes, 0, bytes.length);
} }
protected static void writeEscapedString (Writer writer, String string) protected static Writer writeEscapedString (Writer writer, String string)
throws IOException throws IOException
{ {
int index = 0; int index = 0;
@@ -262,6 +269,7 @@ public class JarDiff implements JarDiffCodes
writer.write(string); writer.write(string);
} }
return writer;
} }
private static void writeEntry (ZipOutputStream jos, ZipEntry entry, JarFile2 file) 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 { public String hasSameContent (JarFile2 file, ZipEntry entry) throws IOException {
String thisName = null; 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 // check if this jar contains files with the passed in entry's crc
if (_crcToEntryMap.containsKey(crcL)) { if (_crcToEntryMap.containsKey(crcL)) {
// get the Linked List with files with the crc // get the Linked List with files with the crc
@@ -415,7 +423,7 @@ public class JarDiff implements JarDiffCodes
while (entries.hasMoreElements()) { while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement(); ZipEntry entry = entries.nextElement();
long crc = entry.getCrc(); long crc = entry.getCrc();
Long crcL = crc; Long crcL = Long.valueOf(crc);
if (_debug) { if (_debug) {
System.out.println("\t" + entry.getName() + " CRC " + crc); System.out.println("\t" + entry.getName() + " CRC " + crc);
} }
@@ -432,7 +440,7 @@ public class JarDiff implements JarDiffCodes
} else { } else {
// create a new entry in the hashmap for the new key // create a new entry in the hashmap for the new key
LinkedList<ZipEntry> ll = new LinkedList<ZipEntry>(); LinkedList<ZipEntry> ll = new LinkedList<>();
ll.add(entry); ll.add(entry);
_crcToEntryMap.put(crcL, ll); _crcToEntryMap.put(crcL, ll);
} }
@@ -41,22 +41,22 @@ public class JarDiffPatcher implements JarDiffCodes
* *
* @throws IOException if any problem occurs during patching. * @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 throws IOException
{ {
File oldFile = new File(jarPath), diffFile = new File(diffPath); File oldFile = new File(jarPath), diffFile = new File(diffPath);
try (ZipFile oldJar = new ZipFile(oldFile); try (ZipFile oldJar = new ZipFile(oldFile);
ZipFile jarDiff = new ZipFile(diffFile); ZipFile jarDiff = new ZipFile(diffFile);
ZipOutputStream jos = new ZipOutputStream(new FileOutputStream(target))) { ZipOutputStream jos = new ZipOutputStream(new FileOutputStream(target))) {
final Set<String> ignoreSet = new HashSet<>(); Set<String> ignoreSet = new HashSet<>();
final Map<String, String> renameMap = new HashMap<>(); Map<String, String> renameMap = new HashMap<>();
determineNameMapping(jarDiff, ignoreSet, renameMap); determineNameMapping(jarDiff, ignoreSet, renameMap);
// get all keys in renameMap // get all keys in renameMap
String[] keys = renameMap.keySet().toArray(new String[renameMap.size()]); String[] keys = renameMap.keySet().toArray(new String[renameMap.size()]);
// Files to implicit move // Files to implicit move
final Set<String> oldjarNames = new HashSet<>(); Set<String> oldjarNames = new HashSet<>();
Enumeration<? extends ZipEntry> oldEntries = oldJar.entries(); Enumeration<? extends ZipEntry> oldEntries = oldJar.entries();
if (oldEntries != null) { if (oldEntries != null) {
while (oldEntries.hasMoreElements()) { while (oldEntries.hasMoreElements()) {
@@ -111,7 +111,8 @@ public class JarDiffPatcher implements JarDiffCodes
// Get source ZipEntry // Get source ZipEntry
ZipEntry oldEntry = oldJar.getEntry(oldName); ZipEntry oldEntry = oldJar.getEntry(oldName);
if (oldEntry == null) { 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 // 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) { if (observer != null) {
observer.progress((int)(100*currentSize/size)); observer.progress((int)(100*currentSize/size));
} }
} }
protected static void determineNameMapping( protected void determineNameMapping (
ZipFile jarDiff, Set<String> ignoreSet, Map<String, String> renameMap) ZipFile jarDiff, Set<String> ignoreSet, Map<String, String> renameMap)
throws IOException throws IOException
{ {
@@ -172,7 +173,8 @@ public class JarDiffPatcher implements JarDiffCodes
throw new IOException("error.noindex"); 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(); String line = indexReader.readLine();
if (line == null || !line.equals(VERSION_HEADER)) { if (line == null || !line.equals(VERSION_HEADER)) {
throw new IOException("jardiff.error.badheader: " + line); throw new IOException("jardiff.error.badheader: " + line);
@@ -207,11 +209,12 @@ public class JarDiffPatcher implements JarDiffCodes
} }
} }
protected static List<String> getSubpaths(String path) protected List<String> getSubpaths (String path)
{ {
int index = 0; int index = 0;
int length = path.length(); int length = path.length();
final List<String> sub = new ArrayList<>(); ArrayList<String> sub = new ArrayList<>();
while (index < length) { while (index < length) {
while (index < length && Character.isWhitespace while (index < length && Character.isWhitespace
(path.charAt(index))) { (path.charAt(index))) {
@@ -251,7 +254,7 @@ public class JarDiffPatcher implements JarDiffCodes
return sub; return sub;
} }
protected static void writeEntry(ZipOutputStream jos, ZipEntry entry, ZipFile file) protected void writeEntry (ZipOutputStream jos, ZipEntry entry, ZipFile file)
throws IOException throws IOException
{ {
try (InputStream data = file.getInputStream(entry)) { 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 throws IOException
{ {
jos.putNextEntry(new ZipEntry(entry.getName())); jos.putNextEntry(new ZipEntry(entry.getName()));
@@ -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 target = new File(appdir, path);
File patch = new File(appdir, entry.getName()); File patch = new File(appdir, entry.getName());
@@ -154,7 +155,7 @@ public class Patcher
// now apply the patch to create the new target file // now apply the patch to create the new target file
patcher = new JarDiffPatcher(); patcher = new JarDiffPatcher();
JarDiffPatcher.patchJar(otarget.getPath(), patch.getPath(), target, obs); patcher.patchJar(otarget.getPath(), patch.getPath(), target, obs);
} catch (IOException ioe) { } catch (IOException ioe) {
if (patcher == null) { if (patcher == null) {
@@ -120,9 +120,7 @@ public final class FileUtil
{ {
List<String> lines = new ArrayList<>(); List<String> lines = new ArrayList<>();
try (BufferedReader bin = new BufferedReader(in)) { try (BufferedReader bin = new BufferedReader(in)) {
for (String line; (line = bin.readLine()) != null;) { for (String line = null; (line = bin.readLine()) != null; lines.add(line)) {}
lines.add(line);
}
} }
return lines; return lines;
} }
@@ -142,9 +140,8 @@ public final class FileUtil
File efile = new File(target, entry.getName()); File efile = new File(target, entry.getName());
if (efile.exists()) { if (efile.exists()) {
for (File f : efile.listFiles()) { for (File f : efile.listFiles()) {
if (!f.isDirectory()) { if (!f.isDirectory())
f.delete(); f.delete();
}
} }
} }
} }