Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Gottox committed Aug 10, 2024
1 parent 751cebe commit 794615e
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 63 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ containing a set of command line tools and a C library.
* [**fuse2**](https://libfuse.github.io/) *optional*: For mounting squashfs
archives on systems that don't support fuse3. e.g. OpenBSD.
* [**libcurl**](https://curl.se/) *optional*: For transparently reading squashfs
archives from the internet without downloading them first.
archives from an URL.

Note that to do anything useful with *libsqsh*, you need to have at least one of the
compression libraries enabled.
Expand Down
3 changes: 1 addition & 2 deletions libmksqsh/include/mksqsh_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ struct MksqshIdTable {
int mksqsh__id_table_init(
struct MksqshIdTable *table, FILE *content_output, FILE *lookup_output);

int mksqsh__id_table_add(
struct MksqshIdTable *table, uint64_t start, uint32_t size);
int mksqsh__id_table_add(struct MksqshIdTable *table, uint32_t id);

int mksqsh__id_table_flush(struct MksqshIdTable *table);

Expand Down
7 changes: 6 additions & 1 deletion libmksqsh/src/metablock/metablock_builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
* @file inode_builder.c
*/

#include "sqsh_error.h"
#define _DEFAULT_SOURCE

#include <mksqsh_metablock.h>
#include <sqsh_common_private.h>
#include <sqsh_data_set.h>
#include <sqsh_error.h>
#include <string.h>

int
Expand Down Expand Up @@ -110,6 +110,11 @@ mksqsh__metablock_flush(struct MksqshMetablock *metablock) {
return rv;
}

bool
mksqsh__metablock_was_flushed(const struct MksqshMetablock *metablock) {
return metablock->flushed;
}

int
mksqsh__metablock_cleanup(struct MksqshMetablock *metablock) {
(void)metablock;
Expand Down
9 changes: 5 additions & 4 deletions libmksqsh/src/table/id_table_builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
#include <mksqsh_table.h>
#include <sqsh_data_set.h>

#define ENTRY_SIZE sizeof(uint32_t)

int
mksqsh__id_table_init(
struct MksqshIdTable *table, FILE *content_output,
FILE *lookup_output) {
const size_t entry_size = sizeof(uint32_t);
return mksqsh__table_init(
&table->table, entry_size, content_output, lookup_output);
&table->table, ENTRY_SIZE, content_output, lookup_output);
}

int
mksqsh__id_table_write(struct MksqshIdTable *table, uint32_t id) {
mksqsh__id_table_add(struct MksqshIdTable *table, uint32_t id) {
uint32_t id_le = CX_CPU_2_LE32(id);

return mksqsh__table_add(&table->table, &id_le, sizeof(id_le));
return mksqsh__table_add(&table->table, &id_le, ENTRY_SIZE);
}

int
Expand Down
60 changes: 39 additions & 21 deletions libmksqsh/src/table/table_builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,47 +39,65 @@
#include <sqsh_common_private.h>
#include <sqsh_data_set.h>

static int
table_add_lookup(struct MksqshTable *table) {
int rv = 0;
const uint64_t ref = mksqsh__metablock_ref(&table->metablock_writer);
const uint64_t outer_ref = sqsh_address_ref_outer_offset(ref);
const uint64_t outer_ref_le = CX_CPU_2_LE64(outer_ref);
const unsigned long write =
fwrite(&outer_ref_le, sizeof(outer_ref_le), 1, table->output);
if (write != 1) {
rv = -1; // TODO: proper error code
goto out;
}
out:
return rv;
}

int
mksqsh__table_init(
struct MksqshTable *table, size_t entry_size, FILE *metablock_output,
FILE *output) {
assert(0 == entry_size % SQSH_METABLOCK_BLOCK_SIZE);
int rv = 0;
assert(0 == SQSH_METABLOCK_BLOCK_SIZE % entry_size);

table->entry_size = entry_size;
table->output = output;
table->metablock_output = metablock_output;
return mksqsh__metablock_init(&table->metablock_writer, metablock_output);

rv = mksqsh__metablock_init(&table->metablock_writer, metablock_output);
if (rv < 0) {
goto out;
}

rv = table_add_lookup(table);
if (rv < 0) {
goto out;
}

out:
return rv;
}

int
mksqsh__table_add(
struct MksqshTable *table, const void *entry, size_t entry_size) {
int rv = 0;
(void)table;
(void)entry;
(void)entry_size;
assert(entry_size == table->entry_size);

const uint64_t ref = mksqsh__metablock_ref(&table->metablock_writer);
const uint64_t outer_ref = sqsh_address_ref_outer_offset(ref);
const uint64_t inner_ref =
sqsh_address_ref_inner_offset(ref) / table->entry_size;

const uint64_t current_ref = sqsh_address_ref_create(outer_ref, inner_ref);
table->entry_count += 1;

const uint64_t current_ref_le = CX_CPU_2_LE64(current_ref);
const unsigned long write =
fwrite(&current_ref_le, sizeof(current_ref_le), 1, table->output);
if (write != 1) {
rv = -1; // TODO: proper error code
goto out;
}

rv = mksqsh__metablock_write(&table->metablock_writer, entry, entry_size);
if (rv < 0) {
goto out;
}

if (mksqsh__metablock_was_flushed(&table->metablock_writer)) {
rv = table_add_lookup(table);
if (rv < 0) {
goto out;
}
}

out:
return rv;
}
Expand Down
2 changes: 1 addition & 1 deletion test/libsqsh/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
/* We're using a ridiculously small block size to
* test the mappers ability to handle small blocks.
*/
#define DEFAULT_BLOCK_SIZE 1
#define DEFAULT_BLOCK_SIZE ~0

#define DEFAULT_CONFIG(s) \
(struct SqshConfig) { \
Expand Down
18 changes: 9 additions & 9 deletions test/libsqsh/directory/directory_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
UTEST(directory_iterator, iter_invalid_file_name_with_0) {
int rv;
struct SqshArchive archive = {0};
uint8_t payload[] = {
uint8_t payload[8192] = {
/* clang-format off */
SQSH_HEADER,
/* inode */
Expand Down Expand Up @@ -80,7 +80,7 @@ UTEST(directory_iterator, iter_invalid_file_name_with_0) {
UTEST(directory_iterator, iter_invalid_file_name_with_slash) {
int rv;
struct SqshArchive archive = {0};
uint8_t payload[] = {
uint8_t payload[8192] = {
/* clang-format off */
SQSH_HEADER,
/* inode */
Expand Down Expand Up @@ -116,7 +116,7 @@ UTEST(directory_iterator, iter_invalid_file_name_with_slash) {
UTEST(directory_iterator, iter_two_files) {
int rv;
struct SqshArchive archive = {0};
uint8_t payload[] = {
uint8_t payload[8192] = {
/* clang-format off */
SQSH_HEADER,
/* inode */
Expand Down Expand Up @@ -179,7 +179,7 @@ UTEST(directory_iterator, iter_two_files) {
UTEST(directory_iterator, iter_invalid_file_type) {
int rv;
struct SqshArchive archive = {0};
uint8_t payload[] = {
uint8_t payload[8192] = {
/* clang-format off */
SQSH_HEADER,
/* inode */
Expand Down Expand Up @@ -210,7 +210,7 @@ UTEST(directory_iterator, iter_invalid_file_type) {
UTEST(directory_iterator, iter_inconsistent_file_type) {
int rv;
struct SqshArchive archive = {0};
uint8_t payload[] = {
uint8_t payload[8192] = {
/* clang-format off */
SQSH_HEADER,
/* inode */
Expand Down Expand Up @@ -254,7 +254,7 @@ UTEST(directory_iterator, iter_inconsistent_file_type) {
UTEST(directory_iterator, iter_over_corrupt_header_too_small) {
int rv;
struct SqshArchive archive = {0};
uint8_t payload[] = {
uint8_t payload[8192] = {
/* clang-format off */
SQSH_HEADER,
/* inode */
Expand Down Expand Up @@ -292,7 +292,7 @@ UTEST(directory_iterator, iter_over_corrupt_header_too_small) {
UTEST(directory_iterator, iter_inode_overflow) {
int rv;
struct SqshArchive archive = {0};
uint8_t payload[] = {
uint8_t payload[8192] = {
/* clang-format off */
SQSH_HEADER,
/* inode */
Expand Down Expand Up @@ -328,7 +328,7 @@ UTEST(directory_iterator, iter_inode_overflow) {
UTEST(directory_iterator, iter_inode_underflow) {
int rv;
struct SqshArchive archive = {0};
uint8_t payload[] = {
uint8_t payload[8192] = {
/* clang-format off */
SQSH_HEADER,
/* inode */
Expand Down Expand Up @@ -364,7 +364,7 @@ UTEST(directory_iterator, iter_inode_underflow) {
UTEST(directory_iterator, iter_inode_to_zero) {
int rv;
struct SqshArchive archive = {0};
uint8_t payload[] = {
uint8_t payload[8192] = {
/* clang-format off */
SQSH_HEADER,
/* inode */
Expand Down
4 changes: 2 additions & 2 deletions test/libsqsh/easy/directory.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
UTEST(ease_directory, list_two_files) {
int rv = 0;
struct SqshArchive archive = {0};
uint8_t payload[] = {
uint8_t payload[8192] = {
/* clang-format off */
SQSH_HEADER,
/* inode */
Expand Down Expand Up @@ -82,7 +82,7 @@ UTEST(ease_directory, list_two_files) {
UTEST(ease_directory, list_two_paths) {
int rv = 0;
struct SqshArchive archive = {0};
uint8_t payload[] = {
uint8_t payload[8192] = {
/* clang-format off */
SQSH_HEADER,
/* inode */
Expand Down
12 changes: 6 additions & 6 deletions test/libsqsh/easy/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

UTEST(ease_file, test_file_get_content_through_symlink) {
struct SqshArchive archive = {0};
uint8_t payload[] = {
uint8_t payload[8192] = {
/* clang-format off */
SQSH_HEADER,
[1024] = '1', '2', '3', '4', '5', '6', '7', '8',
Expand Down Expand Up @@ -80,7 +80,7 @@ UTEST(ease_file, test_file_get_content_through_symlink) {
UTEST(ease_file, test_file_exists_through_dead_symlink) {
int rv = 0;
struct SqshArchive archive = {0};
uint8_t payload[] = {
uint8_t payload[8192] = {
/* clang-format off */
SQSH_HEADER,
[1024] = '1', '2', '3', '4', '5', '6', '7', '8',
Expand Down Expand Up @@ -111,7 +111,7 @@ UTEST(ease_file, test_file_exists_through_dead_symlink) {
UTEST(ease_file, test_file_exists_through_symlink) {
struct SqshArchive archive = {0};
int rv = 0;
uint8_t payload[] = {
uint8_t payload[8192] = {
/* clang-format off */
SQSH_HEADER,
[1024] = '1', '2', '3', '4', '5', '6', '7', '8',
Expand Down Expand Up @@ -152,7 +152,7 @@ UTEST(ease_file, test_file_exists_through_symlink) {
UTEST(ease_file, test_file_size) {
struct SqshArchive archive = {0};
int rv = 0;
uint8_t payload[] = {
uint8_t payload[8192] = {
/* clang-format off */
SQSH_HEADER,
/* inode */
Expand Down Expand Up @@ -186,7 +186,7 @@ UTEST(ease_file, test_file_size) {
UTEST(ease_file, test_file_permission) {
struct SqshArchive archive = {0};
int rv = 0;
uint8_t payload[] = {
uint8_t payload[8192] = {
/* clang-format off */
SQSH_HEADER,
/* inode */
Expand Down Expand Up @@ -220,7 +220,7 @@ UTEST(ease_file, test_file_permission) {
UTEST(ease_file, test_file_mtime) {
struct SqshArchive archive = {0};
int rv = 0;
uint8_t payload[] = {
uint8_t payload[8192] = {
/* clang-format off */
SQSH_HEADER,
/* inode */
Expand Down
28 changes: 28 additions & 0 deletions test/libsqsh/file/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
*/

#include "../common.h"
#include <stdint.h>
#include <utest.h>

#include <sqsh_archive_private.h>
Expand Down Expand Up @@ -158,7 +159,34 @@ UTEST(file, resolve_unkown_dir_inode) {
ASSERT_EQ(-SQSH_ERROR_INODE_PARENT_UNSET, rv);

sqsh_close(symlink);
sqsh__archive_cleanup(&archive);
}

UTEST(file, get_ids) {
int rv;
struct SqshArchive archive = {0};
struct SqshFile file = {0};
uint8_t payload[8192] = {
SQSH_HEADER,
/* inode */
[INODE_TABLE_OFFSET + 15] = METABLOCK_HEADER(0, 128),
INODE_HEADER(2, 0666, 0, 0, 4242, 1),
INODE_BASIC_FILE(1024, 0xFFFFFFFF, 0, 1),
UINT32_BYTES(42),
[ID_TABLE_OFFSET] = UINT64_BYTES(0),
METABLOCK_HEADER(0, 128),
UINT32_BYTES(0),
UINT32_BYTES(0),
};
mk_stub(&archive, payload, sizeof(payload));

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

ASSERT_EQ((uint32_t)123, sqsh_file_gid(&file));

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

Expand Down
Loading

0 comments on commit 794615e

Please sign in to comment.