summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Schumacher <timschumi@gmx.de>2023-02-07 12:01:31 +0100
committerLinus Groh <mail@linusgroh.de>2023-02-08 18:50:31 +0000
commit23e10a30ad332b7b6f1294205198a68af0084b0b (patch)
treedf5308d6e630618fbdeb3c34f84f8513bf065c90
parentccc871e6088c85e985ffd8ef53086df09f943de5 (diff)
downloadserenity-23e10a30ad332b7b6f1294205198a68af0084b0b.zip
Kernel: Modernize Error handling when serializing directory entries
-rw-r--r--Kernel/FileSystem/OpenFileDescription.cpp36
1 files changed, 14 insertions, 22 deletions
diff --git a/Kernel/FileSystem/OpenFileDescription.cpp b/Kernel/FileSystem/OpenFileDescription.cpp
index 360270fe5a..2a412e4dc3 100644
--- a/Kernel/FileSystem/OpenFileDescription.cpp
+++ b/Kernel/FileSystem/OpenFileDescription.cpp
@@ -220,43 +220,35 @@ ErrorOr<size_t> OpenFileDescription::get_dir_entries(UserOrKernelBuffer& output_
return EIO;
size_t remaining = size;
- ErrorOr<void> error;
u8 stack_buffer[PAGE_SIZE];
Bytes temp_buffer(stack_buffer, sizeof(stack_buffer));
DeprecatedOutputMemoryStream stream { temp_buffer };
- auto flush_stream_to_output_buffer = [&error, &stream, &remaining, &output_buffer]() -> bool {
- if (error.is_error())
- return false;
+ auto flush_stream_to_output_buffer = [&stream, &remaining, &output_buffer]() -> ErrorOr<void> {
if (stream.size() == 0)
- return true;
- if (remaining < stream.size()) {
- error = EINVAL;
- return false;
- }
- if (auto result = output_buffer.write(stream.bytes()); result.is_error()) {
- error = result.release_error();
- return false;
- }
+ return {};
+
+ if (remaining < stream.size())
+ return Error::from_errno(EINVAL);
+
+ TRY(output_buffer.write(stream.bytes()));
output_buffer = output_buffer.offset(stream.size());
remaining -= stream.size();
stream.reset();
- return true;
+ return {};
};
- ErrorOr<void> result = VirtualFileSystem::the().traverse_directory_inode(*m_inode, [&flush_stream_to_output_buffer, &error, &stream, this](auto& entry) -> ErrorOr<void> {
+ ErrorOr<void> result = VirtualFileSystem::the().traverse_directory_inode(*m_inode, [&flush_stream_to_output_buffer, &stream, this](auto& entry) -> ErrorOr<void> {
size_t serialized_size = sizeof(ino_t) + sizeof(u8) + sizeof(size_t) + sizeof(char) * entry.name.length();
- if (serialized_size > stream.remaining()) {
- if (!flush_stream_to_output_buffer())
- return error;
- }
+ if (serialized_size > stream.remaining())
+ TRY(flush_stream_to_output_buffer());
+
stream << (u64)entry.inode.index().value();
stream << m_inode->fs().internal_file_type_to_directory_entry_type(entry);
stream << (u32)entry.name.length();
stream << entry.name.bytes();
return {};
});
- flush_stream_to_output_buffer();
if (result.is_error()) {
// We should only return EFAULT when the userspace buffer is too small,
@@ -266,8 +258,8 @@ ErrorOr<size_t> OpenFileDescription::get_dir_entries(UserOrKernelBuffer& output_
return result.release_error();
}
- if (error.is_error())
- return error.release_error();
+ TRY(flush_stream_to_output_buffer());
+
return size - remaining;
}