com.google.devtools.build.lib.vfs.Path.getParentDirectory() - java examples

Here are the examples of the java api com.google.devtools.build.lib.vfs.Path.getParentDirectory() taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

80 Examples 7

19 View Complete Implementation : RemoteCache.java
Copyright Apache License 2.0
Author : bazelbuild
private static Path toTmpDownloadPath(Path actualPath) {
    return actualPath.getParentDirectory().getRelative(actualPath.getBaseName() + ".tmp");
}

19 View Complete Implementation : InMemoryFileSystem.java
Copyright Apache License 2.0
Author : bazelbuild
/**
 * Helper method for stat and inodeStat: return the path's (no symlink-followed) stat.
 */
private synchronized InodeOrErrno noFollowStatErrno(Path path) {
    InodeOrErrno dirInfoOrError = getDirectoryErrno(path.getParentDirectory());
    if (dirInfoOrError.hasError()) {
        return dirInfoOrError;
    }
    return directoryLookupErrno(dirInfoOrError.inode(), baseNameOrWindowsDrive(path), /*create=*/
    false, path);
}

19 View Complete Implementation : ArtifactFunctionTest.java
Copyright Apache License 2.0
Author : bazelbuild
private void file(Path path, String contents) throws Exception {
    FileSystemUtils.createDirectoryAndParents(path.getParentDirectory());
    writeFile(path, contents);
}

18 View Complete Implementation : BuildViewTestBase.java
Copyright Apache License 2.0
Author : bazelbuild
protected void runTestDepOnGoodTargetInBadPkgAndTransitiveCycle(boolean incremental) throws Exception {
    reporter.removeHandler(failFastHandler);
    scratch.file("parent/BUILD", "sh_library(name = 'foo',", "           srcs = ['//badpkg:okay-target', '//okaypkg:transitively-a-cycle'])");
    Path symlinkcycleBuildFile = scratch.file("symlinkcycle/BUILD", "sh_library(name = 'cycle', srcs = glob(['*.sh']))");
    Path dirPath = symlinkcycleBuildFile.getParentDirectory();
    dirPath.getRelative("foo.sh").createSymbolicLink(PathFragment.create("foo.sh"));
    scratch.file("okaypkg/BUILD", "sh_library(name = 'transitively-a-cycle',", "           srcs = ['//symlinkcycle:cycle'])");
    Path badpkgBuildFile = scratch.file("badpkg/BUILD", "exports_files(['okay-target'])", "fail()");
    if (incremental) {
        update(defaultFlags().with(Flag.KEEP_GOING), "//okaypkg:transitively-a-cycle");
        replacedertContainsEvent("circular symlinks detected");
        eventCollector.clear();
    }
    update(defaultFlags().with(Flag.KEEP_GOING), "//parent:foo");
    replacedertThat(getFrequencyOfErrorsWithLocation(badpkgBuildFile.asFragment(), eventCollector)).isEqualTo(1);
    // TODO(nharmata): This test currently only works because each BuildViewTest#update call
    // dirties all FileNodes that are in error. There is actually a skyframe bug with cycle
    // reporting on incremental builds (see b/14622820).
    replacedertContainsEvent("circular symlinks detected");
}

18 View Complete Implementation : RemoteCache.java
Copyright Apache License 2.0
Author : bazelbuild
/**
 * Download a file (that is not a directory). The content is fetched from the digest.
 */
public ListenableFuture<Void> downloadFile(Path path, Digest digest) throws IOException {
    Preconditions.checkNotNull(path.getParentDirectory()).createDirectoryAndParents();
    if (digest.getSizeBytes() == 0) {
        // Handle empty file locally.
        FileSystemUtils.writeContent(path, new byte[0]);
        return COMPLETED_SUCCESS;
    }
    OutputStream out = new LazyFileOutputStream(path);
    SettableFuture<Void> outerF = SettableFuture.create();
    ListenableFuture<Void> f = cacheProtocol.downloadBlob(digest, out);
    Futures.addCallback(f, new FutureCallback<Void>() {

        @Override
        public void onSuccess(Void result) {
            try {
                out.close();
                outerF.set(null);
            } catch (IOException e) {
                outerF.setException(e);
            }
        }

        @Override
        public void onFailure(Throwable t) {
            try {
                out.close();
            } catch (IOException e) {
                if (t != e) {
                    t.addSuppressed(e);
                }
            } finally {
                outerF.setException(t);
            }
        }
    }, directExecutor());
    return outerF;
}

18 View Complete Implementation : FilesetEntryFunctionTest.java
Copyright Apache License 2.0
Author : bazelbuild
private void createFile(Path path, String... contents) throws Exception {
    if (!path.getParentDirectory().exists()) {
        scratch.dir(path.getParentDirectory().getPathString());
    }
    scratch.file(path.getPathString(), contents);
}

18 View Complete Implementation : IOExceptionsTest.java
Copyright Apache License 2.0
Author : bazelbuild
@Test
public void testOneLevelUpFailure() throws Exception {
    // expect errors
    reporter.removeHandler(failFastHandler);
    final Path buildPath = scratch.file("top/BUILD", "sh_library(name = 'x')");
    buildPath.getParentDirectory().getRelative("pkg").createDirectory();
    crashMessage = new Function<Path, String>() {

        @Override
        public String apply(Path path) {
            if (buildPath.equals(path)) {
                return "custom crash: " + buildPath;
            }
            return null;
        }
    };
    replacedertThat(visitTransitively(Label.parseAbsolute("//top/pkg:x", ImmutableMap.of()))).isFalse();
}

18 View Complete Implementation : SkylarkRepositoryContext.java
Copyright Apache License 2.0
Author : bazelbuild
// Create parent directories for the given path
private void makeDirectories(Path path) throws IOException {
    Path parent = path.getParentDirectory();
    if (parent != null) {
        parent.createDirectoryAndParents();
    }
}

18 View Complete Implementation : ArtifactFunctionTest.java
Copyright Apache License 2.0
Author : bazelbuild
private TreeFileArtifact createFakeTreeFileArtifact(SpecialArtifact treeArtifact, String parentRelativePath, String content) throws Exception {
    TreeFileArtifact treeFileArtifact = ActionsTestUtil.createTreeFileArtifactWithNoGeneratingAction(treeArtifact, parentRelativePath);
    Path path = treeFileArtifact.getPath();
    FileSystemUtils.createDirectoryAndParents(path.getParentDirectory());
    writeFile(path, content);
    return treeFileArtifact;
}

