touch(): behavioral change, call utimensat() first, then mknod()

Optimize for the case the file exists already.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2021-03-26 12:35:41 +01:00
parent 8ce46eb439
commit 7ad11a31df
+3 -3
View File
@@ -117,9 +117,9 @@ int whichp (const char *cmd);
#include <sys/time.h> /* 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;