Compare commits

...

2 Commits

Author SHA1 Message Date
chloe 1d4bb8769a core: Add files to blob list on scan
Signed-off-by: Chloe M. <chloe@mirocom.org>
2026-04-30 21:12:13 -04:00
chloe d991988fd8 core: blob: Always terminate blob next ptr
Signed-off-by: Chloe M. <chloe@mirocom.org>
2026-04-30 21:11:42 -04:00
6 changed files with 83 additions and 3 deletions
+38
View File
@@ -3,11 +3,25 @@
* Provided under the BSD-3 clause * Provided under the BSD-3 clause
*/ */
#include <stdio.h>
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include "blobchain/blob.h" #include "blobchain/blob.h"
#include "blobchain/memlib.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 int
blob_list_init(struct blob_list *lp) blob_list_init(struct blob_list *lp)
{ {
@@ -65,3 +79,27 @@ blob_list_destroy(struct blob_list *lp)
free(tmp); 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;
}
}
+1
View File
@@ -14,6 +14,7 @@ blob_allocate(struct data_stream *stmp)
bp = xmalloc(sizeof(*bp)); bp = xmalloc(sizeof(*bp));
bp->length = (stmp != NULL) ? stmp->length : 0; bp->length = (stmp != NULL) ? stmp->length : 0;
bp->next = NULL;
if (stmp == NULL) { if (stmp == NULL) {
bp->data = NULL; bp->data = NULL;
} else { } else {
+31 -3
View File
@@ -12,6 +12,7 @@
#include <dirent.h> #include <dirent.h>
#include "blobchain/common.h" #include "blobchain/common.h"
#include "blobchain/stream.h" #include "blobchain/stream.h"
#include "blobchain/blob.h"
/* Input directory path */ /* Input directory path */
static char *input_dir = NULL; static char *input_dir = NULL;
@@ -32,11 +33,13 @@ version(void)
} }
static 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]; char pathbuf[512];
struct dirent *dirent; struct dirent *dirent;
DIR *dir; DIR *dir;
int error;
if (input_dir == NULL) { if (input_dir == NULL) {
return; return;
@@ -55,12 +58,27 @@ pack_foreach(const char *input_dir)
switch (dirent->d_type) { switch (dirent->d_type) {
case DT_DIR: case DT_DIR:
snprintf(pathbuf, sizeof(pathbuf), "%s/%s", input_dir, dirent->d_name); snprintf(pathbuf, sizeof(pathbuf), "%s/%s", input_dir, dirent->d_name);
pack_foreach(pathbuf); pack_foreach(blobs, pathbuf);
printf("[d] %s\n", pathbuf); printf("[d] %s\n", pathbuf);
break; break;
case DT_REG: case DT_REG:
snprintf(pathbuf, sizeof(pathbuf), "%s/%s", input_dir, dirent->d_name); snprintf(pathbuf, sizeof(pathbuf), "%s/%s", input_dir, dirent->d_name);
printf("[r] %s\n", pathbuf); 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; break;
} }
} }
@@ -70,6 +88,7 @@ static void
pack_dir(void) pack_dir(void)
{ {
struct stat sb; struct stat sb;
struct blob_list blobs;
if (stat(input_dir, &sb) < 0) { if (stat(input_dir, &sb) < 0) {
perror("stat"); perror("stat");
@@ -82,7 +101,16 @@ pack_dir(void)
return; 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 int
+5
View File
@@ -66,6 +66,11 @@ stream_destroy(struct data_stream *stmp)
return; return;
} }
if (stmp->data == NULL) {
return;
}
free(stmp->data); free(stmp->data);
stmp->length = 0; stmp->length = 0;
stmp->data = NULL;
} }
+7
View File
@@ -67,4 +67,11 @@ int blob_list_init(struct blob_list *lp);
*/ */
void blob_list_destroy(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 */ #endif /* !BLOBCHAIN_BLOB_H */
+1
View File
@@ -6,6 +6,7 @@
#ifndef BLOBCHAIN_COMMON_H #ifndef BLOBCHAIN_COMMON_H
#define BLOBCHAIN_COMMON_H 1 #define BLOBCHAIN_COMMON_H 1
#define BLOBCHAIN_DEBUG 1
#define BLOBCHAIN_VERSION "0.0.1" #define BLOBCHAIN_VERSION "0.0.1"
#endif /* !BLOBCHAIN_COMMON_H */ #endif /* !BLOBCHAIN_COMMON_H */