From: alex Date: Mon, 6 Dec 2021 07:30:22 +0000 (-0800) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=c90d08d558c2a4a01ebbf9fc35c0a3ca97f94737;p=seeder ... --- diff --git a/inc/file.h b/inc/file.h index 9c4843c..2d7d006 100644 --- a/inc/file.h +++ b/inc/file.h @@ -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); diff --git a/inc/torrent.h b/inc/torrent.h index 7266833..e38995c 100644 --- a/inc/torrent.h +++ b/inc/torrent.h @@ -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 diff --git a/src/torrent/add.c b/src/torrent/add.c index e14bace..c23f5f0 100644 --- a/src/torrent/add.c +++ b/src/torrent/add.c @@ -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;ifiles->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 index 0000000..ffa062e --- /dev/null +++ b/src/torrent/piece.c @@ -0,0 +1,5 @@ +#include + +int torrent_bencode_piece_layers() { + return -1; +} diff --git a/test/unit/torrent.tests.c b/test/unit/torrent.tests.c index 38389c7..93470d6 100644 --- a/test/unit/torrent.tests.c +++ b/test/unit/torrent.tests.c @@ -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();