Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build-runfiles: remove temporary file prior to creating it #19241

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/tools/build-runfiles.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ class RunfilesCreator {

void ReadManifest(const std::string &manifest_file, bool allow_relative,
bool use_metadata) {
// Remove file left over from previous invocation. This ensures that
// opening succeeds if the existing file is read-only.
if (unlink(temp_filename_.c_str()) != 0 && errno != ENOENT) {
EdSchouten marked this conversation as resolved.
Show resolved Hide resolved
PDIE("removing temporary file at '%s/%s'", output_base_.c_str(),
temp_filename_.c_str());
}
FILE *outfile = fopen(temp_filename_.c_str(), "w");
if (!outfile) {
PDIE("opening '%s/%s' for writing", output_base_.c_str(),
Expand Down
31 changes: 31 additions & 0 deletions src/test/shell/integration/runfiles_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -474,4 +474,35 @@ EOF
assert_contains "${TEST_FOLDER_2}" "${MANIFEST_PATH}"
}

function test_removal_of_old_tempfiles() {
cat > BUILD << EOF
sh_binary(
name = "foo",
srcs = ["foo.sh"],
)
EOF
touch foo.sh
chmod +x foo.sh

# Build once to create a runfiles directory.
bazel build //:foo $EXTRA_BUILD_FLAGS >&$TEST_log || fail "build failed"

# Remove the MANIFEST file that was created by the previous build.
EdSchouten marked this conversation as resolved.
Show resolved Hide resolved
# Create an inaccessible file in the place where build-runfiles writes
# its temporary results.
#
# This simulates the case where the runfiles creation process is
# interrupted and leaves the temporary file behind. The temporary file
# may become read-only if it was stored in a snapshot.
rm ${PRODUCT_NAME}-bin/foo${EXT}.runfiles/MANIFEST
touch ${PRODUCT_NAME}-bin/foo${EXT}.runfiles/MANIFEST.tmp
chmod 0 ${PRODUCT_NAME}-bin/foo${EXT}.runfiles/MANIFEST.tmp

# Even with the inaccessible temporary file in place, build-runfiles
# should complete successfully. The MANIFEST file should be recreated.
bazel build //:foo $EXTRA_BUILD_FLAGS >&$TEST_log || fail "build failed"
[[ -f ${PRODUCT_NAME}-bin/foo${EXT}.runfiles/MANIFEST ]] \
|| fail "MANIFEST file not recreated"
}

run_suite "runfiles"
Loading