tweaks: avoid an unneeded, extra stat() for temporary files

This commit is contained in:
Benno Schulenberg
2019-06-12 10:48:03 +02:00
parent e8e30e5197
commit 43caf7bb7b

View File

@@ -1554,10 +1554,9 @@ bool write_file(const char *name, FILE *f_open, bool tmp,
mode_t original_umask = 0; mode_t original_umask = 0;
/* Our umask, from when nano started. */ /* Our umask, from when nano started. */
#ifndef NANO_TINY #ifndef NANO_TINY
bool realexists; bool realexists = FALSE;
/* The result of stat(). TRUE if the file exists, FALSE /* The result of stat(). TRUE if the file exists, FALSE otherwise.
* otherwise. If name is a link that points nowhere, realexists * If name is a link that points nowhere, realexists is FALSE. */
* is FALSE. */
#endif #endif
struct stat st; struct stat st;
/* The status fields filled in by stat(). */ /* The status fields filled in by stat(). */
@@ -1591,13 +1590,14 @@ bool write_file(const char *name, FILE *f_open, bool tmp,
#ifndef NANO_TINY #ifndef NANO_TINY
/* Check whether the file (at the end of the symlink) exists. */ /* Check whether the file (at the end of the symlink) exists. */
realexists = (stat(realname, &st) != -1); if (!tmp)
realexists = (stat(realname, &st) != -1);
/* If we haven't stat()d this file before (say, the user just /* If we haven't stat()d this file before (say, the user just
* specified it interactively), stat and save the value now, * specified it interactively), stat and save the value now,
* or else we will chase null pointers when we do modtime checks, * or else we will chase null pointers when we do modtime checks,
* preserve file times, and so on, during backup. */ * preserve file times, and so on, during backup. */
if (openfile->current_stat == NULL && !tmp && realexists) if (openfile->current_stat == NULL && realexists)
stat_with_alloc(realname, &openfile->current_stat); stat_with_alloc(realname, &openfile->current_stat);
/* We backup only if the backup toggle is set, the file isn't /* We backup only if the backup toggle is set, the file isn't
@@ -1605,7 +1605,7 @@ bool write_file(const char *name, FILE *f_open, bool tmp,
* aren't appending, prepending, or writing a selection, we backup * aren't appending, prepending, or writing a selection, we backup
* only if the file has not been modified by someone else since nano * only if the file has not been modified by someone else since nano
* opened it. */ * opened it. */
if (ISSET(BACKUP_FILE) && !tmp && realexists && openfile->current_stat && if (ISSET(BACKUP_FILE) && realexists && openfile->current_stat &&
(method != OVERWRITE || openfile->mark || (method != OVERWRITE || openfile->mark ||
openfile->current_stat->st_mtime == st.st_mtime)) { openfile->current_stat->st_mtime == st.st_mtime)) {
static struct timespec filetime[2]; static struct timespec filetime[2];