Archived
1
0
Fork 0

Support for iniconfig in arch.c + small output API fixes

This commit is contained in:
Fredric N 2010-09-21 19:38:02 +02:00 committed by Henrik Hautakoski
parent 5da0562350
commit 89b4b6f85b
6 changed files with 118 additions and 77 deletions

View file

@ -19,11 +19,15 @@ endif
SOURCES := \ SOURCES := \
src/arch.c \ src/arch.c \
src/arch/mysql.c \ src/output/mysql.c \
src/ini/iniparser.c \
src/ini/dictionary.c \
src/indexer.c \ src/indexer.c \
src/common/strbuf.c \ src/common/strbuf.c \
src/common/path.c \ src/common/path.c \
src/common/rbtree.c \ src/common/rbtree.c \
src/common/xalloc.c \
src/common/die.c \
src/notify/inotify.c \ src/notify/inotify.c \
src/notify/event.c \ src/notify/event.c \
src/notify/tree.c src/notify/tree.c

1
TODO
View file

@ -4,5 +4,4 @@
################## ##################
* Use queue in inotify_read(), tree and remove indexer * Use queue in inotify_read(), tree and remove indexer
* ini-like config support
* fix mysql module * fix mysql module

View file

@ -2,8 +2,12 @@
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "arch/db.h" #include "output/output.h"
#include "notify/notify.h" #include "notify/notify.h"
#include "ini/iniparser.h"
static dictionary *config;
/* only way to exit the application properly /* only way to exit the application properly
when in main loop is by signal */ when in main loop is by signal */
@ -12,7 +16,15 @@ static void clean_exit(int excode) {
time_t t = time(NULL); time_t t = time(NULL);
notify_cleanup(); 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)); printf("\nprocess exit at: %s", ctime(&t));
exit(excode); exit(excode);
@ -43,20 +55,21 @@ static void sighandl(int sig) {
void arch_loop() { void arch_loop() {
int status;
notify_event *event; notify_event *event;
for(;;) { for(;;) {
if (indexer_pending() && !notify_is_ready()) { if (indexer_pending() && !notify_is_ready()) {
#ifdef __DEBUG__ #ifdef __DEBUG__
printf("sched: running indexer\n"); printf("sched: running indexer\n");
#endif #endif
indexer_run(15); indexer_run(15);
continue; continue;
} }
#ifdef __DEBUG__ #ifdef __DEBUG__
printf("sched: notify block\n"); printf("sched: notify block\n");
#endif #endif
event = notify_read(-1); event = notify_read(-1);
@ -65,51 +78,57 @@ void arch_loop() {
continue; continue;
#ifdef __DEBUG__ #ifdef __DEBUG__
printf("-- EVENT --\n" printf("-- EVENT --\n"
" TYPE: %s\n" " TYPE: %s\n"
" DIR: %i\n" " DIR: %i\n"
" PATH: %s%s\n" " PATH: %s%s\n"
"---------------\n", notify_event_typetostr(event), event->dir, event->path, event->filename); "---------------\n", notify_event_typetostr(event), event->dir, event->path, event->filename);
#endif #endif
switch(event->type) { switch(event->type) {
case NOTIFY_MOVE_TO : case NOTIFY_MOVE_TO :
if (event->dir) if (event->dir)
indexer_register(event->path, event->filename); indexer_register(event->path, event->filename);
case NOTIFY_CREATE : case NOTIFY_CREATE :
arch_db_insert(event->path, event->filename, event->dir);
break; break;
case NOTIFY_MOVE_FROM : case NOTIFY_MOVE_FROM :
case NOTIFY_DELETE : case NOTIFY_DELETE :
arch_db_delete(event->path, event->filename);
break; break;
} }
status = output_process(event);
if (0 != status) {
fprintf(stderr,"%s",output_error(status));
}
#ifdef __DEBUG__ #ifdef __DEBUG__
notify_stat(); notify_stat();
#endif #endif
} }
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
/* Validate arguments */ // Validate arguments
if (argc != 7) { if (argc != 2) {
printf("Usage: %s <Root Directory> <Db user> <Db pass> <Db name>.\n" printf("Usage: %s <Root Directory>\n"
"Root Directory - Path to indexroot. All subdirectories will be indexed.\n" "Root Directory - Path to indexroot. All subdirectories will be indexed.\n", argv[0]);
"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; return EXIT_FAILURE;
} }
/* setup signal handlers */
// 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(SIGTERM, sighandl);
signal(SIGKILL, sighandl); signal(SIGKILL, sighandl);
signal(SIGQUIT, sighandl); signal(SIGQUIT, sighandl);
@ -118,9 +137,13 @@ int main(int argc, char **argv) {
signal(SIGUSR1, sighandl); signal(SIGUSR1, sighandl);
signal(SIGUSR2, sighandl); signal(SIGUSR2, sighandl);
/* connect to database */ // Connect to database
if (!arch_db_init(argv[2], argv[3], argv[4], argv[5], argv[6])) int status = output_init(config);
if (0 != status) {
fprintf(stderr,"%s",output_error(status));
return EXIT_FAILURE; return EXIT_FAILURE;
}
notify_init(); notify_init();

View file

@ -11,7 +11,7 @@
#include <string.h> #include <string.h>
#include "common/debug.h" #include "common/debug.h"
#include "common/path.h" #include "common/path.h"
#include "arch/db.h" #include "output/output.h"
#include "notify/notify.h" #include "notify/notify.h"
#include "notify/tree.h" #include "notify/tree.h"
#include "indexer.h" #include "indexer.h"
@ -27,7 +27,17 @@ static void index_entry(struct entry *ent) {
#if __DEBUG__ #if __DEBUG__
printf("DEBUG: index_entry adding to db: %s %s %d\n", ent->base, ent->name, ent->dir); printf("DEBUG: index_entry adding to db: %s %s %d\n", ent->base, ent->name, ent->dir);
#endif #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) { if (ent->dir) {
memcpy(buf, ent->base, strlen(ent->base)); memcpy(buf, ent->base, strlen(ent->base));

View file

@ -212,6 +212,8 @@ int output_exit() {
*/ */
char *output_error(int error) { char *output_error(int error) {
char *str;
switch (error) { switch (error) {
case 1: case 1:
return "Missing 'host' in configuration"; return "Missing 'host' in configuration";
@ -224,7 +226,8 @@ char *output_error(int error) {
case 5: case 5:
return "Missing 'table' in configuration"; return "Missing 'table' in configuration";
case 6: case 6:
return mysql_error(db.connection); sprintf(str, "mysql error: %s", mysql_error(db.connection));
return str;
case 7: case 7:
return "Error while creating table"; return "Error while creating table";
case 8: case 8:
@ -248,9 +251,9 @@ static int database_setup() {
"`Base` varchar(512) default NULL, " "`Base` varchar(512) default NULL, "
"`Type` tinyint(1) default NULL, " "`Type` tinyint(1) default NULL, "
"`Status` tinyint(1) default NULL, " "`Status` tinyint(1) default NULL, "
"`Date` datetime default NULL" "`Date` datetime default NULL, "
"KEY `idx_path` (`Path`(333))," "KEY `idx_path` (`Path`(333)), "
"KEY `idx_base` (`Base`(333))" "KEY `idx_base` (`Base`(333)) "
") ENGINE=MyISAM DEFAULT CHARSET=utf8 "; ") ENGINE=MyISAM DEFAULT CHARSET=utf8 ";
char stmt_trunc[] = "TRUNCATE TABLE `%s`"; char stmt_trunc[] = "TRUNCATE TABLE `%s`";

View file

@ -58,7 +58,9 @@ indexer :
../src/notify/tree.c \ ../src/notify/tree.c \
../src/notify/inotify.c \ ../src/notify/inotify.c \
../src/notify/event.c \ ../src/notify/event.c \
../src/arch/mysql.c \ ../src/ini/iniparser.c \
../src/ini/dictionary.c \
../src/output/stdout.c \
../src/indexer.c \ ../src/indexer.c \
t_indexer.c -o test_indexer t_indexer.c -o test_indexer