...
authoralex <[email protected]>
Thu, 3 Feb 2022 20:15:39 +0000 (12:15 -0800)
committeralex <[email protected]>
Thu, 3 Feb 2022 20:15:39 +0000 (12:15 -0800)
inc/opt.h
inc/session.h
inc/torrent.h
src/feed/feeds.c
src/session.c
src/watch.c
test/unit/Makefile.am
test/unit/session.tests.c

index ecfa853b43b7975bab1bf4df372a86f05d465623..b7d968f0c7df338e97ece980965310b93b03c564 100644 (file)
--- a/inc/opt.h
+++ b/inc/opt.h
@@ -6,9 +6,8 @@
 
 #include<fs.h>
 #include<log.h>
-#include<torrent.h>
-
 #include<session.h>
+#include<torrent.h>
 
 enum file_filters {
        FILE_FILTER_IGNORE_DOTFILES,
index a3c3e690ff71ac5302cc9c652a8af8e2dc319559..8054421383d56315ae4c3505a59e771b4091ecab 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef __SESSION_H_
 #define __SESSION_H_
 
+#include<sodium.h>
+
 #include<hashmap.h>
 #include<log.h>
 #include<torrent.h>
index d6c3b33df870a439662f6a55826fbe270c1d71d7..cd43544a2fa3a93144c1a487c8a52ac95b026c4f 100644 (file)
@@ -9,7 +9,6 @@
 #include<file.h>
 #include<fs.h>
 #include<hashmap.h>
-#include<opt.h>
 #include<tree.h>
 
 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<opt.h>
+
 #define TORRENT_FILE_DIRECTORY PREFIX "/torrents"
 #define TORRENT_FILE_EXTENSION ".torrent"
 
index 5ea7e95f5e880fb0edc1c970ea7a0239e5a6058d..9d12ffb330c77e22eb254b1edcd43b9d1e934236 100644 (file)
@@ -1,8 +1,12 @@
 #include<feed.h>
 
 int feeds() {
-       for(size_t i=0;i<session.torrent_count;i++) {
-               if(feed_generate(session.torrents[i])<0) { return -1; }
+       struct torrent *p;
+       for(size_t i=0;i<session.torrents.infohashes->size;i++) {
+               p = session.torrents.infohashes->map[i];
+               if(p!=NULL) {
+                       if(feed_generate(p)<0) { return -1; }
+               }
        }
 
        return 1;
index 6b3a2c64b8d7af55d8663864e0cc3a31113fc792..1a2f2ea9ba2c89cd75005838bd8f23011ff76087 100644 (file)
@@ -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; }
 
index 2f5b9a1b231b78810720de21ea97827a9579e693..7ee9e5d820e558895bfb8d4c5e5c5f5cd357c74b 100644 (file)
@@ -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;i<session.torrent_count;i++) {
-               if(watch_spawn_all(fd,session.torrents[i]->root,session.torrents[i]->tree)<0) {
-                       return NULL;
+       for(size_t i=0;i<session.torrents.infohashes->size;i++) {
+               p = session.torrents.infohashes->map[i];
+               if(p!=NULL) {
+                       if(watch_spawn_all(fd,p->root,p->tree)<0) {
+                               return NULL;
+                       }
                }
 
        }
index 1af237b168643b4f081bc964d8296d30dcccc5d4..3942a6eabfa4101876050d2041251102d93ab666 100644 (file)
@@ -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) \
index 2ead6e7618017cb32e514fe57e9adf60b9c41272..55bb859f41ad2d52d4d58437f69d6ba9e2a967df 100644 (file)
@@ -2,12 +2,35 @@
 
 #include<session.h>
 
+#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));
+}