Skip to content

Commit

Permalink
Firestore: Another use foundation API instead of C API (#12315)
Browse files Browse the repository at this point in the history
  • Loading branch information
dconeybe authored Jan 24, 2024
1 parent 5048195 commit e8ec72b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
31 changes: 31 additions & 0 deletions Firestore/core/src/util/filesystem_apple.mm
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,37 @@
return Status::OK();
}

StatusOr<int64_t> 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
Expand Down
2 changes: 1 addition & 1 deletion Firestore/core/src/util/filesystem_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ Status Filesystem::IsDirectory(const Path& path) {

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

StatusOr<int64_t> Filesystem::FileSize(const Path& path) {
struct stat st {};
Expand All @@ -177,6 +176,7 @@ StatusOr<int64_t> 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)) {
Expand Down

0 comments on commit e8ec72b

Please sign in to comment.