Skip to content

Commit 3f60f8d

Browse files
author
Greg Soltis
authored
Add GetFileSize to filesystem.h (#1882)
Add FileSize() to filesystem.h
1 parent 845a25e commit 3f60f8d

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

Firestore/core/src/firebase/firestore/util/filesystem.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "Firestore/core/src/firebase/firestore/util/path.h"
2323
#include "Firestore/core/src/firebase/firestore/util/status.h"
24+
#include "Firestore/core/src/firebase/firestore/util/statusor.h"
2425

2526
namespace firebase {
2627
namespace firestore {
@@ -72,6 +73,12 @@ Status RecursivelyDelete(const Path& path);
7273
*/
7374
Path TempDir();
7475

76+
/**
77+
* On success, returns the size in bytes of the file specified by
78+
* `path`.
79+
*/
80+
StatusOr<off_t> FileSize(const Path& path);
81+
7582
/**
7683
* Implements an iterator over the contents of a directory. Initializes to the
7784
* first entry in the directory.

Firestore/core/src/firebase/firestore/util/filesystem_posix.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ Path TempDir() {
9595
}
9696
#endif // !defined(__APPLE__)
9797

98+
StatusOr<off_t> FileSize(const Path& path) {
99+
struct stat st {};
100+
if (stat(path.c_str(), &st) == 0) {
101+
return StatusOr<off_t>(st.st_size);
102+
} else {
103+
return StatusOr<off_t>(Status::FromErrno(
104+
errno, StringFormat("Failed to stat file: %s", path.ToUtf8String())));
105+
}
106+
}
107+
98108
namespace detail {
99109

100110
Status CreateDir(const Path& path) {

Firestore/core/test/firebase/firestore/util/filesystem_test.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ static Path TestFilename() {
4545
return Path::FromUtf8("firestore-testing-" + CreateAutoId());
4646
}
4747

48+
static void WriteBytesToFile(const Path& path, int byte_count) {
49+
std::string bytes(byte_count, 'a');
50+
std::ofstream out{path.native_value()};
51+
ASSERT_TRUE(out.good());
52+
out << bytes;
53+
out.close();
54+
ASSERT_TRUE(out.good());
55+
}
56+
4857
#define ASSERT_NOT_FOUND(expression) \
4958
do { \
5059
ASSERT_EQ(FirestoreErrorCode::NotFound, (expression).code()); \
@@ -236,6 +245,22 @@ TEST(FilesystemTest, RecursivelyDeletePreservesPeers) {
236245
EXPECT_OK(RecursivelyDelete(root_dir));
237246
}
238247

248+
TEST(FilesystemTest, FileSize) {
249+
Path file = Path::JoinUtf8(TempDir(), TestFilename());
250+
ASSERT_NOT_FOUND(FileSize(file).status());
251+
Touch(file);
252+
StatusOr<off_t> result = FileSize(file);
253+
ASSERT_OK(result.status());
254+
ASSERT_EQ(0, result.ValueOrDie());
255+
256+
WriteBytesToFile(file, 100);
257+
result = FileSize(file);
258+
ASSERT_OK(result.status());
259+
ASSERT_EQ(100, result.ValueOrDie());
260+
261+
EXPECT_OK(RecursivelyDelete(file));
262+
}
263+
239264
} // namespace util
240265
} // namespace firestore
241266
} // namespace firebase

0 commit comments

Comments
 (0)