diff options
Diffstat (limited to 'src/test/java/com/google/devtools/build/lib/sandbox/FakeSandboxfsProcess.java')
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/sandbox/FakeSandboxfsProcess.java | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/sandbox/FakeSandboxfsProcess.java b/src/test/java/com/google/devtools/build/lib/sandbox/FakeSandboxfsProcess.java new file mode 100644 index 0000000000..8a9c63b288 --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/sandbox/FakeSandboxfsProcess.java @@ -0,0 +1,111 @@ +// Copyright 2018 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.devtools.build.lib.sandbox; + +import static com.google.common.base.Preconditions.checkState; + +import com.google.devtools.build.lib.vfs.FileSystem; +import com.google.devtools.build.lib.vfs.FileSystemUtils; +import com.google.devtools.build.lib.vfs.Path; +import com.google.devtools.build.lib.vfs.PathFragment; +import java.io.IOException; +import java.util.List; + +/** + * A fake in-process sandboxfs implementation that uses symlinks on the Bazel file system API. + * + * <p>TODO(jmmv): It's possible that this could replace {@link SymlinkedSandboxedSpawn} altogether, + * simplifying all callers that need to perform a sandboxed spawn because they would all go through + * the sandboxfs worker interface. Evaluate this idea once we are confident enough that we won't + * just remove all sandboxfs support code. + */ +final class FakeSandboxfsProcess implements SandboxfsProcess { + + /** File system on which the fake sandboxfs instance operates. */ + private final FileSystem fileSystem; + + /** Directory on which the sandboxfs is serving. */ + private final PathFragment mountPoint; + + /** + * Whether this "process" is valid or not. Used to better represent the workflow of a real + * sandboxfs subprocess. + */ + private boolean alive = true; + + /** + * Initializes a new sandboxfs process instance. + * + * <p>To better represent reality, this ensures that the mount point is present and valid. + * + * @param fileSystem file system on which the fake sandboxfs instance operates + * @param mountPoint directory on which the sandboxfs instance is serving + * @throws IOException if the mount point is missing + */ + FakeSandboxfsProcess(FileSystem fileSystem, PathFragment mountPoint) throws IOException { + if (!fileSystem.getPath(mountPoint).exists()) { + throw new IOException("Mount point " + mountPoint + " does not exist"); + } else if (!fileSystem.getPath(mountPoint).isDirectory()) { + throw new IOException("Mount point " + mountPoint + " is not a directory"); + } + + this.fileSystem = fileSystem; + this.mountPoint = mountPoint; + } + + @Override + public Path getMountPoint() { + return fileSystem.getPath(mountPoint); + } + + @Override + public synchronized boolean isAlive() { + return alive; + } + + @Override + public synchronized void destroy() { + alive = false; + } + + @Override + public synchronized void map(List<Mapping> mappings) throws IOException { + checkState(alive, "Cannot be called after destroy()"); + + for (Mapping mapping : mappings) { + checkState(mapping.path().isAbsolute(), "Mapping specifications are expected to be absolute" + + " but %s is not", mapping.path()); + Path link = fileSystem.getPath(mountPoint).getRelative(mapping.path().toRelative()); + link.getParentDirectory().createDirectoryAndParents(); + + if (!fileSystem.getPath(mapping.target()).exists()) { + // Not a requirement for the creation of a symbolic link but this reflects the behavior of + // the real sandboxfs. + throw new IOException("Target " + mapping.target() + " does not exist"); + } + + link.createSymbolicLink(fileSystem.getPath(mapping.target())); + } + } + + @Override + public synchronized void unmap(PathFragment mapping) throws IOException { + checkState(alive, "Cannot be called after destroy()"); + + checkState(mapping.isAbsolute(), "Mapping specifications are expected to be absolute" + + " but %s is not", mapping); + FileSystemUtils.deleteTree(fileSystem.getPath(mountPoint).getRelative(mapping.toRelative())); + } +} |