]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Wed, 1 Dec 2021 01:59:25 +0000 (17:59 -0800)
committeralex <[email protected]>
Wed, 1 Dec 2021 01:59:25 +0000 (17:59 -0800)
inc/tree.h
src/torrent.c
src/tree.c
src/watch.c
test/unit/tree.tests.c

index 47842caaecc4f5f5367c2ed19dc48194ddeae3e9..10e3dd43bc4ae0066916f001ab07154239e17041 100644 (file)
@@ -6,15 +6,16 @@
 #include<file.h>
 
 struct tree_entry {
+       char *name;
+       
+       struct tree *children;
        struct file *file;
+
        struct tree_entry *next;
 };
 
 struct tree {
-       char *name;
-       struct tree_entry *files;
-       struct tree *directories;
-       struct tree *next;
+       struct tree_entry *entries;
        int watch_fd;
 };
 
@@ -23,6 +24,6 @@ ssize_t tree_bencode(struct tree*,uint8_t*,size_t);
 void tree_entry_free(struct tree_entry*);
 int tree_entry_init(struct tree_entry**, struct file*);
 void tree_free(struct tree*);
-int tree_init(struct tree**, const char*);
+int tree_init(struct tree**);
 
 #endif
index 7e843d17fa6e235c90e864800c23347a476d9100..3c452a97994b0572ff1d78da2c426dbbbcf36e2e 100644 (file)
@@ -261,7 +261,7 @@ int torrent_init(struct torrent **torrent_p, const char *root, const char *name)
        (*torrent_p)->name = strdup(name);
        if(NULL==(*torrent_p)->name) { goto clean; }
 
-       if(tree_init(&((*torrent_p)->tree),NULL)<0) { goto clean; }
+       if(tree_init(&((*torrent_p)->tree))<0) { goto clean; }
 
        if(hashmap_init(&((*torrent_p)->files),TORRENT_FILES_HASHMAP_INITIAL_SIZE)<0) { goto clean; }
 
index e7d4ff8278f8d0a5ac4a5dc4bc07935337411dde..4f2065a41e5fe983dc3d2a8ecc2c378f28b5f406 100644 (file)
@@ -2,6 +2,7 @@
 
 static struct tree* tree_add_directory(struct tree*,const char*);
 static int tree_add_file(struct tree*,struct file*);
+static int tree_entry_init_directory(struct tree_entry**,const char*);
 
 int tree_add(struct tree *root, const char *path, struct file *to_add) {
        char *str, *p, *prev;
@@ -9,7 +10,7 @@ int tree_add(struct tree *root, const char *path, struct file *to_add) {
 
        if(NULL==root) { return -1; }
        if(NULL==to_add) { return -1; }
-       if((NULL==path)||(strlen(path)<=0)) { return -1; }
+       if((NULL==path)||(0==strlen(path))) { return -1; }
 
        tree = root;
 
@@ -33,13 +34,13 @@ int tree_add(struct tree *root, const char *path, struct file *to_add) {
        free(str);
        
        return 1;
-       clean:
+clean:
                free(str);
                return -1;
 }
 
 static struct tree* tree_add_directory(struct tree *root, const char *dirname) {
-       struct tree *p, *prev, *next;
+       struct tree_entry *p, *prev, *next;
        int i;
 
        if(NULL==root) { return NULL; }
@@ -47,12 +48,12 @@ static struct tree* tree_add_directory(struct tree *root, const char *dirname) {
        if(NULL==dirname) { return NULL; }
        if(0==strlen(dirname)) { return NULL; }
 
-       p = root->directories;
+       p = root->entries;
 
        prev = NULL;
        while(p!=NULL) {
                if((i = strcmp(p->name,dirname))>=0) {
-                       if(i==0) { return p; }
+                       if(i==0) { return p->children; }
                        break;
                }
                
@@ -60,45 +61,19 @@ static struct tree* tree_add_directory(struct tree *root, const char *dirname) {
                p = p->next;
        }
 
-       if(tree_init(&next,dirname)<0) { return NULL; }
+       if(tree_entry_init_directory(&next,dirname)<0) { return NULL; }
 
        if(prev!=NULL) {
                prev->next = next;
        } else {
-               root->directories = next;
+               root->entries = next;
        }
 
        next->next = p;
        
-       return next;
-}
-
-ssize_t tree_bencode(struct tree *tree, uint8_t *buf, size_t buf_len) {
-       return -1;
+       return next->children;
 }
 
-void tree_entry_free(struct tree_entry *p) {
-       if(NULL==p) { return; }
-       
-       file_free(p->file);
-       tree_entry_free(p->next);
-
-       free(p);
-}
-
-int tree_entry_init(struct tree_entry **p, struct file *file) {
-       if(NULL==p) { return -1; }
-       if(NULL==file) { return -1; }
-
-       (*p) = malloc(sizeof(struct tree_entry));
-       if(NULL==(*p)) { return -1; }
-
-       (*p)->file = file;
-       (*p)->next = NULL;
-
-       return 1;
-};
-
 static int tree_add_file(struct tree *root, struct file *to_add) {
        struct tree_entry *p, *prev, *next;
        int i;
@@ -106,12 +81,15 @@ static int tree_add_file(struct tree *root, struct file *to_add) {
        if(NULL==root) { return -1; }
        if(NULL==to_add) { return -1; }
 
-       p = root->files;
+       p = root->entries;
 
        prev = NULL;
        while(p!=NULL) {
-               if((i = strcmp(p->file->name,to_add->name))>=0) {
-                       if(i==0) { return 0; }
+               if((i = strcmp(p->name,to_add->name))>=0) {
+                       if(i==0) {
+                               if(p->children!=NULL) { return -1; }
+                               return 0;
+                       }
                        break;
                }
 
@@ -124,7 +102,7 @@ static int tree_add_file(struct tree *root, struct file *to_add) {
        if(prev!=NULL) {
                prev->next = next;
        } else {
-               root->files = next;
+               root->entries = next;
        }
 
        next->next = p;
@@ -132,21 +110,66 @@ static int tree_add_file(struct tree *root, struct file *to_add) {
        return 1;
 }
 
-void tree_free(struct tree *p) {
+ssize_t tree_bencode(struct tree *tree, uint8_t *buf, size_t buf_len) {
+       struct tree_entry *p;
+
+       p = tree->entries;
+       while(p!=NULL) {
+               if(p->children!=NULL) {
+                       // write directory
+                       // recurse into subdirectory
+               } else {
+                       // write file
+               }
+       }
+
+       return -1;
+}
+
+void tree_entry_free(struct tree_entry *p) {
        if(NULL==p) { return; }
+       
+       if(NULL!=p->file) { file_free(p->file); }
+       if(NULL!=p->children) { tree_free(p->children); }
+
+       tree_entry_free(p->next);
+
+       free(p);
+}
 
-       if(p->name!=NULL) {
-               free(p->name);
+int tree_entry_init(struct tree_entry **p, struct file *file) {
+       if(NULL==p) { return -1; }
+       if(NULL==file) { return -1; }
+
+       (*p) = malloc(sizeof(struct tree_entry));
+       if(NULL==(*p)) { return -1; }
+
+       (*p)->name = strdup(file->name);
+       if(NULL==(*p)->name) {
+               free(p);
+               return -1;
        }
 
-       tree_entry_free(p->files);
-       tree_free(p->directories);
-       tree_free(p->next);
+       (*p)->children = NULL;
+       (*p)->file = file;
+       (*p)->next = NULL;
+
+       return 1;
+};
+
+static int tree_entry_init_directory(struct tree_entry **p, const char *dirname) {
+       return -1;
+}
+
+void tree_free(struct tree *p) {
+       if(NULL==p) { return; }
+
+       tree_entry_free(p->entries);
 
        free(p);
 }
 
-int tree_init(struct tree **p, const char *name) {
+int tree_init(struct tree **p) {
        if(NULL==p) { return -1; }
 
        (*p) = malloc(sizeof(struct tree));
@@ -155,15 +178,8 @@ int tree_init(struct tree **p, const char *name) {
                return -1;
        }
 
-       if(name!=NULL) {
-               (*p)->name = strdup(name);
-       } else {
-               (*p)->name = NULL;
-       }
-
-       (*p)->files = NULL;
-       (*p)->directories = NULL;
-       (*p)->next = NULL;
+       (*p)->entries = NULL;
+       (*p)->watch_fd = -1;
 
        return 1;
 }
index 3d0a1786b3d74acd49a8ed776d84caab5d74371b..2f5b9a1b231b78810720de21ea97827a9579e693 100644 (file)
@@ -40,7 +40,7 @@ static void *watch_spawn(void *unused) {
 #define WATCH_FLAGS IN_CLOSE_WRITE | IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_TO
 
 static int watch_spawn_all(int fd, const char *root, struct tree *tree) {
-       struct tree *p;
+       struct tree_entry *p;
 
        if(fd<0) { return -1; }
        if(NULL==root) { return -1; }
@@ -56,22 +56,13 @@ static int watch_spawn_all(int fd, const char *root, struct tree *tree) {
 
        log_info(WATCH_MESSAGE_START,root);
 
-       p = tree->next;
+       p = tree->entries;
        while(p!=NULL) {
-               if(watch_spawn_all(fd,root,p)<0) { return -1; }
-               p = p->next;
-       }
-
-       p = tree->directories;
-       while(p!=NULL) {
-               char *newroot = concat(root,p->name);
-
-               if(watch_spawn_all(fd,newroot,p)<0) {
+               if(p->children!=NULL) {
+                       char *newroot = concat(root,p->name);
+                       if(watch_spawn_all(fd,newroot,p->children)<0) { return -1; }
                        free(newroot);
-                       return -1;
                }
-
-               free(newroot);
                p = p->next;
        }
 
index f5de59c2d1a2bb537f769c8164baea35352f8420..9346c422b4fe5dd24bea3a80f3a1835f0b4c3fb0 100644 (file)
@@ -7,6 +7,7 @@ static char *create_random_path();
 static int tree_deep_equals(struct tree *a, struct tree *b);
 static void tree_add_basic_test();
 static void tree_add_extended_test();
+static void tree_bencode_basic_test();
 static void tree_init_basic_test();
 
 int main() {
@@ -14,6 +15,7 @@ int main() {
 
        tree_init_basic_test();
        tree_add_basic_test();
+       tree_bencode_basic_test();
        tree_add_extended_test();
 
        clean_env();
@@ -168,6 +170,23 @@ static void tree_add_extended_test() {
        tree_free(tree);
 }
 
+static void tree_bencode_basic_test() {
+       struct tree *root;
+       struct file *file;
+       uint8_t buf[100];
+       char fileA_path[] = "dir1/dir2/fileA.txt";
+       uint8_t expected[] = "d4:dir1d4:dir2d9:fileA.txtd0:d5:lengthi102411:pieces root32:00000000000000000000000000000000eeeee";
+
+       assert(1==tree_init(&root,NULL));
+
+       assert(1==file_init(&file,fileA_path));
+       memset(file->root,48,crypto_hash_sha256_BYTES);
+
+       assert(45==tree_bencode(root,buf,100));
+
+       assert(memcmp(buf,expected,45)==0);
+}
+
 static void tree_init_basic_test() {
        struct tree *p;