Skip to content

Add GetFileSize to filesystem.h #1882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 1, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Firestore/core/src/firebase/firestore/util/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ Status RecursivelyDelete(const Path& path);
*/
Path TempDir();

/**
* On success, sets `size` to be the size in bytes of the file specified by
* `path`.
*/
Status GetFileSize(const Path& path, off_t* size);

/**
* Implements an iterator over the contents of a directory. Initializes to the
* first entry in the directory.
Expand Down
11 changes: 11 additions & 0 deletions Firestore/core/src/firebase/firestore/util/filesystem_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ Path TempDir() {
}
#endif // !defined(__APPLE__)

Status GetFileSize(const Path& path, off_t* size) {
struct stat st;
if (stat(path.c_str(), &st) == 0) {
*size = st.st_size;
return Status::OK();
} else {
return Status::FromErrno(
errno, StringFormat("Failed to stat file: %s", path.ToUtf8String()));
}
}

namespace detail {

Status CreateDir(const Path& path) {
Expand Down
24 changes: 24 additions & 0 deletions Firestore/core/test/firebase/firestore/util/filesystem_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ static Path TestFilename() {
return Path::FromUtf8("firestore-testing-" + CreateAutoId());
}

static void WriteBytesToFile(const Path& path, int byte_count) {
std::string bytes(byte_count, 'a');
std::ofstream out{path.native_value()};
ASSERT_TRUE(out.good());
out << bytes;
out.close();
ASSERT_TRUE(out.good());
}

#define ASSERT_NOT_FOUND(expression) \
do { \
ASSERT_EQ(FirestoreErrorCode::NotFound, (expression).code()); \
Expand Down Expand Up @@ -236,6 +245,21 @@ TEST(FilesystemTest, RecursivelyDeletePreservesPeers) {
EXPECT_OK(RecursivelyDelete(root_dir));
}

TEST(FilesystemTest, GetFileSize) {
Path file = Path::JoinUtf8(TempDir(), TestFilename());
off_t size;
ASSERT_NOT_FOUND(GetFileSize(file, &size));
Touch(file);
ASSERT_OK(GetFileSize(file, &size));
ASSERT_EQ(0, size);

WriteBytesToFile(file, 100);
ASSERT_OK(GetFileSize(file, &size));
ASSERT_EQ(100, size);

EXPECT_OK(RecursivelyDelete(file));
}

} // namespace util
} // namespace firestore
} // namespace firebase