From 44045db876ae57faf66b3fdeb3380d2e3851edc7 Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 30 Oct 2021 11:39:24 -0700 Subject: [PATCH] ... --- Makefile.am | 1 + inc/block.h | 4 ++++ inc/file.h | 5 ++++- inc/util.h | 1 + src/add.c | 2 +- src/block.c | 4 +--- src/file.c | 9 +++++---- src/util/pow.c | 5 +++++ test/unit/Makefile.am | 10 +++++++++- test/unit/block.tests.c | 34 ++++++++++++++++++++++++++++++++++ 10 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 src/util/pow.c create mode 100644 test/unit/block.tests.c diff --git a/Makefile.am b/Makefile.am index 16073d3..403c3ac 100644 --- a/Makefile.am +++ b/Makefile.am @@ -43,6 +43,7 @@ seederd_SOURCES = \ src/util/dir.c \ src/util/file.c \ src/util/filter.c \ + src/util/pow.c \ src/watch.c seederd_SOURCES += \ diff --git a/inc/block.h b/inc/block.h index 9bde2bf..c406f87 100644 --- a/inc/block.h +++ b/inc/block.h @@ -1,8 +1,12 @@ #ifndef __BLOCK_H_ #define __BLOCK_H_ +#include + #include +#include + #define BLOCK_SIZE 16384 struct block { diff --git a/inc/file.h b/inc/file.h index 28501ee..5322409 100644 --- a/inc/file.h +++ b/inc/file.h @@ -8,6 +8,9 @@ #include +#include +#include + #define FILE_MESSAGE_FREAD_FAILED "failed to read file: %s\n" struct file { @@ -19,7 +22,7 @@ struct file { }; void file_free(struct file*); -int file_hash(struct file*); +int file_hash(struct file*,int); int file_init(struct file**,const char*); #endif diff --git a/inc/util.h b/inc/util.h index cb8f070..7a8310d 100644 --- a/inc/util.h +++ b/inc/util.h @@ -13,5 +13,6 @@ int file_filter_all(const char*); int file_filter_ignore_dotfiles(const char*); int is_directory(const char*); int is_file(const char*); +size_t next_power_2(size_t); #endif diff --git a/src/add.c b/src/add.c index 774806c..ea41ddc 100644 --- a/src/add.c +++ b/src/add.c @@ -63,7 +63,7 @@ static void *add_hash(void *unused) { if(NULL==p) { return NULL; } - if(file_hash(p)<0) { return NULL; } + if(file_hash(p,global_options.piece_length)<0) { return NULL; } log_info(ADD_MESSAGE_ADDED_FILE,p->path); } diff --git a/src/block.c b/src/block.c index 5a3b51b..1219125 100644 --- a/src/block.c +++ b/src/block.c @@ -1,6 +1,6 @@ #include -int block_append_block(struct block *p) { +int block_append_blank(struct block *p) { if(NULL==p) { return -1; } while(p->next!=NULL) { @@ -14,8 +14,6 @@ int block_append_block(struct block *p) { } int block_duplicate(struct block **p, struct block *to_dup) { - struct block *next; - if(NULL==p) { return -1; } if(NULL==to_dup) { return -1; } diff --git a/src/file.c b/src/file.c index 13c191f..5262ebf 100644 --- a/src/file.c +++ b/src/file.c @@ -14,13 +14,13 @@ void file_free(struct file *p) { free(p); } -int file_hash(struct file *file_p) { +int file_hash(struct file *file_p, int piece_length) { uint8_t data[BLOCK_SIZE]; struct block *start, *p, *next, *end; FILE *fp; int blocks_per_piece; - blocks_per_piece = global_opts.piece_length / BLOCK_SIZE; + blocks_per_piece = piece_length / BLOCK_SIZE; fp = fopen(file_p->path,"rb"); if(NULL==fp) { return -1; } @@ -47,8 +47,8 @@ int file_hash(struct file *file_p) { p = p->next; } - ssize_t blocks = block_length(start); - if(blocks<=0) { goto clean; } + size_t blocks = block_length(start); + if(0==blocks) { goto clean; } if(blocks!=blocks_per_piece) { // if the file is smaller than one piece then the block hashes @@ -83,6 +83,7 @@ int file_hash(struct file *file_p) { clean: block_free(start); fclose(fp); + return -1; } int file_init(struct file **p, const char *path) { diff --git a/src/util/pow.c b/src/util/pow.c new file mode 100644 index 0000000..aba6520 --- /dev/null +++ b/src/util/pow.c @@ -0,0 +1,5 @@ +#include + +size_t next_power_2(size_t i) { + return 0; +} diff --git a/test/unit/Makefile.am b/test/unit/Makefile.am index 77aa90a..f9eca27 100644 --- a/test/unit/Makefile.am +++ b/test/unit/Makefile.am @@ -12,7 +12,7 @@ AM_CPPFLAGS += \ -DNDEBUG endif -check_PROGRAMS = bencode.tests file.tests hash.tests hashmap.tests torrent.tests tree.tests util.filter.tests +check_PROGRAMS = bencode.tests block.tests file.tests hash.tests hashmap.tests torrent.tests tree.tests util.filter.tests TESTS = $(check_PROGRAMS) if ENABLE_MEMCHECK @@ -28,9 +28,15 @@ bencode_tests_SOURCES = \ $(top_srcdir)/src/bencode/decode.c \ $(top_srcdir)/src/bencode/encode.c +block_tests_SOURCES = \ + $(common_SOURCES) \ + block.tests.c \ + $(top_srcdir)/src/block.c + file_tests_SOURCES = \ $(common_SOURCES) \ file.tests.c \ + $(top_srcdir)/src/block.c \ $(top_srcdir)/src/file.c hash_tests_SOURCES = \ @@ -46,6 +52,7 @@ hashmap_tests_SOURCES = \ torrent_tests_SOURCES = \ $(common_SOURCES) \ torrent.tests.c \ + $(top_srcdir)/src/block.c \ $(top_srcdir)/src/file.c \ $(top_srcdir)/src/hashmap.c \ $(top_srcdir)/src/torrent.c \ @@ -54,6 +61,7 @@ torrent_tests_SOURCES = \ tree_tests_SOURCES = \ $(common_SOURCES) \ tree.tests.c \ + $(top_srcdir)/src/block.c \ $(top_srcdir)/src/file.c \ $(top_srcdir)/src/tree.c diff --git a/test/unit/block.tests.c b/test/unit/block.tests.c new file mode 100644 index 0000000..0cb3027 --- /dev/null +++ b/test/unit/block.tests.c @@ -0,0 +1,34 @@ +#include + +#include + +int main(); +static void block_append_blank_basic_test(); +static void block_init_basic_test(); + +int main() { + setup_env(); + +// block_append_blank_basic_test(); +// block_duplicate_basic_test(); + block_init_basic_test(); + //block_length_basic_test(); + //block_merkle_root_basic_test(); + + clean_env(); + + return EXIT_SUCCESS; +} + +static void block_append_blank_basic_test() { + +} + +static void block_init_basic_test() { + struct block *p; + + assert(block_init(NULL)==-1); + assert(block_init(&p)==1); + + block_free(p); +} -- 2.39.5