1 /*
   2  * Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nio.zipfs;
  27 
  28 import java.io.*;
  29 import java.nio.channels.*;
  30 import java.nio.file.*;
  31 import java.nio.file.DirectoryStream.Filter;
  32 import java.nio.file.attribute.*;
  33 import java.nio.file.spi.FileSystemProvider;
  34 import java.net.URI;
  35 import java.net.URISyntaxException;
  36 import java.util.HashMap;
  37 import java.util.Map;
  38 import java.util.Set;
  39 import java.util.zip.ZipException;
  40 import java.util.concurrent.ExecutorService;
  41 
  42 /*
  43  *
  44  * @author  Xueming Shen, Rajendra Gutupalli, Jaya Hangal
  45  */
  46 
  47 public class ZipFileSystemProvider extends FileSystemProvider {
  48 
  49 
  50     private final Map<Path, ZipFileSystem> filesystems = new HashMap<>();
  51 
  52     public ZipFileSystemProvider() {}
  53 
  54     @Override
  55     public String getScheme() {
  56         return "jar";
  57     }
  58 
  59     protected Path uriToPath(URI uri) {
  60         String scheme = uri.getScheme();
  61         if ((scheme == null) || !scheme.equalsIgnoreCase(getScheme())) {
  62             throw new IllegalArgumentException("URI scheme is not '" + getScheme() + "'");
  63         }
  64         try {
  65             // only support legacy JAR URL syntax  jar:{uri}!/{entry} for now
  66             String spec = uri.getRawSchemeSpecificPart();
  67             int sep = spec.indexOf("!/");
  68             if (sep != -1) {
  69                 spec = spec.substring(0, sep);
  70             }
  71             return Paths.get(new URI(spec)).toAbsolutePath();
  72         } catch (URISyntaxException e) {
  73             throw new IllegalArgumentException(e.getMessage(), e);
  74         }
  75     }
  76 
  77     private boolean ensureFile(Path path) {
  78         try {
  79             BasicFileAttributes attrs =
  80                 Files.readAttributes(path, BasicFileAttributes.class);
  81             if (!attrs.isRegularFile())
  82                 throw new UnsupportedOperationException();
  83             return true;
  84         } catch (IOException ioe) {
  85             return false;
  86         }
  87     }
  88 
  89     @Override
  90     public FileSystem newFileSystem(URI uri, Map<String, ?> env)
  91         throws IOException
  92     {
  93         Path path = uriToPath(uri);
  94         synchronized(filesystems) {
  95             Path realPath = null;
  96             if (ensureFile(path)) {
  97                 realPath = path.toRealPath();
  98                 if (filesystems.containsKey(realPath))
  99                     throw new FileSystemAlreadyExistsException();
 100             }
 101             ZipFileSystem zipfs = null;
 102             try {
 103                 if (env.containsKey("multi-release")) {
 104                     zipfs = new JarFileSystem(this, path, env);
 105                 } else {
 106                     zipfs = new ZipFileSystem(this, path, env);
 107                 }
 108             } catch (ZipException ze) {
 109                 String pname = path.toString();
 110                 if (pname.endsWith(".zip") || pname.endsWith(".jar"))
 111                     throw ze;
 112                 // assume NOT a zip/jar file
 113                 throw new UnsupportedOperationException();
 114             }
 115             if (realPath == null) {  // newly created
 116                 realPath = path.toRealPath();
 117             }
 118             filesystems.put(realPath, zipfs);
 119             return zipfs;
 120         }
 121     }
 122 
 123     @Override
 124     public FileSystem newFileSystem(Path path, Map<String, ?> env)
 125         throws IOException
 126     {
 127         ensureFile(path);
 128          try {
 129              ZipFileSystem zipfs;
 130              if (env.containsKey("multi-release")) {
 131                  zipfs = new JarFileSystem(this, path, env);
 132              } else {
 133                  zipfs = new ZipFileSystem(this, path, env);
 134              }
 135             return zipfs;
 136         } catch (ZipException ze) {
 137             String pname = path.toString();
 138             if (pname.endsWith(".zip") || pname.endsWith(".jar"))
 139                 throw ze;
 140             throw new UnsupportedOperationException();
 141         }
 142     }
 143 
 144     @Override
 145     public Path getPath(URI uri) {
 146         String spec = uri.getSchemeSpecificPart();
 147         int sep = spec.indexOf("!/");
 148         if (sep == -1)
 149             throw new IllegalArgumentException("URI: "
 150                 + uri
 151                 + " does not contain path info ex. jar:file:/c:/foo.zip!/BAR");
 152         return getFileSystem(uri).getPath(spec.substring(sep + 1));
 153     }
 154 
 155 
 156     @Override
 157     public FileSystem getFileSystem(URI uri) {
 158         synchronized (filesystems) {
 159             ZipFileSystem zipfs = null;
 160             try {
 161                 zipfs = filesystems.get(uriToPath(uri).toRealPath());
 162             } catch (IOException x) {
 163                 // ignore the ioe from toRealPath(), return FSNFE
 164             }
 165             if (zipfs == null)
 166                 throw new FileSystemNotFoundException();
 167             return zipfs;
 168         }
 169     }
 170 
 171     // Checks that the given file is a UnixPath
 172     static final ZipPath toZipPath(Path path) {
 173         if (path == null)
 174             throw new NullPointerException();
 175         if (!(path instanceof ZipPath))
 176             throw new ProviderMismatchException();
 177         return (ZipPath)path;
 178     }
 179 
 180     @Override
 181     public void checkAccess(Path path, AccessMode... modes) throws IOException {
 182         toZipPath(path).checkAccess(modes);
 183     }
 184 
 185     @Override
 186     public void copy(Path src, Path target, CopyOption... options)
 187         throws IOException
 188     {
 189         toZipPath(src).copy(toZipPath(target), options);
 190     }
 191 
 192     @Override
 193     public void createDirectory(Path path, FileAttribute<?>... attrs)
 194         throws IOException
 195     {
 196         toZipPath(path).createDirectory(attrs);
 197     }
 198 
 199     @Override
 200     public final void delete(Path path) throws IOException {
 201         toZipPath(path).delete();
 202     }
 203 
 204     @Override
 205     @SuppressWarnings("unchecked")
 206     public <V extends FileAttributeView> V
 207         getFileAttributeView(Path path, Class<V> type, LinkOption... options)
 208     {
 209         return ZipFileAttributeView.get(toZipPath(path), type);
 210     }
 211 
 212     @Override
 213     public FileStore getFileStore(Path path) throws IOException {
 214         return toZipPath(path).getFileStore();
 215     }
 216 
 217     @Override
 218     public boolean isHidden(Path path) {
 219         return toZipPath(path).isHidden();
 220     }
 221 
 222     @Override
 223     public boolean isSameFile(Path path, Path other) throws IOException {
 224         return toZipPath(path).isSameFile(other);
 225     }
 226 
 227     @Override
 228     public void move(Path src, Path target, CopyOption... options)
 229         throws IOException
 230     {
 231         toZipPath(src).move(toZipPath(target), options);
 232     }
 233 
 234     @Override
 235     public AsynchronousFileChannel newAsynchronousFileChannel(Path path,
 236             Set<? extends OpenOption> options,
 237             ExecutorService exec,
 238             FileAttribute<?>... attrs)
 239             throws IOException
 240     {
 241         throw new UnsupportedOperationException();
 242     }
 243 
 244     @Override
 245     public SeekableByteChannel newByteChannel(Path path,
 246                                               Set<? extends OpenOption> options,
 247                                               FileAttribute<?>... attrs)
 248         throws IOException
 249     {
 250         return toZipPath(path).newByteChannel(options, attrs);
 251     }
 252 
 253     @Override
 254     public DirectoryStream<Path> newDirectoryStream(
 255         Path path, Filter<? super Path> filter) throws IOException
 256     {
 257         return toZipPath(path).newDirectoryStream(filter);
 258     }
 259 
 260     @Override
 261     public FileChannel newFileChannel(Path path,
 262                                       Set<? extends OpenOption> options,
 263                                       FileAttribute<?>... attrs)
 264         throws IOException
 265     {
 266         return toZipPath(path).newFileChannel(options, attrs);
 267     }
 268 
 269     @Override
 270     public InputStream newInputStream(Path path, OpenOption... options)
 271         throws IOException
 272     {
 273         return toZipPath(path).newInputStream(options);
 274     }
 275 
 276     @Override
 277     public OutputStream newOutputStream(Path path, OpenOption... options)
 278         throws IOException
 279     {
 280         return toZipPath(path).newOutputStream(options);
 281     }
 282 
 283     @Override
 284     @SuppressWarnings("unchecked") // Cast to A
 285     public <A extends BasicFileAttributes> A
 286         readAttributes(Path path, Class<A> type, LinkOption... options)
 287         throws IOException
 288     {
 289         if (type == BasicFileAttributes.class || type == ZipFileAttributes.class)
 290             return (A)toZipPath(path).getAttributes();
 291         return null;
 292     }
 293 
 294     @Override
 295     public Map<String, Object>
 296         readAttributes(Path path, String attribute, LinkOption... options)
 297         throws IOException
 298     {
 299         return toZipPath(path).readAttributes(attribute, options);
 300     }
 301 
 302     @Override
 303     public Path readSymbolicLink(Path link) throws IOException {
 304         throw new UnsupportedOperationException("Not supported.");
 305     }
 306 
 307     @Override
 308     public void setAttribute(Path path, String attribute,
 309                              Object value, LinkOption... options)
 310         throws IOException
 311     {
 312         toZipPath(path).setAttribute(attribute, value, options);
 313     }
 314 
 315     //////////////////////////////////////////////////////////////
 316     void removeFileSystem(Path zfpath, ZipFileSystem zfs) throws IOException {
 317         synchronized (filesystems) {
 318             zfpath = zfpath.toRealPath();
 319             if (filesystems.get(zfpath) == zfs)
 320                 filesystems.remove(zfpath);
 321         }
 322     }
 323 }