X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=lib-lib%2Ffile.c;h=66a2028b90986f49566a8675566009ea6a00ce0d;hb=330047119d7dc2674918d12253fc24146aefc8d3;hp=793ac103638376c04d0ba9f74947c0d484de6a32;hpb=ae0ce4dfcafa0c3820f107c5bfa8bd06e5272b57;p=apps%2Fmadmutt.git diff --git a/lib-lib/file.c b/lib-lib/file.c index 793ac10..66a2028 100644 --- a/lib-lib/file.c +++ b/lib-lib/file.c @@ -26,20 +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/debug.h" -#include "../lib/str.h" +#include "lib-lib.h" #ifndef O_NOFOLLOW # define O_NOFOLLOW 0 @@ -77,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; } @@ -107,11 +93,11 @@ int safe_symlink(const char *oldpath, const char *newpath) return -1; len = m_strcat(abs_oldpath, sizeof(abs_oldpath), "/"); - if (len >= sizeof(abs_oldpath)) + if (len >= ssizeof(abs_oldpath)) return -1; len += m_strcpy(abs_oldpath + len, sizeof(abs_oldpath) - len, oldpath); - if (len >= sizeof(abs_oldpath)) + if (len >= ssizeof(abs_oldpath)) return -1; if (symlink (abs_oldpath, newpath) < 0) @@ -209,10 +195,10 @@ void mutt_unlink(const char *s) char buf[BUFSIZ]; unlink(s); - p_clear(buf, sizeof(buf)); + p_clear(buf, countof(buf)); while (sb.st_size > 0) { - fwrite(buf, 1, MIN(sizeof(buf), sb.st_size), f); - sb.st_size -= MIN(sizeof(buf), sb.st_size); + fwrite(buf, 1, MIN(ssizeof(buf), sb.st_size), f); + sb.st_size -= MIN(ssizeof(buf), sb.st_size); } fclose (f); } @@ -256,14 +242,51 @@ 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, * and the next line is read too. */ -char *mutt_read_line(char *s, size_t *size, FILE * fp, int *line) +char *mutt_read_line(char *s, ssize_t *size, FILE * fp, int *line) { - size_t offset = 0; + ssize_t offset = 0; char *ch; if (!s) { @@ -310,9 +333,9 @@ char *mutt_read_line(char *s, size_t *size, FILE * fp, int *line) int mutt_copy_stream(FILE *fin, FILE *fout) { char buf[BUFSIZ]; - ssize_t l; + size_t l; - while ((l = fread(buf, 1, sizeof (buf), fin)) > 0) { + while ((l = fread(buf, 1, sizeof(buf), fin)) > 0) { if (fwrite(buf, 1, l, fout) != l) return -1; } @@ -320,17 +343,16 @@ int mutt_copy_stream(FILE *fin, FILE *fout) return 0; } -int mutt_copy_bytes(FILE *in, FILE *out, size_t size) +int mutt_copy_bytes(FILE *in, FILE *out, ssize_t size) { char buf[BUFSIZ]; while (size > 0) { - ssize_t chunk = MIN(size, sizeof(buf)); + size_t chunk = MIN(size, ssizeof(buf)); 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; @@ -354,12 +376,16 @@ const char *mutt_basename(const char *f) char * mutt_concat_path(char *d, ssize_t n, const char *dir, const char *fname) { - const char *fmt = "%s/%s"; + int pos; + + pos = m_strcpy(d, n, dir); + if (pos >= n - 1) + return d; - if (!*fname || (*dir && dir[m_strlen(dir) - 1] == '/')) - fmt = "%s%s"; + if (pos && d[pos - 1] != '/') + d[pos++] = '/'; - snprintf(d, n, fmt, dir, fname); + m_strcpy(d + pos, n - pos, fname); return d; } @@ -385,7 +411,7 @@ void mutt_sanitize_filename(char *s, short slash) /* FIXME: API is very wrong */ ssize_t mutt_quote_filename(char *d, ssize_t l, const char *s) { - size_t i, j = 0; + ssize_t i, j = 0; if (!s) { *d = '\0';