aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/vfs/RootedPath.java
diff options
context:
space:
mode:
authorGravatar tomlu <tomlu@google.com>2018-01-17 14:36:26 -0800
committerGravatar Copybara-Service <copybara-piper@google.com>2018-01-17 14:37:59 -0800
commitee6a6862e26704854fb08bd90912890814cc3426 (patch)
tree294eac82202e393c5baae0e357325f2488ab3cbb /src/main/java/com/google/devtools/build/lib/vfs/RootedPath.java
parentf323fb3043bc782526e0e47933efedea9c5c2ad9 (diff)
Introduce Root class.
This class represents a root (such as a package path or an output root) used for file lookups and artifacts. It is meant to be as opaque as possible in order to hide the user's environment from sky keys and sky functions. Roots are used by RootedPaths and ArtifactRoots. This CL attempts to make the minimum number of modifications necessary to change RootedPath and ArtifactRoot to use these fields. Deprecated methods and invasive accessors are permitted to minimise the risk of any observable changes. RELNOTES: None PiperOrigin-RevId: 182271759
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.java60
1 files changed, 28 insertions, 32 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 a704b37b73..415e0ac80d 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
@@ -23,25 +23,24 @@ import java.io.Serializable;
import java.util.Objects;
/**
- * A {@link PathFragment} relative to a root, which is an absolute {@link Path}. Typically the root
- * will be a package path entry.
+ * A {@link PathFragment} relative to a {@link Root}. Typically the root will be a package path
+ * entry.
*
- * Two {@link RootedPath}s are considered equal iff they have equal roots and equal relative paths.
+ * <p>Two {@link RootedPath}s are considered equal iff they have equal roots and equal relative
+ * paths.
*
- * TODO(bazel-team): refactor Artifact to use this instead of Root.
- * TODO(bazel-team): use an opaque root representation so as to not expose the absolute path to
- * clients via #asPath or #getRoot.
+ * <p>TODO(bazel-team): refactor Artifact to use this instead of Root. TODO(bazel-team): use an
+ * opaque root representation so as to not expose the absolute path to clients via #asPath or
+ * #getRoot.
*/
public class RootedPath implements Serializable {
- private final Path root;
+ private final Root root;
private final PathFragment relativePath;
private final Path path;
- /**
- * Constructs a {@link RootedPath} from an absolute root path and a non-absolute relative path.
- */
- private RootedPath(Path root, PathFragment relativePath) {
+ /** Constructs a {@link RootedPath} from a {@link Root} and path fragment relative to the root. */
+ private RootedPath(Root root, PathFragment relativePath) {
Preconditions.checkState(!relativePath.isAbsolute(), "relativePath: %s root: %s", relativePath,
root);
this.root = root;
@@ -49,32 +48,29 @@ public class RootedPath implements Serializable {
this.path = root.getRelative(this.relativePath);
}
- /**
- * Returns a rooted path representing {@code relativePath} relative to {@code root}.
- */
- public static RootedPath toRootedPath(Path root, PathFragment relativePath) {
+ /** Returns a rooted path representing {@code relativePath} relative to {@code root}. */
+ public static RootedPath toRootedPath(Root root, PathFragment relativePath) {
if (relativePath.isAbsolute()) {
if (root.isRootDirectory()) {
return new RootedPath(
- root.getRelative(relativePath.windowsVolume()), relativePath.toRelative());
+ Root.fromPath(root.getRelative(relativePath.windowsVolume())),
+ relativePath.toRelative());
} else {
Preconditions.checkArgument(
- relativePath.startsWith(root.asFragment()),
+ root.contains(relativePath),
"relativePath '%s' is absolute, but it's not under root '%s'",
relativePath,
root);
- return new RootedPath(root, relativePath.relativeTo(root.asFragment()));
+ return new RootedPath(root, root.relativize(relativePath));
}
} else {
return new RootedPath(root, relativePath);
}
}
- /**
- * Returns a rooted path representing {@code path} under the root {@code root}.
- */
- public static RootedPath toRootedPath(Path root, Path path) {
- Preconditions.checkState(path.startsWith(root), "path: %s root: %s", path, root);
+ /** Returns a rooted path representing {@code path} under the root {@code root}. */
+ public static RootedPath toRootedPath(Root root, Path path) {
+ Preconditions.checkState(root.contains(path), "path: %s root: %s", path, root);
return toRootedPath(root, path.asFragment());
}
@@ -82,13 +78,13 @@ public class RootedPath implements Serializable {
* Returns a rooted path representing {@code path} under one of the package roots, or under the
* filesystem root if it's not under any package root.
*/
- public static RootedPath toRootedPathMaybeUnderRoot(Path path, Iterable<Path> packagePathRoots) {
- for (Path root : packagePathRoots) {
- if (path.startsWith(root)) {
+ public static RootedPath toRootedPathMaybeUnderRoot(Path path, Iterable<Root> packagePathRoots) {
+ for (Root root : packagePathRoots) {
+ if (root.contains(path)) {
return toRootedPath(root, path);
}
}
- return toRootedPath(path.getFileSystem().getRootDirectory(), path);
+ return toRootedPath(Root.fromFileSystemRoot(path.getFileSystem()), path);
}
public Path asPath() {
@@ -99,7 +95,7 @@ public class RootedPath implements Serializable {
return path;
}
- public Path getRoot() {
+ public Root getRoot() {
return root;
}
@@ -135,11 +131,11 @@ public class RootedPath implements Serializable {
/** Custom serialization for {@link RootedPath}s. */
public static class RootedPathCodec implements ObjectCodec<RootedPath> {
- private final PathCodec pathCodec;
+ private final ObjectCodec<Root> rootCodec;
/** Create an instance which will deserialize RootedPaths on {@code fileSystem}. */
public RootedPathCodec(FileSystem fileSystem) {
- this.pathCodec = new PathCodec(fileSystem);
+ this.rootCodec = Root.getCodec(new PathCodec(fileSystem));
}
@Override
@@ -150,14 +146,14 @@ public class RootedPath implements Serializable {
@Override
public void serialize(RootedPath rootedPath, CodedOutputStream codedOut)
throws IOException, SerializationException {
- pathCodec.serialize(rootedPath.getRoot(), codedOut);
+ rootCodec.serialize(rootedPath.getRoot(), codedOut);
PathFragment.CODEC.serialize(rootedPath.getRelativePath(), codedOut);
}
@Override
public RootedPath deserialize(CodedInputStream codedIn)
throws IOException, SerializationException {
- Path root = pathCodec.deserialize(codedIn);
+ Root root = rootCodec.deserialize(codedIn);
PathFragment relativePath = PathFragment.CODEC.deserialize(codedIn);
return toRootedPath(root, relativePath);
}