8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
8034773: (zipfs) newOutputstream uses CREATE_NEW when no options specified To open the new steram with appropricate open options Reviewed-by: alanb
This commit is contained in:
parent
48a0b96187
commit
ba7c5970f9
@ -503,6 +503,7 @@ 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");
|
||||||
@ -512,7 +513,11 @@ 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.
|
||||||
@ -564,6 +569,8 @@ 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
|
||||||
@ -711,15 +718,19 @@ 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));
|
||||||
|
@ -773,7 +773,7 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* 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,7 +22,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/* @test
|
/* @test
|
||||||
* @bug 7156873 8040059
|
* @bug 7156873 8040059 8028480 8034773
|
||||||
* @summary ZipFileSystem regression tests
|
* @summary ZipFileSystem regression tests
|
||||||
*
|
*
|
||||||
* @run main ZFSTests
|
* @run main ZFSTests
|
||||||
@ -30,15 +30,19 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
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 {
|
||||||
@ -56,4 +60,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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user