diff options
| author | Markus Mittendrein <git@maxmitti.tk> | 2019-08-16 19:40:28 +0200 |
|---|---|---|
| committer | Markus Mittendrein <git@maxmitti.tk> | 2019-08-19 13:43:23 +0200 |
| commit | f9084d17239791334dbac304160f3e3759f0955d (patch) | |
| tree | 234ee89c7e5f4153184ec10347632e97d68fef2a | |
| parent | ba261c9e3bfce6a89ba74711ff69c13536fd70b1 (diff) | |
| download | cc4group-f9084d17239791334dbac304160f3e3759f0955d.tar.gz cc4group-f9084d17239791334dbac304160f3e3759f0955d.zip | |
Handle warnings in a more uniform way
| -rw-r--r-- | src/cc4group.c | 49 |
1 files changed, 32 insertions, 17 deletions
diff --git a/src/cc4group.c b/src/cc4group.c index fff46e7..3a78073 100644 --- a/src/cc4group.c +++ b/src/cc4group.c @@ -227,6 +227,18 @@ static char* cc4group_noerrorFormatter(int32_t const code, const char* const met return strdup("No Error"); } +// TODO: allow the user to set a warning callback with falling back to fprintf(stderr) by default +static void cc4group_warn(const CC4Group* const this, const char* const message, ...) +{ + (void)this; + va_list ap; + va_start(ap, message); + fputs("WARNING: ", stderr); + vfprintf(stderr, message, ap); + fputs("\n", stderr); + va_end (ap); +} + #define AddCleanUpJob(func, data) CleanUpJobListPrepend(this->cleanupJobs, (CC4Group_CleanupJob){(CC4Group_CleanupFunc)func, data}); static void memScrambleHeader(uint8_t* const data) @@ -365,13 +377,14 @@ typedef struct { size_t size; char fileName[sizeof(tmpFileTemplate) / sizeof(tmpFileTemplate[0])]; bool unlinkLater; + const CC4Group* group; } MunmapData; -static void cc4group_tryWarnUnlink(const char* const fileName) +static void cc4group_tryWarnUnlink(const CC4Group* const this, const char* const fileName) { if(unlink(fileName) == -1) { - fprintf(stderr, "WARNING: Removing file \"%s\" failed. Manual deletion is required", fileName); + cc4group_warn(this, "Removing file \"%s\" failed. Manual deletion is required", fileName); } } @@ -379,12 +392,12 @@ static void cc4group_unmapTmpMemoryFile(MunmapData* const data) { if(cc4group_munmap(data->addr, data->size) == -1) { - fprintf(stderr, "WARNING: munmap: Unmapping tempory file failed: %s\n", strerror(errno)); + cc4group_warn(data->group, "munmap: Unmapping tempory file failed: %s", strerror(errno)); } if(data->unlinkLater) { - cc4group_tryWarnUnlink(data->fileName); + cc4group_tryWarnUnlink(data->group, data->fileName); } free(data); @@ -423,6 +436,7 @@ static void* cc4group_createTmpMemoryFile(CC4Group* const this, const size_t siz { unmapData->addr = ret; unmapData->size = size; + unmapData->group = this; *cleanupJob = (CC4Group_CleanupJob){.func = (CC4Group_CleanupFunc)cc4group_unmapTmpMemoryFile, .data = unmapData}; } @@ -430,14 +444,14 @@ static void* cc4group_createTmpMemoryFile(CC4Group* const this, const size_t siz ret: if(tmpFile != -1 && close(tmpFile) == -1) { - fprintf(stderr, "WARNING: close: Closing tmp file failed: %s\n", strerror(errno)); + cc4group_warn(this, "close: Closing tmp file failed: %s", strerror(errno)); } if(ret == NULL) { if(unmapData->fileName[0] != '\0' && unmapData->unlinkLater) { - cc4group_tryWarnUnlink(unmapData->fileName); + cc4group_tryWarnUnlink(this, unmapData->fileName); } free(unmapData); } @@ -593,7 +607,7 @@ static void cc4group_cleanupReadState(CC4Group* const this) { if(!this->readState.callback.deinit(this->readState.callback.arg)) { - fprintf(stderr, "WARNING: cc4group_cleanupReadState: the deinitCallback failed\n"); + cc4group_warn(this, "cc4group_cleanupReadState: the deinitCallback failed"); } } @@ -866,7 +880,7 @@ ret: { if(!deinitCallback(callbackArg)) { - fprintf(stderr, "WARNING: cc4group_uncompressGroup: the deinitCallback failed\n"); + cc4group_warn(this, "cc4group_uncompressGroup: the deinitCallback failed"); } } @@ -924,6 +938,7 @@ ret: typedef struct { const void* data; size_t size; + const CC4Group* group; } CompleteDataReadCallbackArg; static bool cc4group_completeDataReadCallback(const void** const data, size_t* const size, void* callbackArg) @@ -1002,7 +1017,7 @@ static bool cc4group_openMemory(CC4Group* const this, const void* const compress assert(compressedData); assert(size); - CompleteDataReadCallbackArg data = {.data = compressedData, .size = size}; + CompleteDataReadCallbackArg data = {.data = compressedData, .size = size, .group = this}; return cc4group_uncompressGroup(this, cc4group_completeDataReadCallback, &data, memoryManagement, NULL, NULL); } @@ -1029,7 +1044,7 @@ static bool cc4group_cleanupMappedGroupFile(void* const arg) CompleteDataReadCallbackArg* data = arg; if(cc4group_munmap((void*)data->data, data->size) == -1) { - fprintf(stderr, "WARNING: munmap: Unmapping the group file failed: %s\n", strerror(errno)); + cc4group_warn(data->group, "munmap: Unmapping the group file failed: %s", strerror(errno)); } free(data); @@ -1120,7 +1135,7 @@ static bool cc4group_uncompressGroupFromFile(CC4Group* const this, const char* c goto ret; } - *data = (CompleteDataReadCallbackArg){.data = mappedFile, .size = size}; + *data = (CompleteDataReadCallbackArg){.data = mappedFile, .size = size, .group = this}; success = cc4group_uncompressGroup(this, cc4group_completeDataReadCallback, data, cc4group.MemoryManagement.Reference, NULL, cc4group_cleanupMappedGroupFile); mappedFile = MAP_FAILED; @@ -1129,7 +1144,7 @@ ret: { if(cc4group_munmap(mappedFile, size) == -1) { - fprintf(stderr, "WARNING: munmap: Unmapping the group file failed: %s\n", strerror(errno)); + cc4group_warn(this, "munmap: Unmapping the group file failed: %s", strerror(errno)); } } @@ -1492,7 +1507,7 @@ static bool cc4group_extractEntry(CC4Group* const this, const C4GroupEntryData* if(close(file) == -1) { - fprintf(stderr, "WARNING: close: Closing the extracted file \"%s\" failed: %s\n", targetPath, strerror(errno)); + cc4group_warn(this, "close: Closing the extracted file \"%s\" failed: %s", targetPath, strerror(errno)); } if(success) @@ -1500,7 +1515,7 @@ static bool cc4group_extractEntry(CC4Group* const this, const C4GroupEntryData* struct utimbuf times = {.actime = root->core.Modified, .modtime = root->core.Modified}; if(utime(targetPath, ×) == -1) { - fprintf(stderr, "WARNING: utime: Setting modification time for \"%s\" failed: %s\n", targetPath, strerror(errno)); + cc4group_warn(this, "utime: Setting modification time for \"%s\" failed: %s", targetPath, strerror(errno)); } } @@ -1557,7 +1572,7 @@ static bool cc4group_extractChildren(CC4Group* const this, const C4GroupEntryDat struct utimbuf times = {.actime = root->header->Creation, .modtime = root->header->Creation}; if(utime(tmpPath, ×) == -1) { - fprintf(stderr, "WARNING: utime: Setting modification time for \"%s\" failed: %s\n", tmpPath, strerror(errno)); + cc4group_warn(this, "utime: Setting modification time for \"%s\" failed: %s", tmpPath, strerror(errno)); } success = true; @@ -2246,7 +2261,7 @@ static bool cc4group_saveIt(CC4Group* const this, const char* const path, bool c { if(close(file) == -1) { - fprintf(stderr, "WARNING: close: Closing the target group file failed: %s\n", strerror(errno)); + cc4group_warn(this, "close: Closing the target group file failed: %s", strerror(errno)); } } @@ -2254,7 +2269,7 @@ static bool cc4group_saveIt(CC4Group* const this, const char* const path, bool c { if(unlink(path) == -1) { - fprintf(stderr, "WARNING: unlink: Deleting the incomplete group file failed: %s\n", strerror(errno)); + cc4group_warn(this, "unlink: Deleting the incomplete group file failed: %s", strerror(errno)); } } |
