src/opt/set.c \
src/opt/watch.c \
src/opt/worker.c \
+ src/session.c \
src/setup.c \
src/torrent.c \
src/tree.c \
inc/log.h \
inc/main.h \
inc/opt.h \
+ inc/session.h \
inc/setup.h \
inc/torrent.h \
inc/tree.h \
#ifndef __OPT_H_
#define __OPT_H_
+#include<libgen.h>
#include<string.h>
#include<log.h>
+#include<session.h>
#include<torrent.h>
#include<util.h>
#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"
--- /dev/null
+#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
#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
extern struct torrent **torrents;
void torrent_free(struct torrent*);
-int torrent_init(struct torrent**,char*);
+int torrent_init(struct torrent**,char*,char*);
#endif
#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
#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;
}
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; }
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;
}
--- /dev/null
+#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;
+}
#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;
}
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;
return 1;
}
+
+int setup_session() {
+ if(session_init(&session)<0) { return -1; }
+
+ if(0!=atexit(&session_clean)) {
+ perror("atexit");
+ return -1;
+ }
+
+ return 1;
+}
}
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");
(*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);
(*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);
free(p);
}
+
+int tree_init(struct tree **p) {
+ return -1;
+}