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