Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Gottox committed Aug 12, 2024
1 parent ed8da5d commit 0d929df
Showing 1 changed file with 48 additions and 18 deletions.
66 changes: 48 additions & 18 deletions libsqsh/src/extract/extract_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,58 +125,88 @@ sqsh__extract_manager_init(
return rv;
}

static int
extract(struct SqshExtractManager *manager, const struct SqshMapReader *reader,
struct CxBuffer *buffer) {
int rv = 0;
struct SqshExtractor extractor = {0};
const struct SqshExtractorImpl *extractor_impl = manager->extractor_impl;
const uint32_t block_size = manager->block_size;
const size_t size = sqsh__map_reader_size(reader);

rv = cx_buffer_init(buffer);
if (rv < 0) {
goto out;
}
const uint8_t *data = sqsh__map_reader_data(reader);

rv = sqsh__extractor_init(&extractor, buffer, extractor_impl, block_size);
if (rv < 0) {
goto out;
}

rv = sqsh__extractor_write(&extractor, data, size);
if (rv < 0) {
goto out;
}

rv = sqsh__extractor_finish(&extractor);
if (rv < 0) {
goto out;
}

out:
if (rv < 0) {
cx_buffer_cleanup(buffer);
}
sqsh__extractor_cleanup(&extractor);
return rv;
}

int
sqsh__extract_manager_uncompress(
struct SqshExtractManager *manager, const struct SqshMapReader *reader,
const struct CxBuffer **target) {
int rv = 0;
struct SqshExtractor extractor = {0};
const struct SqshExtractorImpl *extractor_impl = manager->extractor_impl;
const uint32_t block_size = manager->block_size;
bool locked = false;

rv = sqsh__mutex_lock(&manager->lock);
if (rv < 0) {
goto out;
}
locked = true;

const uint64_t address = sqsh__map_reader_address(reader);
const size_t size = sqsh__map_reader_size(reader);

*target = cx_rc_hash_map_retain(&manager->hash_map, address);

if (*target == NULL) {
struct CxBuffer buffer = {0};
rv = cx_buffer_init(&buffer);
if (rv < 0) {
goto out;
}
const uint8_t *data = sqsh__map_reader_data(reader);

rv = sqsh__extractor_init(
&extractor, &buffer, extractor_impl, block_size);
rv = sqsh__mutex_unlock(&manager->lock);
if (rv < 0) {
goto out;
}
locked = false;

rv = sqsh__extractor_write(&extractor, data, size);
rv = extract(manager, reader, &buffer);
if (rv < 0) {
cx_buffer_cleanup(&buffer);
goto out;
}

rv = sqsh__extractor_finish(&extractor);
rv = sqsh__mutex_lock(&manager->lock);
if (rv < 0) {
cx_buffer_cleanup(&buffer);
goto out;
}
locked = true;

*target = cx_rc_hash_map_put(&manager->hash_map, address, &buffer);
}
rv = cx_lru_touch(&manager->lru, address);

out:
sqsh__extractor_cleanup(&extractor);
sqsh__mutex_unlock(&manager->lock);
if (locked) {
sqsh__mutex_unlock(&manager->lock);
}
return rv;
}

Expand Down

0 comments on commit 0d929df

Please sign in to comment.