Skip to content

Commit

Permalink
Merge pull request #110 from Gottox/fix/error-when-iterator-is-not-file
Browse files Browse the repository at this point in the history
file_iterator: return correct error when iterator is not a file
  • Loading branch information
Gottox authored Sep 3, 2023
2 parents 6d1c745 + 2008335 commit fe3867d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/file/file_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,18 @@ int
sqsh__file_iterator_init(
struct SqshFileIterator *iterator, const struct SqshFile *file) {
int rv = 0;
enum SqshFileType file_type = sqsh_file_type(file);
if (file_type != SQSH_FILE_TYPE_FILE) {
rv = -SQSH_ERROR_NOT_A_FILE;
goto out;
}

struct SqshArchive *archive = file->archive;
const struct SqshSuperblock *superblock = sqsh_archive_superblock(archive);
struct SqshMapManager *map_manager = sqsh_archive_map_manager(archive);
uint64_t block_address = sqsh_file_blocks_start(file);
const uint64_t upper_limit = sqsh_superblock_bytes_used(superblock);

rv = sqsh__archive_data_extract_manager(
archive, &iterator->compression_manager);
if (rv < 0) {
Expand Down
33 changes: 33 additions & 0 deletions test/file/file_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,38 @@ load_two_zero_blocks(void) {
sqsh__archive_cleanup(&archive);
}

static void
open_directory_with_file_iterator(void) {
int rv;
struct SqshArchive archive = {0};
struct SqshFile file = {0};
uint8_t payload[8192] = {
/* clang-format off */
SQSH_HEADER,
/* inode */
[INODE_TABLE_OFFSET] = METABLOCK_HEADER(0, 128),
INODE_HEADER(1, 0, 0, 0, 0, 1),
INODE_BASIC_DIR(512, 104, 0, 0),
/* clang-format on */
};
mk_stub(&archive, payload, sizeof(payload));

uint64_t inode_ref = sqsh_address_ref_create(0, 0);
rv = sqsh__file_init(&file, &archive, inode_ref);
assert(rv == 0);

assert(sqsh_file_type(&file) == SQSH_FILE_TYPE_DIRECTORY);
assert(sqsh_file_has_fragment(&file) == false);

struct SqshFileIterator iter = {0};
rv = sqsh__file_iterator_init(&iter, &file);
assert(rv == -SQSH_ERROR_NOT_A_FILE);

sqsh__file_iterator_cleanup(&iter);
sqsh__file_cleanup(&file);
sqsh__archive_cleanup(&archive);
}

DECLARE_TESTS
TEST(load_two_segments_from_uncompressed_data_block)
TEST(load_segment_from_uncompressed_data_block)
Expand All @@ -491,4 +523,5 @@ TEST(load_zero_padding)
TEST(load_zero_big_padding)
TEST(load_zero_block)
TEST(load_two_zero_blocks)
TEST(open_directory_with_file_iterator)
END_TESTS

0 comments on commit fe3867d

Please sign in to comment.