use of com.google.devtools.build.lib.vfs.Path in project bazel by bazelbuild.
the class HardlinkedExecRoot method linkInputs.
/**
* Make all specified inputs available in the sandbox.
*
* <p>We want the sandboxed process to have access only to these input files and not anything else
* from the workspace. Furthermore, the process should not be able to modify these input files. We
* achieve this by hardlinking all input files into a temporary "inputs" directory, then
* symlinking them into their correct place inside the sandbox.
*
* <p>The hardlinks / symlinks combination (as opposed to simply directly hardlinking to the final
* destination) is necessary, because we build a solib symlink tree for shared libraries where the
* original file and the created symlink have two different file names (libblaze_util.so vs.
* src_Stest_Scpp_Sblaze_Uutil_Utest.so) and our cc_wrapper.sh needs to be able to figure out both
* names (by following solib symlinks back) to modify the paths to the shared libraries in
* cc_binaries.
*/
private void linkInputs(Map<PathFragment, Path> inputs, Set<Path> createdDirs) throws IOException {
// Create directory for input files.
Path inputsDir = sandboxPath.getRelative("inputs");
if (!inputsDir.exists()) {
inputsDir.createDirectory();
}
for (ImmutableMap.Entry<PathFragment, Path> entry : inputs.entrySet()) {
Path targetName = sandboxExecRoot.getRelative(entry.getKey());
FileSystemUtils.createDirectoryAndParentsWithCache(createdDirs, targetName.getParentDirectory());
// The target is supposed to be an empty file.
if (entry.getValue() == null) {
FileSystemUtils.createEmptyFile(targetName);
continue;
}
// Hardlink, resolve symlink here instead in finalizeLinks.
Path target = entry.getValue().resolveSymbolicLinks();
Path hardlinkName = target.startsWith(execRoot) ? inputsDir.getRelative(target.relativeTo(execRoot)) : inputsDir.getRelative(entry.getKey());
if (errWriter != null) {
errWriter.printf("hardlink: %s -> %s\n", hardlinkName, target);
}
try {
createHardLink(hardlinkName, target);
} catch (IOException e) {
// Creating a hardlink might fail when the input file and the sandbox directory are not on
// the same filesystem / device. Then we use symlink instead.
hardlinkName.createSymbolicLink(target);
}
// symlink
if (errWriter != null) {
errWriter.printf("symlink: %s -> %s\n", targetName, hardlinkName);
}
targetName.createSymbolicLink(hardlinkName);
}
}
use of com.google.devtools.build.lib.vfs.Path in project bazel by bazelbuild.
the class HardlinkedExecRoot method copyOutputs.
@Override
public void copyOutputs(Path execRoot, Collection<PathFragment> outputs) throws IOException {
for (PathFragment output : outputs) {
Path source = sandboxExecRoot.getRelative(output);
Path target = execRoot.getRelative(output);
if (source.isFile() || source.isSymbolicLink()) {
Files.move(source.getPathFile(), target.getPathFile());
} else if (source.isDirectory()) {
try {
source.renameTo(target);
} catch (IOException e) {
// Failed to move directory directly, thus move it recursively.
target.createDirectory();
FileSystemUtils.moveTreesBelow(source, target);
}
}
}
}
use of com.google.devtools.build.lib.vfs.Path in project bazel by bazelbuild.
the class HardlinkedExecRoot method createFileSystem.
@Override
public void createFileSystem(Map<PathFragment, Path> inputs, Collection<PathFragment> outputs, Set<Path> writableDirs) throws IOException {
Set<Path> createdDirs = new HashSet<>();
FileSystemUtils.createDirectoryAndParentsWithCache(createdDirs, sandboxExecRoot);
createDirectoriesForOutputs(outputs, createdDirs);
// Create all needed directories.
for (Path createDir : writableDirs) {
if (errWriter != null) {
errWriter.printf("createdir: %s\n", createDir.getPathString());
}
FileSystemUtils.createDirectoryAndParentsWithCache(createdDirs, createDir);
}
// Link all the inputs.
linkInputs(inputs, createdDirs);
}
use of com.google.devtools.build.lib.vfs.Path in project bazel by bazelbuild.
the class HardlinkedExecRoot method createHardLink.
private void createHardLink(Path target, Path source) throws IOException {
java.nio.file.Path targetNio = java.nio.file.Paths.get(target.toString());
java.nio.file.Path sourceNio = java.nio.file.Paths.get(source.toString());
if (!source.exists() || target.exists()) {
return;
}
// Regular file
if (source.isFile()) {
Path parentDir = target.getParentDirectory();
if (!parentDir.exists()) {
FileSystemUtils.createDirectoryAndParents(parentDir);
}
java.nio.file.Files.createLink(targetNio, sourceNio);
// Directory
} else if (source.isDirectory()) {
// Eagerly create target directory in case source is an empty directory.
FileSystemUtils.createDirectoryAndParents(target);
Collection<Path> subpaths = source.getDirectoryEntries();
for (Path sourceSubpath : subpaths) {
Path targetSubpath = target.getRelative(sourceSubpath.relativeTo(source));
createHardLink(targetSubpath, sourceSubpath);
}
}
}
use of com.google.devtools.build.lib.vfs.Path in project bazel by bazelbuild.
the class LinuxSandboxRunner method isSupported.
static boolean isSupported(CommandEnvironment commandEnv) {
Path execRoot = commandEnv.getExecRoot();
PathFragment embeddedTool = commandEnv.getBlazeWorkspace().getBinTools().getExecPath(LINUX_SANDBOX);
if (embeddedTool == null) {
// bootstrapping).
return false;
}
List<String> args = new ArrayList<>();
args.add(execRoot.getRelative(embeddedTool).getPathString());
args.add("-C");
ImmutableMap<String, String> env = ImmutableMap.of();
File cwd = execRoot.getPathFile();
Command cmd = new Command(args.toArray(new String[0]), env, cwd);
try {
cmd.execute(/* stdin */
new byte[] {}, Command.NO_OBSERVER, ByteStreams.nullOutputStream(), ByteStreams.nullOutputStream(), /* killSubprocessOnInterrupt */
true);
} catch (CommandException e) {
return false;
}
return true;
}
Aggregations