From e8ec72b4703cbd3de499749f0b8dfb56d6bb47dd Mon Sep 17 00:00:00 2001 From: Denver Coneybeare Date: Wed, 24 Jan 2024 13:18:52 -0500 Subject: [PATCH] Firestore: Another use foundation API instead of C API (#12315) --- Firestore/core/src/util/filesystem_apple.mm | 31 +++++++++++++++++++++ Firestore/core/src/util/filesystem_posix.cc | 2 +- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/Firestore/core/src/util/filesystem_apple.mm b/Firestore/core/src/util/filesystem_apple.mm index 761e37e0f1d..9802017a6f8 100644 --- a/Firestore/core/src/util/filesystem_apple.mm +++ b/Firestore/core/src/util/filesystem_apple.mm @@ -109,6 +109,37 @@ return Status::OK(); } +StatusOr Filesystem::FileSize(const Path& path) { + NSFileManager* file_manager = NSFileManager.defaultManager; + NSString* ns_path_str = path.ToNSString(); + NSError* error = nil; + + NSDictionary* attributes = [file_manager attributesOfItemAtPath:ns_path_str + error:&error]; + + if (attributes == nil) { + if ([error.domain isEqualToString:NSCocoaErrorDomain]) { + switch (error.code) { + case NSFileReadNoSuchFileError: + case NSFileNoSuchFileError: + return Status{Error::kErrorNotFound, path.ToUtf8String()}.CausedBy( + Status::FromNSError(error)); + } + } + + return Status{Error::kErrorInternal, + StringFormat("attributesOfItemAtPath failed for %s", + path.ToUtf8String())} + .CausedBy(Status::FromNSError(error)); + } + + NSNumber* fileSizeNumber = [attributes objectForKey:NSFileSize]; + + // Use brace initialization of the in64_t return value so that compilation + // will fail if the conversion from long long is narrowing. + return {[fileSizeNumber longLongValue]}; +} + } // 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 56c008a8d06..8688ce5897c 100644 --- a/Firestore/core/src/util/filesystem_posix.cc +++ b/Firestore/core/src/util/filesystem_posix.cc @@ -166,7 +166,6 @@ Status Filesystem::IsDirectory(const Path& path) { return Status::OK(); } -#endif // !__APPLE__ StatusOr Filesystem::FileSize(const Path& path) { struct stat st {}; @@ -177,6 +176,7 @@ StatusOr Filesystem::FileSize(const Path& path) { errno, StringFormat("Failed to stat file: %s", path.ToUtf8String())); } } +#endif // !__APPLE__ Status Filesystem::CreateDir(const Path& path) { if (::mkdir(path.c_str(), 0777)) {