]> diplodocus.org Git - nmh/commitdiff
Use rename(2), not link(2), in m_mktemps() if mkstemps() is not
authorDavid Levine <levinedl@acm.org>
Tue, 4 Feb 2014 03:42:44 +0000 (21:42 -0600)
committerDavid Levine <levinedl@acm.org>
Tue, 4 Feb 2014 03:44:35 +0000 (21:44 -0600)
available.

h/prototypes.h
sbr/m_mktemp.c

index fceef407bb09ee93a4cf569a61a2bf3fa5d1e4d8..1ff92f64c5cf0c3c33e832ab5b3de6ad62caf34c 100644 (file)
@@ -191,7 +191,7 @@ int m_putenv (char *, char *);
 int m_rand (unsigned char *, size_t);
 char *m_mktemp(const char *, int *, FILE **);
 char *m_mktemp2(const char *, const char *, int *, FILE **);
-char *m_mktemps(const char *pfx, const char *, int *, FILE **);
+char *m_mktemps(const char *pfx, const char *suffix, int *, FILE **);
 char *get_temp_dir();
 void m_unknown(m_getfld_state_t *, FILE *);
 int makedir (char *);
index 17cf74580265b4fb92210bce3d495c8ae12b4702..009df0a195cf3c1709742c768e28798c63e8cc53 100644 (file)
@@ -181,31 +181,15 @@ m_mktemps(
     fd = mkstemp(tmpfil);
     {
         char *oldfilename = tmpfil;
-
         tmpfil = concat(oldfilename, suffix, NULL);
 
-        /* link(2) requires that the new path not exist.  And if we
-           have to resort to rename(2), at least try to remove a file
-           that would be in the way. */
-        if (unlink(tmpfil) != 0  &&  errno != ENOENT) {
-            advise("unlink", "Failed to unlink \"%s\"", tmpfil);
-            (void) unlink(oldfilename);
-            free(oldfilename);
-            free(tmpfil);
-            return NULL;
-        }
-
-        /* link() doesn't always work, such as on Windows FAT
-           filesystems.  If it fails, try rename(). */
-        if (link(oldfilename, tmpfil) != 0  &&
-            rename(oldfilename, tmpfil) != 0) {
+        if (rename(oldfilename, tmpfil) != 0) {
             (void) unlink(oldfilename);
             free(oldfilename);
             free(tmpfil);
             return NULL;
         }
 
-        (void) unlink(oldfilename);
         free(oldfilename);
     }
 #endif /* ! HAVE_MKSTEMPS */