Skip to content

Commit

Permalink
Update crashing input modification time to keep track of reproducibil…
Browse files Browse the repository at this point in the history
…ity checks.

PiperOrigin-RevId: 698753304
  • Loading branch information
hadi88 authored and copybara-github committed Nov 21, 2024
1 parent 8bd3142 commit cb56b52
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions centipede/centipede_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,8 @@ absl::flat_hash_set<std::string> PruneOldCrashesAndGetRemainingCrashMetadata(
.second;
if (!is_reproducible || is_duplicate) {
CHECK_OK(RemotePathDelete(crashing_input_file, /*recursively=*/false));
} else {
CHECK_OK(RemotePathTouchExistingFile(crashing_input_file));
}
}
return remaining_crash_metadata;
Expand Down
1 change: 1 addition & 0 deletions common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ cc_library(
":remote_file",
":test_util",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/time",
"@com_google_googletest//:gtest",
],
alwayslink = True,
Expand Down
3 changes: 3 additions & 0 deletions common/remote_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ absl::StatusOr<std::vector<std::string>> RemoteListFiles(std::string_view path,
// Renames `from` to `to`.
absl::Status RemotePathRename(std::string_view from, std::string_view to);

// Updates the last-modified time of `path` to the current time.
absl::Status RemotePathTouchExistingFile(std::string_view path);

// Deletes `path`. If `path` is a directory and `recursively` is true,
// recursively deletes all files and subdirectories within `path`.
absl::Status RemotePathDelete(std::string_view path, bool recursively);
Expand Down
20 changes: 20 additions & 0 deletions common/remote_file_oss.cc
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ absl::Status RemotePathRename(std::string_view from, std::string_view to) {
LOG(FATAL) << "Filesystem API not supported in iOS/MacOS";
}

absl::Status RemotePathTouchExistingFile(std::string_view path) {
LOG(FATAL) << "Filesystem API not supported in iOS/MacOS";
}

absl::Status RemotePathDelete(std::string_view path, bool recursively) {
LOG(FATAL) << "Filesystem API not supported in iOS/MacOS";
}
Expand Down Expand Up @@ -238,6 +242,22 @@ absl::Status RemotePathRename(std::string_view from, std::string_view to) {
return absl::OkStatus();
}

absl::Status RemotePathTouchExistingFile(std::string_view path) {
if (!RemotePathExists(path)) {
return absl::InvalidArgumentError(
absl::StrCat("path: ", std::string(path), " does not exist."));
}
std::error_code error;
std::filesystem::last_write_time(
path, std::filesystem::file_time_type::clock::now(), error);
if (error) {
return absl::UnknownError(absl::StrCat(
"filesystem::last_write_time() failed, path: ", std::string(path),
", error: ", error.message()));
}
return absl::OkStatus();
}

absl::Status RemotePathDelete(std::string_view path, bool recursively) {
std::error_code error;
if (recursively) {
Expand Down
21 changes: 21 additions & 0 deletions common/remote_file_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/log/check.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "./common/logging.h"
#include "./common/test_util.h"

Expand Down Expand Up @@ -142,5 +144,24 @@ TEST(RemotePathDelete, RecursivelyDeletesAllFilesAndSubdirectories) {
EXPECT_FALSE(fs::exists(a_b_c));
}

TEST(RemotePathTouchExistingFile, FailsWhenPathDoesNotExist) {
EXPECT_FALSE(RemotePathTouchExistingFile("/non/existent/path").ok());
}

TEST(RemotePathTouchExistingFile, UpdatesTheLastModifiedTime) {
const fs::path temp_dir = GetTestTempDir(test_info_->name());
const std::string file_path = temp_dir / "file";
CreateFileOrDie(file_path);

const auto start_time = std::filesystem::file_time_type::clock::now();
absl::SleepFor(absl::Milliseconds(1));
ASSERT_TRUE(RemotePathTouchExistingFile(file_path).ok());
const auto end_time = std::filesystem::file_time_type::clock::now();

const auto last_modified_time = fs::last_write_time(file_path);
ASSERT_LT(last_modified_time, end_time);
EXPECT_GT(last_modified_time, start_time);
}

} // namespace
} // namespace centipede

0 comments on commit cb56b52

Please sign in to comment.