Skip to content

Commit aa46234

Browse files
authored
[k2] add file_exists (#1465)
1 parent bf48ed8 commit aa46234

5 files changed

Lines changed: 29 additions & 7 deletions

File tree

builtin-functions/kphp-light/stdlib/file-functions.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ function sprintf ($format ::: string, ...$args) ::: string;
3838

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

41+
function file_exists ($name ::: string) ::: bool;
42+
4143
// === UNSUPPORTED ===
4244

4345
/** @kphp-extern-func-info stub generation-required */
@@ -51,8 +53,6 @@ function file ($name ::: string) ::: string[] | false;
5153
/** @kphp-extern-func-info stub generation-required */
5254
function file_put_contents ($name ::: string, $content ::: mixed, $flags ::: int = 0) ::: int | false;
5355
/** @kphp-extern-func-info stub generation-required */
54-
function file_exists ($name ::: string) ::: bool;
55-
/** @kphp-extern-func-info stub generation-required */
5656
function filectime ($name ::: string) ::: int | false;
5757
/** @kphp-extern-func-info stub generation-required */
5858
function filemtime ($name ::: string) ::: int | false;

runtime-light/k2-platform/k2-api.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ inline constexpr int32_t errno_etimedout = ETIMEDOUT;
4545
inline constexpr int32_t errno_eshutdown = ESHUTDOWN;
4646
inline constexpr int32_t errno_ecanceled = ECANCELED;
4747
inline constexpr int32_t errno_erange = ERANGE;
48+
inline constexpr int32_t errno_enoent = ENOENT;
4849

4950
using descriptor = uint64_t;
5051
inline constexpr k2::descriptor INVALID_PLATFORM_DESCRIPTOR = 0;
@@ -176,6 +177,13 @@ inline auto readdir(k2::descriptor descriptor) noexcept {
176177
: return_type{std::nullopt};
177178
}
178179

180+
inline std::expected<void, int32_t> access(std::string_view path, int32_t mode) noexcept {
181+
if (auto error_code{k2_access(path.data(), path.size(), mode)}; error_code != k2::errno_ok) [[unlikely]] {
182+
return std::unexpected{error_code};
183+
}
184+
return {};
185+
}
186+
179187
inline std::expected<void, int32_t> unlink(std::string_view path) noexcept {
180188
if (auto error_code{k2_unlink(path.data(), path.size())}; error_code != k2::errno_ok) [[unlikely]] {
181189
return std::unexpected{error_code};
@@ -184,8 +192,8 @@ inline std::expected<void, int32_t> unlink(std::string_view path) noexcept {
184192
}
185193
}
186194

187-
inline int32_t access(std::string_view component_name) noexcept {
188-
return k2_access(component_name.size(), component_name.data());
195+
inline int32_t component_access(std::string_view component_name) noexcept {
196+
return k2_component_access(component_name.size(), component_name.data());
189197
}
190198

191199
inline void stream_status(k2::descriptor descriptor, StreamStatus* status) noexcept {

runtime-light/k2-platform/k2-header.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include <time.h>
2828
#endif
2929

30-
#define K2_PLATFORM_HEADER_H_VERSION 12
30+
#define K2_PLATFORM_HEADER_H_VERSION 13
3131

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

@@ -280,6 +280,14 @@ struct DirEntry {
280280
*/
281281
int32_t k2_readdir(uint64_t dd, struct DirEntry* entry, struct DirEntry** result);
282282

283+
/**
284+
* Semantically equivalent to libc's `access` function.
285+
*
286+
* @return On success (all requested permissions granted), zero is returned. On failure, returns a non-zero value corresponding to a libc-like `errno`.
287+
*
288+
*/
289+
int32_t k2_access(const char* pathname, size_t pathname_len, int32_t mode);
290+
283291
/**
284292
* Semantically equivalent of libc's unlink
285293
*
@@ -299,7 +307,7 @@ int32_t k2_unlink(const char* path, size_t path_len);
299307
* `EINVAL` => `name` has invalid format (empty, non ascii, too long, etc..)
300308
* `EACCES` => permission denied
301309
*/
302-
int32_t k2_access(size_t name_len, const char* name);
310+
int32_t k2_component_access(size_t name_len, const char* name);
303311

304312
/**
305313
* If the write or read status is `Blocked` - then the platform ensures that

runtime-light/stdlib/confdata/confdata-functions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "runtime-light/stdlib/diagnostics/logs.h"
1212

1313
inline bool f$is_confdata_loaded() noexcept {
14-
return k2::access(kphp::confdata::COMPONENT_NAME) == k2::errno_ok;
14+
return k2::component_access(kphp::confdata::COMPONENT_NAME) == k2::errno_ok;
1515
}
1616

1717
// SAFETY: `key` is only used before await point

runtime-light/stdlib/file/file-system-functions.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <span>
1414
#include <string_view>
1515
#include <sys/stat.h>
16+
#include <unistd.h>
1617
#include <utility>
1718

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

118+
inline bool f$file_exists(const string& name) noexcept {
119+
const auto exists_res{k2::access(std::string_view{name.c_str(), name.size()}, F_OK)};
120+
return exists_res.has_value();
121+
}
122+
117123
inline bool f$unlink(const string& name) noexcept {
118124
std::expected<void, int32_t> unlink_res{k2::unlink(std::string_view{name.c_str(), name.size()})};
119125
return unlink_res.has_value();

0 commit comments

Comments
 (0)