Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
40 changes: 22 additions & 18 deletions GoogleDataTransport/GDTCORLibrary/GDTCORPlatform.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
22 changes: 22 additions & 0 deletions GoogleDataTransport/GDTCORTests/Unit/GDTCORPlatformTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading