]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Fri, 26 Nov 2021 23:30:10 +0000 (15:30 -0800)
committeralex <[email protected]>
Fri, 26 Nov 2021 23:30:10 +0000 (15:30 -0800)
inc/torrent.h
src/add.c
src/torrent.c
test/unit/torrent.tests.c
test/unit/tree.tests.c

index 0bf6e4a2c3c63c2bb08cb5ce0d2d04d1107a6e69..2befb2eb988aa8cb5cf34f9ceb6142230ad66590 100644 (file)
@@ -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*);
 
index 450768f1b0634dff6a4b9fde560ab002154fa50c..c1d28c5fea3985b108b22ed3a056e52e9c4eec14 100644 (file)
--- 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;
 }
index c9f6d648674db9b601977f618114957083d28a12..bc67e5e5008b7f8a42b819d3387dda1feb0cd83f 100644 (file)
@@ -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; }
index 70140a429594b8a18cc07f20d35fdfc8a04a11ef..a81cfeca7a0a540f57f05ae0f325e9edc0d42230 100644 (file)
@@ -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);
 
index dfe2e52e84e321669d156e4ee4a0c885dbe2ba74..f5de59c2d1a2bb537f769c8164baea35352f8420 100644 (file)
@@ -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);
        }