Added conditional directory creation

Signed-off-by: Tyrolyean <tyrolyean@tyrolyean.net>
This commit is contained in:
Tyrolyean 2020-04-30 22:58:07 +02:00
parent 8c5cbe50e9
commit fa6027c437
No known key found for this signature in database
GPG key ID: 81EC9BAC5E9667C6
4 changed files with 25 additions and 16 deletions

View file

@ -32,6 +32,6 @@ void free_submails(struct email_t* mail);
char* attach_files(char* message, size_t len); char* attach_files(char* message, size_t len);
int replace_base64_files(struct email_t* mail, const char* dirname); int replace_files(struct email_t* mail, const char* dirname, bool* created);
#endif /* ATTACH_H */ #endif /* ATTACH_H */

View file

@ -297,12 +297,8 @@ char* attach_files(char* message, size_t len){
printf("Storing mail messages into directory [%s]\n", printf("Storing mail messages into directory [%s]\n",
storage_dir); storage_dir);
} }
if(mkdir(storage_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0){ bool dir_cre = false;
perror("Failed to create storage directory!"); if(replace_files(email, storage_dir, &dir_cre) < 0){
goto finish;
}
if(replace_base64_files(email, storage_dir) < 0){
fprintf(stderr, "Failed to store base64 messages!\n"); fprintf(stderr, "Failed to store base64 messages!\n");
goto finish; goto finish;
} }
@ -325,14 +321,17 @@ finish:
/* This function RECURSIVELY replaces ALL base 64 encoded messages above the /* This function RECURSIVELY replaces ALL base 64 encoded messages above the
* threshold set in the config file with links to that file in the HTML format. * threshold set in the config file with links to that file in the HTML format.
* Call this function with the mail ROOT object if you want to replace all * Call this function with the mail ROOT object if you want to replace all
* base64 files, or only one when you want to replace only one. * base64 files, or only one when you want to replace only one. Created is a
* boolean wether the directory to store files has already been created.
*/ */
int replace_base64_files(struct email_t* mail, const char* dirname){ int replace_files(struct email_t* mail, const char* dirname, bool* created){
if(mail->is_multipart){ if(mail->is_multipart){
for(size_t i = 0; i < mail->submes_cnt; i++){ for(size_t i = 0; i < mail->submes_cnt; i++){
if(replace_base64_files(mail->submes[i], dirname) < 0){ if(replace_files(mail->submes[i], dirname, created)
< 0){
return -1; return -1;
} }
} }
@ -346,7 +345,17 @@ int replace_base64_files(struct email_t* mail, const char* dirname){
min_filesize ){ min_filesize ){
return 0; return 0;
} }
if(!*created){
*created = true;
if(mkdir(directory, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
< 0){
perror("Failed to create storage directory!");
return -1;
}
}
if(mail->base64_encoded){ if(mail->base64_encoded){
if(base64_decode_file(dirname, mail) < 0){ if(base64_decode_file(dirname, mail) < 0){
fprintf(stderr, "Failed to decode base64 file\n!"); fprintf(stderr, "Failed to decode base64 file\n!");
@ -362,7 +371,7 @@ int replace_base64_files(struct email_t* mail, const char* dirname){
} }
} }
/* Replace the mail message with some html text TODO */ /* Insert a message telling you what has happened to the attachment */
/* Delete old attachment */ /* Delete old attachment */
if(mail->parent != NULL){ if(mail->parent != NULL){
@ -371,6 +380,10 @@ int replace_base64_files(struct email_t* mail, const char* dirname){
return -1; return -1;
} }
} }
free_submails(mail);
free(mail);
return 0; return 0;
} }

View file

@ -27,5 +27,4 @@ int verbose = false;
char* directory = NULL; char* directory = NULL;
char* url_base = NULL; char* url_base = NULL;
long min_filesize= 1024*512; long min_filesize= 1024*512;

View file

@ -150,9 +150,6 @@ int remove_mail(struct email_t* mail){
propagate_insert_delete(root, root->message+remove_offset, -remove_len); propagate_insert_delete(root, root->message+remove_offset, -remove_len);
free_submails(mail);
free(mail);
return 0; return 0;
} }