rev 52310 : 8213031: (zipfs) Add support for POSIX file permissions
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.IOException; 29 import java.io.InputStream; 30 import java.io.OutputStream; 31 import java.net.URI; 32 import java.net.URISyntaxException; 33 import java.nio.channels.AsynchronousFileChannel; 34 import java.nio.channels.FileChannel; 35 import java.nio.channels.SeekableByteChannel; 36 import java.nio.file.AccessMode; 37 import java.nio.file.CopyOption; 38 import java.nio.file.DirectoryStream; 39 import java.nio.file.DirectoryStream.Filter; 40 import java.nio.file.FileStore; 41 import java.nio.file.FileSystem; 42 import java.nio.file.FileSystemAlreadyExistsException; 43 import java.nio.file.FileSystemNotFoundException; 44 import java.nio.file.Files; 45 import java.nio.file.LinkOption; 46 import java.nio.file.OpenOption; 47 import java.nio.file.Path; 48 import java.nio.file.Paths; 49 import java.nio.file.ProviderMismatchException; 50 import java.nio.file.attribute.BasicFileAttributes; 51 import java.nio.file.attribute.FileAttribute; 52 import java.nio.file.attribute.FileAttributeView; 53 import java.nio.file.attribute.PosixFileAttributes; 54 import java.nio.file.spi.FileSystemProvider; 55 import java.util.HashMap; 56 import java.util.Map; 57 import java.util.Set; 58 import java.util.concurrent.ExecutorService; 59 import java.util.zip.ZipException; 60 61 /** 62 * @author Xueming Shen, Rajendra Gutupalli, Jaya Hangal 63 */ 64 public class ZipFileSystemProvider extends FileSystemProvider { 65 66 private final Map<Path, ZipFileSystem> filesystems = new HashMap<>(); 67 68 public ZipFileSystemProvider() {} 69 70 @Override 71 public String getScheme() { 72 return "jar"; 73 } 74 75 protected Path uriToPath(URI uri) { 76 String scheme = uri.getScheme(); 77 if ((scheme == null) || !scheme.equalsIgnoreCase(getScheme())) { 78 throw new IllegalArgumentException("URI scheme is not '" + getScheme() + "'"); 79 } 80 try { 81 // only support legacy JAR URL syntax jar:{uri}!/{entry} for now 82 String spec = uri.getRawSchemeSpecificPart(); 83 int sep = spec.indexOf("!/"); 84 if (sep != -1) { 85 spec = spec.substring(0, sep); 86 } 87 return Paths.get(new URI(spec)).toAbsolutePath(); 88 } catch (URISyntaxException e) { 89 throw new IllegalArgumentException(e.getMessage(), e); 90 } 91 } 92 93 private boolean ensureFile(Path path) { 94 try { 95 BasicFileAttributes attrs = 96 Files.readAttributes(path, BasicFileAttributes.class); 97 if (!attrs.isRegularFile()) 98 throw new UnsupportedOperationException(); 99 return true; 100 } catch (IOException ioe) { 101 return false; 102 } 103 } 104 105 @Override 106 public FileSystem newFileSystem(URI uri, Map<String, ?> env) 107 throws IOException 108 { 109 Path path = uriToPath(uri); 110 synchronized(filesystems) { 111 Path realPath = null; 112 if (ensureFile(path)) { 113 realPath = path.toRealPath(); 114 if (filesystems.containsKey(realPath)) 115 throw new FileSystemAlreadyExistsException(); 116 } 117 ZipFileSystem zipfs = null; 118 try { 119 if (env.containsKey("multi-release")) { 120 zipfs = new JarFileSystem(this, path, env); 121 } else { 122 zipfs = new ZipFileSystem(this, path, env); 123 } 124 } catch (ZipException ze) { 125 String pname = path.toString(); 126 if (pname.endsWith(".zip") || pname.endsWith(".jar")) 127 throw ze; 128 // assume NOT a zip/jar file 129 throw new UnsupportedOperationException(); 130 } 131 if (realPath == null) { // newly created 132 realPath = path.toRealPath(); 133 } 134 filesystems.put(realPath, zipfs); 135 return zipfs; 136 } 137 } 138 139 @Override 140 public FileSystem newFileSystem(Path path, Map<String, ?> env) 141 throws IOException 142 { 143 ensureFile(path); 144 try { 145 ZipFileSystem zipfs; 146 if (env.containsKey("multi-release")) { 147 zipfs = new JarFileSystem(this, path, env); 148 } else { 149 zipfs = new ZipFileSystem(this, path, env); 150 } 151 return zipfs; 152 } catch (ZipException ze) { 153 String pname = path.toString(); 154 if (pname.endsWith(".zip") || pname.endsWith(".jar")) 155 throw ze; 156 throw new UnsupportedOperationException(); 157 } 158 } 159 160 @Override 161 public Path getPath(URI uri) { 162 String spec = uri.getSchemeSpecificPart(); 163 int sep = spec.indexOf("!/"); 164 if (sep == -1) 165 throw new IllegalArgumentException("URI: " 166 + uri 167 + " does not contain path info ex. jar:file:/c:/foo.zip!/BAR"); 168 return getFileSystem(uri).getPath(spec.substring(sep + 1)); 169 } 170 171 172 @Override 173 public FileSystem getFileSystem(URI uri) { 174 synchronized (filesystems) { 175 ZipFileSystem zipfs = null; 176 try { 177 zipfs = filesystems.get(uriToPath(uri).toRealPath()); 178 } catch (IOException x) { 179 // ignore the ioe from toRealPath(), return FSNFE 180 } 181 if (zipfs == null) 182 throw new FileSystemNotFoundException(); 183 return zipfs; 184 } 185 } 186 187 // Checks that the given file is a UnixPath 188 static final ZipPath toZipPath(Path path) { 189 if (path == null) 190 throw new NullPointerException(); 191 if (!(path instanceof ZipPath)) 192 throw new ProviderMismatchException(); 193 return (ZipPath)path; 194 } 195 196 @Override 197 public void checkAccess(Path path, AccessMode... modes) throws IOException { 198 toZipPath(path).checkAccess(modes); 199 } 200 201 @Override 202 public void copy(Path src, Path target, CopyOption... options) 203 throws IOException 204 { 205 toZipPath(src).copy(toZipPath(target), options); 206 } 207 208 @Override 209 public void createDirectory(Path path, FileAttribute<?>... attrs) 210 throws IOException 211 { 212 toZipPath(path).createDirectory(attrs); 213 } 214 215 @Override 216 public final void delete(Path path) throws IOException { 217 toZipPath(path).delete(); 218 } 219 220 @Override 221 public <V extends FileAttributeView> V 222 getFileAttributeView(Path path, Class<V> type, LinkOption... options) 223 { 224 return ZipFileAttributeView.get(toZipPath(path), type); 225 } 226 227 @Override 228 public FileStore getFileStore(Path path) throws IOException { 229 return toZipPath(path).getFileStore(); 230 } 231 232 @Override 233 public boolean isHidden(Path path) { 234 return toZipPath(path).isHidden(); 235 } 236 237 @Override 238 public boolean isSameFile(Path path, Path other) throws IOException { 239 return toZipPath(path).isSameFile(other); 240 } 241 242 @Override 243 public void move(Path src, Path target, CopyOption... options) 244 throws IOException 245 { 246 toZipPath(src).move(toZipPath(target), options); 247 } 248 249 @Override 250 public AsynchronousFileChannel newAsynchronousFileChannel(Path path, 251 Set<? extends OpenOption> options, 252 ExecutorService exec, 253 FileAttribute<?>... attrs) 254 throws IOException 255 { 256 throw new UnsupportedOperationException(); 257 } 258 259 @Override 260 public SeekableByteChannel newByteChannel(Path path, 261 Set<? extends OpenOption> options, 262 FileAttribute<?>... attrs) 263 throws IOException 264 { 265 return toZipPath(path).newByteChannel(options, attrs); 266 } 267 268 @Override 269 public DirectoryStream<Path> newDirectoryStream( 270 Path path, Filter<? super Path> filter) throws IOException 271 { 272 return toZipPath(path).newDirectoryStream(filter); 273 } 274 275 @Override 276 public FileChannel newFileChannel(Path path, 277 Set<? extends OpenOption> options, 278 FileAttribute<?>... attrs) 279 throws IOException 280 { 281 return toZipPath(path).newFileChannel(options, attrs); 282 } 283 284 @Override 285 public InputStream newInputStream(Path path, OpenOption... options) 286 throws IOException 287 { 288 return toZipPath(path).newInputStream(options); 289 } 290 291 @Override 292 public OutputStream newOutputStream(Path path, OpenOption... options) 293 throws IOException 294 { 295 return toZipPath(path).newOutputStream(options); 296 } 297 298 @Override 299 @SuppressWarnings("unchecked") // Cast to A 300 public <A extends BasicFileAttributes> A 301 readAttributes(Path path, Class<A> type, LinkOption... options) 302 throws IOException 303 { 304 if (type == BasicFileAttributes.class || 305 type == PosixFileAttributes.class || 306 type == ZipFileAttributes.class) 307 return (A)toZipPath(path).getAttributes(); 308 return null; 309 } 310 311 @Override 312 public Map<String, Object> 313 readAttributes(Path path, String attribute, LinkOption... options) 314 throws IOException 315 { 316 return toZipPath(path).readAttributes(attribute, options); 317 } 318 319 @Override 320 public Path readSymbolicLink(Path link) throws IOException { 321 throw new UnsupportedOperationException("Not supported."); 322 } 323 324 @Override 325 public void setAttribute(Path path, String attribute, 326 Object value, LinkOption... options) 327 throws IOException 328 { 329 toZipPath(path).setAttribute(attribute, value, options); 330 } 331 332 ////////////////////////////////////////////////////////////// 333 void removeFileSystem(Path zfpath, ZipFileSystem zfs) throws IOException { 334 synchronized (filesystems) { 335 zfpath = zfpath.toRealPath(); 336 if (filesystems.get(zfpath) == zfs) 337 filesystems.remove(zfpath); 338 } 339 } 340 } --- EOF ---