src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystem.java
author clanger
Fri, 22 Nov 2019 09:25:09 +0100
changeset 59216 47c879f478d2
parent 59112 fe87a92570db
permissions -rw-r--r--
8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem Reviewed-by: lancea, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
     1
/*
53363
20a872899315 8211919: ZipDirectoryStream should provide a stream of paths that are relative to the directory
lancea
parents: 53043
diff changeset
     2
 * Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
23925
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
     4
 *
23925
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    10
 *
23925
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    15
 * accompanied this code).
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    16
 *
23925
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    20
 *
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    23
 * questions.
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    24
 */
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    25
23925
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    26
package jdk.nio.zipfs;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    27
10365
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
    28
import java.io.BufferedOutputStream;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    29
import java.io.ByteArrayInputStream;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    30
import java.io.ByteArrayOutputStream;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    31
import java.io.EOFException;
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
    32
import java.io.FilterOutputStream;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    33
import java.io.IOException;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    34
import java.io.InputStream;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    35
import java.io.OutputStream;
59216
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
    36
import java.lang.Runtime.Version;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    37
import java.nio.ByteBuffer;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    38
import java.nio.MappedByteBuffer;
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    39
import java.nio.channels.FileChannel;
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    40
import java.nio.channels.FileLock;
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    41
import java.nio.channels.ReadableByteChannel;
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    42
import java.nio.channels.SeekableByteChannel;
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    43
import java.nio.channels.WritableByteChannel;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    44
import java.nio.file.*;
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
    45
import java.nio.file.attribute.*;
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    46
import java.nio.file.spi.FileSystemProvider;
23925
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    47
import java.security.AccessController;
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    48
import java.security.PrivilegedAction;
24364
da8afb112f5d 8040059: Change default policy for extensions to no permission
mchung
parents: 23925
diff changeset
    49
import java.security.PrivilegedActionException;
da8afb112f5d 8040059: Change default policy for extensions to no permission
mchung
parents: 23925
diff changeset
    50
import java.security.PrivilegedExceptionAction;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    51
import java.util.*;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
    52
import java.util.concurrent.locks.ReadWriteLock;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
    53
import java.util.concurrent.locks.ReentrantReadWriteLock;
59216
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
    54
import java.util.function.Consumer;
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
    55
import java.util.function.Function;
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
    56
import java.util.jar.Attributes;
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
    57
import java.util.jar.Manifest;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    58
import java.util.regex.Pattern;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    59
import java.util.zip.CRC32;
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    60
import java.util.zip.Deflater;
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    61
import java.util.zip.DeflaterOutputStream;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    62
import java.util.zip.Inflater;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    63
import java.util.zip.InflaterInputStream;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    64
import java.util.zip.ZipException;
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    65
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    66
import static java.lang.Boolean.TRUE;
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    67
import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    68
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    69
import static java.nio.file.StandardOpenOption.APPEND;
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    70
import static java.nio.file.StandardOpenOption.CREATE;
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    71
import static java.nio.file.StandardOpenOption.CREATE_NEW;
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    72
import static java.nio.file.StandardOpenOption.READ;
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    73
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    74
import static java.nio.file.StandardOpenOption.WRITE;
23925
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    75
import static jdk.nio.zipfs.ZipConstants.*;
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    76
import static jdk.nio.zipfs.ZipUtils.*;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    77
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    78
/**
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    79
 * A FileSystem built on a zip file
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    80
 *
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    81
 * @author Xueming Shen
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    82
 */