18 View Complete Implementation : BuildIntegrationTestCase.java
Copyright Apache License 2.0
Author : bazelbuild
/**
 * Creates folders on the path to {@code relativeLinkPath} and a symlink to {@code target} at
 * {@code relativeLinkPath} (equivalent to {@code ln -s <target> <relativeLinkPath>}).
 */
protected void createSymlink(String target, String relativeLinkPath) throws IOException {
    Path path = getWorkspace().getRelative(relativeLinkPath);
    path.getParentDirectory().createDirectoryAndParents();
    path.createSymbolicLink(PathFragment.create(target));
}

17 View Complete Implementation : PersistentMap.java
Copyright Apache License 2.0
Author : bazelbuild
/**
 * Creates the specified file and returns the DataOuputStream suitable for writing entries.
 *
 * @param mapFile the file the map is written to.
 * @return the DataOutputStream that was can be used for saving the map to the file.
 * @throws IOException
 */
private DataOutputStream createMapFile(Path mapFile) throws IOException {
    FileSystemUtils.createDirectoryAndParents(mapFile.getParentDirectory());
    DataOutputStream out = new DataOutputStream(new BufferedOutputStream(mapFile.getOutputStream()));
    out.writeLong(MAGIC);
    out.writeLong(version);
    return out;
}

17 View Complete Implementation : SkyframeQueryHelper.java
Copyright Apache License 2.0
Author : bazelbuild
@Override
public void overwriteFile(String fileName, String... lines) throws IOException {
    Path file = rootDirectory.getRelative(fileName);
    file.getParentDirectory().createDirectoryAndParents();
    FileSystemUtils.writeContentAsLatin1(file, Joiner.on('\n').join(lines));
}

17 View Complete Implementation : SkyframeQueryHelper.java
Copyright Apache License 2.0
Author : bazelbuild
@Override
public void ensureSymbolicLink(String link, String target) throws IOException {
    Path linkPath = rootDirectory.getRelative(link);
    Path targetPath = rootDirectory.getRelative(target);
    linkPath.getParentDirectory().createDirectoryAndParents();
    FileSystemUtils.ensureSymbolicLink(linkPath, targetPath);
}

17 View Complete Implementation : InMemoryFileSystem.java
Copyright Apache License 2.0
Author : bazelbuild
@Override
public boolean delete(Path path) throws IOException {
    if (isRootDirectory(path)) {
        throw Error.EBUSY.exception(path);
    }
    synchronized (this) {
        if (!exists(path, /*followSymlinks=*/
        false)) {
            return false;
        }
        InMemoryDirectoryInfo parent = getDirectory(path.getParentDirectory());
        InMemoryContentInfo child = parent.getChild(baseNameOrWindowsDrive(path));
        if (child.isDirectory() && child.getSize() > 2) {
            throw Error.ENOTEMPTY.exception(path);
        }
        unlink(parent, baseNameOrWindowsDrive(path), path);
        return true;
    }
}

17 View Complete Implementation : CompactPersistentActionCache.java
Copyright Apache License 2.0
Author : bazelbuild
/**
 * Rename corrupted files so they could be replacedyzed later. This would also ensure
 * that next initialization attempt will create empty cache.
 */
private static void renameCorruptedFiles(Path cacheRoot) {
    try {
        for (Path path : UnixGlob.forPath(cacheRoot).addPattern("action_*_v" + VERSION + ".*").glob()) {
            path.renameTo(path.getParentDirectory().getChild(path.getBaseName() + ".bad"));
        }
        for (Path path : UnixGlob.forPath(cacheRoot).addPattern("filename_*_v" + VERSION + ".*").glob()) {
            path.renameTo(path.getParentDirectory().getChild(path.getBaseName() + ".bad"));
        }
    } catch (IOException e) {
    // do nothing
    }
}

17 View Complete Implementation : AbstractPackageLoaderTest.java
Copyright Apache License 2.0
Author : bazelbuild
protected Path file(Path path, String... contents) throws Exception {
    path.getParentDirectory().createDirectoryAndParents();
    FileSystemUtils.writeContentAsLatin1(path, Joiner.on("\n").join(contents));
    return path;
}

17 View Complete Implementation : SkyframeQueryHelper.java
Copyright Apache License 2.0
Author : bazelbuild
@Override
public void writeFile(String fileName, String... lines) throws IOException {
    Path file = rootDirectory.getRelative(fileName);
    if (file.exists()) {
        throw new IOException("Could not create scratch file (file exists) " + fileName);
    }
    file.getParentDirectory().createDirectoryAndParents();
    FileSystemUtils.writeContentAsLatin1(file, Joiner.on('\n').join(lines));
}

17 View Complete Implementation : ArtifactFunctionTestCase.java
Copyright Apache License 2.0
Author : bazelbuild
protected static void writeFile(Path path, String contents) throws IOException {
    FileSystemUtils.createDirectoryAndParents(path.getParentDirectory());
    FileSystemUtils.writeContentAsLatin1(path, contents);
}

17 View Complete Implementation : FsApparatus.java
Copyright Apache License 2.0
Author : bazelbuild
/**
 * Creates a scratch file in the scratch filesystem with the given {@code pathName} with
 * {@code lines} being its content. The method returns a Path instance for the scratch file.
 */
public Path file(String pathName, String... lines) throws IOException {
    Path file = path(pathName);
    Path parentDir = file.getParentDirectory();
    if (!parentDir.exists()) {
        FileSystemUtils.createDirectoryAndParents(parentDir);
    }
    if (file.exists()) {
        throw new IOException("Could not create scratch file (file exists) " + file);
    }
    String fileContent = StringUtilities.joinLines(lines);
    FileSystemUtils.writeContentAsLatin1(file, fileContent);
    return file;
}

17 View Complete Implementation : SpawnIncludeScanner.java
Copyright Apache License 2.0
Author : bazelbuild
/**
 * Extracts and returns inclusions from "file" using a spawn.
 */
