From d10d746d7475f9492b34ffd61367dceed5dd3372 Mon Sep 17 00:00:00 2001 From: Sergey Shevchenko Date: Thu, 25 Jan 2024 12:30:44 -0800 Subject: [PATCH] #Centipede Add `RemoteFileGetSize()` API PiperOrigin-RevId: 601529743 --- centipede/BUILD | 1 + centipede/remote_file.cc | 10 ++++++++++ centipede/remote_file.h | 4 ++++ centipede/remote_file_test.cc | 22 +++++++++++++++++++++- 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/centipede/BUILD b/centipede/BUILD index d87f7d3d6..5c9d58d11 100644 --- a/centipede/BUILD +++ b/centipede/BUILD @@ -1235,6 +1235,7 @@ cc_test( deps = [ ":remote_file", ":test_util", + "@com_google_absl//absl/log:check", "@com_google_googletest//:gtest_main", ], ) diff --git a/centipede/remote_file.cc b/centipede/remote_file.cc index 80b324c88..48015a777 100644 --- a/centipede/remote_file.cc +++ b/centipede/remote_file.cc @@ -19,6 +19,7 @@ #include +#include #include #include // NOLINT #include @@ -138,6 +139,15 @@ ABSL_ATTRIBUTE_WEAK bool RemotePathExists(std::string_view path) { return std::filesystem::exists(path); } +ABSL_ATTRIBUTE_WEAK int64_t RemoteFileGetSize(std::string_view path) { + FILE *f = std::fopen(path.data(), "r"); + CHECK(f != nullptr) << VV(path); + std::fseek(f, 0, SEEK_END); + const auto sz = std::ftell(f); + std::fclose(f); + return sz; +} + namespace { int HandleGlobError(const char *epath, int eerrno) { diff --git a/centipede/remote_file.h b/centipede/remote_file.h index 1b4a59a75..19ca891cd 100644 --- a/centipede/remote_file.h +++ b/centipede/remote_file.h @@ -22,6 +22,7 @@ #ifndef THIRD_PARTY_CENTIPEDE_REMOTE_FILE_H_ #define THIRD_PARTY_CENTIPEDE_REMOTE_FILE_H_ +#include #include // NOLINT #include #include @@ -75,6 +76,9 @@ void RemoteFileGetContents(const std::filesystem::path &path, // Returns true if `path` exists. bool RemotePathExists(std::string_view path); +// Returns the size of the file at `path` in bytes. The file must exist. +int64_t RemoteFileGetSize(std::string_view path); + // Finds all files matching `glob` and appends them to `matches`. void RemoteGlobMatch(std::string_view glob, std::vector &matches); diff --git a/centipede/remote_file_test.cc b/centipede/remote_file_test.cc index f521315b4..dbf702b18 100644 --- a/centipede/remote_file_test.cc +++ b/centipede/remote_file_test.cc @@ -14,6 +14,7 @@ #include "./centipede/remote_file.h" +#include #include // NOLINT #include #include @@ -22,6 +23,7 @@ #include "gmock/gmock.h" #include "gtest/gtest.h" +#include "absl/log/check.h" #include "./centipede/test_util.h" namespace centipede { @@ -30,14 +32,32 @@ namespace { using ::testing::IsEmpty; using ::testing::UnorderedElementsAre; -bool CreateFile(std::string_view path) { +bool CreateFile(std::string_view path, std::string_view contents = "") { std::ofstream f((std::string(path))); if (!f) { return false; } + f << contents; return true; } +TEST(RemoteFile, GetSize) { + const std::filesystem::path temp_dir{GetTestTempDir(test_info_->name())}; + const std::string file_path = temp_dir / "file_01"; + { + const std::string file_contents1 = "abcd1234"; + CHECK(CreateFile(file_path, file_contents1)); + const int64_t size = RemoteFileGetSize(file_path); + EXPECT_EQ(size, file_contents1.size()); + } + { + const std::string file_contents2 = "efg567"; + RemoteFileSetContents(file_path, file_contents2); + const int64_t size = RemoteFileGetSize(file_path); + EXPECT_EQ(size, file_contents2.size()); + } +} + TEST(RemoteListFilesRecursively, ListsFilesInRecursiveDirectories) { auto temp_dir = std::filesystem::path(GetTestTempDir(test_info_->name()));