From: Richard Russon Date: Mon, 4 Mar 2019 15:37:24 +0000 (+0000) Subject: separate enum for mutt_save_attachment() X-Git-Tag: 2019-10-25~338^2~2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9ac841cd03fdeec039f4c356e98bb449920ecbcf;p=neomutt separate enum for mutt_save_attachment() --- diff --git a/mutt.h b/mutt.h index ac1e79055..e3f5ca2ee 100644 --- a/mutt.h +++ b/mutt.h @@ -171,9 +171,6 @@ enum MuttMisc #ifdef USE_NNTP MUTT_NEWSGROUPS, ///< Pattern matches newsgroup #endif - - MUTT_SAVE_APPEND, ///< Append to existing file - mutt_save_attachment() - MUTT_SAVE_OVERWRITE, ///< Overwrite existing file - mutt_save_attachment() }; /* flags for parse_spam_list */ diff --git a/mutt_attach.c b/mutt_attach.c index d86b7244e..abc8cf3af 100644 --- a/mutt_attach.c +++ b/mutt_attach.c @@ -460,7 +460,7 @@ int mutt_view_attachment(FILE *fp, struct Body *a, enum ViewAttachMode mode, { /* recv case: we need to save the attachment to a file */ FREE(&fname); - if (mutt_save_attachment(fp, a, tempfile, 0, NULL) == -1) + if (mutt_save_attachment(fp, a, tempfile, MUTT_SAVE_NO_FLAGS, NULL) == -1) goto return_error; mutt_file_chmod(tempfile, S_IRUSR); } @@ -588,7 +588,7 @@ int mutt_view_attachment(FILE *fp, struct Body *a, enum ViewAttachMode mode, * mutt_decode_attachment() since it assumes the content-encoding has * already been applied */ - if (mutt_save_attachment(fp, a, pagerfile, 0, NULL)) + if (mutt_save_attachment(fp, a, pagerfile, MUTT_SAVE_NO_FLAGS, NULL)) goto return_error; } } @@ -597,7 +597,7 @@ int mutt_view_attachment(FILE *fp, struct Body *a, enum ViewAttachMode mode, /* Use built-in handler */ OptViewAttach = true; /* disable the "use 'v' to view this part" * message in case of error */ - if (mutt_decode_save_attachment(fp, a, pagerfile, MUTT_DISPLAY, 0)) + if (mutt_decode_save_attachment(fp, a, pagerfile, MUTT_DISPLAY, MUTT_SAVE_NO_FLAGS)) { OptViewAttach = false; goto return_error; @@ -758,15 +758,15 @@ bail: /** * save_attachment_open - Open a file to write an attachment to - * @param path Path to file to open - * @param flags Flags, e.g. #MUTT_SAVE_APPEND + * @param path Path to file to open + * @param opt Save option, see #SaveAttach * @retval ptr File handle to attachment file */ -static FILE *save_attachment_open(char *path, int flags) +static FILE *save_attachment_open(char *path, enum SaveAttach opt) { - if (flags == MUTT_SAVE_APPEND) + if (opt == MUTT_SAVE_APPEND) return fopen(path, "a"); - if (flags == MUTT_SAVE_OVERWRITE) + if (opt == MUTT_SAVE_OVERWRITE) return fopen(path, "w"); return mutt_file_fopen(path, "w"); @@ -774,15 +774,16 @@ static FILE *save_attachment_open(char *path, int flags) /** * mutt_save_attachment - Save an attachment - * @param fp Source file stream. Can be NULL - * @param m Email Body - * @param path Where to save the attachment - * @param flags Flags, e.g. #MUTT_SAVE_APPEND - * @param e Current Email. Can be NULL + * @param fp Source file stream. Can be NULL + * @param m Email Body + * @param path Where to save the attachment + * @param opt Save option, see #SaveAttach + * @param e Current Email. Can be NULL * @retval 0 Success * @retval -1 Error */ -int mutt_save_attachment(FILE *fp, struct Body *m, char *path, int flags, struct Email *e) +int mutt_save_attachment(FILE *fp, struct Body *m, char *path, + enum SaveAttach opt, struct Email *e) { if (!m) return -1; @@ -845,7 +846,7 @@ int mutt_save_attachment(FILE *fp, struct Body *m, char *path, int flags, struct struct State s = { 0 }; - s.fp_out = save_attachment_open(path, flags); + s.fp_out = save_attachment_open(path, opt); if (!s.fp_out) { mutt_perror("fopen"); @@ -875,7 +876,7 @@ int mutt_save_attachment(FILE *fp, struct Body *m, char *path, int flags, struct return -1; } - FILE *fp_new = save_attachment_open(path, flags); + FILE *fp_new = save_attachment_open(path, opt); if (!fp_new) { mutt_perror("fopen"); @@ -907,11 +908,12 @@ int mutt_save_attachment(FILE *fp, struct Body *m, char *path, int flags, struct * @param m Attachment * @param path Path to save the Attachment to * @param displaying Flags, e.g. #MUTT_DISPLAY - * @param flags Flags, e.g. #MUTT_SAVE_APPEND + * @param opt Save option, see #SaveAttach * @retval 0 Success * @retval -1 Error */ -int mutt_decode_save_attachment(FILE *fp, struct Body *m, char *path, int displaying, int flags) +int mutt_decode_save_attachment(FILE *fp, struct Body *m, char *path, + int displaying, enum SaveAttach opt) { struct State s = { 0 }; unsigned int saved_encoding = 0; @@ -921,9 +923,9 @@ int mutt_decode_save_attachment(FILE *fp, struct Body *m, char *path, int displa s.flags = displaying; - if (flags == MUTT_SAVE_APPEND) + if (opt == MUTT_SAVE_APPEND) s.fp_out = fopen(path, "a"); - else if (flags == MUTT_SAVE_OVERWRITE) + else if (opt == MUTT_SAVE_OVERWRITE) s.fp_out = fopen(path, "w"); else s.fp_out = mutt_file_fopen(path, "w"); @@ -1047,7 +1049,7 @@ int mutt_print_attachment(FILE *fp, struct Body *a) } /* in recv mode, save file to newfile first */ - if (fp && (mutt_save_attachment(fp, a, newfile, 0, NULL) != 0)) + if (fp && (mutt_save_attachment(fp, a, newfile, MUTT_SAVE_NO_FLAGS, NULL) != 0)) return 0; mutt_str_strfcpy(cmd, entry->printcommand, sizeof(cmd)); @@ -1114,7 +1116,7 @@ int mutt_print_attachment(FILE *fp, struct Body *a) fp_out = NULL; mutt_mktemp(newfile, sizeof(newfile)); - if (mutt_decode_save_attachment(fp, a, newfile, MUTT_PRINTING, 0) == 0) + if (mutt_decode_save_attachment(fp, a, newfile, MUTT_PRINTING, MUTT_SAVE_NO_FLAGS) == 0) { mutt_debug(LL_DEBUG2, "successfully decoded %s type attachment to %s\n", type, newfile); diff --git a/mutt_attach.h b/mutt_attach.h index 13b244aa6..6e8001aa6 100644 --- a/mutt_attach.h +++ b/mutt_attach.h @@ -43,6 +43,19 @@ enum ViewAttachMode MUTT_VA_AS_TEXT, ///< Force viewing as text }; +/** + * enum SaveAttach - Options for saving attachments + * + * @sa mutt_save_attachment(), mutt_decode_save_attachment(), + * save_attachment_open(), mutt_check_overwrite() + */ +enum SaveAttach +{ + MUTT_SAVE_NO_FLAGS = 0, ///< No flags set + MUTT_SAVE_APPEND, ///< Append to existing file + MUTT_SAVE_OVERWRITE, ///< Overwrite existing file +}; + int attach_tag(struct Menu *menu, int sel, int act); int mutt_attach_display_loop(struct Menu *menu, int op, struct Email *e, struct AttachCtx *actx, bool recv); @@ -58,12 +71,12 @@ int mutt_view_attachment(FILE *fp, struct Body *a, enum ViewAttachMode mode, str void mutt_check_lookup_list(struct Body *b, char *type, size_t len); int mutt_compose_attachment(struct Body *a); -int mutt_decode_save_attachment(FILE *fp, struct Body *m, char *path, int displaying, int flags); +int mutt_decode_save_attachment(FILE *fp, struct Body *m, char *path, int displaying, enum SaveAttach opt); int mutt_edit_attachment(struct Body *a); int mutt_get_tmp_attachment(struct Body *a); int mutt_pipe_attachment(FILE *fp, struct Body *b, const char *path, char *outfile); int mutt_print_attachment(FILE *fp, struct Body *a); -int mutt_save_attachment(FILE *fp, struct Body *m, char *path, int flags, struct Email *e); +int mutt_save_attachment(FILE *fp, struct Body *m, char *path, enum SaveAttach opt, struct Email *e); /* small helper functions to handle temporary attachment files */ void mutt_add_temp_attachment(char *filename); diff --git a/mutt_body.c b/mutt_body.c index 788a97528..a8fedc4c1 100644 --- a/mutt_body.c +++ b/mutt_body.c @@ -67,7 +67,7 @@ int mutt_body_copy(FILE *fp, struct Body **tgt, struct Body *src) } mutt_adv_mktemp(tmp, sizeof(tmp)); - if (mutt_save_attachment(fp, src, tmp, 0, NULL) == -1) + if (mutt_save_attachment(fp, src, tmp, MUTT_SAVE_NO_FLAGS, NULL) == -1) return -1; *tgt = mutt_body_new(); diff --git a/muttlib.c b/muttlib.c index e06c13f20..bcec16cef 100644 --- a/muttlib.c +++ b/muttlib.c @@ -675,14 +675,14 @@ void mutt_pretty_mailbox(char *buf, size_t buflen) * @param[in] path Path to save the file * @param[out] fname Buffer for filename * @param[out] flen Length of buffer - * @param[out] append Flags set to #MUTT_SAVE_APPEND or #MUTT_SAVE_OVERWRITE + * @param[out] opt Save option, see #SaveAttach * @param[out] directory Directory to save under (OPTIONAL) * @retval 0 Success * @retval -1 Abort * @retval 1 Error */ int mutt_check_overwrite(const char *attname, const char *path, char *fname, - size_t flen, int *append, char **directory) + size_t flen, enum SaveAttach *opt, char **directory) { struct stat st; @@ -736,7 +736,7 @@ int mutt_check_overwrite(const char *attname, const char *path, char *fname, mutt_path_concat(fname, path, tmp, flen); } - if (*append == 0 && access(fname, F_OK) == 0) + if ((*opt == MUTT_SAVE_NO_FLAGS) && access(fname, F_OK) == 0) { switch ( mutt_multi_choice(_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), @@ -749,10 +749,10 @@ int mutt_check_overwrite(const char *attname, const char *path, char *fname, return 1; case 2: /* append */ - *append = MUTT_SAVE_APPEND; + *opt = MUTT_SAVE_APPEND; break; case 1: /* overwrite */ - *append = MUTT_SAVE_OVERWRITE; + *opt = MUTT_SAVE_OVERWRITE; break; } } diff --git a/muttlib.h b/muttlib.h index 79ac11788..e062330fc 100644 --- a/muttlib.h +++ b/muttlib.h @@ -29,6 +29,7 @@ #include #include "mutt.h" #include "format_flags.h" +#include "mutt_attach.h" struct Address; struct Body; @@ -44,7 +45,7 @@ extern struct Regex *C_GecosMask; void mutt_adv_mktemp(char *s, size_t l); void mutt_buffer_adv_mktemp (struct Buffer *buf); void mutt_buffer_mktemp_full(struct Buffer *buf, const char *prefix, const char *suffix, const char *src, int line); -int mutt_check_overwrite(const char *attname, const char *path, char *fname, size_t flen, int *append, char **directory); +int mutt_check_overwrite(const char *attname, const char *path, char *fname, size_t flen, enum SaveAttach *opt, char **directory); void mutt_encode_path(char *dest, size_t dlen, const char *src); void mutt_expando_format(char *buf, size_t buflen, size_t col, int cols, const char *src, format_t *callback, unsigned long data, MuttFormatFlags flags); char * mutt_expand_path(char *s, size_t slen); diff --git a/ncrypt/crypt_gpgme.c b/ncrypt/crypt_gpgme.c index 1f5adaa6d..12dd4ee25 100644 --- a/ncrypt/crypt_gpgme.c +++ b/ncrypt/crypt_gpgme.c @@ -2614,7 +2614,7 @@ static int pgp_check_traditional_one_body(FILE *fp, struct Body *b) return 0; mutt_mktemp(tempfile, sizeof(tempfile)); - if (mutt_decode_save_attachment(fp, b, tempfile, 0, 0) != 0) + if (mutt_decode_save_attachment(fp, b, tempfile, 0, MUTT_SAVE_NO_FLAGS) != 0) { unlink(tempfile); return 0; diff --git a/ncrypt/pgp.c b/ncrypt/pgp.c index 4da4f0a93..831f56618 100644 --- a/ncrypt/pgp.c +++ b/ncrypt/pgp.c @@ -799,7 +799,7 @@ static int pgp_check_traditional_one_body(FILE *fp, struct Body *b) return 0; mutt_mktemp(tempfile, sizeof(tempfile)); - if (mutt_decode_save_attachment(fp, b, tempfile, 0, 0) != 0) + if (mutt_decode_save_attachment(fp, b, tempfile, 0, MUTT_SAVE_NO_FLAGS) != 0) { unlink(tempfile); return 0; diff --git a/recvattach.c b/recvattach.c index 820de466e..a4db10ace 100644 --- a/recvattach.c +++ b/recvattach.c @@ -494,7 +494,7 @@ static int query_save_attachment(FILE *fp, struct Body *body, struct Email *e, c { char *prompt = NULL; char buf[PATH_MAX], tfile[PATH_MAX]; - int append = 0; + enum SaveAttach opt = MUTT_SAVE_NO_FLAGS; int rc; if (body->filename) @@ -549,7 +549,7 @@ static int query_save_attachment(FILE *fp, struct Body *body, struct Email *e, c } else { - rc = mutt_check_overwrite(body->filename, buf, tfile, sizeof(tfile), &append, directory); + rc = mutt_check_overwrite(body->filename, buf, tfile, sizeof(tfile), &opt, directory); if (rc == -1) return -1; else if (rc == 1) @@ -560,7 +560,7 @@ static int query_save_attachment(FILE *fp, struct Body *body, struct Email *e, c } mutt_message(_("Saving...")); - if (mutt_save_attachment(fp, body, tfile, append, (e || !is_message) ? e : body->email) == 0) + if (mutt_save_attachment(fp, body, tfile, opt, (e || !is_message) ? e : body->email) == 0) { mutt_message(_("Attachment saved")); return 0; @@ -607,7 +607,7 @@ void mutt_save_attachment_list(struct AttachCtx *actx, FILE *fp, bool tag, { if (!buf[0]) { - int append = 0; + enum SaveAttach opt = MUTT_SAVE_NO_FLAGS; mutt_str_strfcpy(buf, mutt_path_basename(NONULL(top->filename)), sizeof(buf)); prepend_savedir(buf, sizeof(buf)); @@ -618,9 +618,9 @@ void mutt_save_attachment_list(struct AttachCtx *actx, FILE *fp, bool tag, return; } mutt_expand_path(buf, sizeof(buf)); - if (mutt_check_overwrite(top->filename, buf, tfile, sizeof(tfile), &append, NULL)) + if (mutt_check_overwrite(top->filename, buf, tfile, sizeof(tfile), &opt, NULL)) return; - rc = mutt_save_attachment(fp, top, tfile, append, e); + rc = mutt_save_attachment(fp, top, tfile, opt, e); if ((rc == 0) && C_AttachSep && (fp_out = fopen(tfile, "a"))) { fprintf(fp_out, "%s", C_AttachSep); @@ -900,7 +900,8 @@ static void print_attachment_list(struct AttachCtx *actx, FILE *fp, bool tag, FILE *fp_in = NULL; mutt_mktemp(newfile, sizeof(newfile)); - if (mutt_decode_save_attachment(fp, top, newfile, MUTT_PRINTING, 0) == 0) + if (mutt_decode_save_attachment(fp, top, newfile, MUTT_PRINTING, + MUTT_SAVE_NO_FLAGS) == 0) { if (!state->fp_out) {