From: alex Date: Mon, 18 Oct 2021 17:37:23 +0000 (-0700) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=3bad5e78b763c4e9445bac8b4b2a388ffe34c671;p=seeder ... --- diff --git a/src/file.c b/src/file.c index 46eee42..45b6dd8 100644 --- a/src/file.c +++ b/src/file.c @@ -2,13 +2,15 @@ void file_free(struct file *p) { free(p->name); + free(p->path); + free(p); } int file_init(struct file **p, const char *path) { char *b; if(NULL==p) { return -1; } - if(0==strlen(path)) { return -1; } + if((NULL==path)||(0==strlen(path))) { return -1; } *p = malloc(sizeof(struct file)); if(NULL==(*p)) { diff --git a/src/tree.c b/src/tree.c index e971c66..bef2cc2 100644 --- a/src/tree.c +++ b/src/tree.c @@ -1,10 +1,10 @@ #include static struct tree* tree_add_directory(struct tree*,const char*); -static int tree_add_file(struct tree_entry*,struct file*); +static int tree_add_file(struct tree*,struct file*); int tree_add(struct tree *root, struct file *to_add) { - char *str, *p; + char *str, *p, *prev; struct tree *tree; if(NULL==root) { return -1; } @@ -15,15 +15,19 @@ int tree_add(struct tree *root, struct file *to_add) { str = strdup(to_add->path); if(NULL==str) { return -1; } + prev = NULL; p = strtok(str,"/"); while(p) { - tree = tree_add_directory(tree->directories,p); - if(NULL==tree) { goto clean; } + if(prev!=NULL) { + tree = tree_add_directory(tree,prev); + if(NULL==tree) { goto clean; } + } + prev = p; p = strtok(NULL,"/"); } - if(tree_add_file(tree->files,to_add)<0) { goto clean; } + if(tree_add_file(tree,to_add)<0) { goto clean; } free(str); @@ -33,10 +37,17 @@ int tree_add(struct tree *root, struct file *to_add) { return -1; } -static struct tree* tree_add_directory(struct tree *p, const char *dirname) { - struct tree *prev, *next; +static struct tree* tree_add_directory(struct tree *root, const char *dirname) { + struct tree *p, *prev, *next; int i; + if(NULL==root) { return NULL; } + + if(NULL==dirname) { return NULL; } + if(0==strlen(dirname)) { return NULL; } + + p = root->directories; + prev = NULL; while(p!=NULL) { if((i = strcmp(p->name,dirname))>=0) { @@ -52,6 +63,8 @@ static struct tree* tree_add_directory(struct tree *p, const char *dirname) { if(prev!=NULL) { prev->next = next; + } else { + root->directories = next; } next->next = p; @@ -59,10 +72,15 @@ static struct tree* tree_add_directory(struct tree *p, const char *dirname) { return next; } -static int tree_add_file(struct tree_entry *p, struct file *to_add) { - struct tree_entry *prev, *next; +static int tree_add_file(struct tree *root, struct file *to_add) { + struct tree_entry *p, *prev, *next; int i; + if(NULL==root) { return -1; } + if(NULL==to_add) { return -1; } + + p = root->files; + prev = NULL; while(p!=NULL) { if((i = strcmp(p->file->name,to_add->name))>=0) { @@ -82,6 +100,8 @@ static int tree_add_file(struct tree_entry *p, struct file *to_add) { if(prev!=NULL) { prev->next = next; + } else { + root->files = next; } next->next = p; @@ -108,14 +128,12 @@ void tree_free(struct tree *p) { subdir = p->directories; p->directories = p->directories->next; tree_free(subdir); - free(subdir); } while(p->next!=NULL) { subdir = p->next; p->next = p->next->next; tree_free(subdir); - free(subdir); } free(p); diff --git a/test/unit/Makefile.am b/test/unit/Makefile.am index 151d762..4f7e3f4 100644 --- a/test/unit/Makefile.am +++ b/test/unit/Makefile.am @@ -12,7 +12,7 @@ AM_CPPFLAGS += \ -DNDEBUG endif -check_PROGRAMS = hashmap.tests tree.tests util.filter.tests +check_PROGRAMS = file.tests hashmap.tests tree.tests util.filter.tests TESTS = $(check_PROGRAMS) if ENABLE_MEMCHECK @@ -22,6 +22,11 @@ endif common_SOURCES = test_utils.c +file_tests_SOURCES = \ + $(common_SOURCES) \ + file.tests.c \ + $(top_srcdir)/src/file.c + hashmap_tests_SOURCES = \ $(common_SOURCES) \ hashmap.tests.c \ diff --git a/test/unit/file.tests.c b/test/unit/file.tests.c new file mode 100644 index 0000000..0e0a31b --- /dev/null +++ b/test/unit/file.tests.c @@ -0,0 +1,31 @@ +#include + +#include + +int main(); +static void file_init_basic_test(); + +int main() { + setup_env(); + + file_init_basic_test(); + + clean_env(); + + return EXIT_SUCCESS; +} + +static void file_init_basic_test() { + struct file *p; + + assert(-1==file_init(NULL,NULL)); + assert(-1==file_init(&p,NULL)); + assert(-1==file_init(&p,"")); + + assert(1==file_init(&p,"testdir/test")); + + assert(strcmp(p->name,"test")==0); + assert(strcmp(p->path,"testdir/test")==0); + + file_free(p); +} diff --git a/test/unit/tree.tests.c b/test/unit/tree.tests.c index f0992b8..cf53058 100644 --- a/test/unit/tree.tests.c +++ b/test/unit/tree.tests.c @@ -4,12 +4,14 @@ int main(); static void tree_add_basic_test(); +static void tree_add_extended_test(); static void tree_init_basic_test(); int main() { setup_env(); tree_add_basic_test(); + tree_add_extended_test(); tree_init_basic_test(); clean_env(); @@ -50,9 +52,33 @@ static void tree_add_basic_test() { assert(tree->directories->directories->next==NULL); tree_free(tree); +} + +static void tree_add_extended_test() { + struct tree *tree; + struct file *file; + + char valid[] = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; + - file_free(file1); - file_free(file2); + char files[7][100] = { + "test/test1", + "hello/test2", + "/notalsdkfj asdfkkl /asdlfkj /asdflkjasdf/klaasdlfjasd", + "src/asldfkak/a/aslkdfj//a/lsd/as/alskdf/lasdfjasdfasdlfkajsdfa/sdf", + "//./asd//sd/f/sadfasldklfasld///s/d/flskdfkl/asdk", + "..", + "." + }; + + assert(1==tree_init(&tree,NULL)); + + for(int i = 0;i<3;i++) { + assert(1==file_init(&file,files[i])); + assert(1==tree_add(tree,file)); + } + + tree_free(tree); } static void tree_init_basic_test() {