From: alex Date: Wed, 8 Dec 2021 05:18:41 +0000 (-0800) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=75e88d0e7c6f7761a56673d6dedefb745b9e3288;p=seeder ... --- diff --git a/inc/torrent.h b/inc/torrent.h index db1d775..ad8aafc 100644 --- a/inc/torrent.h +++ b/inc/torrent.h @@ -24,7 +24,7 @@ struct torrent { unsigned long long piece_length; struct tree *tree; - struct torrent_files *files; + struct torrent_files files; int watch_fd; }; diff --git a/src/torrent/add.c b/src/torrent/add.c index 284eff0..6b3b8da 100644 --- a/src/torrent/add.c +++ b/src/torrent/add.c @@ -3,7 +3,7 @@ static int torrent_add_file(struct torrent*,struct file*); static int torrent_add_file_by_path(struct hash_map*, struct file*); static int torrent_add_file_by_root(struct hash_map*,struct file*); -static int torrent_files_resize(struct torrent *torrent_p, size_t new_size); +static int torrent_files_resize(struct torrent *torrent_p, struct hash_map**, size_t new_size); int torrent_add(struct torrent *p, struct file *f) { const char *path; @@ -27,14 +27,22 @@ int torrent_add(struct torrent *p, struct file *f) { static int torrent_add_file(struct torrent *p, struct file *f) { int i; - while((i = torrent_add_file_by_root(p->files,f))<=0) { + while((i = torrent_add_file_by_root(p->files.roots,f))<=0) { if(i<0) { return -1; } - if(torrent_files_resize(p,p->files->size<<1)<0) { return -1; } + if(torrent_files_resize( + p, /* torrent_p */ + &(p->files.roots), /* map */ + p->files.roots->size<<1 /* new_size */ + )<0) { return -1; } } - while((i = torrent_add_file_by_path(p->files,f))<=0) { + while((i = torrent_add_file_by_path(p->files.paths,f))<=0) { if(i<0) { return -1; } - if(torrent_files_resize(p,p->files->size<<1)<0) { return -1; } + if(torrent_files_resize( + p, /* torrent_p */ + &(p->files.paths), /* map */ + p->files.paths->size<<1 /* new_size */ + )<0) { return -1; } } return 1; @@ -68,54 +76,38 @@ static int torrent_add_file_by_root(struct hash_map *files, struct file *f) { return 1; } -static int torrent_files_resize(struct torrent *torrent_p, size_t new_size) { +static int torrent_files_resize(struct torrent *torrent_p, struct hash_map **map, size_t new_size) { struct file *p; struct hash_map *new, *old; - int ret1, ret2; + int ret, flag; if(hashmap_init(&new,new_size)<0) { return -1; } - for(size_t i=0;ifiles->size;i++) { - p = torrent_p->files->map[i]; - /* - * cases: - * 1.) p = NULL - * 2.) p = added by torrent_add_file_by_root, not already added - * 3.) p = added by torrent_add_file_by_path, not already added - * 4.) p = added by torrent_add_file_by_root, already added - * 5.) p = added by torrent_add_file_by_path, already added - */ - if(p!=NULL) { - found = hashmap_find(torrent_p->files,p->path,strlen(p->path)); - if( - if((ret1 = torrent_add_file_by_root(new,p))<=0) { - if(ret1<0) { goto clean; } - printf("%s already found [root]\n",p->name); - } - if((ret2 = torrent_add_file_by_path(new,p))<=0) { - if(ret2<0) { goto clean; } - printf("%s already found [path]\n",p->name); - } - - if((!ret1)&&(!ret2)) { goto resize; } - } + flag = 0; + if(torrent_p->files.paths==(*map)) { + flag = 1; } - printf("after\n"); - for(size_t i=0;isize;i++) { - p = new->map[i]; - printf("%lu: ",i); - if(p==NULL) { - printf("NULL\n"); - } else { - printf("%s\n",p->name); + for(size_t i=0;i<(*map)->size;i++) { + p = (*map)->map[i]; + if(p!=NULL) { + if(flag) { + if((ret = hashmap_insert(new,p->path,strlen(p->path),p))<=0) { + if(ret<0) { goto clean; } + goto resize; + } + } else { + if((ret = hashmap_insert(new,p->root,crypto_hash_sha256_BYTES,p))<=0) { + if(ret<0) { goto clean; } + goto resize; + } + } } } - hashmap_clear(torrent_p->files); - - old = torrent_p->files; - torrent_p->files = new; + hashmap_clear(*map); + old = *map; + *map = new; hashmap_free(old); @@ -123,8 +115,7 @@ static int torrent_files_resize(struct torrent *torrent_p, size_t new_size) { resize: hashmap_clear(new); hashmap_free(new); - printf("resizing to %lu\n",new_size<<1); - return torrent_files_resize(torrent_p,new_size<<1); + return torrent_files_resize(torrent_p,map,new_size<<1); clean: hashmap_clear(new); hashmap_free(new); diff --git a/src/torrent/file.c b/src/torrent/file.c index 9b48dbf..a1763d7 100644 --- a/src/torrent/file.c +++ b/src/torrent/file.c @@ -155,8 +155,8 @@ static int torrent_file_piece_layers(FILE *fp, struct torrent *p) { struct block **piece_layers, *block_p; pieces = 0; - for(size_t i=0;ifiles->size;i++) { - file_p = (struct file*)p->files->map[i]; + for(size_t i=0;ifiles.roots->size;i++) { + file_p = (struct file*)p->files.roots->map[i]; if(file_p!=NULL) { pieces += block_length(file_p->piece_layers); } @@ -168,8 +168,8 @@ static int torrent_file_piece_layers(FILE *fp, struct torrent *p) { } index = 0; - for(size_t i=0;ifiles->size;i++) { - file_p = (struct file*)p->files->map[i]; + for(size_t i=0;ifiles.roots->size;i++) { + file_p = (struct file*)p->files.roots->map[i]; if(file_p!=NULL) { block_p = file_p->piece_layers; while(block_p!=NULL) { diff --git a/src/torrent/free.c b/src/torrent/free.c index 5952053..3905e66 100644 --- a/src/torrent/free.c +++ b/src/torrent/free.c @@ -6,11 +6,11 @@ void torrent_free(struct torrent *p) { if(p->root!=NULL) { free(p->root); } if(p->name!=NULL) { free(p->name); } - if(p->files!=NULL) { - // clear first, tree_free will also free files - hashmap_clear(p->files); - hashmap_free(p->files); - } + // clear first, tree_free will also free files + hashmap_clear(p->files.paths); + hashmap_clear(p->files.roots); + hashmap_free(p->files.paths); + hashmap_free(p->files.roots); if(p->tree!=NULL) { tree_free(p->tree); } diff --git a/src/torrent/init.c b/src/torrent/init.c index 0f90a33..6b631f9 100644 --- a/src/torrent/init.c +++ b/src/torrent/init.c @@ -25,7 +25,8 @@ int torrent_init(struct torrent **torrent_p, const char *root, const char *name, if(tree_init(&((*torrent_p)->tree))<0) { goto clean; } - if(hashmap_init(&((*torrent_p)->files),TORRENT_FILES_HASHMAP_INITIAL_SIZE)<0) { goto clean; } + if(hashmap_init(&((*torrent_p)->files.paths),TORRENT_FILES_HASHMAP_INITIAL_SIZE)<0) { goto clean; } + if(hashmap_init(&((*torrent_p)->files.roots),TORRENT_FILES_HASHMAP_INITIAL_SIZE)<0) { goto clean; } return 1; clean: diff --git a/test/unit/torrent.tests.c b/test/unit/torrent.tests.c index e353d44..0a0ff31 100644 --- a/test/unit/torrent.tests.c +++ b/test/unit/torrent.tests.c @@ -50,24 +50,24 @@ static void torrent_add_basic_test() { assert(torrent->tree->entries->next->next->file==file1); assert(torrent->tree->entries->next->next->next->file==file2); - p = hashmap_find(torrent->files,file1->path,strlen(file1->path)); + p = hashmap_find(torrent->files.paths,file1->path,strlen(file1->path)); assert(p==file1); - p = hashmap_find(torrent->files,file1->root,crypto_hash_sha256_BYTES); + p = hashmap_find(torrent->files.roots,file1->root,crypto_hash_sha256_BYTES); assert(p==file1); - p = hashmap_find(torrent->files,file2->path,strlen(file2->path)); + p = hashmap_find(torrent->files.paths,file2->path,strlen(file2->path)); assert(p==file2); - p = hashmap_find(torrent->files,file2->root,crypto_hash_sha256_BYTES); + p = hashmap_find(torrent->files.roots,file2->root,crypto_hash_sha256_BYTES); assert(p==file2); - p = hashmap_find(torrent->files,file3->path,strlen(file3->path)); + p = hashmap_find(torrent->files.paths,file3->path,strlen(file3->path)); assert(p==file3); - p = hashmap_find(torrent->files,file3->root,crypto_hash_sha256_BYTES); + p = hashmap_find(torrent->files.roots,file3->root,crypto_hash_sha256_BYTES); assert(p==file3); - p = hashmap_find(torrent->files,file4->path,strlen(file4->path)); + p = hashmap_find(torrent->files.paths,file4->path,strlen(file4->path)); assert(p==file4); - p = hashmap_find(torrent->files,file4->root,crypto_hash_sha256_BYTES); + p = hashmap_find(torrent->files.roots,file4->root,crypto_hash_sha256_BYTES); assert(p==file4); torrent_free(torrent);