diff --git a/Makefile b/Makefile index 3e95fd4..2e1a819 100644 --- a/Makefile +++ b/Makefile @@ -26,17 +26,17 @@ ifeq ($(VERBOSE), 2) CFLAGS += -v endif -ifeq ($(output), mysql) - CFLAGS += `mysql_config --cflags` - LDFLAGS += -L/usr/lib/mysql -lmysqlclient -else - output = stdout -endif - obj = -obj += src/ini/iniparser.o -obj += src/ini/dictionary.o +ifeq ($(DEBUG), 2) + obj += src/client/stdout.o +else + CFLAGS += `mysql_config --cflags` + LDFLAGS += -L/usr/lib/mysql -lmysqlclient + obj += src/ini/iniparser.o + obj += src/ini/dictionary.o + obj += src/client/mysql.o +endif obj += src/common/rbtree.o obj += src/common/path.o @@ -44,15 +44,11 @@ obj += src/common/strbuf.o obj += src/common/xalloc.o obj += src/common/die.o -obj += src/output/$(output).o - obj += src/notify/inotify.o obj += src/notify/event.o obj += src/notify/fscrawl.o obj += src/notify/queue.o -obj += src/arch.o - .PHONY : all clean cleaner all : $(PROGRAM) @@ -64,7 +60,7 @@ clean : @for obj in `$(FINDOBJ)`; do \ echo $(RM) $$obj;$(RM) $$obj; \ done - + cleaner : clean $(RM) -r $(BUILD) diff --git a/Makefile.local.mk-dist b/Makefile.local.mk-dist index d068216..af4ad52 100644 --- a/Makefile.local.mk-dist +++ b/Makefile.local.mk-dist @@ -4,7 +4,3 @@ # Verbose output # VERBOSE = 1 - -# Define what type of output should be used. default stdout -# output = stdout -# output = mysql diff --git a/src/arch.c b/src/arch.c deleted file mode 100644 index cb0a7a6..0000000 --- a/src/arch.c +++ /dev/null @@ -1,137 +0,0 @@ -/* arch.c - * - * (C) Copyright 2010 Henrik Hautakoski - * (C) Copyright 2010 Fredric Nilsson - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - */ -#include -#include -#include -#include -#include "output/output.h" -#include "notify/notify.h" -#include "ini/iniparser.h" -#include "common/util.h" - -static dictionary *config = NULL; - - -/* only way to exit the application properly - when in main loop is by signal */ -static void clean_exit(int excode) { - - time_t t = time(NULL); - - notify_exit(); - - // Clean output - int status = output_exit(); - if (0 != status) { - fprintf(stderr,"%s",output_error(status)); - } - - // Clean config - iniparser_freedict(config); - - printf("\nprocess exit at: %s", ctime(&t)); - exit(excode); -} - -/* signal handler */ -static void sighandl(int sig) { - - switch(sig) { - /* normal exit signals */ - case SIGTERM : - case SIGKILL : - case SIGQUIT : - case SIGINT : - clean_exit(EXIT_SUCCESS); - /* segmentation violation, let user now */ - case SIGSEGV : - fprintf(stderr, "SEGFAULT: o no he didn't\n"); - exit(EXIT_FAILURE); - case SIGUSR1 : - case SIGUSR2 : - printf("notify stat:\n"); - notify_stat(); - /* don't know why, but everything goes bananas if we keep executing */ - clean_exit(EXIT_SUCCESS); - } -} - -void arch_loop() { - - int status; - notify_event *event; - - for(;;) { - - event = notify_read(); - - if (event == NULL) - continue; - - status = output_process(event); - if (status) - fprintf(stderr,"%s",output_error(status)); - - notify_event_del(event); - } -} - -int main(int argc, char **argv) { - - int status; - - /* Validate arguments */ - if (argc != 2) { - - printf("Usage: %s \n" - "Root Directory - Path to indexroot. All subdirectories will be indexed.\n", argv[0]); - - return EXIT_FAILURE; - } - - /* Load configuration */ - if (file_exists("config.ini")) { - config = iniparser_load("config.ini"); - if (NULL == config) { - fprintf(stderr, "Could not load configuration"); - return EXIT_FAILURE; - } - } - - /* Setup signal handlers */ - signal(SIGTERM, sighandl); - signal(SIGKILL, sighandl); - signal(SIGQUIT, sighandl); - signal(SIGINT, sighandl); - signal(SIGSEGV, sighandl); - signal(SIGUSR1, sighandl); - signal(SIGUSR2, sighandl); - - status = output_init(config); - if (status) { - fprintf(stderr, "%s", output_error(status)); - return EXIT_FAILURE; - } - - status = notify_init(); - if (status == -1) - return EXIT_FAILURE; - - status = notify_add_watch(argv[1]); - if (status == -1) { - fprintf(stderr, "Invalid path: %s\n", argv[1]); - return EXIT_FAILURE; - } - - arch_loop(); - - return EXIT_SUCCESS; -} diff --git a/src/client/stdout.c b/src/client/stdout.c new file mode 100644 index 0000000..2391682 --- /dev/null +++ b/src/client/stdout.c @@ -0,0 +1,89 @@ +/* + * Debugging client + * + * Copyright (C) 2010 Henrik Hautakoski + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + */ +#include +#include +#include +#include +#include "../notify/notify.h" + +static void p_exit_time() { + + time_t t = time(NULL); + printf("\nprocess exit at: %s", ctime(&t)); +} + +static void sighandler(int sig) { + + p_exit_time(); + + if (sig == SIGSEGV) { + fprintf(stderr, "Segmentation fault\n"); + abort(); + } + + notify_exit(); + exit(EXIT_SUCCESS); +} + +static void pevent(notify_event *ev) { + + const char *strtype = notify_event_typetostr(ev); + char slash = ev->dir ? '/' : '\0'; + + printf("%s : %s%s%c\n", strtype, ev->path, ev->filename, slash); +} + +static void mainloop() { + + notify_event *event; + + for(;;) { + + event = notify_read(); + + if (event == NULL) + continue; + + pevent(event); + + notify_event_del(event); + } +} + +int main(int argc, char **argv) { + + int rc; + + if (argc < 2) { + fprintf(stderr, "usage: %s \n", argv[0]); + return EXIT_FAILURE; + } + + signal(SIGINT, sighandler); + signal(SIGTERM, sighandler); + signal(SIGKILL, sighandler); + signal(SIGQUIT, sighandler); + signal(SIGSEGV, sighandler); + + rc = notify_init(); + if (rc < 0) + return EXIT_FAILURE; + + rc = notify_add_watch(argv[1]); + if (rc < 0) { + fprintf(stderr, "Invalid path: %s\n", argv[1]); + return EXIT_FAILURE; + } + + mainloop(); + + return EXIT_SUCCESS; +} diff --git a/src/output/mysql.c b/src/output/mysql.c deleted file mode 100644 index 7c79463..0000000 --- a/src/output/mysql.c +++ /dev/null @@ -1,297 +0,0 @@ -/* output/mysql.c - Mysql output-driver - * - * Copyright (C) 2010 Fredric Nilsson - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - */ - -#include -#include -#include -#include -#include -#include - -#include "output.h" -#include "../ini/iniparser.h" -#include "../common/xalloc.h" -#include "../notify/event.h" - -typedef struct { - char *host; - int port; - char *username; - char *password; - char *database; - char *table; - - unsigned long thread; - MYSQL* connection; - my_bool reconnect; - -} database; - -static database db; - -static int database_setup(); - -/* - * Initialize database connection and connect to database - */ -int output_init(dictionary *config) { - - if (config == NULL) - return 9; - - // Load database information from ini config - db.host = iniparser_getstring(config, "mysql:host", NULL); - db.port = iniparser_getint(config, "mysql:port", 3306); - db.username = iniparser_getstring(config, "mysql:username", NULL); - db.password = iniparser_getstring(config, "mysql:password", NULL); - db.database = iniparser_getstring(config, "mysql:database", NULL); - db.table = iniparser_getstring(config, "mysql:table", NULL); - - // Return exit code for missconfiguration - if (NULL == db.host) - return 1; - - if (NULL == db.username) - return 2; - - if (NULL == db.password) - return 3; - - if (NULL == db.database) - return 4; - - if (NULL == db.table) - return 5; - - // Enable reconnection - if (1 == iniparser_getboolean(config, "mysql:reconnect", 1)) { - db.reconnect = 1; - } else { - db.reconnect = 0; - } - - // Init. database connection - db.connection = mysql_init(NULL); - - // Set mysql options - mysql_options(db.connection, MYSQL_OPT_RECONNECT, &db.reconnect); - - // Connect to database - if (!mysql_real_connect(db.connection, db.host, db.username, db.password, db.database, db.port, NULL, 0)) - return 6; - - - // Fix for mysql versions prior to 5.0.19 - mysql_options(db.connection, MYSQL_OPT_RECONNECT, &db.reconnect); - - - // Save mysql thread id - db.thread = mysql_thread_id(db.connection); - -#ifdef DB_DEBUG - fprintf(stderr, "output_init(): %li\n", db.thread); -#endif - - // Setup database - return database_setup(); - -} - - -/* - * Process events - */ -int output_process(notify_event *event) { - - int ret; - char *stmt; - - // Skip if event is unknown - if (NOTIFY_UNKNOWN == event->type) - return 0; - - // Insert new row in database - if (NOTIFY_CREATE == event->type) { - - char stmt_insert[] = "INSERT INTO `%s` (`Path`, `Base`, `Type`, `Status`, `Date`) VALUES('%s','%s','1','0', NOW())"; - - if(mysql_ping(db.connection) != 0) { - return 8; - } - - stmt = xmalloc(strlen(stmt_insert) + strlen(db.table) + strlen(event->path) + strlen(event->filename) + 1); - - // Escape paths - char *escaped_path = xmalloc(strlen(event->path) * 2 + 1); - char *escaped_filename = xmalloc(strlen(event->filename) * 2 + 1); - - mysql_real_escape_string(db.connection, escaped_path, event->path, strlen(event->path)); - mysql_real_escape_string(db.connection, escaped_filename, event->filename, strlen(event->filename)); - - // Create mysql query - sprintf(stmt, stmt_insert, db.table, escaped_path, escaped_filename); - - // Run mysql query - ret = mysql_query(db.connection, stmt); - - // Clean up - free(stmt); - free(escaped_path); - free(escaped_filename); - - // Delete row in database - } else if (NOTIFY_DELETE == event->type) { - - - char stmt_delete[] = "DELETE FROM `%s` WHERE `Path` LIKE '%s%s%%' OR (`Path` = '%s' AND `Base` = '%s')"; - - if(mysql_ping(db.connection) != 0) { - return 8; - } - - stmt = xmalloc(strlen(stmt_delete) + strlen(db.table) + strlen(event->path) + strlen(event->filename) + 1); - - // Escape paths - char *escaped_path = xmalloc(strlen(event->path) * 2 + 1); - char *escaped_filename = xmalloc(strlen(event->filename) * 2 + 1); - - mysql_real_escape_string(db.connection, escaped_path, event->path, strlen(event->path)); - mysql_real_escape_string(db.connection, escaped_filename, event->filename, strlen(event->filename)); - - // Create mysql query - sprintf(stmt, stmt_delete, db.table, escaped_path, escaped_filename, escaped_path, escaped_filename); - - // Run mysql query - ret = mysql_query(db.connection, stmt); - - // Clean up - free(stmt); - free(escaped_path); - free(escaped_filename); - - } - - // Make sure query was successfull - if(ret != 0) { - return 6; - } - -#ifdef DB_DEBUG - - if(db.thread != mysql_thread_id(db.connection)) { - fprintf(stderr, "Connection was lost. Reconnected. Old_Thread: %li, New_Thread: %li\n", db.thread, mysql_thread_id(db.connection)); - } - -#endif - - return 0; -} - - -/* - * Close database connection - */ -int output_exit() { - - // Close database connection - mysql_close(db.connection); - - // another leak fix - mysql_library_end(); - - return 0; -} - - -/* - * Converts output error codes to string - */ -char *output_error(int error) { - - char *str; - - switch (error) { - case 1: - return "Missing 'host' in configuration"; - case 2: - return "Missing 'username' in configuration"; - case 3: - return "Missing 'password' in configuration"; - case 4: - return "Missing 'database' in configuration"; - case 5: - return "Missing 'table' in configuration"; - case 6: - sprintf(str, "mysql error: %s", mysql_error(db.connection)); - return str; - case 7: - return "Error while creating table"; - case 8: - return "Lost connection to database. Could not reconnect"; - case 9: - return "Missing configuration"; - } - - return "Unkown error"; -} - - -/* - * Database setup - */ -static int database_setup() { - - int ret; - - // Sql statements - char stmt_create[] = "CREATE TABLE IF NOT EXISTS `%s` (" - "`Path` varchar(512) default NULL, " - "`Base` varchar(512) default NULL, " - "`Type` tinyint(1) default NULL, " - "`Status` tinyint(1) default NULL, " - "`Date` datetime default NULL, " - "KEY `idx_path` (`Path`(333)), " - "KEY `idx_base` (`Base`(333)) " - ") ENGINE=MyISAM DEFAULT CHARSET=utf8 "; - - char stmt_trunc[] = "TRUNCATE TABLE `%s`"; - - // Build query - // Notice: -1 for "%s" in stmt_create and \0 - char *stmt = (char *) xmalloc(strlen(stmt_create) + strlen(db.table) - 1); - if (stmt == NULL || sprintf(stmt, stmt_create, db.table) < 0) - return 7; - - // Run mysql query - ret = mysql_query(db.connection, stmt); - - // Make sure query was successfull - if (ret != 0) { - free(stmt); - return 6; - } - - // Build new query - stmt = (char *) xrealloc(stmt, strlen(stmt_trunc) + strlen(db.table) - 1); - if (sprintf(stmt, stmt_trunc, db.table) < 0) - return 8; - - // Run mysql query - ret = mysql_query(db.connection, stmt); - - // Make sure query was successfull - if (ret != 0) { - free(stmt); - return 6; - } - - - return 0; -} diff --git a/src/output/output.h b/src/output/output.h deleted file mode 100644 index d302cc3..0000000 --- a/src/output/output.h +++ /dev/null @@ -1,27 +0,0 @@ -/* arch/output.h - output API - * - * Copyright (C) 2010 Fredric Nilsson - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - */ - -#ifndef __OUTPUT_H - -#define __OUTPUT_H - -#include "../ini/dictionary.h" -#include "../notify/event.h" - -int output_init(dictionary *config); - -int output_process(notify_event *event); - -char *output_error(int error); - -int output_exit(void); - - -#endif diff --git a/src/output/stdout.c b/src/output/stdout.c deleted file mode 100644 index d783da3..0000000 --- a/src/output/stdout.c +++ /dev/null @@ -1,58 +0,0 @@ -/* output/stdout.c - Standard output output-driver - * - * Copyright (C) 2010 Fredric Nilsson - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - */ -#include -#include -#include - -#include "output.h" -#include "../notify/event.h" - -/* - * Init. output-driver. Stdout has nothing - * to setup. - */ -int output_init(dictionary *config) { - printf("Running output_init()\n"); - return 0; -} - -int output_process(notify_event *event) { - - char *event_string; - - switch (event->type) { - case NOTIFY_CREATE: - event_string = "create"; - break; - case NOTIFY_DELETE: - event_string = "delete"; - break; - case NOTIFY_MOVE_FROM: - event_string = "move_from"; - break; - case NOTIFY_MOVE_TO: - event_string = "move_to"; - break; - default: - event_string = "unknown"; - } - - printf("output_process: {Path: %s, Filename: %s, Type: %s}\n", event->path, event->filename, event_string); - return 0; -} - -char *output_error(int error) { - return "Some kind of error"; -} - -int output_exit(void) { - printf("Running output_exit()\n"); - return 0; -} diff --git a/test/Makefile b/test/Makefile index 1642a50..bf599b8 100644 --- a/test/Makefile +++ b/test/Makefile @@ -3,7 +3,7 @@ CC=gcc CFLAGS=-g -D__DEBUG__ LDFLAGS=-L/usr/lib64/mysql -lmysqlclient -all : raw_inotify strbuf path rbtree inotify fscrawl mysql stdout queue +all : raw_inotify strbuf path rbtree inotify fscrawl queue raw_inotify : $(CC) -linotifytools t_raw_inotify.c -o test_raw_inotify @@ -54,22 +54,6 @@ fscrawl : ../src/common/strbuf.c \ t_fscrawl.c -o test_fscrawl -mysql : - $(CC) -D DB_DEBUG $(CFLAGS) $(LDFLAGS) \ - ../src/output/mysql.c \ - ../src/common/die.c \ - ../src/common/xalloc.c \ - ../src/ini/iniparser.c \ - ../src/ini/dictionary.c \ - t_mysql.c -o test_mysql - -stdout : - $(CC) $(CFLAGS) $(LDFLAGS) \ - ../src/output/stdout.c \ - ../src/ini/iniparser.c \ - ../src/ini/dictionary.c \ - t_stdout.c -o test_stdout - queue : $(CC) $(CFLAGS) ../src/notify/queue.c t_queue.c -o test_queue diff --git a/test/config.ini b/test/config.ini deleted file mode 100644 index b2a8036..0000000 --- a/test/config.ini +++ /dev/null @@ -1,9 +0,0 @@ -[general] -test = mytest ; - -[mysql] -host = localhost ; -username = user ; -password = pw ; -database = development ; -table = filesystem ; diff --git a/test/t_mysql.c b/test/t_mysql.c deleted file mode 100644 index 83c0ebd..0000000 --- a/test/t_mysql.c +++ /dev/null @@ -1,46 +0,0 @@ - -#include -#include -#include -#include -#include - -#include "../src/output/output.h" -#include "../src/ini/iniparser.h" -#include "../src/notify/event.h" - - -int main(int argc, char *argv[]) { - - printf("Run!\n"); - - printf("Here 1\n"); - dictionary *config = iniparser_load("config.ini"); - - printf("Here 2\n"); - notify_event ev; - - printf("Here 3\n"); - ev.path = "/home/kalle/bilder"; - ev.filename = "kalle.jpg"; - ev.type = NOTIFY_CREATE; - - printf("Here 4\n"); - output_init(config); - - printf("Here 5\n"); - int r = output_process(&ev); - - printf("Here 6\n"); - if (r != 0) { - printf("Error:\n"); - printf(output_error(r)); - } - - printf("Here 7\n"); - output_exit(); - - printf("Here 8\n"); - - return 0; -} \ No newline at end of file diff --git a/test/t_stdout.c b/test/t_stdout.c deleted file mode 100644 index 7aa6324..0000000 --- a/test/t_stdout.c +++ /dev/null @@ -1,33 +0,0 @@ - -#include -#include -#include -#include - -#include "../src/output/output.h" -#include "../src/notify/event.h" - -int main(int argc, char *argv[]) { - - int r; - notify_event ev; - - ev.path = "/home/kalle/bilder"; - ev.filename = "kalle.jpg"; - ev.type = NOTIFY_CREATE; - - output_init(NULL); - - r = output_process(&ev); - - ev.type = NOTIFY_DELETE; - - r = output_process(&ev); - - if (r != 0) - printf("Error: %s\n", output_error(r)); - - output_exit(); - - return 0; -}