From b9e6e3ab1af9e025dd886fe9055ed33a729fe91d Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 30 Nov 2021 17:59:25 -0800 Subject: [PATCH] ... --- inc/tree.h | 11 ++-- src/torrent.c | 2 +- src/tree.c | 124 +++++++++++++++++++++++------------------ src/watch.c | 19 ++----- test/unit/tree.tests.c | 19 +++++++ 5 files changed, 101 insertions(+), 74 deletions(-) diff --git a/inc/tree.h b/inc/tree.h index 47842ca..10e3dd4 100644 --- a/inc/tree.h +++ b/inc/tree.h @@ -6,15 +6,16 @@ #include 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 diff --git a/src/torrent.c b/src/torrent.c index 7e843d1..3c452a9 100644 --- a/src/torrent.c +++ b/src/torrent.c @@ -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; } diff --git a/src/tree.c b/src/tree.c index e7d4ff8..4f2065a 100644 --- a/src/tree.c +++ b/src/tree.c @@ -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; } diff --git a/src/watch.c b/src/watch.c index 3d0a178..2f5b9a1 100644 --- a/src/watch.c +++ b/src/watch.c @@ -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; } diff --git a/test/unit/tree.tests.c b/test/unit/tree.tests.c index f5de59c..9346c42 100644 --- a/test/unit/tree.tests.c +++ b/test/unit/tree.tests.c @@ -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; -- 2.30.2