Skip to content

Commit

Permalink
Use NSFileSystem instead of stat()
Browse files Browse the repository at this point in the history
  • Loading branch information
dconeybe committed Jan 11, 2024
1 parent 4ae0c5d commit fee25aa
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Firestore/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Unreleased
- [fixed] Replace calls to the `stat()` system call with `NSFileManager`.

# 10.19.0
- [fixed] Made an optimization to the synchronization logic for resumed queries
to only re-download locally-cached documents that are known to be out-of-sync. (#12044)
Expand Down
19 changes: 19 additions & 0 deletions Firestore/core/src/util/filesystem_apple.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "Firestore/core/src/util/path.h"
#include "Firestore/core/src/util/statusor.h"
#include "Firestore/core/src/util/string_format.h"
#include "absl/strings/str_cat.h"

namespace firebase {
Expand Down Expand Up @@ -90,6 +91,24 @@
return Path::FromUtf8("/tmp");
}

Status Filesystem::IsDirectory(const Path& path) {
NSFileManager* file_manager = NSFileManager.defaultManager;
NSString* ns_path_str = path.ToNSString();
BOOL is_directory = NO;

if (![file_manager fileExistsAtPath:ns_path_str isDirectory:&is_directory]) {
return Status{Error::kErrorNotFound, path.ToUtf8String()};
}

if (!is_directory) {
return Status{Error::kErrorFailedPrecondition,
StringFormat("Path %s exists but is not a directory",
path.ToUtf8String())};
}

return Status::OK();
}

} // namespace util
} // namespace firestore
} // namespace firebase
Expand Down
2 changes: 2 additions & 0 deletions Firestore/core/src/util/filesystem_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ Path Filesystem::TempDir() {
}
#endif // !__APPLE__ && !_WIN32

#if !__APPLE__
Status Filesystem::IsDirectory(const Path& path) {
struct stat buffer {};
if (::stat(path.c_str(), &buffer)) {
Expand Down Expand Up @@ -165,6 +166,7 @@ Status Filesystem::IsDirectory(const Path& path) {

return Status::OK();
}
#endif // !__APPLE__

StatusOr<int64_t> Filesystem::FileSize(const Path& path) {
struct stat st {};
Expand Down

0 comments on commit fee25aa

Please sign in to comment.