Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
import java.util.zip.InflaterInputStream;
import java.util.zip.ZipException;

import dev.mutwakil.androidide.zipfs2.listing.JarListing;

import static java.lang.Boolean.TRUE;
import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
Expand Down Expand Up @@ -94,8 +96,8 @@ public class ZipFileSystem extends FileSystem {
// METHOD_DEFLATED otherwise

protected ZipFileSystem(ZipFileSystemProvider provider,
Path zfpath,
Map<String, ?> env) throws IOException {
Path zfpath,
Map<String, ?> env) throws IOException {
// default encoding for name/comment
String nameEncoding = env.containsKey("encoding")
? (String) env.get("encoding") : "UTF-8";
Expand Down Expand Up @@ -132,6 +134,35 @@ protected ZipFileSystem(ZipFileSystemProvider provider,
}
this.provider = provider;
this.zfpath = zfpath;
this.listing = null;
}

/**
* Creates a read-only file system that looks entries up in the given listing instead of
* indexing the JAR itself.
*
* <p>The channel must be open on the same file the listing was read from; this file system
* takes ownership of it.
*/
protected ZipFileSystem(ZipFileSystemProvider provider,
Path zfpath,
JarListing listing,
SeekableByteChannel ch) {
this.noExtt = false;
this.useTempFile = false;
this.forceEnd64 = false;
this.defaultMethod = METHOD_DEFLATED;
this.readOnly = true;
// JarListing keeps names as raw CEN bytes; ZipPath must encode lookups the same way.
this.zc = ZipCoder.get("UTF-8");
this.rootdir = new ZipPath(this, new byte[]{'/'});
this.ch = ch;
this.cen = null;
// JarListing offsets already include any bytes before the archive.
this.locpos = 0;
this.listing = listing;
this.provider = provider;
this.zfpath = zfpath;
}

// returns true if there is a name=true/"true" setting in env
Expand Down Expand Up @@ -285,7 +316,7 @@ public void close() throws IOException {
try {
sync();
closeChannelQuietly(ch); // close the ch just in case no update
// and sync didn't close the ch
// and sync didn't close the ch
} finally {
endWrite();
}
Expand Down Expand Up @@ -417,8 +448,11 @@ boolean isDirectory(byte[] path)

// returns the list of child paths of "path"
Iterator<Path> iteratorOf(ZipPath dir,
DirectoryStream.Filter<? super Path> filter)
DirectoryStream.Filter<? super Path> filter)
throws IOException {
if (listing != null) {
return listingIteratorOf(dir, filter);
}
beginWrite(); // iteration of inodes needs exclusive lock
try {
ensureOpen();
Expand Down Expand Up @@ -450,6 +484,31 @@ Iterator<Path> iteratorOf(ZipPath dir,
}
}

private Iterator<Path> listingIteratorOf(ZipPath dir,
DirectoryStream.Filter<? super Path> filter)
throws IOException {
beginRead(); // the listing is immutable
try {
ensureOpen();
byte[] path = dir.getResolvedPath();
int i = listing.find(path);
if (i < 0) {
throw new NotDirectoryException(getString(path));
}
List<Path> list = new ArrayList<>();
for (int child = listing.firstChild(i); child != -1; child = listing.nextSibling(child)) {
ZipPath childPath = new ZipPath(this, listing.nameBytes(child), true);
ZipPath zpath = dir.resolve(childPath.getFileName());
if (filter == null || filter.accept(zpath)) {
list.add(zpath);
}
}
return list.iterator();
} finally {
endRead();
}
}

void createDirectory(byte[] dir, FileAttribute<?>... attrs)
throws IOException {
checkWritable();
Expand Down Expand Up @@ -676,8 +735,8 @@ private int getCompressMethod() {
// file on the default file system and create a FileChannel on top of
// it.
SeekableByteChannel newByteChannel(byte[] path,
Set<? extends OpenOption> options,
FileAttribute<?>... attrs)
Set<? extends OpenOption> options,
FileAttribute<?>... attrs)
throws IOException {
checkOptions(options);
if (options.contains(StandardOpenOption.WRITE)
Expand Down Expand Up @@ -815,8 +874,8 @@ public byte[] readNBytes(InputStream is, int len) throws IOException {
// copy the entry data into it if the entry exists, and then create a
// FileChannel on top of it.
FileChannel newFileChannel(byte[] path,
Set<? extends OpenOption> options,
FileAttribute<?>... attrs)
Set<? extends OpenOption> options,
FileAttribute<?>... attrs)
throws IOException {
checkOptions(options);
final boolean forWrite = (options.contains(StandardOpenOption.WRITE)
Expand Down Expand Up @@ -894,13 +953,13 @@ public void force(boolean metaData)
}

public long transferTo(long position, long count,
WritableByteChannel target)
WritableByteChannel target)
throws IOException {
return fch.transferTo(position, count, target);
}

public long transferFrom(ReadableByteChannel src,
long position, long count)
long position, long count)
throws IOException {
return fch.transferFrom(src, position, count);
}
Expand All @@ -925,7 +984,7 @@ public int write(ByteBuffer src, long position)
}

public MappedByteBuffer map(MapMode mode,
long position, long size)
long position, long size)
throws IOException {
throw new UnsupportedOperationException();
}
Expand Down Expand Up @@ -1046,6 +1105,8 @@ private final void endRead() {
private volatile boolean isOpen = true;
private final SeekableByteChannel ch; // channel to the zipfile
final byte[] cen; // CEN & ENDHDR
/* Replaces cen and inodes for read-only JARs; null otherwise. */
private final JarListing listing;
private END end;
private long locpos; // position of first LOC header (usually 0)

Expand Down Expand Up @@ -1323,8 +1384,8 @@ private void update(Entry e) {
// copy over the whole LOC entry (header if necessary, data and ext) from
// old zip to the new one.
private long copyLOCEntry(Entry e, boolean updateHeader,
OutputStream os,
long written, byte[] buf)
OutputStream os,
long written, byte[] buf)
throws IOException {
long locoff = e.locoff; // where to read
e.locoff = written; // update the e.locoff with new value
Expand Down Expand Up @@ -1538,14 +1599,45 @@ private PosixFileAttributes getPosixAttributes(Path path) throws IOException {
}
}

/**
* Returns how many index nodes this file system keeps in memory: 0 when it reads through a
* {@link JarListing}.
*/
int retainedIndexNodeCount() {
beginRead();
try {
return inodes == null ? 0 : inodes.size();
} finally {
endRead();
}
}

IndexNode getInode(byte[] path) {
if (path == null) {
throw new NullPointerException("path");
}
if (listing != null) {
int i = listing.find(path);
if (i < 0) {
return null;
}
// Transient and never used as a map key, so it skips hashing the name.
IndexNode inode = new IndexNode();
inode.name = path;
inode.isdir = listing.isDirectory(i);
return inode;
}
return inodes.get(IndexNode.keyOf(path));
}

Entry getEntry(byte[] path) throws IOException {
if (listing != null) {
int i = listing.find(path);
if (i < 0 || listing.localHeaderOffset(i) == -1) {
return null; // absent, or the root or a synthesized directory
}
return new Entry(listing, i);
}
IndexNode inode = getInode(path);
if (inode instanceof Entry) {
return (Entry) inode;
Expand Down Expand Up @@ -2282,6 +2374,19 @@ static class Entry extends IndexNode implements ZipFileAttributes {
this.file = file;
}

/* An entry read from a listing has no extra field or comment, so only the DOS time. */
Entry(JarListing listing, int i) {
name(listing.nameBytes(i));
this.isdir = listing.isDirectory(i);
this.flag = listing.flag(i);
this.method = listing.method(i);
this.mtime = dosToJavaTime(listing.dosTime(i));
this.crc = listing.crc(i);
this.csize = listing.compressedSize(i);
this.size = listing.size(i);
this.locoff = listing.localHeaderOffset(i);
}

int version() throws ZipException {
if (method == METHOD_DEFLATED) {
return 20;
Expand Down Expand Up @@ -2814,8 +2919,8 @@ private static class ExistingChannelCloser {
private final Set<InputStream> streams;

ExistingChannelCloser(Path path,
SeekableByteChannel ch,
Set<InputStream> streams) {
SeekableByteChannel ch,
Set<InputStream> streams) {
this.path = path;
this.ch = ch;
this.streams = streams;
Expand Down Expand Up @@ -2905,19 +3010,19 @@ public boolean equals(Object other) {
private boolean arrEquals(byte[] a, int aI, int aT, byte[] b, int bI, int bT) {
rangeCheck(a.length, aI, aT);
rangeCheck(b.length, bI, bT);

int aL = aT - aI;
int bL = bT - bI;
if (aL != bL) {
return false;
}

for (int i=0;i<aL;i++) {
if (a[aI + i] != b[bI + i]) {
return false;
}
}

return true;
}

Expand Down
Loading
Loading