提交 ed0b3549 编写于 作者: S sherman

8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE

8034773: (zipfs) newOutputstream uses CREATE_NEW when no options specified
Summary: to open the new steram with appropricate open options
Reviewed-by: alanb, clanger
上级 0d61098c
...@@ -497,6 +497,7 @@ public class ZipFileSystem extends FileSystem { ...@@ -497,6 +497,7 @@ public class ZipFileSystem extends FileSystem {
boolean hasCreateNew = false; boolean hasCreateNew = false;
boolean hasCreate = false; boolean hasCreate = false;
boolean hasAppend = false; boolean hasAppend = false;
boolean hasTruncate = false;
for (OpenOption opt: options) { for (OpenOption opt: options) {
if (opt == READ) if (opt == READ)
throw new IllegalArgumentException("READ not allowed"); throw new IllegalArgumentException("READ not allowed");
...@@ -506,7 +507,11 @@ public class ZipFileSystem extends FileSystem { ...@@ -506,7 +507,11 @@ public class ZipFileSystem extends FileSystem {
hasCreate = true; hasCreate = true;
if (opt == APPEND) if (opt == APPEND)
hasAppend = true; hasAppend = true;
if (opt == TRUNCATE_EXISTING)
hasTruncate = true;
} }
if (hasAppend && hasTruncate)
throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed");
beginRead(); // only need a readlock, the "update()" will beginRead(); // only need a readlock, the "update()" will
try { // try to obtain a writelock when the os is try { // try to obtain a writelock when the os is
ensureOpen(); // being closed. ensureOpen(); // being closed.
...@@ -558,6 +563,8 @@ public class ZipFileSystem extends FileSystem { ...@@ -558,6 +563,8 @@ public class ZipFileSystem extends FileSystem {
if (!(option instanceof StandardOpenOption)) if (!(option instanceof StandardOpenOption))
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
if (options.contains(APPEND) && options.contains(TRUNCATE_EXISTING))
throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed");
} }
// Returns a Writable/ReadByteChannel for now. Might consdier to use // Returns a Writable/ReadByteChannel for now. Might consdier to use
...@@ -705,15 +712,19 @@ public class ZipFileSystem extends FileSystem { ...@@ -705,15 +712,19 @@ public class ZipFileSystem extends FileSystem {
if (forWrite) { if (forWrite) {
checkWritable(); checkWritable();
if (e == null) { if (e == null) {
if (!options.contains(StandardOpenOption.CREATE_NEW)) if (!options.contains(StandardOpenOption.CREATE) &&
throw new NoSuchFileException(getString(path)); !options.contains(StandardOpenOption.CREATE_NEW)) {
throw new NoSuchFileException(getString(path));
}
} else { } else {
if (options.contains(StandardOpenOption.CREATE_NEW)) if (options.contains(StandardOpenOption.CREATE_NEW)) {
throw new FileAlreadyExistsException(getString(path)); throw new FileAlreadyExistsException(getString(path));
}
if (e.isDir()) if (e.isDir())
throw new FileAlreadyExistsException("directory <" throw new FileAlreadyExistsException("directory <"
+ getString(path) + "> exists"); + getString(path) + "> exists");
} }
options = new HashSet<>(options);
options.remove(StandardOpenOption.CREATE_NEW); // for tmpfile options.remove(StandardOpenOption.CREATE_NEW); // for tmpfile
} else if (e == null || e.isDir()) { } else if (e == null || e.isDir()) {
throw new NoSuchFileException(getString(path)); throw new NoSuchFileException(getString(path));
......
...@@ -788,7 +788,7 @@ public class ZipPath implements Path { ...@@ -788,7 +788,7 @@ public class ZipPath implements Path {
{ {
if (options.length == 0) if (options.length == 0)
return zfs.newOutputStream(getResolvedPath(), return zfs.newOutputStream(getResolvedPath(),
CREATE_NEW, WRITE); CREATE, TRUNCATE_EXISTING, WRITE);
return zfs.newOutputStream(getResolvedPath(), options); return zfs.newOutputStream(getResolvedPath(), options);
} }
......
/* /*
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -22,20 +22,23 @@ ...@@ -22,20 +22,23 @@
*/ */
/* @test /* @test
@bug 7156873 @bug 7156873 8028480 8034773
@summary ZipFileSystem regression tests @summary ZipFileSystem regression tests
*/ */
import java.io.OutputStream;
import java.net.URI; import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.nio.file.*; import java.nio.file.*;
import java.util.Map; import java.nio.file.spi.*;
import java.util.HashMap; import java.util.*;
public class ZFSTests { public class ZFSTests {
public static void main(String[] args) throws Throwable { public static void main(String[] args) throws Throwable {
test7156873(); test7156873();
testOpenOptions();
} }
static void test7156873() throws Throwable { static void test7156873() throws Throwable {
...@@ -53,4 +56,44 @@ public class ZFSTests { ...@@ -53,4 +56,44 @@ public class ZFSTests {
Files.deleteIfExists(dir); Files.deleteIfExists(dir);
} }
} }
static void testOpenOptions() throws Throwable {
Path path = Paths.get("file.zip");
try {
URI uri = URI.create("jar:" + path.toUri());
Map<String, Object> env = new HashMap<String, Object>();
env.put("create", "true");
try (FileSystem fs = FileSystems.newFileSystem(uri, env)) {
FileSystemProvider fsp = fs.provider();
Set<? extends OpenOption> options;
Path p = fs.getPath("test.txt");
// 8028480
options = EnumSet.of(StandardOpenOption.CREATE,
StandardOpenOption.WRITE,
StandardOpenOption.APPEND);
try (FileChannel ch = fsp.newFileChannel(p, options)) {
ch.write(ByteBuffer.wrap("Hello!".getBytes("ASCII")));
}
// 8034773
try (OutputStream os = fsp.newOutputStream(p, new OpenOption[0])) {
os.write("Hello2!".getBytes("ASCII"));
}
if (!"Hello2!".equals(new String(
Files.readAllBytes(fs.getPath("test.txt"))))) {
throw new RuntimeException("failed to open as truncate_existing");
}
options = EnumSet.of(StandardOpenOption.CREATE,
StandardOpenOption.APPEND,
StandardOpenOption.TRUNCATE_EXISTING);
try (FileChannel ch = fsp.newFileChannel(p, options)) {
throw new RuntimeException("expected IAE not thrown!");
} catch (IllegalArgumentException x) {
// expected x.printStackTrace();
}
}
} finally {
Files.deleteIfExists(path);
}
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册