diff --git a/builtin-functions/kphp-light/stdlib/file-functions.txt b/builtin-functions/kphp-light/stdlib/file-functions.txt index a04f361f1a..4de0dff12c 100644 --- a/builtin-functions/kphp-light/stdlib/file-functions.txt +++ b/builtin-functions/kphp-light/stdlib/file-functions.txt @@ -48,6 +48,8 @@ function file_exists ($name ::: string) ::: bool; function file ($name ::: string) ::: string[] | false; +function is_dir ($name ::: string) ::: bool; + function is_file ($name ::: string) ::: bool; function fgets ($stream ::: mixed, $length ::: int = -1) ::: string | false; @@ -66,8 +68,6 @@ function filectime ($name ::: string) ::: int | false; /** @kphp-extern-func-info stub generation-required */ function filemtime ($name ::: string) ::: int | false; /** @kphp-extern-func-info stub generation-required */ -function is_dir ($name ::: string) ::: bool; -/** @kphp-extern-func-info stub generation-required */ function is_readable ($name ::: string) ::: bool; /** @kphp-extern-func-info stub generation-required */ function mkdir ($name ::: string, $mode ::: int = 0777, $recursive ::: bool = false) ::: bool; diff --git a/runtime-light/stdlib/file/file-system-functions.h b/runtime-light/stdlib/file/file-system-functions.h index ea10a4fa63..8741257e5f 100644 --- a/runtime-light/stdlib/file/file-system-functions.h +++ b/runtime-light/stdlib/file/file-system-functions.h @@ -237,6 +237,15 @@ inline Optional> f$file(const string& name) noexcept { return result; } +inline bool f$is_dir(const string& name) noexcept { + struct stat stat_buf {}; + // TODO: the semantics in PHP are different: PHP expects stat + if (!k2::lstat(name.c_str(), std::addressof(stat_buf)).has_value()) { + return false; + } + return S_ISDIR(stat_buf.st_mode); +} + inline bool f$is_file(const string& name) noexcept { struct stat stat_buf {}; // TODO: the semantics in PHP are different: PHP expects stat diff --git a/tests/phpt/dl/473_file_for_k2.php b/tests/phpt/dl/473_file_for_k2.php index 1d40c542c2..08414fe1f4 100644 --- a/tests/phpt/dl/473_file_for_k2.php +++ b/tests/phpt/dl/473_file_for_k2.php @@ -8,6 +8,8 @@ var_dump (basename ('/etc/sudoers.d', '.d')); $filename = '/tmp/' . rand(1, 2e9); + var_dump (is_dir ('/tmp')); + var_dump (is_dir ($filename)); var_dump (is_file ($filename)); // var_dump ($filename); $file = fopen ($filename, "w");