Skip to content

Commit

Permalink
atomicallyWriteTo: remove temporary file prior to creating it
Browse files Browse the repository at this point in the history
This change is the same in spirit to bazelbuild#19241. When using Bazel in
combination with bb_clientd, bb_clientd may restore a copy of bazel-out/
from a snapshot. Files restored from the snapshot are not mutable, as
they may be backed by other snapshots, a remote CAS, etc. etc. etc..

This change extends atomicallyWriteTo() to always write contents into a
new file that is guaranteed to be writable. The logic that's added here
is merely copy-pasted from what's already present at the bottom of the
same function.
  • Loading branch information
EdSchouten committed Aug 14, 2023
1 parent 7d911b5 commit d412613
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public byte[] atomicallyWriteRelativeTo(Path execRoot, String uniqueSuffix) thro
protected byte[] atomicallyWriteTo(Path outputPath, String uniqueSuffix) throws IOException {
Path tmpPath = outputPath.getFileSystem().getPath(outputPath.getPathString() + uniqueSuffix);
tmpPath.getParentDirectory().createDirectoryAndParents();
try {
tmpPath.delete();
} catch (IOException e) {
// Ignore.
}
try {
byte[] digest = writeTo(tmpPath);
// We expect the following to replace the params file atomically in case we are using
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ public void atomicallyWriteVirtualInput_writesBinToolsFile() throws Exception {
public void atomicallyWriteVirtualInput_writesArbitraryVirtualInput() throws Exception {
VirtualActionInput input = ActionsTestUtil.createVirtualActionInput("file", "hello");

// Store an existing directory at the location where atomicallyWriteTo()
// writes its temporary file. It should be removed prior to the creation of
// the temporary file.
scratch.resolve("/outputs/file-1234").createDirectoryAndParents();

input.atomicallyWriteRelativeTo(scratch.resolve("/outputs"), "-1234");

assertThat(scratch.resolve("/outputs").readdir(Symlinks.NOFOLLOW))
Expand Down

0 comments on commit d412613

Please sign in to comment.