2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-02-08 02:38:21 +01:00
|
|
|
#include <assert.h>
|
2023-04-08 11:39:28 +03:00
|
|
|
#include <bits/utimens.h>
|
2019-06-07 11:49:03 +02:00
|
|
|
#include <errno.h>
|
2021-05-14 20:34:31 +02:00
|
|
|
#include <fcntl.h>
|
2019-02-08 02:38:21 +01:00
|
|
|
#include <stdio.h>
|
2020-01-06 11:05:59 +01:00
|
|
|
#include <string.h>
|
2019-06-07 11:49:03 +02:00
|
|
|
#include <sys/stat.h>
|
2021-02-05 12:16:30 +01:00
|
|
|
#include <syscall.h>
|
2020-07-16 16:01:03 -06:00
|
|
|
#include <unistd.h>
|
2018-11-06 15:45:16 +01:00
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
2021-12-21 16:00:08 -08:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/umask.html
|
2018-11-06 15:45:16 +01:00
|
|
|
mode_t umask(mode_t mask)
|
|
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
return syscall(SC_umask, mask);
|
2018-11-06 15:45:16 +01:00
|
|
|
}
|
|
|
|
|
|
2021-12-21 16:00:08 -08:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/mkdir.html
|
2022-04-01 20:58:27 +03:00
|
|
|
int mkdir(char const* pathname, mode_t mode)
|
2022-10-01 11:36:24 +00:00
|
|
|
{
|
|
|
|
|
return mkdirat(AT_FDCWD, pathname, mode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/mkdirat.html
|
|
|
|
|
int mkdirat(int dirfd, char const* pathname, mode_t mode)
|
2018-11-18 14:57:41 +01:00
|
|
|
{
|
2020-01-06 11:05:59 +01:00
|
|
|
if (!pathname) {
|
|
|
|
|
errno = EFAULT;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2022-10-01 11:36:24 +00:00
|
|
|
int rc = syscall(SC_mkdir, dirfd, pathname, strlen(pathname), mode);
|
2018-11-18 14:57:41 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 16:00:08 -08:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/chmod.html
|
2022-04-01 20:58:27 +03:00
|
|
|
int chmod(char const* pathname, mode_t mode)
|
2022-01-11 16:51:34 +01:00
|
|
|
{
|
|
|
|
|
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)
|
2019-01-29 04:55:08 +01:00
|
|
|
{
|
2020-01-06 11:05:59 +01:00
|
|
|
if (!pathname) {
|
|
|
|
|
errno = EFAULT;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2022-01-11 16:51:34 +01:00
|
|
|
|
|
|
|
|
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, ¶ms);
|
2019-01-29 04:55:08 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 16:00:08 -08:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fchmod.html
|
2019-02-08 02:38:21 +01:00
|
|
|
int fchmod(int fd, mode_t mode)
|
|
|
|
|
{
|
2019-03-01 10:39:19 +01:00
|
|
|
int rc = syscall(SC_fchmod, fd, mode);
|
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2019-02-08 02:38:21 +01:00
|
|
|
}
|
2020-07-16 16:01:03 -06:00
|
|
|
|
2021-12-21 16:00:08 -08:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/mkfifo.html
|
2022-04-01 20:58:27 +03:00
|
|
|
int mkfifo(char const* pathname, mode_t mode)
|
2020-07-16 16:01:03 -06:00
|
|
|
{
|
|
|
|
|
return mknod(pathname, mode | S_IFIFO, 0);
|
|
|
|
|
}
|
2020-08-11 18:56:41 +02:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
static int do_stat(int dirfd, char const* path, struct stat* statbuf, bool follow_symlinks)
|
2020-08-11 18:56:41 +02:00
|
|
|
{
|
|
|
|
|
if (!path) {
|
|
|
|
|
errno = EFAULT;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2021-09-15 23:45:30 -07:00
|
|
|
Syscall::SC_stat_params params { { path, strlen(path) }, statbuf, dirfd, follow_symlinks };
|
2020-08-11 18:56:41 +02:00
|
|
|
int rc = syscall(SC_stat, ¶ms);
|
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 16:00:08 -08:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/lstat.html
|
2022-04-01 20:58:27 +03:00
|
|
|
int lstat(char const* path, struct stat* statbuf)
|
2020-08-11 18:56:41 +02:00
|
|
|
{
|
2021-05-14 20:34:31 +02:00
|
|
|
return do_stat(AT_FDCWD, path, statbuf, false);
|
2020-08-11 18:56:41 +02:00
|
|
|
}
|
|
|
|
|
|
2021-12-21 16:00:08 -08:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/stat.html
|
2022-04-01 20:58:27 +03:00
|
|
|
int stat(char const* path, struct stat* statbuf)
|
2020-08-11 18:56:41 +02:00
|
|
|
{
|
2021-05-14 20:34:31 +02:00
|
|
|
return do_stat(AT_FDCWD, path, statbuf, true);
|
2020-08-11 18:56:41 +02:00
|
|
|
}
|
|
|
|
|
|
2021-12-21 16:00:08 -08:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fstat.html
|
2020-08-11 18:56:41 +02:00
|
|
|
int fstat(int fd, struct stat* statbuf)
|
|
|
|
|
{
|
|
|
|
|
int rc = syscall(SC_fstat, fd, statbuf);
|
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
|
}
|
2021-05-14 20:34:31 +02:00
|
|
|
|
2021-12-21 16:00:08 -08:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fstatat.html
|
2022-04-01 20:58:27 +03:00
|
|
|
int fstatat(int fd, char const* path, struct stat* statbuf, int flags)
|
2021-05-14 20:34:31 +02:00
|
|
|
{
|
|
|
|
|
return do_stat(fd, path, statbuf, !(flags & AT_SYMLINK_NOFOLLOW));
|
|
|
|
|
}
|
2022-05-03 14:52:08 -05:00
|
|
|
|
|
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/futimens.html
|
|
|
|
|
int futimens(int fd, struct timespec const times[2])
|
|
|
|
|
{
|
2023-04-08 11:39:28 +03:00
|
|
|
return __utimens(fd, nullptr, times, 0);
|
2022-05-03 14:52:08 -05:00
|
|
|
}
|
2018-11-06 15:45:16 +01:00
|
|
|
}
|