1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:37:35 +00:00

Kernel+LibC+LibCore+UE: Implement fchmodat(2)

This function is an extended version of `chmod(2)` that lets one control
whether to dereference symlinks, and specify a file descriptor to a
directory that will be used as the base for relative paths.
This commit is contained in:
Daniel Bertalan 2022-01-11 16:51:34 +01:00 committed by Andreas Kling
parent 5f71925aa4
commit 182016d7c0
10 changed files with 64 additions and 14 deletions

View file

@ -34,12 +34,30 @@ int mkdir(const char* pathname, mode_t mode)
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/chmod.html
int chmod(const char* pathname, mode_t mode)
{
return fchmodat(AT_FDCWD, pathname, mode, 0);
}
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fchmodat.html
int fchmodat(int dirfd, char const* pathname, mode_t mode, int flags)
{
if (!pathname) {
errno = EFAULT;
return -1;
}
int rc = syscall(SC_chmod, pathname, strlen(pathname), mode);
if (flags & ~AT_SYMLINK_NOFOLLOW) {
errno = EINVAL;
return -1;
}
Syscall::SC_chmod_params params {
dirfd,
{ pathname, strlen(pathname) },
mode,
!(flags & AT_SYMLINK_NOFOLLOW)
};
int rc = syscall(SC_chmod, &params);
__RETURN_WITH_ERRNO(rc, rc, -1);
}

View file

@ -15,6 +15,7 @@ __BEGIN_DECLS
mode_t umask(mode_t);
int chmod(const char* pathname, mode_t);
int fchmodat(int fd, char const* path, mode_t mode, int flag);
int fchmod(int fd, mode_t);
int mkdir(const char* pathname, mode_t);
int mkfifo(const char* pathname, mode_t);

View file

@ -386,7 +386,13 @@ ErrorOr<void> chmod(StringView pathname, mode_t mode)
return Error::from_syscall("chmod"sv, -EFAULT);
#ifdef __serenity__
int rc = syscall(SC_chmod, pathname.characters_without_null_termination(), pathname.length(), mode);
Syscall::SC_chmod_params params {
AT_FDCWD,
{ pathname.characters_without_null_termination(), pathname.length() },
mode,
true
};
int rc = syscall(SC_chmod, &params);
HANDLE_SYSCALL_RETURN_VALUE("chmod"sv, rc, {});
#else
String path = pathname;