aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/sandbox/SandboxedSpawn.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/sandbox/SandboxedSpawn.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/sandbox/SandboxedSpawn.java37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/sandbox/SandboxedSpawn.java b/src/main/java/com/google/devtools/build/lib/sandbox/SandboxedSpawn.java
index 431a4b1d43..e6408dc3cc 100644
--- a/src/main/java/com/google/devtools/build/lib/sandbox/SandboxedSpawn.java
+++ b/src/main/java/com/google/devtools/build/lib/sandbox/SandboxedSpawn.java
@@ -14,8 +14,12 @@
package com.google.devtools.build.lib.sandbox;
+import com.google.common.io.Files;
+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.Collection;
import java.util.List;
import java.util.Map;
@@ -56,4 +60,37 @@ interface SandboxedSpawn {
* Deletes the sandbox directory.
*/
void delete();
+
+ /**
+ * Moves all given outputs from a root to another.
+ *
+ * <p>This is a support function to help with the implementation of {@link #copyOutputs(Path)}.
+ *
+ * @param outputs outputs to move as relative paths to a root
+ * @param sourceRoot source directory from which to resolve outputs
+ * @param targetRoot target directory to which to move the resolved outputs from the source
+ * @throws IOException if any of the moves fails
+ */
+ static void moveOutputs(Collection<PathFragment> outputs, Path sourceRoot, Path targetRoot)
+ throws IOException {
+ for (PathFragment output : outputs) {
+ Path source = sourceRoot.getRelative(output);
+ Path target = targetRoot.getRelative(output);
+ if (source.isFile() || source.isSymbolicLink()) {
+ // Ensure the target directory exists in the target. The directories for the action outputs
+ // have already been created, but the spawn outputs may be different from the overall action
+ // outputs. This is the case for test actions.
+ target.getParentDirectory().createDirectoryAndParents();
+ 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);
+ }
+ }
+ }
+ }
}