-
Notifications
You must be signed in to change notification settings - Fork 112
[k2] support file_get_contents #1592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,8 @@ | |
| #include <type_traits> | ||
| #include <utility> | ||
|
|
||
| #include <sys/stat.h> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. System headers should be placed in a single block |
||
|
|
||
| #include "runtime-common/core/allocator/script-allocator.h" | ||
| #include "runtime-common/core/class-instance/refcountable-php-classes.h" | ||
| #include "runtime-common/core/runtime-core.h" | ||
|
|
@@ -25,6 +27,7 @@ | |
| #include "runtime-light/coroutine/task.h" | ||
| #include "runtime-light/k2-platform/k2-api.h" | ||
| #include "runtime-light/server/http/http-server-state.h" | ||
| #include "runtime-light/stdlib/diagnostics/logs.h" | ||
| #include "runtime-light/stdlib/output/output-state.h" | ||
| #include "runtime-light/streams/stream.h" | ||
|
|
||
|
|
@@ -146,7 +149,29 @@ inline auto file::pread(std::span<std::byte> buf, uint64_t offset) noexcept -> s | |
| } | ||
|
|
||
| inline auto file::get_contents() noexcept -> std::expected<string, int32_t> { | ||
| return std::unexpected{m_descriptor != k2::INVALID_PLATFORM_DESCRIPTOR ? k2::errno_efault : k2::errno_enodev}; | ||
| struct stat stat_buf {}; | ||
|
|
||
| return k2::fstat(m_descriptor, std::addressof(stat_buf)).and_then([&stat_buf, this] noexcept -> std::expected<string, int32_t> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the point of wrapping everything inside lambdas? I doubt it makes this code more readable |
||
| if (!S_ISREG(stat_buf.st_mode)) { | ||
| kphp::log::warning("regular file expected"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The return type of your lambda already contains error type: |
||
| return std::unexpected{k2::errno_efault}; | ||
| } | ||
|
|
||
| const size_t size{static_cast<size_t>(stat_buf.st_size)}; | ||
| if (size > string::max_size()) { | ||
| kphp::log::warning("file is too large"); | ||
| return std::unexpected{k2::errno_efault}; | ||
| } | ||
|
|
||
| string file_content{static_cast<string::size_type>(size), false}; | ||
| return read({reinterpret_cast<std::byte*>(file_content.buffer()), file_content.size()}) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| .and_then([size, &file_content](size_t read_result) noexcept -> std::expected<string, int32_t> { | ||
| if (read_result < size) { | ||
| return std::unexpected{k2::errno_efault}; | ||
| } | ||
| return file_content; | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| inline auto file::flush() noexcept -> std::expected<void, int32_t> { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: let's add explicit
std::string_viewcreation here so we can passname.size()to avoid call tostd::strlen