From: alex Date: Thu, 3 Feb 2022 20:15:39 +0000 (-0800) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=3a0ea89ba9d8452fa6fdca5e9df69695693487fc;p=seeder ... --- diff --git a/inc/opt.h b/inc/opt.h index ecfa853..b7d968f 100644 --- a/inc/opt.h +++ b/inc/opt.h @@ -6,9 +6,8 @@ #include #include -#include - #include +#include enum file_filters { FILE_FILTER_IGNORE_DOTFILES, diff --git a/inc/session.h b/inc/session.h index a3c3e69..8054421 100644 --- a/inc/session.h +++ b/inc/session.h @@ -1,6 +1,8 @@ #ifndef __SESSION_H_ #define __SESSION_H_ +#include + #include #include #include diff --git a/inc/torrent.h b/inc/torrent.h index d6c3b33..cd43544 100644 --- a/inc/torrent.h +++ b/inc/torrent.h @@ -9,7 +9,6 @@ #include #include #include -#include #include struct torrent_files { @@ -32,6 +31,12 @@ struct torrent { int watch_fd; }; +/* + * required to be down belong struct torrent + * because of opt inclusion of session.h + */ +#include + #define TORRENT_FILE_DIRECTORY PREFIX "/torrents" #define TORRENT_FILE_EXTENSION ".torrent" diff --git a/src/feed/feeds.c b/src/feed/feeds.c index 5ea7e95..9d12ffb 100644 --- a/src/feed/feeds.c +++ b/src/feed/feeds.c @@ -1,8 +1,12 @@ #include int feeds() { - for(size_t i=0;isize;i++) { + p = session.torrents.infohashes->map[i]; + if(p!=NULL) { + if(feed_generate(p)<0) { return -1; } + } } return 1; diff --git a/src/session.c b/src/session.c index 6b3a2c6..1a2f2ea 100644 --- a/src/session.c +++ b/src/session.c @@ -8,33 +8,37 @@ static int session_torrent_resize(struct hash_map**,size_t); void session_clean() { // clear infohashes so no double-free - hashmap_clear(session.torrent.infohashes); + hashmap_clear(session.torrents.infohashes); - hashmap_free(session.torrent.infohashes); - hashmap_free(session.torrent.paths); + hashmap_free(session.torrents.infohashes); + hashmap_free(session.torrents.paths); } +#define SESSION_HASHMAP_INITIAL_SIZE 8 + int session_init() { - if(hashmap_init(session.torrents.infohashes,SESSION_HASHMAP_INITIAL_SIZE)<0) { return -1; } - if(hashmap_init(session.torrents.paths,SESSION_HASHMAP_INITIAL_SIZE)<0) { return -1; } + if(hashmap_init(&(session.torrents.infohashes),SESSION_HASHMAP_INITIAL_SIZE)<0) { return -1; } + if(hashmap_init(&(session.torrents.paths),SESSION_HASHMAP_INITIAL_SIZE)<0) { return -1; } return 1; } int session_torrent_add(struct torrent *p) { + int i; + while((i = session_torrent_add_by_infohash(p))<=0) { if(i<0) { return -1; } if(session_torrent_resize( - &(session.torrent.infohashes), /* map */ - session.torrent.infohashes.size<<1 /* new_size */ + &(session.torrents.infohashes), /* map */ + session.torrents.infohashes->size<<1 /* new_size */ )<0) { return -1; } } while((i = session_torrent_add_by_path(p))<=0) { if(i<0) { return -1; } if(session_torrent_resize( - &(session.torrent.paths), /* map */ - session.torrent.paths.size<<1 /* new_size */ + &(session.torrents.paths), /* map */ + session.torrents.paths->size<<1 /* new_size */ )<0) { return -1; } } @@ -45,10 +49,10 @@ static int session_torrent_add_by_infohash(struct torrent *torrent) { struct torrent *p; int i; - if((i = hashmap_insert(session.torrents.infohashes,torrent->infohash,crypto_hash_sha_256_BYTES,p))<=0) { + if((i = hashmap_insert(session.torrents.infohashes,torrent->infohash,crypto_hash_sha256_BYTES,torrent))<=0) { if(i<0) { return -1; } - p = hashmap_find(session.torrents.infohashes,torrent->infohash,crypto_hash_sha_256_BYTES); + p = hashmap_find(session.torrents.infohashes,torrent->infohash,crypto_hash_sha256_BYTES); if((p!=NULL)&&(memcmp(torrent->infohash,p->infohash,crypto_hash_sha256_BYTES)!=0)) { return 0; } } @@ -59,7 +63,7 @@ static int session_torrent_add_by_path(struct torrent *torrent) { struct torrent *p; int i; - if((i = hashmap_insert(session.torrents.paths,torrent->root,strlen(torrent->root),p))<=0) { + if((i = hashmap_insert(session.torrents.paths,torrent->root,strlen(torrent->root),torrent))<=0) { if(i<0) { return -1; } p = hashmap_find(session.torrents.paths,torrent->root,strlen(torrent->root)); @@ -71,6 +75,8 @@ 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; + struct torrent *p; + int flag, ret; if(hashmap_init(&new,new_size)<0) { return -1; } diff --git a/src/watch.c b/src/watch.c index 2f5b9a1..7ee9e5d 100644 --- a/src/watch.c +++ b/src/watch.c @@ -16,6 +16,7 @@ int watch() { } static void *watch_spawn(void *unused) { + struct torrent *p; int fd; fd = inotify_init(); @@ -25,9 +26,12 @@ static void *watch_spawn(void *unused) { return NULL; } - for(size_t i=0;iroot,session.torrents[i]->tree)<0) { - return NULL; + for(size_t i=0;isize;i++) { + p = session.torrents.infohashes->map[i]; + if(p!=NULL) { + if(watch_spawn_all(fd,p->root,p->tree)<0) { + return NULL; + } } } diff --git a/test/unit/Makefile.am b/test/unit/Makefile.am index 1af237b..3942a6e 100644 --- a/test/unit/Makefile.am +++ b/test/unit/Makefile.am @@ -151,7 +151,14 @@ rss_tests_SOURCES = \ session_tests_SOURCES = \ $(common_SOURCES) \ $(torrent_SOURCES) \ - session.tests.c + session.tests.c \ + $(top_srcdir)/src/fs/concat.c \ + $(top_srcdir)/src/fs/dir.c \ + $(top_srcdir)/src/hash.c \ + $(top_srcdir)/src/session.c + +session_tests_CPPFLAGS = $(AM_CPPFLAGS) \ + -DTORRENT_INFO_SRC_FILE="$(top_srcdir)/src/torrent/info.c" torrent_tests_SOURCES = \ $(common_SOURCES) \ diff --git a/test/unit/session.tests.c b/test/unit/session.tests.c index 2ead6e7..55bb859 100644 --- a/test/unit/session.tests.c +++ b/test/unit/session.tests.c @@ -2,12 +2,35 @@ #include +#include INCLUDE(TORRENT_INFO_SRC_FILE) + int main(); +static void session_init_basic_test(); +static void session_torrent_add_basic_test(); int main() { setup_env(); + session_init_basic_test(); + session_torrent_add_basic_test(); + clean_env(); return EXIT_SUCCESS; } + +static void session_init_basic_test() { + assert(1==session_init()); + + session_clean(); +} + +static void session_torrent_add_basic_test() { + struct torrent *p; + + TORRENT_SETUP_EXAMPLE(&p); + + assert(1==session_init()); + + assert(1==session_torrent_add(p)); +}