core: Add files to blob list on scan
Signed-off-by: Chloe M. <chloe@mirocom.org>
This commit is contained in:
@@ -3,11 +3,25 @@
|
||||
* Provided under the BSD-3 clause
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#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;
|
||||
}
|
||||
}
|
||||
|
||||
+31
-3
@@ -12,6 +12,7 @@
|
||||
#include <dirent.h>
|
||||
#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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user