X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=lib-lib%2Ffile.c;h=66a2028b90986f49566a8675566009ea6a00ce0d;hp=b51b1ffb971ba0d63ae53e1e76851f79a91fd890;hb=1ee89902de184a640c171ae3285bff6882a791bd;hpb=23e6291cb5d5b4cd2008403d8b628007fd75ff23 diff --git a/lib-lib/file.c b/lib-lib/file.c index b51b1ff..66a2028 100644 --- a/lib-lib/file.c +++ b/lib-lib/file.c @@ -26,17 +26,7 @@ * please see the file GPL in the top level source directory. */ -#include -#include -#include -#include -#include -#include - -#include "macros.h" -#include "mem.h" -#include "str.h" -#include "file.h" +#include "lib-lib.h" #ifndef O_NOFOLLOW # define O_NOFOLLOW 0 @@ -252,6 +242,43 @@ int safe_fclose(FILE **f) return r; } +/* If rfc1524_expand_command() is used on a recv'd message, then + * the filename doesn't exist yet, but if its 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, *nfp; + + 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) { + fclose (ofp); + return 3; + } + + mutt_copy_stream(ofp, nfp); + fclose(nfp); + fclose(ofp); + mutt_unlink(oldfile); + return 0; +} + /* Read a line from ``fp'' into the dynamically allocated ``s'', * increasing ``s'' if necessary. The ending "\n" or "\r\n" is removed. * If a line ends with "\", this char and the linefeed is removed,