From 7ad11a31df62bb66ee33bf05025730ebfdf8396b Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 26 Mar 2021 12:35:41 +0100 Subject: [PATCH] touch(): behavioral change, call utimensat() first, then mknod() Optimize for the case the file exists already. Signed-off-by: Joachim Wiberg --- src/lite.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lite.h b/src/lite.h index 13cb5cc..b64e596 100644 --- a/src/lite.h +++ b/src/lite.h @@ -117,9 +117,9 @@ int whichp (const char *cmd); #include /* utimensat() on *BSD */ static inline int touch(const char *path) { - if (mknod((path), S_IFREG|0644, 0)) { - if (errno == EEXIST) - return utimensat(AT_FDCWD, path, NULL, 0); + if (utimensat(AT_FDCWD, path, NULL, 0)) { + if (errno == ENOENT) + return mknod((path), S_IFREG|0644, 0); return -1; } return 0;