From d05d938e738fa2b0de0b5e1412384a0b843b4863 Mon Sep 17 00:00:00 2001 From: Liav A Date: Sat, 8 Apr 2023 11:39:28 +0300 Subject: [PATCH] LibC: Properly implement the futimens function Use the new futimens syscall to ensure futimens can actually work. This change for example allows a user to run "touch non-existing-file" without getting any error, as expected. --- Userland/Libraries/LibC/bits/utimens.h | 18 +++++++++++++++++ Userland/Libraries/LibC/fcntl.cpp | 28 ++++++++++++++++++++------ Userland/Libraries/LibC/stat.cpp | 3 ++- 3 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 Userland/Libraries/LibC/bits/utimens.h diff --git a/Userland/Libraries/LibC/bits/utimens.h b/Userland/Libraries/LibC/bits/utimens.h new file mode 100644 index 0000000000..649acedda5 --- /dev/null +++ b/Userland/Libraries/LibC/bits/utimens.h @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2018-2022, Andreas Kling + * Copyright (c) 2021, sin-ack + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include + +__BEGIN_DECLS + +int __utimens(int fd, char const* path, struct timespec const times[2], int flag); + +__END_DECLS diff --git a/Userland/Libraries/LibC/fcntl.cpp b/Userland/Libraries/LibC/fcntl.cpp index 8a8ab0f11f..4857ded2d5 100644 --- a/Userland/Libraries/LibC/fcntl.cpp +++ b/Userland/Libraries/LibC/fcntl.cpp @@ -6,6 +6,7 @@ */ #include +#include #include #include #include @@ -127,11 +128,18 @@ int utimensat(int dirfd, char const* path, struct timespec const times[2], int f errno = EFAULT; return -1; } + return __utimens(dirfd, path, times, flag); +} - size_t path_length = strlen(path); - if (path_length > INT32_MAX) { - errno = EINVAL; - return -1; +int __utimens(int fd, char const* path, struct timespec const times[2], int flag) +{ + size_t path_length = 0; + if (path) { + path_length = strlen(path); + if (path_length > INT32_MAX) { + errno = EINVAL; + return -1; + } } // POSIX allows AT_SYMLINK_NOFOLLOW flag or no flags. @@ -161,8 +169,16 @@ int utimensat(int dirfd, char const* path, struct timespec const times[2], int f } } - Syscall::SC_utimensat_params params { dirfd, { path, path_length }, times, flag }; - int rc = syscall(SC_utimensat, ¶ms); + int rc = 0; + if (path) { + // NOTE: fd is treated as dirfd for this syscall. + Syscall::SC_utimensat_params params { fd, { path, path_length }, times, flag }; + rc = syscall(SC_utimensat, ¶ms); + } else { + Syscall::SC_futimens_params params { fd, times }; + rc = syscall(SC_futimens, ¶ms); + } + __RETURN_WITH_ERRNO(rc, rc, -1); } } diff --git a/Userland/Libraries/LibC/stat.cpp b/Userland/Libraries/LibC/stat.cpp index 10c95b1d26..c6139a602a 100644 --- a/Userland/Libraries/LibC/stat.cpp +++ b/Userland/Libraries/LibC/stat.cpp @@ -5,6 +5,7 @@ */ #include +#include #include #include #include @@ -119,6 +120,6 @@ int fstatat(int fd, char const* path, struct stat* statbuf, int flags) // https://pubs.opengroup.org/onlinepubs/9699919799/functions/futimens.html int futimens(int fd, struct timespec const times[2]) { - return utimensat(fd, "", times, 0); + return __utimens(fd, nullptr, times, 0); } }