From 89b4b6f85b9d356c39767d4678c3164be49e4e23 Mon Sep 17 00:00:00 2001 From: Fredric N Date: Tue, 21 Sep 2010 19:38:02 +0200 Subject: [PATCH] Support for iniconfig in arch.c + small output API fixes --- Makefile | 6 +- TODO | 1 - src/arch.c | 159 ++++++++++++++++++++++++++------------------- src/indexer.c | 14 +++- src/output/mysql.c | 11 ++-- test/Makefile | 4 +- 6 files changed, 118 insertions(+), 77 deletions(-) diff --git a/Makefile b/Makefile index 3a4ef86..61acd29 100644 --- a/Makefile +++ b/Makefile @@ -19,11 +19,15 @@ endif SOURCES := \ src/arch.c \ - src/arch/mysql.c \ + src/output/mysql.c \ + src/ini/iniparser.c \ + src/ini/dictionary.c \ src/indexer.c \ src/common/strbuf.c \ src/common/path.c \ src/common/rbtree.c \ + src/common/xalloc.c \ + src/common/die.c \ src/notify/inotify.c \ src/notify/event.c \ src/notify/tree.c diff --git a/TODO b/TODO index 2116158..e1cffa2 100644 --- a/TODO +++ b/TODO @@ -4,5 +4,4 @@ ################## * Use queue in inotify_read(), tree and remove indexer - * ini-like config support * fix mysql module diff --git a/src/arch.c b/src/arch.c index 2a10a4b..549f9f6 100644 --- a/src/arch.c +++ b/src/arch.c @@ -2,8 +2,12 @@ #include #include #include -#include "arch/db.h" +#include "output/output.h" #include "notify/notify.h" +#include "ini/iniparser.h" + +static dictionary *config; + /* only way to exit the application properly when in main loop is by signal */ @@ -12,7 +16,15 @@ static void clean_exit(int excode) { time_t t = time(NULL); notify_cleanup(); - arch_db_close(); + + // 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); @@ -43,95 +55,106 @@ static void sighandl(int sig) { void arch_loop() { - notify_event *event; - - for(;;) { - - if (indexer_pending() && !notify_is_ready()) { + int status; + notify_event *event; + + for(;;) { + + if (indexer_pending() && !notify_is_ready()) { #ifdef __DEBUG__ - printf("sched: running indexer\n"); +printf("sched: running indexer\n"); #endif indexer_run(15); continue; } - + #ifdef __DEBUG__ - printf("sched: notify block\n"); +printf("sched: notify block\n"); #endif - event = notify_read(-1); - - if (event == NULL) - continue; - + event = notify_read(-1); + + if (event == NULL) + continue; + #ifdef __DEBUG__ - printf("-- EVENT --\n" - " TYPE: %s\n" - " DIR: %i\n" - " PATH: %s%s\n" - "---------------\n", notify_event_typetostr(event), event->dir, event->path, event->filename); +printf("-- EVENT --\n" +" TYPE: %s\n" +" DIR: %i\n" +" PATH: %s%s\n" +"---------------\n", notify_event_typetostr(event), event->dir, event->path, event->filename); #endif - - switch(event->type) { - + + switch(event->type) { case NOTIFY_MOVE_TO : - if (event->dir) indexer_register(event->path, event->filename); case NOTIFY_CREATE : - arch_db_insert(event->path, event->filename, event->dir); - break; + break; case NOTIFY_MOVE_FROM : - case NOTIFY_DELETE : - arch_db_delete(event->path, event->filename); - break; - } - + case NOTIFY_DELETE : + break; + } + + status = output_process(event); + if (0 != status) { + fprintf(stderr,"%s",output_error(status)); + } + #ifdef __DEBUG__ - notify_stat(); +notify_stat(); #endif - } + } } int main(int argc, char **argv) { - /* Validate arguments */ - if (argc != 7) { - - printf("Usage: %s .\n" - "Root Directory - Path to indexroot. All subdirectories will be indexed.\n" - "Db host - Database host\n" - "Db user - Database user\n" - "Db pass - Database password\n" - "Db name - Database to use for indexing\n" - "Db tbl - Database tablename", argv[0]); - - return EXIT_FAILURE; - } + // Validate arguments + if (argc != 2) { - /* setup signal handlers */ - signal(SIGTERM, sighandl); - signal(SIGKILL, sighandl); - signal(SIGQUIT, sighandl); - signal(SIGINT, sighandl); - signal(SIGSEGV, sighandl); - signal(SIGUSR1, sighandl); - signal(SIGUSR2, sighandl); + printf("Usage: %s \n" + "Root Directory - Path to indexroot. All subdirectories will be indexed.\n", argv[0]); - /* connect to database */ - if (!arch_db_init(argv[2], argv[3], argv[4], argv[5], argv[6])) - return EXIT_FAILURE; - - notify_init(); - - if(notify_add_watch(argv[1]) == -1) { - fprintf(stderr, "Invalid path: %s\n", argv[1]); - return EXIT_FAILURE; - } + return EXIT_FAILURE; + } + + + // Load configuration + 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); + + // Connect to database + int status = output_init(config); + if (0 != status) { + fprintf(stderr,"%s",output_error(status)); + return EXIT_FAILURE; + } + + + notify_init(); + + if(notify_add_watch(argv[1]) == -1) { + fprintf(stderr, "Invalid path: %s\n", argv[1]); + return EXIT_FAILURE; + } indexer_register(argv[1], NULL); - - arch_loop(); - - return EXIT_SUCCESS; + + arch_loop(); + + return EXIT_SUCCESS; } diff --git a/src/indexer.c b/src/indexer.c index 43009f4..2301379 100644 --- a/src/indexer.c +++ b/src/indexer.c @@ -11,7 +11,7 @@ #include #include "common/debug.h" #include "common/path.h" -#include "arch/db.h" +#include "output/output.h" #include "notify/notify.h" #include "notify/tree.h" #include "indexer.h" @@ -27,8 +27,18 @@ static void index_entry(struct entry *ent) { #if __DEBUG__ printf("DEBUG: index_entry adding to db: %s %s %d\n", ent->base, ent->name, ent->dir); #endif - arch_db_insert(ent->base, ent->name, ent->dir); + + // Event change, just for test. + notify_event ev; + + ev.path = ent->base; + ev.filename = ent->name; + ev.type = NOTIFY_CREATE; + int status = output_process(&ev); + if (0 != status) + fprintf(stderr,"%s",output_error(status)); + if (ent->dir) { memcpy(buf, ent->base, strlen(ent->base)); memcpy(buf + strlen(ent->base), ent->name, strlen(ent->name)+1); diff --git a/src/output/mysql.c b/src/output/mysql.c index 9ce0c29..204b2b3 100644 --- a/src/output/mysql.c +++ b/src/output/mysql.c @@ -212,6 +212,8 @@ int output_exit() { */ char *output_error(int error) { + char *str; + switch (error) { case 1: return "Missing 'host' in configuration"; @@ -224,7 +226,8 @@ char *output_error(int error) { case 5: return "Missing 'table' in configuration"; case 6: - return mysql_error(db.connection); + sprintf(str, "mysql error: %s", mysql_error(db.connection)); + return str; case 7: return "Error while creating table"; case 8: @@ -248,9 +251,9 @@ static int database_setup() { "`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))" + "`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`"; diff --git a/test/Makefile b/test/Makefile index 9b527ae..91df5dd 100644 --- a/test/Makefile +++ b/test/Makefile @@ -58,7 +58,9 @@ indexer : ../src/notify/tree.c \ ../src/notify/inotify.c \ ../src/notify/event.c \ - ../src/arch/mysql.c \ + ../src/ini/iniparser.c \ + ../src/ini/dictionary.c \ + ../src/output/stdout.c \ ../src/indexer.c \ t_indexer.c -o test_indexer