]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Sat, 30 Oct 2021 18:39:24 +0000 (11:39 -0700)
committeralex <[email protected]>
Sat, 30 Oct 2021 18:39:24 +0000 (11:39 -0700)
Makefile.am
inc/block.h
inc/file.h
inc/util.h
src/add.c
src/block.c
src/file.c
src/util/pow.c [new file with mode: 0644]
test/unit/Makefile.am
test/unit/block.tests.c [new file with mode: 0644]

index 16073d38a3f21f289915d7a1db6425f81de75638..403c3ac7a6ed9fb8d74cf35a9cab89c8e40f0827 100644 (file)
@@ -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 += \
index 9bde2bfcaf0ac43f0d30ddc518cb46ab6439b126..c406f87f65618959e5c9ac58f031d0d05b687377 100644 (file)
@@ -1,8 +1,12 @@
 #ifndef __BLOCK_H_
 #define __BLOCK_H_
 
+#include<string.h>
+
 #include<sodium.h>
 
+#include<hash.h>
+
 #define BLOCK_SIZE 16384
 
 struct block {
index 28501eee2d951eb608af9f124a30c8a4cc24635c..532240987e61a85edb4d50765d6141eb083f8b3d 100644 (file)
@@ -8,6 +8,9 @@
 
 #include<sodium.h>
 
+#include<block.h>
+#include<log.h>
+
 #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
index cb8f070d9677cb6d1ae193d1278f58b2063074ee..7a8310daa51e54c829b6b337e13a43d8cfa0cafe 100644 (file)
@@ -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
index 774806c29483436b9339155949070525b6f62dea..ea41ddc6851543758c994c44d613f9cdfa696fb9 100644 (file)
--- 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);
        }
index 5a3b51bdf3e5993e8c6f30ba59d5eb88794959ab..12191252c0728877a86bb5a6d98e29c11825846d 100644 (file)
@@ -1,6 +1,6 @@
 #include<block.h>
 
-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; }
 
index 13c191f709f0cc4f8e30b7a81709a159b16d009b..5262ebf010413d3b1c00f8955de48d08c0b5478a 100644 (file)
@@ -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 (file)
index 0000000..aba6520
--- /dev/null
@@ -0,0 +1,5 @@
+#include<util.h>
+
+size_t next_power_2(size_t i) {
+       return 0;
+}
index 77aa90a0eff92f7ba30fe64c63fd3adb0d08b5b4..f9eca27b27d75325ea02724aac26902379ca4a10 100644 (file)
@@ -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 (file)
index 0000000..0cb3027
--- /dev/null
@@ -0,0 +1,34 @@
+#include<test_utils.h>
+
+#include<block.h>
+
+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);
+}