From 0bf16116bb2ec8542260ef399063876ff3b69da9 Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 15 Jan 2022 00:03:12 -0800 Subject: [PATCH] ... --- Makefile.am | 1 + inc/feed.h | 1 + inc/tree.h | 1 + src/feed/feeds.c | 10 ++++++ src/feed/gen.c | 14 ++------ src/torrent/remove.c | 5 ++- src/tree.c | 78 ++++++++++++++++++++++++++++++++++++++++++ test/unit/Makefile.am | 19 ++++++---- test/unit/feed.tests.c | 26 ++++++++++++-- test/unit/tree.tests.c | 47 +++++++++++++++++++++++++ 10 files changed, 180 insertions(+), 22 deletions(-) create mode 100644 src/feed/feeds.c diff --git a/Makefile.am b/Makefile.am index 2a45b3a..b1672fa 100644 --- a/Makefile.am +++ b/Makefile.am @@ -18,6 +18,7 @@ seederd_SOURCES = \ src/block.c \ src/default.c \ src/feed/entries.c \ + src/feed/feeds.c \ src/feed/gen.c \ src/feed/info.c \ src/feed/path.c \ diff --git a/inc/feed.h b/inc/feed.h index 261ed72..50f50bf 100644 --- a/inc/feed.h +++ b/inc/feed.h @@ -10,6 +10,7 @@ int feeds(); int feed_entries(FILE*,struct torrent*); +int feed_generate(struct torrent*); int feed_info(FILE*,struct torrent*); char *feed_path(const char*); diff --git a/inc/tree.h b/inc/tree.h index bfdbad6..b41850c 100644 --- a/inc/tree.h +++ b/inc/tree.h @@ -28,5 +28,6 @@ int tree_entry_init(struct tree_entry**, struct file*); void tree_free(struct tree*); int tree_init(struct tree**); size_t tree_length(const struct tree*); +int tree_remove(struct tree*,const char*); #endif diff --git a/src/feed/feeds.c b/src/feed/feeds.c new file mode 100644 index 0000000..5ea7e95 --- /dev/null +++ b/src/feed/feeds.c @@ -0,0 +1,10 @@ +#include + +int feeds() { + for(size_t i=0;i -static int feed_generate(struct torrent*); - -int feeds() { - for(size_t i=0;itree,path)<0) { return -1; } p = hashmap_remove(torrent->files.paths,path,strlen(path)); if(p!=NULL) { p = hashmap_remove(torrent->files.roots,p->root,crypto_hash_sha256_BYTES); if(NULL==p) { return -1; } - - file_free(p); } + if(tree_remove(torrent->tree,path)<0) { return -1; } + return 1; } diff --git a/src/tree.c b/src/tree.c index be39db5..e4f0104 100644 --- a/src/tree.c +++ b/src/tree.c @@ -3,7 +3,9 @@ static int advance(ssize_t,uint8_t**,size_t*); static struct tree* tree_add_directory(struct tree*,const char*); static int tree_add_file(struct tree*,struct file*); +static struct tree_entry *tree_entry_find(struct tree*,const char*); static int tree_entry_init_directory(struct tree_entry**,const char*); +static struct tree_entry *tree_entry_remove(struct tree*,const char*); static int advance(ssize_t i, uint8_t **buf, size_t *len) { if(i<0) { return -1; } @@ -194,6 +196,21 @@ size_t tree_bencode_length(struct tree *tree) { return i; } +struct tree_entry *tree_entry_find(struct tree *tree, const char *name) { + struct tree_entry *p; + + p = tree->entries; + while(p!=NULL) { + if(strcmp(p->name,name)==0) { + return p; + } + + p = p->next; + } + + return NULL; +} + void tree_entry_free(struct tree_entry *p) { if(NULL==p) { return; } @@ -249,6 +266,30 @@ clean: return -1; } +static struct tree_entry *tree_entry_remove(struct tree *tree, const char *name) { + struct tree_entry *prev, *p; + + prev = NULL; + p = tree->entries; + while(p!=NULL) { + if(strcmp(p->name,name)==0) { + if(prev!=NULL) { + prev->next = p->next; + } else { + tree->entries = p->next; + } + + p->next = NULL; + return p; + } + + prev = p; + p = p->next; + } + + return NULL; +} + void tree_free(struct tree *p) { if(NULL==p) { return; } @@ -284,3 +325,40 @@ size_t tree_length(const struct tree *root) { return i; } + +int tree_remove(struct tree *tree, const char *to_remove) { + struct tree_entry *entry_p; + char *path, *p, *prev; + + if(NULL==tree) { return -1; } + if(NULL==to_remove) { return -1; } + + path = strdup(to_remove); + + prev = NULL; + p = strtok(path,"/"); + while(p!=NULL) { + if(prev!=NULL) { // prev contains next subdirectory + entry_p = tree_entry_find(tree,prev); + if(NULL==entry_p) { goto clean; } + + if(NULL==entry_p->children) { goto clean; } + + tree = entry_p->children; + } + + prev = p; + p = strtok(NULL,"/"); + } + + entry_p = tree_entry_remove(tree,prev); + if(NULL==entry_p) { goto clean; } + + tree_entry_free(entry_p); + free(path); + + return 1; +clean: + free(path); + return 0; +} diff --git a/test/unit/Makefile.am b/test/unit/Makefile.am index edd06a7..d45dacb 100644 --- a/test/unit/Makefile.am +++ b/test/unit/Makefile.am @@ -30,8 +30,13 @@ torrent_SOURCES = \ $(top_srcdir)/src/file.c \ $(top_srcdir)/src/hashmap.c \ $(top_srcdir)/src/torrent/add.c \ + $(top_srcdir)/src/torrent/file.c \ $(top_srcdir)/src/torrent/free.c \ $(top_srcdir)/src/torrent/init.c \ + $(top_srcdir)/src/torrent/magnet.c \ + $(top_srcdir)/src/torrent/path.c \ + $(top_srcdir)/src/torrent/piece.c \ + $(top_srcdir)/src/torrent/remove.c \ $(top_srcdir)/src/tree.c bencode_tests_SOURCES = \ @@ -52,16 +57,22 @@ feed_tests_SOURCES = \ feed.tests.c \ $(top_srcdir)/src/feed/entries.c \ $(top_srcdir)/src/feed/info.c \ - $(top_srcdir)/src/feed/path.c \ $(top_srcdir)/src/fs/concat.c \ $(top_srcdir)/src/fs/dir.c \ $(top_srcdir)/src/hash.c \ $(top_srcdir)/src/meta.c \ $(top_srcdir)/src/rss/entry.c \ $(top_srcdir)/src/rss/free.c \ + $(top_srcdir)/src/rss/footer.c \ + $(top_srcdir)/src/rss/header.c \ $(top_srcdir)/src/rss/info.c \ $(top_srcdir)/src/rss/init.c +feed_tests_CPPFLAGS = $(AM_CPPFLAGS) \ + -DFEED_GEN_SRC_FILE="$(top_srcdir)/src/feed/gen.c" \ + -DFEED_PATH_SRC_FILE="$(top_srcdir)/src/feed/path.c" \ + -DTORRENT_INFO_SRC_FILE="$(top_srcdir)/src/torrent/info.c" + file_tests_SOURCES = \ $(common_SOURCES) \ file.tests.c \ @@ -118,11 +129,7 @@ torrent_tests_SOURCES = \ torrent.tests.c \ $(top_srcdir)/src/fs/concat.c \ $(top_srcdir)/src/fs/dir.c \ - $(top_srcdir)/src/hash.c \ - $(top_srcdir)/src/torrent/file.c \ - $(top_srcdir)/src/torrent/magnet.c \ - $(top_srcdir)/src/torrent/path.c \ - $(top_srcdir)/src/torrent/piece.c + $(top_srcdir)/src/hash.c torrent_tests_CPPFLAGS = $(AM_CPPFLAGS) \ -DTORRENT_INFO_SRC_FILE="$(top_srcdir)/src/torrent/info.c" diff --git a/test/unit/feed.tests.c b/test/unit/feed.tests.c index 95f5c4d..278218f 100644 --- a/test/unit/feed.tests.c +++ b/test/unit/feed.tests.c @@ -2,8 +2,13 @@ #include +#include INCLUDE(FEED_PATH_SRC_FILE) +#include INCLUDE(FEED_GEN_SRC_FILE) +#include INCLUDE(TORRENT_INFO_SRC_FILE) + int main(); static void feed_entries_basic_test(); +static void feed_generate_basic_test(); static void feed_info_basic_test(); static void feed_path_basic_test(); static FILE *setup_file_pointer(); @@ -14,6 +19,7 @@ int main() { feed_info_basic_test(); feed_entries_basic_test(); feed_path_basic_test(); + feed_generate_basic_test(); clean_env(); @@ -24,7 +30,7 @@ static void feed_entries_basic_test() { FILE *fp; struct torrent *p; unsigned char hash[crypto_hash_sha256_BYTES]; - unsigned char expected[crypto_hash_sha256_BYTES] = {185,160,108,238,240,237,217,219,97,190,118,39,179,198,241,61,191,24,149,75,124,56,189,148,163,13,185,110,30,115,222,186}; + unsigned char expected[crypto_hash_sha256_BYTES] = {247,202,150,77,202,46,21,104,21,158,65,19,138,184,234,91,149,83,166,163,25,206,148,227,250,179,195,162,207,128,251,211}; fp = setup_file_pointer(); @@ -33,6 +39,8 @@ static void feed_entries_basic_test() { assert(-1==feed_entries(NULL,NULL)); assert(-1==feed_entries(fp,NULL)); + assert(torrent_remove(p,"/test2")==1); + assert(1==feed_entries(fp,p)); torrent_free(p); @@ -45,6 +53,20 @@ static void feed_entries_basic_test() { reset_env(); } +static void feed_generate_basic_test() { + struct torrent *p; + + TORRENT_SETUP_EXAMPLE(&p); + + assert(feed_generate(NULL)==-1); + + assert(feed_generate(p)==1); + + torrent_free(p); + + reset_env(); +} + static void feed_info_basic_test() { FILE *fp; struct torrent *p; @@ -78,7 +100,7 @@ static void feed_path_basic_test() { p = feed_path("test"); assert(p!=NULL); - assert(strcmp(p,TEST_DIRECTORY "/test" FEED_FILE_EXTENSION)==0); + assert(strcmp(p,FEED_DIRECTORY "/test" FEED_FILE_EXTENSION)==0); free(p); } diff --git a/test/unit/tree.tests.c b/test/unit/tree.tests.c index cb169b3..8a1a89f 100644 --- a/test/unit/tree.tests.c +++ b/test/unit/tree.tests.c @@ -11,6 +11,7 @@ static void tree_bencode_basic_test(); static void tree_bencode_length_basic_test(); static void tree_init_basic_test(); static void tree_length_basic_test(); +static void tree_remove_basic_test(); int main() { setup_env(); @@ -22,6 +23,7 @@ int main() { tree_add_alphabetic_test(); tree_bencode_length_basic_test(); tree_length_basic_test(); + tree_remove_basic_test(); clean_env(); @@ -290,3 +292,48 @@ static void tree_length_basic_test() { tree_free(p); } + +static void tree_remove_basic_test() { + struct tree *p; + struct file *file; + + assert(1==tree_init(&p)); + + assert(-1==tree_remove(NULL,NULL)); + assert(-1==tree_remove(p,NULL)); + + assert(1==file_init(&file,"/test/hello/what?")); + + assert(1==tree_add(p,"/test/hello/what?",file)); + + assert(1==file_init(&file,"/test/hello/temp")); + assert(1==tree_add(p,"/test/hello/temp",file)); + + assert(1==file_init(&file,"test3")); + assert(1==tree_add(p,"/test3",file)); + + assert(2==tree_length(p)); + + assert(p->entries->children!=NULL); + assert(strcmp(p->entries->children->entries->name,"hello")==0); + + assert(2==tree_length(p->entries->children->entries->children)); + + assert(1==tree_remove(p,"/test3")); + assert(1==tree_length(p)); + + assert(2==tree_length(p->entries->children->entries->children)); + + assert(1==tree_remove(p,"/test/hello/temp")); + assert(1==tree_length(p->entries->children->entries->children)); + + assert(1==tree_remove(p,"/test/hello")); + + assert(p->entries->children!=NULL); + assert(0==tree_length(p->entries->children)); + + assert(p->entries->children->entries==NULL); + assert(1==tree_length(p)); + + tree_free(p); +} -- 2.30.2