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 := \
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

1
TODO
View file

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

View file

@ -2,8 +2,12 @@
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#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 <Root Directory> <Db user> <Db pass> <Db name>.\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 <Root Directory>\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;
}

View file

@ -11,7 +11,7 @@
#include <string.h>
#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);

View file

@ -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`";

View file

@ -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