public Collection<Inclusion> extractInclusions(Artifact file, ActionExecutionMetadata actionExecutionMetadata, ActionExecutionContext actionExecutionContext, Artifact grepIncludes, GrepIncludesFileType fileType, boolean isOutputFile) throws IOException, ExecException, InterruptedException {
    boolean placeNextToFile = isOutputFile && !file.hasParent();
    Path output = getIncludesOutput(file, actionExecutionContext.getPathResolver(), fileType, placeNextToFile);
    if (!inMemoryOutput) {
        AbstractAction.deleteOutput(output, placeNextToFile ? file.getRoot() : null);
        if (!placeNextToFile) {
            output.getParentDirectory().createDirectoryAndParents();
        }
    }
    InputStream dotIncludeStream = spawnGrep(file, execPath(output), inMemoryOutput, // We use {@link GrepIncludesAction} primarily to overwrite {@link Action#getMnemonic}.
    // You might be tempted to use a custom mnemonic on the Spawn instead, but rest replacedured
    // that _this does not work_. We call Spawn.getResourceOwner().getMnemonic() in a lot of
    // places, some of which are downstream from here, and doing so would cause the Spawn
    // and its owning ActionExecutionMetadata to be inconsistent with each other.
    new GrepIncludesAction(actionExecutionMetadata, file.getExecPath()), actionExecutionContext, grepIncludes, fileType);
    return IncludeParser.processIncludes(output, dotIncludeStream == null ? output.getInputStream() : dotIncludeStream);
}

17 View Complete Implementation : WorkspaceASTFunctionTest.java
Copyright Apache License 2.0
Author : bazelbuild
private RootedPath createWorkspaceFile(String... contents) throws IOException {
    Path workspacePath = scratch.overwriteFile("WORKSPACE", contents);
    fakeWorkspaceFileValue.setSize(workspacePath.getFileSize());
    return RootedPath.toRootedPath(Root.fromPath(workspacePath.getParentDirectory()), PathFragment.create(workspacePath.getBaseName()));
}

17 View Complete Implementation : SkylarkPath.java
Copyright Apache License 2.0
Author : bazelbuild
@Override
public SkylarkPath getDirname() {
    Path parentPath = path.getParentDirectory();
    return parentPath == null ? null : new SkylarkPath(parentPath);
}

17 View Complete Implementation : FileWriteActionTestCase.java
Copyright Apache License 2.0
Author : bazelbuild
@Before
public final void createAction() throws Exception {
    outputArtifact = getBinArtifactWithNoOwner("destination.txt");
    output = outputArtifact.getPath();
    FileSystemUtils.createDirectoryAndParents(output.getParentDirectory());
    action = createAction(NULL_ACTION_OWNER, outputArtifact, "Hello World", false);
}

17 View Complete Implementation : RemoteSpawnRunner.java
Copyright Apache License 2.0
Author : bazelbuild
private void maybeWriteParamFilesLocally(Spawn spawn) throws IOException {
    if (!executionOptions.shouldMaterializeParamFiles()) {
        return;
    }
    for (ActionInput actionInput : spawn.getInputFiles().toList()) {
        if (actionInput instanceof ParamFileActionInput) {
            ParamFileActionInput paramFileActionInput = (ParamFileActionInput) actionInput;
            Path outputPath = execRoot.getRelative(paramFileActionInput.getExecPath());
            if (outputPath.exists()) {
                outputPath.delete();
            }
            outputPath.getParentDirectory().createDirectoryAndParents();
            try (OutputStream out = outputPath.getOutputStream()) {
                paramFileActionInput.writeTo(out);
            }
        }
    }
}

17 View Complete Implementation : RepositoryDelegatorFunction.java
Copyright Apache License 2.0
Author : bazelbuild
private void setupRepositoryRoot(Path repoRoot) throws RepositoryFunctionException {
    try {
        repoRoot.deleteTree();
        Preconditions.checkNotNull(repoRoot.getParentDirectory()).createDirectoryAndParents();
    } catch (IOException e) {
        throw new RepositoryFunctionException(e, Transience.TRANSIENT);
    }
}

