From 94d628677eb6fd4bfa804b6a55f2648add91423b Mon Sep 17 00:00:00 2001 From: Pietro Cerutti Date: Thu, 19 Oct 2017 15:13:47 +0000 Subject: [PATCH] Move mutt_rename_file to lib/file.[hc] --- compose.c | 1 - lib/file.c | 35 +++++++++++++++++++++++++++++++++++ lib/file.h | 1 + rfc1524.c | 36 ------------------------------------ rfc1524.h | 1 - 5 files changed, 36 insertions(+), 38 deletions(-) diff --git a/compose.c b/compose.c index c33318661..07d5acc51 100644 --- a/compose.c +++ b/compose.c @@ -56,7 +56,6 @@ #include "opcodes.h" #include "options.h" #include "protos.h" -#include "rfc1524.h" #include "rfc822.h" #include "sort.h" #ifdef MIXMASTER diff --git a/lib/file.c b/lib/file.c index 6a6fcf666..6e978e76c 100644 --- a/lib/file.c +++ b/lib/file.c @@ -38,6 +38,7 @@ * | mutt_quote_filename() | Quote a filename to survive the shell's quoting rules * | mutt_read_line() | Read a line from a file * | mutt_rmtree() | Recursively remove a directory + * | mutt_rename_file() | Rename a file * | mutt_regex_sanitize_string() | Escape any regex-magic characters in a string * | mutt_sanitize_filename() | Replace unsafe characters in a filename * | mutt_set_mtime() | Set the modification time of one file from another @@ -1111,3 +1112,37 @@ void mutt_unlink_empty(const char *path) mutt_unlock_file(path, fd); close(fd); } + +/** + * mutt_rename_file - Rename a file + * + * This function returns 0 on successful move, 1 on old file doesn't exist, + * 2 on new file already exists, and 3 on other failure. + * + * note on access(2) use: No dangling symlink problems here due to + * safe_fopen(). + */ + +int mutt_rename_file(char *oldfile, char *newfile) +{ + FILE *ofp = NULL, *nfp = NULL; + + if (access(oldfile, F_OK) != 0) + return 1; + if (access(newfile, F_OK) == 0) + return 2; + ofp = fopen(oldfile, "r"); + if (!ofp) + return 3; + nfp = safe_fopen(newfile, "w"); + if (!nfp) + { + safe_fclose(&ofp); + return 3; + } + mutt_copy_stream(ofp, nfp); + safe_fclose(&nfp); + safe_fclose(&ofp); + mutt_unlink(oldfile); + return 0; +} diff --git a/lib/file.h b/lib/file.h index 38170bedd..34e08e973 100644 --- a/lib/file.h +++ b/lib/file.h @@ -45,6 +45,7 @@ size_t mutt_quote_filename(char *d, size_t l, const char *f); char * mutt_read_line(char *s, size_t *size, FILE *fp, int *line, int flags); int mutt_rmtree(const char *path); int mutt_regex_sanitize_string(char *dest, size_t destlen, const char *src); +int mutt_rename_file(char *oldfile, char *newfile); void mutt_sanitize_filename(char *f, short slash); void mutt_set_mtime(const char *from, const char *to); void mutt_touch_atime(int f); diff --git a/rfc1524.c b/rfc1524.c index 49a6e3dec..4e4dab31e 100644 --- a/rfc1524.c +++ b/rfc1524.c @@ -573,39 +573,3 @@ int rfc1524_expand_filename(char *nametemplate, char *oldfile, char *newfile, si else return 1; } - -/* If rfc1524_expand_command() is used on a recv'd message, then - * the filename doesn't exist yet, but if it's used while sending a message, - * then we need to rename the existing file. - * - * This function returns 0 on successful move, 1 on old file doesn't exist, - * 2 on new file already exists, and 3 on other failure. - */ - -/* note on access(2) use: No dangling symlink problems here due to - * safe_fopen(). - */ - -int mutt_rename_file(char *oldfile, char *newfile) -{ - FILE *ofp = NULL, *nfp = NULL; - - if (access(oldfile, F_OK) != 0) - return 1; - if (access(newfile, F_OK) == 0) - return 2; - ofp = fopen(oldfile, "r"); - if (!ofp) - return 3; - nfp = safe_fopen(newfile, "w"); - if (!nfp) - { - safe_fclose(&ofp); - return 3; - } - mutt_copy_stream(ofp, nfp); - safe_fclose(&nfp); - safe_fclose(&ofp); - mutt_unlink(oldfile); - return 0; -} diff --git a/rfc1524.h b/rfc1524.h index e52770141..16987468d 100644 --- a/rfc1524.h +++ b/rfc1524.h @@ -50,6 +50,5 @@ void rfc1524_free_entry(struct Rfc1524MailcapEntry **entry); int rfc1524_expand_command(struct Body *a, char *filename, char *_type, char *command, int clen); int rfc1524_expand_filename(char *nametemplate, char *oldfile, char *newfile, size_t nflen); int rfc1524_mailcap_lookup(struct Body *a, char *type, struct Rfc1524MailcapEntry *entry, int opt); -int mutt_rename_file(char *oldfile, char *newfile); #endif /* _MUTT_RFC1524_H */ -- 2.40.0