...
authoralex <[email protected]>
Mon, 6 Dec 2021 07:30:22 +0000 (23:30 -0800)
committeralex <[email protected]>
Mon, 6 Dec 2021 07:30:22 +0000 (23:30 -0800)
inc/file.h
inc/torrent.h
src/torrent/add.c
src/torrent/piece.c [new file with mode: 0644]
test/unit/torrent.tests.c

index 9c4843c35d6b7356137d8b8d80fcb091bfdd1d40..2d7d00698477eccc6c10795ab3d218e53a7c9e10 100644 (file)
@@ -17,7 +17,7 @@ struct file {
        char *path;
        unsigned char root[crypto_hash_sha256_BYTES];
        struct block *piece_layers;
-       size_t size;
+       uint64_t size;
 };
 
 ssize_t file_bencode(struct file*,uint8_t*,size_t);
index 7266833e9ef07f64b8b4a71de6ebaa5e668e6539..e38995cca59b634520718937f86d42a7ccd07c56 100644 (file)
@@ -31,5 +31,6 @@ char *torrent_file_path(unsigned char*,size_t);
 void torrent_free(struct torrent*);
 int torrent_init(struct torrent**,const char*,const char*,unsigned long long);
 char *torrent_magnet(struct torrent*);
+int torrent_bencode_piece_layers();
 
 #endif
index e14bace89ed103b4cff1af92e6c20608a8c3f569..c23f5f0d00d67da6393d04953b94ffabb65de1da 100644 (file)
@@ -43,7 +43,7 @@ static int torrent_add_file_by_path(struct hash_map *files, struct file *f) {
        struct file *p;
        int i;
        
-       if((i = hashmap_insert(files,f->path,strlen(f->path),f))<0) {
+       if((i = hashmap_insert(files,f->path,strlen(f->path),f))<=0) {
                if(i<0) { return -1; }
 
                p = hashmap_find(files,f->path,strlen(f->path));
@@ -57,7 +57,7 @@ static int torrent_add_file_by_root(struct hash_map *files, struct file *f) {
        struct file *p;
        int i;
 
-       if((i = hashmap_insert(files,f->root,crypto_hash_sha256_BYTES,f))<0) {
+       if((i = hashmap_insert(files,f->root,crypto_hash_sha256_BYTES,f))<=0) {
                if(i<0) { return -1; }
 
                p = hashmap_find(files,f->root,crypto_hash_sha256_BYTES);
@@ -68,7 +68,7 @@ static int torrent_add_file_by_root(struct hash_map *files, struct file *f) {
 }
 
 static int torrent_files_resize(struct torrent *torrent_p, size_t new_size) {
-       struct file *p;
+       struct file *p, *found;
        struct hash_map *new, *old;
        int ret;
 
@@ -77,6 +77,12 @@ static int torrent_files_resize(struct torrent *torrent_p, size_t new_size) {
        for(size_t i=0;i<torrent_p->files->size;i++) {
                p = torrent_p->files->map[i];
                if(p!=NULL) {
+                       // remove reference to file
+                       // check if already added this file
+                       // necessary because of dual keys
+                       found = hashmap_find(new,p->root,crypto_hash_sha256_BYTES);
+                       if(found!=NULL) { continue; }
+
                        if(
                                ((ret = torrent_add_file_by_root(new,p))<=0) ||
                                ((ret = torrent_add_file_by_path(new,p))<=0)
diff --git a/src/torrent/piece.c b/src/torrent/piece.c
new file mode 100644 (file)
index 0000000..ffa062e
--- /dev/null
@@ -0,0 +1,5 @@
+#include<torrent.h>
+
+int torrent_bencode_piece_layers() {
+       return -1;
+}
index 38389c73bfaf27064dc89541248486cdc13555ab..93470d6d7ffb3f23b557c94e40bcbbb79706bde0 100644 (file)
@@ -22,12 +22,18 @@ static void torrent_add_basic_test() {
        struct file *file1, *file2, *file3, *file4;
 
        assert(file_init(&file1,TEST_FILE_1)==1);
+       memset(file1->root,1,crypto_hash_sha256_BYTES);
        assert(file_init(&file2,TEST_FILE_2)==1);
+       memset(file2->root,2,crypto_hash_sha256_BYTES);
        assert(file_init(&file3,TEST_FILE_3)==1);
+       memset(file3->root,3,crypto_hash_sha256_BYTES);
        assert(file_init(&file4,TEST_FILE_4)==1);
+       memset(file4->root,4,crypto_hash_sha256_BYTES);
 
        assert(torrent_init(&torrent,TEST_DIRECTORY,TEST_DIRECTORY,16384)==1);
 
+       memset(torrent->files->key,0,crypto_shorthash_KEYBYTES);
+
        assert(torrent_add(torrent,file1)==1);
        assert(torrent_add(torrent,file2)==1);
        assert(torrent_add(torrent,file3)==1);
@@ -38,6 +44,8 @@ static void torrent_add_basic_test() {
        assert(torrent->tree->entries->next->next->file==file1);
        assert(torrent->tree->entries->next->next->next->file==file2);
 
+       assert(0);
+
        torrent_free(torrent);
 
        reset_env();