From a1d11d01b949fdb8d5039f2ccc8230c771b7d5b1 Mon Sep 17 00:00:00 2001 From: SJ Date: Sat, 29 Jun 2013 15:43:16 +0200 Subject: [PATCH] prepared statement fixes --- src/config.h | 2 +- src/import.c | 11 ++++++----- src/message.c | 9 +++++++++ src/mysql.c | 21 --------------------- src/pilerimport.c | 8 -------- src/retr.c | 8 -------- src/session.c | 8 -------- src/sql.h | 2 -- 8 files changed, 16 insertions(+), 53 deletions(-) diff --git a/src/config.h b/src/config.h index 1b75d79b..df78de86 100644 --- a/src/config.h +++ b/src/config.h @@ -14,7 +14,7 @@ #define VERSION "0.1.24-master-branch" -#define BUILD 818 +#define BUILD 819 #define HOSTID "mailarchiver" diff --git a/src/import.c b/src/import.c index b3a25f6f..f4d57ce2 100644 --- a/src/import.c +++ b/src/import.c @@ -72,11 +72,6 @@ int import_message(char *filename, struct session_data *sdata, struct __data *da state = parse_message(sdata, 1, data, cfg); post_parse(sdata, &state, cfg); - if(sdata->hdr_len < 10){ - printf("invalid message, hdr_len: %d\n", sdata->hdr_len); - return ERR; - } - if(sdata->sent <= 0 && sdata->delivered > 0) sdata->sent = sdata->delivered; if(sdata->sent > sdata->now) sdata->sent = sdata->now; @@ -94,6 +89,12 @@ int import_message(char *filename, struct session_data *sdata, struct __data *da } else { make_digests(sdata, cfg); + + if(sdata->hdr_len < 10){ + printf("%s: invalid message, hdr_len: %d\n", filename, sdata->hdr_len); + return ERR; + } + rc = process_message(sdata, &state, data, cfg); } diff --git a/src/message.c b/src/message.c index 1d69b41d..d4cfbc60 100644 --- a/src/message.c +++ b/src/message.c @@ -66,6 +66,7 @@ int store_index_data(struct session_data *sdata, struct _state *state, struct __ uint64 get_metaid_by_messageid(struct session_data *sdata, struct __data *data, char *message_id, struct __config *cfg){ uint64 id=0; + if(prepare_sql_statement(sdata, &(data->stmt_get_meta_id_by_message_id), SQL_PREPARED_STMT_GET_META_ID_BY_MESSAGE_ID) == ERR) return id; p_bind_init(data); data->sql[data->pos] = message_id; data->type[data->pos] = TYPE_STRING; data->pos++; @@ -83,6 +84,8 @@ uint64 get_metaid_by_messageid(struct session_data *sdata, struct __data *data, p_free_results(data->stmt_get_meta_id_by_message_id); CLOSE: + mysql_stmt_close(data->stmt_get_meta_id_by_message_id); + return id; } @@ -91,6 +94,8 @@ int store_recipients(struct session_data *sdata, struct __data *data, char *to, int ret=OK, n=0; char *p, *q, puf[SMALLBUFSIZE]; + if(prepare_sql_statement(sdata, &(data->stmt_insert_into_rcpt_table), SQL_PREPARED_STMT_INSERT_INTO_RCPT_TABLE) == ERR) return ret; + p = to; do { p = split_str(p, " ", puf, sizeof(puf)-1); @@ -115,6 +120,7 @@ int store_recipients(struct session_data *sdata, struct __data *data, char *to, } while(p); + mysql_stmt_close(data->stmt_insert_into_rcpt_table); if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: added %d recipients", sdata->ttmpfile, n); @@ -125,6 +131,7 @@ int store_recipients(struct session_data *sdata, struct __data *data, char *to, int update_metadata_reference(struct session_data *sdata, struct _state *state, struct __data *data, char *ref, struct __config *cfg){ int ret = ERR; + if(prepare_sql_statement(sdata, &(data->stmt_update_metadata_reference), SQL_PREPARED_STMT_UPDATE_METADATA_REFERENCE) == ERR) return ret; p_bind_init(data); @@ -133,6 +140,8 @@ int update_metadata_reference(struct session_data *sdata, struct _state *state, if(p_exec_query(sdata, data->stmt_update_metadata_reference, data) == OK) ret = OK; + mysql_stmt_close(data->stmt_update_metadata_reference); + if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: updated meta reference for '%s', rc=%d", sdata->ttmpfile, state->reference, ret); return ret; diff --git a/src/mysql.c b/src/mysql.c index 770d8dd3..4803cb27 100644 --- a/src/mysql.c +++ b/src/mysql.c @@ -235,29 +235,8 @@ void insert_offset(struct session_data *sdata, int server_id){ } -int create_prepared_statements(struct session_data *sdata, struct __data *data){ - int rc = OK; - - data->stmt_get_meta_id_by_message_id = NULL; - data->stmt_insert_into_rcpt_table = NULL; - data->stmt_update_metadata_reference = NULL; - - if(prepare_sql_statement(sdata, &(data->stmt_get_meta_id_by_message_id), SQL_PREPARED_STMT_GET_META_ID_BY_MESSAGE_ID) == ERR) rc = ERR; - if(prepare_sql_statement(sdata, &(data->stmt_insert_into_rcpt_table), SQL_PREPARED_STMT_INSERT_INTO_RCPT_TABLE) == ERR) rc = ERR; - if(prepare_sql_statement(sdata, &(data->stmt_update_metadata_reference), SQL_PREPARED_STMT_UPDATE_METADATA_REFERENCE) == ERR) rc = ERR; - - return rc; -} - - void close_prepared_statement(MYSQL_STMT *stmt){ if(stmt) mysql_stmt_close(stmt); } -void close_prepared_statements(struct __data *data){ - if(data->stmt_get_meta_id_by_message_id) mysql_stmt_close(data->stmt_get_meta_id_by_message_id); - if(data->stmt_insert_into_rcpt_table) mysql_stmt_close(data->stmt_insert_into_rcpt_table); - if(data->stmt_update_metadata_reference) mysql_stmt_close(data->stmt_update_metadata_reference); -} - diff --git a/src/pilerimport.c b/src/pilerimport.c index c9e5136e..c6c60b46 100644 --- a/src/pilerimport.c +++ b/src/pilerimport.c @@ -527,11 +527,6 @@ int main(int argc, char **argv){ if(open_database(&sdata, &cfg) == ERR) return 0; - if(create_prepared_statements(&sdata, &data) == ERR){ - rc = ERR; - goto ENDE; - } - setlocale(LC_CTYPE, cfg.locale); (void) openlog("pilerimport", LOG_PID, LOG_MAIL); @@ -576,9 +571,6 @@ int main(int argc, char **argv){ free_rule(data.archiving_rules); free_rule(data.retention_rules); -ENDE: - close_prepared_statements(&data); - close_database(&sdata); if(quiet == 0) printf("\n"); diff --git a/src/retr.c b/src/retr.c index 95e996f3..3d77f98b 100644 --- a/src/retr.c +++ b/src/retr.c @@ -146,13 +146,6 @@ int handle_pilerget_request(int new_sd, struct __data *data, struct __config *cf syslog(LOG_PRIORITY, "%s", ERR_MYSQL_CONNECT); #endif - if(db_conn == 1 && create_prepared_statements(&sdata, data) == ERR){ - close_prepared_statements(data); - close_database(&sdata); - db_conn = 0; - } - - if(db_conn == 0){ send(new_sd, SMTP_RESP_421_ERR_TMP, strlen(SMTP_RESP_421_ERR_TMP), 0); syslog(LOG_PRIORITY, "cannot make prepared statement"); @@ -264,7 +257,6 @@ int handle_pilerget_request(int new_sd, struct __data *data, struct __config *cf QUITTING: #ifdef NEED_MYSQL - close_prepared_statements(data); close_database(&sdata); #endif diff --git a/src/session.c b/src/session.c index a9836689..600144ed 100644 --- a/src/session.c +++ b/src/session.c @@ -72,13 +72,6 @@ int handle_smtp_session(int new_sd, struct __data *data, struct __config *cfg){ syslog(LOG_PRIORITY, "%s", ERR_MYSQL_CONNECT); #endif - if(db_conn == 1 && create_prepared_statements(&sdata, data) == ERR){ - close_prepared_statements(data); - close_database(&sdata); - db_conn = 0; - } - - if(db_conn == 0){ send(new_sd, SMTP_RESP_421_ERR_TMP, strlen(SMTP_RESP_421_ERR_TMP), 0); syslog(LOG_PRIORITY, "cannot make prepared statement"); @@ -563,7 +556,6 @@ QUITTING: update_counters(&sdata, data, &counters, cfg); #ifdef NEED_MYSQL - close_prepared_statements(data); close_database(&sdata); #endif diff --git a/src/sql.h b/src/sql.h index 2d3266cd..d1d3dbb4 100644 --- a/src/sql.h +++ b/src/sql.h @@ -18,8 +18,6 @@ void p_bind_init(struct __data *data); uint64 p_get_insert_id(MYSQL_STMT *stmt); int p_get_affected_rows(MYSQL_STMT *stmt); void close_prepared_statement(MYSQL_STMT *stmt); -int create_prepared_statements(struct session_data *sdata, struct __data *data); -void close_prepared_statements(struct __data *data); #endif /* _PILER_H */