提交 fa7b30b0 编写于 作者: M michaelm

Merge

...@@ -42,3 +42,10 @@ DEMO_DESTDIR = $(DEMODIR)/nio/$(DEMONAME) ...@@ -42,3 +42,10 @@ DEMO_DESTDIR = $(DEMODIR)/nio/$(DEMONAME)
# #
include $(BUILDDIR)/common/Demo.gmk include $(BUILDDIR)/common/Demo.gmk
#EXTJAR = $(EXTDIR)/$(DEMONAME).jar
#
#all : build $(EXTJAR)
#
#$(EXTJAR) : $(DEMO_JAR)
# $(prep-target)
# $(CP) $(DEMO_JAR) $(EXTJAR)
...@@ -44,11 +44,11 @@ public interface Readable { ...@@ -44,11 +44,11 @@ public interface Readable {
* rewinding of the buffer is performed. * rewinding of the buffer is performed.
* *
* @param cb the buffer to read characters into * @param cb the buffer to read characters into
* @return @return The number of <tt>char</tt> values added to the buffer, * @return The number of {@code char} values added to the buffer,
* or -1 if this source of characters is at its end * or -1 if this source of characters is at its end
* @throws IOException if an I/O error occurs * @throws IOException if an I/O error occurs
* @throws NullPointerException if cb is null * @throws NullPointerException if cb is null
* @throws ReadOnlyBufferException if cb is a read only buffer * @throws java.nio.ReadOnlyBufferException if cb is a read only buffer
*/ */
public int read(java.nio.CharBuffer cb) throws IOException; public int read(java.nio.CharBuffer cb) throws IOException;
......
...@@ -126,10 +126,8 @@ public class LinkedBlockingDeque<E> ...@@ -126,10 +126,8 @@ public class LinkedBlockingDeque<E>
*/ */
Node<E> next; Node<E> next;
Node(E x, Node<E> p, Node<E> n) { Node(E x) {
item = x; item = x;
prev = p;
next = n;
} }
} }
...@@ -199,7 +197,7 @@ public class LinkedBlockingDeque<E> ...@@ -199,7 +197,7 @@ public class LinkedBlockingDeque<E>
for (E e : c) { for (E e : c) {
if (e == null) if (e == null)
throw new NullPointerException(); throw new NullPointerException();
if (!linkLast(e)) if (!linkLast(new Node<E>(e)))
throw new IllegalStateException("Deque full"); throw new IllegalStateException("Deque full");
} }
} finally { } finally {
...@@ -211,38 +209,38 @@ public class LinkedBlockingDeque<E> ...@@ -211,38 +209,38 @@ public class LinkedBlockingDeque<E>
// Basic linking and unlinking operations, called only while holding lock // Basic linking and unlinking operations, called only while holding lock
/** /**
* Links e as first element, or returns false if full. * Links node as first element, or returns false if full.
*/ */
private boolean linkFirst(E e) { private boolean linkFirst(Node<E> node) {
// assert lock.isHeldByCurrentThread(); // assert lock.isHeldByCurrentThread();
if (count >= capacity) if (count >= capacity)
return false; return false;
Node<E> f = first; Node<E> f = first;
Node<E> x = new Node<E>(e, null, f); node.next = f;
first = x; first = node;
if (last == null) if (last == null)
last = x; last = node;
else else
f.prev = x; f.prev = node;
++count; ++count;
notEmpty.signal(); notEmpty.signal();
return true; return true;
} }
/** /**
* Links e as last element, or returns false if full. * Links node as last element, or returns false if full.
*/ */
private boolean linkLast(E e) { private boolean linkLast(Node<E> node) {
// assert lock.isHeldByCurrentThread(); // assert lock.isHeldByCurrentThread();
if (count >= capacity) if (count >= capacity)
return false; return false;
Node<E> l = last; Node<E> l = last;
Node<E> x = new Node<E>(e, l, null); node.prev = l;
last = x; last = node;
if (first == null) if (first == null)
first = x; first = node;
else else
l.next = x; l.next = node;
++count; ++count;
notEmpty.signal(); notEmpty.signal();
return true; return true;
...@@ -339,10 +337,11 @@ public class LinkedBlockingDeque<E> ...@@ -339,10 +337,11 @@ public class LinkedBlockingDeque<E>
*/ */
public boolean offerFirst(E e) { public boolean offerFirst(E e) {
if (e == null) throw new NullPointerException(); if (e == null) throw new NullPointerException();
Node<E> node = new Node<E>(e);
final ReentrantLock lock = this.lock; final ReentrantLock lock = this.lock;
lock.lock(); lock.lock();
try { try {
return linkFirst(e); return linkFirst(node);
} finally { } finally {
lock.unlock(); lock.unlock();
} }
...@@ -353,10 +352,11 @@ public class LinkedBlockingDeque<E> ...@@ -353,10 +352,11 @@ public class LinkedBlockingDeque<E>
*/ */
public boolean offerLast(E e) { public boolean offerLast(E e) {
if (e == null) throw new NullPointerException(); if (e == null) throw new NullPointerException();
Node<E> node = new Node<E>(e);
final ReentrantLock lock = this.lock; final ReentrantLock lock = this.lock;
lock.lock(); lock.lock();
try { try {
return linkLast(e); return linkLast(node);
} finally { } finally {
lock.unlock(); lock.unlock();
} }
...@@ -368,10 +368,11 @@ public class LinkedBlockingDeque<E> ...@@ -368,10 +368,11 @@ public class LinkedBlockingDeque<E>
*/ */
public void putFirst(E e) throws InterruptedException { public void putFirst(E e) throws InterruptedException {
if (e == null) throw new NullPointerException(); if (e == null) throw new NullPointerException();
Node<E> node = new Node<E>(e);
final ReentrantLock lock = this.lock; final ReentrantLock lock = this.lock;
lock.lock(); lock.lock();
try { try {
while (!linkFirst(e)) while (!linkFirst(node))
notFull.await(); notFull.await();
} finally { } finally {
lock.unlock(); lock.unlock();
...@@ -384,10 +385,11 @@ public class LinkedBlockingDeque<E> ...@@ -384,10 +385,11 @@ public class LinkedBlockingDeque<E>
*/ */
public void putLast(E e) throws InterruptedException { public void putLast(E e) throws InterruptedException {
if (e == null) throw new NullPointerException(); if (e == null) throw new NullPointerException();
Node<E> node = new Node<E>(e);
final ReentrantLock lock = this.lock; final ReentrantLock lock = this.lock;
lock.lock(); lock.lock();
try { try {
while (!linkLast(e)) while (!linkLast(node))
notFull.await(); notFull.await();
} finally { } finally {
lock.unlock(); lock.unlock();
...@@ -401,11 +403,12 @@ public class LinkedBlockingDeque<E> ...@@ -401,11 +403,12 @@ public class LinkedBlockingDeque<E>
public boolean offerFirst(E e, long timeout, TimeUnit unit) public boolean offerFirst(E e, long timeout, TimeUnit unit)
throws InterruptedException { throws InterruptedException {
if (e == null) throw new NullPointerException(); if (e == null) throw new NullPointerException();
Node<E> node = new Node<E>(e);
long nanos = unit.toNanos(timeout); long nanos = unit.toNanos(timeout);
final ReentrantLock lock = this.lock; final ReentrantLock lock = this.lock;
lock.lockInterruptibly(); lock.lockInterruptibly();
try { try {
while (!linkFirst(e)) { while (!linkFirst(node)) {
if (nanos <= 0) if (nanos <= 0)
return false; return false;
nanos = notFull.awaitNanos(nanos); nanos = notFull.awaitNanos(nanos);
...@@ -423,11 +426,12 @@ public class LinkedBlockingDeque<E> ...@@ -423,11 +426,12 @@ public class LinkedBlockingDeque<E>
public boolean offerLast(E e, long timeout, TimeUnit unit) public boolean offerLast(E e, long timeout, TimeUnit unit)
throws InterruptedException { throws InterruptedException {
if (e == null) throw new NullPointerException(); if (e == null) throw new NullPointerException();
Node<E> node = new Node<E>(e);
long nanos = unit.toNanos(timeout); long nanos = unit.toNanos(timeout);
final ReentrantLock lock = this.lock; final ReentrantLock lock = this.lock;
lock.lockInterruptibly(); lock.lockInterruptibly();
try { try {
while (!linkLast(e)) { while (!linkLast(node)) {
if (nanos <= 0) if (nanos <= 0)
return false; return false;
nanos = notFull.awaitNanos(nanos); nanos = notFull.awaitNanos(nanos);
...@@ -955,7 +959,20 @@ public class LinkedBlockingDeque<E> ...@@ -955,7 +959,20 @@ public class LinkedBlockingDeque<E>
final ReentrantLock lock = this.lock; final ReentrantLock lock = this.lock;
lock.lock(); lock.lock();
try { try {
return super.toString(); Node<E> p = first;
if (p == null)
return "[]";
StringBuilder sb = new StringBuilder();
sb.append('[');
for (;;) {
E e = p.item;
sb.append(e == this ? "(this Collection)" : e);
p = p.next;
if (p == null)
return sb.append(']').toString();
sb.append(',').append(' ');
}
} finally { } finally {
lock.unlock(); lock.unlock();
} }
...@@ -1053,6 +1070,26 @@ public class LinkedBlockingDeque<E> ...@@ -1053,6 +1070,26 @@ public class LinkedBlockingDeque<E>
} }
} }
/**
* Returns the successor node of the given non-null, but
* possibly previously deleted, node.
*/
private Node<E> succ(Node<E> n) {
// Chains of deleted nodes ending in null or self-links
// are possible if multiple interior nodes are removed.
for (;;) {
Node<E> s = nextNode(n);
if (s == null)
return null;
else if (s.item != null)
return s;
else if (s == n)
return firstNode();
else
n = s;
}
}
/** /**
* Advances next. * Advances next.
*/ */
...@@ -1061,16 +1098,7 @@ public class LinkedBlockingDeque<E> ...@@ -1061,16 +1098,7 @@ public class LinkedBlockingDeque<E>
lock.lock(); lock.lock();
try { try {
// assert next != null; // assert next != null;
Node<E> s = nextNode(next); next = succ(next);
if (s == next) {
next = firstNode();
} else {
// Skip over removed nodes.
// May be necessary if multiple interior Nodes are removed.
while (s != null && s.item == null)
s = nextNode(s);
next = s;
}
nextItem = (next == null) ? null : next.item; nextItem = (next == null) ? null : next.item;
} finally { } finally {
lock.unlock(); lock.unlock();
......
...@@ -28,6 +28,7 @@ package java.util.jar; ...@@ -28,6 +28,7 @@ package java.util.jar;
import java.util.zip.*; import java.util.zip.*;
import java.io.*; import java.io.*;
import sun.security.util.ManifestEntryVerifier; import sun.security.util.ManifestEntryVerifier;
import sun.misc.JarIndex;
/** /**
* The <code>JarInputStream</code> class is used to read the contents of * The <code>JarInputStream</code> class is used to read the contents of
...@@ -47,7 +48,8 @@ class JarInputStream extends ZipInputStream { ...@@ -47,7 +48,8 @@ class JarInputStream extends ZipInputStream {
private JarEntry first; private JarEntry first;
private JarVerifier jv; private JarVerifier jv;
private ManifestEntryVerifier mev; private ManifestEntryVerifier mev;
private final boolean doVerify;
private boolean tryManifest;
/** /**
* Creates a new <code>JarInputStream</code> and reads the optional * Creates a new <code>JarInputStream</code> and reads the optional
...@@ -72,25 +74,33 @@ class JarInputStream extends ZipInputStream { ...@@ -72,25 +74,33 @@ class JarInputStream extends ZipInputStream {
*/ */
public JarInputStream(InputStream in, boolean verify) throws IOException { public JarInputStream(InputStream in, boolean verify) throws IOException {
super(in); super(in);
JarEntry e = (JarEntry)super.getNextEntry(); this.doVerify = verify;
// This implementation assumes the META-INF/MANIFEST.MF entry
// should be either the first or the second entry (when preceded
// by the dir META-INF/). It skips the META-INF/ and then
// "consumes" the MANIFEST.MF to initialize the Manifest object.
JarEntry e = (JarEntry)super.getNextEntry();
if (e != null && e.getName().equalsIgnoreCase("META-INF/")) if (e != null && e.getName().equalsIgnoreCase("META-INF/"))
e = (JarEntry)super.getNextEntry(); e = (JarEntry)super.getNextEntry();
first = checkManifest(e);
}
private JarEntry checkManifest(JarEntry e)
throws IOException
{
if (e != null && JarFile.MANIFEST_NAME.equalsIgnoreCase(e.getName())) { if (e != null && JarFile.MANIFEST_NAME.equalsIgnoreCase(e.getName())) {
man = new Manifest(); man = new Manifest();
byte bytes[] = getBytes(new BufferedInputStream(this)); byte bytes[] = getBytes(new BufferedInputStream(this));
man.read(new ByteArrayInputStream(bytes)); man.read(new ByteArrayInputStream(bytes));
//man.read(new BufferedInputStream(this));
closeEntry(); closeEntry();
if (verify) { if (doVerify) {
jv = new JarVerifier(bytes); jv = new JarVerifier(bytes);
mev = new ManifestEntryVerifier(man); mev = new ManifestEntryVerifier(man);
} }
first = getNextJarEntry(); return (JarEntry)super.getNextEntry();
} else {
first = e;
} }
return e;
} }
private byte[] getBytes(InputStream is) private byte[] getBytes(InputStream is)
...@@ -98,10 +108,7 @@ class JarInputStream extends ZipInputStream { ...@@ -98,10 +108,7 @@ class JarInputStream extends ZipInputStream {
{ {
byte[] buffer = new byte[8192]; byte[] buffer = new byte[8192];
ByteArrayOutputStream baos = new ByteArrayOutputStream(2048); ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);
int n; int n;
baos.reset();
while ((n = is.read(buffer, 0, buffer.length)) != -1) { while ((n = is.read(buffer, 0, buffer.length)) != -1) {
baos.write(buffer, 0, n); baos.write(buffer, 0, n);
} }
...@@ -133,8 +140,14 @@ class JarInputStream extends ZipInputStream { ...@@ -133,8 +140,14 @@ class JarInputStream extends ZipInputStream {
JarEntry e; JarEntry e;
if (first == null) { if (first == null) {
e = (JarEntry)super.getNextEntry(); e = (JarEntry)super.getNextEntry();
if (tryManifest) {
e = checkManifest(e);
tryManifest = false;
}
} else { } else {
e = first; e = first;
if (first.getName().equalsIgnoreCase(JarIndex.INDEX_NAME))
tryManifest = true;
first = null; first = null;
} }
if (jv != null && e != null) { if (jv != null && e != null) {
......
...@@ -74,7 +74,7 @@ final class P11Cipher extends CipherSpi { ...@@ -74,7 +74,7 @@ final class P11Cipher extends CipherSpi {
// DEC: return the length of trailing padding bytes given the specified // DEC: return the length of trailing padding bytes given the specified
// padded data // padded data
int unpad(byte[] paddedData, int len) int unpad(byte[] paddedData, int len)
throws BadPaddingException; throws BadPaddingException, IllegalBlockSizeException;
} }
private static class PKCS5Padding implements Padding { private static class PKCS5Padding implements Padding {
...@@ -96,9 +96,10 @@ final class P11Cipher extends CipherSpi { ...@@ -96,9 +96,10 @@ final class P11Cipher extends CipherSpi {
} }
public int unpad(byte[] paddedData, int len) public int unpad(byte[] paddedData, int len)
throws BadPaddingException { throws BadPaddingException, IllegalBlockSizeException {
if (len < 1 || len > paddedData.length) { if ((len < 1) || (len % blockSize != 0)) {
throw new BadPaddingException("Invalid pad array length!"); throw new IllegalBlockSizeException
("Input length must be multiples of " + blockSize);
} }
byte padValue = paddedData[len - 1]; byte padValue = paddedData[len - 1];
if (padValue < 1 || padValue > blockSize) { if (padValue < 1 || padValue > blockSize) {
......
...@@ -75,9 +75,15 @@ public class Demo { ...@@ -75,9 +75,15 @@ public class Demo {
// copy an external src file into zipfile // copy an external src file into zipfile
// as entry dst // as entry dst
copyin_attrs, // <java Demo copyin_attrs zipfile src dst>
// copy an external src file into zipfile
// as entry dst, with attributes (timestamp)
copyout, // <java Demo copyout zipfile src dst> copyout, // <java Demo copyout zipfile src dst>
// copy zipfile entry src" out to file dst // copy zipfile entry src" out to file dst
copyout_attrs, // <java Demo copyout_attrs zipfile src dst>
zzmove, // <java Demo zzmove zfsrc zfdst path> zzmove, // <java Demo zzmove zfsrc zfdst path>
// move entry path/dir from zfsrc to zfdst // move entry path/dir from zfsrc to zfdst
...@@ -94,6 +100,9 @@ public class Demo { ...@@ -94,6 +100,9 @@ public class Demo {
setmtime, // <java Demo setmtime zipfile "MM/dd/yy-HH:mm:ss" path...> setmtime, // <java Demo setmtime zipfile "MM/dd/yy-HH:mm:ss" path...>
// set the lastModifiedTime of entry path // set the lastModifiedTime of entry path
setatime, // <java Demo setatime zipfile "MM/dd/yy-HH:mm:ss" path...>
setctime, // <java Demo setctime zipfile "MM/dd/yy-HH:mm:ss" path...>
lsdir, // <java Demo lsdir zipfile dir> lsdir, // <java Demo lsdir zipfile dir>
// list dir's direct child files/dirs // list dir's direct child files/dirs
...@@ -135,12 +144,14 @@ public class Demo { ...@@ -135,12 +144,14 @@ public class Demo {
attrs2, // <java Demo attrs2 zipfile file [...]> attrs2, // <java Demo attrs2 zipfile file [...]>
// test different ways to print attrs // test different ways to print attrs
prof,
} }
public static void main(String[] args) throws Throwable { public static void main(String[] args) throws Throwable {
Action action = Action.valueOf(args[0]);; Action action = Action.valueOf(args[0]);
Map<String, Object> env = env = new HashMap<String, Object>(); Map<String, Object> env = env = new HashMap<>();
if (action == Action.create) if (action == Action.create)
env.put("createNew", true); env.put("createNew", true);
if (action == Action.tlist || action == Action.twalk) if (action == Action.tlist || action == Action.twalk)
...@@ -185,6 +196,16 @@ public class Demo { ...@@ -185,6 +196,16 @@ public class Demo {
dst = fs.getPath(args[3]); dst = fs.getPath(args[3]);
src.copyTo(dst); src.copyTo(dst);
break; break;
case copyin_attrs:
src = Paths.get(args[2]);
dst = fs.getPath(args[3]);
src.copyTo(dst, COPY_ATTRIBUTES);
break;
case copyout_attrs:
src = fs.getPath(args[2]);
dst = Paths.get(args[3]);
src.copyTo(dst, COPY_ATTRIBUTES);
break;
case zzmove: case zzmove:
fs2 = FileSystems.newFileSystem( fs2 = FileSystems.newFileSystem(
URI.create("zip" + Paths.get(args[2]).toUri().toString().substring(4)), URI.create("zip" + Paths.get(args[2]).toUri().toString().substring(4)),
...@@ -206,6 +227,7 @@ public class Demo { ...@@ -206,6 +227,7 @@ public class Demo {
case attrs: case attrs:
for (int i = 2; i < args.length; i++) { for (int i = 2; i < args.length; i++) {
path = fs.getPath(args[i]); path = fs.getPath(args[i]);
System.out.println(path);
System.out.println( System.out.println(
Attributes.readBasicFileAttributes(path).toString()); Attributes.readBasicFileAttributes(path).toString());
} }
...@@ -221,6 +243,28 @@ public class Demo { ...@@ -221,6 +243,28 @@ public class Demo {
Attributes.readBasicFileAttributes(path).toString()); Attributes.readBasicFileAttributes(path).toString());
} }
break; break;
case setctime:
df = new SimpleDateFormat("MM/dd/yyyy-HH:mm:ss");
newDatetime = df.parse(args[2]);
for (int i = 3; i < args.length; i++) {
path = fs.getPath(args[i]);
path.setAttribute("creationTime",
FileTime.fromMillis(newDatetime.getTime()));
System.out.println(
Attributes.readBasicFileAttributes(path).toString());
}
break;
case setatime:
df = new SimpleDateFormat("MM/dd/yyyy-HH:mm:ss");
newDatetime = df.parse(args[2]);
for (int i = 3; i < args.length; i++) {
path = fs.getPath(args[i]);
path.setAttribute("lastAccessTime",
FileTime.fromMillis(newDatetime.getTime()));
System.out.println(
Attributes.readBasicFileAttributes(path).toString());
}
break;
case attrsspace: case attrsspace:
path = fs.getPath("/"); path = fs.getPath("/");
FileStore fstore = path.getFileStore(); FileStore fstore = path.getFileStore();
...@@ -293,6 +337,7 @@ public class Demo { ...@@ -293,6 +337,7 @@ public class Demo {
case attrs2: case attrs2:
for (int i = 2; i < args.length; i++) { for (int i = 2; i < args.length; i++) {
path = fs.getPath(args[i]); path = fs.getPath(args[i]);
System.out.printf("%n%s%n", path);
System.out.println("-------(1)---------"); System.out.println("-------(1)---------");
System.out.println( System.out.println(
Attributes.readBasicFileAttributes(path).toString()); Attributes.readBasicFileAttributes(path).toString());
...@@ -308,6 +353,13 @@ public class Demo { ...@@ -308,6 +353,13 @@ public class Demo {
} }
} }
break; break;
case prof:
list(fs.getPath("/"), false);
while (true) {
Thread.sleep(10000);
//list(fs.getPath("/"), true);
System.out.println("sleeping...");
}
} }
} catch (Exception x) { } catch (Exception x) {
x.printStackTrace(); x.printStackTrace();
...@@ -501,10 +553,11 @@ public class Demo { ...@@ -501,10 +553,11 @@ public class Demo {
} }
private static void list(Path path, boolean verbose ) throws IOException { private static void list(Path path, boolean verbose ) throws IOException {
if (!"/".equals(path.toString())) {
System.out.printf(" %s%n", path.toString());
if (verbose) if (verbose)
System.out.println(Attributes.readBasicFileAttributes(path).toString()); System.out.println(Attributes.readBasicFileAttributes(path).toString());
else }
System.out.printf(" %s%n", path.toString());
if (path.notExists()) if (path.notExists())
return; return;
if (Attributes.readBasicFileAttributes(path).isDirectory()) { if (Attributes.readBasicFileAttributes(path).isDirectory()) {
......
...@@ -2,7 +2,7 @@ ZipFileSystem is a file system provider that treats the contents of a zip or ...@@ -2,7 +2,7 @@ ZipFileSystem is a file system provider that treats the contents of a zip or
JAR file as a java.nio.file.FileSystem. JAR file as a java.nio.file.FileSystem.
To deploy the provider you must copy zipfs.jar into your extensions To deploy the provider you must copy zipfs.jar into your extensions
directory or else add <JDK_HOME>/demo/nio/ZipFileSystem/zipfs.jar directory or else add <JDK_HOME>/demo/nio/zipfs/zipfs.jar
to your class path. to your class path.
The factory methods defined by the java.nio.file.FileSystems class can be The factory methods defined by the java.nio.file.FileSystems class can be
...@@ -10,8 +10,8 @@ used to create a FileSystem, eg: ...@@ -10,8 +10,8 @@ used to create a FileSystem, eg:
// use file type detection // use file type detection
Map<String,?> env = Collections.emptyMap(); Map<String,?> env = Collections.emptyMap();
Path jarfile = Path.get("foo.jar"); Path jarfile = Paths.get("foo.jar");
FileSystem fs = FileSystems.newFileSystem(jarfile, env); FileSystem fs = FileSystems.newFileSystem(jarfile, env, null);
-or -or
......
...@@ -68,4 +68,21 @@ public class JarFileSystemProvider extends ZipFileSystemProvider ...@@ -68,4 +68,21 @@ public class JarFileSystemProvider extends ZipFileSystemProvider
throw new AssertionError(e); //never thrown throw new AssertionError(e); //never thrown
} }
} }
@Override
public Path getPath(URI uri) {
FileSystem fs = getFileSystem(uri);
String path = uri.getFragment();
if (path == null) {
String uristr = uri.toString();
int off = uristr.indexOf("!/");
if (off != -1)
path = uristr.substring(off + 2);
}
if (path != null)
return fs.getPath(path);
throw new IllegalArgumentException("URI: "
+ uri
+ " does not contain path fragment ex. jar:///c:/foo.zip!/BAR");
}
} }
...@@ -31,7 +31,6 @@ ...@@ -31,7 +31,6 @@
package com.sun.nio.zipfs; package com.sun.nio.zipfs;
import java.nio.ByteBuffer;
/** /**
* *
...@@ -48,6 +47,7 @@ class ZipConstants { ...@@ -48,6 +47,7 @@ class ZipConstants {
static final int METHOD_BZIP2 = 12; static final int METHOD_BZIP2 = 12;
static final int METHOD_LZMA = 14; static final int METHOD_LZMA = 14;
static final int METHOD_LZ77 = 19; static final int METHOD_LZ77 = 19;
static final int METHOD_AES = 99;
/* /*
* General purpose big flag * General purpose big flag
...@@ -168,7 +168,8 @@ class ZipConstants { ...@@ -168,7 +168,8 @@ class ZipConstants {
static final int EXTID_ZIP64 = 0x0001; // ZIP64 static final int EXTID_ZIP64 = 0x0001; // ZIP64
static final int EXTID_NTFS = 0x000a; // NTFS static final int EXTID_NTFS = 0x000a; // NTFS
static final int EXTID_UNIX = 0x000d; // UNIX static final int EXTID_UNIX = 0x000d; // UNIX
static final int EXTID_EFS = 0x0017; // Strong Encryption
static final int EXTID_EXTT = 0x5455; // Info-ZIP Extended Timestamp
/* /*
* fields access methods * fields access methods
...@@ -226,34 +227,23 @@ class ZipConstants { ...@@ -226,34 +227,23 @@ class ZipConstants {
static final long ZIP64_ENDOFF(byte[] b) { return LL(b, 48);} // central directory offset static final long ZIP64_ENDOFF(byte[] b) { return LL(b, 48);} // central directory offset
static final long ZIP64_LOCOFF(byte[] b) { return LL(b, 8);} // zip64 end offset static final long ZIP64_LOCOFF(byte[] b) { return LL(b, 8);} // zip64 end offset
////////////////////////////////////////// // central directory header (CEN) fields
static final int CH(ByteBuffer b, int pos) { static final long CENSIG(byte[] b, int pos) { return LG(b, pos + 0); }
return b.get(pos) & 0xff; static final int CENVEM(byte[] b, int pos) { return SH(b, pos + 4); }
} static final int CENVER(byte[] b, int pos) { return SH(b, pos + 6); }
static final int SH(ByteBuffer b, int pos) { static final int CENFLG(byte[] b, int pos) { return SH(b, pos + 8); }
return b.getShort(pos) & 0xffff; static final int CENHOW(byte[] b, int pos) { return SH(b, pos + 10);}
} static final long CENTIM(byte[] b, int pos) { return LG(b, pos + 12);}
static final long LG(ByteBuffer b, int pos) { static final long CENCRC(byte[] b, int pos) { return LG(b, pos + 16);}
return b.getInt(pos) & 0xffffffffL; static final long CENSIZ(byte[] b, int pos) { return LG(b, pos + 20);}
} static final long CENLEN(byte[] b, int pos) { return LG(b, pos + 24);}
static final int CENNAM(byte[] b, int pos) { return SH(b, pos + 28);}
// central directory header (END) fields static final int CENEXT(byte[] b, int pos) { return SH(b, pos + 30);}
static final long CENSIG(ByteBuffer b, int pos) { return LG(b, pos + 0); } static final int CENCOM(byte[] b, int pos) { return SH(b, pos + 32);}
static final int CENVEM(ByteBuffer b, int pos) { return SH(b, pos + 4); } static final int CENDSK(byte[] b, int pos) { return SH(b, pos + 34);}
static final int CENVER(ByteBuffer b, int pos) { return SH(b, pos + 6); } static final int CENATT(byte[] b, int pos) { return SH(b, pos + 36);}
static final int CENFLG(ByteBuffer b, int pos) { return SH(b, pos + 8); } static final long CENATX(byte[] b, int pos) { return LG(b, pos + 38);}
static final int CENHOW(ByteBuffer b, int pos) { return SH(b, pos + 10);} static final long CENOFF(byte[] b, int pos) { return LG(b, pos + 42);}
static final long CENTIM(ByteBuffer b, int pos) { return LG(b, pos + 12);}
static final long CENCRC(ByteBuffer b, int pos) { return LG(b, pos + 16);}
static final long CENSIZ(ByteBuffer b, int pos) { return LG(b, pos + 20);}
static final long CENLEN(ByteBuffer b, int pos) { return LG(b, pos + 24);}
static final int CENNAM(ByteBuffer b, int pos) { return SH(b, pos + 28);}
static final int CENEXT(ByteBuffer b, int pos) { return SH(b, pos + 30);}
static final int CENCOM(ByteBuffer b, int pos) { return SH(b, pos + 32);}
static final int CENDSK(ByteBuffer b, int pos) { return SH(b, pos + 34);}
static final int CENATT(ByteBuffer b, int pos) { return SH(b, pos + 36);}
static final long CENATX(ByteBuffer b, int pos) { return LG(b, pos + 38);}
static final long CENOFF(ByteBuffer b, int pos) { return LG(b, pos + 42);}
/* The END header is followed by a variable length comment of size < 64k. */ /* The END header is followed by a variable length comment of size < 64k. */
static final long END_MAXLEN = 0xFFFF + ENDHDR; static final long END_MAXLEN = 0xFFFF + ENDHDR;
......
...@@ -38,7 +38,6 @@ import java.nio.file.Path; ...@@ -38,7 +38,6 @@ import java.nio.file.Path;
import java.util.Iterator; import java.util.Iterator;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.io.IOException; import java.io.IOException;
import static com.sun.nio.zipfs.ZipUtils.*;
/** /**
* *
...@@ -77,7 +76,7 @@ public class ZipDirectoryStream implements DirectoryStream<Path> { ...@@ -77,7 +76,7 @@ public class ZipDirectoryStream implements DirectoryStream<Path> {
} catch (IOException e) { } catch (IOException e) {
throw new IllegalStateException(e); throw new IllegalStateException(e);
} }
return new Iterator<Path>() { return new Iterator<>() {
private Path next; private Path next;
@Override @Override
public boolean hasNext() { public boolean hasNext() {
......
...@@ -32,7 +32,6 @@ ...@@ -32,7 +32,6 @@
package com.sun.nio.zipfs; package com.sun.nio.zipfs;
import java.nio.file.ReadOnlyFileSystemException;
import java.nio.file.attribute.BasicFileAttributeView; import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.FileAttributeView; import java.nio.file.attribute.FileAttributeView;
import java.nio.file.attribute.FileTime; import java.nio.file.attribute.FileTime;
...@@ -113,6 +112,10 @@ public class ZipFileAttributeView implements BasicFileAttributeView ...@@ -113,6 +112,10 @@ public class ZipFileAttributeView implements BasicFileAttributeView
try { try {
if (AttrID.valueOf(attribute) == AttrID.lastModifiedTime) if (AttrID.valueOf(attribute) == AttrID.lastModifiedTime)
setTimes ((FileTime)value, null, null); setTimes ((FileTime)value, null, null);
if (AttrID.valueOf(attribute) == AttrID.lastAccessTime)
setTimes (null, (FileTime)value, null);
if (AttrID.valueOf(attribute) == AttrID.creationTime)
setTimes (null, null, (FileTime)value);
return; return;
} catch (IllegalArgumentException x) {} } catch (IllegalArgumentException x) {}
throw new UnsupportedOperationException("'" + attribute + throw new UnsupportedOperationException("'" + attribute +
......
...@@ -56,7 +56,7 @@ public class ZipFileAttributes implements BasicFileAttributes ...@@ -56,7 +56,7 @@ public class ZipFileAttributes implements BasicFileAttributes
@Override @Override
public FileTime creationTime() { public FileTime creationTime() {
if (e.ctime != -1) if (e.ctime != -1)
return FileTime.fromMillis(dosToJavaTime(e.ctime)); return FileTime.fromMillis(e.ctime);
return null; return null;
} }
...@@ -78,13 +78,13 @@ public class ZipFileAttributes implements BasicFileAttributes ...@@ -78,13 +78,13 @@ public class ZipFileAttributes implements BasicFileAttributes
@Override @Override
public FileTime lastAccessTime() { public FileTime lastAccessTime() {
if (e.atime != -1) if (e.atime != -1)
return FileTime.fromMillis(dosToJavaTime(e.atime)); return FileTime.fromMillis(e.atime);
return null; return null;
} }
@Override @Override
public FileTime lastModifiedTime() { public FileTime lastModifiedTime() {
return FileTime.fromMillis(dosToJavaTime(e.mtime)); return FileTime.fromMillis(e.mtime);
} }
@Override @Override
...@@ -103,10 +103,6 @@ public class ZipFileAttributes implements BasicFileAttributes ...@@ -103,10 +103,6 @@ public class ZipFileAttributes implements BasicFileAttributes
} }
///////// zip entry attributes /////////// ///////// zip entry attributes ///////////
public byte[] name() {
return Arrays.copyOf(e.name, e.name.length);
}
public long compressedSize() { public long compressedSize() {
return e.csize; return e.csize;
} }
...@@ -132,10 +128,13 @@ public class ZipFileAttributes implements BasicFileAttributes ...@@ -132,10 +128,13 @@ public class ZipFileAttributes implements BasicFileAttributes
} }
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder(1024);
Formatter fm = new Formatter(sb); Formatter fm = new Formatter(sb);
fm.format("[/%s]%n", new String(e.name)); // TBD encoding if (creationTime() != null)
fm.format(" creationTime : %s%n", creationTime()); fm.format(" creationTime : %tc%n", creationTime().toMillis());
else
fm.format(" creationTime : null%n");
if (lastAccessTime() != null) if (lastAccessTime() != null)
fm.format(" lastAccessTime : %tc%n", lastAccessTime().toMillis()); fm.format(" lastAccessTime : %tc%n", lastAccessTime().toMillis());
else else
......
...@@ -35,19 +35,18 @@ import java.io.ByteArrayInputStream; ...@@ -35,19 +35,18 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.EOFException; import java.io.EOFException;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.MappedByteBuffer; import java.nio.MappedByteBuffer;
import java.nio.channels.*; import java.nio.channels.*;
import java.nio.file.*; import java.nio.file.*;
import java.nio.file.attribute.*; import java.nio.file.attribute.*;
import java.nio.file.spi.*; import java.nio.file.spi.*;
import java.net.URI;
import java.util.*; import java.util.*;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.zip.CRC32; import java.util.zip.CRC32;
import java.util.zip.Inflater; import java.util.zip.Inflater;
...@@ -76,8 +75,6 @@ public class ZipFileSystem extends FileSystem { ...@@ -76,8 +75,6 @@ public class ZipFileSystem extends FileSystem {
private final Path zfpath; private final Path zfpath;
private final ZipCoder zc; private final ZipCoder zc;
private final Object lock = new Object();
// configurable by env map // configurable by env map
private final String defaultDir; // default dir for the file system private final String defaultDir; // default dir for the file system
private final String nameEncoding; // default encoding for name/comment private final String nameEncoding; // default encoding for name/comment
...@@ -85,6 +82,8 @@ public class ZipFileSystem extends FileSystem { ...@@ -85,6 +82,8 @@ public class ZipFileSystem extends FileSystem {
private final boolean useTempFile; // use a temp file for newOS, default private final boolean useTempFile; // use a temp file for newOS, default
// is to use BAOS for better performance // is to use BAOS for better performance
private final boolean createNew; // create a new zip if not exists private final boolean createNew; // create a new zip if not exists
private static final boolean isWindows =
System.getProperty("os.name").startsWith("Windows");
ZipFileSystem(ZipFileSystemProvider provider, ZipFileSystem(ZipFileSystemProvider provider,
Path zfpath, Path zfpath,
...@@ -92,7 +91,7 @@ public class ZipFileSystem extends FileSystem { ...@@ -92,7 +91,7 @@ public class ZipFileSystem extends FileSystem {
throws IOException throws IOException
{ {
// configurable env setup // configurable env setup
this.buildDirTree = TRUE.equals(env.get("buildDirTree")); this.buildDirTree = TRUE.equals(env.get("buildDirTreea"));
this.useTempFile = TRUE.equals(env.get("useTempFile")); this.useTempFile = TRUE.equals(env.get("useTempFile"));
this.createNew = TRUE.equals(env.get("createNew")); this.createNew = TRUE.equals(env.get("createNew"));
this.nameEncoding = env.containsKey("nameEncoding") ? this.nameEncoding = env.containsKey("nameEncoding") ?
...@@ -121,7 +120,8 @@ public class ZipFileSystem extends FileSystem { ...@@ -121,7 +120,8 @@ public class ZipFileSystem extends FileSystem {
} }
this.zc = ZipCoder.get(nameEncoding); this.zc = ZipCoder.get(nameEncoding);
this.defaultdir = new ZipPath(this, getBytes(defaultDir)); this.defaultdir = new ZipPath(this, getBytes(defaultDir));
initZipFile(); this.ch = zfpath.newByteChannel(READ);
this.cen = initCEN();
} }
@Override @Override
...@@ -183,7 +183,7 @@ public class ZipFileSystem extends FileSystem { ...@@ -183,7 +183,7 @@ public class ZipFileSystem extends FileSystem {
@Override @Override
public Iterable<FileStore> getFileStores() { public Iterable<FileStore> getFileStores() {
ArrayList<FileStore> list = new ArrayList<FileStore>(1); ArrayList<FileStore> list = new ArrayList<>(1);
list.add(new ZipFileStore(new ZipPath(this, new byte[]{'/'}))); list.add(new ZipFileStore(new ZipPath(this, new byte[]{'/'})));
return list; return list;
} }
...@@ -240,19 +240,27 @@ public class ZipFileSystem extends FileSystem { ...@@ -240,19 +240,27 @@ public class ZipFileSystem extends FileSystem {
@Override @Override
public void close() throws IOException { public void close() throws IOException {
synchronized (lock) { beginWrite();
try {
if (!isOpen) if (!isOpen)
return; return;
isOpen = false; isOpen = false; // set closed
if (!streams.isEmpty()) { } finally {
synchronized(streams) { endWrite();
for (InputStream is: streams)
is.close();
} }
if (!streams.isEmpty()) { // unlock and close all remaining streams
Set<InputStream> copy = new HashSet<>(streams);
for (InputStream is: copy)
is.close();
} }
beginWrite(); // lock and sync
try {
sync(); sync();
ch.close(); ch.close(); // close the ch just in case no update
} finally { // and sync dose not close the ch
endWrite();
} }
synchronized (inflaters) { synchronized (inflaters) {
for (Inflater inf : inflaters) for (Inflater inf : inflaters)
inf.end(); inf.end();
...@@ -261,6 +269,8 @@ public class ZipFileSystem extends FileSystem { ...@@ -261,6 +269,8 @@ public class ZipFileSystem extends FileSystem {
for (Deflater def : deflaters) for (Deflater def : deflaters)
def.end(); def.end();
} }
synchronized (tmppaths) {
for (Path p: tmppaths) { for (Path p: tmppaths) {
try { try {
p.deleteIfExists(); p.deleteIfExists();
...@@ -268,36 +278,26 @@ public class ZipFileSystem extends FileSystem { ...@@ -268,36 +278,26 @@ public class ZipFileSystem extends FileSystem {
x.printStackTrace(); x.printStackTrace();
} }
} }
provider.removeFileSystem(zfpath);
}
ZipFileAttributes[] getAllAttributes() throws IOException {
ensureOpen();
int n = inodes.size();
ZipFileAttributes[] zes = new ZipFileAttributes[n];
Iterator<IndexNode> itr = inodes.values().iterator();
int i = 0;
while(itr.hasNext()) {
zes[i++] = new ZipFileAttributes(Entry.readCEN(cen, itr.next().pos));
}
return zes;
} }
provider.removeFileSystem(zfpath);
EntryName[] getEntryNames() throws IOException {
ensureOpen();
return inodes.keySet().toArray(new EntryName[0]);
} }
ZipFileAttributes getFileAttributes(byte[] path) ZipFileAttributes getFileAttributes(byte[] path)
throws IOException throws IOException
{ {
synchronized (lock) { Entry e;
Entry e = getEntry0(path); beginRead();
try {
ensureOpen();
e = getEntry0(path);
} finally {
endRead();
}
if (e == null) { if (e == null) {
if (path.length == 0) { if (path.length == 0) {
e = new Entry(new byte[0]); // root e = new Entry(new byte[0]); // root
} else if (buildDirTree) { } else if (buildDirTree) {
IndexNode inode = getDirs().get(new EntryName(path)); IndexNode inode = getDirs().get(IndexNode.keyOf(path));
if (inode == null) if (inode == null)
return null; return null;
e = new Entry(inode.name); e = new Entry(inode.name);
...@@ -307,51 +307,63 @@ public class ZipFileSystem extends FileSystem { ...@@ -307,51 +307,63 @@ public class ZipFileSystem extends FileSystem {
e.method = METHOD_STORED; // STORED for dir e.method = METHOD_STORED; // STORED for dir
BasicFileAttributes bfas = Attributes.readBasicFileAttributes(zfpath); BasicFileAttributes bfas = Attributes.readBasicFileAttributes(zfpath);
if (bfas.lastModifiedTime() != null) if (bfas.lastModifiedTime() != null)
e.mtime = javaToDosTime(bfas.lastModifiedTime().toMillis()); e.mtime = bfas.lastModifiedTime().toMillis();
if (bfas.lastAccessTime() != null) if (bfas.lastAccessTime() != null)
e.atime = javaToDosTime(bfas.lastAccessTime().toMillis()); e.atime = bfas.lastAccessTime().toMillis();
if (bfas.creationTime() != null) if (bfas.creationTime() != null)
e.ctime = javaToDosTime(bfas.creationTime().toMillis()); e.ctime = bfas.creationTime().toMillis();
} }
return new ZipFileAttributes(e); return new ZipFileAttributes(e);
} }
}
void setTimes(byte[] path, FileTime mtime, FileTime atime, FileTime ctime) void setTimes(byte[] path, FileTime mtime, FileTime atime, FileTime ctime)
throws IOException throws IOException
{ {
checkWritable(); checkWritable();
synchronized (lock) { beginWrite();
try {
ensureOpen();
Entry e = getEntry0(path); // ensureOpen checked Entry e = getEntry0(path); // ensureOpen checked
if (e == null) if (e == null)
throw new NoSuchFileException(getString(path)); throw new NoSuchFileException(getString(path));
if (e.type == Entry.CEN) if (e.type == Entry.CEN)
e.type = Entry.COPY; // copy e e.type = Entry.COPY; // copy e
if (mtime != null) if (mtime != null)
e.mtime = javaToDosTime(mtime.toMillis()); e.mtime = mtime.toMillis();
if (atime != null) if (atime != null)
e.atime = javaToDosTime(atime.toMillis()); e.atime = atime.toMillis();
if (ctime != null) if (ctime != null)
e.ctime = javaToDosTime(ctime.toMillis()); e.ctime = ctime.toMillis();
update(e); update(e);
} finally {
endWrite();
} }
} }
boolean exists(byte[] path) boolean exists(byte[] path)
throws IOException throws IOException
{ {
beginRead();
try {
ensureOpen();
return getEntry0(path) != null; return getEntry0(path) != null;
} finally {
endRead();
}
} }
boolean isDirectory(byte[] path) boolean isDirectory(byte[] path)
throws IOException throws IOException
{ {
synchronized (lock) { if (buildDirTree)
if (buildDirTree) { return getDirs().containsKey(IndexNode.keyOf(path));
return getDirs().containsKey(new EntryName(path));
} beginRead();
try {
Entry e = getEntry0(path); Entry e = getEntry0(path);
return (e != null && e.isDir()) || path.length == 0; return (e != null && e.isDir()) || path.length == 0;
} finally {
endRead();
} }
} }
...@@ -368,12 +380,14 @@ public class ZipFileSystem extends FileSystem { ...@@ -368,12 +380,14 @@ public class ZipFileSystem extends FileSystem {
DirectoryStream.Filter<? super Path> filter) DirectoryStream.Filter<? super Path> filter)
throws IOException throws IOException
{ {
synchronized (lock) { beginWrite(); // iteration of inodes needs exclusive lock
try {
ensureOpen();
if (buildDirTree) { if (buildDirTree) {
IndexNode inode = getDirs().get(new EntryName(path)); IndexNode inode = getDirs().get(IndexNode.keyOf(path));
if (inode == null) if (inode == null)
throw new NotDirectoryException(getString(path)); throw new NotDirectoryException(getString(path));
List<Path> list = new ArrayList<Path>(); List<Path> list = new ArrayList<>();
IndexNode child = inode.child; IndexNode child = inode.child;
while (child != null) { while (child != null) {
ZipPath zp = toZipPath(child.name); ZipPath zp = toZipPath(child.name);
...@@ -386,25 +400,26 @@ public class ZipFileSystem extends FileSystem { ...@@ -386,25 +400,26 @@ public class ZipFileSystem extends FileSystem {
if (!isDirectory(path)) if (!isDirectory(path))
throw new NotDirectoryException(getString(path)); throw new NotDirectoryException(getString(path));
List<Path> list = new ArrayList<Path>(); List<Path> list = new ArrayList<>();
EntryName[] entries = getEntryNames();
path = toDirectoryPath(path); path = toDirectoryPath(path);
for (EntryName en :entries) { for (IndexNode key : inodes.keySet()) {
if (!isParentOf(path, en.name)) // is "path" the parent of "name" if (!isParentOf(path, key.name)) // is "path" the parent of "name"
continue; continue;
int off = path.length; int off = path.length;
while (off < en.name.length) { while (off < key.name.length) {
if (en.name[off] == '/') if (key.name[off] == '/')
break; break;
off++; off++;
} }
if (off < (en.name.length - 1)) if (off < (key.name.length - 1))
continue; continue;
ZipPath zp = toZipPath(en.name); ZipPath zp = toZipPath(key.name);
if (filter == null || filter.accept(zp)) if (filter == null || filter.accept(zp))
list.add(zp); list.add(zp);
} }
return list.iterator(); return list.iterator();
} finally {
endWrite();
} }
} }
...@@ -413,16 +428,18 @@ public class ZipFileSystem extends FileSystem { ...@@ -413,16 +428,18 @@ public class ZipFileSystem extends FileSystem {
{ {
checkWritable(); checkWritable();
dir = toDirectoryPath(dir); dir = toDirectoryPath(dir);
synchronized (lock) { beginWrite();
try {
ensureOpen(); ensureOpen();
// pseudo root dir, or exiting dir if (dir.length == 0 || exists(dir)) // root dir, or exiting dir
if (dir.length == 0 || exists(dir))
throw new FileAlreadyExistsException(getString(dir)); throw new FileAlreadyExistsException(getString(dir));
checkParents(dir);
checkParents(dir);
Entry e = new Entry(dir, Entry.NEW); Entry e = new Entry(dir, Entry.NEW);
e.method = METHOD_STORED; // STORED for dir e.method = METHOD_STORED; // STORED for dir
update(e); update(e);
} finally {
endWrite();
} }
} }
...@@ -432,7 +449,10 @@ public class ZipFileSystem extends FileSystem { ...@@ -432,7 +449,10 @@ public class ZipFileSystem extends FileSystem {
checkWritable(); checkWritable();
if (Arrays.equals(src, dst)) if (Arrays.equals(src, dst))
return; // do nothing, src and dst are the same return; // do nothing, src and dst are the same
synchronized (lock) {
beginWrite();
try {
ensureOpen();
Entry eSrc = getEntry0(src); // ensureOpen checked Entry eSrc = getEntry0(src); // ensureOpen checked
if (eSrc == null) if (eSrc == null)
throw new NoSuchFileException(getString(src)); throw new NoSuchFileException(getString(src));
...@@ -457,11 +477,6 @@ public class ZipFileSystem extends FileSystem { ...@@ -457,11 +477,6 @@ public class ZipFileSystem extends FileSystem {
} }
Entry u = new Entry(eSrc, Entry.COPY); // copy eSrc entry Entry u = new Entry(eSrc, Entry.COPY); // copy eSrc entry
u.name = dst; // change name u.name = dst; // change name
// don't touch the "nlen and elen" here. writeLOC() always
// re-calculate from "name" and "extra" for the correct length,
// copyLOCEntry however needs the original lengths to skip the
// loc header.
// u.nlen = dst.length;
if (eSrc.type == Entry.NEW || eSrc.type == Entry.FILECH) if (eSrc.type == Entry.NEW || eSrc.type == Entry.FILECH)
{ {
u.type = eSrc.type; // make it the same type u.type = eSrc.type; // make it the same type
...@@ -475,10 +490,12 @@ public class ZipFileSystem extends FileSystem { ...@@ -475,10 +490,12 @@ public class ZipFileSystem extends FileSystem {
} }
} }
if (!hasCopyAttrs) if (!hasCopyAttrs)
u.mtime = u.atime= u.ctime = javaToDosTime(System.currentTimeMillis()); u.mtime = u.atime= u.ctime = System.currentTimeMillis();
update(u); update(u);
if (deletesrc) if (deletesrc)
updateDelete(eSrc); updateDelete(eSrc);
} finally {
endWrite();
} }
} }
...@@ -501,7 +518,9 @@ public class ZipFileSystem extends FileSystem { ...@@ -501,7 +518,9 @@ public class ZipFileSystem extends FileSystem {
if (opt == APPEND) if (opt == APPEND)
hasAppend = true; hasAppend = true;
} }
synchronized (lock) { beginRead(); // only need a readlock, the "update()" will
try { // try to obtain a writelock when the os is
ensureOpen(); // being closed.
Entry e = getEntry0(path); Entry e = getEntry0(path);
if (e != null) { if (e != null) {
if (e.isDir() || hasCreateNew) if (e.isDir() || hasCreateNew)
...@@ -520,19 +539,25 @@ public class ZipFileSystem extends FileSystem { ...@@ -520,19 +539,25 @@ public class ZipFileSystem extends FileSystem {
checkParents(path); checkParents(path);
return getOutputStream(new Entry(path, Entry.NEW)); return getOutputStream(new Entry(path, Entry.NEW));
} }
} finally {
endRead();
} }
} }
// Returns an input stream for reading the contents of the specified // Returns an input stream for reading the contents of the specified
// file entry. // file entry.
InputStream newInputStream(byte[] path) throws IOException { InputStream newInputStream(byte[] path) throws IOException {
synchronized (lock) { beginRead();
try {
ensureOpen();
Entry e = getEntry0(path); Entry e = getEntry0(path);
if (e == null) if (e == null)
throw new NoSuchFileException(getString(path)); throw new NoSuchFileException(getString(path));
if (e.isDir()) if (e.isDir())
throw new FileSystemException(getString(path), "is a directory", null); throw new FileSystemException(getString(path), "is a directory", null);
return getInputStream(e); return getInputStream(e);
} finally {
endRead();
} }
} }
...@@ -559,9 +584,11 @@ public class ZipFileSystem extends FileSystem { ...@@ -559,9 +584,11 @@ public class ZipFileSystem extends FileSystem {
if (options.contains(StandardOpenOption.WRITE) || if (options.contains(StandardOpenOption.WRITE) ||
options.contains(StandardOpenOption.APPEND)) { options.contains(StandardOpenOption.APPEND)) {
checkWritable(); checkWritable();
final WritableByteChannel wbc = Channels.newChannel(newOutputStream(path, beginRead();
options.toArray(new OpenOption[0]))); try {
long leftover = 0;; final WritableByteChannel wbc = Channels.newChannel(
newOutputStream(path, options.toArray(new OpenOption[0])));
long leftover = 0;
if (options.contains(StandardOpenOption.APPEND)) { if (options.contains(StandardOpenOption.APPEND)) {
Entry e = getEntry0(path); Entry e = getEntry0(path);
if (e != null && e.size >= 0) if (e != null && e.size >= 0)
...@@ -573,31 +600,48 @@ public class ZipFileSystem extends FileSystem { ...@@ -573,31 +600,48 @@ public class ZipFileSystem extends FileSystem {
public boolean isOpen() { public boolean isOpen() {
return wbc.isOpen(); return wbc.isOpen();
} }
public long position() throws IOException { public long position() throws IOException {
return written; return written;
} }
public SeekableByteChannel position(long pos) throws IOException {
public SeekableByteChannel position(long pos)
throws IOException
{
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
public int read(ByteBuffer dst) throws IOException { public int read(ByteBuffer dst) throws IOException {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
public SeekableByteChannel truncate(long size) throws IOException {
public SeekableByteChannel truncate(long size)
throws IOException
{
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
public int write(ByteBuffer src) throws IOException { public int write(ByteBuffer src) throws IOException {
int n = wbc.write(src); int n = wbc.write(src);
written += n; written += n;
return n; return n;
} }
public long size() throws IOException { public long size() throws IOException {
return written; return written;
} }
public void close() throws IOException { public void close() throws IOException {
wbc.close(); wbc.close();
} }
}; };
} finally {
endRead();
}
} else { } else {
beginRead();
try {
ensureOpen();
Entry e = getEntry0(path); Entry e = getEntry0(path);
if (e == null || e.isDir()) if (e == null || e.isDir())
throw new NoSuchFileException(getString(path)); throw new NoSuchFileException(getString(path));
...@@ -609,28 +653,42 @@ public class ZipFileSystem extends FileSystem { ...@@ -609,28 +653,42 @@ public class ZipFileSystem extends FileSystem {
public boolean isOpen() { public boolean isOpen() {
return rbc.isOpen(); return rbc.isOpen();
} }
public long position() throws IOException { public long position() throws IOException {
return read; return read;
} }
public SeekableByteChannel position(long pos) throws IOException {
public SeekableByteChannel position(long pos)
throws IOException
{
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
public int read(ByteBuffer dst) throws IOException { public int read(ByteBuffer dst) throws IOException {
return rbc.read(dst); return rbc.read(dst);
} }
public SeekableByteChannel truncate(long size) throws IOException {
public SeekableByteChannel truncate(long size)
throws IOException
{
throw new NonWritableChannelException(); throw new NonWritableChannelException();
} }
public int write (ByteBuffer src) throws IOException { public int write (ByteBuffer src) throws IOException {
throw new NonWritableChannelException(); throw new NonWritableChannelException();
} }
public long size() throws IOException { public long size() throws IOException {
return size; return size;
} }
public void close() throws IOException { public void close() throws IOException {
rbc.close(); rbc.close();
} }
}; };
} finally {
endRead();
}
} }
} }
...@@ -647,6 +705,9 @@ public class ZipFileSystem extends FileSystem { ...@@ -647,6 +705,9 @@ public class ZipFileSystem extends FileSystem {
checkOptions(options); checkOptions(options);
final boolean forWrite = (options.contains(StandardOpenOption.WRITE) || final boolean forWrite = (options.contains(StandardOpenOption.WRITE) ||
options.contains(StandardOpenOption.APPEND)); options.contains(StandardOpenOption.APPEND));
beginRead();
try {
ensureOpen();
Entry e = getEntry0(path); Entry e = getEntry0(path);
if (forWrite) { if (forWrite) {
checkWritable(); checkWritable();
...@@ -757,7 +818,7 @@ public class ZipFileSystem extends FileSystem { ...@@ -757,7 +818,7 @@ public class ZipFileSystem extends FileSystem {
protected void implCloseChannel() throws IOException { protected void implCloseChannel() throws IOException {
fch.close(); fch.close();
if (forWrite) { if (forWrite) {
u.mtime = javaToDosTime(System.currentTimeMillis()); u.mtime = System.currentTimeMillis();
u.size = Attributes.readBasicFileAttributes(u.file).size(); u.size = Attributes.readBasicFileAttributes(u.file).size();
update(u); update(u);
} else { } else {
...@@ -766,6 +827,9 @@ public class ZipFileSystem extends FileSystem { ...@@ -766,6 +827,9 @@ public class ZipFileSystem extends FileSystem {
} }
} }
}; };
} finally {
endRead();
}
} }
// the outstanding input streams that need to be closed // the outstanding input streams that need to be closed
...@@ -776,11 +840,9 @@ public class ZipFileSystem extends FileSystem { ...@@ -776,11 +840,9 @@ public class ZipFileSystem extends FileSystem {
// input streams are all closed by the obtainers. // input streams are all closed by the obtainers.
private Set<ExChannelCloser> exChClosers = new HashSet<>(); private Set<ExChannelCloser> exChClosers = new HashSet<>();
private Set<Path> tmppaths = new HashSet<>(); private Set<Path> tmppaths = Collections.synchronizedSet(new HashSet<Path>());
private Path getTempPathForEntry(byte[] path) throws IOException { private Path getTempPathForEntry(byte[] path) throws IOException {
Path tmpPath = createTempFileInSameDirectoryAs(zfpath); Path tmpPath = createTempFileInSameDirectoryAs(zfpath);
tmppaths.add(tmpPath);
if (path != null) { if (path != null) {
Entry e = getEntry0(path); Entry e = getEntry0(path);
if (e != null) { if (e != null) {
...@@ -805,10 +867,15 @@ public class ZipFileSystem extends FileSystem { ...@@ -805,10 +867,15 @@ public class ZipFileSystem extends FileSystem {
// check if all parents really exit. ZIP spec does not require // check if all parents really exit. ZIP spec does not require
// the existence of any "parent directory". // the existence of any "parent directory".
private void checkParents(byte[] path) throws IOException { private void checkParents(byte[] path) throws IOException {
beginRead();
try {
while ((path = getParent(path)) != null) { while ((path = getParent(path)) != null) {
if (!inodes.containsKey(new EntryName(path))) if (!inodes.containsKey(IndexNode.keyOf(path)))
throw new NoSuchFileException(getString(path)); throw new NoSuchFileException(getString(path));
} }
} finally {
endRead();
}
} }
private static byte[] getParent(byte[] path) { private static byte[] getParent(byte[] path) {
...@@ -839,25 +906,40 @@ public class ZipFileSystem extends FileSystem { ...@@ -839,25 +906,40 @@ public class ZipFileSystem extends FileSystem {
return true; return true;
} }
/////////////////////////////////////////////////////////////////// private final void beginWrite() {
private void initZipFile() throws IOException { rwlock.writeLock().lock();
ch = zfpath.newByteChannel(READ); }
initCEN();
private final void endWrite() {
rwlock.writeLock().unlock();
}
private final void beginRead() {
rwlock.readLock().lock();
}
private final void endRead() {
rwlock.readLock().unlock();
} }
///////////////////////////////////////////////////////////////////
private volatile boolean isOpen = true; private volatile boolean isOpen = true;
private SeekableByteChannel ch; // channel to the zipfile private final SeekableByteChannel ch; // channel to the zipfile
ByteBuffer cen; // CEN & ENDHDR final byte[] cen; // CEN & ENDHDR
private END end; private END end;
private long locpos; // position of first LOC header (usually 0) private long locpos; // position of first LOC header (usually 0)
// name -> pos (in cen), package private for ZipInfo private final ReadWriteLock rwlock = new ReentrantReadWriteLock();
LinkedHashMap<EntryName, IndexNode> inodes;
// name -> pos (in cen), IndexNode itself can be used as a "key"
private LinkedHashMap<IndexNode, IndexNode> inodes;
byte[] getBytes(String name) { final byte[] getBytes(String name) {
return zc.getBytes(name); return zc.getBytes(name);
} }
String getString(byte[] name) {
final String getString(byte[] name) {
return zc.toString(name); return zc.toString(name);
} }
...@@ -881,7 +963,7 @@ public class ZipFileSystem extends FileSystem { ...@@ -881,7 +963,7 @@ public class ZipFileSystem extends FileSystem {
// Reads len bytes of data from the specified offset into buf. // Reads len bytes of data from the specified offset into buf.
// Returns the total number of bytes read. // Returns the total number of bytes read.
// Each/every byte read from here (except the cen, which is mapped). // Each/every byte read from here (except the cen, which is mapped).
private long readFullyAt(byte[] buf, int off, long len, long pos) final long readFullyAt(byte[] buf, int off, long len, long pos)
throws IOException throws IOException
{ {
ByteBuffer bb = ByteBuffer.wrap(buf); ByteBuffer bb = ByteBuffer.wrap(buf);
...@@ -890,7 +972,7 @@ public class ZipFileSystem extends FileSystem { ...@@ -890,7 +972,7 @@ public class ZipFileSystem extends FileSystem {
return readFullyAt(bb, pos); return readFullyAt(bb, pos);
} }
private long readFullyAt(ByteBuffer bb, long pos) private final long readFullyAt(ByteBuffer bb, long pos)
throws IOException throws IOException
{ {
synchronized(ch) { synchronized(ch) {
...@@ -971,12 +1053,12 @@ public class ZipFileSystem extends FileSystem { ...@@ -971,12 +1053,12 @@ public class ZipFileSystem extends FileSystem {
// CEN header, otherwise returns -1 if an error occured. If zip->msg != NULL // CEN header, otherwise returns -1 if an error occured. If zip->msg != NULL
// then the error was a zip format error and zip->msg has the error text. // then the error was a zip format error and zip->msg has the error text.
// Always pass in -1 for knownTotal; it's used for a recursive call. // Always pass in -1 for knownTotal; it's used for a recursive call.
private long initCEN() throws IOException { private byte[] initCEN() throws IOException {
end = findEND(); end = findEND();
if (end.endpos == 0) { if (end.endpos == 0) {
inodes = new LinkedHashMap<EntryName, IndexNode>(10); inodes = new LinkedHashMap<>(10);
locpos = 0; locpos = 0;
return 0; // only END header present return null; // only END header present
} }
if (end.cenlen > end.endpos) if (end.cenlen > end.endpos)
zerror("invalid END header (bad central directory size)"); zerror("invalid END header (bad central directory size)");
...@@ -989,18 +1071,14 @@ public class ZipFileSystem extends FileSystem { ...@@ -989,18 +1071,14 @@ public class ZipFileSystem extends FileSystem {
zerror("invalid END header (bad central directory offset)"); zerror("invalid END header (bad central directory offset)");
// read in the CEN and END // read in the CEN and END
cen = ByteBuffer.allocate((int)(end.cenlen + ENDHDR)); byte[] cen = new byte[(int)(end.cenlen + ENDHDR)];
if (readFullyAt(cen, cenpos) != end.cenlen + ENDHDR) { if (readFullyAt(cen, 0, cen.length, cenpos) != end.cenlen + ENDHDR) {
zerror("read CEN tables failed"); zerror("read CEN tables failed");
} }
cen.order(ByteOrder.LITTLE_ENDIAN).flip();
// Iterate through the entries in the central directory // Iterate through the entries in the central directory
inodes = new LinkedHashMap<EntryName, IndexNode>(end.centot + 1); inodes = new LinkedHashMap<>(end.centot + 1);
int pos = 0; int pos = 0;
int limit = cen.remaining() - ENDHDR; int limit = cen.length - ENDHDR;
int i = 0;
byte[] bBuf = new byte[1024];
while (pos < limit) { while (pos < limit) {
if (CENSIG(cen, pos) != CENSIG) if (CENSIG(cen, pos) != CENSIG)
zerror("invalid CEN header (bad signature)"); zerror("invalid CEN header (bad signature)");
...@@ -1011,24 +1089,19 @@ public class ZipFileSystem extends FileSystem { ...@@ -1011,24 +1089,19 @@ public class ZipFileSystem extends FileSystem {
if ((CENFLG(cen, pos) & 1) != 0) if ((CENFLG(cen, pos) & 1) != 0)
zerror("invalid CEN header (encrypted entry)"); zerror("invalid CEN header (encrypted entry)");
if (method != METHOD_STORED && method != METHOD_DEFLATED) if (method != METHOD_STORED && method != METHOD_DEFLATED)
zerror("invalid CEN header (bad compression method: " + method + ")"); zerror("invalid CEN header (unsupported compression method: " + method + ")");
if (pos + CENHDR + nlen > limit) if (pos + CENHDR + nlen > limit)
zerror("invalid CEN header (bad header size)"); zerror("invalid CEN header (bad header size)");
if (bBuf.length < nlen) byte[] name = Arrays.copyOfRange(cen, pos + CENHDR, pos + CENHDR + nlen);
bBuf = new byte[nlen]; IndexNode inode = new IndexNode(name, pos);
cen.position(pos + CENHDR); inodes.put(inode, inode);
byte[] name = new byte[nlen];
cen.get(name);
inodes.put(new EntryName(name), new IndexNode(name, pos));
// skip ext and comment // skip ext and comment
cen.position(pos += (CENHDR + nlen + elen + clen)); pos += (CENHDR + nlen + elen + clen);
i++;
} }
if (cen.remaining() != ENDHDR) { if (pos + ENDHDR != cen.length) {
zerror("invalid CEN header (bad header size)"); zerror("invalid CEN header (bad header size)");
} }
dirs = null; // clear the dir map return cen;
return cenpos;
} }
private void ensureOpen() throws IOException { private void ensureOpen() throws IOException {
...@@ -1038,27 +1111,40 @@ public class ZipFileSystem extends FileSystem { ...@@ -1038,27 +1111,40 @@ public class ZipFileSystem extends FileSystem {
// Creates a new empty temporary file in the same directory as the // Creates a new empty temporary file in the same directory as the
// specified file. A variant of File.createTempFile. // specified file. A variant of File.createTempFile.
private static Path createTempFileInSameDirectoryAs(Path path) private Path createTempFileInSameDirectoryAs(Path path)
throws IOException throws IOException
{ {
Path parent = path.toAbsolutePath().getParent(); Path parent = path.toAbsolutePath().getParent();
String dir = (parent == null)? "." : parent.toString(); String dir = (parent == null)? "." : parent.toString();
return File.createTempFile("zipfstmp", null, new File(dir)).toPath(); Path tmpPath = File.createTempFile("zipfstmp", null, new File(dir)).toPath();
tmppaths.add(tmpPath);
return tmpPath;
} }
////////////////////update & sync ////////////////////////////////////// ////////////////////update & sync //////////////////////////////////////
private boolean hasUpdate = false; private boolean hasUpdate = false;
private void updateDelete(Entry e) { private void updateDelete(Entry e) {
EntryName en = new EntryName(e.name); beginWrite();
inodes.remove(en); try {
inodes.remove(IndexNode.keyOf(e.name)); //inodes.remove(e.name);
hasUpdate = true; hasUpdate = true;
dirs = null;
} finally {
endWrite();
}
} }
private void update(Entry e) { private void update(Entry e) {
EntryName en = new EntryName(e.name); beginWrite();
inodes.put(en, e); try {
inodes.put(IndexNode.keyOf(e.name), e); //inodes.put(e, e);
hasUpdate = true; hasUpdate = true;
dirs = null;
} finally {
endWrite();
}
} }
// copy over the whole LOC entry (header if necessary, data and ext) from // copy over the whole LOC entry (header if necessary, data and ext) from
...@@ -1080,13 +1166,18 @@ public class ZipFileSystem extends FileSystem { ...@@ -1080,13 +1166,18 @@ public class ZipFileSystem extends FileSystem {
else else
size = 16; size = 16;
} }
if (updateHeader) { // if we need update the loc header // read loc, use the original loc.elen/nlen
locoff += LOCHDR + e.nlen + e.elen; // skip header if (readFullyAt(buf, 0, LOCHDR , locoff) != LOCHDR)
throw new ZipException("loc: reading failed");
if (updateHeader) {
locoff += LOCHDR + LOCNAM(buf) + LOCEXT(buf); // skip header
size += e.csize; size += e.csize;
written = e.writeLOC(os) + size; written = e.writeLOC(os) + size;
} else { } else {
size += LOCHDR + e.nlen + e.elen + e.csize; os.write(buf, 0, LOCHDR); // write out the loc header
written = size; locoff += LOCHDR;
size += LOCNAM(buf) + LOCEXT(buf) + LOCSIZ(buf);
written = LOCHDR + size;
} }
int n; int n;
while (size > 0 && while (size > 0 &&
...@@ -1103,7 +1194,7 @@ public class ZipFileSystem extends FileSystem { ...@@ -1103,7 +1194,7 @@ public class ZipFileSystem extends FileSystem {
// sync the zip file system, if there is any udpate // sync the zip file system, if there is any udpate
private void sync() throws IOException { private void sync() throws IOException {
assert Thread.holdsLock(this); //System.out.printf("->sync(%s) starting....!%n", toString());
// check ex-closer // check ex-closer
if (!exChClosers.isEmpty()) { if (!exChClosers.isEmpty()) {
...@@ -1117,7 +1208,6 @@ public class ZipFileSystem extends FileSystem { ...@@ -1117,7 +1208,6 @@ public class ZipFileSystem extends FileSystem {
} }
if (!hasUpdate) if (!hasUpdate)
return; return;
Path tmpFile = createTempFileInSameDirectoryAs(zfpath); Path tmpFile = createTempFileInSameDirectoryAs(zfpath);
OutputStream os = tmpFile.newOutputStream(WRITE); OutputStream os = tmpFile.newOutputStream(WRITE);
ArrayList<Entry> elist = new ArrayList<>(inodes.size()); ArrayList<Entry> elist = new ArrayList<>(inodes.size());
...@@ -1174,7 +1264,7 @@ public class ZipFileSystem extends FileSystem { ...@@ -1174,7 +1264,7 @@ public class ZipFileSystem extends FileSystem {
x.printStackTrace(); // skip any in-accurate entry x.printStackTrace(); // skip any in-accurate entry
} }
} else { // unchanged inode } else { // unchanged inode
e = Entry.readCEN(cen, inode.pos); e = Entry.readCEN(this, inode.pos);
try { try {
written += copyLOCEntry(e, false, os, written, buf); written += copyLOCEntry(e, false, os, written, buf);
elist.add(e); elist.add(e);
...@@ -1195,6 +1285,11 @@ public class ZipFileSystem extends FileSystem { ...@@ -1195,6 +1285,11 @@ public class ZipFileSystem extends FileSystem {
os.close(); os.close();
if (!streams.isEmpty()) { if (!streams.isEmpty()) {
//
// TBD: ExChannelCloser should not be necessary if we only
// sync when being closed, all streams should have been
// closed already. Keep the logic here for now.
//
// There are outstanding input streams open on existing "ch", // There are outstanding input streams open on existing "ch",
// so, don't close the "cha" and delete the "file for now, let // so, don't close the "cha" and delete the "file for now, let
// the "ex-channel-closer" to handle them // the "ex-channel-closer" to handle them
...@@ -1209,45 +1304,41 @@ public class ZipFileSystem extends FileSystem { ...@@ -1209,45 +1304,41 @@ public class ZipFileSystem extends FileSystem {
ch.close(); ch.close();
zfpath.delete(); zfpath.delete();
} }
tmpFile.moveTo(zfpath, REPLACE_EXISTING); tmpFile.moveTo(zfpath, REPLACE_EXISTING);
hasUpdate = false; // clear hasUpdate = false; // clear
/*
if (isOpen) { if (isOpen) {
ch = zfpath.newByteChannel(READ); // re-fresh "ch" and "cen" ch = zfpath.newByteChannel(READ); // re-fresh "ch" and "cen"
initCEN(); cen = initCEN();
} }
//System.out.println("->sync() done!"); */
//System.out.printf("->sync(%s) done!%n", toString());
} }
private Entry getEntry0(byte[] path) throws IOException { private Entry getEntry0(byte[] path) throws IOException {
assert Thread.holdsLock(this);
if (path == null) if (path == null)
throw new NullPointerException("path"); throw new NullPointerException("path");
if (path.length == 0) if (path.length == 0)
return null; return null;
EntryName en = new EntryName(path);
IndexNode inode = null; IndexNode inode = null;
synchronized (lock) { IndexNode key = IndexNode.keyOf(path);
ensureOpen(); if ((inode = inodes.get(key)) == null) {
if ((inode = inodes.get(en)) == null) {
if (path[path.length -1] == '/') // already has a slash if (path[path.length -1] == '/') // already has a slash
return null; return null;
path = Arrays.copyOf(path, path.length + 1); path = Arrays.copyOf(path, path.length + 1);
path[path.length - 1] = '/'; path[path.length - 1] = '/';
en.name(path); if ((inode = inodes.get(key.as(path))) == null)
if ((inode = inodes.get(en)) == null)
return null; return null;
} }
if (inode instanceof Entry) if (inode instanceof Entry)
return (Entry)inode; return (Entry)inode;
return Entry.readCEN(cen, inode.pos); return Entry.readCEN(this, inode.pos);
}
} }
// Test if the "name" a parent directory of any entry (dir empty) // Test if the "name" a parent directory of any entry (dir empty)
boolean isAncestor(byte[] name) { boolean isAncestor(byte[] name) {
for (Map.Entry<EntryName, IndexNode> entry : inodes.entrySet()) { for (Map.Entry<IndexNode, IndexNode> entry : inodes.entrySet()) {
byte[] ename = entry.getKey().name; byte[] ename = entry.getKey().name;
if (isParentOf(name, ename)) if (isParentOf(name, ename))
return true; return true;
...@@ -1259,7 +1350,6 @@ public class ZipFileSystem extends FileSystem { ...@@ -1259,7 +1350,6 @@ public class ZipFileSystem extends FileSystem {
throws IOException throws IOException
{ {
checkWritable(); checkWritable();
synchronized(lock) {
Entry e = getEntry0(path); Entry e = getEntry0(path);
if (e == null) { if (e == null) {
if (path != null && path.length == 0) if (path != null && path.length == 0)
...@@ -1272,7 +1362,6 @@ public class ZipFileSystem extends FileSystem { ...@@ -1272,7 +1362,6 @@ public class ZipFileSystem extends FileSystem {
updateDelete(e); updateDelete(e);
} }
} }
}
private static void copyStream(InputStream is, OutputStream os) private static void copyStream(InputStream is, OutputStream os)
throws IOException throws IOException
...@@ -1289,9 +1378,8 @@ public class ZipFileSystem extends FileSystem { ...@@ -1289,9 +1378,8 @@ public class ZipFileSystem extends FileSystem {
// (2) updating/replacing the contents of the specified existing entry. // (2) updating/replacing the contents of the specified existing entry.
private OutputStream getOutputStream(Entry e) throws IOException { private OutputStream getOutputStream(Entry e) throws IOException {
ensureOpen();
if (e.mtime == -1) if (e.mtime == -1)
e.mtime = javaToDosTime(System.currentTimeMillis()); e.mtime = System.currentTimeMillis();
if (e.method == -1) if (e.method == -1)
e.method = METHOD_DEFLATED; // TBD: use default method e.method = METHOD_DEFLATED; // TBD: use default method
// store size, compressed size, and crc-32 in LOC header // store size, compressed size, and crc-32 in LOC header
...@@ -1334,7 +1422,7 @@ public class ZipFileSystem extends FileSystem { ...@@ -1334,7 +1422,7 @@ public class ZipFileSystem extends FileSystem {
long bufSize = e.size + 2; // Inflater likes a bit of slack long bufSize = e.size + 2; // Inflater likes a bit of slack
if (bufSize > 65536) if (bufSize > 65536)
bufSize = 8192; bufSize = 8192;
final long size = e.size;; final long size = e.size;
eis = new InflaterInputStream(eis, getInflater(), (int)bufSize) { eis = new InflaterInputStream(eis, getInflater(), (int)bufSize) {
private boolean isClosed = false; private boolean isClosed = false;
...@@ -1343,6 +1431,7 @@ public class ZipFileSystem extends FileSystem { ...@@ -1343,6 +1431,7 @@ public class ZipFileSystem extends FileSystem {
releaseInflater(inf); releaseInflater(inf);
this.in.close(); this.in.close();
isClosed = true; isClosed = true;
streams.remove(this);
} }
} }
// Override fill() method to provide an extra "dummy" byte // Override fill() method to provide an extra "dummy" byte
...@@ -1372,7 +1461,9 @@ public class ZipFileSystem extends FileSystem { ...@@ -1372,7 +1461,9 @@ public class ZipFileSystem extends FileSystem {
Integer.MAX_VALUE : (int) avail; Integer.MAX_VALUE : (int) avail;
} }
}; };
} else if (e.method != METHOD_STORED) { } else if (e.method == METHOD_STORED) {
// TBD: wrap/ it does not seem necessary
} else {
throw new ZipException("invalid compression method"); throw new ZipException("invalid compression method");
} }
streams.add(eis); streams.add(eis);
...@@ -1382,11 +1473,11 @@ public class ZipFileSystem extends FileSystem { ...@@ -1382,11 +1473,11 @@ public class ZipFileSystem extends FileSystem {
// Inner class implementing the input stream used to read // Inner class implementing the input stream used to read
// a (possibly compressed) zip file entry. // a (possibly compressed) zip file entry.
private class EntryInputStream extends InputStream { private class EntryInputStream extends InputStream {
private SeekableByteChannel zfch; // local ref to zipfs's "ch". zipfs.ch might private final SeekableByteChannel zfch; // local ref to zipfs's "ch". zipfs.ch might
// point to a new channel after sync() // point to a new channel after sync()
private long pos; // current position within entry data private long pos; // current position within entry data
protected long rem; // number of remaining bytes within entry protected long rem; // number of remaining bytes within entry
protected long size; // uncompressed size of this entry protected final long size; // uncompressed size of this entry
EntryInputStream(Entry e, SeekableByteChannel zfch) EntryInputStream(Entry e, SeekableByteChannel zfch)
throws IOException throws IOException
...@@ -1527,14 +1618,14 @@ public class ZipFileSystem extends FileSystem { ...@@ -1527,14 +1618,14 @@ public class ZipFileSystem extends FileSystem {
} }
} }
private static void zerror(String msg) { static void zerror(String msg) {
throw new ZipError(msg); throw new ZipError(msg);
} }
// Maxmum number of de/inflater we cache // Maxmum number of de/inflater we cache
private final int MAX_FLATER = 20; private final int MAX_FLATER = 20;
// List of available Inflater objects for decompression // List of available Inflater objects for decompression
private List<Inflater> inflaters = new ArrayList<>(); private final List<Inflater> inflaters = new ArrayList<>();
// Gets an inflater from the list of available inflaters or allocates // Gets an inflater from the list of available inflaters or allocates
// a new one. // a new one.
...@@ -1563,7 +1654,7 @@ public class ZipFileSystem extends FileSystem { ...@@ -1563,7 +1654,7 @@ public class ZipFileSystem extends FileSystem {
} }
// List of available Deflater objects for compression // List of available Deflater objects for compression
private List<Deflater> deflaters = new ArrayList<>(); private final List<Deflater> deflaters = new ArrayList<>();
// Gets an deflater from the list of available deflaters or allocates // Gets an deflater from the list of available deflaters or allocates
// a new one. // a new one.
...@@ -1660,44 +1751,40 @@ public class ZipFileSystem extends FileSystem { ...@@ -1660,44 +1751,40 @@ public class ZipFileSystem extends FileSystem {
} }
} }
// wrapper for the byte[] name // Internal node that links a "name" to its pos in cen table.
static class EntryName { // The node itself can be used as a "key" to lookup itself in
// the HashMap inodes.
static class IndexNode {
byte[] name; byte[] name;
int hashcode; // node is hashable/hashed by its name int hashcode; // node is hashable/hashed by its name
int pos = -1; // postion in cen table, -1 menas the
// entry does not exists in zip file
IndexNode(byte[] name, int pos) {
as(name);
this.pos = pos;
}
public EntryName (byte[] name) { final static IndexNode keyOf(byte[] name) { // get a lookup key;
name(name); return new IndexNode(name, -1);
} }
void name(byte[] name) { final IndexNode as(byte[] name) { // reuse the node, mostly
this.name = name; this.name = name; // as a lookup "key"
this.hashcode = Arrays.hashCode(name); this.hashcode = Arrays.hashCode(name);
return this;
} }
public boolean equals(Object other) { public boolean equals(Object other) {
if (!(other instanceof EntryName)) if (!(other instanceof IndexNode))
return false; return false;
return Arrays.equals(name, ((EntryName)other).name); return Arrays.equals(name, ((IndexNode)other).name);
} }
public int hashCode() { public int hashCode() {
return hashcode; return hashcode;
} }
}
// can simply use Integer instead, if we don't use it to
// build a internal node tree.
static class IndexNode {
byte[] name;
int pos = -1; // postion in cen table, -1 menas the
// entry does not exists in zip file
IndexNode(byte[] name, int pos) {
this.name = name;
this.pos = pos;
}
IndexNode() {} IndexNode() {}
IndexNode sibling; IndexNode sibling;
IndexNode child; // 1st child IndexNode child; // 1st child
} }
...@@ -1723,33 +1810,21 @@ public class ZipFileSystem extends FileSystem { ...@@ -1723,33 +1810,21 @@ public class ZipFileSystem extends FileSystem {
long crc = -1; // crc-32 of entry data long crc = -1; // crc-32 of entry data
long csize = -1; // compressed size of entry data long csize = -1; // compressed size of entry data
long size = -1; // uncompressed size of entry data long size = -1; // uncompressed size of entry data
int nlen;
int elen;
byte[] extra; byte[] extra;
// loc
long startPos;
long endPos; // exclusive
// cen // cen
int versionMade; int versionMade;
int disk; int disk;
int attrs; int attrs;
long attrsEx; long attrsEx;
long locoff; long locoff;
int clen;
byte[] comment; byte[] comment;
// ZIP64 flag
boolean hasZip64;
Entry() {} Entry() {}
Entry(byte[] name) { Entry(byte[] name) {
this.name = name; this.name = name;
//this.nlen = name.length; this.mtime = System.currentTimeMillis();
this.mtime = javaToDosTime(System.currentTimeMillis());
this.crc = 0; this.crc = 0;
this.size = 0; this.size = 0;
this.csize = 0; this.csize = 0;
...@@ -1761,16 +1836,9 @@ public class ZipFileSystem extends FileSystem { ...@@ -1761,16 +1836,9 @@ public class ZipFileSystem extends FileSystem {
this.type = type; this.type = type;
} }
Entry (byte[] name, Path file, int type) { Entry (Entry e, int type) {
this(name, type);
this.file = file;
this.method = METHOD_STORED;
}
Entry(Entry e) {
this.version = e.version; this.version = e.version;
this.name = e.name; // copyOf? this.name = e.name;
this.nlen = e.nlen;
this.ctime = e.ctime; this.ctime = e.ctime;
this.atime = e.atime; this.atime = e.atime;
this.mtime = e.mtime; this.mtime = e.mtime;
...@@ -1778,27 +1846,23 @@ public class ZipFileSystem extends FileSystem { ...@@ -1778,27 +1846,23 @@ public class ZipFileSystem extends FileSystem {
this.size = e.size; this.size = e.size;
this.csize = e.csize; this.csize = e.csize;
this.method = e.method; this.method = e.method;
this.extra = (e.extra == null)? this.extra = e.extra;
null:Arrays.copyOf(e.extra, e.extra.length);
this.elen = e.elen;
this.versionMade = e.versionMade; this.versionMade = e.versionMade;
this.disk = e.disk; this.disk = e.disk;
this.attrs = e.attrs; this.attrs = e.attrs;
this.attrsEx = e.attrsEx; this.attrsEx = e.attrsEx;
this.locoff = e.locoff; this.locoff = e.locoff;
this.clen = e.clen; this.comment = e.comment;
this.comment = (e.comment == null)?
null:Arrays.copyOf(e.comment, e.comment.length);
this.startPos = e.startPos;
this.endPos = e.endPos;
this.hasZip64 = e.hasZip64;;
}
Entry (Entry e, int type) {
this(e);
this.type = type; this.type = type;
} }
Entry (byte[] name, Path file, int type) {
this(name, type);
this.file = file;
this.method = METHOD_STORED;
}
boolean isDir() { boolean isDir() {
return name != null && return name != null &&
(name.length == 0 || (name.length == 0 ||
...@@ -1814,77 +1878,45 @@ public class ZipFileSystem extends FileSystem { ...@@ -1814,77 +1878,45 @@ public class ZipFileSystem extends FileSystem {
} }
///////////////////// CEN ////////////////////// ///////////////////// CEN //////////////////////
static Entry readCEN(ByteBuffer cen, int pos) throws IOException static Entry readCEN(ZipFileSystem zipfs, int pos)
throws IOException
{ {
return new Entry().cen(cen, pos); return new Entry().cen(zipfs, pos);
} }
private Entry cen(ByteBuffer cen, int pos) throws IOException private Entry cen(ZipFileSystem zipfs, int pos)
throws IOException
{ {
byte[] cen = zipfs.cen;
if (CENSIG(cen, pos) != CENSIG) if (CENSIG(cen, pos) != CENSIG)
zerror("invalid CEN header (bad signature)"); zerror("invalid CEN header (bad signature)");
versionMade = CENVEM(cen, pos); versionMade = CENVEM(cen, pos);
version = CENVER(cen, pos); version = CENVER(cen, pos);
flag = CENFLG(cen, pos); flag = CENFLG(cen, pos);
method = CENHOW(cen, pos); method = CENHOW(cen, pos);
mtime = CENTIM(cen, pos); mtime = dosToJavaTime(CENTIM(cen, pos));
crc = CENCRC(cen, pos); crc = CENCRC(cen, pos);
csize = CENSIZ(cen, pos); csize = CENSIZ(cen, pos);
size = CENLEN(cen, pos); size = CENLEN(cen, pos);
nlen = CENNAM(cen, pos); int nlen = CENNAM(cen, pos);
elen = CENEXT(cen, pos); int elen = CENEXT(cen, pos);
clen = CENCOM(cen, pos); int clen = CENCOM(cen, pos);
disk = CENDSK(cen, pos); disk = CENDSK(cen, pos);
attrs = CENATT(cen, pos); attrs = CENATT(cen, pos);
attrsEx = CENATX(cen, pos); attrsEx = CENATX(cen, pos);
locoff = CENOFF(cen, pos); locoff = CENOFF(cen, pos);
cen.position(pos + CENHDR); pos += CENHDR;
name = new byte[nlen]; name = Arrays.copyOfRange(cen, pos, pos + nlen);
cen.get(name);
pos += nlen;
if (elen > 0) { if (elen > 0) {
extra = new byte[elen]; extra = Arrays.copyOfRange(cen, pos, pos + elen);
cen.get(extra); pos += elen;
if (csize == ZIP64_MINVAL || size == ZIP64_MINVAL || readExtra(zipfs);
locoff == ZIP64_MINVAL) {
int off = 0;
while (off + 4 < elen) {
// extra spec: HeaderID+DataSize+Data
int sz = SH(extra, off + 2);
if (SH(extra, off) == EXTID_ZIP64) {
off += 4;
if (size == ZIP64_MINVAL) {
// if invalid zip64 extra fields, just skip
if (sz < 8 || (off + 8) > elen)
break;
size = LL(extra, off);
sz -= 8;
off += 8;
}
if (csize == ZIP64_MINVAL) {
if (sz < 8 || (off + 8) > elen)
break;
csize = LL(extra, off);
sz -= 8;
off += 8;
}
if (locoff == ZIP64_MINVAL) {
if (sz < 8 || (off + 8) > elen)
break;
locoff = LL(extra, off);
sz -= 8;
off += 8;
}
break;
}
off += (sz + 4);
}
}
} }
if (clen > 0) { if (clen > 0) {
comment = new byte[clen]; comment = Arrays.copyOfRange(cen, pos, pos + clen);
cen.get(comment);
} }
return this; return this;
} }
...@@ -1897,31 +1929,37 @@ public class ZipFileSystem extends FileSystem { ...@@ -1897,31 +1929,37 @@ public class ZipFileSystem extends FileSystem {
long csize0 = csize; long csize0 = csize;
long size0 = size; long size0 = size;
long locoff0 = locoff; long locoff0 = locoff;
int e64len = 0; int elen64 = 0; // extra for ZIP64
int elenNTFS = 0; // extra for NTFS (a/c/mtime)
int elenEXTT = 0; // extra for Extended Timestamp
// confirm size/length // confirm size/length
nlen = (name != null) ? name.length : 0; int nlen = (name != null) ? name.length : 0;
elen = (extra != null) ? extra.length : 0; int elen = (extra != null) ? extra.length : 0;
clen = (comment != null) ? comment.length : 0; int clen = (comment != null) ? comment.length : 0;
boolean hasZip64 = false;
if (csize >= ZIP64_MINVAL) { if (csize >= ZIP64_MINVAL) {
csize0 = ZIP64_MINVAL; csize0 = ZIP64_MINVAL;
e64len += 8; // csize(8) elen64 += 8; // csize(8)
hasZip64 = true;
} }
if (size >= ZIP64_MINVAL) { if (size >= ZIP64_MINVAL) {
size0 = ZIP64_MINVAL; // size(8) size0 = ZIP64_MINVAL; // size(8)
e64len += 8; elen64 += 8;
hasZip64 = true;
} }
if (locoff >= ZIP64_MINVAL) { if (locoff >= ZIP64_MINVAL) {
locoff0 = ZIP64_MINVAL; locoff0 = ZIP64_MINVAL;
e64len += 8; // offset(8) elen64 += 8; // offset(8)
hasZip64 = true; }
if (elen64 != 0)
elen64 += 4; // header and data sz 4 bytes
if (atime != -1) {
if (isWindows) // use NTFS
elenNTFS = 36; // total 36 bytes
else // Extended Timestamp otherwise
elenEXTT = 9; // only mtime in cen
} }
writeInt(os, CENSIG); // CEN header signature writeInt(os, CENSIG); // CEN header signature
if (hasZip64) { if (elen64 != 0) {
writeShort(os, 45); // ver 4.5 for zip64 writeShort(os, 45); // ver 4.5 for zip64
writeShort(os, 45); writeShort(os, 45);
} else { } else {
...@@ -1930,18 +1968,14 @@ public class ZipFileSystem extends FileSystem { ...@@ -1930,18 +1968,14 @@ public class ZipFileSystem extends FileSystem {
} }
writeShort(os, flag); // general purpose bit flag writeShort(os, flag); // general purpose bit flag
writeShort(os, method); // compression method writeShort(os, method); // compression method
writeInt(os, mtime); // last modification time // last modification time
writeInt(os, (int)javaToDosTime(mtime));
writeInt(os, crc); // crc-32 writeInt(os, crc); // crc-32
writeInt(os, csize0); // compressed size writeInt(os, csize0); // compressed size
writeInt(os, size0); // uncompressed size writeInt(os, size0); // uncompressed size
writeShort(os, name.length); writeShort(os, name.length);
writeShort(os, elen + elen64 + elenNTFS + elenEXTT);
if (hasZip64) {
// + headid(2) + datasize(2)
writeShort(os, e64len + 4 + elen);
} else {
writeShort(os, elen);
}
if (comment != null) { if (comment != null) {
writeShort(os, Math.min(clen, 0xffff)); writeShort(os, Math.min(clen, 0xffff));
} else { } else {
...@@ -1952,9 +1986,9 @@ public class ZipFileSystem extends FileSystem { ...@@ -1952,9 +1986,9 @@ public class ZipFileSystem extends FileSystem {
writeInt(os, 0); // external file attributes (unused) writeInt(os, 0); // external file attributes (unused)
writeInt(os, locoff0); // relative offset of local header writeInt(os, locoff0); // relative offset of local header
writeBytes(os, name); writeBytes(os, name);
if (hasZip64) { if (elen64 != 0) {
writeShort(os, EXTID_ZIP64);// Zip64 extra writeShort(os, EXTID_ZIP64);// Zip64 extra
writeShort(os, e64len); writeShort(os, elen64); // size of "this" extra block
if (size0 == ZIP64_MINVAL) if (size0 == ZIP64_MINVAL)
writeLong(os, size); writeLong(os, size);
if (csize0 == ZIP64_MINVAL) if (csize0 == ZIP64_MINVAL)
...@@ -1962,58 +1996,73 @@ public class ZipFileSystem extends FileSystem { ...@@ -1962,58 +1996,73 @@ public class ZipFileSystem extends FileSystem {
if (locoff0 == ZIP64_MINVAL) if (locoff0 == ZIP64_MINVAL)
writeLong(os, locoff); writeLong(os, locoff);
} }
if (extra != null) { if (elenNTFS != 0) {
writeBytes(os, extra); // System.out.println("writing NTFS:" + elenNTFS);
writeShort(os, EXTID_NTFS);
writeShort(os, elenNTFS - 4);
writeInt(os, 0); // reserved
writeShort(os, 0x0001); // NTFS attr tag
writeShort(os, 24);
writeLong(os, javaToWinTime(mtime));
writeLong(os, javaToWinTime(atime));
writeLong(os, javaToWinTime(ctime));
}
if (elenEXTT != 0) {
writeShort(os, EXTID_EXTT);
writeShort(os, elenEXTT - 4);
if (ctime == -1)
os.write(0x3); // mtime and atime
else
os.write(0x7); // mtime, atime and ctime
writeInt(os, javaToUnixTime(mtime));
} }
if (comment != null) { if (extra != null) // whatever not recognized
//TBD: 0, Math.min(commentBytes.length, 0xffff)); writeBytes(os, extra);
if (comment != null) //TBD: 0, Math.min(commentBytes.length, 0xffff));
writeBytes(os, comment); writeBytes(os, comment);
} return CENHDR + nlen + elen + clen + elen64 + elenNTFS + elenEXTT;
return CENHDR + nlen + elen + clen + (hasZip64?(e64len + 4):0);
} }
///////////////////// LOC ////////////////////// ///////////////////// LOC //////////////////////
static Entry readLOC(ZipFileSystem zf, long pos) static Entry readLOC(ZipFileSystem zipfs, long pos)
throws IOException throws IOException
{ {
return readLOC(zf, pos, new byte[1024]); return readLOC(zipfs, pos, new byte[1024]);
} }
static Entry readLOC(ZipFileSystem zf, long pos, byte[] buf) static Entry readLOC(ZipFileSystem zipfs, long pos, byte[] buf)
throws IOException throws IOException
{ {
return new Entry().loc(zf, pos, buf); return new Entry().loc(zipfs, pos, buf);
} }
Entry loc(ZipFileSystem zf, long pos, byte[] buf) Entry loc(ZipFileSystem zipfs, long pos, byte[] buf)
throws IOException throws IOException
{ {
assert (buf.length >= LOCHDR); assert (buf.length >= LOCHDR);
if (zf.readFullyAt(buf, 0, LOCHDR , pos) != LOCHDR) { if (zipfs.readFullyAt(buf, 0, LOCHDR , pos) != LOCHDR)
throw new ZipException("loc: reading failed"); throw new ZipException("loc: reading failed");
} if (LOCSIG(buf) != LOCSIG)
if (LOCSIG(buf) != LOCSIG) {
throw new ZipException("loc: wrong sig ->" throw new ZipException("loc: wrong sig ->"
+ Long.toString(LOCSIG(buf), 16)); + Long.toString(LOCSIG(buf), 16));
} //startPos = pos;
startPos = pos;
version = LOCVER(buf); version = LOCVER(buf);
flag = LOCFLG(buf); flag = LOCFLG(buf);
method = LOCHOW(buf); method = LOCHOW(buf);
mtime = LOCTIM(buf); mtime = dosToJavaTime(LOCTIM(buf));
crc = LOCCRC(buf); crc = LOCCRC(buf);
csize = LOCSIZ(buf); csize = LOCSIZ(buf);
size = LOCLEN(buf); size = LOCLEN(buf);
nlen = LOCNAM(buf); int nlen = LOCNAM(buf);
elen = LOCEXT(buf); int elen = LOCEXT(buf);
name = new byte[nlen]; name = new byte[nlen];
if (zf.readFullyAt(name, 0, nlen, pos + LOCHDR) != nlen) { if (zipfs.readFullyAt(name, 0, nlen, pos + LOCHDR) != nlen) {
throw new ZipException("loc: name reading failed"); throw new ZipException("loc: name reading failed");
} }
if (elen > 0) { if (elen > 0) {
extra = new byte[elen]; extra = new byte[elen];
if (zf.readFullyAt(extra, 0, elen, pos + LOCHDR + nlen) if (zipfs.readFullyAt(extra, 0, elen, pos + LOCHDR + nlen)
!= elen) { != elen) {
throw new ZipException("loc: ext reading failed"); throw new ZipException("loc: ext reading failed");
} }
...@@ -2021,7 +2070,7 @@ public class ZipFileSystem extends FileSystem { ...@@ -2021,7 +2070,7 @@ public class ZipFileSystem extends FileSystem {
pos += (LOCHDR + nlen + elen); pos += (LOCHDR + nlen + elen);
if ((flag & FLAG_DATADESCR) != 0) { if ((flag & FLAG_DATADESCR) != 0) {
// Data Descriptor // Data Descriptor
Entry e = zf.getEntry0(name); // get the size/csize from cen Entry e = zipfs.getEntry0(name); // get the size/csize from cen
if (e == null) if (e == null)
throw new ZipException("loc: name not found in cen"); throw new ZipException("loc: name not found in cen");
size = e.size; size = e.size;
...@@ -2032,7 +2081,6 @@ public class ZipFileSystem extends FileSystem { ...@@ -2032,7 +2081,6 @@ public class ZipFileSystem extends FileSystem {
else else
pos += 16; pos += 16;
} else { } else {
boolean hasZip64 = false;
if (extra != null && if (extra != null &&
(size == ZIP64_MINVAL || csize == ZIP64_MINVAL)) { (size == ZIP64_MINVAL || csize == ZIP64_MINVAL)) {
// zip64 ext: must include both size and csize // zip64 ext: must include both size and csize
...@@ -2042,7 +2090,6 @@ public class ZipFileSystem extends FileSystem { ...@@ -2042,7 +2090,6 @@ public class ZipFileSystem extends FileSystem {
if (SH(extra, off) == EXTID_ZIP64 && sz == 16) { if (SH(extra, off) == EXTID_ZIP64 && sz == 16) {
size = LL(extra, off + 4); size = LL(extra, off + 4);
csize = LL(extra, off + 12); csize = LL(extra, off + 12);
hasZip64 = true;
break; break;
} }
off += (sz + 4); off += (sz + 4);
...@@ -2050,7 +2097,6 @@ public class ZipFileSystem extends FileSystem { ...@@ -2050,7 +2097,6 @@ public class ZipFileSystem extends FileSystem {
} }
pos += (method == METHOD_STORED ? size : csize); pos += (method == METHOD_STORED ? size : csize);
} }
endPos = pos;
return this; return this;
} }
...@@ -2058,14 +2104,18 @@ public class ZipFileSystem extends FileSystem { ...@@ -2058,14 +2104,18 @@ public class ZipFileSystem extends FileSystem {
throws IOException throws IOException
{ {
writeInt(os, LOCSIG); // LOC header signature writeInt(os, LOCSIG); // LOC header signature
int version = version(); int version = version();
int nlen = (name != null) ? name.length : 0;
int elen = (extra != null) ? extra.length : 0;
int elen64 = 0;
int elenEXTT = 0;
if ((flag & FLAG_DATADESCR) != 0) { if ((flag & FLAG_DATADESCR) != 0) {
writeShort(os, version()); // version needed to extract writeShort(os, version()); // version needed to extract
writeShort(os, flag); // general purpose bit flag writeShort(os, flag); // general purpose bit flag
writeShort(os, method); // compression method writeShort(os, method); // compression method
writeInt(os, mtime); // last modification time // last modification time
writeInt(os, (int)javaToDosTime(mtime));
// store size, uncompressed size, and crc-32 in data descriptor // store size, uncompressed size, and crc-32 in data descriptor
// immediately following compressed entry data // immediately following compressed entry data
writeInt(os, 0); writeInt(os, 0);
...@@ -2073,7 +2123,7 @@ public class ZipFileSystem extends FileSystem { ...@@ -2073,7 +2123,7 @@ public class ZipFileSystem extends FileSystem {
writeInt(os, 0); writeInt(os, 0);
} else { } else {
if (csize >= ZIP64_MINVAL || size >= ZIP64_MINVAL) { if (csize >= ZIP64_MINVAL || size >= ZIP64_MINVAL) {
hasZip64 = true; elen64 = 20; //headid(2) + size(2) + size(8) + csize(8)
writeShort(os, 45); // ver 4.5 for zip64 writeShort(os, 45); // ver 4.5 for zip64
} else { } else {
writeShort(os, version()); // version needed to extract writeShort(os, version()); // version needed to extract
...@@ -2082,29 +2132,45 @@ public class ZipFileSystem extends FileSystem { ...@@ -2082,29 +2132,45 @@ public class ZipFileSystem extends FileSystem {
writeShort(os, method); // compression method writeShort(os, method); // compression method
writeInt(os, mtime); // last modification time writeInt(os, mtime); // last modification time
writeInt(os, crc); // crc-32 writeInt(os, crc); // crc-32
if (hasZip64) { if (elen64 != 0) {
writeInt(os, ZIP64_MINVAL); writeInt(os, ZIP64_MINVAL);
writeInt(os, ZIP64_MINVAL); writeInt(os, ZIP64_MINVAL);
//TBD: e.elen += 20; //headid(2) + size(2) + size(8) + csize(8)
} else { } else {
writeInt(os, csize); // compressed size writeInt(os, csize); // compressed size
writeInt(os, size); // uncompressed size writeInt(os, size); // uncompressed size
} }
} }
if (atime != -1 && !isWindows) { // on unix use "ext time"
if (ctime == -1)
elenEXTT = 13;
else
elenEXTT = 17;
}
writeShort(os, name.length); writeShort(os, name.length);
writeShort(os, elen + (hasZip64 ? 20 : 0)); writeShort(os, elen + elen64 + elenEXTT);
writeBytes(os, name); writeBytes(os, name);
if (hasZip64) { if (elen64 != 0) {
// TBD: should we update extra directory?
writeShort(os, EXTID_ZIP64); writeShort(os, EXTID_ZIP64);
writeShort(os, 16); writeShort(os, 16);
writeLong(os, size); writeLong(os, size);
writeLong(os, csize); writeLong(os, csize);
} }
if (elenEXTT != 0) {
writeShort(os, EXTID_EXTT);
writeShort(os, elenEXTT - 4);// size for the folowing data block
if (ctime == -1)
os.write(0x3); // mtime and atime
else
os.write(0x7); // mtime, atime and ctime
writeInt(os, javaToUnixTime(mtime));
writeInt(os, javaToUnixTime(atime));
if (ctime != -1)
writeInt(os, javaToUnixTime(ctime));
}
if (extra != null) { if (extra != null) {
writeBytes(os, extra); writeBytes(os, extra);
} }
return LOCHDR + name.length + elen + (hasZip64 ? 20 : 0); return LOCHDR + name.length + elen + elen64 + elenEXTT;
} }
// Data Descriptior // Data Descriptior
...@@ -2125,17 +2191,18 @@ public class ZipFileSystem extends FileSystem { ...@@ -2125,17 +2191,18 @@ public class ZipFileSystem extends FileSystem {
} }
// read NTFS, UNIX and ZIP64 data from cen.extra // read NTFS, UNIX and ZIP64 data from cen.extra
void readExtra() { void readExtra(ZipFileSystem zipfs) throws IOException {
if (extra == null) if (extra == null)
return; return;
int elen = extra.length; int elen = extra.length;
int off = 0; int off = 0;
int newOff = 0;
while (off + 4 < elen) { while (off + 4 < elen) {
// extra spec: HeaderID+DataSize+Data // extra spec: HeaderID+DataSize+Data
int sz = SH(extra, off + 2);
int tag = SH(extra, off);
off += 4;
int pos = off; int pos = off;
int tag = SH(extra, pos);
int sz = SH(extra, pos + 2);
pos += 4;
if (pos + sz > elen) // invalid data if (pos + sz > elen) // invalid data
break; break;
switch (tag) { switch (tag) {
...@@ -2165,18 +2232,66 @@ public class ZipFileSystem extends FileSystem { ...@@ -2165,18 +2232,66 @@ public class ZipFileSystem extends FileSystem {
break; break;
if (SH(extra, pos + 2) != 24) if (SH(extra, pos + 2) != 24)
break; break;
mtime = LL(extra, pos + 4); // override the loc field, datatime here is
atime = LL(extra, pos + 12); // more "accurate"
ctime = LL(extra, pos + 20); mtime = winToJavaTime(LL(extra, pos + 4));
atime = winToJavaTime(LL(extra, pos + 12));
ctime = winToJavaTime(LL(extra, pos + 20));
break;
case EXTID_EXTT:
// spec says the Extened timestamp in cen only has mtime
// need to read the loc to get the extra a/ctime
byte[] buf = new byte[LOCHDR];
if (zipfs.readFullyAt(buf, 0, buf.length , locoff)
!= buf.length)
throw new ZipException("loc: reading failed");
if (LOCSIG(buf) != LOCSIG)
throw new ZipException("loc: wrong sig ->"
+ Long.toString(LOCSIG(buf), 16));
int locElen = LOCEXT(buf);
if (locElen < 9) // EXTT is at lease 9 bytes
break;
int locNlen = LOCNAM(buf);
buf = new byte[locElen];
if (zipfs.readFullyAt(buf, 0, buf.length , locoff + LOCHDR + locNlen)
!= buf.length)
throw new ZipException("loc extra: reading failed");
int locPos = 0;
while (locPos + 4 < buf.length) {
int locTag = SH(buf, locPos);
int locSZ = SH(buf, locPos + 2);
locPos += 4;
if (locTag != EXTID_EXTT) {
locPos += locSZ;
continue;
}
int flag = CH(buf, locPos++);
if ((flag & 0x1) != 0) {
mtime = unixToJavaTime(LG(buf, locPos));
locPos += 4;
}
if ((flag & 0x2) != 0) {
atime = unixToJavaTime(LG(buf, locPos));
locPos += 4;
}
if ((flag & 0x4) != 0) {
ctime = unixToJavaTime(LG(buf, locPos));
locPos += 4;
}
break; break;
case EXTID_UNIX: }
atime = LG(extra, pos);
mtime = LG(extra, pos + 4);
break; break;
default: // unknow default: // unknown tag
System.arraycopy(extra, off, extra, newOff, sz + 4);
newOff += (sz + 4);
} }
off += sz; off += (sz + 4);
} }
if (newOff != 0 && newOff != extra.length)
extra = Arrays.copyOf(extra, newOff);
else
extra = null;
} }
} }
...@@ -2201,9 +2316,9 @@ public class ZipFileSystem extends FileSystem { ...@@ -2201,9 +2316,9 @@ public class ZipFileSystem extends FileSystem {
// structure. // structure.
// A possible solution is to build the node tree ourself as // A possible solution is to build the node tree ourself as
// implemented below. // implemented below.
private HashMap<EntryName, IndexNode> dirs; private HashMap<IndexNode, IndexNode> dirs;
private IndexNode root; private IndexNode root;
private IndexNode addToDir(EntryName child) { private IndexNode addToDir(IndexNode child) {
IndexNode cinode = dirs.get(child); IndexNode cinode = dirs.get(child);
if (cinode != null) if (cinode != null)
return cinode; return cinode;
...@@ -2213,7 +2328,7 @@ public class ZipFileSystem extends FileSystem { ...@@ -2213,7 +2328,7 @@ public class ZipFileSystem extends FileSystem {
IndexNode pinode; IndexNode pinode;
if (pname != null) if (pname != null)
pinode = addToDir(new EntryName(pname)); pinode = addToDir(IndexNode.keyOf(pname));
else else
pinode = root; pinode = root;
cinode = inodes.get(child); cinode = inodes.get(child);
...@@ -2222,7 +2337,7 @@ public class ZipFileSystem extends FileSystem { ...@@ -2222,7 +2337,7 @@ public class ZipFileSystem extends FileSystem {
pinode.child = cinode; pinode.child = cinode;
return null; return null;
} }
cinode = dirs.get(child); //cinode = dirs.get(child);
if (cinode == null) // pseudo directry entry if (cinode == null) // pseudo directry entry
cinode = new IndexNode(cname, -1); cinode = new IndexNode(cname, -1);
cinode.sibling = pinode.child; cinode.sibling = pinode.child;
...@@ -2232,26 +2347,21 @@ public class ZipFileSystem extends FileSystem { ...@@ -2232,26 +2347,21 @@ public class ZipFileSystem extends FileSystem {
return cinode; return cinode;
} }
private HashMap<EntryName, IndexNode> getDirs() private HashMap<IndexNode, IndexNode> getDirs()
throws IOException throws IOException
{ {
if (hasUpdate) beginWrite();
sync(); try {
if (dirs != null) if (dirs != null)
return dirs; return dirs;
dirs = new HashMap<EntryName, IndexNode>(); dirs = new HashMap<>();
byte[] empty = new byte[0]; root = new IndexNode(new byte[0], -1);
root = new IndexNode(empty, -1); dirs.put(root, root);
dirs.put(new EntryName(empty), root); for (IndexNode node : inodes.keySet())
addToDir(node);
EntryName[] names = inodes.keySet().toArray(new EntryName[0]);
int i = names.length;
while (--i >= 0) {
addToDir(names[i]);
}
// for (int i EntryName en : inodes.keySet()) {
// addToDir(en);
// }
return dirs; return dirs;
} finally {
endWrite();
}
} }
} }
...@@ -55,6 +55,8 @@ import java.util.Set; ...@@ -55,6 +55,8 @@ import java.util.Set;
*/ */
public class ZipFileSystemProvider extends FileSystemProvider { public class ZipFileSystemProvider extends FileSystemProvider {
private final Map<Path, ZipFileSystem> filesystems = new HashMap<>(); private final Map<Path, ZipFileSystem> filesystems = new HashMap<>();
public ZipFileSystemProvider() {} public ZipFileSystemProvider() {}
...@@ -101,10 +103,16 @@ public class ZipFileSystemProvider extends FileSystemProvider { ...@@ -101,10 +103,16 @@ public class ZipFileSystemProvider extends FileSystemProvider {
throws IOException throws IOException
{ {
synchronized(filesystems) { synchronized(filesystems) {
if (filesystems.containsKey(path)) Path realPath = null;
if (path.exists()) {
realPath = path.toRealPath(true);
if (filesystems.containsKey(realPath))
throw new FileSystemAlreadyExistsException(); throw new FileSystemAlreadyExistsException();
}
ZipFileSystem zipfs = new ZipFileSystem(this, path, env); ZipFileSystem zipfs = new ZipFileSystem(this, path, env);
filesystems.put(path, zipfs); if (realPath == null)
realPath = path.toRealPath(true);
filesystems.put(realPath, zipfs);
return zipfs; return zipfs;
} }
} }
...@@ -137,16 +145,21 @@ public class ZipFileSystemProvider extends FileSystemProvider { ...@@ -137,16 +145,21 @@ public class ZipFileSystemProvider extends FileSystemProvider {
@Override @Override
public FileSystem getFileSystem(URI uri) { public FileSystem getFileSystem(URI uri) {
synchronized (filesystems) { synchronized (filesystems) {
ZipFileSystem zipfs = filesystems.get(uriToPath(uri)); ZipFileSystem zipfs = null;
try {
zipfs = filesystems.get(uriToPath(uri).toRealPath(true));
} catch (IOException x) {
// ignore the ioe from toRealPath(), return FSNFE
}
if (zipfs == null) if (zipfs == null)
throw new FileSystemNotFoundException(); throw new FileSystemNotFoundException();
return zipfs; return zipfs;
} }
} }
void removeFileSystem(Path zfpath) { void removeFileSystem(Path zfpath) throws IOException {
synchronized (filesystems) { synchronized (filesystems) {
filesystems.remove(zfpath); filesystems.remove(zfpath.toRealPath(true));
} }
} }
} }
...@@ -31,7 +31,6 @@ ...@@ -31,7 +31,6 @@
package com.sun.nio.zipfs; package com.sun.nio.zipfs;
import java.io.PrintStream;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
...@@ -41,7 +40,7 @@ import static com.sun.nio.zipfs.ZipConstants.*; ...@@ -41,7 +40,7 @@ import static com.sun.nio.zipfs.ZipConstants.*;
import static com.sun.nio.zipfs.ZipUtils.*; import static com.sun.nio.zipfs.ZipUtils.*;
/** /**
* Print the loc and cen tables of the ZIP file * Print all loc and cen headers of the ZIP file
* *
* @author Xueming Shen * @author Xueming Shen
*/ */
...@@ -49,34 +48,38 @@ import static com.sun.nio.zipfs.ZipUtils.*; ...@@ -49,34 +48,38 @@ import static com.sun.nio.zipfs.ZipUtils.*;
public class ZipInfo { public class ZipInfo {
public static void main(String[] args) throws Throwable { public static void main(String[] args) throws Throwable {
if (args.length < 2) { if (args.length < 1) {
print("Usage: java ZipInfo [cen|loc] zfname"); print("Usage: java ZipInfo zfname");
} else { } else {
Map<String, ?> env = Collections.emptyMap(); Map<String, ?> env = Collections.emptyMap();
ZipFileSystem zfs = (ZipFileSystem)(new ZipFileSystemProvider() ZipFileSystem zfs = (ZipFileSystem)(new ZipFileSystemProvider()
.newFileSystem(Paths.get(args[1]), env)); .newFileSystem(Paths.get(args[0]), env));
byte[] cen = zfs.cen;
long pos = 0; if (cen == null) {
print("zip file is empty%n");
if ("loc".equals(args[0])) { return;
print("[Local File Header]%n"); }
int pos = 0;
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
for (int i = 0; i < zfs.getEntryNames().length; i++) { int no = 1;
Entry loc = Entry.readLOC(zfs, pos, buf); while (pos + CENHDR < cen.length) {
print("--------loc[%x]--------%n", pos); print("----------------#%d--------------------%n", no++);
printLOC(loc); printCEN(cen, pos);
pos = loc.endPos;
} // use size CENHDR as the extra bytes to read, just in case the
} if ("cen".equals(args[0])) { // loc.extra is bigger than the cen.extra, try to avoid to read
int i = 0; // twice
Iterator<ZipFileSystem.IndexNode> itr = zfs.inodes.values().iterator(); long len = LOCHDR + CENNAM(cen, pos) + CENEXT(cen, pos) + CENHDR;
print("[Central Directory Header]%n"); if (zfs.readFullyAt(buf, 0, len, locoff(cen, pos)) != len)
while (itr.hasNext()) { zfs.zerror("read loc header failed");
Entry cen = Entry.readCEN(zfs.cen, itr.next().pos); if (LOCEXT(buf) > CENEXT(cen, pos) + CENHDR) {
print("--------cen[%d]--------%n", i); // have to read the second time;
printCEN(cen); len = LOCHDR + LOCNAM(buf) + LOCEXT(buf);
i++; if (zfs.readFullyAt(buf, 0, len, locoff(cen, pos)) != len)
zfs.zerror("read loc header failed");
} }
printLOC(buf);
pos += CENHDR + CENNAM(cen, pos) + CENEXT(cen, pos) + CENCOM(cen, pos);
} }
zfs.close(); zfs.close();
} }
...@@ -86,47 +89,135 @@ public class ZipInfo { ...@@ -86,47 +89,135 @@ public class ZipInfo {
System.out.printf(fmt, objs); System.out.printf(fmt, objs);
} }
static void printLOC(Entry loc) { static void printLOC(byte[] loc) {
print(" [%x, %x]%n", loc.startPos, loc.endPos); print("%n");
print(" Signature : %8x%n", LOCSIG); print("[Local File Header]%n");
print(" Version : %4x [%d.%d]%n", print(" Signature : %#010x%n", LOCSIG(loc));
loc.version, loc. version/10, loc. version%10); if (LOCSIG(loc) != LOCSIG) {
print(" Flag : %4x%n", loc.flag); print(" Wrong signature!");
print(" Method : %4x%n", loc. method); return;
print(" LastMTime : %8x [%tc]%n", }
loc.mtime, dosToJavaTime(loc.mtime)); print(" Version : %#6x [%d.%d]%n",
print(" CRC : %8x%n", loc.crc); LOCVER(loc), LOCVER(loc) / 10, LOCVER(loc) % 10);
print(" CSize : %8x%n", loc.csize); print(" Flag : %#6x%n", LOCFLG(loc));
print(" Size : %8x%n", loc.size); print(" Method : %#6x%n", LOCHOW(loc));
print(" NameLength : %4x [%s]%n", print(" LastMTime : %#10x [%tc]%n",
loc.nlen, new String(loc.name)); LOCTIM(loc), dosToJavaTime(LOCTIM(loc)));
print(" ExtraLength : %4x%n", loc.elen); print(" CRC : %#10x%n", LOCCRC(loc));
if (loc.hasZip64) print(" CSize : %#10x%n", LOCSIZ(loc));
print(" *ZIP64*%n"); print(" Size : %#10x%n", LOCLEN(loc));
print(" NameLength : %#6x [%s]%n",
LOCNAM(loc), new String(loc, LOCHDR, LOCNAM(loc)));
print(" ExtraLength : %#6x%n", LOCEXT(loc));
if (LOCEXT(loc) != 0)
printExtra(loc, LOCHDR + LOCNAM(loc), LOCEXT(loc));
}
static void printCEN(byte[] cen, int off) {
print("[Central Directory Header]%n");
print(" Signature : %#010x%n", CENSIG(cen, off));
if (CENSIG(cen, off) != CENSIG) {
print(" Wrong signature!");
return;
} }
print(" VerMadeby : %#6x [%d, %d.%d]%n",
CENVEM(cen, off), (CENVEM(cen, off) >> 8),
(CENVEM(cen, off) & 0xff) / 10,
(CENVEM(cen, off) & 0xff) % 10);
print(" VerExtract : %#6x [%d.%d]%n",
CENVER(cen, off), CENVER(cen, off) / 10, CENVER(cen, off) % 10);
print(" Flag : %#6x%n", CENFLG(cen, off));
print(" Method : %#6x%n", CENHOW(cen, off));
print(" LastMTime : %#10x [%tc]%n",
CENTIM(cen, off), dosToJavaTime(CENTIM(cen, off)));
print(" CRC : %#10x%n", CENCRC(cen, off));
print(" CSize : %#10x%n", CENSIZ(cen, off));
print(" Size : %#10x%n", CENLEN(cen, off));
print(" NameLen : %#6x [%s]%n",
CENNAM(cen, off), new String(cen, off + CENHDR, CENNAM(cen, off)));
print(" ExtraLen : %#6x%n", CENEXT(cen, off));
if (CENEXT(cen, off) != 0)
printExtra(cen, off + CENHDR + CENNAM(cen, off), CENEXT(cen, off));
print(" CommentLen : %#6x%n", CENCOM(cen, off));
print(" DiskStart : %#6x%n", CENDSK(cen, off));
print(" Attrs : %#6x%n", CENATT(cen, off));
print(" AttrsEx : %#10x%n", CENATX(cen, off));
print(" LocOff : %#10x%n", CENOFF(cen, off));
static void printCEN(Entry cen) { }
print(" Signature : %08x%n", CENSIG);
print(" VerMadeby : %4x [%d.%d]%n", static long locoff(byte[] cen, int pos) {
cen.versionMade, cen.versionMade/10, cen.versionMade%10); long locoff = CENOFF(cen, pos);
print(" VerExtract : %4x [%d.%d]%n", if (locoff == ZIP64_MINVAL) { //ZIP64
cen.version, cen.version/10, cen.version%10); int off = pos + CENHDR + CENNAM(cen, pos);
print(" Flag : %4x%n", cen.flag); int end = off + CENEXT(cen, pos);
print(" Method : %4x%n", cen.method); while (off + 4 < end) {
print(" LastMTime : %8x [%tc]%n", int tag = SH(cen, off);
cen.mtime, dosToJavaTime(cen.mtime)); int sz = SH(cen, off + 2);
print(" CRC : %8x%n", cen.crc); if (tag != EXTID_ZIP64) {
print(" CSize : %8x%n", cen.csize); off += 4 + sz;
print(" Size : %8x%n", cen.size); continue;
print(" NameLen : %4x [%s]%n", }
cen.nlen, new String(cen.name)); off += 4;
print(" ExtraLen : %4x%n", cen.elen); if (CENLEN(cen, pos) == ZIP64_MINVAL)
print(" CommentLen : %4x%n", cen.clen); off += 8;
print(" DiskStart : %4x%n", cen.disk); if (CENSIZ(cen, pos) == ZIP64_MINVAL)
print(" Attrs : %4x%n", cen.attrs); off += 8;
print(" AttrsEx : %8x%n", cen.attrsEx); return LL(cen, off);
print(" LocOff : %8x%n", cen.locoff); }
if (cen.hasZip64) // should never be here
print(" *ZIP64*%n"); }
return locoff;
}
static void printExtra(byte[] extra, int off, int len) {
int end = off + len;
while (off + 4 < end) {
int tag = SH(extra, off);
int sz = SH(extra, off + 2);
print(" [tag=0x%04x, sz=%d, data= ", tag, sz);
if (off + sz > end) {
print(" Error: Invalid extra data, beyond extra length");
break;
}
off += 4;
for (int i = 0; i < sz; i++)
print("%02x ", extra[off + i]);
print("]%n");
switch (tag) {
case EXTID_ZIP64 :
print(" ->ZIP64: ");
int pos = off;
while (pos + 8 <= off + sz) {
print(" *0x%x ", LL(extra, pos));
pos += 8;
}
print("%n");
break;
case EXTID_NTFS:
print(" ->PKWare NTFS%n");
// 4 bytes reserved
if (SH(extra, off + 4) != 0x0001 || SH(extra, off + 6) != 24)
print(" Error: Invalid NTFS sub-tag or subsz");
print(" mtime:%tc%n",
winToJavaTime(LL(extra, off + 8)));
print(" atime:%tc%n",
winToJavaTime(LL(extra, off + 16)));
print(" ctime:%tc%n",
winToJavaTime(LL(extra, off + 24)));
break;
case EXTID_EXTT:
print(" ->Inof-ZIP Extended Timestamp: flag=%x%n",extra[off]);
pos = off + 1 ;
while (pos + 4 <= off + sz) {
print(" *%tc%n",
unixToJavaTime(LG(extra, pos)));
pos += 4;
}
break;
default:
}
off += sz;
}
} }
} }
...@@ -32,24 +32,19 @@ ...@@ -32,24 +32,19 @@
package com.sun.nio.zipfs; package com.sun.nio.zipfs;
import java.io.File; import java.io.File;
import java.io.FilterInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.URI; import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.nio.channels.SeekableByteChannel; import java.nio.channels.SeekableByteChannel;
import java.nio.file.*; import java.nio.file.*;
import java.nio.file.DirectoryStream.Filter; import java.nio.file.DirectoryStream.Filter;
import java.nio.file.spi.FileSystemProvider;
import java.nio.file.attribute.BasicFileAttributeView; import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.FileAttribute; import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.FileAttributeView; import java.nio.file.attribute.FileAttributeView;
import java.nio.file.attribute.FileTime; import java.nio.file.attribute.FileTime;
import java.util.*; import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.nio.file.StandardOpenOption.*; import static java.nio.file.StandardOpenOption.*;
import static java.nio.file.StandardCopyOption.*; import static java.nio.file.StandardCopyOption.*;
...@@ -599,7 +594,7 @@ public class ZipPath extends Path { ...@@ -599,7 +594,7 @@ public class ZipPath extends Path {
} }
private static final DirectoryStream.Filter<Path> acceptAllFilter = private static final DirectoryStream.Filter<Path> acceptAllFilter =
new DirectoryStream.Filter<Path>() { new DirectoryStream.Filter<>() {
@Override public boolean accept(Path entry) { return true; } @Override public boolean accept(Path entry) { return true; }
}; };
...@@ -625,7 +620,7 @@ public class ZipPath extends Path { ...@@ -625,7 +620,7 @@ public class ZipPath extends Path {
// create a matcher and return a filter that uses it. // create a matcher and return a filter that uses it.
final PathMatcher matcher = getFileSystem().getPathMatcher("glob:" + glob); final PathMatcher matcher = getFileSystem().getPathMatcher("glob:" + glob);
DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() { DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<>() {
@Override @Override
public boolean accept(Path entry) { public boolean accept(Path entry) {
return matcher.matches(entry.getName()); return matcher.matches(entry.getName());
...@@ -758,7 +753,7 @@ public class ZipPath extends Path { ...@@ -758,7 +753,7 @@ public class ZipPath extends Path {
@Override @Override
public Iterator<Path> iterator() { public Iterator<Path> iterator() {
return new Iterator<Path>() { return new Iterator<>() {
private int i = 0; private int i = 0;
@Override @Override
...@@ -803,7 +798,7 @@ public class ZipPath extends Path { ...@@ -803,7 +798,7 @@ public class ZipPath extends Path {
@Override @Override
public SeekableByteChannel newByteChannel(OpenOption... options) public SeekableByteChannel newByteChannel(OpenOption... options)
throws IOException { throws IOException {
Set<OpenOption> set = new HashSet<OpenOption>(options.length); Set<OpenOption> set = new HashSet<>(options.length);
Collections.addAll(set, options); Collections.addAll(set, options);
return newByteChannel(set); return newByteChannel(set);
} }
...@@ -908,7 +903,7 @@ public class ZipPath extends Path { ...@@ -908,7 +903,7 @@ public class ZipPath extends Path {
if (opt == REPLACE_EXISTING) if (opt == REPLACE_EXISTING)
replaceExisting = true; replaceExisting = true;
else if (opt == COPY_ATTRIBUTES) else if (opt == COPY_ATTRIBUTES)
copyAttrs = false; copyAttrs = true;
} }
// attributes of source file // attributes of source file
ZipFileAttributes zfas = getAttributes(); ZipFileAttributes zfas = getAttributes();
...@@ -951,7 +946,9 @@ public class ZipPath extends Path { ...@@ -951,7 +946,9 @@ public class ZipPath extends Path {
BasicFileAttributeView view = BasicFileAttributeView view =
target.getFileAttributeView(BasicFileAttributeView.class); target.getFileAttributeView(BasicFileAttributeView.class);
try { try {
view.setTimes(zfas.lastModifiedTime(), null, null); view.setTimes(zfas.lastModifiedTime(),
zfas.lastAccessTime(),
zfas.creationTime());
} catch (IOException x) { } catch (IOException x) {
// rollback? // rollback?
try { try {
......
...@@ -36,6 +36,7 @@ import java.io.OutputStream; ...@@ -36,6 +36,7 @@ import java.io.OutputStream;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.regex.PatternSyntaxException; import java.util.regex.PatternSyntaxException;
import java.util.concurrent.TimeUnit;
/** /**
* *
...@@ -48,7 +49,7 @@ class ZipUtils { ...@@ -48,7 +49,7 @@ class ZipUtils {
* Writes a 16-bit short to the output stream in little-endian byte order. * Writes a 16-bit short to the output stream in little-endian byte order.
*/ */
public static void writeShort(OutputStream os, int v) throws IOException { public static void writeShort(OutputStream os, int v) throws IOException {
os.write((v >>> 0) & 0xff); os.write(v & 0xff);
os.write((v >>> 8) & 0xff); os.write((v >>> 8) & 0xff);
} }
...@@ -56,7 +57,7 @@ class ZipUtils { ...@@ -56,7 +57,7 @@ class ZipUtils {
* Writes a 32-bit int to the output stream in little-endian byte order. * Writes a 32-bit int to the output stream in little-endian byte order.
*/ */
public static void writeInt(OutputStream os, long v) throws IOException { public static void writeInt(OutputStream os, long v) throws IOException {
os.write((int)((v >>> 0) & 0xff)); os.write((int)(v & 0xff));
os.write((int)((v >>> 8) & 0xff)); os.write((int)((v >>> 8) & 0xff));
os.write((int)((v >>> 16) & 0xff)); os.write((int)((v >>> 16) & 0xff));
os.write((int)((v >>> 24) & 0xff)); os.write((int)((v >>> 24) & 0xff));
...@@ -66,7 +67,7 @@ class ZipUtils { ...@@ -66,7 +67,7 @@ class ZipUtils {
* Writes a 64-bit int to the output stream in little-endian byte order. * Writes a 64-bit int to the output stream in little-endian byte order.
*/ */
public static void writeLong(OutputStream os, long v) throws IOException { public static void writeLong(OutputStream os, long v) throws IOException {
os.write((int)((v >>> 0) & 0xff)); os.write((int)(v & 0xff));
os.write((int)((v >>> 8) & 0xff)); os.write((int)((v >>> 8) & 0xff));
os.write((int)((v >>> 16) & 0xff)); os.write((int)((v >>> 16) & 0xff));
os.write((int)((v >>> 24) & 0xff)); os.write((int)((v >>> 24) & 0xff));
...@@ -132,6 +133,27 @@ class ZipUtils { ...@@ -132,6 +133,27 @@ class ZipUtils {
d.getSeconds() >> 1; d.getSeconds() >> 1;
} }
// used to adjust values between Windows and java epoch
private static final long WINDOWS_EPOCH_IN_MICROSECONDS = -11644473600000000L;
public static final long winToJavaTime(long wtime) {
return TimeUnit.MILLISECONDS.convert(
wtime / 10 + WINDOWS_EPOCH_IN_MICROSECONDS, TimeUnit.MICROSECONDS);
}
public static final long javaToWinTime(long time) {
return (TimeUnit.MICROSECONDS.convert(time, TimeUnit.MILLISECONDS)
- WINDOWS_EPOCH_IN_MICROSECONDS) * 10;
}
public static final long unixToJavaTime(long utime) {
return TimeUnit.MILLISECONDS.convert(utime, TimeUnit.SECONDS);
}
public static final long javaToUnixTime(long time) {
return TimeUnit.SECONDS.convert(time, TimeUnit.MILLISECONDS);
}
private static final String regexMetaChars = ".^$+{[]|()"; private static final String regexMetaChars = ".^$+{[]|()";
private static final String globMetaChars = "\\*?[{"; private static final String globMetaChars = "\\*?[{";
private static boolean isRegexMeta(char c) { private static boolean isRegexMeta(char c) {
......
...@@ -31,5 +31,9 @@ disabledMechanisms = { ...@@ -31,5 +31,9 @@ disabledMechanisms = {
CKM_SHA256_RSA_PKCS CKM_SHA256_RSA_PKCS
CKM_SHA384_RSA_PKCS CKM_SHA384_RSA_PKCS
CKM_SHA512_RSA_PKCS CKM_SHA512_RSA_PKCS
# the following mechanisms are disabled to ensure backward compatibility (Solaris bug 6545046)
CKM_DES_CBC_PAD
CKM_DES3_CBC_PAD
CKM_AES_CBC_PAD
} }
...@@ -64,7 +64,6 @@ public class ZipFSTester { ...@@ -64,7 +64,6 @@ public class ZipFSTester {
fs0.close(); // sync to file fs0.close(); // sync to file
fs = newZipFileSystem(tmpfsPath, new HashMap<String, Object>()); fs = newZipFileSystem(tmpfsPath, new HashMap<String, Object>());
try { try {
// prepare a src // prepare a src
Path src = getTempPath(); Path src = getTempPath();
...@@ -146,13 +145,6 @@ public class ZipFSTester { ...@@ -146,13 +145,6 @@ public class ZipFSTester {
Path fs2Path = getTempPath(); Path fs2Path = getTempPath();
Path fs3Path = getTempPath(); Path fs3Path = getTempPath();
if (fs1Path.exists())
fs1Path.delete();
if (fs2Path.exists())
fs2Path.delete();
if (fs3Path.exists())
fs3Path.delete();
// create a new filesystem, copy everything from fs // create a new filesystem, copy everything from fs
Map<String, Object> env = new HashMap<String, Object>(); Map<String, Object> env = new HashMap<String, Object>();
env.put("createNew", true); env.put("createNew", true);
...@@ -280,7 +272,6 @@ public class ZipFSTester { ...@@ -280,7 +272,6 @@ public class ZipFSTester {
walk(fs4.getPath("/")); walk(fs4.getPath("/"));
System.out.println("closing: fs4"); System.out.println("closing: fs4");
fs4.close(); fs4.close();
System.out.printf("failed=%d%n", failed); System.out.printf("failed=%d%n", failed);
fs1Path.delete(); fs1Path.delete();
...@@ -426,6 +417,8 @@ public class ZipFSTester { ...@@ -426,6 +417,8 @@ public class ZipFSTester {
} }
private static void mkdirs(Path path) throws IOException { private static void mkdirs(Path path) throws IOException {
if (path.exists())
return;
path = path.toAbsolutePath(); path = path.toAbsolutePath();
Path parent = path.getParent(); Path parent = path.getParent();
if (parent != null) { if (parent != null) {
......
...@@ -22,15 +22,19 @@ ...@@ -22,15 +22,19 @@
*/ */
/* @test /* @test
* @bug 4607272 * @bug 4607272 6999915
* @summary Unit test for AsynchronousSocketChannel * @summary Unit test for AsynchronousSocketChannel
* @run main/othervm -XX:+DisableExplicitGC -mx64m Leaky * @run main/othervm -XX:+DisableExplicitGC -XX:MaxDirectMemorySize=64m Leaky
*/ */
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.BufferPoolMXBean;
import java.nio.channels.*; import java.nio.channels.*;
import java.net.*; import java.net.*;
import java.util.List;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import java.lang.management.ManagementFactory;
/** /**
* Heap buffers must be substituted with direct buffers when doing I/O. This * Heap buffers must be substituted with direct buffers when doing I/O. This
...@@ -49,13 +53,13 @@ public class Leaky { ...@@ -49,13 +53,13 @@ public class Leaky {
private final ByteBuffer dst; private final ByteBuffer dst;
private Future<Integer> readResult; private Future<Integer> readResult;
Connection() throws Exception { Connection(AsynchronousChannelGroup group) throws Exception {
ServerSocketChannel ssc = ServerSocketChannel ssc =
ServerSocketChannel.open().bind(new InetSocketAddress(0)); ServerSocketChannel.open().bind(new InetSocketAddress(0));
InetAddress lh = InetAddress.getLocalHost(); InetAddress lh = InetAddress.getLocalHost();
int port = ((InetSocketAddress)(ssc.getLocalAddress())).getPort(); int port = ((InetSocketAddress)(ssc.getLocalAddress())).getPort();
SocketAddress remote = new InetSocketAddress(lh, port); SocketAddress remote = new InetSocketAddress(lh, port);
client = AsynchronousSocketChannel.open(); client = AsynchronousSocketChannel.open(group);
client.connect(remote).get(); client.connect(remote).get();
peer = ssc.accept(); peer = ssc.accept();
ssc.close(); ssc.close();
...@@ -77,11 +81,21 @@ public class Leaky { ...@@ -77,11 +81,21 @@ public class Leaky {
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
ThreadFactory threadFactory = new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setDaemon(true);
return t;
}
};
AsynchronousChannelGroup group =
AsynchronousChannelGroup.withFixedThreadPool(4, threadFactory);
final int CONNECTION_COUNT = 10; final int CONNECTION_COUNT = 10;
Connection[] connections = new Connection[CONNECTION_COUNT]; Connection[] connections = new Connection[CONNECTION_COUNT];
for (int i=0; i<CONNECTION_COUNT; i++) { for (int i=0; i<CONNECTION_COUNT; i++) {
connections[i] = new Connection(); connections[i] = new Connection(group);
} }
for (int i=0; i<1024; i++) { for (int i=0; i<1024; i++) {
...@@ -100,5 +114,20 @@ public class Leaky { ...@@ -100,5 +114,20 @@ public class Leaky {
conn.finishRead(); conn.finishRead();
} }
} }
// print summary of buffer pool usage
List<BufferPoolMXBean> pools =
ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
for (BufferPoolMXBean pool: pools)
System.out.format(" %8s ", pool.getName());
System.out.println();
for (int i=0; i<pools.size(); i++)
System.out.format("%6s %10s %10s ", "Count", "Capacity", "Memory");
System.out.println();
for (BufferPoolMXBean pool: pools) {
System.out.format("%6d %10d %10d ",
pool.getCount(), pool.getTotalCapacity(), pool.getMemoryUsed());
}
System.out.println();
} }
} }
...@@ -56,8 +56,9 @@ public class IteratorWeakConsistency { ...@@ -56,8 +56,9 @@ public class IteratorWeakConsistency {
// test(new ArrayBlockingQueue(20)); // test(new ArrayBlockingQueue(20));
} }
void test(Queue q) throws Throwable { void test(Queue q) {
// TODO: make this more general // TODO: make this more general
try {
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
q.add(i); q.add(i);
Iterator it = q.iterator(); Iterator it = q.iterator();
...@@ -73,6 +74,26 @@ public class IteratorWeakConsistency { ...@@ -73,6 +74,26 @@ public class IteratorWeakConsistency {
System.out.printf("%s: %s%n", System.out.printf("%s: %s%n",
q.getClass().getSimpleName(), q.getClass().getSimpleName(),
list); list);
} catch (Throwable t) { unexpected(t); }
try {
q.clear();
q.add(1);
q.add(2);
q.add(3);
q.add(4);
Iterator it = q.iterator();
it.next();
q.remove(2);
q.remove(1);
q.remove(3);
boolean found4 = false;
while (it.hasNext()) {
found4 |= it.next().equals(4);
}
check(found4);
} catch (Throwable t) { unexpected(t); }
} }
//--------------------- Infrastructure --------------------------- //--------------------- Infrastructure ---------------------------
...@@ -85,7 +106,6 @@ public class IteratorWeakConsistency { ...@@ -85,7 +106,6 @@ public class IteratorWeakConsistency {
void equal(Object x, Object y) { void equal(Object x, Object y) {
if (x == null ? y == null : x.equals(y)) pass(); if (x == null ? y == null : x.equals(y)) pass();
else fail(x + " not equal to " + y);} else fail(x + " not equal to " + y);}
static Class<?> thisClass = new Object(){}.getClass().getEnclosingClass();
public static void main(String[] args) throws Throwable { public static void main(String[] args) throws Throwable {
new IteratorWeakConsistency().instanceMain(args);} new IteratorWeakConsistency().instanceMain(args);}
public void instanceMain(String[] args) throws Throwable { public void instanceMain(String[] args) throws Throwable {
......
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 6544278
* @summary Confirm the JarInputStream throws the SecurityException when
* verifying an indexed jar file with corrupted signature
*/
import java.io.IOException;
import java.io.FileInputStream;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
public class TestIndexedJarWithBadSignature {
public static void main(String...args) throws Throwable {
try (JarInputStream jis = new JarInputStream(
new FileInputStream(System.getProperty("tst.src", ".") +
System.getProperty("file.separator") +
"BadSignedJar.jar")))
{
JarEntry je1 = jis.getNextJarEntry();
while(je1!=null){
System.out.println("Jar Entry1==>"+je1.getName());
je1 = jis.getNextJarEntry(); // This should throw Security Exception
}
throw new RuntimeException(
"Test Failed:Security Exception not being thrown");
} catch (IOException ie){
ie.printStackTrace();
} catch (SecurityException e) {
System.out.println("Test passed: Security Exception thrown as expected");
}
}
}
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 6687725
* @summary Test internal PKCS5Padding impl with various error conditions.
* @author Valerie Peng
* @library ..
*/
import java.io.*;
import java.nio.*;
import java.util.*;
import java.security.*;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
public class TestPKCS5PaddingError extends PKCS11Test {
private static class CI { // class for holding Cipher Information
String transformation;
String keyAlgo;
CI(String transformation, String keyAlgo) {
this.transformation = transformation;
this.keyAlgo = keyAlgo;
}
}
private static final CI[] TEST_LIST = {
// algorithms which use the native padding impl
new CI("DES/CBC/PKCS5Padding", "DES"),
new CI("DESede/CBC/PKCS5Padding", "DESede"),
new CI("AES/CBC/PKCS5Padding", "AES"),
// algorithms which use SunPKCS11's own padding impl
new CI("DES/ECB/PKCS5Padding", "DES"),
new CI("DESede/ECB/PKCS5Padding", "DESede"),
new CI("AES/ECB/PKCS5Padding", "AES"),
};
private static StringBuffer debugBuf = new StringBuffer();
public void main(Provider p) throws Exception {
boolean status = true;
Random random = new Random();
try {
byte[] plainText = new byte[200];
for (int i = 0; i < TEST_LIST.length; i++) {
CI currTest = TEST_LIST[i];
System.out.println("===" + currTest.transformation + "===");
try {
KeyGenerator kg =
KeyGenerator.getInstance(currTest.keyAlgo, p);
SecretKey key = kg.generateKey();
Cipher c1 = Cipher.getInstance(currTest.transformation,
"SunJCE");
c1.init(Cipher.ENCRYPT_MODE, key);
byte[] cipherText = c1.doFinal(plainText);
AlgorithmParameters params = c1.getParameters();
Cipher c2 = Cipher.getInstance(currTest.transformation, p);
c2.init(Cipher.DECRYPT_MODE, key, params);
// 1st test: wrong output length
// NOTE: Skip NSS since it reports CKR_DEVICE_ERROR when
// the data passed to its EncryptUpdate/DecryptUpdate is
// not multiple of blocks
if (!p.getName().equals("SunPKCS11-NSS")) {
try {
System.out.println("Testing with wrong cipherText length");
c2.doFinal(cipherText, 0, cipherText.length - 2);
} catch (IllegalBlockSizeException ibe) {
// expected
} catch (Exception ex) {
System.out.println("Error: Unexpected Ex " + ex);
ex.printStackTrace();
}
}
// 2nd test: wrong padding value
try {
System.out.println("Testing with wrong padding bytes");
cipherText[cipherText.length - 1]++;
c2.doFinal(cipherText);
} catch (BadPaddingException bpe) {
// expected
} catch (Exception ex) {
System.out.println("Error: Unexpected Ex " + ex);
ex.printStackTrace();
}
System.out.println("DONE");
} catch (NoSuchAlgorithmException nsae) {
System.out.println("Skipping unsupported algorithm: " +
nsae);
}
}
} catch (Exception ex) {
// print out debug info when exception is encountered
if (debugBuf != null) {
System.out.println(debugBuf.toString());
debugBuf = new StringBuffer();
}
throw ex;
}
}
public static void main(String[] args) throws Exception {
main(new TestPKCS5PaddingError());
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册