added recursive folder support to pilerimport

This commit is contained in:
SJ 2012-09-03 17:12:02 +02:00
parent fd976f1daf
commit 36e395426f
6 changed files with 83 additions and 16 deletions

View File

@ -119,15 +119,15 @@ ENDE:
}
unsigned long get_folder_id(struct session_data *sdata, char *foldername){
unsigned long get_folder_id(struct session_data *sdata, char *foldername, int parent_id){
unsigned long id=0;
char s[SMALLBUFSIZE];
MYSQL_STMT *stmt;
MYSQL_BIND bind[1];
unsigned long len[1];
MYSQL_BIND bind[2];
unsigned long len[2];
snprintf(s, SMALLBUFSIZE-1, "SELECT `id` FROM %s WHERE `name`=?", SQL_FOLDER_TABLE);
snprintf(s, SMALLBUFSIZE-1, "SELECT `id` FROM %s WHERE `name`=? AND `parent_id`=?", SQL_FOLDER_TABLE);
if(prepare_a_mysql_statement(sdata, &stmt, s) == ERR) goto ENDE;
@ -138,6 +138,10 @@ unsigned long get_folder_id(struct session_data *sdata, char *foldername){
bind[0].is_null = 0;
len[0] = strlen(foldername); bind[0].length = &len[0];
bind[1].buffer_type = MYSQL_TYPE_LONG;
bind[1].buffer = (char *)&parent_id;
bind[1].is_null = 0;
bind[1].length = 0;
if(mysql_stmt_bind_param(stmt, bind)){
goto CLOSE;
@ -175,7 +179,7 @@ ENDE:
}
unsigned long add_new_folder(struct session_data *sdata, char *foldername){
unsigned long add_new_folder(struct session_data *sdata, char *foldername, int parent_id){
unsigned long id=0;
char s[SMALLBUFSIZE];
MYSQL_STMT *stmt;
@ -183,7 +187,7 @@ unsigned long add_new_folder(struct session_data *sdata, char *foldername){
unsigned long len[2];
snprintf(s, sizeof(s)-1, "INSERT INTO %s (`name`) VALUES(?)", SQL_FOLDER_TABLE);
snprintf(s, sizeof(s)-1, "INSERT INTO %s (`name`, `parent_id`) VALUES(?,?)", SQL_FOLDER_TABLE);
if(prepare_a_mysql_statement(sdata, &stmt, s) == ERR) goto ENDE;
@ -194,6 +198,11 @@ unsigned long add_new_folder(struct session_data *sdata, char *foldername){
bind[0].is_null = 0;
len[0] = strlen(foldername); bind[0].length = &len[0];
bind[1].buffer_type = MYSQL_TYPE_LONG;
bind[1].buffer = (char *)&parent_id;
bind[1].is_null = 0;
bind[1].length = 0;
if(mysql_stmt_bind_param(stmt, bind)){
syslog(LOG_PRIORITY, "%s: %s.mysql_stmt_bind_param() error: %s", sdata->ttmpfile, SQL_FOLDER_TABLE, mysql_stmt_error(stmt));
goto CLOSE;

View File

@ -289,6 +289,7 @@ void initialise_configuration(){
free_rule(data.retention_rules);
data.folder = 0;
data.recursive_folder_names = 0;
data.archiving_rules = NULL;
data.retention_rules = NULL;
@ -343,6 +344,7 @@ int main(int argc, char **argv){
(void) openlog(PROGNAME, LOG_PID, LOG_MAIL);
data.folder = 0;
data.recursive_folder_names = 0;
data.archiving_rules = NULL;
data.retention_rules = NULL;

View File

@ -52,8 +52,8 @@ int retrieve_email_from_archive(struct session_data *sdata, FILE *dest, struct _
int prepare_a_mysql_statement(struct session_data *sdata, MYSQL_STMT **stmt, char *s);
int import_message(char *filename, struct session_data *sdata, struct __data *data, struct __config *cfg);
unsigned long get_folder_id(struct session_data *sdata, char *foldername);
unsigned long add_new_folder(struct session_data *sdata, char *foldername);
unsigned long get_folder_id(struct session_data *sdata, char *foldername, int parent_id);
unsigned long add_new_folder(struct session_data *sdata, char *foldername, int parent_id);
int store_index_data(struct session_data *sdata, struct _state *state, struct __data *data, uint64 id, struct __config *cfg);

View File

@ -88,7 +88,8 @@ int import_from_mailbox(char *mailbox, struct session_data *sdata, struct __data
int import_mbox_from_dir(char *directory, struct session_data *sdata, struct __data *data, int *tot_msgs, struct __config *cfg){
DIR *dir;
struct dirent *de;
int rc=ERR, ret=OK;
int rc=ERR, ret=OK, i=0;
unsigned long folder;
char fname[SMALLBUFSIZE];
struct stat st;
@ -106,16 +107,35 @@ int import_mbox_from_dir(char *directory, struct session_data *sdata, struct __d
if(stat(fname, &st) == 0){
if(S_ISDIR(st.st_mode)){
folder = data->folder;
rc = import_mbox_from_dir(fname, sdata, data, tot_msgs, cfg);
data->folder = folder;
if(rc == ERR) ret = ERR;
}
else {
if(S_ISREG(st.st_mode)){
if(i == 0 && data->recursive_folder_names == 1){
folder = get_folder_id(sdata, fname, data->folder);
if(folder == 0){
folder = add_new_folder(sdata, fname, data->folder);
if(folder == 0){
printf("error: cannot get/add folder '%s' to parent id: %d\n", fname, data->folder);
return ERR;
}
else {
data->folder = folder;
}
}
}
rc = import_from_mailbox(fname, sdata, data, cfg);
if(rc == OK) (*tot_msgs)++;
else ret = ERR;
i++;
}
else {
printf("%s is not a file\n", fname);
@ -137,8 +157,9 @@ int import_mbox_from_dir(char *directory, struct session_data *sdata, struct __d
int import_from_maildir(char *directory, struct session_data *sdata, struct __data *data, int *tot_msgs, struct __config *cfg){
DIR *dir;
struct dirent *de;
int rc=ERR, ret=OK;
char fname[SMALLBUFSIZE];
int rc=ERR, ret=OK, i=0;
unsigned long folder;
char *p, fname[SMALLBUFSIZE];
struct stat st;
dir = opendir(directory);
@ -155,16 +176,43 @@ int import_from_maildir(char *directory, struct session_data *sdata, struct __da
if(stat(fname, &st) == 0){
if(S_ISDIR(st.st_mode)){
folder = data->folder;
rc = import_from_maildir(fname, sdata, data, tot_msgs, cfg);
data->folder = folder;
if(rc == ERR) ret = ERR;
}
else {
if(S_ISREG(st.st_mode)){
if(i == 0 && data->recursive_folder_names == 1){
p = strrchr(directory, '/');
if(p) p++;
else {
printf("invalid directory name: '%s'\n", directory);
return ERR;
}
folder = get_folder_id(sdata, p, data->folder);
if(folder == 0){
folder = add_new_folder(sdata, p, data->folder);
if(folder == 0){
printf("error: cannot get/add folder '%s' to parent id: %d\n", p, data->folder);
return ERR;
}
else {
data->folder = folder;
}
}
}
rc = import_message(fname, sdata, data, cfg);
if(rc == OK) (*tot_msgs)++;
else ret = ERR;
i++;
if(quiet == 0) printf("processed: %7d\r", *tot_msgs); fflush(stdout);
}
else {
@ -261,6 +309,7 @@ int main(int argc, char **argv){
for(i=0; i<MBOX_ARGS; i++) mbox[i] = NULL;
data.folder = 0;
data.recursive_folder_names = 0;
data.archiving_rules = NULL;
data.retention_rules = NULL;
@ -279,15 +328,16 @@ int main(int argc, char **argv){
{"password", required_argument, 0, 'p' },
{"skiplist", required_argument, 0, 'x' },
{"folder", required_argument, 0, 'F' },
{"recursive", required_argument, 0, 'R' },
{"help", no_argument, 0, 'h' },
{0,0,0,0}
};
int option_index = 0;
c = getopt_long(argc, argv, "c:m:M:e:d:i:u:p:x:F:h?", long_options, &option_index);
c = getopt_long(argc, argv, "c:m:M:e:d:i:u:p:x:F:Rh?", long_options, &option_index);
#else
c = getopt(argc, argv, "c:m:M:e:d:i:u:p:x:F:h?");
c = getopt(argc, argv, "c:m:M:e:d:i:u:p:x:F:Rh?");
#endif
if(c == -1) break;
@ -339,6 +389,10 @@ int main(int argc, char **argv){
folder = optarg;
break;
case 'R' :
data.recursive_folder_names = 1;
break;
case 'h' :
case '?' :
usage();
@ -376,10 +430,10 @@ int main(int argc, char **argv){
(void) openlog("pilerimport", LOG_PID, LOG_MAIL);
if(folder){
data.folder = get_folder_id(&sdata, folder);
data.folder = get_folder_id(&sdata, folder, 0);
if(data.folder == 0){
data.folder = add_new_folder(&sdata, folder);
data.folder = add_new_folder(&sdata, folder, 0);
}
if(data.folder == 0){

View File

@ -170,11 +170,12 @@ int main(int argc, char **argv){
}
data.folder = 0;
data.recursive_folder_names = 0;
data.archiving_rules = NULL;
data.retention_rules = NULL;
if(folder){
data.folder = get_folder_id(&sdata, folder);
data.folder = get_folder_id(&sdata, folder, 0);
if(data.folder == 0){
printf("error: could not get folder id for '%s'\n", folder);
return 0;

View File

@ -54,6 +54,7 @@ int main(int argc, char **argv){
printf("build: %d\n", get_build());
data.folder = 0;
data.recursive_folder_names = 0;
data.archiving_rules = NULL;
data.retention_rules = NULL;