diff --git a/src/main/java/com/google/devtools/build/lib/sandbox/DarwinSandboxedStrategy.java b/src/main/java/com/google/devtools/build/lib/sandbox/DarwinSandboxedStrategy.java index 8c62dfe2e9db69..3d7d2ae41dbd3d 100644 --- a/src/main/java/com/google/devtools/build/lib/sandbox/DarwinSandboxedStrategy.java +++ b/src/main/java/com/google/devtools/build/lib/sandbox/DarwinSandboxedStrategy.java @@ -45,6 +45,7 @@ import com.google.devtools.build.lib.vfs.SearchPath; import com.google.devtools.build.lib.vfs.Symlinks; import java.io.IOException; +import java.io.PrintWriter; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -153,10 +154,20 @@ public void exec(Spawn spawn, ActionExecutionContext actionExecutionContext) SandboxHelpers.reportSubcommand(executor, spawn); SandboxHelpers.postActionStatusMessage(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"); + if (errWriter != null) { + errWriter.printf("sandbox root is %s\n", sandboxPath.toString()); + errWriter.printf("working dir is %s\n", sandboxExecRoot.toString()); + } + ImmutableMap spawnEnvironment = StandaloneSpawnStrategy.locallyDeterminedEnv(execRoot, productName, spawn.getEnvironment()); @@ -166,7 +177,11 @@ public void exec(Spawn spawn, ActionExecutionContext actionExecutionContext) try { HardlinkedExecRoot hardlinkedExecRoot = - new HardlinkedExecRoot(execRoot, sandboxPath, sandboxExecRoot); + new HardlinkedExecRoot(execRoot, sandboxPath, sandboxExecRoot, errWriter); + if (errWriter != null) { + errWriter.flush(); + } + ImmutableSet outputs = SandboxHelpers.getOutputFiles(spawn); hardlinkedExecRoot.createFileSystem( getMounts(spawn, actionExecutionContext), outputs, writableDirs); diff --git a/src/main/java/com/google/devtools/build/lib/sandbox/HardlinkedExecRoot.java b/src/main/java/com/google/devtools/build/lib/sandbox/HardlinkedExecRoot.java index 8aeabe3dbd0d39..0ce8c76bce218f 100644 --- a/src/main/java/com/google/devtools/build/lib/sandbox/HardlinkedExecRoot.java +++ b/src/main/java/com/google/devtools/build/lib/sandbox/HardlinkedExecRoot.java @@ -20,6 +20,7 @@ import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.build.lib.vfs.PathFragment; import java.io.IOException; +import java.io.PrintWriter; import java.util.Collection; import java.util.HashSet; import java.util.Map; @@ -34,11 +35,14 @@ public class HardlinkedExecRoot implements SandboxExecRoot { private final Path execRoot; private final Path sandboxPath; private final Path sandboxExecRoot; + private final PrintWriter errWriter; - public HardlinkedExecRoot(Path execRoot, Path sandboxPath, Path sandboxExecRoot) { + public HardlinkedExecRoot( + Path execRoot, Path sandboxPath, Path sandboxExecRoot, PrintWriter errWriter) { this.execRoot = execRoot; this.sandboxPath = sandboxPath; this.sandboxExecRoot = sandboxExecRoot; + this.errWriter = errWriter; } @Override @@ -51,6 +55,9 @@ public void createFileSystem( // Create all needed directories. for (Path createDir : writableDirs) { + if (errWriter != null) { + errWriter.printf("createdir: %s\n", createDir.getPathString()); + } FileSystemUtils.createDirectoryAndParents(createDir); } @@ -91,23 +98,29 @@ private void linkInputs(Map inputs) throws IOException { for (ImmutableMap.Entry entry : inputs.entrySet()) { // Hardlink, resolve symlink here instead in finalizeLinks. - Path source = entry.getValue().resolveSymbolicLinks(); - Path target = - source.startsWith(execRoot) - ? inputsDir.getRelative(source.relativeTo(execRoot)) + 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(target, source); + 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. - target.createSymbolicLink(source); + hardlinkName.createSymbolicLink(target); } // symlink - Path symlinkNewPath = sandboxExecRoot.getRelative(entry.getKey()); - FileSystemUtils.createDirectoryAndParents(symlinkNewPath.getParentDirectory()); - symlinkNewPath.createSymbolicLink(target); + Path symlinkName = sandboxExecRoot.getRelative(entry.getKey()); + if (errWriter != null) { + errWriter.printf("symlink: %s -> %s\n", symlinkName, hardlinkName); + } + FileSystemUtils.createDirectoryAndParents(symlinkName.getParentDirectory()); + symlinkName.createSymbolicLink(hardlinkName); } }