From: David Levine Date: Tue, 4 Feb 2014 03:42:44 +0000 (-0600) Subject: Use rename(2), not link(2), in m_mktemps() if mkstemps() is not X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/034abb1cc56e6b4d15282635ea46f276b97680d8?ds=inline;hp=95a485d2b8a2bf28fdb80ca62c12d0541ac0a2fc Use rename(2), not link(2), in m_mktemps() if mkstemps() is not available. --- diff --git a/h/prototypes.h b/h/prototypes.h index fceef407..1ff92f64 100644 --- a/h/prototypes.h +++ b/h/prototypes.h @@ -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 *); diff --git a/sbr/m_mktemp.c b/sbr/m_mktemp.c index 17cf7458..009df0a1 100644 --- a/sbr/m_mktemp.c +++ b/sbr/m_mktemp.c @@ -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 */