]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Fri, 5 Nov 2021 01:52:02 +0000 (18:52 -0700)
committeralex <[email protected]>
Fri, 5 Nov 2021 01:52:02 +0000 (18:52 -0700)
13 files changed:
Makefile.am
inc/block.h
src/block.c
src/file.c
src/fs/concat.c [moved from src/util/concat.c with 100% similarity]
src/fs/dir.c [moved from src/util/dir.c with 100% similarity]
src/fs/file.c [moved from src/util/file.c with 100% similarity]
src/fs/filter.c [moved from src/util/filter.c with 100% similarity]
src/util/pow.c [deleted file]
test/unit/Makefile.am
test/unit/block.tests.c
test/unit/file.tests.c
test/unit/fs.filter.tests.c [moved from test/unit/util.filter.tests.c with 100% similarity]

index 403c3ac7a6ed9fb8d74cf35a9cab89c8e40f0827..aef125587bbe46428056a06e4caa95c04e0ad9d3 100644 (file)
@@ -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
index c406f87f65618959e5c9ac58f031d0d05b687377..c426d12bfc02c8673fd9df5dadc5be38c216577d 100644 (file)
@@ -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
index f1b515965a704fcd30931d5679cc382a834354f6..c8531ff032cc1d7723f3c88cae6c1359165f5cf2 100644 (file)
@@ -1,5 +1,7 @@
 #include<block.h>
 
+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;
+}
index 8bcbc0dd9c7d25d5b9fee0c3d2186d2cee00ebea..1c04fa1ac4167addd430c0ae1535b8477ca52a1b 100644 (file)
@@ -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);
similarity index 100%
rename from src/util/concat.c
rename to src/fs/concat.c
similarity index 100%
rename from src/util/dir.c
rename to src/fs/dir.c
similarity index 100%
rename from src/util/file.c
rename to src/fs/file.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 (file)
index b3db5da..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-#include<util.h>
-
-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;
-}
index 728c1b5ee2aed4548cedd462d44324263735f731..7139f6d8863354595d288e7987069faa654cdde7 100644 (file)
@@ -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
index 22a61c8bc9c4ce3d4f46fe9617d9e61b3a0e3e10..f388cbbed7c368000e9e65626b003eb929388f18 100644 (file)
@@ -3,46 +3,26 @@
 #include<block.h>
 
 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);
+}
index bb8aae4c801a76883ae1773cefa7cd8c6807f779..5888d3225c184fe37eba28dc85e5c7e65c50c6f3 100644 (file)
@@ -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);
 }