aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcgdecker <cgdecker@google.com>2019-03-11 09:33:32 -0700
committerColin Decker <cgdecker@gmail.com>2019-03-21 14:24:58 -0400
commit24a71cc920572bcf3f7cf9bd739843460098dd17 (patch)
tree738f182e0777ef4491c0102f3398c0fb542446ab
parent94ef329706347150b7fec23f45dc37fab6b254fa (diff)
downloadjimfs-24a71cc920572bcf3f7cf9bd739843460098dd17.tar.gz
Fix an issue with Files.newOutputStream for Jimfs where it wouldn't truncate the file if the TRUNCATE_EXISTING option was explicitly provided and the WRITE option wasn't.
The code that handles TRUNCATE_EXISTING requires that WRITE also be present for it to do anything (because it's documented that it's ignored otherwise), but newOutputStream should always add the WRITE option if the user doesn't provide it because an OutputStream is obviously for writing. (This matches the default behavior of FileSystemProvider.newOutputStream as well.) Fixes #77 RELNOTES=Fixed an issue where `Files.newOutputStream` wouldn't truncate the file if the `TRUNCATE_EXISTING` option was explicitly provided and the `WRITE` option wasn't. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=237818016
-rw-r--r--jimfs/src/main/java/com/google/common/jimfs/Options.java25
-rw-r--r--jimfs/src/test/java/com/google/common/jimfs/JimfsUnixLikeFileSystemTest.java22
2 files changed, 31 insertions, 16 deletions
diff --git a/jimfs/src/main/java/com/google/common/jimfs/Options.java b/jimfs/src/main/java/com/google/common/jimfs/Options.java
index 03a5b94..a240eca 100644
--- a/jimfs/src/main/java/com/google/common/jimfs/Options.java
+++ b/jimfs/src/main/java/com/google/common/jimfs/Options.java
@@ -26,10 +26,11 @@ import static java.nio.file.StandardOpenOption.WRITE;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
-
import java.nio.file.CopyOption;
import java.nio.file.LinkOption;
import java.nio.file.OpenOption;
+import java.util.Arrays;
+import java.util.Collection;
import java.util.Set;
/**
@@ -95,15 +96,7 @@ final class Options {
// options contains write or append and may also contain read
// it does not contain both read and append
-
- if (options.contains(WRITE)) {
- return ImmutableSet.copyOf(options);
- } else {
- return new ImmutableSet.Builder<OpenOption>()
- .add(WRITE)
- .addAll(options)
- .build();
- }
+ return addWrite(options);
}
/**
@@ -135,7 +128,7 @@ final class Options {
return DEFAULT_WRITE;
}
- ImmutableSet<OpenOption> result = ImmutableSet.copyOf(options);
+ ImmutableSet<OpenOption> result = addWrite(Arrays.asList(options));
if (result.contains(READ)) {
throw new UnsupportedOperationException("'READ' not allowed");
}
@@ -143,6 +136,16 @@ final class Options {
}
/**
+ * Returns an {@link ImmutableSet} copy of the given {@code options}, adding {@link
+ * StandardOpenOption#WRITE} if it isn't already present.
+ */
+ private static ImmutableSet<OpenOption> addWrite(Collection<? extends OpenOption> options) {
+ return options.contains(WRITE)
+ ? ImmutableSet.copyOf(options)
+ : ImmutableSet.<OpenOption>builder().add(WRITE).addAll(options).build();
+ }
+
+ /**
* Returns an immutable set of the given options for a move.
*/
public static ImmutableSet<CopyOption> getMoveOptions(CopyOption... options) {
diff --git a/jimfs/src/test/java/com/google/common/jimfs/JimfsUnixLikeFileSystemTest.java b/jimfs/src/test/java/com/google/common/jimfs/JimfsUnixLikeFileSystemTest.java
index 8ab4e39..625353c 100644
--- a/jimfs/src/test/java/com/google/common/jimfs/JimfsUnixLikeFileSystemTest.java
+++ b/jimfs/src/test/java/com/google/common/jimfs/JimfsUnixLikeFileSystemTest.java
@@ -51,11 +51,6 @@ import com.google.common.io.ByteStreams;
import com.google.common.io.CharStreams;
import com.google.common.primitives.Bytes;
import com.google.common.util.concurrent.Uninterruptibles;
-
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.JUnit4;
-
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -96,6 +91,9 @@ import java.util.Arrays;
import java.util.Iterator;
import java.util.Set;
import java.util.regex.PatternSyntaxException;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
/**
* Tests an in-memory file system through the public APIs in {@link Files}, etc. This also acts as
@@ -1385,6 +1383,20 @@ public class JimfsUnixLikeFileSystemTest extends AbstractJimfsIntegrationTest {
}
@Test
+ public void testOutputStream_withTruncateExistingAndNotWrite_truncatesFile() throws IOException {
+ // https://github.com/google/jimfs/pull/77
+ Path path = path("/test");
+ Files.write(path, new byte[] {1, 2, 3});
+ assertThatPath(path).containsBytes(1, 2, 3);
+
+ try (OutputStream out = Files.newOutputStream(path, CREATE, TRUNCATE_EXISTING)) {
+ out.write(new byte[] {1, 2});
+ }
+
+ assertThatPath(path).containsBytes(1, 2);
+ }
+
+ @Test
public void testChannels() throws IOException {
try (FileChannel channel = FileChannel.open(path("/test.txt"), CREATE_NEW, WRITE)) {
ByteBuffer buf1 = UTF_8.encode("hello");