use of com.google.devtools.build.lib.vfs.Path in project bazel by bazelbuild.
the class DarwinSandboxedStrategy method create.
public static DarwinSandboxedStrategy create(BuildRequest buildRequest, Map<String, String> clientEnv, BlazeDirectories blazeDirs, boolean verboseFailures, String productName) throws IOException {
// On OS X, in addition to what is specified in $TMPDIR, two other temporary directories may be
// written to by processes. We have to get their location by calling "getconf".
List<String> confVars = ImmutableList.of("DARWIN_USER_TEMP_DIR", "DARWIN_USER_CACHE_DIR");
ImmutableList.Builder<Path> writablePaths = ImmutableList.builder();
for (String confVar : confVars) {
Path path = blazeDirs.getFileSystem().getPath(getConfStr(confVar));
if (path.exists()) {
writablePaths.add(path);
}
}
return new DarwinSandboxedStrategy(buildRequest, clientEnv, blazeDirs, verboseFailures, productName, writablePaths.build(), new SpawnHelpers(blazeDirs.getExecRoot()));
}
use of com.google.devtools.build.lib.vfs.Path in project bazel by bazelbuild.
the class DarwinSandboxedStrategy method finalizeLinks.
private Map<PathFragment, Path> finalizeLinks(Map<PathFragment, Path> unfinalized) throws IOException {
HashMap<PathFragment, Path> finalizedLinks = new HashMap<>();
for (Map.Entry<PathFragment, Path> mount : unfinalized.entrySet()) {
PathFragment target = mount.getKey();
Path source = mount.getValue();
// have to deal with finalizing the link.
if (source == null) {
finalizedLinks.put(target, source);
continue;
}
FileStatus stat = source.statNullable(Symlinks.NOFOLLOW);
if (stat != null && stat.isDirectory()) {
for (Path subSource : FileSystemUtils.traverseTree(source, Predicates.alwaysTrue())) {
PathFragment subTarget = target.getRelative(subSource.relativeTo(source));
finalizeLinksPath(finalizedLinks, subTarget, subSource, subSource.statNullable(Symlinks.NOFOLLOW));
}
} else {
finalizeLinksPath(finalizedLinks, target, source, stat);
}
}
return finalizedLinks;
}
use of com.google.devtools.build.lib.vfs.Path in project bazel by bazelbuild.
the class DarwinSandboxedStrategy method getRunUnderPath.
/**
* If a --run_under= option is set and refers to a command via its path (as opposed to via its
* label), we have to mount this. Note that this is best effort and works fine for shell scripts
* and small binaries, but we can't track any further dependencies of this command.
*
* <p>If --run_under= refers to a label, it is automatically provided in the spawn's input files,
* so mountInputs() will catch that case.
*/
private Path getRunUnderPath(Spawn spawn) {
if (spawn.getResourceOwner() instanceof TestRunnerAction) {
TestRunnerAction testRunnerAction = ((TestRunnerAction) spawn.getResourceOwner());
RunUnder runUnder = testRunnerAction.getExecutionSettings().getRunUnder();
if (runUnder != null && runUnder.getCommand() != null) {
PathFragment sourceFragment = new PathFragment(runUnder.getCommand());
Path mount;
if (sourceFragment.isAbsolute()) {
mount = blazeDirs.getFileSystem().getPath(sourceFragment);
} else if (blazeDirs.getExecRoot().getRelative(sourceFragment).exists()) {
mount = blazeDirs.getExecRoot().getRelative(sourceFragment);
} else {
List<Path> searchPath = SearchPath.parse(blazeDirs.getFileSystem(), clientEnv.get("PATH"));
mount = SearchPath.which(searchPath, runUnder.getCommand());
}
// only need to hardlink when under workspace
Path workspace = blazeDirs.getWorkspace();
if (mount != null && mount.startsWith(workspace)) {
return mount;
}
}
}
return null;
}
use of com.google.devtools.build.lib.vfs.Path in project bazel by bazelbuild.
the class DarwinSandboxedStrategy method actuallyExec.
private void actuallyExec(Spawn spawn, ActionExecutionContext actionExecutionContext, AtomicReference<Class<? extends SpawnActionContext>> writeOutputFiles) throws ExecException, InterruptedException {
Executor executor = actionExecutionContext.getExecutor();
SandboxHelpers.reportSubcommand(executor, spawn);
PrintWriter errWriter = sandboxDebug ? new PrintWriter(actionExecutionContext.getFileOutErr().getErrorStream()) : null;
// Each invocation of "exec" gets its own sandbox.
Path sandboxPath = SandboxHelpers.getSandboxRoot(blazeDirs, productName, uuid, execCounter);
Path sandboxExecRoot = sandboxPath.getRelative("execroot").getRelative(execRoot.getBaseName());
if (errWriter != null) {
errWriter.printf("sandbox root is %s\n", sandboxPath.toString());
errWriter.printf("working dir is %s\n", sandboxExecRoot.toString());
}
ImmutableMap<String, String> spawnEnvironment = StandaloneSpawnStrategy.locallyDeterminedEnv(execRoot, productName, spawn.getEnvironment());
Set<Path> writableDirs = getWritableDirs(sandboxExecRoot, spawn.getEnvironment());
Path runUnderPath = getRunUnderPath(spawn);
HardlinkedExecRoot hardlinkedExecRoot = new HardlinkedExecRoot(execRoot, sandboxPath, sandboxExecRoot, errWriter);
ImmutableSet<PathFragment> outputs = SandboxHelpers.getOutputFiles(spawn);
try {
hardlinkedExecRoot.createFileSystem(getMounts(spawn, actionExecutionContext), outputs, writableDirs);
} catch (IOException e) {
throw new UserExecException("Could not prepare sandbox directory", e);
}
// Flush our logs before executing the spawn, otherwise they might get overwritten.
if (errWriter != null) {
errWriter.flush();
}
DarwinSandboxRunner runner = new DarwinSandboxRunner(sandboxPath, sandboxExecRoot, getWritableDirs(sandboxExecRoot, spawnEnvironment), getInaccessiblePaths(), runUnderPath, verboseFailures);
try {
runSpawn(spawn, actionExecutionContext, spawnEnvironment, hardlinkedExecRoot, outputs, runner, writeOutputFiles);
} finally {
if (!sandboxDebug) {
try {
FileSystemUtils.deleteTree(sandboxPath);
} catch (IOException e) {
executor.getEventHandler().handle(Event.warn(String.format("Cannot delete sandbox directory after action execution: %s (%s)", sandboxPath.getPathString(), e)));
}
}
}
}
use of com.google.devtools.build.lib.vfs.Path in project bazel by bazelbuild.
the class DarwinSandboxedStrategy method getMounts.
@Override
public Map<PathFragment, Path> getMounts(Spawn spawn, ActionExecutionContext executionContext) throws ExecException {
try {
Map<PathFragment, Path> mounts = new HashMap<>();
spawnHelpers.mountInputs(mounts, spawn, executionContext);
Map<PathFragment, Path> unfinalized = new HashMap<>();
spawnHelpers.mountRunfilesFromSuppliers(unfinalized, spawn);
spawnHelpers.mountFilesFromFilesetManifests(unfinalized, spawn, executionContext);
mounts.putAll(finalizeLinks(unfinalized));
return mounts;
} catch (IllegalArgumentException | IOException e) {
throw new EnvironmentalExecException("Could not prepare mounts for sandbox execution", e);
}
}
Aggregations