Skip to content

[k2] support file_get_contents#1592

Open
Shamzik wants to merge 1 commit intomasterfrom
kshamazov/file_get_contents
Open

[k2] support file_get_contents#1592
Shamzik wants to merge 1 commit intomasterfrom
kshamazov/file_get_contents

Conversation

@Shamzik
Copy link
Copy Markdown
Contributor

@Shamzik Shamzik commented Apr 3, 2026

This PR implements file::get_contents method. Implementation was got from file kphp function.

@Shamzik Shamzik self-assigned this Apr 3, 2026
@Shamzik Shamzik added runtime Feature related to runtime k2 Affects compiler or runtime in K2 mode labels Apr 3, 2026
@Shamzik Shamzik added this to the next milestone Apr 3, 2026
#include <type_traits>
#include <utility>

#include <sys/stat.h>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

System headers should be placed in a single block


return k2::fstat(m_descriptor, std::addressof(stat_buf)).and_then([&stat_buf, this] noexcept -> std::expected<string, int32_t> {
if (!S_ISREG(stat_buf.st_mode)) {
kphp::log::warning("regular file expected");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type of your lambda already contains error type: int32_t. I think it's better to remove these warnings and return proper errno instead

}

string file_content{static_cast<string::size_type>(size), false};
return read({reinterpret_cast<std::byte*>(file_content.buffer()), file_content.size()})
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. nit: std::as_writable_bytes can be cleaner here
  2. Isn't mmap a better choice here?

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> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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

inline Optional<array<string>> f$file(const string& name) noexcept {
struct stat stat_buf {};

auto expected_file{kphp::fs::file::open(name.c_str(), "r")};
Copy link
Copy Markdown
Contributor

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_view creation here so we can pass name.size() to avoid call to std::strlen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

k2 Affects compiler or runtime in K2 mode runtime Feature related to runtime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants