Compare commits
3 Commits
d7c0f78d02
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| e35d8cf136 | |||
| 1d4bb8769a | |||
| d991988fd8 |
@@ -8,6 +8,8 @@ DFILES = $(CFILES:.c=.d)
|
||||
OFILES = $(CFILES:.c=.o)
|
||||
|
||||
CC = gcc
|
||||
LIBS = $(shell pkg-config --libs "openssl")
|
||||
|
||||
CFLAGS = \
|
||||
-Wall \
|
||||
-pedantic \
|
||||
@@ -19,7 +21,7 @@ all: blobchain
|
||||
|
||||
.PHONY: blobchain
|
||||
blobchain: $(OFILES)
|
||||
$(CC) $^ -o $@
|
||||
$(CC) $(LIBS) $^ -o $@
|
||||
|
||||
-include $(DFILES)
|
||||
%.o: %.c
|
||||
|
||||
@@ -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,30 @@ 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("... hash :: ");
|
||||
dump_bytes(blob->hash, SHA256_N_BYTES);
|
||||
|
||||
printf("... data :: ");
|
||||
dump_bytes(blob->data, blob->length < DUMP_SIZE
|
||||
? blob->length
|
||||
: DUMP_SIZE);
|
||||
|
||||
blob = blob->next;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ blob_allocate(struct data_stream *stmp)
|
||||
|
||||
bp = xmalloc(sizeof(*bp));
|
||||
bp->length = (stmp != NULL) ? stmp->length : 0;
|
||||
bp->next = NULL;
|
||||
if (stmp == NULL) {
|
||||
bp->data = NULL;
|
||||
} else {
|
||||
@@ -21,5 +22,6 @@ blob_allocate(struct data_stream *stmp)
|
||||
memcpy(bp->data, stmp->data, stmp->length);
|
||||
}
|
||||
|
||||
memcpy(bp->hash, stmp->hash, SHA256_N_BYTES);
|
||||
return bp;
|
||||
}
|
||||
|
||||
+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
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <openssl/sha.h>
|
||||
#include "blobchain/memlib.h"
|
||||
#include "blobchain/stream.h"
|
||||
|
||||
@@ -56,6 +57,7 @@ stream_from_file(struct data_stream *stmp, const char *path)
|
||||
return -1;
|
||||
}
|
||||
|
||||
SHA256(stmp->data, stmp->length, stmp->hash);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -66,6 +68,11 @@ stream_destroy(struct data_stream *stmp)
|
||||
return;
|
||||
}
|
||||
|
||||
if (stmp->data == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
free(stmp->data);
|
||||
stmp->length = 0;
|
||||
stmp->data = NULL;
|
||||
}
|
||||
|
||||
@@ -9,17 +9,20 @@
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include "blobchain/stream.h"
|
||||
#include "blobchain/common.h"
|
||||
|
||||
/*
|
||||
* Represents a single blob of data
|
||||
*
|
||||
* @data: Buffer containing blob data
|
||||
* @length: Length of blob
|
||||
* @hash: Hash of file contents
|
||||
* @next: Next blob
|
||||
*/
|
||||
struct blob {
|
||||
void *data;
|
||||
size_t length;
|
||||
uint8_t hash[SHA256_N_BYTES];
|
||||
struct blob *next;
|
||||
};
|
||||
|
||||
@@ -67,4 +70,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 */
|
||||
|
||||
@@ -6,6 +6,9 @@
|
||||
#ifndef BLOBCHAIN_COMMON_H
|
||||
#define BLOBCHAIN_COMMON_H 1
|
||||
|
||||
#define SHA256_N_BYTES 32
|
||||
|
||||
#define BLOBCHAIN_DEBUG 1
|
||||
#define BLOBCHAIN_VERSION "0.0.1"
|
||||
|
||||
#endif /* !BLOBCHAIN_COMMON_H */
|
||||
|
||||
@@ -6,17 +6,21 @@
|
||||
#ifndef BLOBCHAIN_STREAM_H
|
||||
#define BLOBCHAIN_STREAM_H 1
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include "blobchain/common.h"
|
||||
|
||||
/*
|
||||
* Represents a data stream
|
||||
*
|
||||
* @data: Data buffer
|
||||
* @length: Number of bytes in data buffer
|
||||
* @hash: SHA256 hash of file contents
|
||||
*/
|
||||
struct data_stream {
|
||||
void *data;
|
||||
size_t length;
|
||||
uint8_t hash[SHA256_N_BYTES];
|
||||
};
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user