16 View Complete Implementation : SandboxfsSandboxedSpawnTest.java
Copyright Apache License 2.0
Author : bazelbuild
public void testSymlinks(boolean mapSymlinkTargets) throws Exception {
    Path input1 = workspaceDir.getRelative("dir1/input-1.txt");
    input1.getParentDirectory().createDirectory();
    FileSystemUtils.createEmptyFile(input1);
    Path input2 = workspaceDir.getRelative("dir1/input-2.txt");
    input2.getParentDirectory().createDirectory();
    FileSystemUtils.createEmptyFile(input2);
    Path linkToInput1 = workspaceDir.getRelative("dir2/link-to-input-1");
    linkToInput1.getParentDirectory().createDirectory();
    linkToInput1.createSymbolicLink(PathFragment.create("../dir1/input-1.txt"));
    replacedertThat(linkToInput1.readSymbolicLink().isAbsolute()).isFalse();
    Path linkToInput2 = workspaceDir.getRelative("dir2/link-to-input-2");
    linkToInput2.getParentDirectory().createDirectory();
    linkToInput2.createSymbolicLink(PathFragment.create("../dir1/input-2.txt"));
    replacedertThat(linkToInput2.readSymbolicLink().isAbsolute()).isFalse();
    Path linkToLink = workspaceDir.getRelative("dir2/link-to-link");
    linkToLink.getParentDirectory().createDirectory();
    linkToLink.createSymbolicLink(PathFragment.create("link-to-input-2"));
    replacedertThat(linkToLink.readSymbolicLink().isAbsolute()).isFalse();
    Path linkToAbsolutePath = workspaceDir.getRelative("dir2/link-to-absolute-path");
    linkToAbsolutePath.getParentDirectory().createDirectory();
    Path randomPath = workspaceDir.getRelative("/some-random-path");
    FileSystemUtils.createEmptyFile(randomPath);
    linkToAbsolutePath.createSymbolicLink(randomPath.asFragment());
    replacedertThat(linkToAbsolutePath.readSymbolicLink().isAbsolute()).isTrue();
    SandboxedSpawn spawn = new SandboxfsSandboxedSpawn(sandboxfs, outerDir, ImmutableList.of("/bin/true"), ImmutableMap.of(), new SandboxInputs(ImmutableMap.of(PathFragment.create("dir1/input-1.txt"), input1, // input2 and linkToInput2 intentionally left unmapped to verify they are mapped
    // as symlink targets of linktoLink.
    PathFragment.create("such/link-1.txt"), linkToInput1, PathFragment.create("such/link-to-link.txt"), linkToLink, PathFragment.create("such/abs-link.txt"), linkToAbsolutePath), ImmutableMap.of()), SandboxOutputs.create(ImmutableSet.of(PathFragment.create("very/output.txt")), ImmutableSet.of()), ImmutableSet.of(), mapSymlinkTargets, new SynchronousTreeDeleter(), /* statisticsPath= */
    null);
    spawn.createFileSystem();
    Path execRoot = spawn.getSandboxExecRoot();
    // Relative symlinks must be kept as such in the sandbox and they must resolve properly.
    replacedertThat(execRoot.getRelative("such/link-1.txt").readSymbolicLink()).isEqualTo(PathFragment.create("../dir1/input-1.txt"));
    replacedertThat(execRoot.getRelative("such/link-1.txt").resolveSymbolicLinks()).isEqualTo(input1);
    replacedertThat(execRoot.getRelative("such/link-to-link.txt").readSymbolicLink()).isEqualTo(PathFragment.create("link-to-input-2"));
    if (mapSymlinkTargets) {
        replacedertThat(execRoot.getRelative("such/link-to-link.txt").resolveSymbolicLinks()).isEqualTo(input2);
        replacedertThat(execRoot.getRelative("such/link-to-input-2").readSymbolicLink()).isEqualTo(PathFragment.create("../dir1/input-2.txt"));
        replacedertThat(execRoot.getRelative("such/link-to-input-2").resolveSymbolicLinks()).isEqualTo(input2);
    } else {
        replacedertThrows("Symlink resolution worked, which means the target was mapped when not expected", IOException.clreplaced, () -> execRoot.getRelative("such/link-to-link.txt").resolveSymbolicLinks());
    }
    // Targets of symlinks must have been mapped inside the sandbox only when requested.
    replacedertThat(execRoot.getRelative("dir1/input-1.txt").exists()).isTrue();
    if (mapSymlinkTargets) {
        replacedertThat(execRoot.getRelative("dir1/input-2.txt").exists()).isTrue();
    } else {
        replacedertThat(execRoot.getRelative("dir1/input-2.txt").exists()).isFalse();
    }
    // Absolute symlinks must be kept as such in the sandbox no matter where they point to.
    replacedertThat(execRoot.getRelative("such/abs-link.txt").isSymbolicLink()).isTrue();
    replacedertThat(execRoot.getRelative("such/abs-link.txt").readSymbolicLinkUnchecked()).isEqualTo(randomPath.asFragment());
}

16 View Complete Implementation : RemoteSpawnRunnerTest.java
Copyright Apache License 2.0
Author : bazelbuild
@Before
public final void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    digestUtil = new DigestUtil(DigestHashFunction.SHA256);
    FileSystem fs = new InMemoryFileSystem(new JavaClock(), DigestHashFunction.SHA256);
    execRoot = fs.getPath("/exec/root");
    logDir = fs.getPath("/server-logs");
    FileSystemUtils.createDirectoryAndParents(execRoot);
    fakeFileCache = new FakeActionInputFileCache(execRoot);
    Path stdout = fs.getPath("/tmp/stdout");
    Path stderr = fs.getPath("/tmp/stderr");
    FileSystemUtils.createDirectoryAndParents(stdout.getParentDirectory());
    FileSystemUtils.createDirectoryAndParents(stderr.getParentDirectory());
    outErr = new FileOutErr(stdout, stderr);
    remoteOptions = Options.getDefaults(RemoteOptions.clreplaced);
}

16 View Complete Implementation : FileArtifactValueTest.java
Copyright Apache License 2.0
Author : bazelbuild
private Path scratchFile(String name, long mtime, String content) throws IOException {
    Path path = fs.getPath(name);
    path.getParentDirectory().createDirectoryAndParents();
    FileSystemUtils.writeContentAsLatin1(path, content);
    path.setLastModifiedTime(mtime);
    return path;
}

16 View Complete Implementation : FilesystemValueCheckerTest.java
Copyright Apache License 2.0
Author : bazelbuild
@Test
public void testFileWithIOExceptionNotConsideredDirty() throws Exception {
    Path path = fs.getPath("/testroot/foo");
    path.getParentDirectory().createDirectory();
    path.createSymbolicLink(PathFragment.create("bar"));
    fs.readlinkThrowsIoException = true;
    SkyKey fileKey = FileStateValue.key(RootedPath.toRootedPath(Root.fromPath(pkgRoot), PathFragment.create("foo")));
    EvaluationResult<SkyValue> result = driver.evaluate(ImmutableList.of(fileKey), EVALUATION_OPTIONS);
    replacedertThat(result.hasError()).isTrue();
    fs.readlinkThrowsIoException = false;
    FilesystemValueChecker checker = new FilesystemValueChecker(null, null);
    Diff diff = getDirtyFilesystemKeys(evaluator, checker);
    replacedertThat(diff.changedKeysWithoutNewValues()).isEmpty();
    replacedertThat(diff.changedKeysWithNewValues()).isEmpty();
}

16 View Complete Implementation : PathCasingLookupFunctionTest.java
Copyright Apache License 2.0
Author : bazelbuild
private void createFile(RootedPath p) throws IOException {
    Path path = p.asPath();
    if (!path.getParentDirectory().exists()) {
        scratch.dir(path.getParentDirectory().getPathString());
    }
    scratch.file(path.getPathString());
    invalidateFileAndParents(p);
}

16 View Complete Implementation : BlazeTestUtils.java
Copyright Apache License 2.0
Author : bazelbuild
/**
 * Creates an empty file, along with all its parent directories.
 */
public static void makeEmptyFile(Path path) throws IOException {
    FileSystemUtils.createDirectoryAndParents(path.getParentDirectory());
    FileSystemUtils.createEmptyFile(path);
}

16 View Complete Implementation : Scratch.java
Copyright Apache License 2.0
Author : bazelbuild
/**
 * Creates a new scratch file, ensuring parents exist.
 */
