diff options
author | Kenneth Myhra <kennethmyhra@gmail.com> | 2022-03-23 16:36:03 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-03-24 11:57:51 +0100 |
commit | 4a57be824cc6d4fed95f182431ba807b2327395e (patch) | |
tree | 6f0185eb39bbf6cd199d30decd63bdbae655c3bb /Userland/Libraries/LibCore/File.cpp | |
parent | 10093a67732431924fc76145443ffac157b50e01 (diff) | |
download | serenity-4a57be824cc6d4fed95f182431ba807b2327395e.zip |
Userland+Tests: Convert File::read_link() from String to ErrorOr<String>
This converts the return value of File::read_link() from String to
ErrorOr<String>.
The rest of the change is to support the potential of an Error being
returned and subsequent release of the value when no Error is returned.
Unfortunately at this stage none of the places affected can utililize
our TRY() macro.
Diffstat (limited to 'Userland/Libraries/LibCore/File.cpp')
-rw-r--r-- | Userland/Libraries/LibCore/File.cpp | 20 |
1 files changed, 10 insertions, 10 deletions
diff --git a/Userland/Libraries/LibCore/File.cpp b/Userland/Libraries/LibCore/File.cpp index 9ae04bc028..811a50dd44 100644 --- a/Userland/Libraries/LibCore/File.cpp +++ b/Userland/Libraries/LibCore/File.cpp @@ -247,28 +247,28 @@ String File::absolute_path(String const& path) #ifdef __serenity__ -String File::read_link(String const& link_path) +ErrorOr<String> File::read_link(String const& link_path) { // First, try using a 64-byte buffer, that ought to be enough for anybody. char small_buffer[64]; int rc = serenity_readlink(link_path.characters(), link_path.length(), small_buffer, sizeof(small_buffer)); if (rc < 0) - return {}; + return Error::from_errno(errno); size_t size = rc; // 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 // was enough to read the whole link. if (size <= sizeof(small_buffer)) - return { small_buffer, size }; + return String { small_buffer, size }; // Nope, but at least now we know the right size. char* large_buffer_ptr; auto large_buffer = StringImpl::create_uninitialized(size, large_buffer_ptr); rc = serenity_readlink(link_path.characters(), link_path.length(), large_buffer_ptr, size); if (rc < 0) - return {}; + return Error::from_errno(errno); size_t new_size = rc; if (new_size == size) @@ -277,31 +277,31 @@ String File::read_link(String const& link_path) // If we're here, the symlink has changed while we were looking at it. // If it became shorter, our buffer is valid, we just have to trim it a bit. if (new_size < size) - return { large_buffer_ptr, new_size }; + return String { large_buffer_ptr, new_size }; // Otherwise, here's not much we can do, unless we want to loop endlessly // in this case. Let's leave it up to the caller whether to loop. errno = EAGAIN; - return {}; + return Error::from_errno(errno); } #else // This is a sad version for other systems. It has to always make a copy of the // link path, and to always make two syscalls to get the right size first. -String File::read_link(String const& link_path) +ErrorOr<String> File::read_link(String const& link_path) { struct stat statbuf = {}; int rc = lstat(link_path.characters(), &statbuf); if (rc < 0) - return {}; + return Error::from_errno(errno); char* buffer_ptr; auto buffer = StringImpl::create_uninitialized(statbuf.st_size, buffer_ptr); if (readlink(link_path.characters(), buffer_ptr, statbuf.st_size) < 0) - return {}; + return Error::from_errno(errno); // (See above.) if (rc == statbuf.st_size) return { *buffer }; - return { buffer_ptr, (size_t)rc }; + return String { buffer_ptr, (size_t)rc }; } #endif |