X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=lib-lib%2Ffile.c;h=978895af0734b9c6f356b1d13ca3c12b6b40f1c0;hb=9faa4e03a4cf3204d1b4a8d622d33dd9c4596ca7;hp=985015f0da26fb904e350b4f4fef0017d8481d8b;hpb=ac813896ca32d850febc2d95065ac4fa040f11f9;p=apps%2Fmadmutt.git diff --git a/lib-lib/file.c b/lib-lib/file.c index 985015f..978895a 100644 --- a/lib-lib/file.c +++ b/lib-lib/file.c @@ -26,18 +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 "debug.h" +#include "lib-lib.h" #ifndef O_NOFOLLOW # define O_NOFOLLOW 0 @@ -75,7 +64,6 @@ int safe_open(const char *path, int flags) if (lstat (path, &osb) < 0 || fstat(fd, &nsb) < 0 || compare_stat(&osb, &nsb) == -1) { - debug_print(1, ("%s is a symlink!\n", path)); close(fd); return -1; } @@ -254,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, @@ -328,7 +353,6 @@ int mutt_copy_bytes(FILE *in, FILE *out, ssize_t size) if ((chunk = fread(buf, 1, chunk, in)) < 1) break; if (fwrite(buf, 1, chunk, out) != chunk) { - debug_print(1, ("fwrite() returned short byte count\n")); return -1; } size -= chunk; @@ -415,3 +439,87 @@ ssize_t mutt_quote_filename(char *d, ssize_t l, const char *s) return j; } + +ssize_t m_file_fmt(char *dst, ssize_t n, const char *fmt, const char *src) +{ + ssize_t pos = 0; + const char *p; + int hadfname = 0; + + for (p = strchr(fmt, '%'); p; p = strchr(fmt, '%')) { + if (p[1] == 's') { + pos += m_strncpy(dst + pos, n - pos, fmt, p - fmt); + pos += m_strcpy(dst + pos, n - pos, src); + fmt = p + 2; + hadfname = 1; + } else { + pos += m_strncpy(dst + pos, n - pos, fmt, p + 1 - fmt); + fmt = p + 1 + (p[1] == '%'); + } + } + pos += m_strcpy(dst + pos, n - pos, fmt); + + if (!hadfname) + pos += snprintf(dst + pos, n - pos, " %s", src); + + return pos; +} + +static ssize_t +m_tempftplize(char *dst, ssize_t dlen, const char *fmt, const char *s) +{ + const char *p; + + while ((p = strchr(fmt, '/'))) { + fmt = p + 1; + } + + if (!*fmt) + return m_strcpy(dst, dlen, s); + + for (p = strchr(fmt, '%'); p; p = strchr(p, '%')) { + if (p[1] == 's') + return m_file_fmt(dst, dlen, fmt, s); + + p += 1 + (p[1] == '%'); + } + + p = strrchr(fmt, '.'); + if (p) { + return snprintf(dst, dlen, "%s%s", s, p); + } else { + return snprintf(dst, dlen, "%s.%s", s, fmt); + } +} + +int m_tempfd(char *dst, ssize_t n, const char *dir, const char *fmt) +{ + char raw[_POSIX_PATH_MAX], tpl[_POSIX_PATH_MAX]; + const char *path = fmt ? tpl : raw; + int fd; + + rand_again: + snprintf(raw, sizeof(raw), "%s/madmutt-%04x-%04x-%08x", + dir, (int)getuid(), (int)getpid(), (int)rand()); + + if (fmt) { + m_tempftplize(tpl, sizeof(tpl), fmt, raw); + } + + fd = open(path, O_CREAT | O_EXCL | O_WRONLY | O_NOFOLLOW, 0600); + + if (fd < 0) { + if (errno == EEXIST) + goto rand_again; + return -1; + } + + m_strcpy(dst, n, path); + return fd; +} + +FILE *m_tempfile(char *dst, ssize_t n, const char *dir, const char *fmt) +{ + int fd = m_tempfd(dst, n, dir, fmt); + return fd < 0 ? NULL : fdopen(fd, "w+"); +}