private Path newFile(String pathName) throws IOException {
    Path file = resolve(pathName);
    Path parentDir = file.getParentDirectory();
    if (!parentDir.exists()) {
        FileSystemUtils.createDirectoryAndParents(parentDir);
    }
    if (file.exists()) {
        throw new IOException("Could not create scratch file (file exists) " + pathName);
    }
    return file;
}

16 View Complete Implementation : ParallelBuilderTest.java
Copyright Apache License 2.0
Author : bazelbuild
private Artifact createInputFile(String name) throws IOException {
    Artifact artifact = createSourceArtifact(name);
    Path path = artifact.getPath();
    FileSystemUtils.createDirectoryAndParents(path.getParentDirectory());
    FileSystemUtils.createEmptyFile(path);
    return artifact;
}

16 View Complete Implementation : DependencySetTest.java
Copyright Apache License 2.0
Author : bazelbuild
@Test
public void writeSet() throws Exception {
    Path file1 = fileSystem.getPath("/usr/local/blah/blah/genhello/hello.cc");
    Path file2 = fileSystem.getPath("/usr/local/blah/blah/genhello/hello.h");
    Path file3 = fileSystem.getPath("/usr/local/blah/blah/genhello/other.h");
    String filename = "/usr/local/blah/blah/genhello/hello.o";
    DependencySet depSet1 = newDependencySet();
    depSet1.addDependencies(ImmutableList.of(file1, file2, file3));
    depSet1.setOutputFileName(filename);
    Path outfile = scratch.resolve(filename);
    Path dotd = scratch.resolve("/usr/local/blah/blah/genhello/hello.d");
    FileSystemUtils.createDirectoryAndParents(dotd.getParentDirectory());
    depSet1.write(outfile, ".d");
    String dotdContents = new String(FileSystemUtils.readContentAsLatin1(dotd));
    String expected = "usr/local/blah/blah/genhello/hello.o:  \\\n" + "  /usr/local/blah/blah/genhello/hello.cc \\\n" + "  /usr/local/blah/blah/genhello/hello.h \\\n" + "  /usr/local/blah/blah/genhello/other.h\n";
    replacedertThat(dotdContents).isEqualTo(expected);
    replacedertThat(depSet1.getOutputFileName()).isEqualTo(filename);
}

16 View Complete Implementation : DependencySetTest.java
Copyright Apache License 2.0
Author : bazelbuild
@Test
public void writeReadSet() throws Exception {
    String filename = "/usr/local/blah/blah/genhello/hello.d";
    Path file1 = fileSystem.getPath("/usr/local/blah/blah/genhello/hello.cc");
    Path file2 = fileSystem.getPath("/usr/local/blah/blah/genhello/hello.h");
    Path file3 = fileSystem.getPath("/usr/local/blah/blah/genhello/other.h");
    DependencySet depSet1 = newDependencySet();
    depSet1.addDependencies(ImmutableList.of(file1, file2, file3));
    depSet1.setOutputFileName(filename);
    Path dotd = scratch.resolve(filename);
    FileSystemUtils.createDirectoryAndParents(dotd.getParentDirectory());
    depSet1.write(dotd, ".d");
    DependencySet depSet2 = newDependencySet().read(dotd);
    replacedertThat(depSet2).isEqualTo(depSet1);
    // due to how pic.d files are written, absolute paths are changed into relatives
    replacedertThat("/" + depSet2.getOutputFileName()).isEqualTo(depSet1.getOutputFileName());
}

16 View Complete Implementation : BlazeOptionHandler.java
Copyright Apache License 2.0
Author : bazelbuild
/**
 * Only some commands work if cwd != workspaceSuffix in Blaze. In that case, also check if Blaze
 * was called from the output directory and fail if it was.
 */
private ExitCode checkCwdInWorkspace(EventHandler eventHandler) {
    if (!commandAnnotation.mustRunInWorkspace()) {
        return ExitCode.SUCCESS;
    }
    if (!workspace.getDirectories().inWorkspace()) {
        eventHandler.handle(Event.error("The '" + commandAnnotation.name() + "' command is only supported from within a workspace" + " (below a directory having a WORKSPACE file).\n" + "See doreplacedentation at" + " https://docs.bazel.build/versions/master/build-ref.html#workspace"));
        return ExitCode.COMMAND_LINE_ERROR;
    }
    Path workspacePath = workspace.getWorkspace();
    // TODO(kchodorow): Remove this once spaces are supported.
    if (workspacePath.getPathString().contains(" ")) {
        eventHandler.handle(Event.error(runtime.getProductName() + " does not currently work properly from paths " + "containing spaces (" + workspace + ")."));
        return ExitCode.LOCAL_ENVIRONMENTAL_ERROR;
    }
    if (workspacePath.getParentDirectory() != null) {
        Path doNotBuild = workspacePath.getParentDirectory().getRelative(BlazeWorkspace.DO_NOT_BUILD_FILE_NAME);
        if (doNotBuild.exists()) {
            if (!commandAnnotation.canRunInOutputDirectory()) {
                eventHandler.handle(Event.error(getNotInRealWorkspaceError(doNotBuild)));
                return ExitCode.COMMAND_LINE_ERROR;
            } else {
                eventHandler.handle(Event.warn(runtime.getProductName() + " is run from output directory. This is unsound."));
            }
        }
    }
    return ExitCode.SUCCESS;
}

16 View Complete Implementation : BlazeWorkspace.java
Copyright Apache License 2.0
Author : bazelbuild
private void writeDoNotBuildHereFile(Path filePath) {
    try {
        FileSystemUtils.createDirectoryAndParents(filePath.getParentDirectory());
        FileSystemUtils.writeContent(filePath, ISO_8859_1, getWorkspace().toString());
    } catch (IOException e) {
        logger.warning("Couldn't write to '" + filePath + "': " + e.getMessage());
    }
}

16 View Complete Implementation : FileFunction.java
Copyright Apache License 2.0
Author : bazelbuild
/**
 * Returns the symlink target and file state of {@code rootedPath}'s symlink to {@code
 * symlinkTarget}, accounting for ancestor symlinks, or {@code null} if there was a missing dep.
 */
