Skip to content

Commit e8ec72b

Browse files
authored
Firestore: Another use foundation API instead of C API (#12315)
1 parent 5048195 commit e8ec72b

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

Firestore/core/src/util/filesystem_apple.mm

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,37 @@
109109
return Status::OK();
110110
}
111111

112+
StatusOr<int64_t> Filesystem::FileSize(const Path& path) {
113+
NSFileManager* file_manager = NSFileManager.defaultManager;
114+
NSString* ns_path_str = path.ToNSString();
115+
NSError* error = nil;
116+
117+
NSDictionary* attributes = [file_manager attributesOfItemAtPath:ns_path_str
118+
error:&error];
119+
120+
if (attributes == nil) {
121+
if ([error.domain isEqualToString:NSCocoaErrorDomain]) {
122+
switch (error.code) {
123+
case NSFileReadNoSuchFileError:
124+
case NSFileNoSuchFileError:
125+
return Status{Error::kErrorNotFound, path.ToUtf8String()}.CausedBy(
126+
Status::FromNSError(error));
127+
}
128+
}
129+
130+
return Status{Error::kErrorInternal,
131+
StringFormat("attributesOfItemAtPath failed for %s",
132+
path.ToUtf8String())}
133+
.CausedBy(Status::FromNSError(error));
134+
}
135+
136+
NSNumber* fileSizeNumber = [attributes objectForKey:NSFileSize];
137+
138+
// Use brace initialization of the in64_t return value so that compilation
139+
// will fail if the conversion from long long is narrowing.
140+
return {[fileSizeNumber longLongValue]};
141+
}
142+
112143
} // namespace util
113144
} // namespace firestore
114145
} // namespace firebase

Firestore/core/src/util/filesystem_posix.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ Status Filesystem::IsDirectory(const Path& path) {
166166

167167
return Status::OK();
168168
}
169-
#endif // !__APPLE__
170169

171170
StatusOr<int64_t> Filesystem::FileSize(const Path& path) {
172171
struct stat st {};
@@ -177,6 +176,7 @@ StatusOr<int64_t> Filesystem::FileSize(const Path& path) {
177176
errno, StringFormat("Failed to stat file: %s", path.ToUtf8String()));
178177
}
179178
}
179+
#endif // !__APPLE__
180180

181181
Status Filesystem::CreateDir(const Path& path) {
182182
if (::mkdir(path.c_str(), 0777)) {

0 commit comments

Comments
 (0)