]> granicus.if.org Git - mutt/commitdiff
Fix a $TMPDIR race condition.
authorThomas Roessler <roessler@does-not-exist.org>
Sun, 28 Feb 1999 08:05:54 +0000 (08:05 +0000)
committerThomas Roessler <roessler@does-not-exist.org>
Sun, 28 Feb 1999 08:05:54 +0000 (08:05 +0000)
attach.c
lib.c
mutt.h
rfc1524.c

index 81b9c561f44daf6504b95b597cc09c56786543d1..f4a0d3b0573f3f18c22f03c5043061856000945c 100644 (file)
--- a/attach.c
+++ b/attach.c
@@ -712,9 +712,11 @@ int mutt_save_attachment (FILE *fp, BODY *m, char *path, int flags, HEADER *hdr)
       
       memset (&s, 0, sizeof (s));
       if (flags == M_SAVE_APPEND)
-       s.fpout = safe_fopen (path, "a");
-      else
+       s.fpout = fopen (path, "a");
+      else if (flags == M_SAVE_OVERWRITE)
        s.fpout = fopen (path, "w");
+      else
+       s.fpout = safe_fopen (path, "w");
       if (s.fpout == NULL)
       {
        mutt_perror ("fopen");
@@ -776,9 +778,12 @@ int mutt_decode_save_attachment (FILE *fp, BODY *m, char *path,
   s.flags = (displaying ? M_DISPLAY : 0);
 
   if (flags == M_SAVE_APPEND)
-    s.fpout = safe_fopen (path, "a");
-  else
+    s.fpout = fopen (path, "a");
+  else if (flags == M_SAVE_OVERWRITE)
     s.fpout = fopen (path, "w");
+  else
+    s.fpout = safe_fopen (path, "w");
+
   if (s.fpout == NULL)
   {
     perror ("fopen");
diff --git a/lib.c b/lib.c
index cd21ba76be8bb49652d655ce8d2970e225eb333e..047a5e45fb62a0482ec784e26ed0a741333c7cf5 100644 (file)
--- a/lib.c
+++ b/lib.c
@@ -807,8 +807,10 @@ int mutt_check_overwrite (const char *attname, const char *path,
 
       case 2: /* append */
         *append = M_SAVE_APPEND;
+        break;
       case 1: /* overwrite */
-       ;
+        *append = M_SAVE_OVERWRITE;
+        break;
     }
   }
   return 0;
diff --git a/mutt.h b/mutt.h
index 46b6667a1978815276047802227d26ecad66ec55..a4bd81276a97283a0479b4e9a3a4bae33740aed8 100644 (file)
--- a/mutt.h
+++ b/mutt.h
@@ -229,7 +229,8 @@ enum
   M_NEW_SOCKET,
 
   /* Options for mutt_save_attachment */
-  M_SAVE_APPEND
+  M_SAVE_APPEND,
+  M_SAVE_OVERWRITE
 };
 
 /* possible arguments to set_quadoption() */
index 4914cb59898093506df57a73ae9bea89e74f751b..6f803dbca032b49ec09426cd4e68244c8ad137e9 100644 (file)
--- a/rfc1524.c
+++ b/rfc1524.c
 #include "mutt.h"
 #include "rfc1524.h"
 
-#include <ctype.h>
+#include <string.h>
 #include <stdlib.h>
-#include <unistd.h>
+#include <ctype.h>
+
+#include <sys/stat.h>
 #include <sys/wait.h>
-#include <string.h>
+#include <errno.h>
+#include <unistd.h>
 
 /* The command semantics include the following:
  * %s is the filename that contains the mail body data
@@ -429,6 +432,7 @@ void mutt_adv_mktemp (char *s, size_t l)
   char tmp[_POSIX_PATH_MAX];
   char *period;
   size_t sl;
+  struct stat sb;
   
   strfcpy (buf, NONULL (Tempdir), sizeof (buf));
   mutt_expand_path (buf, sizeof (buf));
@@ -441,7 +445,7 @@ void mutt_adv_mktemp (char *s, size_t l)
   {
     strfcpy (tmp, s, sizeof (tmp));
     snprintf (s, l, "%s/%s", buf, tmp);
-    if (access (s, F_OK) != 0)
+    if (lstat (s, &sb) == -1 && errno == ENOENT)
       return;
     if ((period = strrchr (tmp, '.')) != NULL)
       *period = 0;
@@ -594,6 +598,11 @@ int rfc1524_expand_filename (char *nametemplate,
  * 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;