From 63e21aa7d1e8737aa57df275b8ecd1c3bf461886 Mon Sep 17 00:00:00 2001 From: Janos SUTO Date: Fri, 11 Aug 2017 18:18:45 +0200 Subject: [PATCH] src: sql structure refactoring Signed-off-by: Janos SUTO --- src/attachment.c | 94 ++++++++++++++--------------- src/counters.c | 29 ++++----- src/defs.h | 43 +++++--------- src/import.c | 41 +++++++------ src/message.c | 147 ++++++++++++++++++++++++---------------------- src/mydomains.c | 21 ++++--- src/mysql.c | 92 ++++++++++++++--------------- src/pilerexport.c | 28 ++++----- src/reindex.c | 48 +++++++-------- src/rules.c | 56 +++++++++--------- src/rules.h | 4 +- src/sql.h | 19 +++--- 12 files changed, 309 insertions(+), 313 deletions(-) diff --git a/src/attachment.c b/src/attachment.c index 8e243191..c4a46538 100644 --- a/src/attachment.c +++ b/src/attachment.c @@ -19,10 +19,10 @@ int store_attachments(struct session_data *sdata, struct parser_state *state, struct data *data, struct config *cfg){ uint64 id=0; int i, rc=1, found, affected_rows; + struct sql sql, sql2; - - if(prepare_sql_statement(sdata, &(data->stmt_insert_into_attachment_table), SQL_PREPARED_STMT_INSERT_INTO_ATTACHMENT_TABLE) == ERR) return rc; - if(prepare_sql_statement(sdata, &(data->stmt_get_attachment_id_by_signature), SQL_PREPARED_STMT_GET_ATTACHMENT_ID_BY_SIGNATURE) == ERR) return rc; + if(prepare_sql_statement(sdata, &sql, SQL_PREPARED_STMT_INSERT_INTO_ATTACHMENT_TABLE) == ERR) return rc; + if(prepare_sql_statement(sdata, &sql2, SQL_PREPARED_STMT_GET_ATTACHMENT_ID_BY_SIGNATURE) == ERR) return rc; for(i=1; i<=state->n_attachments; i++){ @@ -31,20 +31,20 @@ int store_attachments(struct session_data *sdata, struct parser_state *state, st if(state->attachments[i].size > 0){ - p_bind_init(data); + p_bind_init(&sql2); - data->sql[data->pos] = state->attachments[i].digest; data->type[data->pos] = TYPE_STRING; data->pos++; - data->sql[data->pos] = (char *)&(state->attachments[i].size); data->type[data->pos] = TYPE_LONG; data->pos++; + sql2.sql[sql2.pos] = state->attachments[i].digest; sql2.type[sql2.pos] = TYPE_STRING; sql2.pos++; + sql2.sql[sql2.pos] = (char *)&(state->attachments[i].size); sql2.type[sql2.pos] = TYPE_LONG; sql2.pos++; - if(p_exec_query(sdata, data->stmt_get_attachment_id_by_signature, data) == OK){ + if(p_exec_stmt(sdata, &sql2) == OK){ - p_bind_init(data); + p_bind_init(&sql2); - data->sql[data->pos] = (char *)&id; data->type[data->pos] = TYPE_LONGLONG; data->len[data->pos] = sizeof(uint64); data->pos++; + sql2.sql[sql2.pos] = (char *)&id; sql2.type[sql2.pos] = TYPE_LONGLONG; sql2.len[sql2.pos] = sizeof(uint64); sql2.pos++; - p_store_results(data->stmt_get_attachment_id_by_signature, data); - if(p_fetch_results(data->stmt_get_attachment_id_by_signature) == OK) found = 1; - p_free_results(data->stmt_get_attachment_id_by_signature); + p_store_results(&sql2); + if(p_fetch_results(&sql2) == OK) found = 1; + p_free_results(&sql2); } if(found == 0){ @@ -55,20 +55,20 @@ int store_attachments(struct session_data *sdata, struct parser_state *state, st } - p_bind_init(data); + p_bind_init(&sql); - data->sql[data->pos] = sdata->ttmpfile; data->type[data->pos] = TYPE_STRING; data->pos++; - data->sql[data->pos] = (char *)&i; data->type[data->pos] = TYPE_LONG; data->pos++; - data->sql[data->pos] = state->attachments[i].digest; data->type[data->pos] = TYPE_STRING; data->pos++; - data->sql[data->pos] = state->attachments[i].filename; data->type[data->pos] = TYPE_STRING; data->pos++; - data->sql[data->pos] = state->attachments[i].type; data->type[data->pos] = TYPE_STRING; data->pos++; - data->sql[data->pos] = (char *)&(state->attachments[i].size); data->type[data->pos] = TYPE_LONG; data->pos++; - data->sql[data->pos] = (char *)&id; data->type[data->pos] = TYPE_LONGLONG; data->pos++; + sql.sql[sql.pos] = sdata->ttmpfile; sql.type[sql.pos] = TYPE_STRING; sql.pos++; + sql.sql[sql.pos] = (char *)&i; sql.type[sql.pos] = TYPE_LONG; sql.pos++; + sql.sql[sql.pos] = state->attachments[i].digest; sql.type[sql.pos] = TYPE_STRING; sql.pos++; + sql.sql[sql.pos] = state->attachments[i].filename; sql.type[sql.pos] = TYPE_STRING; sql.pos++; + sql.sql[sql.pos] = state->attachments[i].type; sql.type[sql.pos] = TYPE_STRING; sql.pos++; + sql.sql[sql.pos] = (char *)&(state->attachments[i].size); sql.type[sql.pos] = TYPE_LONG; sql.pos++; + sql.sql[sql.pos] = (char *)&id; sql.type[sql.pos] = TYPE_LONGLONG; sql.pos++; - if(p_exec_query(sdata, data->stmt_insert_into_attachment_table, data) == ERR) goto CLOSE; + if(p_exec_stmt(sdata, &sql) == ERR) goto CLOSE; - affected_rows = p_get_affected_rows(data->stmt_insert_into_attachment_table); + affected_rows = p_get_affected_rows(&sql); if(affected_rows != 1){ syslog(LOG_PRIORITY, "%s attachment sql error: affected rows: %d", sdata->ttmpfile, affected_rows); goto CLOSE; @@ -84,8 +84,8 @@ int store_attachments(struct session_data *sdata, struct parser_state *state, st rc = 0; CLOSE: - close_prepared_statement(data->stmt_insert_into_attachment_table); - close_prepared_statement(data->stmt_get_attachment_id_by_signature); + close_prepared_statement(&sql); + close_prepared_statement(&sql2); return rc; } @@ -93,28 +93,29 @@ CLOSE: int query_attachment_pointers(struct session_data *sdata, struct data *data, uint64 ptr, char *piler_id, int *id){ int rc=0; + struct sql sql; - if(prepare_sql_statement(sdata, &(data->stmt_get_attachment_pointer), SQL_PREPARED_STMT_GET_ATTACHMENT_POINTER) == ERR) return rc; + if(prepare_sql_statement(sdata, &sql, SQL_PREPARED_STMT_GET_ATTACHMENT_POINTER) == ERR) return rc; - p_bind_init(data); + p_bind_init(&sql); - data->sql[data->pos] = (char *)&ptr; data->type[data->pos] = TYPE_LONGLONG; data->pos++; + sql.sql[sql.pos] = (char *)&ptr; sql.type[sql.pos] = TYPE_LONGLONG; sql.pos++; - if(p_exec_query(sdata, data->stmt_get_attachment_pointer, data) == OK){ + if(p_exec_stmt(sdata, &sql) == OK){ - p_bind_init(data); + p_bind_init(&sql); - data->sql[data->pos] = piler_id; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = RND_STR_LEN; data->pos++; - data->sql[data->pos] = (char *)id; data->type[data->pos] = TYPE_LONG; data->len[data->pos] = sizeof(int); data->pos++; + sql.sql[sql.pos] = piler_id; sql.type[sql.pos] = TYPE_STRING; sql.len[sql.pos] = RND_STR_LEN; sql.pos++; + sql.sql[sql.pos] = (char *)&id; sql.type[sql.pos] = TYPE_LONG; sql.len[sql.pos] = sizeof(int); sql.pos++; - p_store_results(data->stmt_get_attachment_pointer, data); + p_store_results(&sql); - if(p_fetch_results(data->stmt_get_attachment_pointer) == OK) rc = 1; - p_free_results(data->stmt_get_attachment_pointer); + if(p_fetch_results(&sql) == OK) rc = 1; + p_free_results(&sql); } - close_prepared_statement(data->stmt_get_attachment_pointer); + close_prepared_statement(&sql); return rc; } @@ -123,25 +124,26 @@ int query_attachment_pointers(struct session_data *sdata, struct data *data, uin int query_attachments(struct session_data *sdata, struct data *data, struct ptr_array *ptr_arr){ int i, rc, id, attachments=0; uint64 ptr; + struct sql sql; for(i=0; istmt_query_attachment), SQL_PREPARED_STMT_QUERY_ATTACHMENT) == ERR) return attachments; + if(prepare_sql_statement(sdata, &sql, SQL_PREPARED_STMT_QUERY_ATTACHMENT) == ERR) return attachments; - p_bind_init(data); + p_bind_init(&sql); - data->sql[data->pos] = sdata->ttmpfile; data->type[data->pos] = TYPE_STRING; data->pos++; + sql.sql[sql.pos] = sdata->ttmpfile; sql.type[sql.pos] = TYPE_STRING; sql.pos++; - if(p_exec_query(sdata, data->stmt_query_attachment, data) == ERR) goto CLOSE; + if(p_exec_stmt(sdata, &sql) == ERR) goto CLOSE; - p_bind_init(data); + p_bind_init(&sql); - data->sql[data->pos] = (char *)&id; data->type[data->pos] = TYPE_LONG; data->len[data->pos] = sizeof(int); data->pos++; - data->sql[data->pos] = (char *)&ptr; data->type[data->pos] = TYPE_LONGLONG; data->len[data->pos] = sizeof(uint64); data->pos++; + sql.sql[sql.pos] = (char *)&id; sql.type[sql.pos] = TYPE_LONG; sql.len[sql.pos] = sizeof(int); sql.pos++; + sql.sql[sql.pos] = (char *)&ptr; sql.type[sql.pos] = TYPE_LONGLONG; sql.len[sql.pos] = sizeof(uint64); sql.pos++; - p_store_results(data->stmt_query_attachment, data); + p_store_results(&sql); - while(p_fetch_results(data->stmt_query_attachment) == OK){ + while(p_fetch_results(&sql) == OK){ if(id > 0 && id < MAX_ATTACHMENTS){ if(ptr > 0){ @@ -161,10 +163,10 @@ int query_attachments(struct session_data *sdata, struct data *data, struct ptr_ } } - p_free_results(data->stmt_query_attachment); + p_free_results(&sql); CLOSE: - close_prepared_statement(data->stmt_query_attachment); + close_prepared_statement(&sql); return attachments; } diff --git a/src/counters.c b/src/counters.c index 4a214863..40928bfa 100644 --- a/src/counters.c +++ b/src/counters.c @@ -14,34 +14,35 @@ struct counters load_counters(struct session_data *sdata, struct data *data){ char buf[SMALLBUFSIZE]; struct counters counters; + struct sql sql; bzero(&counters, sizeof(counters)); snprintf(buf, SMALLBUFSIZE-1, "SELECT `rcvd`, `virus`, `duplicate`, `ignore`, `size`, `stored_size` FROM `%s`", SQL_COUNTER_TABLE); - if(prepare_sql_statement(sdata, &(data->stmt_generic), buf) == ERR) return counters; + if(prepare_sql_statement(sdata, &sql, buf) == ERR) return counters; - p_bind_init(data); + p_bind_init(&sql); - if(p_exec_query(sdata, data->stmt_generic, data) == OK){ + if(p_exec_stmt(sdata, &sql) == OK){ - p_bind_init(data); + p_bind_init(&sql); - data->sql[data->pos] = (char *)&counters.c_rcvd; data->type[data->pos] = TYPE_LONGLONG; data->len[data->pos] = sizeof(uint64); data->pos++; - data->sql[data->pos] = (char *)&counters.c_virus; data->type[data->pos] = TYPE_LONGLONG; data->len[data->pos] = sizeof(uint64); data->pos++; - data->sql[data->pos] = (char *)&counters.c_duplicate; data->type[data->pos] = TYPE_LONGLONG; data->len[data->pos] = sizeof(uint64); data->pos++; - data->sql[data->pos] = (char *)&counters.c_ignore; data->type[data->pos] = TYPE_LONGLONG; data->len[data->pos] = sizeof(uint64); data->pos++; - data->sql[data->pos] = (char *)&counters.c_size; data->type[data->pos] = TYPE_LONGLONG; data->len[data->pos] = sizeof(uint64); data->pos++; - data->sql[data->pos] = (char *)&counters.c_stored_size; data->type[data->pos] = TYPE_LONGLONG; data->len[data->pos] = sizeof(uint64); data->pos++; + sql.sql[sql.pos] = (char *)&counters.c_rcvd; sql.type[sql.pos] = TYPE_LONGLONG; sql.len[sql.pos] = sizeof(uint64); sql.pos++; + sql.sql[sql.pos] = (char *)&counters.c_virus; sql.type[sql.pos] = TYPE_LONGLONG; sql.len[sql.pos] = sizeof(uint64); sql.pos++; + sql.sql[sql.pos] = (char *)&counters.c_duplicate; sql.type[sql.pos] = TYPE_LONGLONG; sql.len[sql.pos] = sizeof(uint64); sql.pos++; + sql.sql[sql.pos] = (char *)&counters.c_ignore; sql.type[sql.pos] = TYPE_LONGLONG; sql.len[sql.pos] = sizeof(uint64); sql.pos++; + sql.sql[sql.pos] = (char *)&counters.c_size; sql.type[sql.pos] = TYPE_LONGLONG; sql.len[sql.pos] = sizeof(uint64); sql.pos++; + sql.sql[sql.pos] = (char *)&counters.c_stored_size; sql.type[sql.pos] = TYPE_LONGLONG; sql.len[sql.pos] = sizeof(uint64); sql.pos++; - p_store_results(data->stmt_generic, data); - p_fetch_results(data->stmt_generic); - p_free_results(data->stmt_generic); + p_store_results(&sql); + p_fetch_results(&sql); + p_free_results(&sql); } - close_prepared_statement(data->stmt_generic); + close_prepared_statement(&sql); return counters; } diff --git a/src/defs.h b/src/defs.h index b0025f97..b166b3ed 100644 --- a/src/defs.h +++ b/src/defs.h @@ -9,9 +9,6 @@ #include #include #endif -#ifdef NEED_PSQL - #include -#endif #ifdef HAVE_TRE #include #include @@ -343,32 +340,6 @@ struct data { struct licence licence; char *dedup; int child_serial; - -#ifdef NEED_MYSQL - MYSQL_STMT *stmt_generic; - MYSQL_STMT *stmt_get_meta_id_by_message_id; - MYSQL_STMT *stmt_insert_into_rcpt_table; - MYSQL_STMT *stmt_insert_into_sphinx_table; - MYSQL_STMT *stmt_insert_into_meta_table; - MYSQL_STMT *stmt_insert_into_attachment_table; - MYSQL_STMT *stmt_insert_into_folder_message_table; - MYSQL_STMT *stmt_get_attachment_id_by_signature; - MYSQL_STMT *stmt_get_attachment_pointer; - MYSQL_STMT *stmt_query_attachment; - MYSQL_STMT *stmt_get_folder_id; - MYSQL_STMT *stmt_insert_into_folder_table; - MYSQL_STMT *stmt_update_metadata_reference; - MYSQL_STMT *stmt_update_metadata; - MYSQL_STMT *stmt_select_from_meta_table; - MYSQL_STMT *stmt_select_non_referenced_attachments; -#endif - - char *sql[MAX_SQL_VARS]; - int type[MAX_SQL_VARS]; - int len[MAX_SQL_VARS]; - unsigned long length[MAX_SQL_VARS]; - my_bool is_null[MAX_SQL_VARS]; - my_bool error[MAX_SQL_VARS]; int pos; #ifdef HAVE_TRE @@ -385,6 +356,20 @@ struct data { }; +struct sql { +#ifdef NEED_MYSQL + MYSQL_STMT *stmt; +#endif + char *sql[MAX_SQL_VARS]; + int type[MAX_SQL_VARS]; + int len[MAX_SQL_VARS]; + unsigned long length[MAX_SQL_VARS]; + my_bool is_null[MAX_SQL_VARS]; + my_bool error[MAX_SQL_VARS]; + int pos; +}; + + struct counters { unsigned long long c_rcvd; unsigned long long c_virus; diff --git a/src/import.c b/src/import.c index 33f49262..254a884a 100644 --- a/src/import.c +++ b/src/import.c @@ -145,24 +145,27 @@ int import_message(struct session_data *sdata, struct data *data, struct config int get_folder_id(struct session_data *sdata, struct data *data, char *foldername, int parent_id){ int id=ERR_FOLDER; + struct sql sql; - if(prepare_sql_statement(sdata, &(data->stmt_get_folder_id), SQL_PREPARED_STMT_GET_FOLDER_ID) == ERR) return id; + if(prepare_sql_statement(sdata, &sql, SQL_PREPARED_STMT_GET_FOLDER_ID) == ERR) return id; - p_bind_init(data); - data->sql[data->pos] = foldername; data->type[data->pos] = TYPE_STRING; data->pos++; - data->sql[data->pos] = (char *)&parent_id; data->type[data->pos] = TYPE_LONG; data->pos++; + p_bind_init(&sql); - if(p_exec_query(sdata, data->stmt_get_folder_id, data) == OK){ + sql.sql[sql.pos] = foldername; sql.type[sql.pos] = TYPE_STRING; sql.pos++; + sql.sql[sql.pos] = (char *)&parent_id; sql.type[sql.pos] = TYPE_LONG; sql.pos++; - p_bind_init(data); - data->sql[data->pos] = (char *)&id; data->type[data->pos] = TYPE_LONG; data->len[data->pos] = sizeof(unsigned long); data->pos++; + if(p_exec_stmt(sdata, &sql) == OK){ - p_store_results(data->stmt_get_folder_id, data); - p_fetch_results(data->stmt_get_folder_id); - p_free_results(data->stmt_get_folder_id); + p_bind_init(&sql); + + sql.sql[sql.pos] = (char *)&id; sql.type[sql.pos] = TYPE_LONG; sql.len[sql.pos] = sizeof(unsigned long); sql.pos++; + + p_store_results(&sql); + p_fetch_results(&sql); + p_free_results(&sql); } - close_prepared_statement(data->stmt_get_folder_id); + close_prepared_statement(&sql); return id; } @@ -170,20 +173,22 @@ int get_folder_id(struct session_data *sdata, struct data *data, char *foldernam int add_new_folder(struct session_data *sdata, struct data *data, char *foldername, int parent_id){ int id=ERR_FOLDER; + struct sql sql; if(foldername == NULL) return id; - if(prepare_sql_statement(sdata, &(data->stmt_insert_into_folder_table), SQL_PREPARED_STMT_INSERT_INTO_FOLDER_TABLE) == ERR) return id; + if(prepare_sql_statement(sdata, &sql, SQL_PREPARED_STMT_INSERT_INTO_FOLDER_TABLE) == ERR) return id; - p_bind_init(data); - data->sql[data->pos] = foldername; data->type[data->pos] = TYPE_STRING; data->pos++; - data->sql[data->pos] = (char *)&parent_id; data->type[data->pos] = TYPE_LONG; data->pos++; + p_bind_init(&sql); - if(p_exec_query(sdata, data->stmt_insert_into_folder_table, data) == OK){ - id = p_get_insert_id(data->stmt_insert_into_folder_table); + sql.sql[sql.pos] = foldername; sql.type[sql.pos] = TYPE_STRING; sql.pos++; + sql.sql[sql.pos] = (char *)&parent_id; sql.type[sql.pos] = TYPE_LONG; sql.pos++; + + if(p_exec_stmt(sdata, &sql) == OK){ + id = p_get_insert_id(&sql); } - close_prepared_statement(data->stmt_insert_into_folder_table); + close_prepared_statement(&sql); return id; } diff --git a/src/message.c b/src/message.c index 28825836..c2b1a6a2 100644 --- a/src/message.c +++ b/src/message.c @@ -20,6 +20,7 @@ int store_index_data(struct session_data *sdata, struct parser_state *state, struct data *data, uint64 id, struct config *cfg){ int rc=ERR; char *subj; + struct sql sql; if(data->folder == 0){ data->folder = get_folder_id_by_rule(data, state, sdata->tot_len, sdata->spam_message, cfg); @@ -29,7 +30,7 @@ int store_index_data(struct session_data *sdata, struct parser_state *state, str if(*subj == ' ') subj++; - if(prepare_sql_statement(sdata, &(data->stmt_insert_into_sphinx_table), SQL_PREPARED_STMT_INSERT_INTO_SPHINX_TABLE) == ERR) return rc; + if(prepare_sql_statement(sdata, &sql, SQL_PREPARED_STMT_INSERT_INTO_SPHINX_TABLE) == ERR) return rc; fix_email_address_for_sphinx(state->b_from); @@ -38,26 +39,26 @@ int store_index_data(struct session_data *sdata, struct parser_state *state, str fix_email_address_for_sphinx(state->b_to_domain); - p_bind_init(data); + p_bind_init(&sql); - data->sql[data->pos] = (char *)&id; data->type[data->pos] = TYPE_LONGLONG; data->pos++; - data->sql[data->pos] = state->b_from; data->type[data->pos] = TYPE_STRING; data->pos++; - data->sql[data->pos] = state->b_to; data->type[data->pos] = TYPE_STRING; data->pos++; - data->sql[data->pos] = state->b_from_domain; data->type[data->pos] = TYPE_STRING; data->pos++; - data->sql[data->pos] = state->b_to_domain; data->type[data->pos] = TYPE_STRING; data->pos++; - data->sql[data->pos] = subj; data->type[data->pos] = TYPE_STRING; data->pos++; - data->sql[data->pos] = state->b_body; data->type[data->pos] = TYPE_STRING; data->pos++; - data->sql[data->pos] = (char *)&sdata->now; data->type[data->pos] = TYPE_LONG; data->pos++; - data->sql[data->pos] = (char *)&sdata->sent; data->type[data->pos] = TYPE_LONG; data->pos++; - data->sql[data->pos] = (char *)&sdata->tot_len; data->type[data->pos] = TYPE_LONG; data->pos++; - data->sql[data->pos] = (char *)&sdata->direction; data->type[data->pos] = TYPE_LONG; data->pos++; - data->sql[data->pos] = (char *)&data->folder; data->type[data->pos] = TYPE_LONG; data->pos++; - data->sql[data->pos] = (char *)&state->n_attachments; data->type[data->pos] = TYPE_LONG; data->pos++; - data->sql[data->pos] = sdata->attachments; data->type[data->pos] = TYPE_STRING; data->pos++; + sql.sql[sql.pos] = (char *)&id; sql.type[sql.pos] = TYPE_LONGLONG; sql.pos++; + sql.sql[sql.pos] = state->b_from; sql.type[sql.pos] = TYPE_STRING; sql.pos++; + sql.sql[sql.pos] = state->b_to; sql.type[sql.pos] = TYPE_STRING; sql.pos++; + sql.sql[sql.pos] = state->b_from_domain; sql.type[sql.pos] = TYPE_STRING; sql.pos++; + sql.sql[sql.pos] = state->b_to_domain; sql.type[sql.pos] = TYPE_STRING; sql.pos++; + sql.sql[sql.pos] = subj; sql.type[sql.pos] = TYPE_STRING; sql.pos++; + sql.sql[sql.pos] = state->b_body; sql.type[sql.pos] = TYPE_STRING; sql.pos++; + sql.sql[sql.pos] = (char *)&sdata->now; sql.type[sql.pos] = TYPE_LONG; sql.pos++; + sql.sql[sql.pos] = (char *)&sdata->sent; sql.type[sql.pos] = TYPE_LONG; sql.pos++; + sql.sql[sql.pos] = (char *)&sdata->tot_len; sql.type[sql.pos] = TYPE_LONG; sql.pos++; + sql.sql[sql.pos] = (char *)&sdata->direction; sql.type[sql.pos] = TYPE_LONG; sql.pos++; + sql.sql[sql.pos] = (char *)&data->folder; sql.type[sql.pos] = TYPE_LONG; sql.pos++; + sql.sql[sql.pos] = (char *)&state->n_attachments; sql.type[sql.pos] = TYPE_LONG; sql.pos++; + sql.sql[sql.pos] = sdata->attachments; sql.type[sql.pos] = TYPE_STRING; sql.pos++; - if(p_exec_query(sdata, data->stmt_insert_into_sphinx_table, data) == OK) rc = OK; + if(p_exec_stmt(sdata, &sql) == OK) rc = OK; - close_prepared_statement(data->stmt_insert_into_sphinx_table); + close_prepared_statement(&sql); return rc; @@ -66,26 +67,28 @@ int store_index_data(struct session_data *sdata, struct parser_state *state, str uint64 get_metaid_by_messageid(struct session_data *sdata, struct data *data, char *message_id, char *piler_id){ uint64 id=0; + struct sql sql; - 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; + if(prepare_sql_statement(sdata, &sql, SQL_PREPARED_STMT_GET_META_ID_BY_MESSAGE_ID) == ERR) return id; - p_bind_init(data); + p_bind_init(&sql); data->sql[data->pos] = message_id; data->type[data->pos] = TYPE_STRING; data->pos++; - if(p_exec_query(sdata, data->stmt_get_meta_id_by_message_id, data) == OK){ + if(p_exec_stmt(sdata, &sql) == OK){ - p_bind_init(data); - data->sql[data->pos] = (char *)&id; data->type[data->pos] = TYPE_LONGLONG; data->len[data->pos] = sizeof(uint64); data->pos++; - data->sql[data->pos] = piler_id; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = RND_STR_LEN; data->pos++; + p_bind_init(&sql); - p_store_results(data->stmt_get_meta_id_by_message_id, data); + sql.sql[sql.pos] = (char *)&id; sql.type[sql.pos] = TYPE_LONGLONG; sql.len[sql.pos] = sizeof(uint64); sql.pos++; + sql.sql[sql.pos] = piler_id; sql.type[sql.pos] = TYPE_STRING; sql.len[sql.pos] = RND_STR_LEN; sql.pos++; - p_fetch_results(data->stmt_get_meta_id_by_message_id); + p_store_results(&sql); - p_free_results(data->stmt_get_meta_id_by_message_id); + p_fetch_results(&sql); + + p_free_results(&sql); } - mysql_stmt_close(data->stmt_get_meta_id_by_message_id); + close_prepared_statement(&sql); return id; } @@ -94,8 +97,9 @@ uint64 get_metaid_by_messageid(struct session_data *sdata, struct data *data, ch int store_recipients(struct session_data *sdata, struct data *data, char *to, uint64 id, struct config *cfg){ int ret=OK, n=0; char *p, *q, puf[SMALLBUFSIZE]; + struct sql sql; - if(prepare_sql_statement(sdata, &(data->stmt_insert_into_rcpt_table), SQL_PREPARED_STMT_INSERT_INTO_RCPT_TABLE) == ERR) return ret; + if(prepare_sql_statement(sdata, &sql, SQL_PREPARED_STMT_INSERT_INTO_RCPT_TABLE) == ERR) return ret; p = to; do { @@ -106,14 +110,13 @@ int store_recipients(struct session_data *sdata, struct data *data, char *to, ui if(q && strlen(q) > 3 && does_it_seem_like_an_email_address(puf) == 1){ q++; - p_bind_init(data); + p_bind_init(&sql); - data->sql[data->pos] = (char *)&id; data->type[data->pos] = TYPE_LONGLONG; data->pos++; - data->sql[data->pos] = &puf[0]; data->type[data->pos] = TYPE_STRING; data->pos++; - data->sql[data->pos] = q; data->type[data->pos] = TYPE_STRING; data->pos++; + sql.sql[sql.pos] = (char *)&id; sql.type[sql.pos] = TYPE_LONGLONG; sql.pos++; + sql.sql[sql.pos] = &puf[0]; sql.type[sql.pos] = TYPE_STRING; sql.pos++; + sql.sql[sql.pos] = q; sql.type[sql.pos] = TYPE_STRING; sql.pos++; - - if(p_exec_query(sdata, data->stmt_insert_into_rcpt_table, data) == ERR){ + if(p_exec_stmt(sdata, &sql) == ERR){ if(sdata->sql_errno != ER_DUP_ENTRY) ret = ERR; } else n++; @@ -121,7 +124,7 @@ int store_recipients(struct session_data *sdata, struct data *data, char *to, ui } while(p); - mysql_stmt_close(data->stmt_insert_into_rcpt_table); + close_prepared_statement(&sql); if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: added %d recipients", sdata->ttmpfile, n); @@ -140,18 +143,19 @@ void remove_recipients(struct session_data *sdata, uint64 id){ int store_folder_id(struct session_data *sdata, struct data *data, uint64 id){ int rc = ERR; + struct sql sql; if(data->folder == ERR_FOLDER) return rc; - if(prepare_sql_statement(sdata, &(data->stmt_insert_into_folder_message_table), SQL_PREPARED_STMT_INSERT_FOLDER_MESSAGE) == ERR) return rc; + if(prepare_sql_statement(sdata, &sql, SQL_PREPARED_STMT_INSERT_FOLDER_MESSAGE) == ERR) return rc; - p_bind_init(data); + p_bind_init(&sql); - data->sql[data->pos] = (char *)&data->folder; data->type[data->pos] = TYPE_LONGLONG; data->pos++; - data->sql[data->pos] = (char *)&id; data->type[data->pos] = TYPE_LONGLONG; data->pos++; + sql.sql[sql.pos] = (char *)&data->folder; sql.type[sql.pos] = TYPE_LONGLONG; sql.pos++; + sql.sql[sql.pos] = (char *)&id; sql.type[sql.pos] = TYPE_LONGLONG; sql.pos++; - if(p_exec_query(sdata, data->stmt_insert_into_folder_message_table, data) == OK) rc = OK; - close_prepared_statement(data->stmt_insert_into_folder_message_table); + if(p_exec_stmt(sdata, &sql) == OK) rc = OK; + close_prepared_statement(&sql); return rc; } @@ -168,17 +172,18 @@ void remove_folder_id(struct session_data *sdata, uint64 id){ int update_metadata_reference(struct session_data *sdata, struct parser_state *state, struct data *data, char *ref, struct config *cfg){ int ret = ERR; + struct sql sql; - if(prepare_sql_statement(sdata, &(data->stmt_update_metadata_reference), SQL_PREPARED_STMT_UPDATE_METADATA_REFERENCE) == ERR) return ret; + if(prepare_sql_statement(sdata, &sql, SQL_PREPARED_STMT_UPDATE_METADATA_REFERENCE) == ERR) return ret; - p_bind_init(data); + p_bind_init(&sql); - data->sql[data->pos] = ref; data->type[data->pos] = TYPE_STRING; data->pos++; - data->sql[data->pos] = state->reference; data->type[data->pos] = TYPE_STRING; data->pos++; + sql.sql[sql.pos] = ref; sql.type[sql.pos] = TYPE_STRING; sql.pos++; + sql.sql[sql.pos] = state->reference; sql.type[sql.pos] = TYPE_STRING; sql.pos++; - if(p_exec_query(sdata, data->stmt_update_metadata_reference, data) == OK) ret = OK; + if(p_exec_stmt(sdata, &sql) == OK) ret = OK; - mysql_stmt_close(data->stmt_update_metadata_reference); + close_prepared_statement(&sql); if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: updated meta reference for '%s', rc=%d", sdata->ttmpfile, state->reference, ret); @@ -190,7 +195,7 @@ int store_meta_data(struct session_data *sdata, struct parser_state *state, stru int rc, ret=ERR, result; char *subj, *p, s[MAXBUFSIZE], s2[SMALLBUFSIZE], vcode[2*DIGEST_LENGTH+1], ref[2*DIGEST_LENGTH+1]; uint64 id=0; - + struct sql sql; subj = state->b_subject; if(*subj == ' ') subj++; @@ -206,7 +211,7 @@ int store_meta_data(struct session_data *sdata, struct parser_state *state, stru } - if(prepare_sql_statement(sdata, &(data->stmt_insert_into_meta_table), SQL_PREPARED_STMT_INSERT_INTO_META_TABLE) == ERR) return ERR; + if(prepare_sql_statement(sdata, &sql, SQL_PREPARED_STMT_INSERT_INTO_META_TABLE) == ERR) return ERR; memset(s2, 0, sizeof(s2)); @@ -226,31 +231,31 @@ int store_meta_data(struct session_data *sdata, struct parser_state *state, stru } - p_bind_init(data); + p_bind_init(&sql); - data->sql[data->pos] = &s2[0]; data->type[data->pos] = TYPE_STRING; data->pos++; - data->sql[data->pos] = state->b_from_domain; data->type[data->pos] = TYPE_STRING; data->pos++; - data->sql[data->pos] = subj; data->type[data->pos] = TYPE_STRING; data->pos++; - data->sql[data->pos] = (char *)&sdata->spam_message; data->type[data->pos] = TYPE_LONG; data->pos++; - data->sql[data->pos] = (char *)&sdata->now; data->type[data->pos] = TYPE_LONG; data->pos++; - data->sql[data->pos] = (char *)&sdata->sent; data->type[data->pos] = TYPE_LONG; data->pos++; - data->sql[data->pos] = (char *)&sdata->retained; data->type[data->pos] = TYPE_LONGLONG; data->pos++; - data->sql[data->pos] = (char *)&sdata->tot_len; data->type[data->pos] = TYPE_LONG; data->pos++; - data->sql[data->pos] = (char *)&sdata->hdr_len; data->type[data->pos] = TYPE_LONG; data->pos++; - data->sql[data->pos] = (char *)&sdata->direction; data->type[data->pos] = TYPE_LONG; data->pos++; - data->sql[data->pos] = (char *)&state->n_attachments; data->type[data->pos] = TYPE_LONG; data->pos++; - data->sql[data->pos] = sdata->ttmpfile; data->type[data->pos] = TYPE_STRING; data->pos++; - data->sql[data->pos] = state->message_id; data->type[data->pos] = TYPE_STRING; data->pos++; - data->sql[data->pos] = &ref[0]; data->type[data->pos] = TYPE_STRING; data->pos++; - data->sql[data->pos] = sdata->digest; data->type[data->pos] = TYPE_STRING; data->pos++; - data->sql[data->pos] = sdata->bodydigest; data->type[data->pos] = TYPE_STRING; data->pos++; - data->sql[data->pos] = &vcode[0]; data->type[data->pos] = TYPE_STRING; data->pos++; + sql.sql[sql.pos] = &s2[0]; sql.type[sql.pos] = TYPE_STRING; sql.pos++; + sql.sql[sql.pos] = state->b_from_domain; sql.type[sql.pos] = TYPE_STRING; sql.pos++; + sql.sql[sql.pos] = subj; sql.type[sql.pos] = TYPE_STRING; sql.pos++; + sql.sql[sql.pos] = (char *)&sdata->spam_message; sql.type[sql.pos] = TYPE_LONG; sql.pos++; + sql.sql[sql.pos] = (char *)&sdata->now; sql.type[sql.pos] = TYPE_LONG; sql.pos++; + sql.sql[sql.pos] = (char *)&sdata->sent; sql.type[sql.pos] = TYPE_LONG; sql.pos++; + sql.sql[sql.pos] = (char *)&sdata->retained; sql.type[sql.pos] = TYPE_LONGLONG; sql.pos++; + sql.sql[sql.pos] = (char *)&sdata->tot_len; sql.type[sql.pos] = TYPE_LONG; sql.pos++; + sql.sql[sql.pos] = (char *)&sdata->hdr_len; sql.type[sql.pos] = TYPE_LONG; sql.pos++; + sql.sql[sql.pos] = (char *)&sdata->direction; sql.type[sql.pos] = TYPE_LONG; sql.pos++; + sql.sql[sql.pos] = (char *)&state->n_attachments; sql.type[sql.pos] = TYPE_LONG; sql.pos++; + sql.sql[sql.pos] = sdata->ttmpfile; sql.type[sql.pos] = TYPE_STRING; sql.pos++; + sql.sql[sql.pos] = state->message_id; sql.type[sql.pos] = TYPE_STRING; sql.pos++; + sql.sql[sql.pos] = &ref[0]; sql.type[sql.pos] = TYPE_STRING; sql.pos++; + sql.sql[sql.pos] = sdata->digest; sql.type[sql.pos] = TYPE_STRING; sql.pos++; + sql.sql[sql.pos] = sdata->bodydigest; sql.type[sql.pos] = TYPE_STRING; sql.pos++; + sql.sql[sql.pos] = &vcode[0]; sql.type[sql.pos] = TYPE_STRING; sql.pos++; - if(p_exec_query(sdata, data->stmt_insert_into_meta_table, data) == ERR){ + if(p_exec_stmt(sdata, &sql) == ERR){ ret = ERR_EXISTS; } else { - id = p_get_insert_id(data->stmt_insert_into_meta_table); + id = p_get_insert_id(&sql); rc = store_recipients(sdata, data, state->b_to, id, cfg); if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: stored recipients, rc=%d", sdata->ttmpfile, rc); @@ -267,7 +272,7 @@ int store_meta_data(struct session_data *sdata, struct parser_state *state, stru ret = OK; } - close_prepared_statement(data->stmt_insert_into_meta_table); + close_prepared_statement(&sql); return ret; } diff --git a/src/mydomains.c b/src/mydomains.c index 0048de5b..a25bd25d 100644 --- a/src/mydomains.c +++ b/src/mydomains.c @@ -13,24 +13,25 @@ void load_mydomains(struct session_data *sdata, struct data *data, struct config *cfg){ int rc; char s[SMALLBUFSIZE]; + struct sql sql; memset(s, 0, sizeof(s)); - if(prepare_sql_statement(sdata, &(data->stmt_generic), SQL_PREPARED_STMT_GET_DOMAINS) == ERR) return; + if(prepare_sql_statement(sdata, &sql, SQL_PREPARED_STMT_GET_DOMAINS) == ERR) return; - p_bind_init(data); + p_bind_init(&sql); - if(p_exec_query(sdata, data->stmt_generic, data) == OK){ + if(p_exec_stmt(sdata, &sql) == OK){ - p_bind_init(data); + p_bind_init(&sql); - data->sql[data->pos] = &s[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = sizeof(s)-2; data->pos++; + sql.sql[sql.pos] = &s[0]; sql.type[sql.pos] = TYPE_STRING; sql.len[sql.pos] = sizeof(s)-2; sql.pos++; - p_store_results(data->stmt_generic, data); + p_store_results(&sql); - while(p_fetch_results(data->stmt_generic) == OK){ + while(p_fetch_results(&sql) == OK){ rc = addnode(data->mydomains, s); if(rc == 0) syslog(LOG_PRIORITY, "failed to append mydomain: '%s'", s); @@ -39,10 +40,10 @@ void load_mydomains(struct session_data *sdata, struct data *data, struct config memset(s, 0, sizeof(s)); } - p_free_results(data->stmt_generic); + p_free_results(&sql); } - close_prepared_statement(data->stmt_generic); + close_prepared_statement(&sql); } @@ -65,5 +66,3 @@ int is_email_address_on_my_domains(char *email, struct data *data){ return rc; } - - diff --git a/src/mysql.c b/src/mysql.c index 91addb05..810b4dd1 100644 --- a/src/mysql.c +++ b/src/mysql.c @@ -38,15 +38,15 @@ void close_database(struct session_data *sdata){ } -void p_bind_init(struct data *data){ +void p_bind_init(struct sql *sql){ int i; - data->pos = 0; + sql->pos = 0; for(i=0; isql[i] = NULL; - data->type[i] = TYPE_UNDEF; - data->len[i] = 0; + sql->sql[i] = NULL; + sql->type[i] = TYPE_UNDEF; + sql->len[i] = 0; } } @@ -56,7 +56,7 @@ void p_query(struct session_data *sdata, char *s){ } -int p_exec_query(struct session_data *sdata, MYSQL_STMT *stmt, struct data *data){ +int p_exec_stmt(struct session_data *sdata, struct sql *sql){ MYSQL_BIND bind[MAX_SQL_VARS]; unsigned long length[MAX_SQL_VARS]; int i, ret=ERR; @@ -65,10 +65,10 @@ int p_exec_query(struct session_data *sdata, MYSQL_STMT *stmt, struct data *data memset(bind, 0, sizeof(bind)); for(i=0; itype[i] > TYPE_UNDEF){ + if(sql->type[i] > TYPE_UNDEF){ - switch(data->type[i]) { + switch(sql->type[i]) { case TYPE_SHORT: bind[i].buffer_type = MYSQL_TYPE_SHORT; bind[i].length = 0; @@ -88,7 +88,7 @@ int p_exec_query(struct session_data *sdata, MYSQL_STMT *stmt, struct data *data case TYPE_STRING: bind[i].buffer_type = MYSQL_TYPE_STRING; - length[i] = strlen(data->sql[i]); + length[i] = strlen(sql->sql[i]); bind[i].length = &length[i]; break; @@ -101,7 +101,7 @@ int p_exec_query(struct session_data *sdata, MYSQL_STMT *stmt, struct data *data }; - bind[i].buffer = data->sql[i]; + bind[i].buffer = sql->sql[i]; bind[i].is_null = 0; @@ -109,15 +109,15 @@ int p_exec_query(struct session_data *sdata, MYSQL_STMT *stmt, struct data *data else { break; } } - if(mysql_stmt_bind_param(stmt, bind)){ - sdata->sql_errno = mysql_stmt_errno(stmt); - syslog(LOG_PRIORITY, "%s: mysql_stmt_bind_param() error: %s (errno: %d)", sdata->ttmpfile, mysql_stmt_error(stmt), sdata->sql_errno); + if(mysql_stmt_bind_param(sql->stmt, bind)){ + sdata->sql_errno = mysql_stmt_errno(sql->stmt); + syslog(LOG_PRIORITY, "%s: error: mysql_stmt_bind_param() '%s' (errno: %d)", sdata->ttmpfile, mysql_stmt_error(sql->stmt), sdata->sql_errno); return ret; } - if(mysql_stmt_execute(stmt)){ - sdata->sql_errno = mysql_stmt_errno(stmt); - syslog(LOG_PRIORITY, "%s: mysql_stmt_execute error: *%s* (errno: %d)", sdata->ttmpfile, mysql_error(&(sdata->mysql)), sdata->sql_errno); + if(mysql_stmt_execute(sql->stmt)){ + sdata->sql_errno = mysql_stmt_errno(sql->stmt); + syslog(LOG_PRIORITY, "%s: error: mysql_stmt_execute() '%s' (errno: %d)", sdata->ttmpfile, mysql_error(&(sdata->mysql)), sdata->sql_errno); return ret; } @@ -127,16 +127,16 @@ int p_exec_query(struct session_data *sdata, MYSQL_STMT *stmt, struct data *data } -int p_store_results(MYSQL_STMT *stmt, struct data *data){ +int p_store_results(struct sql *sql){ MYSQL_BIND bind[MAX_SQL_VARS]; int i, ret=ERR; memset(bind, 0, sizeof(bind)); for(i=0; itype[i] > TYPE_UNDEF){ + if(sql->type[i] > TYPE_UNDEF){ - switch(data->type[i]) { + switch(sql->type[i]) { case TYPE_SHORT: bind[i].buffer_type = MYSQL_TYPE_SHORT; break; @@ -152,7 +152,7 @@ int p_store_results(MYSQL_STMT *stmt, struct data *data){ case TYPE_STRING: bind[i].buffer_type = MYSQL_TYPE_STRING; - bind[i].buffer_length = data->len[i]; + bind[i].buffer_length = sql->len[i]; break; default: @@ -162,21 +162,21 @@ int p_store_results(MYSQL_STMT *stmt, struct data *data){ }; - bind[i].buffer = (char *)data->sql[i]; - bind[i].is_null = &(data->is_null[i]); - bind[i].length = &(data->length[i]); - bind[i].error = &(data->error[i]); + bind[i].buffer = (char *)sql->sql[i]; + bind[i].is_null = &(sql->is_null[i]); + bind[i].length = &(sql->length[i]); + bind[i].error = &(sql->error[i]); } else { break; } } - if(mysql_stmt_bind_result(stmt, bind)){ + if(mysql_stmt_bind_result(sql->stmt, bind)){ return ret; } - if(mysql_stmt_store_result(stmt)){ + if(mysql_stmt_store_result(sql->stmt)){ return ret; } @@ -186,39 +186,39 @@ int p_store_results(MYSQL_STMT *stmt, struct data *data){ } -int p_fetch_results(MYSQL_STMT *stmt){ +int p_fetch_results(struct sql *sql){ - if(mysql_stmt_fetch(stmt) == 0) return OK; + if(mysql_stmt_fetch(sql->stmt) == 0) return OK; return ERR; } -void p_free_results(MYSQL_STMT *stmt){ - mysql_stmt_free_result(stmt); +void p_free_results(struct sql *sql){ + mysql_stmt_free_result(sql->stmt); } -uint64 p_get_insert_id(MYSQL_STMT *stmt){ - return mysql_stmt_insert_id(stmt); +uint64 p_get_insert_id(struct sql *sql){ + return mysql_stmt_insert_id(sql->stmt); } -int p_get_affected_rows(MYSQL_STMT *stmt){ - return mysql_stmt_affected_rows(stmt); +int p_get_affected_rows(struct sql *sql){ + return mysql_stmt_affected_rows(sql->stmt); } -int prepare_sql_statement(struct session_data *sdata, MYSQL_STMT **stmt, char *s){ +int prepare_sql_statement(struct session_data *sdata, struct sql *sql, char *s){ - *stmt = mysql_stmt_init(&(sdata->mysql)); - if(!*stmt){ - syslog(LOG_PRIORITY, "%s: mysql_stmt_init() error", sdata->ttmpfile); + sql->stmt = mysql_stmt_init(&(sdata->mysql)); + if(!(sql->stmt)){ + syslog(LOG_PRIORITY, "%s: error: mysql_stmt_init()", sdata->ttmpfile); return ERR; } - if(mysql_stmt_prepare(*stmt, s, strlen(s))){ - syslog(LOG_PRIORITY, "%s: mysql_stmt_prepare() error: %s => sql: %s", sdata->ttmpfile, mysql_stmt_error(*stmt), s); + if(mysql_stmt_prepare(sql->stmt, s, strlen(s))){ + syslog(LOG_PRIORITY, "%s: error: mysql_stmt_prepare() %s => sql: %s", sdata->ttmpfile, mysql_stmt_error(sql->stmt), s); return ERR; } @@ -226,6 +226,11 @@ int prepare_sql_statement(struct session_data *sdata, MYSQL_STMT **stmt, char *s } +void close_prepared_statement(struct sql *sql){ + if(sql->stmt) mysql_stmt_close(sql->stmt); +} + + void insert_offset(struct session_data *sdata, int server_id){ char s[SMALLBUFSIZE]; uint64 id = server_id * 1000000000000ULL; @@ -234,10 +239,3 @@ void insert_offset(struct session_data *sdata, int server_id){ mysql_real_query(&(sdata->mysql), s, strlen(s)); } - - -void close_prepared_statement(MYSQL_STMT *stmt){ - if(stmt) mysql_stmt_close(stmt); -} - - diff --git a/src/pilerexport.c b/src/pilerexport.c index 111f6273..92428da6 100644 --- a/src/pilerexport.c +++ b/src/pilerexport.c @@ -341,27 +341,27 @@ int export_emails_matching_to_query(struct session_data *sdata, struct data *dat char digest[SMALLBUFSIZE], bodydigest[SMALLBUFSIZE]; char filename[SMALLBUFSIZE]; int rc=0; + struct sql sql; + + if(prepare_sql_statement(sdata, &sql, s) == ERR) return ERR; - if(prepare_sql_statement(sdata, &(data->stmt_generic), s) == ERR) return ERR; + p_bind_init(&sql); - - p_bind_init(data); - - if(p_exec_query(sdata, data->stmt_generic, data) == ERR) goto ENDE; + if(p_exec_stmt(sdata, &sql) == ERR) goto ENDE; - p_bind_init(data); + p_bind_init(&sql); - data->sql[data->pos] = (char *)&id; data->type[data->pos] = TYPE_LONGLONG; data->len[data->pos] = sizeof(uint64); data->pos++; - data->sql[data->pos] = sdata->ttmpfile; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = RND_STR_LEN; data->pos++; - data->sql[data->pos] = &digest[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = sizeof(digest)-2; data->pos++; - data->sql[data->pos] = &bodydigest[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = sizeof(bodydigest)-2; data->pos++; + sql.sql[sql.pos] = (char *)&id; sql.type[sql.pos] = TYPE_LONGLONG; sql.len[sql.pos] = sizeof(uint64); sql.pos++; + sql.sql[sql.pos] = sdata->ttmpfile; sql.type[sql.pos] = TYPE_STRING; sql.len[sql.pos] = RND_STR_LEN; sql.pos++; + sql.sql[sql.pos] = &digest[0]; sql.type[sql.pos] = TYPE_STRING; sql.len[sql.pos] = sizeof(digest)-2; sql.pos++; + sql.sql[sql.pos] = &bodydigest[0]; sql.type[sql.pos] = TYPE_STRING; sql.len[sql.pos] = sizeof(bodydigest)-2; sql.pos++; - p_store_results(data->stmt_generic, data); + p_store_results(&sql); - while(p_fetch_results(data->stmt_generic) == OK){ + while(p_fetch_results(&sql) == OK){ if(id > 0){ @@ -397,10 +397,10 @@ int export_emails_matching_to_query(struct session_data *sdata, struct data *dat } - p_free_results(data->stmt_generic); + p_free_results(&sql); ENDE: - close_prepared_statement(data->stmt_generic); + close_prepared_statement(&sql); printf("\n"); diff --git a/src/reindex.c b/src/reindex.c index 472b1a63..b42b5893 100644 --- a/src/reindex.c +++ b/src/reindex.c @@ -45,27 +45,28 @@ void p_clean_exit(char *msg, int rc){ uint64 get_max_meta_id(struct session_data *sdata, struct data *data){ char s[SMALLBUFSIZE]; uint64 id=0; + struct sql sql; snprintf(s, sizeof(s)-1, "SELECT MAX(`id`) FROM %s", SQL_METADATA_TABLE); - if(prepare_sql_statement(sdata, &(data->stmt_generic), s) == ERR) return id; + if(prepare_sql_statement(sdata, &sql, s) == ERR) return id; - p_bind_init(data); + p_bind_init(&sql); - if(p_exec_query(sdata, data->stmt_generic, data) == OK){ + if(p_exec_stmt(sdata, &sql) == OK){ - p_bind_init(data); + p_bind_init(&sql); - data->sql[data->pos] = (char *)&id; data->type[data->pos] = TYPE_LONGLONG; data->len[data->pos] = sizeof(uint64); data->pos++; + sql.sql[sql.pos] = (char *)&id; sql.type[sql.pos] = TYPE_LONGLONG; sql.len[sql.pos] = sizeof(uint64); sql.pos++; - p_store_results(data->stmt_generic, data); - p_fetch_results(data->stmt_generic); - p_free_results(data->stmt_generic); + p_store_results(&sql); + p_fetch_results(&sql); + p_free_results(&sql); } - close_prepared_statement(data->stmt_generic); + close_prepared_statement(&sql); return id; @@ -79,6 +80,7 @@ uint64 retrieve_email_by_metadata_id(struct session_data *sdata, struct data *da int rc=0; uint64 stored_id=0, reindexed=0, delta; struct parser_state state; + struct sql sql; delta = to_id - from_id; @@ -88,25 +90,25 @@ uint64 retrieve_email_by_metadata_id(struct session_data *sdata, struct data *da snprintf(s, sizeof(s)-1, "SELECT `id`, `piler_id`, `arrived`, `sent` FROM %s WHERE (id BETWEEN %llu AND %llu) AND `deleted`=0", SQL_METADATA_TABLE, from_id, to_id); - if(prepare_sql_statement(sdata, &(data->stmt_generic), s) == ERR) return reindexed; + if(prepare_sql_statement(sdata, &sql, s) == ERR) return reindexed; - p_bind_init(data); + p_bind_init(&sql); - if(p_exec_query(sdata, data->stmt_generic, data) == OK){ + if(p_exec_stmt(sdata, &sql) == OK){ - p_bind_init(data); + p_bind_init(&sql); - data->sql[data->pos] = (char *)&stored_id; data->type[data->pos] = TYPE_LONGLONG; data->len[data->pos] = sizeof(uint64); data->pos++; - data->sql[data->pos] = sdata->ttmpfile; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = RND_STR_LEN+2; data->pos++; - data->sql[data->pos] = (char *)&(sdata->now); data->type[data->pos] = TYPE_LONG; data->len[data->pos] = sizeof(unsigned long); data->pos++; - data->sql[data->pos] = (char *)&(sdata->sent); data->type[data->pos] = TYPE_LONG; data->len[data->pos] = sizeof(unsigned long); data->pos++; + sql.sql[sql.pos] = (char *)&stored_id; sql.type[sql.pos] = TYPE_LONGLONG; sql.len[sql.pos] = sizeof(uint64); sql.pos++; + sql.sql[sql.pos] = sdata->ttmpfile; sql.type[sql.pos] = TYPE_STRING; sql.len[sql.pos] = RND_STR_LEN+2; sql.pos++; + sql.sql[sql.pos] = (char *)&(sdata->now); sql.type[sql.pos] = TYPE_LONG; sql.len[sql.pos] = sizeof(unsigned long); sql.pos++; + sql.sql[sql.pos] = (char *)&(sdata->sent); sql.type[sql.pos] = TYPE_LONG; sql.len[sql.pos] = sizeof(unsigned long); sql.pos++; if(cfg->enable_folders == 1){ - data->sql[data->pos] = (char *)&(data->folder); data->type[data->pos] = TYPE_LONG; data->len[data->pos] = sizeof(unsigned long); data->pos++; + sql.sql[sql.pos] = (char *)&(data->folder); sql.type[sql.pos] = TYPE_LONG; sql.len[sql.pos] = sizeof(unsigned long); sql.pos++; } - p_store_results(data->stmt_generic, data); + p_store_results(&sql); - while(p_fetch_results(data->stmt_generic) == OK){ + while(p_fetch_results(&sql) == OK){ if(stored_id > 0){ @@ -147,10 +149,10 @@ uint64 retrieve_email_by_metadata_id(struct session_data *sdata, struct data *da } - p_free_results(data->stmt_generic); + p_free_results(&sql); } - close_prepared_statement(data->stmt_generic); + close_prepared_statement(&sql); if(progressbar) printf("\n"); @@ -269,5 +271,3 @@ int main(int argc, char **argv){ return 0; } - - diff --git a/src/rules.c b/src/rules.c index 7a95bd41..41fd4d97 100644 --- a/src/rules.c +++ b/src/rules.c @@ -13,6 +13,7 @@ void load_rules(struct session_data *sdata, struct data *data, struct node *xhash[], char *table){ char s[SMALLBUFSIZE]; struct rule_cond rule_cond; + struct sql sql; memset(rule_cond.domain, 0, SMALLBUFSIZE); memset(rule_cond.from, 0, SMALLBUFSIZE); @@ -28,34 +29,35 @@ void load_rules(struct session_data *sdata, struct data *data, struct node *xhas snprintf(s, sizeof(s)-1, "SELECT `domain`, `from`, `to`, `subject`, `body`, `_size`, `size`, `attachment_name`, `attachment_type`, `_attachment_size`, `attachment_size`, `spam`, `days`, `folder_id` FROM `%s`", table); - if(prepare_sql_statement(sdata, &(data->stmt_generic), s) == ERR) return; + if(prepare_sql_statement(sdata, &sql, s) == ERR) return; - p_bind_init(data); + p_bind_init(&sql); - if(p_exec_query(sdata, data->stmt_generic, data) == OK){ + if(p_exec_stmt(sdata, &sql) == OK){ - p_bind_init(data); + p_bind_init(&sql); - data->sql[data->pos] = &rule_cond.domain[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = SMALLBUFSIZE-2; data->pos++; - data->sql[data->pos] = &rule_cond.from[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = SMALLBUFSIZE-2; data->pos++; - data->sql[data->pos] = &rule_cond.to[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = SMALLBUFSIZE-2; data->pos++; - data->sql[data->pos] = &rule_cond.subject[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = SMALLBUFSIZE-2; data->pos++; - data->sql[data->pos] = &rule_cond.body[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = SMALLBUFSIZE-2; data->pos++; - data->sql[data->pos] = &rule_cond._size[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = SMALLBUFSIZE-2; data->pos++; - data->sql[data->pos] = (char *)&rule_cond.size; data->type[data->pos] = TYPE_LONG; data->len[data->pos] = sizeof(int); data->pos++; - data->sql[data->pos] = &rule_cond.attachment_name[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = SMALLBUFSIZE-2; data->pos++; - data->sql[data->pos] = &rule_cond.attachment_type[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = SMALLBUFSIZE-2; data->pos++; - data->sql[data->pos] = &rule_cond._attachment_size[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = SMALLBUFSIZE-2; data->pos++; - data->sql[data->pos] = (char *)&rule_cond.attachment_size; data->type[data->pos] = TYPE_LONG; data->len[data->pos] = sizeof(int); data->pos++; - data->sql[data->pos] = (char *)&rule_cond.spam; data->type[data->pos] = TYPE_LONG; data->len[data->pos] = sizeof(int); data->pos++; - data->sql[data->pos] = (char *)&rule_cond.days; data->type[data->pos] = TYPE_LONG; data->len[data->pos] = sizeof(int); data->pos++; - data->sql[data->pos] = (char *)&rule_cond.folder_id; data->type[data->pos] = TYPE_LONG; data->len[data->pos] = sizeof(int); data->pos++; + sql.sql[sql.pos] = &rule_cond.domain[0]; sql.type[sql.pos] = TYPE_STRING; sql.len[sql.pos] = sizeof(rule_cond.domain)-2; sql.pos++; + sql.sql[sql.pos] = &rule_cond.from[0]; sql.type[sql.pos] = TYPE_STRING; sql.len[sql.pos] = sizeof(rule_cond.from)-2; sql.pos++; + sql.sql[sql.pos] = &rule_cond.to[0]; sql.type[sql.pos] = TYPE_STRING; sql.len[sql.pos] = sizeof(rule_cond.to)-2; sql.pos++; + sql.sql[sql.pos] = &rule_cond.subject[0]; sql.type[sql.pos] = TYPE_STRING; sql.len[sql.pos] = sizeof(rule_cond.subject)-2; sql.pos++; + sql.sql[sql.pos] = &rule_cond.body[0]; sql.type[sql.pos] = TYPE_STRING; sql.len[sql.pos] = sizeof(rule_cond.body)-2; sql.pos++; + sql.sql[sql.pos] = &rule_cond._size[0]; sql.type[sql.pos] = TYPE_STRING; sql.len[sql.pos] = sizeof(rule_cond._size)-2; sql.pos++; + sql.sql[sql.pos] = (char *)&rule_cond.size; sql.type[sql.pos] = TYPE_LONG; sql.len[sql.pos] = sizeof(rule_cond.size); sql.pos++; + sql.sql[sql.pos] = &rule_cond.attachment_name[0]; sql.type[sql.pos] = TYPE_STRING; sql.len[sql.pos] = sizeof(rule_cond.attachment_name)-2; sql.pos++; + sql.sql[sql.pos] = &rule_cond.attachment_type[0]; sql.type[sql.pos] = TYPE_STRING; sql.len[sql.pos] = sizeof(rule_cond.attachment_type)-2; sql.pos++; + sql.sql[sql.pos] = &rule_cond._attachment_size[0]; sql.type[sql.pos] = TYPE_STRING; sql.len[sql.pos] = sizeof(rule_cond._attachment_size)-2; sql.pos++; + sql.sql[sql.pos] = (char *)&rule_cond.attachment_size; sql.type[sql.pos] = TYPE_LONG; sql.len[sql.pos] = sizeof(rule_cond.attachment_size); sql.pos++; + sql.sql[sql.pos] = (char *)&rule_cond.spam; sql.type[sql.pos] = TYPE_LONG; sql.len[sql.pos] = sizeof(rule_cond.spam); sql.pos++; + sql.sql[sql.pos] = (char *)&rule_cond.days; sql.type[sql.pos] = TYPE_LONG; sql.len[sql.pos] = sizeof(rule_cond.days); sql.pos++; + sql.sql[sql.pos] = (char *)&rule_cond.folder_id; sql.type[sql.pos] = TYPE_LONG; sql.len[sql.pos] = sizeof(rule_cond.folder_id); sql.pos++; - p_store_results(data->stmt_generic, data); - while(p_fetch_results(data->stmt_generic) == OK){ - append_rule(xhash, &rule_cond, data); + p_store_results(&sql); + + while(p_fetch_results(&sql) == OK){ + append_rule(xhash, &rule_cond); memset(rule_cond.domain, 0, SMALLBUFSIZE); memset(rule_cond.from, 0, SMALLBUFSIZE); @@ -70,15 +72,15 @@ void load_rules(struct session_data *sdata, struct data *data, struct node *xhas rule_cond.size = rule_cond.attachment_size = rule_cond.spam = rule_cond.days = rule_cond.folder_id = 0; } - p_free_results(data->stmt_generic); + p_free_results(&sql); } - close_prepared_statement(data->stmt_generic); + close_prepared_statement(&sql); } -int append_rule(struct node *xhash[], struct rule_cond *rule_cond, struct data *data){ +int append_rule(struct node *xhash[], struct rule_cond *rule_cond){ struct node *q, *Q=NULL, *node; struct rule *rule; int rc=0; @@ -88,7 +90,7 @@ int append_rule(struct node *xhash[], struct rule_cond *rule_cond, struct data * memset(node, 0, sizeof(struct node)); node->r = NULL; - rule = create_rule_item(rule_cond, data); + rule = create_rule_item(rule_cond); if(rule == NULL){ free(node); @@ -116,7 +118,7 @@ int append_rule(struct node *xhash[], struct rule_cond *rule_cond, struct data * } -struct rule *create_rule_item(struct rule_cond *rule_cond, struct data *data){ +struct rule *create_rule_item(struct rule_cond *rule_cond){ struct rule *h=NULL; char empty = '\0'; int len; @@ -130,7 +132,7 @@ struct rule *create_rule_item(struct rule_cond *rule_cond, struct data *data){ h->compiled = 1; h->domain = NULL; - h->domainlen = data->length[0]; + h->domainlen = strlen(rule_cond->domain); if(h->domainlen > 2){ h->domain = malloc(h->domainlen+2); diff --git a/src/rules.h b/src/rules.h index dbae4103..4fa432b1 100644 --- a/src/rules.h +++ b/src/rules.h @@ -8,8 +8,8 @@ #include "defs.h" void load_rules(struct session_data *sdata, struct data *data, struct node *xhash[], char *table); -int append_rule(struct node *xhash[], struct rule_cond *rule_cond, struct data *data); -struct rule *create_rule_item(struct rule_cond *rule_cond, struct data *data); +int append_rule(struct node *xhash[], struct rule_cond *rule_cond); +struct rule *create_rule_item(struct rule_cond *rule_cond); char *check_againt_ruleset(struct node *xhash[], struct parser_state *state, int size, int spam); time_t query_retain_period(struct data *data, struct parser_state *state, int size, int spam, struct config *cfg); int get_folder_id_by_rule(struct data *data, struct parser_state *state, int size, int spam, struct config *cfg); diff --git a/src/sql.h b/src/sql.h index 353e0f07..9118de31 100644 --- a/src/sql.h +++ b/src/sql.h @@ -8,17 +8,16 @@ int open_database(struct session_data *sdata, struct config *cfg); void close_database(struct session_data *sdata); -int prepare_sql_statement(struct session_data *sdata, MYSQL_STMT **stmt, char *s); +int prepare_sql_statement(struct session_data *sdata, struct sql *sql, char *s); void p_query(struct session_data *sdata, char *s); -int p_exec_query(struct session_data *sdata, MYSQL_STMT *stmt, struct data *data); -int p_store_results(MYSQL_STMT *stmt, struct data *data); -int p_fetch_results(MYSQL_STMT *stmt); -void p_free_results(MYSQL_STMT *stmt); -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 p_exec_stmt(struct session_data *sdata, struct sql *sql); +int p_store_results(struct sql *sql); +int p_fetch_results(struct sql *sql); +void p_free_results(struct sql *sql); +void p_bind_init(struct sql *sql); +uint64 p_get_insert_id(struct sql *sql); +int p_get_affected_rows(struct sql *sql); +void close_prepared_statement(struct sql *sql); #endif /* _PILER_H */ -