Search in sources :

Example 46 with Path

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);
    }
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) IOException(java.io.IOException) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 47 with Path

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);
            }
        }
    }
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) IOException(java.io.IOException)

Example 48 with Path

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);
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) HashSet(java.util.HashSet)

Example 49 with Path

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);
        }
    }
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) Collection(java.util.Collection)

Example 50 with Path

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;
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) Command(com.google.devtools.build.lib.shell.Command) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) ArrayList(java.util.ArrayList) CommandException(com.google.devtools.build.lib.shell.CommandException) File(java.io.File)

Aggregations

Path (com.google.devtools.build.lib.vfs.Path)492 Test (org.junit.Test)250 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)111 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)105 IOException (java.io.IOException)102 Artifact (com.google.devtools.build.lib.actions.Artifact)37 SkyKey (com.google.devtools.build.skyframe.SkyKey)37 ArrayList (java.util.ArrayList)29 SpecialArtifact (com.google.devtools.build.lib.actions.Artifact.SpecialArtifact)17 FileSystem (com.google.devtools.build.lib.vfs.FileSystem)17 InMemoryFileSystem (com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem)17 HashMap (java.util.HashMap)17 WindowsPath (com.google.devtools.build.lib.windows.WindowsFileSystem.WindowsPath)16 TreeFileArtifact (com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact)14 Before (org.junit.Before)14 Package (com.google.devtools.build.lib.packages.Package)13 FileStatus (com.google.devtools.build.lib.vfs.FileStatus)13 Map (java.util.Map)12 Nullable (javax.annotation.Nullable)12 Executor (com.google.devtools.build.lib.actions.Executor)10