]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Tue, 7 Sep 2021 04:17:39 +0000 (21:17 -0700)
committeralex <[email protected]>
Tue, 7 Sep 2021 04:17:39 +0000 (21:17 -0700)
13 files changed:
Makefile.am
inc/opt.h
inc/session.h [new file with mode: 0644]
inc/setup.h
inc/torrent.h
inc/tree.h
src/add.c
src/main.c
src/opt/watch.c
src/session.c [new file with mode: 0644]
src/setup.c
src/torrent.c
src/tree.c

index 1b20790dc6690a0683d774bec50aab09133add76..0d16745eb430917b245325d129408e609be9dea5 100644 (file)
@@ -23,6 +23,7 @@ seederd_SOURCES = \
        src/opt/set.c \
        src/opt/watch.c \
        src/opt/worker.c \
+       src/session.c \
        src/setup.c \
        src/torrent.c \
        src/tree.c \
@@ -37,6 +38,7 @@ seederd_SOURCES += \
        inc/log.h \
        inc/main.h \
        inc/opt.h \
+       inc/session.h \
        inc/setup.h \
        inc/torrent.h \
        inc/tree.h \
index 4a43e3b28e673c105054c5da2b55ede181cd394a..f185b1671639800cac15dd863af978a81ea2a23f 100644 (file)
--- a/inc/opt.h
+++ b/inc/opt.h
@@ -1,9 +1,11 @@
 #ifndef __OPT_H_
 #define __OPT_H_
 
+#include<libgen.h>
 #include<string.h>
 
 #include<log.h>
+#include<session.h>
 #include<torrent.h>
 #include<util.h>
 
@@ -23,7 +25,7 @@ extern struct options global_options;
 #define OPT_MESSAGE_PIECE_LENGTH_SET "piece length set to %llu\n"
 #define OPT_MESSAGE_UNABLE_OPEN_FILE "unable to open file %s\n"
 #define OPT_MESSAGE_UNKNOWN_OPTION "unknown option %s\n"
-#define OPT_MESSAGE_WATCH_ADD_SUCCESS "watching %s\n"
+#define OPT_MESSAGE_WATCH_ADD_SUCCESS "adding all files in %s to torrent with name %s\n"
 #define OPT_MESSAGE_WATCH_INVALID_DIRECTORY "watching directory %s failed\n"
 #define OPT_MESSAGE_WORKER_THREADS_INVALID "invalid value to option worker_threads: %u\n"
 #define OPT_MESSAGE_WORKER_THREADS_SET "number of worker threads set to %u\n"
diff --git a/inc/session.h b/inc/session.h
new file mode 100644 (file)
index 0000000..73d5293
--- /dev/null
@@ -0,0 +1,22 @@
+#ifndef __SESSION_H_
+#define __SESSION_H_
+
+#include<log.h>
+#include<torrent.h>
+
+#define SESSION_MESSAGE_REALLOC_COUNT "(re)allocating memory for session torrents to %u\n"
+
+#define SESSION_REALLOC_COUNT 100
+
+struct session {
+       struct torrent **torrents;
+       size_t torrent_count;
+};
+
+extern struct session session;
+
+void session_clean();
+int session_init();
+int session_torrent_add(struct torrent*);
+
+#endif
index 8d5d5260223405cae4dc7f3347d428fc1149cbdd..a255aaf52b5ba8806249aee082f1f4bc85f8ec76 100644 (file)
@@ -1,11 +1,10 @@
 #ifndef __SETUP_H_
 #define __SETUP_H_
 
-#include<add.h>
 #include<log.h>
+#include<session.h>
 
 int setup();
-int setup_adding();
-int setup_logging();
+int setup_session();
 
 #endif
