Skip to content

Commit 8e29812

Browse files
committed
[RTDB] Fix Fatal Exception: FirebaseDatabasePersistenceFailure
1 parent f63f3cc commit 8e29812

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

FirebaseDatabase/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Unreleased
2+
- [fixed] Fix `Fatal Exception: FirebaseDatabasePersistenceFailure`. (#4493)
3+
14
# 11.9.0
25
- [fixed] Fix connection failure issue introduced in 10.27.0 by restoring the
36
Socket Rocket implementation instead of `NSURLSessionWebSocket`. Note that

FirebaseDatabase/Sources/Persistence/FLevelDBStorageEngine.m

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -978,16 +978,23 @@ - (id)deserializePrimitive:(NSData *)data {
978978

979979
+ (void)ensureDir:(NSString *)path markAsDoNotBackup:(BOOL)markAsDoNotBackup {
980980
NSError *error;
981+
NSDictionary *attributes = @{
982+
NSFileProtectionKey :
983+
NSFileProtectionCompleteUntilFirstUserAuthentication
984+
};
981985
BOOL success =
982986
[[NSFileManager defaultManager] createDirectoryAtPath:path
983987
withIntermediateDirectories:YES
984-
attributes:nil
988+
attributes:attributes
985989
error:&error];
986990
if (!success) {
987991
@throw [NSException
988992
exceptionWithName:@"FailedToCreatePersistenceDir"
989993
reason:@"Failed to create persistence directory."
990-
userInfo:@{@"path" : path}];
994+
userInfo:@{
995+
@"path" : path,
996+
@"error" : error ? error : [NSNull null]
997+
}];
991998
}
992999

9931000
if (markAsDoNotBackup) {
@@ -1000,9 +1007,6 @@ + (void)ensureDir:(NSString *)path markAsDoNotBackup:(BOOL)markAsDoNotBackup {
10001007
@"I-RDB076035",
10011008
@"Failed to mark firebase database folder as do not backup: %@",
10021009
error);
1003-
[NSException raise:@"Error marking as do not backup"
1004-
format:@"Failed to mark folder %@ as do not backup",
1005-
firebaseDirURL];
10061010
}
10071011
}
10081012
}

FirebaseDatabase/Tests/Unit/FLevelDBStorageEngineTests.m

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@
2525
#import "FirebaseDatabase/Sources/Snapshot/FSnapshotUtilities.h"
2626
#import "FirebaseDatabase/Tests/Helpers/FTestHelpers.h"
2727

28-
@interface FLevelDBStorageEngineTests : XCTestCase
28+
@interface FLevelDBStorageEngine (Tests)
29+
+ (void)ensureDir:(NSString *)path markAsDoNotBackup:(BOOL)markAsDoNotBackup;
30+
@end
2931

32+
@interface FLevelDBStorageEngineTests : XCTestCase
3033
@end
3134

3235
@implementation FLevelDBStorageEngineTests
@@ -685,4 +688,36 @@ - (void)testRemoveTrackedQueryRemovesTrackedQueryKeys {
685688
([NSSet setWithArray:@[ @"b", @"c" ]]));
686689
}
687690

691+
- (void)testEnsureDirSetsCorrectFileProtection {
692+
NSString *testDirName =
693+
[NSString stringWithFormat:@"fdb_persistence_test_%lu", (unsigned long)arc4random()];
694+
NSString *testPath = [NSTemporaryDirectory() stringByAppendingPathComponent:testDirName];
695+
696+
// Ensure the directory doesn't exist before the test
697+
[[NSFileManager defaultManager] removeItemAtPath:testPath error:nil];
698+
699+
// Call the method to create the directory
700+
[FLevelDBStorageEngine ensureDir:testPath markAsDoNotBackup:NO];
701+
702+
// Get the attributes of the created directory
703+
NSError *error = nil;
704+
NSDictionary<NSFileAttributeKey, id> *attributes =
705+
[[NSFileManager defaultManager] attributesOfItemAtPath:testPath error:&error];
706+
707+
// Assert that the file protection attribute is correct
708+
XCTAssertNil(error, @"Failed to get attributes of directory: %@", error);
709+
710+
#if TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR
711+
// On a physical iOS device, file protection should be set.
712+
XCTAssertEqualObjects(attributes[NSFileProtectionKey],
713+
NSFileProtectionCompleteUntilFirstUserAuthentication);
714+
#else
715+
// In the simulator or on other platforms, file protection is not supported, so the key
716+
// should be nil.
717+
XCTAssertNil(attributes[NSFileProtectionKey]);
718+
#endif
719+
720+
// Clean up
721+
[[NSFileManager defaultManager] removeItemAtPath:testPath error:nil];
722+
}
688723
@end

0 commit comments

Comments
 (0)