From a21328139cd6377ec85e04b7e02deef83afa1e83 Mon Sep 17 00:00:00 2001 From: SJ Date: Sun, 14 May 2017 17:18:09 +0200 Subject: [PATCH] Resolved issue #791 Change-Id: Ie9477db7e13490e1b19594a8d9f8e56d215c8f1d Signed-off-by: SJ --- src/config.h | 2 +- src/defs.h | 1 + src/import.c | 7 ++++++- src/misc.c | 7 +++++-- src/misc.h | 2 +- src/pilerexport.c | 2 +- src/pilerimport.c | 19 +++++++++++++++---- src/reindex.c | 2 +- src/test.c | 2 +- 9 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/config.h b/src/config.h index 14132fb8..f41ccc20 100644 --- a/src/config.h +++ b/src/config.h @@ -11,7 +11,7 @@ #define VERSION "1.3.0-master" -#define BUILD 976 +#define BUILD 977 #define HOSTID "mailarchiver" diff --git a/src/defs.h b/src/defs.h index fe46acb3..89f89974 100644 --- a/src/defs.h +++ b/src/defs.h @@ -282,6 +282,7 @@ struct memcached_server { struct import { char *extra_recipient; char *move_folder; + char *failed_folder; int status; int total_messages; int processed_messages; diff --git a/src/import.c b/src/import.c index 5b82d4fb..864b3a4d 100644 --- a/src/import.c +++ b/src/import.c @@ -20,7 +20,7 @@ int import_message(char *filename, struct session_data *sdata, struct __data *data, struct __config *cfg){ int rc=ERR; - char *rule; + char *rule, newpath[SMALLBUFSIZE]; struct stat st; struct parser_state state; struct counters counters; @@ -126,6 +126,11 @@ int import_message(char *filename, struct session_data *sdata, struct __data *da break; } + if(rc != OK && data->import->failed_folder){ + snprintf(newpath, sizeof(newpath)-2, "%s/%s", data->import->failed_folder, filename); + rename(filename, newpath); + } + return rc; } diff --git a/src/misc.c b/src/misc.c index 9df14d9f..9bd25d7e 100644 --- a/src/misc.c +++ b/src/misc.c @@ -707,11 +707,14 @@ int create_and_bind(char *listen_addr, int listen_port){ } -int can_i_write_current_directory(){ +int can_i_write_directory(char *dir){ int fd; char filename[SMALLBUFSIZE]; - snprintf(filename, sizeof(filename)-1, "__piler_%d", getpid()); + if(dir) + snprintf(filename, sizeof(filename)-1, "%s/__piler_%d", dir, getpid()); + else + snprintf(filename, sizeof(filename)-1, "__piler_%d", getpid()); fd = open(filename, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR|S_IRGRP); if(fd == -1){ diff --git a/src/misc.h b/src/misc.h index 33db13ca..95450021 100644 --- a/src/misc.h +++ b/src/misc.h @@ -47,7 +47,7 @@ void *get_in_addr(struct sockaddr *sa); int make_socket_non_blocking(int fd); int create_and_bind(char *listen_addr, int listen_port); -int can_i_write_current_directory(); +int can_i_write_directory(char *dir); void move_email(struct smtp_session *session); diff --git a/src/pilerexport.c b/src/pilerexport.c index 5595f4f3..3983ff1e 100644 --- a/src/pilerexport.c +++ b/src/pilerexport.c @@ -560,7 +560,7 @@ int main(int argc, char **argv){ regfree(®exp); - if(!can_i_write_current_directory()) __fatal("cannot write current directory!"); + if(!can_i_write_directory(NULL)) __fatal("cannot write current directory!"); (void) openlog("pilerexport", LOG_PID, LOG_MAIL); diff --git a/src/pilerimport.c b/src/pilerimport.c index 4ee4df51..4a1333f1 100644 --- a/src/pilerimport.c +++ b/src/pilerimport.c @@ -53,6 +53,7 @@ void usage(){ printf(" -R Assign IMAP folder names as Piler folder names\n"); printf(" -b Import only this many emails\n"); printf(" -s Start importing POP3 emails from this position\n"); + printf(" -j Move failed to import emails to this folder\n"); printf(" -a Add recipient to the To:/Cc: list\n"); printf(" -D Dry-run, do not import anything\n"); printf(" -o Only download emails for POP3/IMAP import\n"); @@ -83,7 +84,7 @@ int main(int argc, char **argv){ import.import_job_id = import.total_messages = import.total_size = import.processed_messages = import.batch_processing_limit = 0; import.started = import.updated = import.finished = import.remove_after_import = 0; - import.extra_recipient = import.move_folder = NULL; + import.extra_recipient = import.move_folder = import.failed_folder = NULL; import.start_position = 1; import.download_only = 0; import.timeout = 30; @@ -120,6 +121,7 @@ int main(int argc, char **argv){ {"quiet", no_argument, 0, 'q' }, {"recursive", required_argument, 0, 'R' }, {"remove-after-import",no_argument, 0, 'r' }, + {"failed-folder", required_argument, 0, 'j' }, {"move-folder", required_argument, 0, 'g' }, {"only-download",no_argument, 0, 'o' }, {"gui-import", no_argument, 0, 'G' }, @@ -130,9 +132,9 @@ int main(int argc, char **argv){ int option_index = 0; - c = getopt_long(argc, argv, "c:m:M:e:d:i:K:u:p:P:x:F:f:a:b:t:s:g:GDRroqh?", long_options, &option_index); + c = getopt_long(argc, argv, "c:m:M:e:d:i:K:u:p:P:x:F:f:a:b:t:s:g:j:GDRroqh?", long_options, &option_index); #else - c = getopt(argc, argv, "c:m:M:e:d:i:K:u:p:P:x:F:f:a:b:t:s:g:GDRroqh?"); + c = getopt(argc, argv, "c:m:M:e:d:i:K:u:p:P:x:F:f:a:b:t:s:g:j:GDRroqh?"); #endif if(c == -1) break; @@ -209,6 +211,10 @@ int main(int argc, char **argv){ data.import->move_folder = optarg; break; + case 'j' : + data.import->failed_folder = optarg; + break; + case 'o' : data.import->download_only = 1; dryrun = 1; @@ -262,7 +268,12 @@ int main(int argc, char **argv){ if(!mbox[0] && !mboxdir && !emlfile && !directory && !imapserver && !pop3server && import_from_gui == 0) usage(); - if(!can_i_write_current_directory()) __fatal("cannot write current directory!"); + if(data.import->failed_folder && !can_i_write_directory(data.import->failed_folder)){ + printf("cannot write failed directory '%s'\n", data.import->failed_folder); + return ERR; + } + + if(!can_i_write_directory(NULL)) __fatal("cannot write current directory!"); cfg = read_config(configfile); diff --git a/src/reindex.c b/src/reindex.c index dbf1cc69..e4d966e8 100644 --- a/src/reindex.c +++ b/src/reindex.c @@ -211,7 +211,7 @@ int main(int argc, char **argv){ if(all == 0 && (from_id <= 0 || to_id <= 0) ) usage(); - if(!can_i_write_current_directory()) __fatal("cannot write current directory!"); + if(!can_i_write_directory(NULL)) __fatal("cannot write current directory!"); (void) openlog("reindex", LOG_PID, LOG_MAIL); diff --git a/src/test.c b/src/test.c index 3c2cf1aa..aafeef14 100644 --- a/src/test.c +++ b/src/test.c @@ -32,7 +32,7 @@ int main(int argc, char **argv){ exit(1); } - if(!can_i_write_current_directory()) __fatal("cannot write current directory!"); + if(!can_i_write_directory(NULL)) __fatal("cannot write current directory!"); if(stat(argv[1], &st) != 0){ fprintf(stderr, "%s is not found\n", argv[1]);