aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/vfs
diff options
context:
space:
mode:
authorGravatar Taras Tsugrii <ttsugrii@fb.com>2017-10-30 08:07:37 -0400
committerGravatar John Cater <jcater@google.com>2017-10-30 10:42:02 -0400
commit7a3f1048e3c8f8fce57c3d873824511f15cb4aa0 (patch)
tree90ba8a9306a3c0e4eac1c1a00947ded37ce7d212 /src/main/java/com/google/devtools/build/lib/vfs
parentdc73a1d67e351acea7367f7aabb1dfa61c207b01 (diff)
[Trivial] Add a designated method for retrieving java.nio.file.Path
As explained in the javadoc this avoids unnecessary allocations and preserves underlying Java filesystem information. This partially addresses #3952. Closes #3953. PiperOrigin-RevId: 173877045
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/vfs')
-rw-r--r--src/main/java/com/google/devtools/build/lib/vfs/JavaIoFileSystem.java33
1 files changed, 23 insertions, 10 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/vfs/JavaIoFileSystem.java b/src/main/java/com/google/devtools/build/lib/vfs/JavaIoFileSystem.java
index c179589323..73074b2b90 100644
--- a/src/main/java/com/google/devtools/build/lib/vfs/JavaIoFileSystem.java
+++ b/src/main/java/com/google/devtools/build/lib/vfs/JavaIoFileSystem.java
@@ -24,6 +24,7 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
+import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Collection;
@@ -64,6 +65,18 @@ public class JavaIoFileSystem extends AbstractFileSystemWithCustomStat {
return new File(path.toString());
}
+ /**
+ * Returns a {@link java.nio.file.Path} representing the same path as provided {@code path}.
+ *
+ * <p>Note: while it's possible to use {@link #getIoFile(Path)} in combination with {@link
+ * File#toPath()} to achieve essentially the same, using this method is preferable because it
+ * avoids extra allocations and does not lose track of the underlying Java filesystem, which is
+ * useful for some in-memory filesystem implementations like JimFS.
+ */
+ protected java.nio.file.Path getNioPath(Path path) {
+ return Paths.get(path.toString());
+ }
+
private LinkOption[] linkOpts(boolean followSymlinks) {
return followSymlinks ? NO_LINK_OPTION : NOFOLLOW_LINKS_OPTION;
}
@@ -96,10 +109,10 @@ public class JavaIoFileSystem extends AbstractFileSystemWithCustomStat {
@Override
protected boolean exists(Path path, boolean followSymlinks) {
- File file = getIoFile(path);
+ java.nio.file.Path nioPath = getNioPath(path);
long startTime = Profiler.nanoTimeMaybe();
try {
- return Files.exists(file.toPath(), linkOpts(followSymlinks));
+ return Files.exists(nioPath, linkOpts(followSymlinks));
} finally {
profiler.logSimpleTask(startTime, ProfilerTask.VFS_STAT, path.toString());
}
@@ -261,9 +274,9 @@ public class JavaIoFileSystem extends AbstractFileSystemWithCustomStat {
@Override
protected void createSymbolicLink(Path linkPath, PathFragment targetFragment)
throws IOException {
- File file = getIoFile(linkPath);
+ java.nio.file.Path nioPath = getNioPath(linkPath);
try {
- Files.createSymbolicLink(file.toPath(), new File(targetFragment.getPathString()).toPath());
+ Files.createSymbolicLink(nioPath, Paths.get(targetFragment.getPathString()));
} catch (java.nio.file.FileAlreadyExistsException e) {
throw new IOException(linkPath + ERR_FILE_EXISTS);
} catch (java.nio.file.AccessDeniedException e) {
@@ -275,17 +288,17 @@ public class JavaIoFileSystem extends AbstractFileSystemWithCustomStat {
@Override
protected PathFragment readSymbolicLink(Path path) throws IOException {
- File file = getIoFile(path);
+ java.nio.file.Path nioPath = getNioPath(path);
long startTime = Profiler.nanoTimeMaybe();
try {
- String link = Files.readSymbolicLink(file.toPath()).toString();
+ String link = Files.readSymbolicLink(nioPath).toString();
return PathFragment.create(link);
} catch (java.nio.file.NotLinkException e) {
throw new NotASymlinkException(path);
} catch (java.nio.file.NoSuchFileException e) {
throw new FileNotFoundException(path + ERR_NO_SUCH_FILE_OR_DIR);
} finally {
- profiler.logSimpleTask(startTime, ProfilerTask.VFS_READLINK, file.getPath());
+ profiler.logSimpleTask(startTime, ProfilerTask.VFS_READLINK, nioPath);
}
}
@@ -400,11 +413,11 @@ public class JavaIoFileSystem extends AbstractFileSystemWithCustomStat {
*/
@Override
protected FileStatus stat(final Path path, final boolean followSymlinks) throws IOException {
- File file = getIoFile(path);
+ java.nio.file.Path nioPath = getNioPath(path);
final BasicFileAttributes attributes;
try {
- attributes = Files.readAttributes(
- file.toPath(), BasicFileAttributes.class, linkOpts(followSymlinks));
+ attributes =
+ Files.readAttributes(nioPath, BasicFileAttributes.class, linkOpts(followSymlinks));
} catch (java.nio.file.FileSystemException e) {
throw new FileNotFoundException(path + ERR_NO_SUCH_FILE_OR_DIR);
}