From a406b997879e23c5dfae071879513378b845cbf9 Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 14 Jan 2022 17:56:12 -0800 Subject: [PATCH] ... --- Makefile.am | 1 + inc/torrent.h | 1 + src/feed/entries.c | 3 +++ src/torrent/remove.c | 16 ++++++++++++++++ test/unit/feed.tests.c | 2 +- test/unit/test_macros.h | 9 ++++++++- test/unit/torrent.tests.c | 21 +++++++++++++++------ test/unit/tree.tests.c | 23 +++++++++++++++++++++++ 8 files changed, 68 insertions(+), 8 deletions(-) create mode 100644 src/torrent/remove.c diff --git a/Makefile.am b/Makefile.am index 6a28b3c..2a45b3a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -60,6 +60,7 @@ seederd_SOURCES = \ src/torrent/magnet.c \ src/torrent/path.c \ src/torrent/piece.c \ + src/torrent/remove.c \ src/tree.c \ src/usage.c \ src/watch.c diff --git a/inc/torrent.h b/inc/torrent.h index 69c3511..76fd5e8 100644 --- a/inc/torrent.h +++ b/inc/torrent.h @@ -41,5 +41,6 @@ int torrent_file_piece_layers(FILE*,struct torrent*); void torrent_free(struct torrent*); int torrent_init(struct torrent**,unsigned long long); char *torrent_magnet(struct torrent*); +int torrent_remove(struct torrent*,const char*); #endif diff --git a/src/feed/entries.c b/src/feed/entries.c index e5f090c..ec1fd01 100644 --- a/src/feed/entries.c +++ b/src/feed/entries.c @@ -7,6 +7,9 @@ static int entry_default(struct rss_entry *entry, const char *feed_url, struct t entry->title = strdup(p->name); if(NULL==entry->title) { return -1; } + entry->link = concat(feed_url,p->name); + if(NULL==entry->link) { return -1; } + entry->pub_date = *localtime(&(time_t){time(NULL)}); return 1; diff --git a/src/torrent/remove.c b/src/torrent/remove.c new file mode 100644 index 0000000..70fbac3 --- /dev/null +++ b/src/torrent/remove.c @@ -0,0 +1,16 @@ +#include + +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); + } + + return 1; +} diff --git a/test/unit/feed.tests.c b/test/unit/feed.tests.c index 19079f2..95f5c4d 100644 --- a/test/unit/feed.tests.c +++ b/test/unit/feed.tests.c @@ -56,7 +56,7 @@ static void feed_info_basic_test() { assert(feed_info(NULL,NULL)==-1); assert(feed_info(fp,NULL)==-1); - assert(1==torrent_init(&p,TEST_DIRECTORY,TEST_DIRECTORY,16384)); + TORRENT_SETUP_EXAMPLE(&p); assert(1==feed_info(fp,p)); diff --git a/test/unit/test_macros.h b/test/unit/test_macros.h index 9c9233f..3b52da6 100644 --- a/test/unit/test_macros.h +++ b/test/unit/test_macros.h @@ -63,7 +63,14 @@ TORRENT_SETUP_EXAMPLE_FILE(&file1,TEST_FILE_1,1); \ TORRENT_SETUP_EXAMPLE_FILE(&file2,TEST_FILE_2,2); \ \ - assert(torrent_init(p,TEST_DIRECTORY,TEST_DIRECTORY,16384)==1); \ + assert(torrent_init(p,16384)==1); \ +\ + (*p)->root = strdup(TEST_DIRECTORY); \ + assert((*p)->root!=NULL); \ + (*p)->name = strdup(TEST_DIRECTORY); \ + assert((*p)->name!=NULL); \ + (*p)->feed_url = strdup("https://test.com"); \ + assert((*p)->feed_url!=NULL); \ \ assert(torrent_add(*p,file1)==1); \ assert(torrent_add(*p,file2)==1); \ diff --git a/test/unit/torrent.tests.c b/test/unit/torrent.tests.c index 4ae8528..70792e4 100644 --- a/test/unit/torrent.tests.c +++ b/test/unit/torrent.tests.c @@ -43,7 +43,12 @@ static void torrent_add_basic_test() { assert(file_init(&file4,TEST_FILE_4)==1); memset(file4->root,4,crypto_hash_sha256_BYTES); - assert(torrent_init(&torrent,TEST_DIRECTORY,TEST_DIRECTORY,16384)==1); + assert(torrent_init(&torrent,16384)==1); + + torrent->root = strdup(TEST_DIRECTORY); + assert(torrent->root!=NULL); + torrent->name = strdup(TEST_DIRECTORY); + assert(torrent->name!=NULL); assert(torrent_add(torrent,file1)==1); assert(torrent_add(torrent,file2)==1); @@ -120,7 +125,12 @@ static void torrent_file_info_conformance_test() { const char infohash_expected[] = "267a5a34b592074f74287fc221a6cf64a3caad36377e57c5c5f64caea0527646"; const char torrent_file_hash_expected[] = "b39f0f4c88e8de4576d02969dfbefac0bbd6de2c9ab49109632e417f4ceb23ad"; - assert(torrent_init(&torrent_p,TEST_DIRECTORY,TEST_DIRECTORY,16384)==1); + assert(torrent_init(&torrent_p,16384)==1); + + torrent_p->root = strdup(TEST_DIRECTORY); + assert(torrent_p->root!=NULL); + torrent_p->name = strdup(TEST_DIRECTORY); + assert(torrent_p->name!=NULL); assert(1==file_init(&file_p,TEST_FILE_1)); assert(file_hash(file_p,16384)==1); @@ -208,11 +218,10 @@ static void torrent_file_piece_layers_basic_test() { static void torrent_init_basic_test() { struct torrent *torrent; - assert(torrent_init(&torrent,NULL,NULL,0)<0); - assert(torrent_init(&torrent,"src",NULL,0)<0); - assert(torrent_init(&torrent,"src","src",1000)<0); + assert(torrent_init(&torrent,0)<0); + assert(torrent_init(&torrent,1000)<0); - assert(torrent_init(&torrent,"src","src",16384)==1); + assert(torrent_init(&torrent,16384)==1); torrent_free(torrent); } diff --git a/test/unit/tree.tests.c b/test/unit/tree.tests.c index c86529d..cb169b3 100644 --- a/test/unit/tree.tests.c +++ b/test/unit/tree.tests.c @@ -10,6 +10,7 @@ static void tree_add_extended_test(); 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(); int main() { setup_env(); @@ -20,6 +21,7 @@ int main() { tree_add_extended_test(); tree_add_alphabetic_test(); tree_bencode_length_basic_test(); + tree_length_basic_test(); clean_env(); @@ -267,3 +269,24 @@ static void tree_init_basic_test() { tree_free(p); } + +static void tree_length_basic_test() { + struct tree *p; + struct file *file; + + assert(1==tree_init(&p)); + + assert(0==tree_length(p)); + + assert(1==file_init(&file,"test")); + + assert(1==tree_add(p,file->path,file)); + assert(1==tree_length(p)); + + assert(1==file_init(&file,"test2")); + + assert(1==tree_add(p,file->path,file)); + assert(2==tree_length(p)); + + tree_free(p); +} -- 2.30.2