From 26610be2d2d996ab2df039100a423b398b8334a1 Mon Sep 17 00:00:00 2001 From: alex Date: Wed, 2 Feb 2022 17:03:57 -0800 Subject: [PATCH] ... --- inc/opt.h | 3 ++- inc/session.h | 1 + src/add.c | 9 +++++++-- src/session.c | 38 ++++++++++++++++++++++++++++++++++++++ test/unit/Makefile.am | 7 ++++++- test/unit/session.tests.c | 13 +++++++++++++ 6 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 test/unit/session.tests.c diff --git a/inc/opt.h b/inc/opt.h index b7d968f..ecfa853 100644 --- a/inc/opt.h +++ b/inc/opt.h @@ -6,9 +6,10 @@ #include #include -#include #include +#include + enum file_filters { FILE_FILTER_IGNORE_DOTFILES, FILE_FILTER_ALL, diff --git a/inc/session.h b/inc/session.h index 158e68d..a3c3e69 100644 --- a/inc/session.h +++ b/inc/session.h @@ -1,6 +1,7 @@ #ifndef __SESSION_H_ #define __SESSION_H_ +#include #include #include diff --git a/src/add.c b/src/add.c index c1d28c5..ec71143 100644 --- a/src/add.c +++ b/src/add.c @@ -14,10 +14,15 @@ static struct file *get_next(); static int ftw_helper(const char*,const struct stat*,int); int add() { + struct torrent *p; + if(hashmap_init(&add_queue,ADD_QUEUE_INITIAL_SIZE)<0) { return -1; } - for(size_t i=0;isize;i++) { + p = session.torrents.infohashes->map[i]; + if(p!=NULL) { + if(add_find_all(p)<0) { return -1; } + } } add_queue_index = 0; diff --git a/src/session.c b/src/session.c index 7b44b97..6b3a2c6 100644 --- a/src/session.c +++ b/src/session.c @@ -70,5 +70,43 @@ static int session_torrent_add_by_path(struct torrent *torrent) { } static int session_torrent_resize(struct hash_map **map, size_t new_size) { + struct hash_map *new, *old; + + if(hashmap_init(&new,new_size)<0) { return -1; } + + flag = 0; + if(session.torrents.paths==(*map)) { flag = 1; } + + for(size_t i=0;i<(*map)->size;i++) { + p = (*map)->map[i]; + if(p!=NULL) { + if(flag) { + if((ret = hashmap_insert(new,p->root,strlen(p->root),p))<=0) { + if(ret<0) { goto clean; } + goto resize; + } + } else { + if((ret = hashmap_insert(new,p->infohash,crypto_hash_sha256_BYTES,p))<=0) { + if(ret<0) { goto clean; } + goto resize; + } + } + } + } + + hashmap_clear(*map); + old = *map; + *map = new; + + hashmap_free(old); + + return 1; +resize: + hashmap_clear(new); + hashmap_free(new); + return session_torrent_resize(map,new_size<<1); +clean: + hashmap_clear(new); + hashmap_free(new); return -1; } diff --git a/test/unit/Makefile.am b/test/unit/Makefile.am index b0db911..1af237b 100644 --- a/test/unit/Makefile.am +++ b/test/unit/Makefile.am @@ -13,7 +13,7 @@ AM_CPPFLAGS += \ -DNDEBUG endif -check_PROGRAMS = bencode.tests block.tests feed.tests file.tests fs.concat.tests fs.filter.tests hash.tests hashmap.tests meta.tests opt.tests rss.tests torrent.tests tree.tests +check_PROGRAMS = bencode.tests block.tests feed.tests file.tests fs.concat.tests fs.filter.tests hash.tests hashmap.tests meta.tests opt.tests rss.tests session.tests torrent.tests tree.tests TESTS = $(check_PROGRAMS) if ENABLE_MEMCHECK @@ -148,6 +148,11 @@ rss_tests_SOURCES = \ $(top_srcdir)/src/rss/info.c \ $(top_srcdir)/src/rss/init.c +session_tests_SOURCES = \ + $(common_SOURCES) \ + $(torrent_SOURCES) \ + session.tests.c + torrent_tests_SOURCES = \ $(common_SOURCES) \ $(torrent_SOURCES) \ diff --git a/test/unit/session.tests.c b/test/unit/session.tests.c new file mode 100644 index 0000000..2ead6e7 --- /dev/null +++ b/test/unit/session.tests.c @@ -0,0 +1,13 @@ +#include + +#include + +int main(); + +int main() { + setup_env(); + + clean_env(); + + return EXIT_SUCCESS; +} -- 2.30.2