diff --git a/core/blob_list.c b/core/blob_list.c index a0173f8..d7d254b 100644 --- a/core/blob_list.c +++ b/core/blob_list.c @@ -3,11 +3,25 @@ * Provided under the BSD-3 clause */ +#include #include #include #include "blobchain/blob.h" #include "blobchain/memlib.h" +static void +dump_bytes(void *buf, size_t len) +{ + char *p = buf; + size_t i = 0; + + while ((i++) < len) { + printf("%02X ", p[i] & 0xFF); + } + + printf("\n"); +} + int blob_list_init(struct blob_list *lp) { @@ -65,3 +79,27 @@ blob_list_destroy(struct blob_list *lp) free(tmp); } } + +void +blob_list_dump(struct blob_list *lp) +{ + const size_t DUMP_SIZE = 4; + struct blob *blob; + size_t count = 0; + + if (lp == NULL) { + return; + } + + blob = lp->head; + while (blob != NULL) { + printf("blob %zd/%zd\n", count++, lp->count); + printf("... length :: %zd\n", blob->length); + printf("... data :: "); + dump_bytes(blob->data, blob->length < DUMP_SIZE + ? blob->length + : DUMP_SIZE); + + blob = blob->next; + } +} diff --git a/core/blobchain.c b/core/blobchain.c index 60b3be0..834b1f5 100644 --- a/core/blobchain.c +++ b/core/blobchain.c @@ -12,6 +12,7 @@ #include #include "blobchain/common.h" #include "blobchain/stream.h" +#include "blobchain/blob.h" /* Input directory path */ static char *input_dir = NULL; @@ -32,11 +33,13 @@ version(void) } static void -pack_foreach(const char *input_dir) +pack_foreach(struct blob_list *blobs, const char *input_dir) { + struct data_stream stream; char pathbuf[512]; struct dirent *dirent; DIR *dir; + int error; if (input_dir == NULL) { return; @@ -55,12 +58,27 @@ pack_foreach(const char *input_dir) switch (dirent->d_type) { case DT_DIR: snprintf(pathbuf, sizeof(pathbuf), "%s/%s", input_dir, dirent->d_name); - pack_foreach(pathbuf); + pack_foreach(blobs, pathbuf); printf("[d] %s\n", pathbuf); break; case DT_REG: snprintf(pathbuf, sizeof(pathbuf), "%s/%s", input_dir, dirent->d_name); printf("[r] %s\n", pathbuf); + + error = stream_from_file(&stream, pathbuf); + if (error < 0) { + printf("fatal: could not create stream from '%s'\n", pathbuf); + return; + } + + error = blob_list_append(blobs, blob_allocate(&stream)); + if (error < 0) { + printf("fatal: failed to append blob '%s'\n", pathbuf); + stream_destroy(&stream); + continue; + } + + stream_destroy(&stream); break; } } @@ -70,6 +88,7 @@ static void pack_dir(void) { struct stat sb; + struct blob_list blobs; if (stat(input_dir, &sb) < 0) { perror("stat"); @@ -82,7 +101,16 @@ pack_dir(void) return; } - pack_foreach(input_dir); + if (blob_list_init(&blobs) < 0) { + printf("fatal: failed to initialize blob list...\n"); + return; + } + + pack_foreach(&blobs, input_dir); +#if BLOBCHAIN_DEBUG + blob_list_dump(&blobs); +#endif /* BLOBCHAIN_DEBUG */ + blob_list_destroy(&blobs); } int diff --git a/core/stream_file.c b/core/stream_file.c index 740141c..e169bbb 100644 --- a/core/stream_file.c +++ b/core/stream_file.c @@ -66,6 +66,11 @@ stream_destroy(struct data_stream *stmp) return; } + if (stmp->data == NULL) { + return; + } + free(stmp->data); stmp->length = 0; + stmp->data = NULL; } diff --git a/inc/blobchain/blob.h b/inc/blobchain/blob.h index c0b9639..df4562c 100644 --- a/inc/blobchain/blob.h +++ b/inc/blobchain/blob.h @@ -67,4 +67,11 @@ int blob_list_init(struct blob_list *lp); */ void blob_list_destroy(struct blob_list *lp); +/* + * Dump a blob list for debugging purposes + * + * @lp: Blob list to dump + */ +void blob_list_dump(struct blob_list *lp); + #endif /* !BLOBCHAIN_BLOB_H */ diff --git a/inc/blobchain/common.h b/inc/blobchain/common.h index 25739e5..1505c85 100644 --- a/inc/blobchain/common.h +++ b/inc/blobchain/common.h @@ -6,6 +6,7 @@ #ifndef BLOBCHAIN_COMMON_H #define BLOBCHAIN_COMMON_H 1 +#define BLOBCHAIN_DEBUG 1 #define BLOBCHAIN_VERSION "0.0.1" #endif /* !BLOBCHAIN_COMMON_H */