From 67891084e4bf2035549c1b7126f8fd4d447c235d Mon Sep 17 00:00:00 2001 From: SJ Date: Tue, 15 Mar 2016 19:11:26 +0100 Subject: [PATCH] added fixmetadata utility Signed-off-by: SJ --- src/Makefile.in | 7 ++- src/config.h | 3 +- src/defs.h | 1 + src/fixmetadata.c | 128 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 136 insertions(+), 3 deletions(-) create mode 100644 src/fixmetadata.c diff --git a/src/Makefile.in b/src/Makefile.in index 4c5ef87a..50580786 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -33,7 +33,7 @@ MAKE = `which make` INSTALL = @INSTALL@ -all: libpiler.a piler pilerconf pilerget pileraget pilerimport pilerexport pilerpurge reindex test +all: libpiler.a piler pilerconf pilerget pileraget pilerimport pilerexport pilerpurge reindex test fixmetadata install: install-piler @@ -69,6 +69,9 @@ pilerconf: pilerconf.c libpiler.a reindex: reindex.c libpiler.a $(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o $@ $< -lpiler $(LIBS) $(LIBDIR) +fixmetadata: fixmetadata.c libpiler.a + $(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o $@ $< -lpiler $(LIBS) $(LIBDIR) + test: test.c libpiler.a $(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o pilertest $< -lpiler $(LIBS) $(LIBDIR) @LDFLAGS@ @@ -93,7 +96,7 @@ install-piler: $(INSTALL) -m 6755 -o $(RUNNING_USER) -g $(RUNNING_GROUP) pilertest $(DESTDIR)$(bindir) clean: - rm -f *.o *.a libpiler.so* piler pilerconf pilerget pileraget pilerimport pilerexport pilerpurge pilertest reindex + rm -f *.o *.a libpiler.so* piler pilerconf pilerget pileraget pilerimport pilerexport pilerpurge pilertest reindex fixmetadata distclean: clean rm -f Makefile diff --git a/src/config.h b/src/config.h index beef97f1..c273b2d5 100644 --- a/src/config.h +++ b/src/config.h @@ -13,7 +13,7 @@ #define VERSION "1.2.0-master" -#define BUILD 940 +#define BUILD 941 #define HOSTID "mailarchiver" @@ -105,6 +105,7 @@ #define SQL_PREPARED_STMT_GET_FOLDER_ID "SELECT `id` FROM " SQL_FOLDER_TABLE " WHERE `name`=? AND `parent_id`=?" #define SQL_PREPARED_STMT_INSERT_INTO_FOLDER_TABLE "INSERT INTO `" SQL_FOLDER_TABLE "` (`name`, `parent_id`) VALUES(?,?)" #define SQL_PREPARED_STMT_UPDATE_METADATA_REFERENCE "UPDATE " SQL_METADATA_TABLE " SET reference=? WHERE message_id=? AND reference=''" +#define SQL_PREPARED_STMT_UPDATE_META_TABLE "UPDATE " SQL_METADATA_TABLE " SET subject=?, attachments=? WHERE id=?" #define SQL_PREPARED_STMT_GET_GUI_IMPORT_JOBS "SELECT id, type, username, password, server FROM " SQL_IMPORT_TABLE " WHERE started=0 ORDER BY id LIMIT 0,1" #define SQL_PREPARED_STMT_INSERT_FOLDER_MESSAGE "INSERT INTO " SQL_FOLDER_MESSAGE_TABLE " (`folder_id`, `id`) VALUES(?,?)" diff --git a/src/defs.h b/src/defs.h index 3fb72e70..96fc8171 100644 --- a/src/defs.h +++ b/src/defs.h @@ -334,6 +334,7 @@ struct __data { 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 diff --git a/src/fixmetadata.c b/src/fixmetadata.c new file mode 100644 index 00000000..8327273f --- /dev/null +++ b/src/fixmetadata.c @@ -0,0 +1,128 @@ +/* + * fixmetadata.c, SJ + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +uint64 get_metaid_by_messageid(struct session_data *sdata, struct __data *data, char *message_id, struct __config *cfg); + + +int update_meta_data(struct session_data *sdata, struct parser_state *state, struct __data *data, struct __config *cfg){ + int ret=ERR; + uint64 id=0; + char *subj; + + id = get_metaid_by_messageid(sdata, data, state->message_id, cfg); + + if(id <= 0) return ERR; + + subj = state->b_subject; + if(*subj == ' ') subj++; + + if(prepare_sql_statement(sdata, &(data->stmt_update_metadata), SQL_PREPARED_STMT_UPDATE_META_TABLE, cfg) == ERR) return ret; + + p_bind_init(data); + + data->sql[data->pos] = subj; data->type[data->pos] = TYPE_STRING; data->pos++; + data->sql[data->pos] = (char *)&state->n_attachments; data->type[data->pos] = TYPE_LONG; data->pos++; + data->sql[data->pos] = (char *)&id; data->type[data->pos] = TYPE_LONGLONG; data->pos++; + + if(p_exec_query(sdata, data->stmt_update_metadata, data) == OK) ret = OK; + + close_prepared_statement(data->stmt_update_metadata); + + return ret; +} + + +int main(int argc, char **argv){ + int i; + struct stat st; + struct session_data sdata; + struct parser_state state; + struct __config cfg; + struct __data data; + + srand(getpid()); + + if(argc < 2){ + fprintf(stderr, "usage: %s \n", argv[0]); + exit(1); + } + + if(!can_i_write_current_directory()) __fatal("cannot write current directory!"); + + if(stat(argv[1], &st) != 0){ + fprintf(stderr, "%s is not found\n", argv[1]); + return 0; + } + + (void) openlog("fixmetadata", LOG_PID, LOG_MAIL); + + cfg = read_config(CONFIG_FILE); + + if(open_database(&sdata, &cfg) == ERR) return 0; + + setlocale(LC_CTYPE, cfg.locale); + + data.folder = 0; + data.recursive_folder_names = 0; + + inithash(data.mydomains); + + //initrules(data.archiving_rules); + //initrules(data.retention_rules); + //initrules(data.folder_rules); + + load_mydomains(&sdata, &data, &cfg); + + //load_rules(&sdata, &data, data.archiving_rules, SQL_ARCHIVING_RULE_TABLE, &cfg); + //load_rules(&sdata, &data, data.retention_rules, SQL_RETENTION_RULE_TABLE, &cfg); + //load_rules(&sdata, &data, data.folder_rules, SQL_FOLDER_RULE_TABLE, &cfg); + + + init_session_data(&sdata, &cfg); + + sdata.delivered = 0; + sdata.tot_len = st.st_size; + + snprintf(sdata.ttmpfile, SMALLBUFSIZE-1, "%s", argv[1]); + snprintf(sdata.filename, SMALLBUFSIZE-1, "%s", argv[1]); + snprintf(sdata.tmpframe, SMALLBUFSIZE-1, "%s.m", argv[1]); + + state = parse_message(&sdata, 1, &data, &cfg); + post_parse(&sdata, &state, &cfg); + + update_meta_data(&sdata, &state, &data, &cfg); + + //sdata.retained = sdata.now + query_retain_period(&data, &state, st.st_size, sdata.spam_message, &cfg); + + //clearrules(data.archiving_rules); + //clearrules(data.retention_rules); + //clearrules(data.folder_rules); + + clearhash(data.mydomains); + + for(i=1; i<=state.n_attachments; i++){ + unlink(state.attachments[i].internalname); + } + + unlink(sdata.tmpframe); + + close_database(&sdata); + + return 0; +} + +