Skip to content

Commit

Permalink
metablock_iterator: return bool instead of int
Browse files Browse the repository at this point in the history
This commit changes the return type of sqsh__metablock_iterator_next()
from int to bool. The return value is now used to indicate whether
there is more data to read from the metablock iterator. The error code
is now stored in a pointer to an int instead of being returned directly.
  • Loading branch information
Gottox committed Aug 21, 2023
1 parent f618c61 commit 4a098e5
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 23 deletions.
12 changes: 8 additions & 4 deletions include/sqsh_metablock_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,15 @@ SQSH_NO_EXPORT SQSH_NO_UNUSED int sqsh__metablock_iterator_init(
* @memberof SqshMetablockIterator
* @brief Moves the iterator to the next metablock.
*
* @param[in,out] iterator The iterator to advance.
* @return 0 on success, or a negative value on error.
* @param[in,out] iterator The iterator to advance.
* @param[out] err Pointer to an int where the error code will be
* stored.
*
* @retval true If the iterator was moved to the next metablock
* @retval false If an error occured. See err for details.
*/
SQSH_NO_EXPORT SQSH_NO_UNUSED int
sqsh__metablock_iterator_next(struct SqshMetablockIterator *iterator);
SQSH_NO_EXPORT bool
sqsh__metablock_iterator_next(struct SqshMetablockIterator *iterator, int *err);

/**
* @internal
Expand Down
2 changes: 1 addition & 1 deletion lib/archive/compression_options.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ sqsh__compression_options_init(
goto out;
}

rv = sqsh__metablock_iterator_next(&context->metablock);
sqsh__metablock_iterator_next(&context->metablock, &rv);
if (rv < 0) {
goto out;
}
Expand Down
16 changes: 12 additions & 4 deletions lib/metablock/metablock_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ sqsh__metablock_iterator_init(
return rv;
}

int
sqsh__metablock_iterator_next(struct SqshMetablockIterator *iterator) {
bool
sqsh__metablock_iterator_next(
struct SqshMetablockIterator *iterator, int *err) {
int rv = 0;

sqsh__extract_view_cleanup(&iterator->extract_view);
Expand Down Expand Up @@ -114,9 +115,16 @@ sqsh__metablock_iterator_next(struct SqshMetablockIterator *iterator) {
iterator->data = sqsh__map_reader_data(&iterator->reader);
iterator->inner_size = iterator->outer_size;
}
rv = sqsh__metablock_iterator_size(iterator);

rv = 0;
out:
return rv;
if (err != NULL) {
*err = rv;
}
/* metablock iterators have no way to see if there is a next block. this
* needs to be by the caller.
*/
return rv == 0;
}

const uint8_t *
Expand Down
4 changes: 1 addition & 3 deletions lib/metablock/metablock_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@
static bool
metablock_iterator_next(void *iterator, size_t desired_size, int *err) {
(void)desired_size;
int rv = sqsh__metablock_iterator_next(iterator);
*err = rv;
return rv > 0;
return sqsh__metablock_iterator_next(iterator, err);
}
static const uint8_t *
metablock_iterator_data(const void *iterator) {
Expand Down
29 changes: 18 additions & 11 deletions test/metablock/metablock_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ next_once(void) {
&iter, &sqsh, SQSH_SIZEOF_SUPERBLOCK, sizeof(payload));
assert(rv == 0);

rv = sqsh__metablock_iterator_next(&iter);
assert(rv == 4);
bool has_next = sqsh__metablock_iterator_next(&iter, &rv);
assert(rv == 0);
assert(has_next);


assert(sqsh__metablock_iterator_size(&iter) == 4);

Expand Down Expand Up @@ -84,8 +86,9 @@ next_failing_with_no_compression(void) {
&iter, &sqsh, SQSH_SIZEOF_SUPERBLOCK, sizeof(payload));
assert(rv == 0);

rv = sqsh__metablock_iterator_next(&iter);
bool has_next = sqsh__metablock_iterator_next(&iter, &rv);
assert(rv != 0);
assert(!has_next);

rv = sqsh__metablock_iterator_cleanup(&iter);
assert(rv == 0);
Expand Down Expand Up @@ -114,17 +117,19 @@ next_twice(void) {
&iter, &sqsh, SQSH_SIZEOF_SUPERBLOCK, sizeof(payload));
assert(rv == 0);

rv = sqsh__metablock_iterator_next(&iter);
assert(rv == 4);
bool has_next = sqsh__metablock_iterator_next(&iter, &rv);
assert(rv == 0);
assert(has_next);

assert(sqsh__metablock_iterator_size(&iter) == 4);

p = sqsh__metablock_iterator_data(&iter);
assert(p != NULL);
assert(memcmp(p, "abcd", 4) == 0);

rv = sqsh__metablock_iterator_next(&iter);
assert(rv == 4);
has_next = sqsh__metablock_iterator_next(&iter, &rv);
assert(rv == 0);
assert(has_next);

assert(sqsh__metablock_iterator_size(&iter) == 4);

Expand Down Expand Up @@ -156,17 +161,19 @@ next_compressed(void) {
&iter, &sqsh, SQSH_SIZEOF_SUPERBLOCK, sizeof(payload));
assert(rv == 0);

rv = sqsh__metablock_iterator_next(&iter);
assert(rv == 4);
bool has_next = sqsh__metablock_iterator_next(&iter, &rv);
assert(rv == 0);
assert(has_next);

assert(sqsh__metablock_iterator_size(&iter) == 4);

p = sqsh__metablock_iterator_data(&iter);
assert(p != NULL);
assert(memcmp(p, "abcd", 4) == 0);

rv = sqsh__metablock_iterator_next(&iter);
assert(rv == 4);
has_next = sqsh__metablock_iterator_next(&iter, &rv);
assert(rv == 0);
assert(has_next);

assert(sqsh__metablock_iterator_size(&iter) == 4);

Expand Down

0 comments on commit 4a098e5

Please sign in to comment.