From 8cafb25c42cbd374ae4ff0698b240821061cee06 Mon Sep 17 00:00:00 2001 From: H Hautakoski Date: Thu, 26 Aug 2010 21:37:47 +0200 Subject: [PATCH] arch/mysql.c: fixed the test and a overflow in init(). mysql needs alot more love. --- src/arch/mysql.c | 110 ++++++++++++++++++++++++----------------------- test/Makefile | 2 +- test/t_mysql.c | 25 +++-------- 3 files changed, 63 insertions(+), 74 deletions(-) diff --git a/src/arch/mysql.c b/src/arch/mysql.c index a3640aa..f9542f8 100644 --- a/src/arch/mysql.c +++ b/src/arch/mysql.c @@ -23,7 +23,61 @@ static MYSQL* dbconn; static unsigned long dbthread_id; static char *dbtable = NULL; -int db_setup(); +/* + * Database setup + */ +static int db_setup() { + + int ret; + 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) " + "ENGINE=MyISAM DEFAULT CHARSET=utf8"; + char stmt_trunc[] = "TRUNCATE TABLE `%s`"; + + /* Allocate memory big enough for querys */ + char *stmt = (char *) malloc((sizeof(char) * strlen(dbtable)) + strlen(stmt_create) - 1); + + /* Create mysql query */ + if (stmt == NULL || sprintf(stmt, stmt_create, dbtable) < 0) { + fprintf(stderr, "Error, create database sql\n"); + return -1; + } + + /* Run mysql query */ + ret = mysql_query(dbconn, stmt); + + /* Make sure query was successfull */ + if (ret != 0) { + fprintf(stderr, "%s\n", mysql_error(dbconn)); + goto clear; + } + + /* Create mysql query */ + if (sprintf(stmt, stmt_trunc, dbtable) < 0) { + fprintf(stderr, "Error, trunc sql\n"); + goto clear; + } + + /* Run mysql query */ + ret = mysql_query(dbconn, stmt); + + /* Make sure query was successfull */ + if (ret != 0) { + fprintf(stderr, "%s\n", mysql_error(dbconn)); + goto clear; + } + + return 0; + +clear: + /* Clean up */ + free(stmt); + return -1; +} /* * Initialize database connection and connect to database @@ -33,7 +87,7 @@ int arch_db_init(char *host, char *username, char *password, char *database, cha my_bool reconnect = 1; /* Keep tablename for querys */ - dbtable = malloc(sizeof(table)); + dbtable = malloc(strlen(table) + 1); strcpy(dbtable,table); /* Init. database */ @@ -82,58 +136,6 @@ void arch_db_close() { mysql_library_end(); } -/* - * Database setup - */ -int db_setup() { - - int ret; - 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) " - "ENGINE=MyISAM DEFAULT CHARSET=utf8"; - char stmt_trunc[] = "TRUNCATE TABLE `%s`"; - - /* Allocate memory big enough for querys */ - char *stmt = (char *) malloc((sizeof(char) * strlen(dbtable)) + strlen(stmt_create) - 1); - - /* Create mysql query */ - if(sprintf(stmt, stmt_create, dbtable) < 0) { - fprintf(stderr, "Error, create database sql\n"); - } - - /* Run mysql query */ - ret = mysql_query(dbconn, stmt); - - /* Make sure query was successfull */ - if(ret != 0) { - fprintf(stderr, "%s\n", mysql_error(dbconn)); - return -1; - } - - /* Create mysql query */ - if(sprintf(stmt, stmt_trunc, dbtable) < 0) { - fprintf(stderr, "Error, trunc sql\n"); - } - - /* Run mysql query */ - ret = mysql_query(dbconn, stmt); - - /* Clean up */ - free(stmt); - - /* Make sure query was successfull */ - if(ret != 0) { - fprintf(stderr, "%s\n", mysql_error(dbconn)); - return -1; - } - - return 0; -} - /* * Insert into database */ diff --git a/test/Makefile b/test/Makefile index d7ac891..db7e2dc 100644 --- a/test/Makefile +++ b/test/Makefile @@ -39,7 +39,7 @@ indexer : $(CC) $(CFLAGS) -D__DEBUG__ ../src/common/path.c ../src/fs/tree.c ../src/indexer.c t_indexer.c -o test_indexer mysql : - $(CC) -D DB_DEBUG $(CFLAGS) ../src/mysql_db.c -L/usr/lib64/mysql \ + $(CC) -D DB_DEBUG $(CFLAGS) ../src/arch/mysql.c -L/usr/lib64/mysql \ -lmysqlclient -lz -lcrypt -lnsl -lm -L/usr/lib64 -lssl -lcrypto t_mysql.c -o test_mysql queue : diff --git a/test/t_mysql.c b/test/t_mysql.c index 2cfb85d..a3ed558 100644 --- a/test/t_mysql.c +++ b/test/t_mysql.c @@ -1,33 +1,20 @@ -#ifndef INOTIFY_DEBUG - #define INOTIFY_DEBUG -#endif - #include -#include "../src/notify_db.h" +#include "../src/arch/db.h" int main(int argc, char *argv[]) { - // Keep me safe printf("Run!\n"); - // Connect - notify_db_init("localhost", "warez", "elebobo", "filesystem", "filesystem"); - - // Truncate - //notify_db_truncate(); + arch_db_init("localhost", "warez", "elebobo", "filesystem", "filesystem"); sleep(10); - // Insert - notify_db_insert("/this/is/my/path/to/", "myfile", 0); + arch_db_insert("/this/is/my/path/to/", "myfile", 0); - // Delete - //notify_db_delete("/this/is/my/path/to/", "myfile"); + arch_db_delete("/this/is/my/path/to/", "myfile"); - // Close - notify_db_close(); + arch_db_close(); - // Kill me return 0; -} \ No newline at end of file +}