Skip to content

Commit

Permalink
test: add check for copying file_iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
Gottox committed Aug 20, 2024
1 parent 2339950 commit 4b8b952
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 23 deletions.
27 changes: 17 additions & 10 deletions common/src/reader/reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,31 +71,38 @@ sqsh__reader_copy(
void *copied_iterator) {
int rv = 0;

const struct SqshReaderIteratorImpl *impl = source->iterator_impl;
if (impl == NULL) {
goto out;

Check warning on line 76 in common/src/reader/reader.c

View check run for this annotation

Codecov / codecov/patch

common/src/reader/reader.c#L76

Added line #L76 was not covered by tests
}
target->iterator = copied_iterator;
target->iterator_impl = source->iterator_impl;
target->iterator_impl = impl;
target->iterator_offset = source->iterator_offset;
target->offset = source->offset;

const uint8_t *buffer_data = cx_buffer_data(&source->buffer);
const size_t buffer_size = cx_buffer_size(&source->buffer);
rv = cx_buffer_init(&target->buffer);
if (rv < 0) {
goto out;

Check warning on line 87 in common/src/reader/reader.c

View check run for this annotation

Codecov / codecov/patch

common/src/reader/reader.c#L87

Added line #L87 was not covered by tests
}
rv = cx_buffer_append(
&target->buffer, cx_buffer_data(&source->buffer),
cx_buffer_size(&source->buffer));
if (rv < 0) {
goto out;
if (buffer_size > 0) {
rv = cx_buffer_append(
&target->buffer, cx_buffer_data(&source->buffer),
cx_buffer_size(&source->buffer));
if (rv < 0) {
goto out;

Check warning on line 94 in common/src/reader/reader.c

View check run for this annotation

Codecov / codecov/patch

common/src/reader/reader.c#L94

Added line #L94 was not covered by tests
}
}
buffer_data = cx_buffer_data(&target->buffer);
const uint8_t *iterator_data = target->iterator_impl->data(copied_iterator);
const size_t iterator_size = target->iterator_impl->size(copied_iterator);
const uint8_t *iterator_data = impl->data(copied_iterator);
const size_t iterator_size = impl->size(copied_iterator);
const uint8_t *data = source->data;

if (data == NULL) {
target->data = NULL;

Check warning on line 102 in common/src/reader/reader.c

View check run for this annotation

Codecov / codecov/patch

common/src/reader/reader.c#L102

Added line #L102 was not covered by tests
} else if (data >= buffer_data && data < buffer_data + buffer_size) {
target->data = &buffer_data[data - buffer_data];
const uint8_t *target_buffer_data = cx_buffer_data(&target->buffer);
target->data = &target_buffer_data[data - buffer_data];
} else if (data >= iterator_data && data < iterator_data + iterator_size) {
target->data = &iterator_data[data - iterator_data];
} else {
Expand Down
6 changes: 3 additions & 3 deletions libsqsh/include/sqsh_extract_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ SQSH_NO_EXPORT SQSH_NO_UNUSED int sqsh__extract_manager_init(
*/
SQSH_NO_EXPORT int sqsh__extract_manager_uncompress(
struct SqshExtractManager *manager, const struct SqshMapReader *reader,
const struct CxBuffer **target);
struct CxBuffer **target);

/**
* @internal
Expand All @@ -226,7 +226,7 @@ SQSH_NO_EXPORT int sqsh__extract_manager_uncompress(
* @return 0 on success, a negative value on error.
*/
SQSH_NO_EXPORT int sqsh__extract_manager_retain_buffer(
struct SqshExtractManager *manager, const struct CxBuffer *buffer);
struct SqshExtractManager *manager, struct CxBuffer *buffer);

/**
* @internal
Expand Down Expand Up @@ -265,7 +265,7 @@ struct SqshExtractView {
* @privatesection
*/
struct SqshExtractManager *manager;
const struct CxBuffer *buffer;
struct CxBuffer *buffer;
uint64_t address;
size_t size;
};
Expand Down
13 changes: 13 additions & 0 deletions libsqsh/include/sqsh_file_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,19 @@ struct SqshFileIterator {
SQSH_NO_EXPORT SQSH_NO_UNUSED int sqsh__file_iterator_init(
struct SqshFileIterator *iterator, const struct SqshFile *file);

/**
* @internal
* @memberof SqshFileIterator
* @brief Creates a copy of a file iterator.
*
* @param[out] target The iterator to copy to.
* @param[in] source The iterator to copy from.
*
* @return 0 on success, less than 0 on error.
*/
SQSH_NO_EXPORT SQSH_NO_UNUSED int sqsh__file_iterator_copy(
struct SqshFileIterator *target, const struct SqshFileIterator *source);

/**
* @internal
* @memberof SqshFileIterator
Expand Down
12 changes: 12 additions & 0 deletions libsqsh/include/sqsh_mapper_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,18 @@ SQSH_NO_EXPORT SQSH_NO_UNUSED int sqsh__map_manager_get(
struct SqshMapManager *manager, sqsh_index_t index,
const struct SqshMapSlice **target);

/**
* @internal
* @memberof SqshMapManager
* @brief Retains a slice for a chunk.
*
* @param[in] manager The SqshMapManager instance.
* @param[in] mapping The mapping to retain.
* @return Returns 0 on success, a negative value on error.
*/
SQSH_NO_EXPORT SQSH_NO_UNUSED int sqsh__map_manager_retain(
struct SqshMapManager *manager, const struct SqshMapSlice *mapping);

/**
* @internal
* @memberof SqshMapManager
Expand Down
6 changes: 3 additions & 3 deletions libsqsh/src/extract/extract_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ extract(struct SqshExtractManager *manager, const struct SqshMapReader *reader,
int
sqsh__extract_manager_uncompress(
struct SqshExtractManager *manager, const struct SqshMapReader *reader,
const struct CxBuffer **target) {
struct CxBuffer **target) {
int rv = 0;
bool locked = false;
struct CxBuffer *buffer = NULL;
Expand Down Expand Up @@ -174,8 +174,8 @@ sqsh__extract_manager_uncompress(

int
sqsh__extract_manager_retain_buffer(
struct SqshExtractManager *manager, const struct CxBuffer *buffer) {
cx_rc_radix_tree_retain_value(&manager->cache, (void *)buffer);
struct SqshExtractManager *manager, struct CxBuffer *buffer) {
cx_rc_radix_tree_retain_value(&manager->cache, buffer);
return 0;
}

Expand Down
1 change: 1 addition & 0 deletions libsqsh/src/extract/extract_view.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ sqsh__extract_view_copy(
}
target->buffer = source->buffer;
target->size = source->size;
target->address = source->address;
out:
if (rv < 0) {
sqsh__extract_view_cleanup(target);

Check warning on line 80 in libsqsh/src/extract/extract_view.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/extract/extract_view.c#L80

Added line #L80 was not covered by tests
Expand Down
3 changes: 3 additions & 0 deletions libsqsh/src/file/fragment_view.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ sqsh__fragment_view_copy(
const struct SqshFragmentView *source) {
int rv = 0;
target->fragment_table = source->fragment_table;
if (target->fragment_table == NULL) {
goto out;
}
rv = sqsh__map_reader_copy(&target->map_reader, &source->map_reader);

Check warning on line 169 in libsqsh/src/file/fragment_view.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/file/fragment_view.c#L169

Added line #L169 was not covered by tests
if (rv < 0) {
goto out;

Check warning on line 171 in libsqsh/src/file/fragment_view.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/file/fragment_view.c#L171

Added line #L171 was not covered by tests
Expand Down
1 change: 1 addition & 0 deletions libsqsh/src/mapper/map_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ sqsh__map_iterator_copy(
target->map_manager = source->map_manager;
// TODO
// rv = sqsh__map_slice_copy(target->mapping, source->mapping);
rv = sqsh__map_manager_retain(target->map_manager, target->mapping);
if (rv < 0) {
goto out;

Check warning on line 80 in libsqsh/src/mapper/map_iterator.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/map_iterator.c#L80

Added line #L80 was not covered by tests
}
Expand Down
4 changes: 2 additions & 2 deletions libsqsh/src/mapper/map_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ sqsh__map_manager_get(
}

int
sqsh__map_manager_retain_slice(
sqsh__map_manager_retain(
struct SqshMapManager *manager, const struct SqshMapSlice *mapping) {
if (manager == NULL) {
if (manager == NULL || mapping == NULL) {
return 0;
}
int rv = sqsh__mutex_lock(&manager->lock);

Check warning on line 175 in libsqsh/src/mapper/map_manager.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/map_manager.c#L175

Added line #L175 was not covered by tests
Expand Down
6 changes: 3 additions & 3 deletions test/libsqsh/extract/extract_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ UTEST(directory_iterator, decompress) {
int rv;
struct SqshArchive archive = {0};
struct SqshExtractManager manager = {0};
const struct CxBuffer *buffer = NULL;
struct CxBuffer *buffer = NULL;
uint8_t payload[8192] = {SQSH_HEADER, ZLIB_ABCD};

mk_stub(&archive, payload, sizeof(payload));
Expand Down Expand Up @@ -78,8 +78,8 @@ UTEST(directory_iterator, decompress_and_cached) {
int rv;
struct SqshArchive archive = {0};
struct SqshExtractManager manager = {0};
const struct CxBuffer *buffer = NULL;
const struct CxBuffer *cached_buffer = NULL;
struct CxBuffer *buffer = NULL;
struct CxBuffer *cached_buffer = NULL;
uint8_t payload[8192] = {SQSH_HEADER, ZLIB_ABCD};

mk_stub(&archive, payload, sizeof(payload));
Expand Down
71 changes: 69 additions & 2 deletions test/libsqsh/integration.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ UTEST(integration, sqsh_cat_fragment) {
size = sqsh_file_size(file);
ASSERT_EQ((size_t)2, size);

rv = sqsh_file_reader_advance(&reader, 0, size);
rv = sqsh_file_reader_advance2(&reader, 0, size);
ASSERT_EQ(0, rv);

data = sqsh_file_reader_data(&reader);
Expand Down Expand Up @@ -546,7 +546,7 @@ multithreaded_resolver(void *arg) {
} else {
struct SqshFileReader *reader = sqsh_file_reader_new(file, &rv);
size_t size = sqsh_file_size(file);
rv = sqsh_file_reader_advance(reader, 0, size);
rv = sqsh_file_reader_advance2(reader, 0, size);
assert(rv == 0);
sqsh_file_reader_free(reader);
}
Expand Down Expand Up @@ -804,4 +804,71 @@ UTEST(integration, mmap) {
ASSERT_EQ(0, rv);
}

UTEST(integration, copy_iterator_newly) {
int rv;
struct SqshArchive sqsh = {0};
struct SqshFile *file = NULL;
struct SqshFileIterator iter = {0};
struct SqshFileIterator copy_iter = {0};

struct SqshConfig config = DEFAULT_CONFIG(TEST_SQUASHFS_IMAGE_LEN);
config.archive_offset = 1010;
rv = sqsh__archive_init(&sqsh, (char *)TEST_SQUASHFS_IMAGE, &config);
ASSERT_EQ(0, rv);

file = sqsh_open(&sqsh, "/b", &rv);
ASSERT_EQ(0, rv);

rv = sqsh__file_iterator_init(&iter, file);
ASSERT_EQ(0, rv);

rv = sqsh__file_iterator_copy(&copy_iter, &iter);
ASSERT_EQ(0, rv);

rv = sqsh_close(file);
ASSERT_EQ(0, rv);

rv = sqsh__archive_cleanup(&sqsh);
ASSERT_EQ(0, rv);
}

UTEST(integration, copy_iterator_iterated) {
int rv;
struct SqshArchive sqsh = {0};
struct SqshFile *file = NULL;
struct SqshFileIterator iter = {0};
struct SqshFileIterator copy_iter = {0};

struct SqshConfig config = DEFAULT_CONFIG(TEST_SQUASHFS_IMAGE_LEN);
config.archive_offset = 1010;
rv = sqsh__archive_init(&sqsh, (char *)TEST_SQUASHFS_IMAGE, &config);
ASSERT_EQ(0, rv);

file = sqsh_open(&sqsh, "/b", &rv);
ASSERT_EQ(0, rv);

rv = sqsh__file_iterator_init(&iter, file);
ASSERT_EQ(0, rv);

bool has_next = sqsh_file_iterator_next(&iter, 1, &rv);
ASSERT_LE(0, rv);
ASSERT_TRUE(has_next);
rv = 0;

rv = sqsh__file_iterator_copy(&copy_iter, &iter);
ASSERT_EQ(0, rv);

rv = sqsh__file_iterator_cleanup(&iter);
ASSERT_EQ(0, rv);

rv = sqsh__file_iterator_cleanup(&copy_iter);
ASSERT_EQ(0, rv);

rv = sqsh_close(file);
ASSERT_EQ(0, rv);

rv = sqsh__archive_cleanup(&sqsh);
ASSERT_EQ(0, rv);
}

UTEST_MAIN()

0 comments on commit 4b8b952

Please sign in to comment.