1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 12:37:44 +00:00

Kernel+LibC: Implement futimens(3)

Implement futimes() in terms of utimensat(). Now, utimensat() strays
from POSIX compliance because it also accepts a combination of a file
descriptor of a regular file and an empty path. utimensat() then uses
this file descriptor instead of the path to update the last access
and/or modification time of a file. That being said, its prior behavior
remains intact.

With the new behavior of utimensat(), `path` must point to a valid
string; given a null pointer instead of an empty string, utimensat()
sets `errno` to `EFAULT` and returns a failure.
This commit is contained in:
Ariel Don 2022-05-03 14:52:08 -05:00 committed by Andreas Kling
parent 9a6bd85924
commit 8a854ba309
3 changed files with 36 additions and 12 deletions

View file

@ -20,6 +20,9 @@ ErrorOr<FlatPtr> Process::sys$utimensat(Userspace<Syscall::SC_utimensat_params c
auto params = TRY(copy_typed_from_user(user_params)); auto params = TRY(copy_typed_from_user(user_params));
auto now = kgettimeofday().to_truncated_seconds(); auto now = kgettimeofday().to_truncated_seconds();
int dirfd = params.dirfd;
int follow_symlink = params.flag & AT_SYMLINK_NOFOLLOW ? O_NOFOLLOW_NOERROR : 0;
timespec times[2]; timespec times[2];
if (params.times) { if (params.times) {
TRY(copy_from_user(times, params.times, sizeof(times))); TRY(copy_from_user(times, params.times, sizeof(times)));
@ -36,24 +39,38 @@ ErrorOr<FlatPtr> Process::sys$utimensat(Userspace<Syscall::SC_utimensat_params c
times[1].tv_nsec = UTIME_NOW; times[1].tv_nsec = UTIME_NOW;
} }
int dirfd = params.dirfd; OwnPtr<KString> path;
auto path = TRY(get_syscall_path_argument(params.path)); RefPtr<OpenFileDescription> description;
RefPtr<Custody> base; RefPtr<Custody> base;
auto path_or_error = get_syscall_path_argument(params.path);
if (path_or_error.is_error()) {
// If the path is empty ("") but not a nullptr, attempt to get a path
// from the file descriptor. This allows futimens() to be implemented
// in terms of utimensat().
if (params.path.characters && params.path.length == 0) {
description = TRY(open_file_description(dirfd));
path = TRY(description->original_absolute_path());
base = description->custody();
} else {
return path_or_error.release_error();
}
} else {
path = path_or_error.release_value();
if (dirfd == AT_FDCWD) { if (dirfd == AT_FDCWD) {
base = current_directory(); base = current_directory();
} else { } else {
auto base_description = TRY(open_file_description(dirfd)); description = TRY(open_file_description(dirfd));
if (!KLexicalPath::is_absolute(path->view()) && !base_description->is_directory()) if (!KLexicalPath::is_absolute(path->view()) && !description->is_directory())
return ENOTDIR; return ENOTDIR;
if (!base_description->custody()) if (!description->custody())
return EINVAL; return EINVAL;
base = base_description->custody(); base = description->custody();
}
} }
auto& atime = times[0]; auto& atime = times[0];
auto& mtime = times[1]; auto& mtime = times[1];
int follow_symlink = params.flag & AT_SYMLINK_NOFOLLOW ? O_NOFOLLOW_NOERROR : 0;
TRY(VirtualFileSystem::the().utimensat(path->view(), *base, atime, mtime, follow_symlink)); TRY(VirtualFileSystem::the().utimensat(path->view(), *base, atime, mtime, follow_symlink));
return 0; return 0;
} }

View file

@ -109,4 +109,10 @@ int fstatat(int fd, char const* path, struct stat* statbuf, int flags)
{ {
return do_stat(fd, path, statbuf, !(flags & AT_SYMLINK_NOFOLLOW)); return do_stat(fd, path, statbuf, !(flags & AT_SYMLINK_NOFOLLOW));
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/futimens.html
int futimens(int fd, struct timespec const times[2])
{
return utimensat(fd, "", times, 0);
}
} }

View file

@ -23,5 +23,6 @@ int fstat(int fd, struct stat* statbuf);
int lstat(char const* path, struct stat* statbuf); int lstat(char const* path, struct stat* statbuf);
int stat(char const* path, struct stat* statbuf); int stat(char const* path, struct stat* statbuf);
int fstatat(int fd, char const* path, struct stat* statbuf, int flags); int fstatat(int fd, char const* path, struct stat* statbuf, int flags);
int futimens(int fd, struct timespec const times[2]);
__END_DECLS __END_DECLS