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 #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.

Closes #19243.

PiperOrigin-RevId: 571276901
Change-Id: I3963ad6218e1557271c2bb4de94e89538ff512af
  • Loading branch information
EdSchouten authored and copybara-github committed Oct 6, 2023
1 parent cf38460 commit 423a368
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ 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();
tmpPath.delete();
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 423a368

Please sign in to comment.