seederd_SOURCES = \
src/add.c \
src/default.c \
+ src/file.c \
src/log.c \
src/main.c \
src/opt/config.c \
src/opt/worker.c \
src/setup.c \
src/torrent.c \
+ src/tree.c \
src/usage.c \
src/util/dir.c \
src/util/file.c
seederd_SOURCES += \
inc/add.h \
inc/default.h \
+ inc/file.h \
inc/log.h \
inc/main.h \
inc/opt.h \
inc/setup.h \
inc/torrent.h \
+ inc/tree.h \
inc/usage.h \
inc/util.h
--- /dev/null
+#ifndef __FILE_H_
+#define __FILE_H_
+
+struct file {
+ const char *name;
+};
+
+void file_free(struct file*);
+
+#endif
#define LOG_MESSAGE_PREFIX "%s "
#endif
-#define log_err(...) log_message_with_prefix(LOG_LEVEL_ERRORS,stderr,LOG_MESSAGE_PREFIX __VA_ARGS__)
-#define log_info(...) log_message_with_prefix(LOG_LEVEL_VERBOSE,stdout,LOG_MESSAGE_PREFIX __VA_ARGS__)
-#define log_msg(...) log_message_with_prefix(LOG_LEVEL_DEFAULT,stdout,LOG_MESSAGE_PREFIX __VA_ARGS__)
+#define log_err(...) log_message(LOG_LEVEL_ERRORS,stderr,__VA_ARGS__)
+#define log_info(...) log_message(LOG_LEVEL_VERBOSE,stdout,__VA_ARGS__)
+#define log_msg(...) log_message(LOG_LEVEL_DEFAULT,stdout,__VA_ARGS__)
#define LOG_FLUSH_MESSAGE "flushing log queue...\n"
#define LOG_THREAD_START_MESSAGE "logging thread start\n"
size_t next;
};
-struct log_entry *log_dequeue();
-void log_enqueue(struct log_entry*);
int log_entries_init();
void log_entries_clean();
void log_flush();
-void log_print();
void log_message_with_prefix(enum log_level,FILE*,const char*,...);
void log_message(enum log_level,FILE*,const char*,...);
void *log_poll(void*);
#include<stdlib.h>
#include<string.h>
+#include<file.h>
+#include<tree.h>
+
struct torrent {
char *root;
+ char *name;
+ struct tree *file_tree;
};
extern struct torrent **torrents;
--- /dev/null
+#ifndef __TREE_H_
+#define __TREE_H_
+
+struct tree {
+ struct file *files;
+ size_t file_count;
+ struct tree *directories;
+ size_t directory_count;
+};
+
+void tree_free(struct tree*);
+
+#endif
--- /dev/null
+#include<file.h>
+
+void file_free(struct file *p) {
+ return;
+}
static struct log_helper helper;
-struct log_entry *log_dequeue() {
+// static functions
+static struct log_entry *log_dequeue();
+static void log_enqueue(struct log_entry*);
+static void log_print();
+static void log_print_prefix(FILE*);
+
+static struct log_entry *log_dequeue() {
struct log_entry *p;
pthread_mutex_lock(&logging_mutex);
return p;
}
-void log_enqueue(struct log_entry *p) {
+static void log_enqueue(struct log_entry *p) {
pthread_mutex_lock(&logging_mutex);
if(NULL==helper.start) {
}
void log_print() {
- struct log_entry *p = helper.start;
+ struct log_entry *p = log_dequeue();
if(NULL!=p) {
+ log_print_prefix(p->out_stream);
fputs(p->buf,p->out_stream);
helper.start = p->next;
}
}
-void log_message_with_prefix(enum log_level level, FILE *out_stream, const char *format,...) {
+static void log_print_prefix(FILE *out_stream) {
+ char buf[TIMESTAMP_BUF_LENGTH];
time_t t;
- if(level>global_options.verbose_flag) { return; }
-
t = time(NULL);
struct tm now = *localtime(&t);
- char buf[TIMESTAMP_BUF_LENGTH];
- strftime(buf,TIMESTAMP_BUF_LENGTH,"%Y-%m-%d-%H:%M:%S",&now);
-
- va_list args;
- va_start(args,format);
+ strftime(buf,TIMESTAMP_BUF_LENGTH,"%Y-%m-%d_%H:%M:%S",&now);
#ifndef NDEBUG
- log_message(level,out_stream,format,pthread_self(),buf,args);
+ fprintf(out_stream,LOG_MESSAGE_PREFIX,pthread_self(),buf);
#else
- log_message(level,out_stream,format,buf,args);
+ fprintf(out_stream,LOG_MESSAGE_PREFIX,buf);
#endif
-
- va_end(args);
}
-void log_message(enum log_level level, FILE *out_stream, const char *format, va_list args,...) {
- //va_list args;
- //va_start(args,format);
+void log_message(enum log_level level, FILE *out_stream, const char *format,...) {
+ if(level>global_options.verbose_flag) { return; }
+
+ va_list args;
+ va_start(args,format);
if(0==pthread_equal(pthread_self(),logging_thread)) {
// not on logging_thread
log_enqueue(p);
} else {
+ log_print_prefix(out_stream);
vfprintf(out_stream,format,args);
}
if(setup()<0) { return EXIT_FAILURE; }
- log_err(LOG_MESSAGE_PREFIX "this is a test %d %s\n",10,"what?");
+ log_err("this is a test %d %s\n",10,"what?");
while(1) { }
return EXIT_FAILURE;
int opt_add_watch(char *directory) {
struct torrent *p;
+
if(!is_directory(directory)) {
log_err(OPT_MESSAGE_WATCH_INVALID_DIRECTORY,directory);
return -1;
#include<setup.h>
-
int setup() {
if(setup_logging()<0) { return -1; }
if(setup_adding()<0) { 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) {
struct torrent **torrents;
void torrent_free(struct torrent *p) {
- free(p->root);
+ if(p->root!=NULL) { free(p->root); }
+ if(p->name!=NULL) { free(p->name); }
+ if(p->file_tree!=NULL) { tree_free(p->file_tree); }
free(p);
}
-int torrent_init(struct torrent **p, char *root) {
- *p = malloc(sizeof(struct torrent));
- if(NULL==(*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");
+ return -1;
+ }
+
+ (*torrent_p)->root = NULL;
+ (*torrent_p)->name = NULL;
+ (*torrent_p)->file_tree = NULL;
+
+ (*torrent_p)->root = malloc(strlen(root)+1);
+ if(NULL==(*torrent_p)->root) {
perror("malloc");
+ torrent_free(torrent_p);
return -1;
}
+ strcpy((*torrent_p)->root,root);
- (*p)->root = malloc(strlen(root)+1);
- if(NULL==(*p)->root) {
+ (*torrent_p)->name = malloc(strlen(name)+1);
+ if(NULL==(*torrent_p)->name) {
perror("malloc");
- free(*p);
+ torrent_free(torrent_p);
return -1;
}
- strcpy((*p)->root,root);
+ strcpy((*torrent_p)->name,name);
return 1;
}
--- /dev/null
+#include<tree.h>
+
+void tree_free(struct tree *p) {
+ while(p->file_count>0) {
+ file_free(p->files[p->file_count-1]);
+ p->file_count--;
+ }
+
+ while(p->directory_count>0) {
+ tree_free(p->directories[p->directory_count-1]);
+ p->directory_count--;
+ }
+
+ free(p);
+}