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];
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<Resource>
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<Resource>
_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<Resource>
@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<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") ||
path.endsWith(".jar.pack.gz")|| path.endsWith(".jar.pack.gz_new");
}
@@ -88,7 +88,7 @@ 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)
throws IOException
{
@@ -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();
@@ -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 <code>os</code>.
*/
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<String, String> moved = new HashMap<>();
final Set<String> implicit = new HashSet<>();
final Set<String> moveSrc = new HashSet<>();
final Set<String> newEntries = new HashSet<>();
try (JarFile2 oldJar = new JarFile2(oldPath);
JarFile2 newJar = new JarFile2(newPath)) {
HashMap<String,String> moved = new HashMap<>();
HashSet<String> implicit = new HashSet<>();
HashSet<String> moveSrc = new HashSet<>();
HashSet<String> 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 <oldname> <oldname>'
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: <deleted files> = <oldjarnames> - <implicitmoves> -
// <source of move commands> - <new or modified entries>
final List<String> deleted = new ArrayList<>();
ArrayList<String> 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 <code>jos</code>.
* <code>oldEntries</code> gives the names of the files that were removed,
* <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
{
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<ZipEntry> ll = new LinkedList<ZipEntry>();
LinkedList<ZipEntry> ll = new LinkedList<>();
ll.add(entry);
_crcToEntryMap.put(crcL, ll);
}
@@ -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<String> ignoreSet = new HashSet<>();
final Map<String, String> renameMap = new HashMap<>();
Set<String> ignoreSet = new HashSet<>();
Map<String, String> 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<String> oldjarNames = new HashSet<>();
Set<String> oldjarNames = new HashSet<>();
Enumeration<? extends ZipEntry> 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<String> ignoreSet, Map<String, String> 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<String> getSubpaths(String path)
protected List<String> getSubpaths (String path)
{
int index = 0;
int length = path.length();
final List<String> sub = new ArrayList<>();
ArrayList<String> 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()));
@@ -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) {
@@ -120,9 +120,7 @@ public final class FileUtil
{
List<String> 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,14 +140,13 @@ public final class FileUtil
File efile = new File(target, entry.getName());
if (efile.exists()) {
for (File f : efile.listFiles()) {
if (!f.isDirectory()) {
if (!f.isDirectory())
f.delete();
}
}
}
}
}
}
Enumeration<? extends ZipEntry> entries = jar.entries();
while (entries.hasMoreElements()) {