From: alex Date: Fri, 5 Nov 2021 01:52:02 +0000 (-0700) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=edacd24ffc8465107585e66f394945217fab5fbe;p=seeder ... --- diff --git a/Makefile.am b/Makefile.am index 403c3ac..aef1255 100644 --- a/Makefile.am +++ b/Makefile.am @@ -18,6 +18,10 @@ seederd_SOURCES = \ src/block.c \ src/default.c \ src/file.c \ + src/fs/concat.c \ + src/fs/dir.c \ + src/fs/file.c \ + src/fs/filter.c \ src/hash.c \ src/hashmap.c \ src/init.c \ @@ -39,11 +43,6 @@ seederd_SOURCES = \ src/torrent.c \ src/tree.c \ src/usage.c \ - src/util/concat.c \ - src/util/dir.c \ - src/util/file.c \ - src/util/filter.c \ - src/util/pow.c \ src/watch.c seederd_SOURCES += \ @@ -52,6 +51,7 @@ seederd_SOURCES += \ inc/block.h \ inc/default.h \ inc/file.h \ + inc/fs.h \ inc/hash.h \ inc/hashmap.h \ inc/init.h \ @@ -65,7 +65,6 @@ seederd_SOURCES += \ inc/torrent.h \ inc/tree.h \ inc/usage.h \ - inc/util.h \ inc/watch.h SUBDIRS = . test/unit diff --git a/inc/block.h b/inc/block.h index c406f87..c426d12 100644 --- a/inc/block.h +++ b/inc/block.h @@ -21,5 +21,6 @@ void block_free(struct block*); int block_init(struct block**); size_t block_length(struct block*); int block_merkle_root(struct block*); +int block_pad(struct block*); #endif diff --git a/src/block.c b/src/block.c index f1b5159..c8531ff 100644 --- a/src/block.c +++ b/src/block.c @@ -1,5 +1,7 @@ #include +static unsigned long next_power_2(unsigned long); + int block_append_blank(struct block *p) { if(NULL==p) { return -1; } @@ -114,3 +116,35 @@ int block_merkle_root(struct block *root) { return 1; } + +int block_pad(struct block *p) { + if(NULL==p) { return -1; } + + size_t i = 1; + while(p->next!=NULL) { + i++; + p = p->next; + } + + size_t pad = next_power_2(i) - i; + while(pad>0) { + if(block_init(&(p->next))<0) { return -1; } + memset(p->next->hash,0,crypto_hash_sha256_BYTES); + p = p->next; + pad--; + } + + return 1; +} + +static unsigned long next_power_2(unsigned long i) { + i--; + i |= i >> 1; + i |= i >> 2; + i |= i >> 4; + i |= i >> 8; + i |= i >> 16; + i++; + + return i; +} diff --git a/src/file.c b/src/file.c index 8bcbc0d..1c04fa1 100644 --- a/src/file.c +++ b/src/file.c @@ -70,11 +70,7 @@ int file_hash(struct file *file_p, int piece_length) { // if the file is smaller than one piece then the block hashes // should be padded to the next power of two instead of the next // piece boundary - size_t leaves_required = next_power_2(blocks); - while(leaves_required>0) { - if(block_append_blank(start)<0) { goto clean; } - leaves_required--; - } + if(block_pad(start)<0) { goto clean; } } if(block_merkle_root(start)<0) { goto clean; } @@ -92,6 +88,7 @@ done: fclose(fp); if(block_duplicate(&start,file_p->blocks)<0) { return -1; } + if(block_pad(start)<0) { return -1; } if(block_merkle_root(start)<0) { return -1; } memcpy(file_p->root,start->hash,crypto_hash_sha256_BYTES); block_free(start); diff --git a/src/util/concat.c b/src/fs/concat.c similarity index 100% rename from src/util/concat.c rename to src/fs/concat.c diff --git a/src/util/dir.c b/src/fs/dir.c similarity index 100% rename from src/util/dir.c rename to src/fs/dir.c diff --git a/src/util/file.c b/src/fs/file.c similarity index 100% rename from src/util/file.c rename to src/fs/file.c diff --git a/src/util/filter.c b/src/fs/filter.c similarity index 100% rename from src/util/filter.c rename to src/fs/filter.c diff --git a/src/util/pow.c b/src/util/pow.c deleted file mode 100644 index b3db5da..0000000 --- a/src/util/pow.c +++ /dev/null @@ -1,13 +0,0 @@ -#include - -unsigned long next_power_2(unsigned long i) { - i--; - i |= i >> 1; - i |= i >> 2; - i |= i >> 4; - i |= i >> 8; - i |= i >> 16; - i++; - - return i; -} diff --git a/test/unit/Makefile.am b/test/unit/Makefile.am index 728c1b5..7139f6d 100644 --- a/test/unit/Makefile.am +++ b/test/unit/Makefile.am @@ -12,7 +12,7 @@ AM_CPPFLAGS += \ -DNDEBUG endif -check_PROGRAMS = bencode.tests block.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 fs.filter.tests TESTS = $(check_PROGRAMS) if ENABLE_MEMCHECK @@ -39,8 +39,7 @@ file_tests_SOURCES = \ file.tests.c \ $(top_srcdir)/src/block.c \ $(top_srcdir)/src/file.c \ - $(top_srcdir)/src/hash.c \ - $(top_srcdir)/src/util/pow.c + $(top_srcdir)/src/hash.c hash_tests_SOURCES = \ $(common_SOURCES) \ @@ -60,8 +59,7 @@ torrent_tests_SOURCES = \ $(top_srcdir)/src/hash.c \ $(top_srcdir)/src/hashmap.c \ $(top_srcdir)/src/torrent.c \ - $(top_srcdir)/src/tree.c \ - $(top_srcdir)/src/util/pow.c + $(top_srcdir)/src/tree.c tree_tests_SOURCES = \ $(common_SOURCES) \ @@ -69,10 +67,9 @@ tree_tests_SOURCES = \ $(top_srcdir)/src/block.c \ $(top_srcdir)/src/file.c \ $(top_srcdir)/src/hash.c \ - $(top_srcdir)/src/tree.c \ - $(top_srcdir)/src/util/pow.c + $(top_srcdir)/src/tree.c -util_filter_tests_SOURCES = \ +fs_filter_tests_SOURCES = \ $(common_SOURCES) \ - util.filter.tests.c \ - $(top_srcdir)/src/util/filter.c + fs.filter.tests.c \ + $(top_srcdir)/src/fs/filter.c diff --git a/test/unit/block.tests.c b/test/unit/block.tests.c index 22a61c8..f388cbb 100644 --- a/test/unit/block.tests.c +++ b/test/unit/block.tests.c @@ -3,46 +3,26 @@ #include int main(); -static void block_append_blank_basic_test(); static void block_duplicate_basic_test(); static void block_init_basic_test(); static void block_length_basic_test(); static void block_merkle_root_basic_test(); +static void block_pad_basic_test(); int main() { setup_env(); block_init_basic_test(); - block_append_blank_basic_test(); block_duplicate_basic_test(); block_length_basic_test(); block_merkle_root_basic_test(); + block_pad_basic_test(); clean_env(); return EXIT_SUCCESS; } -static void block_append_blank_basic_test() { - struct block *root, *p; - unsigned char expected[crypto_hash_sha256_BYTES] = {0}; - - assert(block_init(&p)==1); - memset(p->hash,10,crypto_hash_sha256_BYTES); - - root = p; - - assert(block_length(root)==1); - assert(block_append_blank(p)==1); - assert(p->next!=NULL); - p = p->next; - - assert(block_length(root)==2); - assert(memcmp(p->hash,expected,crypto_hash_sha256_BYTES)==0); - - block_free(root); -} - static void block_duplicate_basic_test() { struct block *root, *root2, *p, *p2; @@ -163,3 +143,21 @@ static void block_merkle_root_basic_test() { block_free(root); } + +static void block_pad_basic_test() { + struct block *p; + + assert(block_init(&p)==1); + assert(block_pad(p)==1); + assert(block_length(p)==1); + + assert(block_init(&(p->next))==1); + assert(block_pad(p)==1); + assert(block_length(p)==2); + + assert(block_init(&(p->next->next))==1); + assert(block_pad(p)==1); + assert(block_length(p)==4); + + block_free(p); +} diff --git a/test/unit/file.tests.c b/test/unit/file.tests.c index bb8aae4..5888d32 100644 --- a/test/unit/file.tests.c +++ b/test/unit/file.tests.c @@ -71,7 +71,7 @@ static void file_hash_large_file_test() { struct file *p; FILE *fp; unsigned char buf[16384]; - unsigned char expected[crypto_hash_sha256_BYTES] = {0}; + unsigned char expected[crypto_hash_sha256_BYTES] = {58,71,55,213,6,94,65,97,113,64,176,175,139,67,229,164,13,18,213,164,166,129,202,162,224,45,9,227,191,155,144,144}; fp = fopen(TEST_FILE_1,"a"); assert(fp!=NULL); @@ -86,7 +86,7 @@ static void file_hash_large_file_test() { assert(file_init(&p,TEST_FILE_1)==1); assert(file_hash(p,16384)==1); assert(memcmp(p->root,expected,crypto_hash_sha256_BYTES)==0); - assert(10000==block_length(p->blocks)); + assert(10001==block_length(p->blocks)); assert((10000*16384+21)==p->size); file_free(p); @@ -105,8 +105,8 @@ static void file_hash_random_file_test() { assert(file_init(&p,TEST_FILE_5)==1); assert(file_hash(p,16384)==1); - assert(1==block_length(p->blocks)); - assert(24==p->size); + assert(15625==block_length(p->blocks)); + assert(256000000==p->size); file_free(p); } diff --git a/test/unit/util.filter.tests.c b/test/unit/fs.filter.tests.c similarity index 100% rename from test/unit/util.filter.tests.c rename to test/unit/fs.filter.tests.c