aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/vfs/RootedPath.java
diff options
context:
space:
mode:
authorGravatar Laszlo Csomor <laszlocsomor@google.com>2016-10-25 13:15:55 +0000
committerGravatar John Cater <jcater@google.com>2016-10-25 20:18:14 +0000
commitca99bb71b8120e61a3bbde8e54f1a9488fa0478f (patch)
treeb1001b1bbae7123ef97ca8523f914dec145d5aad /src/main/java/com/google/devtools/build/lib/vfs/RootedPath.java
parent86a28b0fa2940377d5073071f79a81383fd7cfe3 (diff)
VFS: implement a Windows-specific Path subclass
This change rolls forward commit e0d7a540e3c615c628f63fcaaaba0c47fca2cb25 and commit 8bb4299b28de14eed9d3b57bcaeb9350c81c7db3, and adds a bugfix: - FileSystem.PathFactory got a new translatePath method that WindowsFileSystem.PathFactory overrides to translate absolute Unix paths to MSYS-relative paths - Path.getCachedChildPath calls this translatePath method so the child path is registered with the correct (translated) parent and under the correct name (e.g. "C:" instead of say "c") Below is the rest of the original change description: The new subclass WindowsFileSystem.WindowsPath is aware of Windows drives. This change: - introduces a new factory for Path objects so FileSystems can return a custom implementation that instantiates filesystem-specific Paths - implements the WindowsPath subclass of Path that is aware of Windows drives - introduces the bazel.windows_unix_root JVM argument that defines the MSYS root, which defines the absolute Windows path that is the root of all Unix paths that Bazel creates (e.g. "/usr/lib" -> "C:/tools/msys64/usr/lib") except if the path is of the form "/c/foo" which is treated as "C:/foo" - removes all Windows-specific logic from Path PathFragment is still aware of drive letters and it has to remain so because it is unaware of file systems. WindowsPath restricts the allowed path strings to absolute Unix paths where the first segment, if any, is a volume specifier. From now on if Bazel attempts to create a WindowsPath from an absolute Unix path, Bazel will make it relative to WindowsPath.UNIX_ROOT, unless the first component is a single-letter name (e.g. "/c/foo" which is "C:/foo"). Subclassing Path is necessary because a Unix-style absolute path doesn't sufficiently define a full Windows path, as it may be relative to any drive. Fixes https://github.com/bazelbuild/bazel/issues/1463 -- MOS_MIGRATED_REVID=137149483
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/vfs/RootedPath.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/vfs/RootedPath.java19
1 files changed, 16 insertions, 3 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/vfs/RootedPath.java b/src/main/java/com/google/devtools/build/lib/vfs/RootedPath.java
index a9825959a8..23e7ad79ed 100644
--- a/src/main/java/com/google/devtools/build/lib/vfs/RootedPath.java
+++ b/src/main/java/com/google/devtools/build/lib/vfs/RootedPath.java
@@ -14,7 +14,6 @@
package com.google.devtools.build.lib.vfs;
import com.google.devtools.build.lib.util.Preconditions;
-
import java.io.Serializable;
import java.util.Objects;
@@ -49,7 +48,21 @@ public class RootedPath implements Serializable {
* Returns a rooted path representing {@code relativePath} relative to {@code root}.
*/
public static RootedPath toRootedPath(Path root, PathFragment relativePath) {
- return new RootedPath(root, relativePath);
+ if (relativePath.isAbsolute()) {
+ if (root.isRootDirectory()) {
+ return new RootedPath(
+ root.getRelative(relativePath.windowsVolume()), relativePath.toRelative());
+ } else {
+ Preconditions.checkArgument(
+ relativePath.startsWith(root.asFragment()),
+ "relativePath '%s' is absolute, but it's not under root '%s'",
+ relativePath,
+ root);
+ return new RootedPath(root, relativePath.relativeTo(root.asFragment()));
+ }
+ } else {
+ return new RootedPath(root, relativePath);
+ }
}
/**
@@ -57,7 +70,7 @@ public class RootedPath implements Serializable {
*/
public static RootedPath toRootedPath(Path root, Path path) {
Preconditions.checkState(path.startsWith(root), "path: %s root: %s", path, root);
- return new RootedPath(root, path.relativeTo(root));
+ return toRootedPath(root, path.asFragment());
}
/**