From 747ce3b5dad2a8bdd4f763d7827f307aadbed2b6 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Wed, 20 May 2020 16:53:19 +0200 Subject: [PATCH] tweaks: don't bother statting the lock file before unlinking it If unlinking would fail because the parent directory is unwritable, then a check in has_valid_path() would have prevented do_lockfile() from being called at all. --- src/files.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/files.c b/src/files.c index 0fa620f5..af7b2e5e 100644 --- a/src/files.c +++ b/src/files.c @@ -128,7 +128,6 @@ bool write_lockfile(const char *lockfilename, const char *filename, bool modifie uid_t myuid = geteuid(); struct passwd *mypwuid = getpwuid(myuid); char myhostname[32]; - struct stat fileinfo; int fd; FILE *filestream; char *lockdata; @@ -146,12 +145,11 @@ bool write_lockfile(const char *lockfilename, const char *filename, bool modifie } else myhostname[31] = '\0'; - /* If the lockfile exists, try to delete it. */ - if (stat(lockfilename, &fileinfo) != -1) - if (!delete_lockfile(lockfilename)) - return FALSE; + /* First make sure to remove an existing lock file. */ + if (!delete_lockfile(lockfilename)) + return FALSE; - /* Create the lockfile -- do not accept an existing one. */ + /* Create the lock file -- do not accept an existing one. */ fd = open(lockfilename, O_WRONLY|O_CREAT|O_EXCL, RW_FOR_ALL); if (fd > 0)