index 4e1e7c0739e29aabb367465d772a3fb0f9bba129..19963b0ca44caaf166b9898da7570eb85048e1db 100644 (file)
@@ -17,6 +17,6 @@ struct torrent {
 extern struct torrent **torrents;
 
 void torrent_free(struct torrent*);
-int torrent_init(struct torrent**,char*);
+int torrent_init(struct torrent**,char*,char*);
 
 #endif
index 666362878fcbce33e523ecf18d83c27cdbc852f2..3ca98d090eaafee80c3d281d8905e85112bd01d0 100644 (file)
@@ -1,13 +1,18 @@
 #ifndef __TREE_H_
 #define __TREE_H_
 
+#include<stdlib.h>
+
+#include<file.h>
+
 struct tree {
-       struct file *files;
+       struct file **files;
        size_t file_count;
-       struct tree *directories;
+       struct tree **directories;
        size_t directory_count;
 };
 
 void tree_free(struct tree*);
+int tree_init(struct tree**);
 
 #endif
index 84984b1175e2f014b7f17e2e9f5f9363a1743769..d65c8ab1971f660167fd84e814695c969c698405 100644 (file)
--- a/src/add.c
+++ b/src/add.c
@@ -1,5 +1,17 @@
 #include<add.h>
 
+//pthread_t adding_thread;
+//pthread_mutex_t adding_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+//static int setup_adding() {
+//     if(pthread_create(&adding_thread,NULL,&add,NULL)!=0) {
+//             perror("pthread_create");
+//             return -1;
+//     }
+
+//     return 1;
+//}
+
 void *add(void *p) {
        return NULL;
 }
index b4d5d779823608c5f60342690009e1576c5f8106..204264ad40186a91fa0f325b27ab21cd2444b535 100644 (file)
@@ -14,6 +14,8 @@ static struct option long_options[] = {
 int main(int argc, char **argv) {
        int c;
 
+       if(setup_session()<0) { return EXIT_FAILURE; }
+
        if(defaults()<0) { return EXIT_FAILURE; }
        if(opt_load_from_env()<0) { return EXIT_FAILURE; }
 
index c1a7736fc45e7c8e1d6b1b5285b9595bbb2caace..a84bae49c3ea56a39239fb165114f491bbe0fe9d 100644 (file)
@@ -8,11 +8,10 @@ int opt_add_watch(char *directory) {
                return -1;
        }
 
-       if(torrent_init(&p,directory)<0) { return -1; }
-       torrent_free(p);
+       if(torrent_init(&p,directory,basename(directory))<0) { return -1; }
+       if(session_torrent_add(p)<0) { return -1; }
 
-       log_msg(OPT_MESSAGE_WATCH_ADD_SUCCESS,directory);
+       log_msg(OPT_MESSAGE_WATCH_ADD_SUCCESS,p->root,p->name);
 
-       log_err("not implemented\n");
-       return -1;
+       return 1;
 }
diff --git a/src/session.c b/src/session.c
new file mode 100644 (file)
index 0000000..6b91b83
--- /dev/null
@@ -0,0 +1,46 @@
+#include<session.h>
+
+struct session session;
+
+void session_clean() {
+       struct torrent *torrent_p;
+       if(session.torrents!=NULL) {
+               while(session.torrent_count>0) {
+                       torrent_p = session.torrents[session.torrent_count];
+                       if(torrent_p!=NULL) { torrent_free(torrent_p); }
+                       session.torrent_count--;
+               }
+       }
+       free(session.torrents);
+}
+
+int session_init() {
+       session.torrents = NULL;
+       session.torrent_count = 0;
+
+       return 1;
+}
+
+int session_torrent_add(struct torrent *p) {
+       size_t new_count;
+
+       if((NULL==session.torrents)||(session.torrent_count%SESSION_REALLOC_COUNT==0)) {
+               new_count = session.torrent_count+SESSION_REALLOC_COUNT;
+               log_info(SESSION_MESSAGE_REALLOC_COUNT,new_count);
+
+               session.torrents = realloc(session.torrents,sizeof(struct torrent*)*new_count);
+               if(NULL==session.torrents) {
+                       perror("realloc");
+                       return -1;
+               }
+
+               for(size_t i=session.torrent_count;i<new_count;i++) {
+                       session.torrents[i] = NULL;
+               }
+       }
+
+       session.torrents[session.torrent_count] = p;
+       session.torrent_count++;
+
+       return 1;
+}
index 8c9f010227abda0298a3d8e0dba17d7914ee1855..166572a50493b5b80759d58828ebad22a5539962 100644 (file)
@@ -1,19 +1,12 @@
 #include<setup.h>
 
+static int setup_logging();
+
 int setup() {
+       // logging must be setup first
        if(setup_logging()<0) { return -1; }
-       if(setup_adding()<0) { return -1; }
-       return 1;
-}
-
-pthread_t adding_thread;
-pthread_mutex_t adding_mutex = PTHREAD_MUTEX_INITIALIZER;
 
-int setup_adding() {
-       if(pthread_create(&adding_thread,NULL,&add,NULL)!=0) {
-               perror("pthread_create");
-               return -1;
-       }
+       if(setup_session()<0) { return -1; }
 
        return 1;
 }
@@ -21,7 +14,7 @@ int setup_adding() {
 pthread_t logging_thread;
 pthread_mutex_t logging_mutex = PTHREAD_MUTEX_INITIALIZER;
 
-int setup_logging() {
+static int setup_logging() {
        if(0!=atexit(&log_entries_clean)) {
                perror("atexit");
                return -1;
@@ -40,3 +33,14 @@ int setup_logging() {
 
        return 1;
 }
+
+int setup_session() {
+       if(session_init(&session)<0) { return -1; }
+
+       if(0!=atexit(&session_clean)) {
+               perror("atexit");
+               return -1;
+       }
+
+       return 1;
+}
index 5611595e88a5cc48c13b67d6c054440299373d1f..000a6529430831ff6b7afb2e453e7a67b51d6e18 100644 (file)
@@ -10,8 +10,6 @@ void torrent_free(struct torrent *p) {
 }
 
 int torrent_init(struct torrent **torrent_p, char *root, char *name) {
-       char *p;
-
        *(torrent_p) = malloc(sizeof(struct torrent));
        if(NULL==(*torrent_p)) {
                perror("malloc");
@@ -25,7 +23,7 @@ int torrent_init(struct torrent **torrent_p, char *root, char *name) {
        (*torrent_p)->root = malloc(strlen(root)+1);
        if(NULL==(*torrent_p)->root) {
                perror("malloc");
-               torrent_free(torrent_p);
+               torrent_free(*torrent_p);
                return -1;
        }
        strcpy((*torrent_p)->root,root);
@@ -33,7 +31,7 @@ int torrent_init(struct torrent **torrent_p, char *root, char *name) {
        (*torrent_p)->name = malloc(strlen(name)+1);
        if(NULL==(*torrent_p)->name) {
                perror("malloc");
-               torrent_free(torrent_p);
+               torrent_free(*torrent_p);
                return -1;
        }
        strcpy((*torrent_p)->name,name);
index 6ac3b7f3104f96b83285ac818983ec8e9331b96f..ac9f08659e8fdbfcb7c50a51ad54a0b3fb06e823 100644 (file)
@@ -13,3 +13,7 @@ void tree_free(struct tree *p) {
 
        free(p);
 }
+
+int tree_init(struct tree **p) {
+       return -1;
+}