From: alex Date: Fri, 26 Nov 2021 23:30:10 +0000 (-0800) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=44748eb448d036ef80bfc41fe7e423ed6f2969e4;p=seeder ... --- diff --git a/inc/torrent.h b/inc/torrent.h index 0bf6e4a..2befb2e 100644 --- a/inc/torrent.h +++ b/inc/torrent.h @@ -17,7 +17,7 @@ struct torrent { int watch_fd; }; -int torrent_add(struct torrent*,const char*,struct file*); +int torrent_add(struct torrent*,struct file*); void torrent_free(struct torrent*); int torrent_init(struct torrent**,const char*,const char*); diff --git a/src/add.c b/src/add.c index 450768f..c1d28c5 100644 --- a/src/add.c +++ b/src/add.c @@ -126,7 +126,6 @@ static int add_to_queue(struct file *to_add) { static int ftw_helper(const char *path, const struct stat *st, int typeflag) { struct file *to_add; - const char *p; if(typeflag!=FTW_F) { return 0; } switch(global_options.file_filter) { @@ -141,14 +140,7 @@ static int ftw_helper(const char *path, const struct stat *st, int typeflag) { if(file_init(&to_add,path)<0) { return -1; } if(add_to_queue(to_add)<0) { return -1; } - p = strstr(path,current_torrent->root); - if((p!=NULL)&&(p==path)) { - p += strlen(current_torrent->root); - } else { - p = path; - } - - if(torrent_add(current_torrent,p,to_add)<0) { return -1; } + if(torrent_add(current_torrent,to_add)<0) { return -1; } return 0; } diff --git a/src/torrent.c b/src/torrent.c index c9f6d64..bc67e5e 100644 --- a/src/torrent.c +++ b/src/torrent.c @@ -2,10 +2,18 @@ static int torrent_add_file(struct torrent *p, struct file *f); -int torrent_add(struct torrent *p, const char *path, struct file *f) { +int torrent_add(struct torrent *p, struct file *f) { + const char *path; + if(NULL==p) { return -1; } if(NULL==f) { return -1; } - if(NULL==path) { return -1; } + + path = strstr(f->path,p->root); + if((path!=NULL)&&(path==f->path)) { + path += strlen(p->root); + } else { + path = f->path; + } if(tree_add(p->tree,path,f)<0) { return -1; } if(torrent_add_file(p,f)<0) { return -1; } diff --git a/test/unit/torrent.tests.c b/test/unit/torrent.tests.c index 70140a4..a81cfec 100644 --- a/test/unit/torrent.tests.c +++ b/test/unit/torrent.tests.c @@ -33,11 +33,11 @@ static void torrent_add_basic_test() { assert(torrent_add(torrent,file3)==1); assert(torrent_add(torrent,file4)==1); - assert(strcmp(torrent->tree->directories->name,TEST_DIRECTORY)==0); - assert(torrent->tree->directories->files->file==file3); - assert(torrent->tree->directories->files->next->file==file4); - assert(torrent->tree->directories->files->next->next->file==file1); - assert(torrent->tree->directories->files->next->next->next->file==file2); + assert(NULL==torrent->tree->directories); + assert(torrent->tree->files->file==file3); + assert(torrent->tree->files->next->file==file4); + assert(torrent->tree->files->next->next->file==file1); + assert(torrent->tree->files->next->next->next->file==file2); torrent_free(torrent); diff --git a/test/unit/tree.tests.c b/test/unit/tree.tests.c index dfe2e52..f5de59c 100644 --- a/test/unit/tree.tests.c +++ b/test/unit/tree.tests.c @@ -68,14 +68,15 @@ static void tree_add_basic_test() { nope->directories = helloo; helloo->files = entry3; - assert(-1==tree_add(NULL,NULL)); - assert(-1==tree_add(tree,NULL)); + assert(-1==tree_add(NULL,NULL,NULL)); + assert(-1==tree_add(tree,NULL,NULL)); + assert(-1==tree_add(tree,"test path",NULL)); - assert(1==tree_add(tree,file1)); - assert(1==tree_add(tree,file1)); - assert(1==tree_add(tree,file2)); - assert(1==tree_add(tree,file3)); - assert(1==tree_add(tree,file4)); + assert(1==tree_add(tree,file1->path,file1)); + assert(1==tree_add(tree,file1->path,file1)); + assert(1==tree_add(tree,file2->path,file2)); + assert(1==tree_add(tree,file3->path,file3)); + assert(1==tree_add(tree,file4->path,file4)); assert(NULL==tree->name); assert(NULL==tree->files); @@ -160,7 +161,7 @@ static void tree_add_extended_test() { for(int i=0;i<100;i++) { char *path = create_random_path(); assert(1==file_init(&file,path)); - assert(1==tree_add(tree,file)); + assert(1==tree_add(tree,path,file)); free(path); }