Kernel+LibC: Add fstatat

The function fstatat can do the same thing as the stat and lstat
functions. However, it can be passed the file descriptor of a directory
which will be used when as the starting point for relative paths. This
is contrary to stat and lstat which use the current working directory as
the starting for relative paths.
This commit is contained in:
Mart G 2021-05-14 20:34:31 +02:00 committed by Andreas Kling
parent 875116c3e5
commit e7310ba45a
Notes: sideshowbarker 2024-07-18 18:08:25 +09:00
6 changed files with 29 additions and 5 deletions

View file

@ -422,6 +422,7 @@ struct SC_waitid_params {
};
struct SC_stat_params {
int dirfd;
StringArgument path;
struct stat* statbuf;
int follow_symlinks;

View file

@ -5,6 +5,7 @@
*/
#include <AK/NonnullRefPtrVector.h>
#include <Kernel/FileSystem/Custody.h>
#include <Kernel/FileSystem/FileDescription.h>
#include <Kernel/FileSystem/VirtualFileSystem.h>
#include <Kernel/Process.h>
@ -33,7 +34,20 @@ KResultOr<int> Process::sys$stat(Userspace<const Syscall::SC_stat_params*> user_
auto path = get_syscall_path_argument(params.path);
if (path.is_error())
return path.error();
auto metadata_or_error = VFS::the().lookup_metadata(path.value(), current_directory(), params.follow_symlinks ? 0 : O_NOFOLLOW_NOERROR);
RefPtr<Custody> base;
if (params.dirfd == AT_FDCWD) {
base = current_directory();
} else {
auto base_description = file_description(params.dirfd);
if (!base_description)
return EBADF;
if (!base_description->is_directory())
return ENOTDIR;
if (!base_description->custody())
return EINVAL;
base = base_description->custody();
}
auto metadata_or_error = VFS::the().lookup_metadata(path.value(), *base, params.follow_symlinks ? 0 : O_NOFOLLOW_NOERROR);
if (metadata_or_error.is_error())
return metadata_or_error.error();
stat statbuf;

View file

@ -680,6 +680,7 @@ struct rtentry {
#define RTF_GATEWAY 0x2 /* the route is a gateway and not an end host */
#define AT_FDCWD -100
#define AT_SYMLINK_NOFOLLOW 0x100
#define PURGE_ALL_VOLATILE 0x1
#define PURGE_ALL_CLEAN_INODE 0x2

View file

@ -40,6 +40,7 @@ __BEGIN_DECLS
int creat(const char* path, mode_t);
int open(const char* path, int options, ...);
#define AT_FDCWD -100
#define AT_SYMLINK_NOFOLLOW 0x100
int openat(int dirfd, const char* path, int options, ...);
int fcntl(int fd, int cmd, ...);

View file

@ -6,6 +6,7 @@
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
@ -50,25 +51,25 @@ int mkfifo(const char* pathname, mode_t mode)
return mknod(pathname, mode | S_IFIFO, 0);
}
static int do_stat(const char* path, struct stat* statbuf, bool follow_symlinks)
static int do_stat(int dirfd, const char* path, struct stat* statbuf, bool follow_symlinks)
{
if (!path) {
errno = EFAULT;
return -1;
}
Syscall::SC_stat_params params { { path, strlen(path) }, statbuf, follow_symlinks };
Syscall::SC_stat_params params { dirfd, { path, strlen(path) }, statbuf, follow_symlinks };
int rc = syscall(SC_stat, &params);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
int lstat(const char* path, struct stat* statbuf)
{
return do_stat(path, statbuf, false);
return do_stat(AT_FDCWD, path, statbuf, false);
}
int stat(const char* path, struct stat* statbuf)
{
return do_stat(path, statbuf, true);
return do_stat(AT_FDCWD, path, statbuf, true);
}
int fstat(int fd, struct stat* statbuf)
@ -76,4 +77,9 @@ int fstat(int fd, struct stat* statbuf)
int rc = syscall(SC_fstat, fd, statbuf);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
int fstatat(int fd, const char* path, struct stat* statbuf, int flags)
{
return do_stat(fd, path, statbuf, !(flags & AT_SYMLINK_NOFOLLOW));
}
}

View file

@ -77,5 +77,6 @@ int mkfifo(const char* pathname, mode_t);
int fstat(int fd, struct stat* statbuf);
int lstat(const char* path, struct stat* statbuf);
int stat(const char* path, struct stat* statbuf);
int fstatat(int fd, const char* path, struct stat* statbuf, int flags);
__END_DECLS