Skip to content

Commit

Permalink
use file descriptor rather than file
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench committed Aug 24, 2017
1 parent 63973ac commit 9b0d3fb
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 20 deletions.
21 changes: 14 additions & 7 deletions Source/KSCrash/Source/KSCrash/Recording/KSCrashReport.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ int kscrw_i_addJSONData(const char* const data,
const size_t length,
void* const userData)
{
FILE *file = userData;
const bool success = ksfu_writeBytesToFD(file, data, (ssize_t)length);
const int fd = *((int*)userData);
const bool success = ksfu_writeBytesToFD(fd, data, (ssize_t)length);
return success ? KSJSON_OK : KSJSON_ERROR_CANNOT_ADD_DATA;
}

Expand Down Expand Up @@ -2155,20 +2155,24 @@ void kscrashreport_writeStandardReport(KSCrash_Context* const crashContext,
const char* const path)
{
KSLOG_INFO("Writing crash report to %s", path);
FILE *reportFile = fopen(path, "w");

int fd = kscrw_i_openCrashReportFile(path);
if(fd < 0)
{
return;
}

g_introspectionRules = &crashContext->config.introspectionRules;

kscrw_i_updateStackOverflowStatus(crashContext);

KSJSONEncodeContext jsonContext;
jsonContext.userData = reportFile;
jsonContext.userData = &fd;
KSCrashReportWriter concreteWriter;
KSCrashReportWriter* writer = &concreteWriter;
kscrw_i_prepareReportWriter(writer, &jsonContext);

ksjson_beginEncode(getJsonContext(writer), true, kscrw_i_addJSONData, reportFile);
jsonContext.reportFile = reportFile;
ksjson_beginEncode(getJsonContext(writer), true, kscrw_i_addJSONData, &fd);

writer->beginObject(writer, KSCrashField_Report);
{
Expand Down Expand Up @@ -2224,7 +2228,10 @@ void kscrashreport_writeStandardReport(KSCrash_Context* const crashContext,

ksjson_endEncode(getJsonContext(writer));

fclose(reportFile);
if (!ksfu_flushWriteBuffer(fd)) {
KSLOG_ERROR("Failed to flush write buffer");
}
close(fd);
}

void kscrashreport_logCrash(const KSCrash_Context* const crashContext)
Expand Down
22 changes: 16 additions & 6 deletions Source/KSCrash/Source/KSCrash/Recording/KSCrashState.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ int kscrashstate_i_addJSONData(const char* const data,
const size_t length,
void* const userData)
{
FILE *file = userData;
const bool success = ksfu_writeBytesToFD(file, data, (ssize_t)length);
const int fd = *((int*)userData);
const bool success = ksfu_writeBytesToFD(fd, data, (ssize_t)length);
return success ? KSJSON_OK : KSJSON_ERROR_CANNOT_ADD_DATA;
}

Expand Down Expand Up @@ -252,14 +252,20 @@ bool kscrashstate_i_loadState(KSCrash_State* const context,
bool kscrashstate_i_saveState(const KSCrash_State* const state,
const char* const path)
{
FILE *reportFile = fopen(path, "w");
int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644);
if(fd < 0)
{
KSLOG_ERROR("Could not open file %s for writing: %s",
path,
strerror(errno));
return false;
}

KSJSONEncodeContext JSONContext;
ksjson_beginEncode(&JSONContext,
true,
kscrashstate_i_addJSONData,
reportFile);
JSONContext.reportFile = reportFile;
&fd);

int result;
if((result = ksjson_beginObject(&JSONContext, NULL)) != KSJSON_OK)
Expand Down Expand Up @@ -306,7 +312,11 @@ bool kscrashstate_i_saveState(const KSCrash_State* const state,
result = ksjson_endEncode(&JSONContext);

done:
fclose(reportFile);
if (!ksfu_flushWriteBuffer(fd)) {
KSLOG_ERROR("Failed to flush write buffer");
}
close(fd);

if(result != KSJSON_OK)
{
KSLOG_ERROR("%s: %s",
Expand Down
23 changes: 20 additions & 3 deletions Source/KSCrash/Source/KSCrash/Recording/Tools/KSFileUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,29 @@ const char* ksfu_lastPathEntry(const char* const path)
return lastFile == NULL ? path : lastFile + 1;
}

bool ksfu_writeBytesToFD(FILE *file,
bool ksfu_flushWriteBuffer(const int fd)
{
return true;
// TODO
}

bool ksfu_writeBytesToFD(const int fd,
const char* const bytes,
ssize_t length)
{
fwrite(bytes, sizeof(char), length, file);
return true; // TODO error handling!
const char* pos = bytes; // FIXME buffer up!
while(length > 0)
{
ssize_t bytesWritten = write(fd, pos, (size_t)length);
if(bytesWritten == -1)
{
KSLOG_ERROR("Could not write to fd %d: %s", fd, strerror(errno));
return false;
}
length -= bytesWritten;
pos += bytesWritten;
}
return true;
}

bool ksfu_readBytesFromFD(const int fd,
Expand Down
11 changes: 9 additions & 2 deletions Source/KSCrash/Source/KSCrash/Recording/Tools/KSFileUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,22 @@ const char* ksfu_lastPathEntry(const char* path);

/** Write bytes to a file descriptor.
*
* @param file The file to write to.
* @param fd The file descriptor
*
* @param bytes Buffer containing the bytes.
*
* @param length The number of bytes to write.
*
* @return true if the operation was successful.
*/
bool ksfu_writeBytesToFD(FILE *file, const char* bytes, ssize_t length);
bool ksfu_writeBytesToFD(const int fd, const char* bytes, ssize_t length);

/**
* Flushes the write buffer
*
* @param fd The file descriptor
*/
bool ksfu_flushWriteBuffer(const int fd);

/** Read bytes from a file descriptor.
*
Expand Down
2 changes: 0 additions & 2 deletions Source/KSCrash/Source/KSCrash/Recording/Tools/KSJSONCodec.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ typedef struct
bool containerFirstEntry;

bool prettyPrint;

FILE *reportFile;

} KSJSONEncodeContext;

Expand Down

0 comments on commit 9b0d3fb

Please sign in to comment.