1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 03:27:45 +00:00

LibCore: Use serenity_readlink() instead of making syscalls directly

This commit is contained in:
Andreas Kling 2021-02-02 19:49:24 +01:00
parent 1658df9b6e
commit d57b4128a1

View file

@ -26,6 +26,7 @@
#ifdef __serenity__ #ifdef __serenity__
# include <Kernel/API/Syscall.h> # include <Kernel/API/Syscall.h>
# include <serenity.h>
#endif #endif
#include <AK/ScopeGuard.h> #include <AK/ScopeGuard.h>
#include <LibCore/File.h> #include <LibCore/File.h>
@ -172,15 +173,11 @@ String File::read_link(const StringView& link_path)
{ {
// First, try using a 64-byte buffer, that ought to be enough for anybody. // First, try using a 64-byte buffer, that ought to be enough for anybody.
char small_buffer[64]; char small_buffer[64];
Syscall::SC_readlink_params small_params {
{ link_path.characters_without_null_termination(), link_path.length() }, int rc = serenity_readlink(link_path.characters_without_null_termination(), link_path.length(), small_buffer, sizeof(small_buffer));
{ small_buffer, sizeof(small_buffer) } if (rc < 0)
};
int rc = syscall(SC_readlink, &small_params);
if (rc < 0) {
errno = -rc;
return {}; return {};
}
size_t size = rc; size_t size = rc;
// If the call was successful, the syscall (unlike the LibC wrapper) // If the call was successful, the syscall (unlike the LibC wrapper)
// returns the full size of the link. Let's see if our small buffer // returns the full size of the link. Let's see if our small buffer
@ -190,15 +187,11 @@ String File::read_link(const StringView& link_path)
// Nope, but at least now we know the right size. // Nope, but at least now we know the right size.
char* large_buffer_ptr; char* large_buffer_ptr;
auto large_buffer = StringImpl::create_uninitialized(size, large_buffer_ptr); auto large_buffer = StringImpl::create_uninitialized(size, large_buffer_ptr);
Syscall::SC_readlink_params large_params {
{ link_path.characters_without_null_termination(), link_path.length() }, rc = serenity_readlink(link_path.characters_without_null_termination(), link_path.length(), large_buffer_ptr, size);
{ large_buffer_ptr, (size_t)size } if (rc < 0)
};
rc = syscall(SC_readlink, &large_params);
if (rc < 0) {
errno = -rc;
return {}; return {};
}
size_t new_size = rc; size_t new_size = rc;
if (new_size == size) if (new_size == size)
return { *large_buffer }; return { *large_buffer };