@Nullable
private PartialResolutionResult getSymlinkTargetRootedPath(RootedPath rootedPath, PathFragment symlinkTarget, TreeSet<Path> sortedLogicalChain, ArrayList<RootedPath> logicalChain, Environment env) throws FileFunctionException, InterruptedException {
    Path path = rootedPath.asPath();
    Path symlinkTargetPath;
    if (symlinkTarget.isAbsolute()) {
        symlinkTargetPath = path.getRelative(symlinkTarget);
    } else {
        Path parentPath = path.getParentDirectory();
        symlinkTargetPath = parentPath != null ? parentPath.getRelative(symlinkTarget) : path.getRelative(symlinkTarget);
    }
    RootedPath symlinkTargetRootedPath = toRootedPath(symlinkTargetPath);
    checkPathSeenDuringPartialResolution(symlinkTargetRootedPath, sortedLogicalChain, logicalChain, env);
    if (env.valuesMissing()) {
        return null;
    }
    // The symlink target could have a different parent directory, which itself could be a directory
    // symlink (or have an ancestor directory symlink)!
    return resolveFromAncestors(symlinkTargetRootedPath, sortedLogicalChain, logicalChain, env);
}

16 View Complete Implementation : InMemoryFileSystem.java
Copyright Apache License 2.0
Author : bazelbuild
@Override
protected void createFSDependentHardLink(Path linkPath, Path originalPath) throws IOException {
    // Same check used when creating a symbolic link
    if (isRootDirectory(originalPath)) {
        throw Error.EACCES.exception(originalPath);
    }
    synchronized (this) {
        InMemoryDirectoryInfo linkParent = getDirectory(linkPath.getParentDirectory());
        // Same check used when creating a symbolic link
        if (linkParent.getChild(baseNameOrWindowsDrive(linkPath)) != null) {
            throw Error.EEXIST.exception(linkPath);
        }
        insert(linkParent, baseNameOrWindowsDrive(linkPath), getDirectory(originalPath.getParentDirectory()).getChild(baseNameOrWindowsDrive(originalPath)), linkPath);
    }
}

16 View Complete Implementation : InMemoryFileSystem.java
Copyright Apache License 2.0
Author : bazelbuild
@Override
public void renameTo(Path sourcePath, Path targetPath) throws IOException {
    if (isRootDirectory(sourcePath)) {
        throw Error.EACCES.exception(sourcePath);
    }
    if (isRootDirectory(targetPath)) {
        throw Error.EACCES.exception(targetPath);
    }
    synchronized (this) {
        InMemoryDirectoryInfo sourceParent = getDirectory(sourcePath.getParentDirectory());
        InMemoryDirectoryInfo targetParent = getDirectory(targetPath.getParentDirectory());
        InMemoryContentInfo sourceInode = sourceParent.getChild(baseNameOrWindowsDrive(sourcePath));
        if (sourceInode == null) {
            throw Error.ENOENT.exception(sourcePath);
        }
        InMemoryContentInfo targetInode = targetParent.getChild(baseNameOrWindowsDrive(targetPath));
        unlink(sourceParent, baseNameOrWindowsDrive(sourcePath), sourcePath);
        try {
            // TODO(bazel-team): (2009) test with symbolic links.
            // Precondition checks:
            if (targetInode != null) {
                // already exists
                if (targetInode.isDirectory()) {
                    if (!sourceInode.isDirectory()) {
                        throw new IOException(sourcePath + " -> " + targetPath + " (" + Error.EISDIR + ")");
                    }
                    if (targetInode.getSize() > 2) {
                        throw Error.ENOTEMPTY.exception(targetPath);
                    }
                } else if (sourceInode.isDirectory()) {
                    throw new IOException(sourcePath + " -> " + targetPath + " (" + Error.ENOTDIR + ")");
                }
                unlink(targetParent, baseNameOrWindowsDrive(targetPath), targetPath);
            }
            sourceInode.movedTo(targetPath);
            insert(targetParent, baseNameOrWindowsDrive(targetPath), sourceInode, targetPath);
            return;
        } catch (IOException e) {
            sourceInode.movedTo(sourcePath);
            insert(sourceParent, baseNameOrWindowsDrive(sourcePath), sourceInode, // restore source
            sourcePath);
            throw e;
        }
    }
}

16 View Complete Implementation : PackageFactoryTest.java
Copyright Apache License 2.0
Author : bazelbuild
@Test
public void testTransientErrorsInGlobbing() throws Exception {
    events.setFailFast(false);
    Path buildFile = scratch.file("/e/BUILD", "sh_library(name = 'e', data = glob(['*.txt']))");
    Path parentDir = buildFile.getParentDirectory();
    scratch.file("/e/data.txt");
    throwOnReaddir = parentDir;
    replacedertThrows(NoSuchPackageException.clreplaced, () -> packages.createPackage("e", RootedPath.toRootedPath(root, buildFile)));
    events.setFailFast(true);
    throwOnReaddir = null;
    Package pkg = packages.createPackage("e", RootedPath.toRootedPath(root, buildFile));
    replacedertThat(pkg.containsErrors()).isFalse();
    replacedertThat(pkg.getRule("e")).isNotNull();
    List<?> globList = (List) pkg.getRule("e").getAttributeContainer().getAttr("data");
    replacedertThat(globList).containsExactly(Label.parseAbsolute("//e:data.txt", ImmutableMap.of()));
}

15 View Complete Implementation : RepositoryCache.java
Copyright Apache License 2.0
Author : bazelbuild
/**
 * Copy or hardlink cached value to a specified directory, if it exists.
 *
 * <p>We're using hardlinking instead of symlinking because symlinking require weird checks to
 * verify that the symlink still points to an existing artifact. e.g. cleaning up the central
 * cache but not the workspace cache.
 *
 * @param cacheKey The string key to cache the value by.
 * @param targetPath The path where the cache value should be copied to.
 * @param keyType The type of key used. See: KeyType
 * @param canonicalId If set to a non-empty string, restrict cache hits to those cases, where the
 *     entry with the given cacheKey was added with this String given.
 * @return The Path value where the cache value has been copied to. If cache value does not exist,
 *     return null.
 * @throws IOException
 */
