diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b25a4b..b2be072 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# v10.1.1 +- Fix `GDTCORFlatFileStorage` crash. + ([firebase-ios-sdk/#14645](https://github.com/firebase/firebase-ios-sdk/issues/14645)) + # v10.1.0 - Fix `[FBLPromise HTTPBody]` SwiftUI Previews crash when using binary distribution. ([firebase-ios-sdk/#13318](https://github.com/firebase/firebase-ios-sdk/issues/13318), diff --git a/GoogleDataTransport/GDTCORLibrary/GDTCORPlatform.m b/GoogleDataTransport/GDTCORLibrary/GDTCORPlatform.m index 0729bde..549b74a 100644 --- a/GoogleDataTransport/GDTCORLibrary/GDTCORPlatform.m +++ b/GoogleDataTransport/GDTCORLibrary/GDTCORPlatform.m @@ -232,28 +232,32 @@ GDTCORNetworkMobileSubtype GDTCORNetworkMobileSubTypeMessage(void) { } BOOL GDTCORWriteDataToFile(NSData *data, NSString *filePath, NSError *_Nullable *outError) { - BOOL result = NO; - if (filePath.length > 0) { - result = [[NSFileManager defaultManager] - createDirectoryAtPath:[filePath stringByDeletingLastPathComponent] - withIntermediateDirectories:YES - attributes:nil - error:outError]; - if (result == NO || *outError) { - GDTCORLogDebug(@"Attempt to create directory failed: path:%@ error:%@", filePath, *outError); - return result; - } + if (filePath.length == 0) { + return NO; } - - if (filePath.length > 0) { - result = [data writeToFile:filePath options:NSDataWritingAtomic error:outError]; - if (result == NO || *outError) { - GDTCORLogDebug(@"Attempt to write archive failed: path:%@ error:%@", filePath, *outError); - } else { - GDTCORLogDebug(@"Writing archive succeeded: %@", filePath); + NSString *directoryPath = [filePath stringByDeletingLastPathComponent]; + NSFileManager *fileManager = [NSFileManager defaultManager]; + if (![fileManager fileExistsAtPath:directoryPath]) { + NSError *error = nil; + if (![fileManager createDirectoryAtPath:directoryPath + withIntermediateDirectories:YES + attributes:nil + error:&error]) { + GDTCORLogDebug(@"Attempt to create directory failed: path:%@ error:%@", directoryPath, error); + if (outError) { + *outError = error; + } + return NO; } } + BOOL result = [data writeToFile:filePath options:NSDataWritingAtomic error:outError]; + if (result == NO) { + GDTCORLogDebug(@"Attempt to write archive failed: path:%@ error:%@", filePath, + outError ? *outError : nil); + } else { + GDTCORLogDebug(@"Writing archive succeeded: %@", filePath); + } return result; } diff --git a/GoogleDataTransport/GDTCORTests/Unit/GDTCORPlatformTest.m b/GoogleDataTransport/GDTCORTests/Unit/GDTCORPlatformTest.m index bda2ee6..d7181d8 100644 --- a/GoogleDataTransport/GDTCORTests/Unit/GDTCORPlatformTest.m +++ b/GoogleDataTransport/GDTCORTests/Unit/GDTCORPlatformTest.m @@ -78,4 +78,26 @@ - (void)testGDTCORApplicationBeginBackgroundTask { XCTAssertNoThrow([application endBackgroundTask:bgID]); } +- (void)testGDTCORWriteDataToFile { + NSString *directoryPath = + [NSTemporaryDirectory() stringByAppendingPathComponent:@"testGDTCORWriteDataToFile"]; + NSString *filePath = [directoryPath stringByAppendingPathComponent:@"testFile.txt"]; + NSData *data = [@"testData" dataUsingEncoding:NSUTF8StringEncoding]; + + // Clean up any old test artifacts. + [[NSFileManager defaultManager] removeItemAtPath:directoryPath error:nil]; + + // Write the data to the file. + NSError *error; + XCTAssertTrue(GDTCORWriteDataToFile(data, filePath, &error)); + XCTAssertNil(error); + + // Verify that the file was created and contains the correct data. + NSData *readData = [NSData dataWithContentsOfFile:filePath]; + XCTAssertEqualObjects(data, readData); + + // Clean up the test artifacts. + [[NSFileManager defaultManager] removeItemAtPath:directoryPath error:nil]; +} + @end