7007596: (zipfs) FileSystems.newFileSystem(FileRef...) always employs zipfs regardless the real Path type.
authorsherman
Fri, 11 Feb 2011 12:20:45 -0800
changeset 8386 4a1a689a32e0
parent 8385 cbcff564fa0d
child 8387 f0fa7bbf889e
7007596: (zipfs) FileSystems.newFileSystem(FileRef...) always employs zipfs regardless the real Path type. Summary: updated newFileSystem() to throw UOE exception for non-zip/jar file Reviewed-by: alanb
jdk/src/share/demo/nio/zipfs/src/com/sun/nio/zipfs/ZipFileSystemProvider.java
jdk/test/demo/zipfs/ZipFSTester.java
jdk/test/demo/zipfs/basic.sh
--- a/jdk/src/share/demo/nio/zipfs/src/com/sun/nio/zipfs/ZipFileSystemProvider.java	Fri Feb 11 01:45:55 2011 -0800
+++ b/jdk/src/share/demo/nio/zipfs/src/com/sun/nio/zipfs/ZipFileSystemProvider.java	Fri Feb 11 12:20:45 2011 -0800
@@ -42,6 +42,7 @@
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
+import java.util.zip.ZipError;
 import java.util.concurrent.ExecutorService;
 
 /*
@@ -78,39 +79,60 @@
         }
     }
 
+    private boolean ensureFile(Path path) {
+        try {
+            BasicFileAttributes attrs =
+                Files.readAttributes(path, BasicFileAttributes.class);
+            if (!attrs.isRegularFile())
+                throw new UnsupportedOperationException();
+            return true;
+        } catch (IOException ioe) {
+            return false;
+        }
+    }
+
     @Override
     public FileSystem newFileSystem(URI uri, Map<String, ?> env)
         throws IOException
     {
-        return newFileSystem(uriToPath(uri), env, true);
+        Path path = uriToPath(uri);
+        synchronized(filesystems) {
+            Path realPath = null;
+            if (ensureFile(path)) {
+                realPath = path.toRealPath(true);
+                if (filesystems.containsKey(realPath))
+                    throw new FileSystemAlreadyExistsException();
+            }
+            ZipFileSystem zipfs = null;
+            try {
+                zipfs = new ZipFileSystem(this, path, env);
+            } catch (ZipError ze) {
+                String pname = path.toString();
+                if (pname.endsWith(".zip") || pname.endsWith(".jar"))
+                    throw ze;
+                // assume NOT a zip/jar file
+                throw new UnsupportedOperationException();
+            }
+            filesystems.put(realPath, zipfs);
+            return zipfs;
+        }
     }
 
     @Override
     public FileSystem newFileSystem(Path path, Map<String, ?> env)
         throws IOException
     {
-        if (!path.toUri().getScheme().equalsIgnoreCase("file")) {
+        if (path.getFileSystem() != FileSystems.getDefault()) {
             throw new UnsupportedOperationException();
         }
-        return newFileSystem(path, env, false);
-    }
-
-    private FileSystem newFileSystem(Path path, Map<String, ?> env, boolean checkIfFSExists)
-        throws IOException
-    {
-        synchronized(filesystems) {
-            Path realPath = null;
-            if (checkIfFSExists && Files.exists(path)) {
-                realPath = path.toRealPath(true);
-                if (filesystems.containsKey(realPath))
-                    throw new FileSystemAlreadyExistsException();
-            }
-            ZipFileSystem zipfs = new ZipFileSystem(this, path, env);
-            if (realPath == null)
-                realPath = path.toRealPath(true);
-            if (!filesystems.containsKey(realPath))
-                filesystems.put(realPath, zipfs);
-            return zipfs;
+        ensureFile(path);
+        try {
+            return new ZipFileSystem(this, path, env);
+        } catch (ZipError ze) {
+            String pname = path.toString();
+            if (pname.endsWith(".zip") || pname.endsWith(".jar"))
+                throw ze;
+            throw new UnsupportedOperationException();
         }
     }
 
--- a/jdk/test/demo/zipfs/ZipFSTester.java	Fri Feb 11 01:45:55 2011 -0800
+++ b/jdk/test/demo/zipfs/ZipFSTester.java	Fri Feb 11 12:20:45 2011 -0800
@@ -105,6 +105,18 @@
             os.write(bits);
             os.close();
 
+            try {
+                provider.newFileSystem(new File(System.getProperty("test.src", ".")).toPath(),
+                                       new HashMap<String, Object>());
+                throw new RuntimeException("newFileSystem() opens a directory as zipfs");
+            } catch (UnsupportedOperationException uoe) {}
+
+            try {
+                provider.newFileSystem(src, new HashMap<String, Object>());
+                throw new RuntimeException("newFileSystem() opens a non-zip file as zipfs");
+            } catch (UnsupportedOperationException uoe) {}
+
+
             // copyin
             Path dst = getPathWithParents(fs, tmpName);
             Files.copy(src, dst);
--- a/jdk/test/demo/zipfs/basic.sh	Fri Feb 11 01:45:55 2011 -0800
+++ b/jdk/test/demo/zipfs/basic.sh	Fri Feb 11 12:20:45 2011 -0800
@@ -21,7 +21,7 @@
 # questions.
 #
 # @test
-# @bug 6990846 7009092 7009085 7015391 7014948 7005986 7017840
+# @bug 6990846 7009092 7009085 7015391 7014948 7005986 7017840 7007596
 # @summary Test ZipFileSystem demo
 # @build Basic PathOps ZipFSTester
 # @run shell basic.sh