diff options
| author | Markus Mittendrein <git@maxmitti.tk> | 2019-08-18 12:16:26 +0200 |
|---|---|---|
| committer | Markus Mittendrein <git@maxmitti.tk> | 2019-08-19 18:56:58 +0200 |
| commit | 228bce0f6612044a18569919798d7e89e18a29c8 (patch) | |
| tree | 0492e77c6635ea9b41675f186166bc3d924e09b3 /src | |
| parent | e29845499ab8831e7e81a02fd98d5853336889f1 (diff) | |
| download | cc4group-228bce0f6612044a18569919798d7e89e18a29c8.tar.gz cc4group-228bce0f6612044a18569919798d7e89e18a29c8.zip | |
Fix losing data due to a mistake in the lazy implementation
Diffstat (limited to 'src')
| -rw-r--r-- | src/cc4group.c | 94 |
1 files changed, 45 insertions, 49 deletions
diff --git a/src/cc4group.c b/src/cc4group.c index 6766745..df8854a 100644 --- a/src/cc4group.c +++ b/src/cc4group.c @@ -640,10 +640,9 @@ static bool cc4group_uncompressUntilPosition(CC4Group* const this, size_t const } assert(rootGroup); - assert(rootGroup->readState.active); assert(position <= rootGroup->uncompressedSize); - if(rootGroup->readState.position < position) + if(rootGroup->readState.active && rootGroup->readState.position < position) { rootGroup->readState.gzStrm.avail_out = position - rootGroup->readState.position; @@ -2041,63 +2040,60 @@ static void cc4group_mmappedFileManagementEnd(void* const data, void* const arg) static const uint8_t* cc4group_getOnlyEntryData(CC4Group* const this, const C4GroupEntryData* entry) { - if(entry->data == NULL) + if(this->lazy) { - if(entry->path != NULL) + if(!cc4group_uncompressUntilPosition(this, entry->absolutePosition + entry->core.Size)) { - int file = open(entry->path, O_RDONLY | O_BINARY); - if(file == -1) - { - SET_ERRNO_ERROR("open: Opening the needed file"); - return NULL; - } - - void* mapped = cc4group_mmap(NULL, entry->core.Size, PROT_READ, MAP_PRIVATE, file, 0); - if(mapped == MAP_FAILED) - { - SET_ERRNO_ERROR("mmap: Mapping the file to add/load from disk"); - if(close(file) == -1) - { - cc4group_warn(this, "close: after mmapping the file \"%s\" failed, closing it failed too: %s", entry->path, strerror(errno)); - } - return NULL; - } - - CC4GroupMMappedFileManagement* management = malloc(sizeof(*management)); - if(management == NULL) - { - SET_ERRNO_ERROR("malloc: allocating memory for temporary memory management"); - return NULL; - } + return NULL; + } - *management = (CC4GroupMMappedFileManagement){ - .management = { - .start = cc4group_mmappedFileManagementStart, - .end = cc4group_mmappedFileManagementEnd, - .arg = management - }, - .size = entry->core.Size, - .fd = -1, - .group = this - }; + ((C4GroupEntryData*)entry)->data = this->uncompressedData + entry->absolutePosition; + } + else if(entry->path != NULL && entry->data == NULL) + { + int file = open(entry->path, O_RDONLY | O_BINARY); + if(file == -1) + { + SET_ERRNO_ERROR("open: Opening the needed file"); + return NULL; + } + void* mapped = cc4group_mmap(NULL, entry->core.Size, PROT_READ, MAP_PRIVATE, file, 0); + if(mapped == MAP_FAILED) + { + SET_ERRNO_ERROR("mmap: Mapping the file to add/load from disk"); if(close(file) == -1) { - management->fd = file; + cc4group_warn(this, "close: after mmapping the file \"%s\" failed, closing it failed too: %s", entry->path, strerror(errno)); } - - ((C4GroupEntryData*)entry)->data = mapped; - ((C4GroupEntryData*)entry)->memoryManagement = &management->management; + return NULL; } - else + + CC4GroupMMappedFileManagement* management = malloc(sizeof(*management)); + if(management == NULL) { - if(!cc4group_uncompressUntilPosition(this, entry->absolutePosition + entry->core.Size)) - { - return NULL; - } + SET_ERRNO_ERROR("malloc: allocating memory for temporary memory management"); + return NULL; + } - ((C4GroupEntryData*)entry)->data = this->uncompressedData + entry->absolutePosition; + *management = (CC4GroupMMappedFileManagement){ + .management = { + .start = cc4group_mmappedFileManagementStart, + .end = cc4group_mmappedFileManagementEnd, + .arg = management + }, + .size = entry->core.Size, + .fd = -1, + .group = this + }; + + if(close(file) == -1) + { + management->fd = file; } + + ((C4GroupEntryData*)entry)->data = mapped; + ((C4GroupEntryData*)entry)->memoryManagement = &management->management; } return entry->data; @@ -2380,7 +2376,7 @@ static bool cc4group_deflateToCallback(WriteCallback* const callback, const void static bool cc4group_writeEntries(CC4Group* const this, C4GroupEntryData* const entryData, WriteCallback* const callback) { - const C4GroupHeader* origHeader = (const C4GroupHeader*)cc4group_getOnlyEntryData(this, entryData); + const C4GroupHeader* origHeader = cc4group_getHeader(this, entryData); if(origHeader == NULL) { return false; |
