From 0949b2d5e3c4723a945667a4e6a13ef623146286 Mon Sep 17 00:00:00 2001 From: Nick Dowell Date: Wed, 12 Jan 2022 15:42:14 +0000 Subject: [PATCH] KSCrashState buffered output --- .../Source/KSCrash/Recording/BSG_KSCrashState.m | 12 ++++++++---- Tests/KSCrashTests/KSCrashState_Tests.m | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSCrashState.m b/Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSCrashState.m index dff9b7231..3b96c6047 100644 --- a/Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSCrashState.m +++ b/Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSCrashState.m @@ -26,7 +26,7 @@ #include "BSG_KSCrashState.h" -#include "BSG_KSFileUtils.h" +#include "BSG_KSFile.h" #include "BSG_KSJSONCodec.h" #include "BSG_KSJSONCodecObjC.h" #include "BSG_KSMach.h" @@ -154,8 +154,7 @@ int bsg_kscrashstate_i_onEndData(__unused void *const userData) { */ int bsg_kscrashstate_i_addJSONData(const char *const data, const size_t length, void *const userData) { - const int fd = *((int *)userData); - const bool success = bsg_ksfuwriteBytesToFD(fd, data, (ssize_t)length); + bool success = BSG_KSFileWrite(userData, data, length); return success ? BSG_KSJSON_OK : BSG_KSJSON_ERROR_CANNOT_ADD_DATA; } @@ -227,9 +226,13 @@ bool bsg_kscrashstate_i_saveState(const BSG_KSCrash_State *const state, return false; } + BSG_KSFile file; + char buffer[256]; + BSG_KSFileInit(&file, fd, buffer, sizeof(buffer) / sizeof(*buffer)); + BSG_KSJSONEncodeContext JSONContext; bsg_ksjsonbeginEncode(&JSONContext, false, bsg_kscrashstate_i_addJSONData, - &fd); + &file); int result; if ((result = bsg_ksjsonbeginObject(&JSONContext, NULL)) != BSG_KSJSON_OK) { @@ -269,6 +272,7 @@ bool bsg_kscrashstate_i_saveState(const BSG_KSCrash_State *const state, result = bsg_ksjsonendEncode(&JSONContext); done: + BSG_KSFileFlush(&file); close(fd); if (result != BSG_KSJSON_OK) { diff --git a/Tests/KSCrashTests/KSCrashState_Tests.m b/Tests/KSCrashTests/KSCrashState_Tests.m index f269a41db..e659edbff 100755 --- a/Tests/KSCrashTests/KSCrashState_Tests.m +++ b/Tests/KSCrashTests/KSCrashState_Tests.m @@ -483,4 +483,20 @@ - (void)testBGFGCrash XCTAssertTrue(context.crashedLastLaunch, @""); } +- (void)testPersistence +{ + NSString *file = [self.tempPath stringByAppendingPathComponent:@"state.json"]; + + BSG_KSCrash_State state = {0}; + bsg_kscrashstate_init([file fileSystemRepresentation], &state); + + id json = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:file] options:0 error:nil]; + XCTAssertEqualObjects([json objectForKey:@"crashedLastLaunch"], @NO); + + bsg_kscrashstate_notifyAppCrash(); + + json = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:file] options:0 error:nil]; + XCTAssertEqualObjects([json objectForKey:@"crashedLastLaunch"], @YES); +} + @end