提交 2a57d4f7 编写于 作者: S sherman

7007596: (zipfs) FileSystems.newFileSystem(FileRef...) always employs zipfs...

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
上级 512644a1
...@@ -42,6 +42,7 @@ import java.net.URISyntaxException; ...@@ -42,6 +42,7 @@ import java.net.URISyntaxException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.zip.ZipError;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
/* /*
...@@ -78,42 +79,63 @@ public class ZipFileSystemProvider extends FileSystemProvider { ...@@ -78,42 +79,63 @@ public class ZipFileSystemProvider extends FileSystemProvider {
} }
} }
@Override private boolean ensureFile(Path path) {
public FileSystem newFileSystem(URI uri, Map<String, ?> env) try {
throws IOException BasicFileAttributes attrs =
{ Files.readAttributes(path, BasicFileAttributes.class);
return newFileSystem(uriToPath(uri), env, true); if (!attrs.isRegularFile())
} throw new UnsupportedOperationException();
return true;
@Override } catch (IOException ioe) {
public FileSystem newFileSystem(Path path, Map<String, ?> env) return false;
throws IOException
{
if (!path.toUri().getScheme().equalsIgnoreCase("file")) {
throw new UnsupportedOperationException();
} }
return newFileSystem(path, env, false);
} }
private FileSystem newFileSystem(Path path, Map<String, ?> env, boolean checkIfFSExists) @Override
public FileSystem newFileSystem(URI uri, Map<String, ?> env)
throws IOException throws IOException
{ {
Path path = uriToPath(uri);
synchronized(filesystems) { synchronized(filesystems) {
Path realPath = null; Path realPath = null;
if (checkIfFSExists && Files.exists(path)) { if (ensureFile(path)) {
realPath = path.toRealPath(true); realPath = path.toRealPath(true);
if (filesystems.containsKey(realPath)) if (filesystems.containsKey(realPath))
throw new FileSystemAlreadyExistsException(); throw new FileSystemAlreadyExistsException();
} }
ZipFileSystem zipfs = new ZipFileSystem(this, path, env); ZipFileSystem zipfs = null;
if (realPath == null) try {
realPath = path.toRealPath(true); zipfs = new ZipFileSystem(this, path, env);
if (!filesystems.containsKey(realPath)) } catch (ZipError ze) {
filesystems.put(realPath, zipfs); 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; return zipfs;
} }
} }
@Override
public FileSystem newFileSystem(Path path, Map<String, ?> env)
throws IOException
{
if (path.getFileSystem() != FileSystems.getDefault()) {
throw new UnsupportedOperationException();
}
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();
}
}
@Override @Override
public Path getPath(URI uri) { public Path getPath(URI uri) {
......
...@@ -105,6 +105,18 @@ public class ZipFSTester { ...@@ -105,6 +105,18 @@ public class ZipFSTester {
os.write(bits); os.write(bits);
os.close(); 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 // copyin
Path dst = getPathWithParents(fs, tmpName); Path dst = getPathWithParents(fs, tmpName);
Files.copy(src, dst); Files.copy(src, dst);
......
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
# questions. # questions.
# #
# @test # @test
# @bug 6990846 7009092 7009085 7015391 7014948 7005986 7017840 # @bug 6990846 7009092 7009085 7015391 7014948 7005986 7017840 7007596
# @summary Test ZipFileSystem demo # @summary Test ZipFileSystem demo
# @build Basic PathOps ZipFSTester # @build Basic PathOps ZipFSTester
# @run shell basic.sh # @run shell basic.sh
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册