mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 09:04:59 +00:00
Kernel+LibC: Implement readlinkat(2)
Co-Authored-By: Daniel Bertalan <dani@danielbertalan.dev>
This commit is contained in:
parent
9850a69cd1
commit
6445a706cf
6 changed files with 16 additions and 6 deletions
|
@ -389,6 +389,7 @@ struct SC_execve_params {
|
|||
struct SC_readlink_params {
|
||||
StringArgument path;
|
||||
MutableBufferArgument<char, size_t> buffer;
|
||||
int dirfd;
|
||||
};
|
||||
|
||||
struct SC_link_params {
|
||||
|
|
|
@ -17,7 +17,7 @@ ErrorOr<FlatPtr> Process::sys$readlink(Userspace<Syscall::SC_readlink_params con
|
|||
auto params = TRY(copy_typed_from_user(user_params));
|
||||
|
||||
auto path = TRY(get_syscall_path_argument(params.path));
|
||||
auto description = TRY(VirtualFileSystem::the().open(credentials(), path->view(), O_RDONLY | O_NOFOLLOW_NOERROR, 0, current_directory()));
|
||||
auto description = TRY(VirtualFileSystem::the().open(credentials(), path->view(), O_RDONLY | O_NOFOLLOW_NOERROR, 0, TRY(custody_for_dirfd(params.dirfd))));
|
||||
|
||||
if (!description->metadata().is_symlink())
|
||||
return EINVAL;
|
||||
|
|
|
@ -105,7 +105,8 @@ int serenity_readlink(char const* path, size_t path_length, char* buffer, size_t
|
|||
{
|
||||
Syscall::SC_readlink_params small_params {
|
||||
{ path, path_length },
|
||||
{ buffer, buffer_size }
|
||||
{ buffer, buffer_size },
|
||||
AT_FDCWD
|
||||
};
|
||||
int rc = syscall(SC_readlink, &small_params);
|
||||
__RETURN_WITH_ERRNO(rc, rc, -1);
|
||||
|
|
|
@ -630,10 +630,16 @@ int sethostname(char const* hostname, ssize_t size)
|
|||
__RETURN_WITH_ERRNO(rc, rc, -1);
|
||||
}
|
||||
|
||||
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/readlink.html
|
||||
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/readlinkat.html
|
||||
ssize_t readlink(char const* path, char* buffer, size_t size)
|
||||
{
|
||||
Syscall::SC_readlink_params params { { path, strlen(path) }, { buffer, size } };
|
||||
return readlinkat(AT_FDCWD, path, buffer, size);
|
||||
}
|
||||
|
||||
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/readlinkat.html
|
||||
ssize_t readlinkat(int dirfd, char const* path, char* buffer, size_t size)
|
||||
{
|
||||
Syscall::SC_readlink_params params { { path, strlen(path) }, { buffer, size }, dirfd };
|
||||
int rc = syscall(SC_readlink, ¶ms);
|
||||
// Return the number of bytes placed in the buffer, not the full path size.
|
||||
__RETURN_WITH_ERRNO(rc, min((size_t)rc, size), -1);
|
||||
|
|
|
@ -88,6 +88,7 @@ int usleep(useconds_t);
|
|||
int gethostname(char*, size_t);
|
||||
int sethostname(char const*, ssize_t);
|
||||
ssize_t readlink(char const* path, char* buffer, size_t);
|
||||
ssize_t readlinkat(int dirfd, char const* path, char* buffer, size_t);
|
||||
char* ttyname(int fd);
|
||||
int ttyname_r(int fd, char* buffer, size_t);
|
||||
off_t lseek(int fd, off_t, int whence);
|
||||
|
|
|
@ -1386,8 +1386,9 @@ ErrorOr<DeprecatedString> readlink(StringView pathname)
|
|||
char data[PATH_MAX];
|
||||
#ifdef AK_OS_SERENITY
|
||||
Syscall::SC_readlink_params small_params {
|
||||
{ pathname.characters_without_null_termination(), pathname.length() },
|
||||
{ data, sizeof(data) }
|
||||
.path = { pathname.characters_without_null_termination(), pathname.length() },
|
||||
.buffer = { data, sizeof(data) },
|
||||
.dirfd = AT_FDCWD,
|
||||
};
|
||||
int rc = syscall(SC_readlink, &small_params);
|
||||
HANDLE_SYSCALL_RETURN_VALUE("readlink", rc, DeprecatedString(data, rc));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue