Skip to content

Commit

Permalink
#Centipede Add RemoteFileGetSize() API
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 601529743
  • Loading branch information
ussuri authored and copybara-github committed Jan 29, 2024
1 parent d47057b commit d10d746
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions centipede/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,7 @@ cc_test(
deps = [
":remote_file",
":test_util",
"@com_google_absl//absl/log:check",
"@com_google_googletest//:gtest_main",
],
)
Expand Down
10 changes: 10 additions & 0 deletions centipede/remote_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <glob.h>

#include <cstdint>
#include <cstdio>
#include <filesystem> // NOLINT
#include <memory>
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions centipede/remote_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#ifndef THIRD_PARTY_CENTIPEDE_REMOTE_FILE_H_
#define THIRD_PARTY_CENTIPEDE_REMOTE_FILE_H_

#include <cstdint>
#include <filesystem> // NOLINT
#include <memory>
#include <string>
Expand Down Expand Up @@ -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<std::string> &matches);

Expand Down
22 changes: 21 additions & 1 deletion centipede/remote_file_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "./centipede/remote_file.h"

#include <cstdint>
#include <filesystem> // NOLINT
#include <fstream>
#include <string>
Expand All @@ -22,6 +23,7 @@

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/log/check.h"
#include "./centipede/test_util.h"

namespace centipede {
Expand All @@ -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()));

Expand Down

0 comments on commit d10d746

Please sign in to comment.