23925
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    83
class ZipFileSystem extends FileSystem {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
    84
    // statics
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
    85
    private static final boolean isWindows = AccessController.doPrivileged(
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
    86
        (PrivilegedAction<Boolean>)()->System.getProperty("os.name")
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
    87
                                             .startsWith("Windows"));
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
    88
    private static final byte[] ROOTPATH = new byte[] { '/' };
58467
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
    89
    private static final String PROPERTY_POSIX = "enablePosixFileAttributes";
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
    90
    private static final String PROPERTY_DEFAULT_OWNER = "defaultOwner";
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
    91
    private static final String PROPERTY_DEFAULT_GROUP = "defaultGroup";
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
    92
    private static final String PROPERTY_DEFAULT_PERMISSIONS = "defaultPermissions";
59216
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
    93
    // Property used to specify the entry version to use for a multi-release JAR
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
    94
    private static final String PROPERTY_RELEASE_VERSION = "releaseVersion";
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
    95
    // Original property used to specify the entry version to use for a
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
    96
    // multi-release JAR which is kept for backwards compatibility.
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
    97
    private static final String PROPERTY_MULTI_RELEASE = "multi-release";
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
    98
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
    99
    private static final Set<PosixFilePermission> DEFAULT_PERMISSIONS =
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   100
        PosixFilePermissions.fromString("rwxrwxrwx");
58467
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   101
    // Property used to specify the compression mode to use
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   102
    private static final String PROPERTY_COMPRESSION_METHOD = "compressionMethod";
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   103
    // Value specified for compressionMethod property to compress Zip entries
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   104
    private static final String COMPRESSION_METHOD_DEFLATED = "DEFLATED";
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   105
    // Value specified for compressionMethod property to not compress Zip entries
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   106
    private static final String COMPRESSION_METHOD_STORED = "STORED";
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   107
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   108
    private final ZipFileSystemProvider provider;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   109
    private final Path zfpath;
38769
8046caf79f1c 8061777: (zipfs) IllegalArgumentException in ZipCoder.toString when using Shitft_JIS
sherman
parents: 37803
diff changeset
   110
    final ZipCoder zc;
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   111
    private final ZipPath rootdir;
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   112
    private boolean readOnly; // readonly file system, false by default
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   113
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   114
    // default time stamp for pseudo entries
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   115
    private final long zfsDefaultTimeStamp = System.currentTimeMillis();
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   116
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   117
    // configurable by env map
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   118
    private final boolean noExtt;        // see readExtra()
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   119
    private final boolean useTempFile;   // use a temp file for newOS, default
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   120
                                         // is to use BAOS for better performance
47223
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
   121
    private final boolean forceEnd64;
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   122
    private final int defaultCompressionMethod; // METHOD_STORED if "noCompression=true"
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   123
                                                // METHOD_DEFLATED otherwise
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   124
59216
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
   125
    // entryLookup is identity by default, will be overridden for multi-release jars
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
   126
    private Function<byte[], byte[]> entryLookup = Function.identity();
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
   127
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   128
    // POSIX support
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   129
    final boolean supportPosix;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   130
    private final UserPrincipal defaultOwner;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   131
    private final GroupPrincipal defaultGroup;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   132
    private final Set<PosixFilePermission> defaultPermissions;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   133
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   134
    private final Set<String> supportedFileAttributeViews;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   135
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   136
    ZipFileSystem(ZipFileSystemProvider provider,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   137
                  Path zfpath,
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
   138
                  Map<String, ?> env) throws IOException
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   139
    {
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   140
        // default encoding for name/comment
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   141
        String nameEncoding = env.containsKey("encoding") ?
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   142
            (String)env.get("encoding") : "UTF-8";
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   143
        this.noExtt = "false".equals(env.get("zipinfo-time"));
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   144
        this.useTempFile  = isTrue(env, "useTempFile");
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   145
        this.forceEnd64 = isTrue(env, "forceZIP64End");
58467
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   146
        this.defaultCompressionMethod = getDefaultCompressionMethod(env);
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   147
        this.supportPosix = isTrue(env, PROPERTY_POSIX);
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   148
        this.defaultOwner = initOwner(zfpath, env);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   149
        this.defaultGroup = initGroup(zfpath, env);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   150
        this.defaultPermissions = initPermissions(env);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   151
        this.supportedFileAttributeViews = supportPosix ?
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   152
            Set.of("basic", "posix", "zip") : Set.of("basic", "zip");
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   153
        if (Files.notExists(zfpath)) {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   154
            // create a new zip if it doesn't exist
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   155
            if (isTrue(env, "create")) {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   156
                try (OutputStream os = Files.newOutputStream(zfpath, CREATE_NEW, WRITE)) {
47223
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
   157
                    new END().write(os, 0, forceEnd64);
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   158
                }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   159
            } else {
58465
ff45c1bf8129 8223771: FileSystemProvider.newFileSystem(Path, Map) should throw IOException when called with a file that cannot be open
lancea
parents: 57842
diff changeset
   160
                throw new NoSuchFileException(zfpath.toString());
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   161
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   162
        }
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   163
        // sm and existence check
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   164
        zfpath.getFileSystem().provider().checkAccess(zfpath, AccessMode.READ);
24364
da8afb112f5d 8040059: Change default policy for extensions to no permission
mchung
parents: 23925
diff changeset
   165
        boolean writeable = AccessController.doPrivileged(
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   166
            (PrivilegedAction<Boolean>)()->Files.isWritable(zfpath));
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   167
        this.readOnly = !writeable;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   168
        this.zc = ZipCoder.get(nameEncoding);
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   169
        this.rootdir = new ZipPath(this, new byte[]{'/'});
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   170
        this.ch = Files.newByteChannel(zfpath, READ);
28562
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
   171
        try {
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
   172
            this.cen = initCEN();
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
   173
        } catch (IOException x) {
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
   174
            try {
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
   175
                this.ch.close();
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
   176
            } catch (IOException xx) {
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
   177
                x.addSuppressed(xx);
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
   178
            }
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
   179
            throw x;
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
   180
        }
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   181
        this.provider = provider;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   182
        this.zfpath = zfpath;
59216
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
   183
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
   184
        initializeReleaseVersion(env);
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   185
    }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   186
58467
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   187
    /**
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   188
     * Return the compression method to use (STORED or DEFLATED).  If the
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   189
     * property {@code commpressionMethod} is set use its value to determine
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   190
     * the compression method to use.  If the property is not set, then the
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   191
     * default compression is DEFLATED unless the property {@code noCompression}
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   192
     * is set which is supported for backwards compatibility.
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   193
     * @param env Zip FS map of properties
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   194
     * @return The Compression method to use
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   195
     */
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   196
    private int getDefaultCompressionMethod(Map<String, ?> env) {
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   197
        int result =
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   198
                isTrue(env, "noCompression") ? METHOD_STORED : METHOD_DEFLATED;
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   199
        if (env.containsKey(PROPERTY_COMPRESSION_METHOD)) {
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   200
            Object compressionMethod =  env.get(PROPERTY_COMPRESSION_METHOD);
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   201
            if (compressionMethod != null) {
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   202
                if (compressionMethod instanceof String) {
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   203
                    switch (((String) compressionMethod).toUpperCase()) {
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   204
                        case COMPRESSION_METHOD_STORED:
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   205
                            result = METHOD_STORED;
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   206
                            break;
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   207
                        case COMPRESSION_METHOD_DEFLATED:
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   208
                            result = METHOD_DEFLATED;
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   209
                            break;
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   210
                        default:
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   211
                            throw new IllegalArgumentException(String.format(
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   212
                                    "The value for the %s property must be %s or %s",
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   213
                                    PROPERTY_COMPRESSION_METHOD, COMPRESSION_METHOD_STORED,
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   214
                                    COMPRESSION_METHOD_DEFLATED));
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   215
                    }
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   216
                } else {
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   217
                    throw new IllegalArgumentException(String.format(
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   218
                            "The Object type for the %s property must be a String",
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   219
                            PROPERTY_COMPRESSION_METHOD));
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   220
                }
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   221
            } else {
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   222
                throw new IllegalArgumentException(String.format(
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   223
                        "The value for the %s property must be %s or %s",
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   224
                        PROPERTY_COMPRESSION_METHOD, COMPRESSION_METHOD_STORED,
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   225
                        COMPRESSION_METHOD_DEFLATED));
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   226
            }
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   227
        }
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   228
        return result;
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   229
    }
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   230
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   231
    // returns true if there is a name=true/"true" setting in env
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   232
    private static boolean isTrue(Map<String, ?> env, String name) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   233
        return "true".equals(env.get(name)) || TRUE.equals(env.get(name));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   234
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   235
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   236
    // Initialize the default owner for files inside the zip archive.
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   237
    // If not specified in env, it is the owner of the archive. If no owner can
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   238
    // be determined, we try to go with system property "user.name". If that's not
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   239
    // accessible, we return "<zipfs_default>".
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   240
    private UserPrincipal initOwner(Path zfpath, Map<String, ?> env) throws IOException {
58467
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   241
        Object o = env.get(PROPERTY_DEFAULT_OWNER);
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   242
        if (o == null) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   243
            try {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   244
                PrivilegedExceptionAction<UserPrincipal> pa = ()->Files.getOwner(zfpath);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   245
                return AccessController.doPrivileged(pa);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   246
            } catch (UnsupportedOperationException | PrivilegedActionException e) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   247
                if (e instanceof UnsupportedOperationException ||
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   248
                    e.getCause() instanceof NoSuchFileException)
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   249
                {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   250
                    PrivilegedAction<String> pa = ()->System.getProperty("user.name");
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   251
                    String userName = AccessController.doPrivileged(pa);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   252
                    return ()->userName;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   253
                } else {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   254
                    throw new IOException(e);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   255
                }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   256
            }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   257
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   258
        if (o instanceof String) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   259
            if (((String)o).isEmpty()) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   260
                throw new IllegalArgumentException("Value for property " +
58467
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   261
                        PROPERTY_DEFAULT_OWNER + " must not be empty.");
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   262
            }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   263
            return ()->(String)o;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   264
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   265
        if (o instanceof UserPrincipal) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   266
            return (UserPrincipal)o;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   267
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   268
        throw new IllegalArgumentException("Value for property " +
58467
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   269
                PROPERTY_DEFAULT_OWNER + " must be of type " + String.class +
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   270
            " or " + UserPrincipal.class);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   271
    }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   272
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   273
    // Initialize the default group for files inside the zip archive.
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   274
    // If not specified in env, we try to determine the group of the zip archive itself.
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   275
    // If this is not possible/unsupported, we will return a group principal going by
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   276
    // the same name as the default owner.
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   277
    private GroupPrincipal initGroup(Path zfpath, Map<String, ?> env) throws IOException {
58467
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   278
        Object o = env.get(PROPERTY_DEFAULT_GROUP);
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   279
        if (o == null) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   280
            try {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   281
                PosixFileAttributeView zfpv = Files.getFileAttributeView(zfpath, PosixFileAttributeView.class);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   282
                if (zfpv == null) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   283
                    return defaultOwner::getName;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   284
                }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   285
                PrivilegedExceptionAction<GroupPrincipal> pa = ()->zfpv.readAttributes().group();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   286
                return AccessController.doPrivileged(pa);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   287
            } catch (UnsupportedOperationException | PrivilegedActionException e) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   288
                if (e instanceof UnsupportedOperationException ||
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   289
                    e.getCause() instanceof NoSuchFileException)
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   290
                {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   291
                    return defaultOwner::getName;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   292
                } else {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   293
                    throw new IOException(e);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   294
                }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   295
            }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   296
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   297
        if (o instanceof String) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   298
            if (((String)o).isEmpty()) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   299
                throw new IllegalArgumentException("Value for property " +
58467
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   300
                        PROPERTY_DEFAULT_GROUP + " must not be empty.");
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   301
            }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   302
            return ()->(String)o;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   303
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   304
        if (o instanceof GroupPrincipal) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   305
            return (GroupPrincipal)o;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   306
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   307
        throw new IllegalArgumentException("Value for property " +
58467
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   308
                PROPERTY_DEFAULT_GROUP + " must be of type " + String.class +
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   309
            " or " + GroupPrincipal.class);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   310
    }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   311
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   312
    // Initialize the default permissions for files inside the zip archive.
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   313
    // If not specified in env, it will return 777.
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   314
    private Set<PosixFilePermission> initPermissions(Map<String, ?> env) {
58467
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   315
        Object o = env.get(PROPERTY_DEFAULT_PERMISSIONS);
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   316
        if (o == null) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   317
            return DEFAULT_PERMISSIONS;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   318
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   319
        if (o instanceof String) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   320
            return PosixFilePermissions.fromString((String)o);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   321
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   322
        if (!(o instanceof Set)) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   323
            throw new IllegalArgumentException("Value for property " +
58467
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   324
                PROPERTY_DEFAULT_PERMISSIONS + " must be of type " + String.class +
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   325
                " or " + Set.class);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   326
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   327
        Set<PosixFilePermission> perms = new HashSet<>();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   328
        for (Object o2 : (Set<?>)o) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   329
            if (o2 instanceof PosixFilePermission) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   330
                perms.add((PosixFilePermission)o2);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   331
            } else {
58467
72ef2c0faf47 8231093: Document the ZIP FS properties noCompression and releaseVersion
lancea
parents: 58465
diff changeset
   332
                throw new IllegalArgumentException(PROPERTY_DEFAULT_PERMISSIONS +
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   333
                    " must only contain objects of type " + PosixFilePermission.class);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   334
            }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   335
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   336
        return perms;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   337
    }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   338
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   339
    @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   340
    public FileSystemProvider provider() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   341
        return provider;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   342
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   343
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   344
    @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   345
    public String getSeparator() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   346
        return "/";
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   347
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   348
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   349
    @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   350
    public boolean isOpen() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   351
        return isOpen;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   352
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   353
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   354
    @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   355
    public boolean isReadOnly() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   356
        return readOnly;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   357
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   358
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   359
    private void checkWritable() {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   360
        if (readOnly) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   361
            throw new ReadOnlyFileSystemException();
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   362
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   363
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   364
34835
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
   365
    void setReadOnly() {
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
   366
        this.readOnly = true;
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
   367
    }
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
   368
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   369
    @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   370
    public Iterable<Path> getRootDirectories() {
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   371
        return List.of(rootdir);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   372
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   373
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   374
    ZipPath getRootDir() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   375
        return rootdir;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   376
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   377
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   378
    @Override
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   379
    public ZipPath getPath(String first, String... more) {
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   380
        if (more.length == 0) {
38769
8046caf79f1c 8061777: (zipfs) IllegalArgumentException in ZipCoder.toString when using Shitft_JIS
sherman
parents: 37803
diff changeset
   381
            return new ZipPath(this, first);
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   382
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   383
        StringBuilder sb = new StringBuilder();
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   384
        sb.append(first);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   385
        for (String path : more) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   386
            if (path.length() > 0) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   387
                if (sb.length() > 0) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   388
                    sb.append('/');
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   389
                }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   390
                sb.append(path);
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   391
            }
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   392
        }
38769
8046caf79f1c 8061777: (zipfs) IllegalArgumentException in ZipCoder.toString when using Shitft_JIS
sherman
parents: 37803
diff changeset
   393
        return new ZipPath(this, sb.toString());
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   394
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   395
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   396
    @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   397
    public UserPrincipalLookupService getUserPrincipalLookupService() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   398
        throw new UnsupportedOperationException();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   399
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   400
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   401
    @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   402
    public WatchService newWatchService() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   403
        throw new UnsupportedOperationException();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   404
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   405
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   406
    FileStore getFileStore(ZipPath path) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   407
        return new ZipFileStore(path);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   408
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   409
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   410
    @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   411
    public Iterable<FileStore> getFileStores() {
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   412
        return List.of(new ZipFileStore(rootdir));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   413
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   414
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   415
    @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   416
    public Set<String> supportedFileAttributeViews() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   417
        return supportedFileAttributeViews;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   418
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   419
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   420
    @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   421
    public String toString() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   422
        return zfpath.toString();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   423
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   424
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   425
    Path getZipFile() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   426
        return zfpath;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   427
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   428
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   429
    private static final String GLOB_SYNTAX = "glob";
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   430
    private static final String REGEX_SYNTAX = "regex";
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   431
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   432
    @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   433
    public PathMatcher getPathMatcher(String syntaxAndInput) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   434
        int pos = syntaxAndInput.indexOf(':');
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   435
        if (pos <= 0 || pos == syntaxAndInput.length()) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   436
            throw new IllegalArgumentException();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   437
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   438
        String syntax = syntaxAndInput.substring(0, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   439
        String input = syntaxAndInput.substring(pos + 1);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   440
        String expr;
29270
0f65e3c44659 8073445: (fs) FileSystem.getPathMatcher(...) should check syntax component without regard to case
bpb
parents: 28562
diff changeset
   441
        if (syntax.equalsIgnoreCase(GLOB_SYNTAX)) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   442
            expr = toRegexPattern(input);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   443
        } else {
29270
0f65e3c44659 8073445: (fs) FileSystem.getPathMatcher(...) should check syntax component without regard to case
bpb
parents: 28562
diff changeset
   444
            if (syntax.equalsIgnoreCase(REGEX_SYNTAX)) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   445
                expr = input;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   446
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   447
                throw new UnsupportedOperationException("Syntax '" + syntax +
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   448
                    "' not recognized");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   449
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   450
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   451
        // return matcher
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   452
        final Pattern pattern = Pattern.compile(expr);
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   453
        return (path)->pattern.matcher(path.toString()).matches();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   454
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   455
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   456
    @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   457
    public void close() throws IOException {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   458
        beginWrite();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   459
        try {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   460
            if (!isOpen)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   461
                return;
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   462
            isOpen = false;          // set closed
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   463
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   464
            endWrite();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   465
        }
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   466
        if (!streams.isEmpty()) {    // unlock and close all remaining streams
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   467
            Set<InputStream> copy = new HashSet<>(streams);
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
   468
            for (InputStream is : copy)
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   469
                is.close();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   470
        }
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   471
        beginWrite();                // lock and sync
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   472
        try {
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
   473
            AccessController.doPrivileged((PrivilegedExceptionAction<Void>)() -> {
24364
da8afb112f5d 8040059: Change default policy for extensions to no permission
mchung
parents: 23925
diff changeset
   474
                sync(); return null;
da8afb112f5d 8040059: Change default policy for extensions to no permission
mchung
parents: 23925
diff changeset
   475
            });
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   476
            ch.close();              // close the ch just in case no update
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   477
                                     // and sync didn't close the ch
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   478
        } catch (PrivilegedActionException e) {
24364
da8afb112f5d 8040059: Change default policy for extensions to no permission
mchung
parents: 23925
diff changeset
   479
            throw (IOException)e.getException();
da8afb112f5d 8040059: Change default policy for extensions to no permission
mchung
parents: 23925
diff changeset
   480
        } finally {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   481
            endWrite();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   482
        }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   483
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   484
        synchronized (inflaters) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   485
            for (Inflater inf : inflaters)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   486
                inf.end();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   487
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   488
        synchronized (deflaters) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   489
            for (Deflater def : deflaters)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   490
                def.end();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   491
        }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   492
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   493
        IOException ioe = null;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   494
        synchronized (tmppaths) {
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
   495
            for (Path p : tmppaths) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   496
                try {
24364
da8afb112f5d 8040059: Change default policy for extensions to no permission
mchung
parents: 23925
diff changeset
   497
                    AccessController.doPrivileged(
da8afb112f5d 8040059: Change default policy for extensions to no permission
mchung
parents: 23925
diff changeset
   498
                        (PrivilegedExceptionAction<Boolean>)() -> Files.deleteIfExists(p));
da8afb112f5d 8040059: Change default policy for extensions to no permission
mchung
parents: 23925
diff changeset
   499
                } catch (PrivilegedActionException e) {
da8afb112f5d 8040059: Change default policy for extensions to no permission
mchung
parents: 23925
diff changeset
   500
                    IOException x = (IOException)e.getException();
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   501
                    if (ioe == null)
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   502
                        ioe = x;
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   503
                    else
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   504
                        ioe.addSuppressed(x);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   505
                }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   506
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   507
        }
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   508
        provider.removeFileSystem(zfpath, this);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   509
        if (ioe != null)
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   510
           throw ioe;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   511
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   512
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   513
    ZipFileAttributes getFileAttributes(byte[] path)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   514
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   515
    {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   516
        beginRead();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   517
        try {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   518
            ensureOpen();
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   519
            IndexNode inode = getInode(path);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   520
            if (inode == null) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   521
                return null;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   522
            } else if (inode instanceof Entry) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   523
                return (Entry)inode;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   524
            } else if (inode.pos == -1) {
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   525
                // pseudo directory, uses METHOD_STORED
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   526
                Entry e = supportPosix ?
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   527
                    new PosixEntry(inode.name, inode.isdir, METHOD_STORED) :
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   528
                    new Entry(inode.name, inode.isdir, METHOD_STORED);
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   529
                e.mtime = e.atime = e.ctime = zfsDefaultTimeStamp;
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   530
                return e;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   531
            } else {
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   532
                return supportPosix ? new PosixEntry(this, inode) : new Entry(this, inode);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   533
            }
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   534
        } finally {
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   535
            endRead();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   536
        }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   537
    }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   538
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   539
    void checkAccess(byte[] path) throws IOException {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   540
        beginRead();
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   541
        try {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   542
            ensureOpen();
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   543
            // is it necessary to readCEN as a sanity check?
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   544
            if (getInode(path) == null) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   545
                throw new NoSuchFileException(toString());
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   546
            }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   547
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   548
        } finally {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   549
            endRead();
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   550
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   551
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   552
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   553
    void setTimes(byte[] path, FileTime mtime, FileTime atime, FileTime ctime)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   554
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   555
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   556
        checkWritable();
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   557
        beginWrite();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   558
        try {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   559
            ensureOpen();
34835
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
   560
            Entry e = getEntry(path);    // ensureOpen checked
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   561
            if (e == null)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   562
                throw new NoSuchFileException(getString(path));
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   563
            if (e.type == Entry.CEN)
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   564
                e.type = Entry.COPY;     // copy e
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   565
            if (mtime != null)
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   566
                e.mtime = mtime.toMillis();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   567
            if (atime != null)
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   568
                e.atime = atime.toMillis();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   569
            if (ctime != null)
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   570
                e.ctime = ctime.toMillis();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   571
            update(e);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   572
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   573
            endWrite();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   574
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   575
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   576
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   577
    void setOwner(byte[] path, UserPrincipal owner) throws IOException {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   578
        checkWritable();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   579
        beginWrite();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   580
        try {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   581
            ensureOpen();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   582
            Entry e = getEntry(path);    // ensureOpen checked
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   583
            if (e == null) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   584
                throw new NoSuchFileException(getString(path));
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   585
            }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   586
            // as the owner information is not persistent, we don't need to
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   587
            // change e.type to Entry.COPY
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   588
            if (e instanceof PosixEntry) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   589
                ((PosixEntry)e).owner = owner;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   590
                update(e);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   591
            }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   592
        } finally {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   593
            endWrite();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   594
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   595
    }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   596
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   597
    void setGroup(byte[] path, GroupPrincipal group) throws IOException {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   598
        checkWritable();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   599
        beginWrite();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   600
        try {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   601
            ensureOpen();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   602
            Entry e = getEntry(path);    // ensureOpen checked
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   603
            if (e == null) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   604
                throw new NoSuchFileException(getString(path));
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   605
            }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   606
            // as the group information is not persistent, we don't need to
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   607
            // change e.type to Entry.COPY
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   608
            if (e instanceof PosixEntry) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   609
                ((PosixEntry)e).group = group;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   610
                update(e);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   611
            }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   612
        } finally {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   613
            endWrite();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   614
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   615
    }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   616
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   617
    void setPermissions(byte[] path, Set<PosixFilePermission> perms) throws IOException {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   618
        checkWritable();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   619
        beginWrite();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   620
        try {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   621
            ensureOpen();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   622
            Entry e = getEntry(path);    // ensureOpen checked
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   623
            if (e == null) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   624
                throw new NoSuchFileException(getString(path));
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   625
            }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   626
            if (e.type == Entry.CEN) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   627
                e.type = Entry.COPY;     // copy e
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   628
            }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   629
            e.posixPerms = perms == null ? -1 : ZipUtils.permsToFlags(perms);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   630
            update(e);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   631
        } finally {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   632
            endWrite();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   633
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   634
    }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   635
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   636
    boolean exists(byte[] path) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   637
        beginRead();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   638
        try {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   639
            ensureOpen();
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   640
            return getInode(path) != null;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   641
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   642
            endRead();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   643
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   644
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   645
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   646
    boolean isDirectory(byte[] path) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   647
        beginRead();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   648
        try {
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   649
            IndexNode n = getInode(path);
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   650
            return n != null && n.isDir();
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   651
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   652
            endRead();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   653
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   654
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   655
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   656
    // returns the list of child paths of "path"
52008
6f04692c7d51 8211385: (zipfs) ZipDirectoryStream yields a stream of absolute paths when directory is relative
sherman
parents: 51795
diff changeset
   657
    Iterator<Path> iteratorOf(ZipPath dir,
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   658
                              DirectoryStream.Filter<? super Path> filter)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   659
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   660
    {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   661
        beginWrite();    // iteration of inodes needs exclusive lock
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   662
        try {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   663
            ensureOpen();
52008
6f04692c7d51 8211385: (zipfs) ZipDirectoryStream yields a stream of absolute paths when directory is relative
sherman
parents: 51795
diff changeset
   664
            byte[] path = dir.getResolvedPath();
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   665
            IndexNode inode = getInode(path);
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   666
            if (inode == null)
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   667
                throw new NotDirectoryException(getString(path));
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   668
            List<Path> list = new ArrayList<>();
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   669
            IndexNode child = inode.child;
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   670
            while (child != null) {
53363
20a872899315 8211919: ZipDirectoryStream should provide a stream of paths that are relative to the directory
lancea
parents: 53043
diff changeset
   671
                // (1) Assume each path from the zip file itself is "normalized"
52008
6f04692c7d51 8211385: (zipfs) ZipDirectoryStream yields a stream of absolute paths when directory is relative
sherman
parents: 51795
diff changeset
   672
                // (2) IndexNode.name is absolute. see IndexNode(byte[],int,int)
53363
20a872899315 8211919: ZipDirectoryStream should provide a stream of paths that are relative to the directory
lancea
parents: 53043
diff changeset
   673
                // (3) If parent "dir" is relative when ZipDirectoryStream
52008
6f04692c7d51 8211385: (zipfs) ZipDirectoryStream yields a stream of absolute paths when directory is relative
sherman
parents: 51795
diff changeset
   674
                //     is created, the returned child path needs to be relative
6f04692c7d51 8211385: (zipfs) ZipDirectoryStream yields a stream of absolute paths when directory is relative
sherman
parents: 51795
diff changeset
   675
                //     as well.
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   676
                ZipPath childPath = new ZipPath(this, child.name, true);
53363
20a872899315 8211919: ZipDirectoryStream should provide a stream of paths that are relative to the directory
lancea
parents: 53043
diff changeset
   677
                ZipPath childFileName = childPath.getFileName();
20a872899315 8211919: ZipDirectoryStream should provide a stream of paths that are relative to the directory
lancea
parents: 53043
diff changeset
   678
                ZipPath zpath = dir.resolve(childFileName);
52008
6f04692c7d51 8211385: (zipfs) ZipDirectoryStream yields a stream of absolute paths when directory is relative
sherman
parents: 51795
diff changeset
   679
                if (filter == null || filter.accept(zpath))
6f04692c7d51 8211385: (zipfs) ZipDirectoryStream yields a stream of absolute paths when directory is relative
sherman
parents: 51795
diff changeset
   680
                    list.add(zpath);
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   681
                child = child.sibling;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   682
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   683
            return list.iterator();
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   684
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   685
            endWrite();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   686
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   687
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   688
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   689
    void createDirectory(byte[] dir, FileAttribute<?>... attrs) throws IOException {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   690
        checkWritable();
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   691
        beginWrite();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   692
        try {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   693
            ensureOpen();
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   694
            if (dir.length == 0 || exists(dir))  // root dir, or existing dir
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   695
                throw new FileAlreadyExistsException(getString(dir));
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   696
            checkParents(dir);
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   697
            Entry e = supportPosix ?
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   698
                new PosixEntry(dir, Entry.NEW, true, METHOD_STORED, attrs) :
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   699
                new Entry(dir, Entry.NEW, true, METHOD_STORED, attrs);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   700
            update(e);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   701
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   702
            endWrite();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   703
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   704
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   705
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   706
    void copyFile(boolean deletesrc, byte[]src, byte[] dst, CopyOption... options)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   707
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   708
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   709
        checkWritable();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   710
        if (Arrays.equals(src, dst))
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   711
            return;    // do nothing, src and dst are the same
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   712
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   713
        beginWrite();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   714
        try {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   715
            ensureOpen();
34835
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
   716
            Entry eSrc = getEntry(src);  // ensureOpen checked
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   717
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   718
            if (eSrc == null)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   719
                throw new NoSuchFileException(getString(src));
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   720
            if (eSrc.isDir()) {    // spec says to create dst dir
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   721
                createDirectory(dst);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   722
                return;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   723
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   724
            boolean hasReplace = false;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   725
            boolean hasCopyAttrs = false;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   726
            for (CopyOption opt : options) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   727
                if (opt == REPLACE_EXISTING)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   728
                    hasReplace = true;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   729
                else if (opt == COPY_ATTRIBUTES)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   730
                    hasCopyAttrs = true;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   731
            }
34835
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
   732
            Entry eDst = getEntry(dst);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   733
            if (eDst != null) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   734
                if (!hasReplace)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   735
                    throw new FileAlreadyExistsException(getString(dst));
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   736
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   737
                checkParents(dst);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   738
            }
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   739
            // copy eSrc entry and change name
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   740
            Entry u = supportPosix ?
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   741
                new PosixEntry((PosixEntry)eSrc, Entry.COPY) :
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   742
                new Entry(eSrc, Entry.COPY);
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   743
            u.name(dst);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   744
            if (eSrc.type == Entry.NEW || eSrc.type == Entry.FILECH) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   745
                u.type = eSrc.type;    // make it the same type
16857
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 14342
diff changeset
   746
                if (deletesrc) {       // if it's a "rename", take the data
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 14342
diff changeset
   747
                    u.bytes = eSrc.bytes;
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 14342
diff changeset
   748
                    u.file = eSrc.file;
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 14342
diff changeset
   749
                } else {               // if it's not "rename", copy the data
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   750
                    if (eSrc.bytes != null)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   751
                        u.bytes = Arrays.copyOf(eSrc.bytes, eSrc.bytes.length);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   752
                    else if (eSrc.file != null) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   753
                        u.file = getTempPathForEntry(null);
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   754
                        Files.copy(eSrc.file, u.file, REPLACE_EXISTING);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   755
                    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   756
                }
58845
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 58822
diff changeset
   757
            } else if (eSrc.type == Entry.CEN && eSrc.method != defaultCompressionMethod) {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 58822
diff changeset
   758
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 58822
diff changeset
   759
                /**
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 58822
diff changeset
   760
                 * We are copying a file within the same Zip file using a
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 58822
diff changeset
   761
                 * different compression method.
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 58822
diff changeset
   762
                 */
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 58822
diff changeset
   763
                try (InputStream in = newInputStream(src);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 58822
diff changeset
   764
                     OutputStream out = newOutputStream(dst,
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 58822
diff changeset
   765
                             CREATE, TRUNCATE_EXISTING, WRITE)) {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 58822
diff changeset
   766
                    in.transferTo(out);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 58822
diff changeset
   767
                }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 58822
diff changeset
   768
                u = getEntry(dst);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   769
            }
58845
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 58822
diff changeset
   770
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   771
            if (!hasCopyAttrs)
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   772
                u.mtime = u.atime= u.ctime = System.currentTimeMillis();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   773
            update(u);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   774
            if (deletesrc)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   775
                updateDelete(eSrc);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   776
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   777
            endWrite();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   778
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   779
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   780
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   781
    // Returns an output stream for writing the contents into the specified
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   782
    // entry.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   783
    OutputStream newOutputStream(byte[] path, OpenOption... options)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   784
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   785
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   786
        checkWritable();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   787
        boolean hasCreateNew = false;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   788
        boolean hasCreate = false;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   789
        boolean hasAppend = false;
30811
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   790
        boolean hasTruncate = false;
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
   791
        for (OpenOption opt : options) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   792
            if (opt == READ)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   793
                throw new IllegalArgumentException("READ not allowed");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   794
            if (opt == CREATE_NEW)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   795
                hasCreateNew = true;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   796
            if (opt == CREATE)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   797
                hasCreate = true;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   798
            if (opt == APPEND)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   799
                hasAppend = true;
30811
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   800
            if (opt == TRUNCATE_EXISTING)
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   801
                hasTruncate = true;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   802
        }
30811
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   803
        if (hasAppend && hasTruncate)
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   804
            throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed");
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   805
        beginRead();                 // only need a readlock, the "update()" will
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   806
        try {                        // try to obtain a writelock when the os is
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   807
            ensureOpen();            // being closed.
34835
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
   808
            Entry e = getEntry(path);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   809
            if (e != null) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   810
                if (e.isDir() || hasCreateNew)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   811
                    throw new FileAlreadyExistsException(getString(path));
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   812
                if (hasAppend) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   813
                    OutputStream os = getOutputStream(new Entry(e, Entry.NEW));
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   814
                    try (InputStream is = getInputStream(e)) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   815
                        is.transferTo(os);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   816
                    }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   817
                    return os;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   818
                }
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   819
                return getOutputStream(supportPosix ?
58845
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 58822
diff changeset
   820
                    new PosixEntry((PosixEntry)e, Entry.NEW, defaultCompressionMethod)
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 58822
diff changeset
   821
                        : new Entry(e, Entry.NEW, defaultCompressionMethod));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   822
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   823
                if (!hasCreate && !hasCreateNew)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   824
                    throw new NoSuchFileException(getString(path));
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   825
                checkParents(path);
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   826
                return getOutputStream(supportPosix ?
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   827
                    new PosixEntry(path, Entry.NEW, false, defaultCompressionMethod) :
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   828
                    new Entry(path, Entry.NEW, false, defaultCompressionMethod));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   829
            }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   830
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   831
            endRead();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   832
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   833
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   834
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   835
    // Returns an input stream for reading the contents of the specified
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   836
    // file entry.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   837
    InputStream newInputStream(byte[] path) throws IOException {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   838
        beginRead();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   839
        try {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   840
            ensureOpen();
34835
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
   841
            Entry e = getEntry(path);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   842
            if (e == null)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   843
                throw new NoSuchFileException(getString(path));
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   844
            if (e.isDir())
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   845
                throw new FileSystemException(getString(path), "is a directory", null);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   846
            return getInputStream(e);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   847
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   848
            endRead();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   849
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   850
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   851
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   852
    private void checkOptions(Set<? extends OpenOption> options) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   853
        // check for options of null type and option is an intance of StandardOpenOption
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   854
        for (OpenOption option : options) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   855
            if (option == null)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   856
                throw new NullPointerException();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   857
            if (!(option instanceof StandardOpenOption))
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   858
                throw new IllegalArgumentException();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   859
        }
30811
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   860
        if (options.contains(APPEND) && options.contains(TRUNCATE_EXISTING))
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   861
            throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed");
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   862
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   863
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   864
    // Returns an output SeekableByteChannel for either
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   865
    // (1) writing the contents of a new entry, if the entry doesn't exist, or
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   866
    // (2) updating/replacing the contents of an existing entry.
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
   867
    // Note: The content of the channel is not compressed until the
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
   868
    // channel is closed
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   869
    private class EntryOutputChannel extends ByteArrayChannel {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   870
        final Entry e;
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   871
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   872
        EntryOutputChannel(Entry e) {
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   873
            super(e.size > 0? (int)e.size : 8192, false);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   874
            this.e = e;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   875
            if (e.mtime == -1)
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   876
                e.mtime = System.currentTimeMillis();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   877
            if (e.method == -1)
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   878
                e.method = defaultCompressionMethod;
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   879
            // store size, compressed size, and crc-32 in datadescriptor
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   880
            e.flag = FLAG_DATADESCR;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   881
            if (zc.isUTF8())
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   882
                e.flag |= FLAG_USE_UTF8;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   883
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   884
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   885
        @Override
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   886
        public void close() throws IOException {
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
   887
            // will update the entry
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
   888
            try (OutputStream os = getOutputStream(e)) {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
   889
                os.write(toByteArray());
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
   890
            }
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   891
            super.close();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   892
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   893
    }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   894
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
   895
    // Returns a Writable/ReadByteChannel for now. Might consider to use
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   896
    // newFileChannel() instead, which dump the entry data into a regular
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   897
    // file on the default file system and create a FileChannel on top of it.
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   898
    SeekableByteChannel newByteChannel(byte[] path,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   899
                                       Set<? extends OpenOption> options,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   900
                                       FileAttribute<?>... attrs)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   901
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   902
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   903
        checkOptions(options);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   904
        if (options.contains(StandardOpenOption.WRITE) ||
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   905
            options.contains(StandardOpenOption.APPEND)) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   906
            checkWritable();
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
   907
            beginRead();    // only need a read lock, the "update()" will obtain
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
   908
                            // the write lock when the channel is closed
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   909
            try {
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   910
                Entry e = getEntry(path);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   911
                if (e != null) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   912
                    if (e.isDir() || options.contains(CREATE_NEW))
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   913
                        throw new FileAlreadyExistsException(getString(path));
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   914
                    SeekableByteChannel sbc =
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   915
                            new EntryOutputChannel(supportPosix ?
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   916
                                new PosixEntry((PosixEntry)e, Entry.NEW) :
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   917
                                new Entry(e, Entry.NEW));
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   918
                    if (options.contains(APPEND)) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   919
                        try (InputStream is = getInputStream(e)) {  // copyover
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   920
                            byte[] buf = new byte[8192];
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   921
                            ByteBuffer bb = ByteBuffer.wrap(buf);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   922
                            int n;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   923
                            while ((n = is.read(buf)) != -1) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   924
                                bb.position(0);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   925
                                bb.limit(n);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   926
                                sbc.write(bb);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   927
                            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   928
                        }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   929
                    }
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   930
                    return sbc;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   931
                }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   932
                if (!options.contains(CREATE) && !options.contains(CREATE_NEW))
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   933
                    throw new NoSuchFileException(getString(path));
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   934
                checkParents(path);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   935
                return new EntryOutputChannel(
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   936
                    supportPosix ?
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   937
                        new PosixEntry(path, Entry.NEW, false, defaultCompressionMethod, attrs) :
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   938
                        new Entry(path, Entry.NEW, false, defaultCompressionMethod, attrs));
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   939
            } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   940
                endRead();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   941
            }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   942
        } else {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   943
            beginRead();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   944
            try {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   945
                ensureOpen();
34835
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
   946
                Entry e = getEntry(path);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   947
                if (e == null || e.isDir())
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   948
                    throw new NoSuchFileException(getString(path));
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   949
                try (InputStream is = getInputStream(e)) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   950
                    // TBD: if (e.size < NNNNN);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   951
                    return new ByteArrayChannel(is.readAllBytes(), true);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   952
                }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   953
            } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   954
                endRead();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   955
            }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   956
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   957
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   958
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   959
    // Returns a FileChannel of the specified entry.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   960
    //
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   961
    // This implementation creates a temporary file on the default file system,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   962
    // copy the entry data into it if the entry exists, and then create a
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   963
    // FileChannel on top of it.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   964
    FileChannel newFileChannel(byte[] path,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   965
                               Set<? extends OpenOption> options,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   966
                               FileAttribute<?>... attrs)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   967
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   968
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   969
        checkOptions(options);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   970
        final  boolean forWrite = (options.contains(StandardOpenOption.WRITE) ||
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   971
                                   options.contains(StandardOpenOption.APPEND));
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   972
        beginRead();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   973
        try {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   974
            ensureOpen();
34835
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
   975
            Entry e = getEntry(path);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   976
            if (forWrite) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   977
                checkWritable();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   978
                if (e == null) {
30811
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   979
                    if (!options.contains(StandardOpenOption.CREATE) &&
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   980
                        !options.contains(StandardOpenOption.CREATE_NEW)) {
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   981
                        throw new NoSuchFileException(getString(path));
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   982
                    }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   983
                } else {
30811
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   984
                    if (options.contains(StandardOpenOption.CREATE_NEW)) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   985
                        throw new FileAlreadyExistsException(getString(path));
30811
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   986
                    }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   987
                    if (e.isDir())
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   988
                        throw new FileAlreadyExistsException("directory <"
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   989
                            + getString(path) + "> exists");
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   990
                }
30811
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   991
                options = new HashSet<>(options);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   992
                options.remove(StandardOpenOption.CREATE_NEW); // for tmpfile
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   993
            } else if (e == null || e.isDir()) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   994
                throw new NoSuchFileException(getString(path));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   995
            }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   996
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   997
            final boolean isFCH = (e != null && e.type == Entry.FILECH);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   998
            final Path tmpfile = isFCH ? e.file : getTempPathForEntry(path);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   999
            final FileChannel fch = tmpfile.getFileSystem()
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1000
                                           .provider()
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1001
                                           .newFileChannel(tmpfile, options, attrs);
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  1002
            final Entry u = isFCH ? e : (
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  1003
                supportPosix ?
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  1004
                new PosixEntry(path, tmpfile, Entry.FILECH, attrs) :
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  1005
                new Entry(path, tmpfile, Entry.FILECH, attrs));
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1006
            if (forWrite) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1007
                u.flag = FLAG_DATADESCR;
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1008
                u.method = defaultCompressionMethod;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1009
            }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1010
            // is there a better way to hook into the FileChannel's close method?
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1011
            return new FileChannel() {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1012
                public int write(ByteBuffer src) throws IOException {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1013
                    return fch.write(src);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1014
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1015
                public long write(ByteBuffer[] srcs, int offset, int length)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1016
                    throws IOException
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1017
                {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1018
                    return fch.write(srcs, offset, length);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1019
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1020
                public long position() throws IOException {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1021
                    return fch.position();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1022
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1023
                public FileChannel position(long newPosition)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1024
                    throws IOException
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1025
                {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1026
                    fch.position(newPosition);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1027
                    return this;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1028
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1029
                public long size() throws IOException {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1030
                    return fch.size();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1031
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1032
                public FileChannel truncate(long size)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1033
                    throws IOException
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1034
                {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1035
                    fch.truncate(size);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1036
                    return this;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1037
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1038
                public void force(boolean metaData)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1039
                    throws IOException
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1040
                {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1041
                    fch.force(metaData);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1042
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1043
                public long transferTo(long position, long count,
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1044
                                       WritableByteChannel target)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1045
                    throws IOException
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1046
                {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1047
                    return fch.transferTo(position, count, target);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1048
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1049
                public long transferFrom(ReadableByteChannel src,
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1050
                                         long position, long count)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1051
                    throws IOException
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1052
                {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1053
                    return fch.transferFrom(src, position, count);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1054
                }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1055
                public int read(ByteBuffer dst) throws IOException {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1056
                    return fch.read(dst);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1057
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1058
                public int read(ByteBuffer dst, long position)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1059
                    throws IOException
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1060
                {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1061
                    return fch.read(dst, position);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1062
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1063
                public long read(ByteBuffer[] dsts, int offset, int length)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1064
                    throws IOException
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1065
                {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1066
                    return fch.read(dsts, offset, length);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1067
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1068
                public int write(ByteBuffer src, long position)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1069
                    throws IOException
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1070
                    {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1071
                   return fch.write(src, position);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1072
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1073
                public MappedByteBuffer map(MapMode mode,
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1074
                                            long position, long size)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1075
                {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1076
                    throw new UnsupportedOperationException();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1077
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1078
                public FileLock lock(long position, long size, boolean shared)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1079
                    throws IOException
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1080
                {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1081
                    return fch.lock(position, size, shared);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1082
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1083
                public FileLock tryLock(long position, long size, boolean shared)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1084
                    throws IOException
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1085
                {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1086
                    return fch.tryLock(position, size, shared);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1087
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1088
                protected void implCloseChannel() throws IOException {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1089
                    fch.close();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1090
                    if (forWrite) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1091
                        u.mtime = System.currentTimeMillis();
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1092
                        u.size = Files.size(u.file);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1093
                        update(u);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1094
                    } else {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1095
                        if (!isFCH)    // if this is a new fch for reading
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1096
                            removeTempPathForEntry(tmpfile);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1097
                    }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1098
               }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1099
            };
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1100
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1101
            endRead();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1102
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1103
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1104
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1105
    // the outstanding input streams that need to be closed
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1106
    private Set<InputStream> streams =
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1107
        Collections.synchronizedSet(new HashSet<>());
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1108
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1109
    // the ex-channel and ex-path that need to close when their outstanding
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1110
    // input streams are all closed by the obtainers.
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1111
    private final Set<ExistingChannelCloser> exChClosers = new HashSet<>();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1112
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1113
    private final Set<Path> tmppaths = Collections.synchronizedSet(new HashSet<>());
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1114
    private Path getTempPathForEntry(byte[] path) throws IOException {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1115
        Path tmpPath = createTempFileInSameDirectoryAs(zfpath);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1116
        if (path != null) {
34835
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
  1117
            Entry e = getEntry(path);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1118
            if (e != null) {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1119
                try (InputStream is = newInputStream(path)) {
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1120
                    Files.copy(is, tmpPath, REPLACE_EXISTING);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1121
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1122
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1123
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1124
        return tmpPath;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1125
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1126
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1127
    private void removeTempPathForEntry(Path path) throws IOException {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1128
        Files.delete(path);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1129
        tmppaths.remove(path);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1130
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1131
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1132
    // check if all parents really exist. ZIP spec does not require
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1133
    // the existence of any "parent directory".
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1134
    private void checkParents(byte[] path) throws IOException {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1135
        beginRead();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1136
        try {
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1137
            while ((path = getParent(path)) != null &&
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1138
                    path != ROOTPATH) {
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1139
                if (!inodes.containsKey(IndexNode.keyOf(path))) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1140
                    throw new NoSuchFileException(getString(path));
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1141
                }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1142
            }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1143
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1144
            endRead();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1145
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1146
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1147
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1148
    private static byte[] getParent(byte[] path) {
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1149
        int off = getParentOff(path);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1150
        if (off <= 1)
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1151
            return ROOTPATH;
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  1152
        return Arrays.copyOf(path, off);
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1153
    }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1154
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1155
    private static int getParentOff(byte[] path) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1156
        int off = path.length - 1;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1157
        if (off > 0 && path[off] == '/')  // isDirectory
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1158
            off--;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1159
        while (off > 0 && path[off] != '/') { off--; }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1160
        return off;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1161
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1162
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1163
    private void beginWrite() {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1164
        rwlock.writeLock().lock();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1165
    }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1166
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1167
    private void endWrite() {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1168
        rwlock.writeLock().unlock();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1169
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1170
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1171
    private void beginRead() {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1172
        rwlock.readLock().lock();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1173
    }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1174
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1175
    private void endRead() {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1176
        rwlock.readLock().unlock();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1177
    }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1178
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1179
    ///////////////////////////////////////////////////////////////////
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1180
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1181
    private volatile boolean isOpen = true;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1182
    private final SeekableByteChannel ch; // channel to the zipfile
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1183
    final byte[]  cen;     // CEN & ENDHDR
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1184
    private END  end;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1185
    private long locpos;   // position of first LOC header (usually 0)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1186
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1187
    private final ReadWriteLock rwlock = new ReentrantReadWriteLock();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1188
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1189
    // name -> pos (in cen), IndexNode itself can be used as a "key"
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1190
    private LinkedHashMap<IndexNode, IndexNode> inodes;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1191
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1192
    final byte[] getBytes(String name) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1193
        return zc.getBytes(name);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1194
    }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1195
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1196
    final String getString(byte[] name) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1197
        return zc.toString(name);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1198
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1199
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 43224
diff changeset
  1200
    @SuppressWarnings("deprecation")
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1201
    protected void finalize() throws IOException {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1202
        close();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1203
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1204
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1205
    // Reads len bytes of data from the specified offset into buf.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1206
    // Returns the total number of bytes read.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1207
    // Each/every byte read from here (except the cen, which is mapped).
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1208
    final long readFullyAt(byte[] buf, int off, long len, long pos)
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1209
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1210
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1211
        ByteBuffer bb = ByteBuffer.wrap(buf);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1212
        bb.position(off);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1213
        bb.limit((int)(off + len));
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1214
        return readFullyAt(bb, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1215
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1216
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1217
    private long readFullyAt(ByteBuffer bb, long pos) throws IOException {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1218
        synchronized(ch) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1219
            return ch.position(pos).read(bb);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1220
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1221
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1222
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1223
    // Searches for end of central directory (END) header. The contents of
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1224
    // the END header will be read and placed in endbuf. Returns the file
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1225
    // position of the END header, otherwise returns -1 if the END header
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1226
    // was not found or an error occurred.
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1227
    private END findEND() throws IOException {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1228
        byte[] buf = new byte[READBLOCKSZ];
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1229
        long ziplen = ch.size();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1230
        long minHDR = (ziplen - END_MAXLEN) > 0 ? ziplen - END_MAXLEN : 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1231
        long minPos = minHDR - (buf.length - ENDHDR);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1232
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1233
        for (long pos = ziplen - buf.length; pos >= minPos; pos -= (buf.length - ENDHDR)) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1234
            int off = 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1235
            if (pos < 0) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1236
                // Pretend there are some NUL bytes before start of file
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1237
                off = (int)-pos;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1238
                Arrays.fill(buf, 0, off, (byte)0);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1239
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1240
            int len = buf.length - off;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1241
            if (readFullyAt(buf, off, len, pos + off) != len)
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1242
                throw new ZipException("zip END header not found");
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1243
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1244
            // Now scan the block backwards for END header signature
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1245
            for (int i = buf.length - ENDHDR; i >= 0; i--) {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1246
                if (buf[i]   == (byte)'P'    &&
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1247
                    buf[i+1] == (byte)'K'    &&
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1248
                    buf[i+2] == (byte)'\005' &&
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1249
                    buf[i+3] == (byte)'\006' &&
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1250
                    (pos + i + ENDHDR + ENDCOM(buf, i) == ziplen)) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1251
                    // Found END header
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1252
                    buf = Arrays.copyOfRange(buf, i, i + ENDHDR);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1253
                    END end = new END();
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1254
                    // end.endsub = ENDSUB(buf); // not used
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1255
                    end.centot = ENDTOT(buf);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1256
                    end.cenlen = ENDSIZ(buf);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1257
                    end.cenoff = ENDOFF(buf);
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1258
                    // end.comlen = ENDCOM(buf); // not used
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1259
                    end.endpos = pos + i;
47223
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1260
                    // try if there is zip64 end;
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1261
                    byte[] loc64 = new byte[ZIP64_LOCHDR];
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1262
                    if (end.endpos < ZIP64_LOCHDR ||
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1263
                        readFullyAt(loc64, 0, loc64.length, end.endpos - ZIP64_LOCHDR)
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1264
                        != loc64.length ||
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1265
                        !locator64SigAt(loc64, 0)) {
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1266
                        return end;
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1267
                    }
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1268
                    long end64pos = ZIP64_LOCOFF(loc64);
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1269
                    byte[] end64buf = new byte[ZIP64_ENDHDR];
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1270
                    if (readFullyAt(end64buf, 0, end64buf.length, end64pos)
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1271
                        != end64buf.length ||
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1272
                        !end64SigAt(end64buf, 0)) {
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1273
                        return end;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1274
                    }
47223
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1275
                    // end64 found,
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1276
                    long cenlen64 = ZIP64_ENDSIZ(end64buf);
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1277
                    long cenoff64 = ZIP64_ENDOFF(end64buf);
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1278
                    long centot64 = ZIP64_ENDTOT(end64buf);
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1279
                    // double-check
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1280
                    if (cenlen64 != end.cenlen && end.cenlen != ZIP64_MINVAL ||
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1281
                        cenoff64 != end.cenoff && end.cenoff != ZIP64_MINVAL ||
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1282
                        centot64 != end.centot && end.centot != ZIP64_MINVAL32) {
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1283
                        return end;
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1284
                    }
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1285
                    // to use the end64 values
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1286
                    end.cenlen = cenlen64;
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1287
                    end.cenoff = cenoff64;
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1288
                    end.centot = (int)centot64; // assume total < 2g
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1289
                    end.endpos = end64pos;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1290
                    return end;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1291
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1292
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1293
        }
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1294
        throw new ZipException("zip END header not found");
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1295
    }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1296
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1297
    private void makeParentDirs(IndexNode node, IndexNode root) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1298
        IndexNode parent;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1299
        ParentLookup lookup = new ParentLookup();
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1300
        while (true) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1301
            int off = getParentOff(node.name);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1302
            // parent is root
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1303
            if (off <= 1) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1304
                node.sibling = root.child;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1305
                root.child = node;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1306
                break;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1307
            }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1308
            // parent exists
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1309
            lookup = lookup.as(node.name, off);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1310
            if (inodes.containsKey(lookup)) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1311
                parent = inodes.get(lookup);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1312
                node.sibling = parent.child;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1313
                parent.child = node;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1314
                break;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1315
            }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1316
            // parent does not exist, add new pseudo directory entry
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1317
            parent = new IndexNode(Arrays.copyOf(node.name, off), true);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1318
            inodes.put(parent, parent);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1319
            node.sibling = parent.child;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1320
            parent.child = node;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1321
            node = parent;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1322
        }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1323
    }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1324
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1325
    // ZIP directory has two issues:
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1326
    // (1) ZIP spec does not require the ZIP file to include
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1327
    //     directory entry
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1328
    // (2) all entries are not stored/organized in a "tree"
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1329
    //     structure.
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1330
    // A possible solution is to build the node tree ourself as
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1331
    // implemented below.
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1332
    private void buildNodeTree() {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1333
        beginWrite();
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1334
        try {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1335
            IndexNode root = inodes.remove(LOOKUPKEY.as(ROOTPATH));
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1336
            if (root == null) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1337
                root = new IndexNode(ROOTPATH, true);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1338
            }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1339
            IndexNode[] nodes = inodes.values().toArray(new IndexNode[0]);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1340
            inodes.put(root, root);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1341
            for (IndexNode node : nodes) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1342
                makeParentDirs(node, root);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1343
            }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1344
        } finally {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1345
            endWrite();
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1346
        }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1347
    }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1348
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1349
    private void removeFromTree(IndexNode inode) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1350
        IndexNode parent = inodes.get(LOOKUPKEY.as(getParent(inode.name)));
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1351
        IndexNode child = parent.child;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1352
        if (child.equals(inode)) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1353
            parent.child = child.sibling;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1354
        } else {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1355
            IndexNode last = child;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1356
            while ((child = child.sibling) != null) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1357
                if (child.equals(inode)) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1358
                    last.sibling = child.sibling;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1359
                    break;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1360
                } else {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1361
                    last = child;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1362
                }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1363
            }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1364
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1365
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1366
59216
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1367
    /**
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1368
     * If a version property has been specified and the file represents a multi-release JAR,
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1369
     * determine the requested runtime version and initialize the ZipFileSystem instance accordingly.
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1370
     *
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1371
     * Checks if the Zip File System property "releaseVersion" has been specified. If it has,
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1372
     * use its value to determine the requested version. If not use the value of the "multi-release" property.
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1373
     */
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1374
    private void initializeReleaseVersion(Map<String, ?> env) throws IOException {
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1375
        Object o = env.containsKey(PROPERTY_RELEASE_VERSION) ?
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1376
            env.get(PROPERTY_RELEASE_VERSION) :
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1377
            env.get(PROPERTY_MULTI_RELEASE);
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1378
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1379
        if (o != null && isMultiReleaseJar()) {
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1380
            int version;
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1381
            if (o instanceof String) {
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1382
                String s = (String)o;
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1383
                if (s.equals("runtime")) {
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1384
                    version = Runtime.version().feature();
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1385
                } else if (s.matches("^[1-9][0-9]*$")) {
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1386
                    version = Version.parse(s).feature();
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1387
                } else {
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1388
                    throw new IllegalArgumentException("Invalid runtime version");
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1389
                }
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1390
            } else if (o instanceof Integer) {
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1391
                version = Version.parse(((Integer)o).toString()).feature();
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1392
            } else if (o instanceof Version) {
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1393
                version = ((Version)o).feature();
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1394
            } else {
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1395
                throw new IllegalArgumentException("env parameter must be String, " +
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1396
                    "Integer, or Version");
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1397
            }
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1398
            createVersionedLinks(version < 0 ? 0 : version);
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1399
            setReadOnly();
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1400
        }
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1401
    }
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1402
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1403
    /**
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1404
     * Returns true if the Manifest main attribute "Multi-Release" is set to true; false otherwise.
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1405
     */
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1406
    private boolean isMultiReleaseJar() throws IOException {
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1407
        try (InputStream is = newInputStream(getBytes("/META-INF/MANIFEST.MF"))) {
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1408
            String multiRelease = new Manifest(is).getMainAttributes()
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1409
                .getValue(Attributes.Name.MULTI_RELEASE);
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1410
            return "true".equalsIgnoreCase(multiRelease);
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1411
        } catch (NoSuchFileException x) {
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1412
            return false;
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1413
        }
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1414
    }
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1415
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1416
    /**
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1417
     * Create a map of aliases for versioned entries, for example:
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1418
     *   version/PackagePrivate.class -> META-INF/versions/9/version/PackagePrivate.class
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1419
     *   version/PackagePrivate.java -> META-INF/versions/9/version/PackagePrivate.java
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1420
     *   version/Version.class -> META-INF/versions/10/version/Version.class
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1421
     *   version/Version.java -> META-INF/versions/10/version/Version.java
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1422
     *
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1423
     * Then wrap the map in a function that getEntry can use to override root
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1424
     * entry lookup for entries that have corresponding versioned entries.
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1425
     */
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1426
    private void createVersionedLinks(int version) {
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1427
        IndexNode verdir = getInode(getBytes("/META-INF/versions"));
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1428
        // nothing to do, if no /META-INF/versions
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1429
        if (verdir == null) {
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1430
            return;
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1431
        }
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1432
        // otherwise, create a map and for each META-INF/versions/{n} directory
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1433
        // put all the leaf inodes, i.e. entries, into the alias map
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1434
        // possibly shadowing lower versioned entries
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1435
        HashMap<IndexNode, byte[]> aliasMap = new HashMap<>();
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1436
        getVersionMap(version, verdir).values().forEach(versionNode ->
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1437
            walk(versionNode.child, entryNode ->
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1438
                aliasMap.put(
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1439
                    getOrCreateInode(getRootName(entryNode, versionNode), entryNode.isdir),
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1440
                    entryNode.name))
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1441
        );
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1442
        entryLookup = path -> {
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1443
            byte[] entry = aliasMap.get(IndexNode.keyOf(path));
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1444
            return entry == null ? path : entry;
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1445
        };
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1446
    }
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1447
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1448
    /**
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1449
     * Create a sorted version map of version -> inode, for inodes <= max version.
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1450
     *   9 -> META-INF/versions/9
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1451
     *  10 -> META-INF/versions/10
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1452
     */
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1453
    private TreeMap<Integer, IndexNode> getVersionMap(int version, IndexNode metaInfVersions) {
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1454
        TreeMap<Integer,IndexNode> map = new TreeMap<>();
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1455
        IndexNode child = metaInfVersions.child;
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1456
        while (child != null) {
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1457
            Integer key = getVersion(child, metaInfVersions);
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1458
            if (key != null && key <= version) {
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1459
                map.put(key, child);
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1460
            }
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1461
            child = child.sibling;
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1462
        }
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1463
        return map;
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1464
    }
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1465
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1466
    /**
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1467
     * Extract the integer version number -- META-INF/versions/9 returns 9.
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1468
     */
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1469
    private Integer getVersion(IndexNode inode, IndexNode metaInfVersions) {
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1470
        try {
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1471
            byte[] fullName = inode.name;
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1472
            return Integer.parseInt(getString(Arrays
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1473
                .copyOfRange(fullName, metaInfVersions.name.length + 1, fullName.length)));
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1474
        } catch (NumberFormatException x) {
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1475
            // ignore this even though it might indicate issues with the JAR structure
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1476
            return null;
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1477
        }
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1478
    }
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1479
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1480
    /**
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1481
     * Walk the IndexNode tree processing all leaf nodes.
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1482
     */
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1483
    private void walk(IndexNode inode, Consumer<IndexNode> consumer) {
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1484
        if (inode == null) return;
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1485
        if (inode.isDir()) {
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1486
            walk(inode.child, consumer);
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1487
        } else {
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1488
            consumer.accept(inode);
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1489
        }
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1490
        walk(inode.sibling, consumer);
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1491
    }
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1492
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1493
    /**
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1494
     * Extract the root name from a versioned entry name.
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1495
     * E.g. given inode 'META-INF/versions/9/foo/bar.class'
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1496
     * and prefix 'META-INF/versions/9/' returns 'foo/bar.class'.
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1497
     */
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1498
    private byte[] getRootName(IndexNode inode, IndexNode prefix) {
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1499
        byte[] fullName = inode.name;
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1500
        return Arrays.copyOfRange(fullName, prefix.name.length, fullName.length);
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1501
    }
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1502
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1503
    // Reads zip file central directory. Returns the file position of first
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 18150
diff changeset
  1504
    // CEN header, otherwise returns -1 if an error occurred. If zip->msg != NULL
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1505
    // then the error was a zip format error and zip->msg has the error text.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1506
    // Always pass in -1 for knownTotal; it's used for a recursive call.
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1507
    private byte[] initCEN() throws IOException {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1508
        end = findEND();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1509
        if (end.endpos == 0) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1510
            inodes = new LinkedHashMap<>(10);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1511
            locpos = 0;
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1512
            buildNodeTree();
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1513
            return null;         // only END header present
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1514
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1515
        if (end.cenlen > end.endpos)
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1516
            throw new ZipException("invalid END header (bad central directory size)");
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1517
        long cenpos = end.endpos - end.cenlen;     // position of CEN table
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1518
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1519
        // Get position of first local file (LOC) header, taking into
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1520
        // account that there may be a stub prefixed to the zip file.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1521
        locpos = cenpos - end.cenoff;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1522
        if (locpos < 0)
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1523
            throw new ZipException("invalid END header (bad central directory offset)");
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1524
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1525
        // read in the CEN and END
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1526
        byte[] cen = new byte[(int)(end.cenlen + ENDHDR)];
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1527
        if (readFullyAt(cen, 0, cen.length, cenpos) != end.cenlen + ENDHDR) {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1528
            throw new ZipException("read CEN tables failed");
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1529
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1530
        // Iterate through the entries in the central directory
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1531
        inodes = new LinkedHashMap<>(end.centot + 1);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1532
        int pos = 0;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1533
        int limit = cen.length - ENDHDR;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1534
        while (pos < limit) {
29714
afac043e9bf0 8075774: Small readability and performance improvements for zipfs
martin
parents: 29270
diff changeset
  1535
            if (!cenSigAt(cen, pos))
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1536
                throw new ZipException("invalid CEN header (bad signature)");
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1537
            int method = CENHOW(cen, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1538
            int nlen   = CENNAM(cen, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1539
            int elen   = CENEXT(cen, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1540
            int clen   = CENCOM(cen, pos);
28562
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
  1541
            if ((CENFLG(cen, pos) & 1) != 0) {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1542
                throw new ZipException("invalid CEN header (encrypted entry)");
28562
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
  1543
            }
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
  1544
            if (method != METHOD_STORED && method != METHOD_DEFLATED) {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1545
                throw new ZipException("invalid CEN header (unsupported compression method: " + method + ")");
28562
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
  1546
            }
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
  1547
            if (pos + CENHDR + nlen > limit) {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1548
                throw new ZipException("invalid CEN header (bad header size)");
28562
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
  1549
            }
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1550
            IndexNode inode = new IndexNode(cen, pos, nlen);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1551
            inodes.put(inode, inode);
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1552
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1553
            // skip ext and comment
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1554
            pos += (CENHDR + nlen + elen + clen);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1555
        }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1556
        if (pos + ENDHDR != cen.length) {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1557
            throw new ZipException("invalid CEN header (bad header size)");
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1558
        }
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1559
        buildNodeTree();
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1560
        return cen;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1561
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1562
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1563
    private void ensureOpen() {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1564
        if (!isOpen)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1565
            throw new ClosedFileSystemException();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1566
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1567
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1568
    // Creates a new empty temporary file in the same directory as the
17699
dfa52f56753b 8004789: (zipfs) zip provider doesn't work correctly with file systems providers rather than the default
sherman
parents: 16857
diff changeset
  1569
    // specified file.  A variant of Files.createTempFile.
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1570
    private Path createTempFileInSameDirectoryAs(Path path)
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1571
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1572
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1573
        Path parent = path.toAbsolutePath().getParent();
17699
dfa52f56753b 8004789: (zipfs) zip provider doesn't work correctly with file systems providers rather than the default
sherman
parents: 16857
diff changeset
  1574
        Path dir = (parent == null) ? path.getFileSystem().getPath(".") : parent;
dfa52f56753b 8004789: (zipfs) zip provider doesn't work correctly with file systems providers rather than the default
sherman
parents: 16857
diff changeset
  1575
        Path tmpPath = Files.createTempFile(dir, "zipfstmp", null);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1576
        tmppaths.add(tmpPath);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1577
        return tmpPath;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1578
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1579
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1580
    ////////////////////update & sync //////////////////////////////////////
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1581
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1582
    private boolean hasUpdate = false;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1583
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1584
    // shared key. consumer guarantees the "writeLock" before use it.
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  1585
    private final IndexNode LOOKUPKEY = new IndexNode(null, -1);
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1586
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1587
    private void updateDelete(IndexNode inode) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1588
        beginWrite();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1589
        try {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1590
            removeFromTree(inode);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1591
            inodes.remove(inode);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1592
            hasUpdate = true;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1593
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1594
             endWrite();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1595
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1596
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1597
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1598
    private void update(Entry e) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1599
        beginWrite();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1600
        try {
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1601
            IndexNode old = inodes.put(e, e);
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1602
            if (old != null) {
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1603
                removeFromTree(old);
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1604
            }
16857
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 14342
diff changeset
  1605
            if (e.type == Entry.NEW || e.type == Entry.FILECH || e.type == Entry.COPY) {
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1606
                IndexNode parent = inodes.get(LOOKUPKEY.as(getParent(e.name)));
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1607
                e.sibling = parent.child;
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1608
                parent.child = e;
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1609
            }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1610
            hasUpdate = true;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1611
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1612
            endWrite();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1613
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1614
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1615
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1616
    // copy over the whole LOC entry (header if necessary, data and ext) from
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1617
    // old zip to the new one.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1618
    private long copyLOCEntry(Entry e, boolean updateHeader,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1619
                              OutputStream os,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1620
                              long written, byte[] buf)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1621
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1622
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1623
        long locoff = e.locoff;  // where to read
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1624
        e.locoff = written;      // update the e.locoff with new value
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1625
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1626
        // calculate the size need to write out
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1627
        long size = 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1628
        //  if there is A ext
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1629
        if ((e.flag & FLAG_DATADESCR) != 0) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1630
            if (e.size >= ZIP64_MINVAL || e.csize >= ZIP64_MINVAL)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1631
                size = 24;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1632
            else
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1633
                size = 16;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1634
        }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1635
        // read loc, use the original loc.elen/nlen
51638
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  1636
        //
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  1637
        // an extra byte after loc is read, which should be the first byte of the
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  1638
        // 'name' field of the loc. if this byte is '/', which means the original
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  1639
        // entry has an absolute path in original zip/jar file, the e.writeLOC()
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  1640
        // is used to output the loc, in which the leading "/" will be removed
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  1641
        if (readFullyAt(buf, 0, LOCHDR + 1 , locoff) != LOCHDR + 1)
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1642
            throw new ZipException("loc: reading failed");
51638
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  1643
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  1644
        if (updateHeader || LOCNAM(buf) > 0 && buf[LOCHDR] == '/') {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1645
            locoff += LOCHDR + LOCNAM(buf) + LOCEXT(buf);  // skip header
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1646
            size += e.csize;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1647
            written = e.writeLOC(os) + size;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1648
        } else {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1649
            os.write(buf, 0, LOCHDR);    // write out the loc header
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1650
            locoff += LOCHDR;
7531
77870839c857 6989148: (fs) zip provider should be available "out of the box"
sherman
parents: 7189
diff changeset
  1651
            // use e.csize,  LOCSIZ(buf) is zero if FLAG_DATADESCR is on
77870839c857 6989148: (fs) zip provider should be available "out of the box"
sherman
parents: 7189
diff changeset
  1652
            // size += LOCNAM(buf) + LOCEXT(buf) + LOCSIZ(buf);
77870839c857 6989148: (fs) zip provider should be available "out of the box"
sherman
parents: 7189
diff changeset
  1653
            size += LOCNAM(buf) + LOCEXT(buf) + e.csize;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1654
            written = LOCHDR + size;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1655
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1656
        int n;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1657
        while (size > 0 &&
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1658
            (n = (int)readFullyAt(buf, 0, buf.length, locoff)) != -1)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1659
        {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1660
            if (size < n)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1661
                n = (int)size;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1662
            os.write(buf, 0, n);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1663
            size -= n;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1664
            locoff += n;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1665
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1666
        return written;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1667
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1668
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1669
    private long writeEntry(Entry e, OutputStream os)
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1670
        throws IOException {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1671
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1672
        if (e.bytes == null && e.file == null)    // dir, 0-length data
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1673
            return 0;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1674
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1675
        long written = 0;
57842
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents: 57665
diff changeset
  1676
        if (e.method != METHOD_STORED && e.csize > 0 && (e.crc != 0 || e.size == 0)) {
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1677
            // pre-compressed entry, write directly to output stream
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1678
            writeTo(e, os);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1679
        } else {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1680
            try (OutputStream os2 = (e.method == METHOD_STORED) ?
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1681
                    new EntryOutputStreamCRC32(e, os) : new EntryOutputStreamDef(e, os)) {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1682
                writeTo(e, os2);
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1683
            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1684
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1685
        written += e.csize;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1686
        if ((e.flag & FLAG_DATADESCR) != 0) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1687
            written += e.writeEXT(os);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1688
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1689
        return written;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1690
    }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1691
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1692
    private void writeTo(Entry e, OutputStream os) throws IOException {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1693
        if (e.bytes != null) {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1694
            os.write(e.bytes, 0, e.bytes.length);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1695
        } else if (e.file != null) {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1696
            if (e.type == Entry.NEW || e.type == Entry.FILECH) {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1697
                try (InputStream is = Files.newInputStream(e.file)) {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1698
                    is.transferTo(os);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1699
                }
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1700
            }
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1701
            Files.delete(e.file);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1702
            tmppaths.remove(e.file);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1703
        }
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1704
    }
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1705
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1706
    // sync the zip file system, if there is any update
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1707
    private void sync() throws IOException {
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1708
        // check ex-closer
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1709
        if (!exChClosers.isEmpty()) {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1710
            for (ExistingChannelCloser ecc : exChClosers) {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1711
                if (ecc.closeAndDeleteIfDone()) {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1712
                    exChClosers.remove(ecc);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1713
                }
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1714
            }
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1715
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1716
        if (!hasUpdate)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1717
            return;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1718
        Path tmpFile = createTempFileInSameDirectoryAs(zfpath);
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1719
        try (OutputStream os = new BufferedOutputStream(Files.newOutputStream(tmpFile, WRITE))) {
10365
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1720
            ArrayList<Entry> elist = new ArrayList<>(inodes.size());
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1721
            long written = 0;
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1722
            byte[] buf = null;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1723
            Entry e;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1724
10365
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1725
            // write loc
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1726
            for (IndexNode inode : inodes.values()) {
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1727
                if (inode instanceof Entry) {    // an updated inode
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1728
                    e = (Entry)inode;
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1729
                    try {
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1730
                        if (e.type == Entry.COPY) {
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1731
                            // entry copy: the only thing changed is the "name"
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1732
                            // and "nlen" in LOC header, so we update/rewrite the
10365
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1733
                            // LOC in new file and simply copy the rest (data and
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1734
                            // ext) without enflating/deflating from the old zip
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1735
                            // file LOC entry.
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1736
                            if (buf == null)
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1737
                                buf = new byte[8192];
10365
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1738
                            written += copyLOCEntry(e, true, os, written, buf);
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1739
                        } else {                          // NEW, FILECH or CEN
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1740
                            e.locoff = written;
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1741
                            written += e.writeLOC(os);    // write loc header
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1742
                            written += writeEntry(e, os);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1743
                        }
10365
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1744
                        elist.add(e);
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1745
                    } catch (IOException x) {
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1746
                        x.printStackTrace();    // skip any in-accurate entry
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1747
                    }
10365
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1748
                } else {                        // unchanged inode
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1749
                    if (inode.pos == -1) {
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1750
                        continue;               // pseudo directory node
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1751
                    }
51638
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  1752
                    if (inode.name.length == 1 && inode.name[0] == '/') {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1753
                        continue;               // no root '/' directory even if it
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1754
                                                // exists in original zip/jar file.
51638
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  1755
                    }
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  1756
                    e = supportPosix ? new PosixEntry(this, inode) : new Entry(this, inode);
10365
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1757
                    try {
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1758
                        if (buf == null)
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1759
                            buf = new byte[8192];
10365
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1760
                        written += copyLOCEntry(e, false, os, written, buf);
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1761
                        elist.add(e);
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1762
                    } catch (IOException x) {
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1763
                        x.printStackTrace();    // skip any wrong entry
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1764
                    }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1765
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1766
            }
10365
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1767
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1768
            // now write back the cen and end table
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1769
            end.cenoff = written;
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1770
            for (Entry entry : elist) {
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1771
                written += entry.writeCEN(os);
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1772
            }
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1773
            end.centot = elist.size();
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1774
            end.cenlen = written - end.cenoff;
47223
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1775
            end.write(os, written, forceEnd64);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1776
        }
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1777
        if (!streams.isEmpty()) {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1778
            //
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1779
            // There are outstanding input streams open on existing "ch",
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1780
            // so, don't close the "cha" and delete the "file for now, let
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1781
            // the "ex-channel-closer" to handle them
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1782
            Path path = createTempFileInSameDirectoryAs(zfpath);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1783
            ExistingChannelCloser ecc = new ExistingChannelCloser(path,
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1784
                                                                  ch,
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1785
                                                                  streams);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1786
            Files.move(zfpath, path, REPLACE_EXISTING);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1787
            exChClosers.add(ecc);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1788
            streams = Collections.synchronizedSet(new HashSet<>());
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1789
        } else {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1790
            ch.close();
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1791
            Files.delete(zfpath);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1792
        }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1793
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1794
        Files.move(tmpFile, zfpath, REPLACE_EXISTING);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1795
        hasUpdate = false;    // clear
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1796
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1797
59216
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1798
    private IndexNode getInode(byte[] path) {
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1799
        return inodes.get(IndexNode.keyOf(Objects.requireNonNull(entryLookup.apply(path), "path")));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1800
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1801
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1802
    /**
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1803
     * Return the IndexNode from the root tree. If it doesn't exist,
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1804
     * it gets created along with all parent directory IndexNodes.
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1805
     */
59216
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  1806
    private IndexNode getOrCreateInode(byte[] path, boolean isdir) {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1807
        IndexNode node = getInode(path);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1808
        // if node exists, return it
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1809
        if (node != null) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1810
            return node;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1811
        }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1812
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1813
        // otherwise create new pseudo node and parent directory hierarchy
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1814
        node = new IndexNode(path, isdir);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1815
        beginWrite();
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1816
        try {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1817
            makeParentDirs(node, Objects.requireNonNull(inodes.get(IndexNode.keyOf(ROOTPATH)), "no root node found"));
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1818
            return node;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1819
        } finally {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1820
            endWrite();
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1821
        }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1822
    }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1823
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1824
    private Entry getEntry(byte[] path) throws IOException {
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1825
        IndexNode inode = getInode(path);
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1826
        if (inode instanceof Entry)
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1827
            return (Entry)inode;
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1828
        if (inode == null || inode.pos == -1)
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1829
            return null;
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  1830
        return supportPosix ? new PosixEntry(this, inode): new Entry(this, inode);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1831
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1832
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1833
    public void deleteFile(byte[] path, boolean failIfNotExists)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1834
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1835
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1836
        checkWritable();
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1837
        IndexNode inode = getInode(path);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1838
        if (inode == null) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1839
            if (path != null && path.length == 0)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1840
                throw new ZipException("root directory </> can't not be delete");
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1841
            if (failIfNotExists)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1842
                throw new NoSuchFileException(getString(path));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1843
        } else {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1844
            if (inode.isDir() && inode.child != null)
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1845
                throw new DirectoryNotEmptyException(getString(path));
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1846
            updateDelete(inode);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1847
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1848
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1849
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1850
    // Returns an out stream for either
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1851
    // (1) writing the contents of a new entry, if the entry exists, or
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1852
    // (2) updating/replacing the contents of the specified existing entry.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1853
    private OutputStream getOutputStream(Entry e) throws IOException {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1854
        if (e.mtime == -1)
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1855
            e.mtime = System.currentTimeMillis();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1856
        if (e.method == -1)
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1857
            e.method = defaultCompressionMethod;
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1858
        // store size, compressed size, and crc-32 in datadescr
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1859
        e.flag = FLAG_DATADESCR;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1860
        if (zc.isUTF8())
50238
a9307f400f5a 8203328: Rename EFS in java.util.zip internals to something meaningful
martin
parents: 47223
diff changeset
  1861
            e.flag |= FLAG_USE_UTF8;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1862
        OutputStream os;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1863
        if (useTempFile) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1864
            e.file = getTempPathForEntry(null);
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1865
            os = Files.newOutputStream(e.file, WRITE);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1866
        } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1867
            os = new ByteArrayOutputStream((e.size > 0)? (int)e.size : 8192);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1868
        }
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1869
        if (e.method == METHOD_DEFLATED) {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1870
            return new DeflatingEntryOutputStream(e, os);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1871
        } else {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1872
            return new EntryOutputStream(e, os);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1873
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1874
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1875
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1876
    private class EntryOutputStream extends FilterOutputStream {
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1877
        private final Entry e;
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1878
        private long written;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1879
        private boolean isClosed;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1880
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1881
        EntryOutputStream(Entry e, OutputStream os) {
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1882
            super(os);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1883
            this.e =  Objects.requireNonNull(e, "Zip entry is null");
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1884
            // this.written = 0;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1885
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1886
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1887
        @Override
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1888
        public synchronized void write(int b) throws IOException {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1889
            out.write(b);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1890
            written += 1;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1891
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1892
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1893
        @Override
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1894
        public synchronized void write(byte[] b, int off, int len)
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1895
                throws IOException {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1896
            out.write(b, off, len);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1897
            written += len;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1898
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1899
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1900
        @Override
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1901
        public synchronized void close() throws IOException {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1902
            if (isClosed) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1903
                return;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1904
            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1905
            isClosed = true;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1906
            e.size = written;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1907
            if (out instanceof ByteArrayOutputStream)
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1908
                e.bytes = ((ByteArrayOutputStream)out).toByteArray();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1909
            super.close();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1910
            update(e);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1911
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1912
    }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1913
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1914
    // Output stream returned when writing "deflated" entries into memory,
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1915
    // to enable eager (possibly parallel) deflation and reduce memory required.
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1916
    private class DeflatingEntryOutputStream extends DeflaterOutputStream {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1917
        private final CRC32 crc;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1918
        private final Entry e;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1919
        private boolean isClosed;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1920
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1921
        DeflatingEntryOutputStream(Entry e, OutputStream os) {
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1922
            super(os, getDeflater());
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1923
            this.e = Objects.requireNonNull(e, "Zip entry is null");
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1924
            this.crc = new CRC32();
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1925
        }
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1926
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1927
        @Override
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1928
        public synchronized void write(byte[] b, int off, int len)
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1929
                throws IOException {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1930
            super.write(b, off, len);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1931
            crc.update(b, off, len);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1932
        }
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1933
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1934
        @Override
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1935
        public synchronized void close() throws IOException {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1936
            if (isClosed)
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1937
                return;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1938
            isClosed = true;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1939
            finish();
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1940
            e.size  = def.getBytesRead();
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1941
            e.csize = def.getBytesWritten();
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1942
            e.crc = crc.getValue();
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1943
            if (out instanceof ByteArrayOutputStream)
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1944
                e.bytes = ((ByteArrayOutputStream)out).toByteArray();
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1945
            super.close();
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1946
            update(e);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1947
            releaseDeflater(def);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1948
        }
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1949
    }
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1950
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1951
    // Wrapper output stream class to write out a "stored" entry.
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1952
    // (1) this class does not close the underlying out stream when
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1953
    //     being closed.
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1954
    // (2) no need to be "synchronized", only used by sync()
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1955
    private class EntryOutputStreamCRC32 extends FilterOutputStream {
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1956
        private final CRC32 crc;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1957
        private final Entry e;
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1958
        private long written;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1959
        private boolean isClosed;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1960
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1961
        EntryOutputStreamCRC32(Entry e, OutputStream os) {
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1962
            super(os);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1963
            this.e =  Objects.requireNonNull(e, "Zip entry is null");
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1964
            this.crc = new CRC32();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1965
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1966
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1967
        @Override
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1968
        public void write(int b) throws IOException {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1969
            out.write(b);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1970
            crc.update(b);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1971
            written += 1;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1972
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1973
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1974
        @Override
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1975
        public void write(byte[] b, int off, int len)
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1976
                throws IOException {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1977
            out.write(b, off, len);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1978
            crc.update(b, off, len);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1979
            written += len;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1980
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1981
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1982
        @Override
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1983
        public void close() {
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1984
            if (isClosed)
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1985
                return;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1986
            isClosed = true;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1987
            e.size = e.csize = written;
51795
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
  1988
            e.crc = crc.getValue();
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1989
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1990
    }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1991
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1992
    // Wrapper output stream class to write out a "deflated" entry.
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1993
    // (1) this class does not close the underlying out stream when
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1994
    //     being closed.
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1995
    // (2) no need to be "synchronized", only used by sync()
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1996
    private class EntryOutputStreamDef extends DeflaterOutputStream {
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1997
        private final CRC32 crc;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1998
        private final Entry e;
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1999
        private boolean isClosed;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  2000
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2001
        EntryOutputStreamDef(Entry e, OutputStream os) {
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  2002
            super(os, getDeflater());
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2003
            this.e = Objects.requireNonNull(e, "Zip entry is null");
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  2004
            this.crc = new CRC32();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  2005
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  2006
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  2007
        @Override
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2008
        public void write(byte[] b, int off, int len) throws IOException {
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  2009
            super.write(b, off, len);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  2010
            crc.update(b, off, len);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  2011
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  2012
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  2013
        @Override
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  2014
        public void close() throws IOException {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  2015
            if (isClosed)
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  2016
                return;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  2017
            isClosed = true;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  2018
            finish();
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2019
            e.size = def.getBytesRead();
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  2020
            e.csize = def.getBytesWritten();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  2021
            e.crc = crc.getValue();
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
  2022
            releaseDeflater(def);
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  2023
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  2024
    }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  2025
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2026
    private InputStream getInputStream(Entry e)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2027
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2028
    {
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2029
        InputStream eis;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2030
        if (e.type == Entry.NEW) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2031
            if (e.bytes != null)
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2032
                eis = new ByteArrayInputStream(e.bytes);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2033
            else if (e.file != null)
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2034
                eis = Files.newInputStream(e.file);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2035
            else
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2036
                throw new ZipException("update entry data is missing");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2037
        } else if (e.type == Entry.FILECH) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2038
            // FILECH result is un-compressed.
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  2039
            eis = Files.newInputStream(e.file);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2040
            // TBD: wrap to hook close()
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2041
            // streams.add(eis);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2042
            return eis;
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
  2043
        } else {  // untouched CEN or COPY
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2044
            eis = new EntryInputStream(e, ch);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2045
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2046
        if (e.method == METHOD_DEFLATED) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2047
            // MORE: Compute good size for inflater stream:
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2048
            long bufSize = e.size + 2; // Inflater likes a bit of slack
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2049
            if (bufSize > 65536)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2050
                bufSize = 8192;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2051
            final long size = e.size;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2052
            eis = new InflaterInputStream(eis, getInflater(), (int)bufSize) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2053
                private boolean isClosed = false;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2054
                public void close() throws IOException {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2055
                    if (!isClosed) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2056
                        releaseInflater(inf);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2057
                        this.in.close();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2058
                        isClosed = true;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2059
                        streams.remove(this);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2060
                    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2061
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2062
                // Override fill() method to provide an extra "dummy" byte
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2063
                // at the end of the input stream. This is required when
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2064
                // using the "nowrap" Inflater option. (it appears the new
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2065
                // zlib in 7 does not need it, but keep it for now)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2066
                protected void fill() throws IOException {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2067
                    if (eof) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2068
                        throw new EOFException(
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2069
                            "Unexpected end of ZLIB input stream");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2070
                    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2071
                    len = this.in.read(buf, 0, buf.length);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2072
                    if (len == -1) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2073
                        buf[0] = 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2074
                        len = 1;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2075
                        eof = true;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2076
                    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2077
                    inf.setInput(buf, 0, len);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2078
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2079
                private boolean eof;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2080
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2081
                public int available() {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2082
                    if (isClosed)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2083
                        return 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2084
                    long avail = size - inf.getBytesWritten();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2085
                    return avail > (long) Integer.MAX_VALUE ?
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2086
                        Integer.MAX_VALUE : (int) avail;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2087
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2088
            };
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2089
        } else if (e.method == METHOD_STORED) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2090
            // TBD: wrap/ it does not seem necessary
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2091
        } else {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2092
            throw new ZipException("invalid compression method");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2093
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2094
        streams.add(eis);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2095
        return eis;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2096
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2097
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2098
    // Inner class implementing the input stream used to read
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2099
    // a (possibly compressed) zip file entry.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2100
    private class EntryInputStream extends InputStream {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2101
        private final SeekableByteChannel zfch; // local ref to zipfs's "ch". zipfs.ch might
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2102
                                                // point to a new channel after sync()
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2103
        private long pos;                       // current position within entry data
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2104
        private long rem;                       // number of remaining bytes within entry
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2105
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2106
        EntryInputStream(Entry e, SeekableByteChannel zfch)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2107
            throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2108
        {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2109
            this.zfch = zfch;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2110
            rem = e.csize;
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2111
            pos = e.locoff;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2112
            if (pos == -1) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2113
                Entry e2 = getEntry(e.name);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2114
                if (e2 == null) {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2115
                    throw new ZipException("invalid loc for entry <" + getString(e.name) + ">");
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2116
                }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2117
                pos = e2.locoff;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2118
            }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2119
            pos = -pos;  // lazy initialize the real data offset
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2120
        }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2121
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2122
        public int read(byte[] b, int off, int len) throws IOException {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2123
            ensureOpen();
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2124
            initDataPos();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2125
            if (rem == 0) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2126
                return -1;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2127
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2128
            if (len <= 0) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2129
                return 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2130
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2131
            if (len > rem) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2132
                len = (int) rem;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2133
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2134
            // readFullyAt()
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2135
            long n;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2136
            ByteBuffer bb = ByteBuffer.wrap(b);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2137
            bb.position(off);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2138
            bb.limit(off + len);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2139
            synchronized(zfch) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2140
                n = zfch.position(pos).read(bb);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2141
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2142
            if (n > 0) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2143
                pos += n;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2144
                rem -= n;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2145
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2146
            if (rem == 0) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2147
                close();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2148
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2149
            return (int)n;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2150
        }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2151
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2152
        public int read() throws IOException {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2153
            byte[] b = new byte[1];
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2154
            if (read(b, 0, 1) == 1) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2155
                return b[0] & 0xff;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2156
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2157
                return -1;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2158
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2159
        }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2160
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2161
        public long skip(long n) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2162
            ensureOpen();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2163
            if (n > rem)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2164
                n = rem;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2165
            pos += n;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2166
            rem -= n;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2167
            if (rem == 0) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2168
                close();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2169
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2170
            return n;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2171
        }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2172
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2173
        public int available() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2174
            return rem > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) rem;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2175
        }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2176
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2177
        public void close() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2178
            rem = 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2179
            streams.remove(this);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2180
        }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2181
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2182
        private void initDataPos() throws IOException {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2183
            if (pos <= 0) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2184
                pos = -pos + locpos;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2185
                byte[] buf = new byte[LOCHDR];
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2186
                if (readFullyAt(buf, 0, buf.length, pos) != LOCHDR) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2187
                    throw new ZipException("invalid loc " + pos + " for entry reading");
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2188
                }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2189
                pos += LOCHDR + LOCNAM(buf) + LOCEXT(buf);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2190
            }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2191
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2192
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2193
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2194
    // Maxmum number of de/inflater we cache
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2195
    private final int MAX_FLATER = 20;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2196
    // List of available Inflater objects for decompression
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2197
    private final List<Inflater> inflaters = new ArrayList<>();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2198
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2199
    // Gets an inflater from the list of available inflaters or allocates
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2200
    // a new one.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2201
    private Inflater getInflater() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2202
        synchronized (inflaters) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2203
            int size = inflaters.size();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2204
            if (size > 0) {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2205
                return inflaters.remove(size - 1);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2206
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2207
                return new Inflater(true);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2208
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2209
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2210
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2211
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2212
    // Releases the specified inflater to the list of available inflaters.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2213
    private void releaseInflater(Inflater inf) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2214
        synchronized (inflaters) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2215
            if (inflaters.size() < MAX_FLATER) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2216
                inf.reset();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2217
                inflaters.add(inf);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2218
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2219
                inf.end();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2220
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2221
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2222
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2223
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2224
    // List of available Deflater objects for compression
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2225
    private final List<Deflater> deflaters = new ArrayList<>();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2226
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
  2227
    // Gets a deflater from the list of available deflaters or allocates
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2228
    // a new one.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2229
    private Deflater getDeflater() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2230
        synchronized (deflaters) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2231
            int size = deflaters.size();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2232
            if (size > 0) {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2233
                return deflaters.remove(size - 1);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2234
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2235
                return new Deflater(Deflater.DEFAULT_COMPRESSION, true);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2236
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2237
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2238
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2239
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2240
    // Releases the specified inflater to the list of available inflaters.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2241
    private void releaseDeflater(Deflater def) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2242
        synchronized (deflaters) {
59112
fe87a92570db 8234011: (zipfs) Memory leak in ZipFileSystem.releaseDeflater()
simonis
parents: 58845
diff changeset
  2243
            if (deflaters.size() < MAX_FLATER) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2244
               def.reset();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2245
               deflaters.add(def);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2246
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2247
               def.end();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2248
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2249
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2250
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2251
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2252
    // End of central directory record
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2253
    static class END {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2254
        // The fields that are commented out below are not used by anyone and write() uses "0"
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2255
        // int  disknum;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2256
        // int  sdisknum;
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2257
        // int  endsub;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2258
        int  centot;        // 4 bytes
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2259
        long cenlen;        // 4 bytes
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2260
        long cenoff;        // 4 bytes
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2261
        // int  comlen;     // comment length
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2262
        // byte[] comment;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2263
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2264
        // members of Zip64 end of central directory locator
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2265
        // int diskNum;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2266
        long endpos;
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2267
        // int disktot;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2268
47223
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  2269
        void write(OutputStream os, long offset, boolean forceEnd64) throws IOException {
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  2270
            boolean hasZip64 = forceEnd64; // false;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2271
            long xlen = cenlen;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2272
            long xoff = cenoff;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2273
            if (xlen >= ZIP64_MINVAL) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2274
                xlen = ZIP64_MINVAL;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2275
                hasZip64 = true;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2276
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2277
            if (xoff >= ZIP64_MINVAL) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2278
                xoff = ZIP64_MINVAL;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2279
                hasZip64 = true;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2280
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2281
            int count = centot;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2282
            if (count >= ZIP64_MINVAL32) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2283
                count = ZIP64_MINVAL32;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2284
                hasZip64 = true;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2285
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2286
            if (hasZip64) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2287
                //zip64 end of central directory record
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2288
                writeInt(os, ZIP64_ENDSIG);       // zip64 END record signature
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2289
                writeLong(os, ZIP64_ENDHDR - 12); // size of zip64 end
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2290
                writeShort(os, 45);               // version made by
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2291
                writeShort(os, 45);               // version needed to extract
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2292
                writeInt(os, 0);                  // number of this disk
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2293
                writeInt(os, 0);                  // central directory start disk
47223
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  2294
                writeLong(os, centot);            // number of directory entries on disk
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  2295
                writeLong(os, centot);            // number of directory entries
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2296
                writeLong(os, cenlen);            // length of central directory
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2297
                writeLong(os, cenoff);            // offset of central directory
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2298
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2299
                //zip64 end of central directory locator
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2300
                writeInt(os, ZIP64_LOCSIG);       // zip64 END locator signature
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2301
                writeInt(os, 0);                  // zip64 END start disk
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2302
                writeLong(os, offset);            // offset of zip64 END
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2303
                writeInt(os, 1);                  // total number of disks (?)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2304
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2305
            writeInt(os, ENDSIG);                 // END record signature
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2306
            writeShort(os, 0);                    // number of this disk
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2307
            writeShort(os, 0);                    // central directory start disk
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2308
            writeShort(os, count);                // number of directory entries on disk
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2309
            writeShort(os, count);                // total number of directory entries
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2310
            writeInt(os, xlen);                   // length of central directory
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2311
            writeInt(os, xoff);                   // offset of central directory
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2312
            writeShort(os, 0);                    // zip file comment, not used
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2313
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2314
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2315
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2316
    // Internal node that links a "name" to its pos in cen table.
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2317
    // The node itself can be used as a "key" to lookup itself in
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2318
    // the HashMap inodes.
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2319
    static class IndexNode {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2320
        byte[]  name;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2321
        int     hashcode;    // node is hashable/hashed by its name
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2322
        boolean isdir;
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2323
        int     pos = -1;    // position in cen table, -1 means the
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2324
                             // entry does not exist in zip file
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2325
        IndexNode child;     // first child
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2326
        IndexNode sibling;   // next sibling
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2327
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2328
        IndexNode() {}
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2329
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2330
        IndexNode(byte[] name, boolean isdir) {
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2331
            name(name);
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2332
            this.isdir = isdir;
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2333
            this.pos = -1;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2334
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2335
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2336
        IndexNode(byte[] name, int pos) {
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2337
            name(name);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2338
            this.pos = pos;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2339
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2340
54944
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2341
        // constructor for initCEN() (1) remove trailing '/' (2) pad leading '/'
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  2342
        IndexNode(byte[] cen, int pos, int nlen) {
51638
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  2343
            int noff = pos + CENHDR;
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2344
            if (cen[noff + nlen - 1] == '/') {
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2345
                isdir = true;
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2346
                nlen--;
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2347
            }
51638
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  2348
            if (nlen > 0 && cen[noff] == '/') {
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  2349
                name = Arrays.copyOfRange(cen, noff, noff + nlen);
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  2350
            } else {
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  2351
                name = new byte[nlen + 1];
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  2352
                System.arraycopy(cen, noff, name, 1, nlen);
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  2353
                name[0] = '/';
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  2354
            }
54944
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2355
            name(normalize(name));
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2356
            this.pos = pos;
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2357
        }
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2358
54944
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2359
        // Normalize the IndexNode.name field.
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2360
        private byte[] normalize(byte[] path) {
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2361
            int len = path.length;
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2362
            if (len == 0)
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2363
                return path;
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2364
            byte prevC = 0;
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2365
            for (int pathPos = 0; pathPos < len; pathPos++) {
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2366
                byte c = path[pathPos];
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2367
                if (c == '/' && prevC == '/')
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2368
                    return normalize(path, pathPos - 1);
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2369
                prevC = c;
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2370
            }
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2371
            if (len > 1 && prevC == '/') {
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2372
                return Arrays.copyOf(path, len - 1);
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2373
            }
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2374
            return path;
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2375
        }
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2376
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2377
        private byte[] normalize(byte[] path, int off) {
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2378
            // As we know we have at least one / to trim, we can reduce
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2379
            // the size of the resulting array
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2380
            byte[] to = new byte[path.length - 1];
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2381
            int pathPos = 0;
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2382
            while (pathPos < off) {
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2383
                to[pathPos] = path[pathPos];
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2384
                pathPos++;
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2385
            }
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2386
            int toPos = pathPos;
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2387
            byte prevC = 0;
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2388
            while (pathPos < path.length) {
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2389
                byte c = path[pathPos++];
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2390
                if (c == '/' && prevC == '/')
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2391
                    continue;
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2392
                to[toPos++] = c;
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2393
                prevC = c;
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2394
            }
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2395
            if (toPos > 1 && to[toPos - 1] == '/')
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2396
                toPos--;
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2397
            return (toPos == to.length) ? to : Arrays.copyOf(to, toPos);
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2398
        }
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2399
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2400
        private static final ThreadLocal<IndexNode> cachedKey = new ThreadLocal<>();
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2401
59216
47c879f478d2 8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents: 59112
diff changeset
  2402
        static final IndexNode keyOf(byte[] name) { // get a lookup key;
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2403
            IndexNode key = cachedKey.get();
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2404
            if (key == null) {
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2405
                key = new IndexNode(name, -1);
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2406
                cachedKey.set(key);
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2407
            }
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2408
            return key.as(name);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2409
        }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2410
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2411
        final void name(byte[] name) {
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2412
            this.name = name;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2413
            this.hashcode = Arrays.hashCode(name);
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2414
        }
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2415
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2416
        final IndexNode as(byte[] name) {           // reuse the node, mostly
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2417
            name(name);                             // as a lookup "key"
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2418
            return this;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2419
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2420
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2421
        boolean isDir() {
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2422
            return isdir;
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2423
        }
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2424
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2425
        @Override
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2426
        public boolean equals(Object other) {
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2427
            if (!(other instanceof IndexNode)) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2428
                return false;
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2429
            }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2430
            if (other instanceof ParentLookup) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2431
                return ((ParentLookup)other).equals(this);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2432
            }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2433
            return Arrays.equals(name, ((IndexNode)other).name);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2434
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2435
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2436
        @Override
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2437
        public int hashCode() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2438
            return hashcode;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2439
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2440
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2441
        @Override
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2442
        public String toString() {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2443
            return new String(name) + (isdir ? " (dir)" : " ") + ", index: " + pos;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2444
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2445
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2446
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2447
    static class Entry extends IndexNode implements ZipFileAttributes {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2448
        static final int CEN    = 1;  // entry read from cen
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2449
        static final int NEW    = 2;  // updated contents in bytes or file
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2450
        static final int FILECH = 3;  // fch update in "file"
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2451
        static final int COPY   = 4;  // copy of a CEN entry
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2452
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2453
        byte[] bytes;                 // updated content bytes
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2454
        Path   file;                  // use tmp file to store bytes;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2455
        int    type = CEN;            // default is the entry read from cen
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2456
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2457
        // entry attributes
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2458
        int    version;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2459
        int    flag;
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2460
        int    posixPerms = -1; // posix permissions
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2461
        int    method = -1;    // compression method
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2462
        long   mtime  = -1;    // last modification time (in DOS time)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2463
        long   atime  = -1;    // last access time
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2464
        long   ctime  = -1;    // create time
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2465
        long   crc    = -1;    // crc-32 of entry data
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2466
        long   csize  = -1;    // compressed size of entry data
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2467
        long   size   = -1;    // uncompressed size of entry data
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2468
        byte[] extra;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2469
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2470
        // CEN
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2471
        // The fields that are commented out below are not used by anyone and write() uses "0"
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2472
        // int    versionMade;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2473
        // int    disk;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2474
        // int    attrs;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2475
        // long   attrsEx;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2476
        long   locoff;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2477
        byte[] comment;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2478
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  2479
        Entry(byte[] name, boolean isdir, int method) {
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2480
            name(name);
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2481
            this.isdir = isdir;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2482
            this.mtime  = this.ctime = this.atime = System.currentTimeMillis();
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2483
            this.crc    = 0;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2484
            this.size   = 0;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2485
            this.csize  = 0;
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  2486
            this.method = method;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2487
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2488
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2489
        @SuppressWarnings("unchecked")
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2490
        Entry(byte[] name, int type, boolean isdir, int method, FileAttribute<?>... attrs) {
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  2491
            this(name, isdir, method);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2492
            this.type = type;
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2493
            for (FileAttribute<?> attr : attrs) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2494
                String attrName = attr.name();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2495
                if (attrName.equals("posix:permissions")) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2496
                    posixPerms = ZipUtils.permsToFlags((Set<PosixFilePermission>)attr.value());
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2497
                }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2498
            }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2499
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2500
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2501
        Entry(byte[] name, Path file, int type, FileAttribute<?>... attrs) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2502
            this(name, type, false, METHOD_STORED, attrs);
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2503
            this.file = file;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2504
        }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2505
58845
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 58822
diff changeset
  2506
        Entry(Entry e, int type, int compressionMethod) {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 58822
diff changeset
  2507
            this(e, type);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 58822
diff changeset
  2508
            this.method = compressionMethod;
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 58822
diff changeset
  2509
        }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 58822
diff changeset
  2510
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2511
        Entry(Entry e, int type) {
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2512
            name(e.name);
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2513
            this.isdir     = e.isdir;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2514
            this.version   = e.version;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2515
            this.ctime     = e.ctime;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2516
            this.atime     = e.atime;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2517
            this.mtime     = e.mtime;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2518
            this.crc       = e.crc;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2519
            this.size      = e.size;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2520
            this.csize     = e.csize;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2521
            this.method    = e.method;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2522
            this.extra     = e.extra;
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2523
            /*
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2524
            this.versionMade = e.versionMade;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2525
            this.disk      = e.disk;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2526
            this.attrs     = e.attrs;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2527
            this.attrsEx   = e.attrsEx;
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2528
            */
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2529
            this.locoff    = e.locoff;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2530
            this.comment   = e.comment;
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2531
            this.posixPerms = e.posixPerms;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2532
            this.type      = type;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2533
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2534
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2535
        Entry(ZipFileSystem zipfs, IndexNode inode) throws IOException {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2536
            readCEN(zipfs, inode);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2537
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2538
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2539
        // Calculates a suitable base for the version number to
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2540
        // be used for fields version made by/version needed to extract.
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2541
        // The lower bytes of these 2 byte fields hold the version number
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2542
        // (value/10 = major; value%10 = minor)
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2543
        // For different features certain minimum versions apply:
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2544
        // stored = 10 (1.0), deflated = 20 (2.0), zip64 = 45 (4.5)
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2545
        private int version(boolean zip64) throws ZipException {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2546
            if (zip64) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2547
                return 45;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2548
            }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2549
            if (method == METHOD_DEFLATED)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2550
                return 20;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2551
            else if (method == METHOD_STORED)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2552
                return 10;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2553
            throw new ZipException("unsupported compression method");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2554
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2555
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2556
        /**
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2557
         * Adds information about compatibility of file attribute information
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2558
         * to a version value.
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2559
         */
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2560
        private int versionMadeBy(int version) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2561
            return (posixPerms < 0) ? version :
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2562
                VERSION_MADE_BY_BASE_UNIX | (version & 0xff);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2563
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2564
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2565
        ///////////////////// CEN //////////////////////
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2566
        private void readCEN(ZipFileSystem zipfs, IndexNode inode) throws IOException {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2567
            byte[] cen = zipfs.cen;
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2568
            int pos = inode.pos;
29714
afac043e9bf0 8075774: Small readability and performance improvements for zipfs
martin
parents: 29270
diff changeset
  2569
            if (!cenSigAt(cen, pos))
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2570
                throw new ZipException("invalid CEN header (bad signature)");
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2571
            version     = CENVER(cen, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2572
            flag        = CENFLG(cen, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2573
            method      = CENHOW(cen, pos);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2574
            mtime       = dosToJavaTime(CENTIM(cen, pos));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2575
            crc         = CENCRC(cen, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2576
            csize       = CENSIZ(cen, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2577
            size        = CENLEN(cen, pos);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2578
            int nlen    = CENNAM(cen, pos);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2579
            int elen    = CENEXT(cen, pos);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2580
            int clen    = CENCOM(cen, pos);
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2581
            /*
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2582
            versionMade = CENVEM(cen, pos);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2583
            disk        = CENDSK(cen, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2584
            attrs       = CENATT(cen, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2585
            attrsEx     = CENATX(cen, pos);
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2586
            */
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2587
            if (CENVEM_FA(cen, pos) == FILE_ATTRIBUTES_UNIX) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2588
                posixPerms = CENATX_PERMS(cen, pos) & 0xFFF; // 12 bits for setuid, setgid, sticky + perms
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2589
            }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2590
            locoff      = CENOFF(cen, pos);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2591
            pos += CENHDR;
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2592
            this.name = inode.name;
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2593
            this.isdir = inode.isdir;
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2594
            this.hashcode = inode.hashcode;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2595
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2596
            pos += nlen;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2597
            if (elen > 0) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2598
                extra = Arrays.copyOfRange(cen, pos, pos + elen);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2599
                pos += elen;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2600
                readExtra(zipfs);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2601
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2602
            if (clen > 0) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2603
                comment = Arrays.copyOfRange(cen, pos, pos + clen);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2604
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2605
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2606
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2607
        private int writeCEN(OutputStream os) throws IOException {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2608
            long csize0  = csize;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2609
            long size0   = size;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2610
            long locoff0 = locoff;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2611
            int elen64   = 0;                // extra for ZIP64
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2612
            int elenNTFS = 0;                // extra for NTFS (a/c/mtime)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2613
            int elenEXTT = 0;                // extra for Extended Timestamp
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2614
            boolean foundExtraTime = false;  // if time stamp NTFS, EXTT present
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2615
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2616
            byte[] zname = isdir ? toDirectoryPath(name) : name;
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2617
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2618
            // confirm size/length
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2619
            int nlen = (zname != null) ? zname.length - 1 : 0;  // name has [0] as "slash"
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2620
            int elen = (extra != null) ? extra.length : 0;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2621
            int eoff = 0;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2622
            int clen = (comment != null) ? comment.length : 0;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2623
            if (csize >= ZIP64_MINVAL) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2624
                csize0 = ZIP64_MINVAL;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2625
                elen64 += 8;                 // csize(8)
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2626
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2627
            if (size >= ZIP64_MINVAL) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2628
                size0 = ZIP64_MINVAL;        // size(8)
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2629
                elen64 += 8;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2630
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2631
            if (locoff >= ZIP64_MINVAL) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2632
                locoff0 = ZIP64_MINVAL;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2633
                elen64 += 8;                 // offset(8)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2634
            }
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2635
            if (elen64 != 0) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2636
                elen64 += 4;                 // header and data sz 4 bytes
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2637
            }
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2638
            boolean zip64 = (elen64 != 0);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2639
            int version0 = version(zip64);
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2640
            while (eoff + 4 < elen) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2641
                int tag = SH(extra, eoff);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2642
                int sz = SH(extra, eoff + 2);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2643
                if (tag == EXTID_EXTT || tag == EXTID_NTFS) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2644
                    foundExtraTime = true;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2645
                }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2646
                eoff += (4 + sz);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2647
            }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2648
            if (!foundExtraTime) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2649
                if (isWindows) {             // use NTFS
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2650
                    elenNTFS = 36;           // total 36 bytes
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2651
                } else {                     // Extended Timestamp otherwise
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2652
                    elenEXTT = 9;            // only mtime in cen
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2653
                }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2654
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2655
            writeInt(os, CENSIG);            // CEN header signature
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2656
            writeShort(os, versionMadeBy(version0)); // version made by
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2657
            writeShort(os, version0);        // version needed to extract
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2658
            writeShort(os, flag);            // general purpose bit flag
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2659
            writeShort(os, method);          // compression method
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2660
                                             // last modification time
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2661
            writeInt(os, (int)javaToDosTime(mtime));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2662
            writeInt(os, crc);               // crc-32
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2663
            writeInt(os, csize0);            // compressed size
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2664
            writeInt(os, size0);             // uncompressed size
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2665
            writeShort(os, nlen);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2666
            writeShort(os, elen + elen64 + elenNTFS + elenEXTT);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2667
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2668
            if (comment != null) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2669
                writeShort(os, Math.min(clen, 0xffff));
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2670
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2671
                writeShort(os, 0);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2672
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2673
            writeShort(os, 0);              // starting disk number
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2674
            writeShort(os, 0);              // internal file attributes (unused)
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2675
            writeInt(os, posixPerms > 0 ? posixPerms << 16 : 0); // external file
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2676
                                            // attributes, used for storing posix
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2677
                                            // permissions
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2678
            writeInt(os, locoff0);          // relative offset of local header
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2679
            writeBytes(os, zname, 1, nlen);
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2680
            if (zip64) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2681
                writeShort(os, EXTID_ZIP64);// Zip64 extra
10365
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  2682
                writeShort(os, elen64 - 4); // size of "this" extra block
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2683
                if (size0 == ZIP64_MINVAL)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2684
                    writeLong(os, size);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2685
                if (csize0 == ZIP64_MINVAL)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2686
                    writeLong(os, csize);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2687
                if (locoff0 == ZIP64_MINVAL)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2688
                    writeLong(os, locoff);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2689
            }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2690
            if (elenNTFS != 0) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2691
                writeShort(os, EXTID_NTFS);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2692
                writeShort(os, elenNTFS - 4);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2693
                writeInt(os, 0);            // reserved
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2694
                writeShort(os, 0x0001);     // NTFS attr tag
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2695
                writeShort(os, 24);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2696
                writeLong(os, javaToWinTime(mtime));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2697
                writeLong(os, javaToWinTime(atime));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2698
                writeLong(os, javaToWinTime(ctime));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2699
            }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2700
            if (elenEXTT != 0) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2701
                writeShort(os, EXTID_EXTT);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2702
                writeShort(os, elenEXTT - 4);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2703
                if (ctime == -1)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2704
                    os.write(0x3);          // mtime and atime
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2705
                else
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2706
                    os.write(0x7);          // mtime, atime and ctime
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2707
                writeInt(os, javaToUnixTime(mtime));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2708
            }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2709
            if (extra != null)              // whatever not recognized
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2710
                writeBytes(os, extra);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2711
            if (comment != null)            //TBD: 0, Math.min(commentBytes.length, 0xffff));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2712
                writeBytes(os, comment);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2713
            return CENHDR + nlen + elen + clen + elen64 + elenNTFS + elenEXTT;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2714
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2715
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2716
        ///////////////////// LOC //////////////////////
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2717
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2718
        private int writeLOC(OutputStream os) throws IOException {
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2719
            byte[] zname = isdir ? toDirectoryPath(name) : name;
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2720
            int nlen = (zname != null) ? zname.length - 1 : 0; // [0] is slash
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2721
            int elen = (extra != null) ? extra.length : 0;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2722
            boolean foundExtraTime = false;     // if extra timestamp present
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2723
            int eoff = 0;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2724
            int elen64 = 0;
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2725
            boolean zip64 = false;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2726
            int elenEXTT = 0;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2727
            int elenNTFS = 0;
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
  2728
            writeInt(os, LOCSIG);               // LOC header signature
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2729
            if ((flag & FLAG_DATADESCR) != 0) {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2730
                writeShort(os, version(false)); // version needed to extract
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2731
                writeShort(os, flag);           // general purpose bit flag
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2732
                writeShort(os, method);         // compression method
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2733
                // last modification time
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2734
                writeInt(os, (int)javaToDosTime(mtime));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2735
                // store size, uncompressed size, and crc-32 in data descriptor
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2736
                // immediately following compressed entry data
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2737
                writeInt(os, 0);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2738
                writeInt(os, 0);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2739
                writeInt(os, 0);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2740
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2741
                if (csize >= ZIP64_MINVAL || size >= ZIP64_MINVAL) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2742
                    elen64 = 20;    //headid(2) + size(2) + size(8) + csize(8)
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2743
                    zip64 = true;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2744
                }
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2745
                writeShort(os, version(zip64)); // version needed to extract
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2746
                writeShort(os, flag);           // general purpose bit flag
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2747
                writeShort(os, method);         // compression method
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2748
                                                // last modification time
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2749
                writeInt(os, (int)javaToDosTime(mtime));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2750
                writeInt(os, crc);              // crc-32
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2751
                if (zip64) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2752
                    writeInt(os, ZIP64_MINVAL);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2753
                    writeInt(os, ZIP64_MINVAL);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2754
                } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2755
                    writeInt(os, csize);        // compressed size
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2756
                    writeInt(os, size);         // uncompressed size
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2757
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2758
            }
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2759
            while (eoff + 4 < elen) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2760
                int tag = SH(extra, eoff);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2761
                int sz = SH(extra, eoff + 2);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2762
                if (tag == EXTID_EXTT || tag == EXTID_NTFS) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2763
                    foundExtraTime = true;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2764
                }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2765
                eoff += (4 + sz);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2766
            }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2767
            if (!foundExtraTime) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2768
                if (isWindows) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2769
                    elenNTFS = 36;              // NTFS, total 36 bytes
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2770
                } else {                        // on unix use "ext time"
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2771
                    elenEXTT = 9;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2772
                    if (atime != -1)
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2773
                        elenEXTT += 4;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2774
                    if (ctime != -1)
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2775
                        elenEXTT += 4;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2776
                }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2777
            }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2778
            writeShort(os, nlen);
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2779
            writeShort(os, elen + elen64 + elenNTFS + elenEXTT);
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2780
            writeBytes(os, zname, 1, nlen);
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2781
            if (zip64) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2782
                writeShort(os, EXTID_ZIP64);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2783
                writeShort(os, 16);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2784
                writeLong(os, size);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2785
                writeLong(os, csize);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2786
            }
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2787
            if (elenNTFS != 0) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2788
                writeShort(os, EXTID_NTFS);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2789
                writeShort(os, elenNTFS - 4);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2790
                writeInt(os, 0);            // reserved
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2791
                writeShort(os, 0x0001);     // NTFS attr tag
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2792
                writeShort(os, 24);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2793
                writeLong(os, javaToWinTime(mtime));
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2794
                writeLong(os, javaToWinTime(atime));
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2795
                writeLong(os, javaToWinTime(ctime));
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2796
            }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2797
            if (elenEXTT != 0) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2798
                writeShort(os, EXTID_EXTT);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2799
                writeShort(os, elenEXTT - 4);// size for the folowing data block
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2800
                int fbyte = 0x1;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2801
                if (atime != -1)           // mtime and atime
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2802
                    fbyte |= 0x2;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2803
                if (ctime != -1)           // mtime, atime and ctime
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2804
                    fbyte |= 0x4;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2805
                os.write(fbyte);           // flags byte
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2806
                writeInt(os, javaToUnixTime(mtime));
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2807
                if (atime != -1)
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2808
                    writeInt(os, javaToUnixTime(atime));
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2809
                if (ctime != -1)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2810
                    writeInt(os, javaToUnixTime(ctime));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2811
            }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2812
            if (extra != null) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2813
                writeBytes(os, extra);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2814
            }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2815
            return LOCHDR + nlen + elen + elen64 + elenNTFS + elenEXTT;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2816
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2817
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2818
        // Data Descriptor
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2819
        private int writeEXT(OutputStream os) throws IOException {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2820
            writeInt(os, EXTSIG);           // EXT header signature
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2821
            writeInt(os, crc);              // crc-32
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2822
            if (csize >= ZIP64_MINVAL || size >= ZIP64_MINVAL) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2823
                writeLong(os, csize);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2824
                writeLong(os, size);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2825
                return 24;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2826
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2827
                writeInt(os, csize);        // compressed size
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2828
                writeInt(os, size);         // uncompressed size
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2829
                return 16;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2830
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2831
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2832
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2833
        // read NTFS, UNIX and ZIP64 data from cen.extra
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2834
        private void readExtra(ZipFileSystem zipfs) throws IOException {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2835
            if (extra == null)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2836
                return;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2837
            int elen = extra.length;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2838
            int off = 0;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2839
            int newOff = 0;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2840
            while (off + 4 < elen) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2841
                // extra spec: HeaderID+DataSize+Data
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2842
                int pos = off;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2843
                int tag = SH(extra, pos);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2844
                int sz = SH(extra, pos + 2);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2845
                pos += 4;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2846
                if (pos + sz > elen)         // invalid data
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2847
                    break;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2848
                switch (tag) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2849
                case EXTID_ZIP64 :
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2850
                    if (size == ZIP64_MINVAL) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2851
                        if (pos + 8 > elen)  // invalid zip64 extra
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2852
                            break;           // fields, just skip
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2853
                        size = LL(extra, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2854
                        pos += 8;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2855
                    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2856
                    if (csize == ZIP64_MINVAL) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2857
                        if (pos + 8 > elen)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2858
                            break;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2859
                        csize = LL(extra, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2860
                        pos += 8;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2861
                    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2862
                    if (locoff == ZIP64_MINVAL) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2863
                        if (pos + 8 > elen)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2864
                            break;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2865
                        locoff = LL(extra, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2866
                    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2867
                    break;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2868
                case EXTID_NTFS:
29815
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29714
diff changeset
  2869
                    if (sz < 32)
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29714
diff changeset
  2870
                        break;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2871
                    pos += 4;    // reserved 4 bytes
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2872
                    if (SH(extra, pos) !=  0x0001)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2873
                        break;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2874
                    if (SH(extra, pos + 2) != 24)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2875
                        break;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2876
                    // override the loc field, datatime here is
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2877
                    // more "accurate"
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2878
                    mtime  = winToJavaTime(LL(extra, pos + 4));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2879
                    atime  = winToJavaTime(LL(extra, pos + 12));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2880
                    ctime  = winToJavaTime(LL(extra, pos + 20));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2881
                    break;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2882
                case EXTID_EXTT:
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2883
                    // spec says the Extened timestamp in cen only has mtime
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2884
                    // need to read the loc to get the extra a/ctime, if flag
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2885
                    // "zipinfo-time" is not specified to false;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2886
                    // there is performance cost (move up to loc and read) to
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2887
                    // access the loc table foreach entry;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2888
                    if (zipfs.noExtt) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2889
                        if (sz == 5)
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2890
                            mtime = unixToJavaTime(LG(extra, pos + 1));
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2891
                         break;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2892
                    }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2893
                    byte[] buf = new byte[LOCHDR];
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2894
                    if (zipfs.readFullyAt(buf, 0, buf.length , locoff)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2895
                        != buf.length)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2896
                        throw new ZipException("loc: reading failed");
29714
afac043e9bf0 8075774: Small readability and performance improvements for zipfs
martin
parents: 29270
diff changeset
  2897
                    if (!locSigAt(buf, 0))
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2898
                        throw new ZipException("loc: wrong sig ->"
29714
afac043e9bf0 8075774: Small readability and performance improvements for zipfs
martin
parents: 29270
diff changeset
  2899
                                           + Long.toString(getSig(buf, 0), 16));
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2900
                    int locElen = LOCEXT(buf);
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2901
                    if (locElen < 9)    // EXTT is at least 9 bytes
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2902
                        break;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2903
                    int locNlen = LOCNAM(buf);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2904
                    buf = new byte[locElen];
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2905
                    if (zipfs.readFullyAt(buf, 0, buf.length , locoff + LOCHDR + locNlen)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2906
                        != buf.length)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2907
                        throw new ZipException("loc extra: reading failed");
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2908
                    int locPos = 0;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2909
                    while (locPos + 4 < buf.length) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2910
                        int locTag = SH(buf, locPos);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2911
                        int locSZ  = SH(buf, locPos + 2);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2912
                        locPos += 4;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2913
                        if (locTag  != EXTID_EXTT) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2914
                            locPos += locSZ;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2915
                             continue;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2916
                        }
43224
355457152ea8 8173072: zipfs fails to handle incorrect info-zip "extended timestamp extra field"
sherman
parents: 43193
diff changeset
  2917
                        int end = locPos + locSZ - 4;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2918
                        int flag = CH(buf, locPos++);
43224
355457152ea8 8173072: zipfs fails to handle incorrect info-zip "extended timestamp extra field"
sherman
parents: 43193
diff changeset
  2919
                        if ((flag & 0x1) != 0 && locPos <= end) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2920
                            mtime = unixToJavaTime(LG(buf, locPos));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2921
                            locPos += 4;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2922
                        }
43224
355457152ea8 8173072: zipfs fails to handle incorrect info-zip "extended timestamp extra field"
sherman
parents: 43193
diff changeset
  2923
                        if ((flag & 0x2) != 0 && locPos <= end) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2924
                            atime = unixToJavaTime(LG(buf, locPos));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2925
                            locPos += 4;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2926
                        }
43224
355457152ea8 8173072: zipfs fails to handle incorrect info-zip "extended timestamp extra field"
sherman
parents: 43193
diff changeset
  2927
                        if ((flag & 0x4) != 0 && locPos <= end) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2928
                            ctime = unixToJavaTime(LG(buf, locPos));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2929
                        }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2930
                        break;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2931
                    }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2932
                    break;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2933
                default:    // unknown tag
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2934
                    System.arraycopy(extra, off, extra, newOff, sz + 4);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2935
                    newOff += (sz + 4);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2936
                }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2937
                off += (sz + 4);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2938
            }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2939
            if (newOff != 0 && newOff != extra.length)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2940
                extra = Arrays.copyOf(extra, newOff);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2941
            else
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2942
                extra = null;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2943
        }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2944
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2945
        @Override
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2946
        public String toString() {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2947
            StringBuilder sb = new StringBuilder(1024);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2948
            Formatter fm = new Formatter(sb);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2949
            fm.format("    name            : %s%n", new String(name));
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2950
            fm.format("    creationTime    : %tc%n", creationTime().toMillis());
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2951
            fm.format("    lastAccessTime  : %tc%n", lastAccessTime().toMillis());
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2952
            fm.format("    lastModifiedTime: %tc%n", lastModifiedTime().toMillis());
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2953
            fm.format("    isRegularFile   : %b%n", isRegularFile());
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2954
            fm.format("    isDirectory     : %b%n", isDirectory());
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2955
            fm.format("    isSymbolicLink  : %b%n", isSymbolicLink());
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2956
            fm.format("    isOther         : %b%n", isOther());
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2957
            fm.format("    fileKey         : %s%n", fileKey());
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2958
            fm.format("    size            : %d%n", size());
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2959
            fm.format("    compressedSize  : %d%n", compressedSize());
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2960
            fm.format("    crc             : %x%n", crc());
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2961
            fm.format("    method          : %d%n", method());
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2962
            Set<PosixFilePermission> permissions = storedPermissions().orElse(null);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2963
            if (permissions != null) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2964
                fm.format("    permissions     : %s%n", permissions);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2965
            }
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2966
            fm.close();
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2967
            return sb.toString();
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2968
        }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2969
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2970
        ///////// basic file attributes ///////////
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2971
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2972
        public FileTime creationTime() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2973
            return FileTime.fromMillis(ctime == -1 ? mtime : ctime);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2974
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2975
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2976
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2977
        public boolean isDirectory() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2978
            return isDir();
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2979
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2980
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2981
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2982
        public boolean isOther() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2983
            return false;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2984
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2985
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2986
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2987
        public boolean isRegularFile() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2988
            return !isDir();
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2989
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2990
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2991
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2992
        public FileTime lastAccessTime() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2993
            return FileTime.fromMillis(atime == -1 ? mtime : atime);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2994
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2995
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2996
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2997
        public FileTime lastModifiedTime() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2998
            return FileTime.fromMillis(mtime);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2999
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3000
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3001
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3002
        public long size() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3003
            return size;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3004
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3005
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3006
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3007
        public boolean isSymbolicLink() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3008
            return false;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3009
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3010
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3011
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3012
        public Object fileKey() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3013
            return null;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3014
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3015
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  3016
        ///////// zip file attributes ///////////
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  3017
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  3018
        @Override
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3019
        public long compressedSize() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3020
            return csize;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3021
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3022
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  3023
        @Override
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3024
        public long crc() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3025
            return crc;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3026
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3027
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  3028
        @Override
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3029
        public int method() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3030
            return method;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3031
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3032
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  3033
        @Override
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3034
        public byte[] extra() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3035
            if (extra != null)
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3036
                return Arrays.copyOf(extra, extra.length);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3037
            return null;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3038
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3039
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  3040
        @Override
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3041
        public byte[] comment() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3042
            if (comment != null)
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3043
                return Arrays.copyOf(comment, comment.length);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3044
            return null;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3045
        }
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3046
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3047
        @Override
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3048
        public Optional<Set<PosixFilePermission>> storedPermissions() {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3049
            Set<PosixFilePermission> perms = null;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3050
            if (posixPerms != -1) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3051
                perms = new HashSet<>(PosixFilePermission.values().length);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3052
                for (PosixFilePermission perm : PosixFilePermission.values()) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3053
                    if ((posixPerms & ZipUtils.permToFlag(perm)) != 0) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3054
                        perms.add(perm);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3055
                    }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3056
                }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3057
            }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3058
            return Optional.ofNullable(perms);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3059
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3060
    }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3061
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3062
    final class PosixEntry extends Entry implements PosixFileAttributes {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3063
        private UserPrincipal owner = defaultOwner;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3064
        private GroupPrincipal group = defaultGroup;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3065
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3066
        PosixEntry(byte[] name, boolean isdir, int method) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3067
            super(name, isdir, method);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3068
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3069
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3070
        PosixEntry(byte[] name, int type, boolean isdir, int method, FileAttribute<?>... attrs) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3071
            super(name, type, isdir, method, attrs);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3072
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3073
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3074
        PosixEntry(byte[] name, Path file, int type, FileAttribute<?>... attrs) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3075
            super(name, file, type, attrs);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3076
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3077
58845
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 58822
diff changeset
  3078
        PosixEntry(PosixEntry e, int type, int compressionMethod) {
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 58822
diff changeset
  3079
            super(e, type);
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 58822
diff changeset
  3080
            this.method = compressionMethod;
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 58822
diff changeset
  3081
        }
e492513d3630 8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file
lancea
parents: 58822
diff changeset
  3082
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3083
        PosixEntry(PosixEntry e, int type) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3084
            super(e, type);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3085
            this.owner = e.owner;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3086
            this.group = e.group;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3087
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3088
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3089
        PosixEntry(ZipFileSystem zipfs, IndexNode inode) throws IOException {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3090
            super(zipfs, inode);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3091
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3092
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3093
        @Override
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3094
        public UserPrincipal owner() {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3095
            return owner;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3096
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3097
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3098
        @Override
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3099
        public GroupPrincipal group() {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3100
            return group;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3101
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3102
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3103
        @Override
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3104
        public Set<PosixFilePermission> permissions() {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3105
            return storedPermissions().orElse(Set.copyOf(defaultPermissions));
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  3106
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  3107
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  3108
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  3109
    private static class ExistingChannelCloser {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  3110
        private final Path path;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  3111
        private final SeekableByteChannel ch;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  3112
        private final Set<InputStream> streams;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  3113
        ExistingChannelCloser(Path path,
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  3114
                              SeekableByteChannel ch,
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  3115
                              Set<InputStream> streams) {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  3116
            this.path = path;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  3117
            this.ch = ch;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  3118
            this.streams = streams;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  3119
        }
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  3120
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  3121
        /**
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  3122
         * If there are no more outstanding streams, close the channel and
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  3123
         * delete the backing file
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  3124
         *
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  3125
         * @return true if we're done and closed the backing file,
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  3126
         *         otherwise false
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  3127
         * @throws IOException
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  3128
         */
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  3129
        private boolean closeAndDeleteIfDone() throws IOException {
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  3130
            if (streams.isEmpty()) {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  3131
                ch.close();
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  3132
                Files.delete(path);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  3133
                return true;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  3134
            }
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  3135
            return false;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  3136
        }
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  3137
    }
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  3138
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3139
    // purely for parent lookup, so we don't have to copy the parent
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3140
    // name every time
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3141
    static class ParentLookup extends IndexNode {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3142
        int len;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3143
        ParentLookup() {}
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3144
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3145
        final ParentLookup as(byte[] name, int len) { // as a lookup "key"
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3146
            name(name, len);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3147
            return this;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3148
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3149
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3150
        void name(byte[] name, int len) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3151
            this.name = name;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3152
            this.len = len;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3153
            // calculate the hashcode the same way as Arrays.hashCode() does
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3154
            int result = 1;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3155
            for (int i = 0; i < len; i++)
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3156
                result = 31 * result + name[i];
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3157
            this.hashcode = result;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3158
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3159
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3160
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3161
        public boolean equals(Object other) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3162
            if (!(other instanceof IndexNode)) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3163
                return false;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3164
            }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3165
            byte[] oname = ((IndexNode)other).name;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3166
            return Arrays.equals(name, 0, len,
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3167
                                 oname, 0, oname.length);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  3168
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  3169
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  3170
}