@Nullable
public synchronized Path get(String cacheKey, Path targetPath, KeyType keyType, String canonicalId) throws IOException, InterruptedException {
    if (Thread.interrupted()) {
        throw new InterruptedException();
    }
    Preconditions.checkState(isEnabled());
    replacedertKeyIsValid(cacheKey, keyType);
    if (!exists(cacheKey, keyType)) {
        return null;
    }
    Path cacheEntry = keyType.getCachePath(contentAddressablePath).getRelative(cacheKey);
    Path cacheValue = cacheEntry.getRelative(DEFAULT_CACHE_FILENAME);
    try {
        replacedertFileChecksum(cacheKey, cacheValue, keyType);
    } catch (IOException e) {
        // New lines because this error message gets large printing multiple absolute filepaths.
        throw new IOException(e.getMessage() + "\n\n" + "Please delete the directory " + cacheEntry + " and try again.");
    }
    if (!Strings.isNullOrEmpty(canonicalId)) {
        if (!hasCanonicalId(cacheKey, keyType, canonicalId)) {
            return null;
        }
    }
    FileSystemUtils.createDirectoryAndParents(targetPath.getParentDirectory());
    if (useHardlinks) {
        FileSystemUtils.createHardLink(targetPath, cacheValue);
    } else {
        FileSystemUtils.copyFile(cacheValue, targetPath);
    }
    try {
        FileSystemUtils.touchFile(cacheValue);
    } catch (IOException e) {
    // Ignore, because the cache might be on a read-only volume.
    }
    return targetPath;
}

15 View Complete Implementation : SpawnIncludeScanner.java
Copyright Apache License 2.0
Author : bazelbuild
/**
 * Extracts and returns inclusions from "file" using a spawn.
 */
public ListenableFuture<Collection<Inclusion>> extractInclusionsAsync(Executor executor, Artifact file, ActionExecutionMetadata actionExecutionMetadata, ActionExecutionContext actionExecutionContext, Artifact grepIncludes, GrepIncludesFileType fileType, boolean isOutputFile) throws IOException {
    boolean placeNextToFile = isOutputFile && !file.hasParent();
    Path output = getIncludesOutput(file, actionExecutionContext.getPathResolver(), fileType, placeNextToFile);
    if (!inMemoryOutput) {
        AbstractAction.deleteOutput(output, placeNextToFile ? file.getRoot() : null);
        if (!placeNextToFile) {
            output.getParentDirectory().createDirectoryAndParents();
        }
    }
    ListenableFuture<InputStream> dotIncludeStreamFuture = spawnGrepAsync(executor, file, execPath(output), inMemoryOutput, // We use {@link GrepIncludesAction} primarily to overwrite {@link Action#getMnemonic}.
    // You might be tempted to use a custom mnemonic on the Spawn instead, but rest replacedured
    // that _this does not work_. We call Spawn.getResourceOwner().getMnemonic() in a lot of
    // places, some of which are downstream from here, and doing so would cause the Spawn
    // and its owning ActionExecutionMetadata to be inconsistent with each other.
    new GrepIncludesAction(actionExecutionMetadata, file.getExecPath()), actionExecutionContext, grepIncludes, fileType);
    return Futures.transform(dotIncludeStreamFuture, (stream) -> {
        try {
            return IncludeParser.processIncludes(output, stream);
        } catch (IOException e) {
            throw new IORuntimeException(e);
        }
    }, MoreExecutors.directExecutor());
}

15 View Complete Implementation : InMemoryFileSystem.java
Copyright Apache License 2.0
Author : bazelbuild
@Override
protected void createSymbolicLink(Path path, PathFragment targetFragment) throws IOException {
    if (isRootDirectory(path)) {
        throw Error.EACCES.exception(path);
    }
    synchronized (this) {
        InMemoryDirectoryInfo parent = getDirectory(path.getParentDirectory());
        if (parent.getChild(baseNameOrWindowsDrive(path)) != null) {
            throw Error.EEXIST.exception(path);
        }
        insert(parent, baseNameOrWindowsDrive(path), new InMemoryLinkInfo(clock, targetFragment), path);
    }
}

15 View Complete Implementation : SymlinkActionTest.java
Copyright Apache License 2.0
Author : bazelbuild
@Before
public final void setUp() throws Exception {
    input = scratch.file("input.txt", "Hello, world.");
    inputArtifact = getSourceArtifact("input.txt");
    Path linkedInput = directories.getExecRoot(TestConstants.WORKSPACE_NAME).getRelative("input.txt");
    FileSystemUtils.createDirectoryAndParents(linkedInput.getParentDirectory());
    linkedInput.createSymbolicLink(input);
    outputArtifact = getBinArtifactWithNoOwner("destination.txt");
    outputArtifact.setGeneratingActionKey(ActionsTestUtil.NULL_ACTION_LOOKUP_DATA);
    output = outputArtifact.getPath();
    FileSystemUtils.createDirectoryAndParents(output.getParentDirectory());
    action = SymlinkAction.toArtifact(NULL_ACTION_OWNER, inputArtifact, outputArtifact, "Symlinking test");
}

15 View Complete Implementation : PostAnalysisQueryHelper.java
Copyright Apache License 2.0
Author : bazelbuild
@Override
public void ensureSymbolicLink(String link, String target) throws IOException {
    Path rootDirectory = getRootDirectory();
    Path linkPath = rootDirectory.getRelative(link);
    Path targetPath = rootDirectory.getRelative(target);
    FileSystemUtils.createDirectoryAndParents(linkPath.getParentDirectory());
    FileSystemUtils.ensureSymbolicLink(linkPath, targetPath);
}

15 View Complete Implementation : FakeActionInputFileCache.java
Copyright Apache License 2.0
Author : bazelbuild
public Digest createScratchInput(ActionInput input, String content) throws IOException {
    Path inputFile = execRoot.getRelative(input.getExecPath());
    FileSystemUtils.createDirectoryAndParents(inputFile.getParentDirectory());
    FileSystemUtils.writeContentAsLatin1(inputFile, content);
    Digest digest = digestUtil.compute(inputFile);
    setDigest(input, digest.getHash());
    return digest;
}

