From fee25aaaa2d794d10c93c02d448db21f341e065c Mon Sep 17 00:00:00 2001 From: Denver Coneybeare Date: Thu, 11 Jan 2024 16:30:39 -0500 Subject: [PATCH] Use NSFileSystem instead of stat() --- Firestore/CHANGELOG.md | 3 +++ Firestore/core/src/util/filesystem_apple.mm | 19 +++++++++++++++++++ Firestore/core/src/util/filesystem_posix.cc | 2 ++ 3 files changed, 24 insertions(+) diff --git a/Firestore/CHANGELOG.md b/Firestore/CHANGELOG.md index 793efc01f5a5..0abb76208414 100644 --- a/Firestore/CHANGELOG.md +++ b/Firestore/CHANGELOG.md @@ -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) diff --git a/Firestore/core/src/util/filesystem_apple.mm b/Firestore/core/src/util/filesystem_apple.mm index 85e4db356f01..761e37e0f1d3 100644 --- a/Firestore/core/src/util/filesystem_apple.mm +++ b/Firestore/core/src/util/filesystem_apple.mm @@ -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 { @@ -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 diff --git a/Firestore/core/src/util/filesystem_posix.cc b/Firestore/core/src/util/filesystem_posix.cc index cb6ed42b5b73..56c008a8d067 100644 --- a/Firestore/core/src/util/filesystem_posix.cc +++ b/Firestore/core/src/util/filesystem_posix.cc @@ -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)) { @@ -165,6 +166,7 @@ Status Filesystem::IsDirectory(const Path& path) { return Status::OK(); } +#endif // !__APPLE__ StatusOr Filesystem::FileSize(const Path& path) { struct stat st {};