From cb56b52b9a33f0a49796281635d280de26a3d5b1 Mon Sep 17 00:00:00 2001 From: Hadi Ravanbakhsh Date: Thu, 21 Nov 2024 06:11:03 -0800 Subject: [PATCH] Update crashing input modification time to keep track of reproducibility checks. PiperOrigin-RevId: 698753304 --- centipede/centipede_interface.cc | 2 ++ common/BUILD | 1 + common/remote_file.h | 3 +++ common/remote_file_oss.cc | 20 ++++++++++++++++++++ common/remote_file_test.cc | 21 +++++++++++++++++++++ 5 files changed, 47 insertions(+) diff --git a/centipede/centipede_interface.cc b/centipede/centipede_interface.cc index aa3e2eda..d527b775 100644 --- a/centipede/centipede_interface.cc +++ b/centipede/centipede_interface.cc @@ -392,6 +392,8 @@ absl::flat_hash_set 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; diff --git a/common/BUILD b/common/BUILD index c13bda56..ed3c69a9 100644 --- a/common/BUILD +++ b/common/BUILD @@ -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, diff --git a/common/remote_file.h b/common/remote_file.h index 627109e2..b4ec099a 100644 --- a/common/remote_file.h +++ b/common/remote_file.h @@ -136,6 +136,9 @@ absl::StatusOr> 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); diff --git a/common/remote_file_oss.cc b/common/remote_file_oss.cc index bc511608..2ea73afe 100644 --- a/common/remote_file_oss.cc +++ b/common/remote_file_oss.cc @@ -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"; } @@ -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) { diff --git a/common/remote_file_test.cc b/common/remote_file_test.cc index d0bc7214..62694146 100644 --- a/common/remote_file_test.cc +++ b/common/remote_file_test.cc @@ -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" @@ -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