15 View Complete Implementation : DirectoryTreeTest.java
Copyright Apache License 2.0
Author : bazelbuild
private Artifact addFile(String path, String content, SortedMap<PathFragment, ActionInput> sortedInputs, Map<ActionInput, FileArtifactValue> metadata) throws IOException {
    Path p = execRoot.getRelative(path);
    p.getParentDirectory().createDirectoryAndParents();
    FileSystemUtils.writeContentAsLatin1(p, content);
    Artifact a = ActionsTestUtil.createArtifact(artifactRoot, p);
    sortedInputs.put(PathFragment.create(path), a);
    metadata.put(a, FileArtifactValue.createForTesting(a));
    return a;
}

15 View Complete Implementation : RemoteSpawnCacheTest.java
Copyright Apache License 2.0
Author : bazelbuild
@Before
public final void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    fs = new InMemoryFileSystem(new JavaClock(), DigestHashFunction.SHA256);
    digestUtil = new DigestUtil(DigestHashFunction.SHA256);
    execRoot = fs.getPath("/exec/root");
    FileSystemUtils.createDirectoryAndParents(execRoot);
    fakeFileCache = new FakeActionInputFileCache(execRoot);
    simpleSpawn = simpleSpawnWithExecutionInfo(ImmutableMap.of());
    Path stdout = fs.getPath("/tmp/stdout");
    Path stderr = fs.getPath("/tmp/stderr");
    FileSystemUtils.createDirectoryAndParents(stdout.getParentDirectory());
    FileSystemUtils.createDirectoryAndParents(stderr.getParentDirectory());
    outErr = new FileOutErr(stdout, stderr);
    RemoteOptions options = Options.getDefaults(RemoteOptions.clreplaced);
    reporter = new Reporter(new EventBus());
    eventHandler = new StoredEventHandler();
    reporter.addHandler(eventHandler);
    cache = remoteSpawnCacheWithOptions(options);
    fakeFileCache.createScratchInput(simpleSpawn.getInputFiles().getSingleton(), "xyz");
}

15 View Complete Implementation : ByteStreamServer.java
Copyright Apache License 2.0
Author : bazelbuild
@Override
public StreamObserver<WriteRequest> write(final StreamObserver<WriteResponse> responseObserver) {
    Path temp = workPath.getRelative("upload").getRelative(UUID.randomUUID().toString());
    try {
        FileSystemUtils.createDirectoryAndParents(temp.getParentDirectory());
        FileSystemUtils.createEmptyFile(temp);
    } catch (IOException e) {
        logger.log(SEVERE, "Failed to create temporary file for upload", e);
        responseObserver.onError(StatusUtils.internalError(e));
        // We need to make sure that subsequent onNext or onCompleted calls don't make any further
        // calls on the responseObserver after the onError above, so we return a no-op observer.
        return new NoOpStreamObserver<>();
    }
    return new StreamObserver<WriteRequest>() {

        private Digest digest;

        private long offset;

        private String resourceName;

        private boolean closed;

        @Override
        public void onNext(WriteRequest request) {
            if (closed) {
                return;
            }
            if (digest == null) {
                resourceName = request.getResourceName();
                digest = parseDigestFromResourceName(resourceName);
            }
            if (digest == null) {
                responseObserver.onError(StatusUtils.invalidArgumentError("resource_name", "Failed parsing digest from resource_name: " + request.getResourceName()));
                closed = true;
                return;
            }
            if (offset == 0) {
                if (cache.containsKey(digest)) {
                    responseObserver.onNext(WriteResponse.newBuilder().setCommittedSize(digest.getSizeBytes()).build());
                    responseObserver.onCompleted();
                    closed = true;
                    return;
                }
            }
            if (request.getWriteOffset() != offset) {
                responseObserver.onError(StatusUtils.invalidArgumentError("write_offset", "Expected: " + offset + ", received: " + request.getWriteOffset()));
                closed = true;
                return;
            }
            if (!request.getResourceName().isEmpty() && !request.getResourceName().equals(resourceName)) {
                responseObserver.onError(StatusUtils.invalidArgumentError("resource_name", "Expected: " + resourceName + ", received: " + request.getResourceName()));
                closed = true;
                return;
            }
            long size = request.getData().size();
            if (size > 0) {
                try (OutputStream out = temp.getOutputStream(true)) {
                    request.getData().writeTo(out);
                } catch (IOException e) {
                    responseObserver.onError(StatusUtils.internalError(e));
                    closed = true;
                    return;
                }
                offset += size;
            }
            boolean shouldFinishWrite = offset == digest.getSizeBytes();
            if (shouldFinishWrite != request.getFinishWrite()) {
                responseObserver.onError(StatusUtils.invalidArgumentError("finish_write", "Expected:" + shouldFinishWrite + ", received: " + request.getFinishWrite()));
                closed = true;
                return;
            }
        }

        @Override
        public void onError(Throwable t) {
            if (Status.fromThrowable(t).getCode() != Status.Code.CANCELLED) {
                logger.log(WARNING, "Write request failed remotely.", t);
            }
            closed = true;
            try {
                temp.delete();
            } catch (IOException e) {
                logger.log(WARNING, "Could not delete temp file.", e);
            }
        }

        @Override
        public void onCompleted() {
            if (closed) {
                return;
            }
            if (digest == null || offset != digest.getSizeBytes()) {
                responseObserver.onError(StatusProto.toStatusRuntimeException(com.google.rpc.Status.newBuilder().setCode(Status.Code.FAILED_PRECONDITION.value()).setMessage("Request completed before all data was sent.").build()));
                closed = true;
                return;
            }
            try {
                Digest d = digestUtil.compute(temp);
                getFromFuture(cache.uploadFile(d, temp));
                try {
                    temp.delete();
                } catch (IOException e) {
                    logger.log(WARNING, "Could not delete temp file.", e);
                }
                if (!d.equals(digest)) {
                    responseObserver.onError(StatusUtils.invalidArgumentError("resource_name", "Received digest " + digest + " does not match computed digest " + d));
                    closed = true;
                    return;
                }
                responseObserver.onNext(WriteResponse.newBuilder().setCommittedSize(offset).build());
                responseObserver.onCompleted();
            } catch (Exception e) {
                logger.log(WARNING, "Write request failed.", e);
                responseObserver.onError(StatusUtils.internalError(e));
                closed = true;
            }
        }
    };
}