...
authoralex <[email protected]>
Sat, 15 Jan 2022 08:03:12 +0000 (00:03 -0800)
committeralex <[email protected]>
Sat, 15 Jan 2022 08:03:12 +0000 (00:03 -0800)
Makefile.am
inc/feed.h
inc/tree.h
src/feed/feeds.c [new file with mode: 0644]
src/feed/gen.c
src/torrent/remove.c
src/tree.c
test/unit/Makefile.am
test/unit/feed.tests.c
test/unit/tree.tests.c

index 2a45b3aaa52610e036e19452b787574068af91cd..b1672fa93ac124fbe966a751e657ee4df60838d1 100644 (file)
@@ -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 \
index 261ed722c24310d2809e6ce0a1d811a5374ad78f..50f50bfdb01e044fa6ee932f69fc32eb8045c953 100644 (file)
@@ -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*);
 
index bfdbad6b78424ce7cee3f02b1c2b7a5ae8a9a927..b41850cfa7f080fd684262563a4f5157b709ec8b 100644 (file)
@@ -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 (file)
index 0000000..5ea7e95
--- /dev/null
@@ -0,0 +1,10 @@
+#include<feed.h>
+
+int feeds() {
+       for(size_t i=0;i<session.torrent_count;i++) {
+               if(feed_generate(session.torrents[i])<0) { return -1; }
+       }
+
+       return 1;
+}
+
index a67aab878750bcb164079a23f00ad1f915509825..23355b8d0176f891d54e555e5dbd5e91dee2ebe4 100644 (file)
@@ -1,19 +1,11 @@
 #include<feed.h>
 
-static int feed_generate(struct torrent*);
-
-int feeds() {
-       for(size_t i=0;i<session.torrent_count;i++) {
-               if(feed_generate(session.torrents[i])<0) { return -1; }
-       }
-
-       return 1;
-}
-
-static int feed_generate(struct torrent *torrent) {
+int feed_generate(struct torrent *torrent) {
        FILE *fp;
        char *path;
 
+       if(NULL==torrent) { return -1; }
+
        if(is_directory(FEED_DIRECTORY)<0) {
                if(directory_create(FEED_DIRECTORY)<0) { return -1; }
        }
index 70fbac369035d0da55444576d68e3b1ceed70b10..5ab07f3ee35301f7d25c74abdc66e8982c7e4109 100644 (file)
@@ -2,15 +2,14 @@
 
 int torrent_remove(struct torrent *torrent, const char *path) {
        struct file *p;
-       if(tree_remove(torrent->tree,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;
 }
index be39db5dc5e6712411d73cf46dfb28b3b781df85..e4f0104aedb34dcdf10a5875ae30958eba77322a 100644 (file)
@@ -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;
+}
index edd06a73027efe6994c184d9c59038333f9dd5e2..d45dacbdba0323564b070b4017d803685f7d4cc1 100644 (file)
@@ -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"
index 95f5c4d47efcd41a89eae796bdc406c768a4efeb..278218ff3c7bf8298ca18085a595c4bcd235ebd9 100644 (file)
@@ -2,8 +2,13 @@
 
 #include<feed.h>
 
+#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);
 }
index cb169b348dd2b03fb93e0c7acc3235ff196af94b..8a1a89fd08527395e9691d9be51aedc7eb4f80d4 100644 (file)
@@ -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);
+}