Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions builtin-functions/kphp-light/stdlib/file-functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ function sprintf ($format ::: string, ...$args) ::: string;

function vsprintf ($format ::: string, $args ::: array) ::: string;

function file_exists ($name ::: string) ::: bool;

// === UNSUPPORTED ===

/** @kphp-extern-func-info stub generation-required */
Expand All @@ -51,8 +53,6 @@ function file ($name ::: string) ::: string[] | false;
/** @kphp-extern-func-info stub generation-required */
function file_put_contents ($name ::: string, $content ::: mixed, $flags ::: int = 0) ::: int | false;
/** @kphp-extern-func-info stub generation-required */
function file_exists ($name ::: string) ::: bool;
/** @kphp-extern-func-info stub generation-required */
function filectime ($name ::: string) ::: int | false;
/** @kphp-extern-func-info stub generation-required */
function filemtime ($name ::: string) ::: int | false;
Expand Down
12 changes: 10 additions & 2 deletions runtime-light/k2-platform/k2-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ inline constexpr int32_t errno_etimedout = ETIMEDOUT;
inline constexpr int32_t errno_eshutdown = ESHUTDOWN;
inline constexpr int32_t errno_ecanceled = ECANCELED;
inline constexpr int32_t errno_erange = ERANGE;
inline constexpr int32_t errno_enoent = ENOENT;

using descriptor = uint64_t;
inline constexpr k2::descriptor INVALID_PLATFORM_DESCRIPTOR = 0;
Expand Down Expand Up @@ -176,6 +177,13 @@ inline auto readdir(k2::descriptor descriptor) noexcept {
: return_type{std::nullopt};
}

inline std::expected<void, int32_t> access(std::string_view path, int32_t mode) noexcept {
if (auto error_code{k2_access(path.data(), path.size(), mode)}; error_code != k2::errno_ok) [[unlikely]] {
return std::unexpected{error_code};
}
return {};
}

inline std::expected<void, int32_t> unlink(std::string_view path) noexcept {
if (auto error_code{k2_unlink(path.data(), path.size())}; error_code != k2::errno_ok) [[unlikely]] {
return std::unexpected{error_code};
Expand All @@ -184,8 +192,8 @@ inline std::expected<void, int32_t> unlink(std::string_view path) noexcept {
}
}

inline int32_t access(std::string_view component_name) noexcept {
return k2_access(component_name.size(), component_name.data());
inline int32_t component_access(std::string_view component_name) noexcept {
return k2_component_access(component_name.size(), component_name.data());
}

inline void stream_status(k2::descriptor descriptor, StreamStatus* status) noexcept {
Expand Down
12 changes: 10 additions & 2 deletions runtime-light/k2-platform/k2-header.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <time.h>
#endif

#define K2_PLATFORM_HEADER_H_VERSION 12
#define K2_PLATFORM_HEADER_H_VERSION 13

// Always check that enum value is a valid value!

Expand Down Expand Up @@ -280,6 +280,14 @@ struct DirEntry {
*/
int32_t k2_readdir(uint64_t dd, struct DirEntry* entry, struct DirEntry** result);

/**
* Semantically equivalent to libc's `access` function.
*
* @return On success (all requested permissions granted), zero is returned. On failure, returns a non-zero value corresponding to a libc-like `errno`.
*
*/
int32_t k2_access(const char* pathname, size_t pathname_len, int32_t mode);

/**
* Semantically equivalent of libc's unlink
*
Expand All @@ -299,7 +307,7 @@ int32_t k2_unlink(const char* path, size_t path_len);
* `EINVAL` => `name` has invalid format (empty, non ascii, too long, etc..)
* `EACCES` => permission denied
*/
int32_t k2_access(size_t name_len, const char* name);
int32_t k2_component_access(size_t name_len, const char* name);

/**
* If the write or read status is `Blocked` - then the platform ensures that
Expand Down
2 changes: 1 addition & 1 deletion runtime-light/stdlib/confdata/confdata-functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "runtime-light/stdlib/diagnostics/logs.h"

inline bool f$is_confdata_loaded() noexcept {
return k2::access(kphp::confdata::COMPONENT_NAME) == k2::errno_ok;
return k2::component_access(kphp::confdata::COMPONENT_NAME) == k2::errno_ok;
}

// SAFETY: `key` is only used before await point
Expand Down
6 changes: 6 additions & 0 deletions runtime-light/stdlib/file/file-system-functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <span>
#include <string_view>
#include <sys/stat.h>
#include <unistd.h>
#include <utility>

#include "runtime-common/core/runtime-core.h"
Expand Down Expand Up @@ -114,6 +115,11 @@ inline kphp::coro::task<bool> f$fclose(resource stream) noexcept {
co_return false;
}

inline bool f$file_exists(const string& name) noexcept {
const auto exists_res{k2::access(std::string_view{name.c_str(), name.size()}, F_OK)};
return exists_res.has_value();
}

inline bool f$unlink(const string& name) noexcept {
std::expected<void, int32_t> unlink_res{k2::unlink(std::string_view{name.c_str(), name.size()})};
return unlink_res.has_value();
Expand Down
Loading