@@ -977,26 +977,47 @@ - (id)deserializePrimitive:(NSData *)data {
977977}
978978
979979+ (void )ensureDir : (NSString *)path markAsDoNotBackup : (BOOL )markAsDoNotBackup {
980- NSError *error = nil ;
981- NSDictionary *attributes = @{
982- NSFileProtectionKey :
983- NSFileProtectionCompleteUntilFirstUserAuthentication
984- };
985- BOOL success =
986- [[NSFileManager defaultManager ] createDirectoryAtPath: path
987- withIntermediateDirectories: YES
988- attributes: attributes
989- error: &error];
980+ NSFileManager *fileManager = [NSFileManager defaultManager ];
981+ NSError *error;
982+
983+ // Create the directory if it doesn't exist. This call is a no-op if it
984+ // already exists.
985+ BOOL success = [fileManager createDirectoryAtPath: path
986+ withIntermediateDirectories: YES
987+ attributes: nil
988+ error: &error];
990989 if (!success) {
991990 @throw [NSException
992991 exceptionWithName: @" FailedToCreatePersistenceDir"
993992 reason: @" Failed to create persistence directory."
994993 userInfo: @{
995994 @" path" : path,
996- @" error" : error ? error : [NSNull null ]
995+ @" error" : error ?: [NSNull null ]
997996 }];
998997 }
999998
999+ #if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_VISION || TARGET_OS_OSX || \
1000+ TARGET_OS_MACCATALYST
1001+ // Now, ensure the file protection attribute is set. This will apply it
1002+ // whether the directory was just created or already existed. This attribute
1003+ // is ignored on systems that do not support it.
1004+ NSDictionary *attributes = @{
1005+ NSFileProtectionKey :
1006+ NSFileProtectionCompleteUntilFirstUserAuthentication
1007+ };
1008+ success = [fileManager setAttributes: attributes
1009+ ofItemAtPath: path
1010+ error: &error];
1011+ if (!success) {
1012+ // This is not a fatal error, as file protection may not be supported on
1013+ // all OS versions.
1014+ FFWarn (@" I-RDB076036" ,
1015+ @" Failed to set file protection attribute on persistence "
1016+ @" directory: %@ " ,
1017+ error);
1018+ }
1019+ #endif
1020+
10001021 if (markAsDoNotBackup) {
10011022 NSURL *firebaseDirURL = [NSURL fileURLWithPath: path];
10021023 success = [firebaseDirURL setResourceValue: @YES
0 commit comments