32#ifdef HAVE_SYS_PARAM_H
33# include <sys/param.h>
46# undef HAVE_SYS_STATVFS_H
50#if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS)
51# include <sys/statvfs.h>
52#elif defined(HAVE_SYS_STATFS_H) && defined(HAVE_STATFS)
53# include <sys/statfs.h>
54#elif defined(HAVE_SYS_MOUNT_H) && defined(HAVE_STATFS)
55# include <sys/mount.h>
60# include "win32/pwd.h"
72# include <sys/utime.h>
95 if (
BG(CurrentStatFile)) {
96 zend_string_release(
BG(CurrentStatFile));
97 BG(CurrentStatFile) =
NULL;
99 if (
BG(CurrentLStatFile)) {
100 zend_string_release(
BG(CurrentLStatFile));
101 BG(CurrentLStatFile) =
NULL;
107static zend_result php_disk_total_space(
char *path,
double *space)
108#if defined(PHP_WIN32)
110 ULARGE_INTEGER FreeBytesAvailableToCaller;
111 ULARGE_INTEGER TotalNumberOfBytes;
112 ULARGE_INTEGER TotalNumberOfFreeBytes;
115 if (GetDiskFreeSpaceExW(pathw, &FreeBytesAvailableToCaller, &TotalNumberOfBytes, &TotalNumberOfFreeBytes) == 0) {
124 *space = TotalNumberOfBytes.HighPart * (
double) (((
zend_ulong)1) << 31) * 2.0 + TotalNumberOfBytes.LowPart;
133 double bytestotal = 0;
134#if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS)
136#elif (defined(HAVE_SYS_STATFS_H) || defined(HAVE_SYS_MOUNT_H)) && defined(HAVE_STATFS)
140#if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS)
141 if (statvfs(path, &
buf)) {
146 bytestotal = (((double)
buf.f_blocks) * ((double)
buf.f_frsize));
148 bytestotal = (((double)
buf.f_blocks) * ((double)
buf.f_bsize));
151#elif (defined(HAVE_SYS_STATFS_H) || defined(HAVE_SYS_MOUNT_H)) && defined(HAVE_STATFS)
152 if (statfs(path, &
buf)) {
156 bytestotal = (((double)
buf.f_bsize) * ((double)
buf.f_blocks));
185 if (php_disk_total_space(fullpath, &bytestotal) ==
SUCCESS) {
192static zend_result php_disk_free_space(
char *path,
double *space)
193#if defined(PHP_WIN32)
195 ULARGE_INTEGER FreeBytesAvailableToCaller;
196 ULARGE_INTEGER TotalNumberOfBytes;
197 ULARGE_INTEGER TotalNumberOfFreeBytes;
200 if (GetDiskFreeSpaceExW(pathw, &FreeBytesAvailableToCaller, &TotalNumberOfBytes, &TotalNumberOfFreeBytes) == 0) {
208 *space = FreeBytesAvailableToCaller.HighPart * (
double) (1ULL << 32) + FreeBytesAvailableToCaller.LowPart;
216 double bytesfree = 0;
217#if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS)
219#elif (defined(HAVE_SYS_STATFS_H) || defined(HAVE_SYS_MOUNT_H)) && defined(HAVE_STATFS)
223#if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS)
224 if (statvfs(path, &
buf)) {
229 bytesfree = (((double)
buf.f_bavail) * ((double)
buf.f_frsize));
231 bytesfree = (((double)
buf.f_bavail) * ((double)
buf.f_bsize));
233#elif (defined(HAVE_SYS_STATFS_H) || defined(HAVE_SYS_MOUNT_H)) && defined(HAVE_STATFS)
234 if (statfs(path, &
buf)) {
238 bytesfree = (((double)
buf.f_bsize) * ((double)
buf.f_bavail));
267 if (php_disk_free_space(fullpath, &bytesfree) ==
SUCCESS) {
277#if defined(ZTS) && defined(HAVE_GETGRNAM_R) && defined(_SC_GETGR_R_SIZE_MAX)
279 struct group *retgrptr;
280 long grbuflen = sysconf(_SC_GETGR_R_SIZE_MAX);
294 err = getgrnam_r(
name, &gr, grbuf, grbuflen, &retgrptr);
295 if (
err != 0 || retgrptr ==
NULL) {
307 struct group *gr = getgrnam(
name);
324#if !defined(PHP_WIN32)
341 option = PHP_STREAM_META_GROUP_NAME;
342 value = ZSTR_VAL(group_str);
344 option = PHP_STREAM_META_GROUP;
372 gid = (gid_t) group_long;
382 ret = VCWD_LCHOWN(filename, -1, gid);
418#if defined(ZTS) && defined(_SC_GETPW_R_SIZE_MAX) && defined(HAVE_GETPWNAM_R)
420 struct passwd *retpwptr =
NULL;
421 long pwbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
435 err = getpwnam_r(
name, &pw, pwbuf, pwbuflen, &retpwptr);
436 if (
err != 0 || retpwptr ==
NULL) {
448 struct passwd *pw = getpwnam(
name);
465#if !defined(PHP_WIN32)
482 option = PHP_STREAM_META_OWNER_NAME;
483 value = ZSTR_VAL(user_str);
485 option = PHP_STREAM_META_OWNER;
514 uid = (uid_t) user_long;
524 ret = VCWD_LCHOWN(filename, uid, -1);
614 bool filetime_is_null = 1, fileatime_is_null = 1;
617 struct utimbuf newtimebuf;
618 struct utimbuf *newtime = &newtimebuf;
632 if (filetime_is_null && fileatime_is_null) {
634 }
else if (!filetime_is_null && fileatime_is_null) {
635 newtime->modtime = newtime->actime = filetime;
636 }
else if (filetime_is_null && !fileatime_is_null) {
640 newtime->modtime = filetime;
654 if(!filetime_is_null || !fileatime_is_null) {
683 ret = VCWD_UTIME(filename, newtime);
702 if (
BG(CurrentStatFile)) {
703 zend_string_release(
BG(CurrentStatFile));
704 BG(CurrentStatFile) =
NULL;
706 if (
BG(CurrentLStatFile)) {
707 zend_string_release(
BG(CurrentLStatFile));
708 BG(CurrentLStatFile) =
NULL;
710 if (clear_realpath_cache) {
711 if (filename !=
NULL) {
723 bool clear_realpath_cache = 0;
724 char *filename =
NULL;
737#define IS_LINK_OPERATION(__t) ((__t) == FS_TYPE || (__t) == FS_IS_LINK || (__t) == FS_LSTAT || (__t) == FS_LPERMS)
738#define IS_EXISTS_CHECK(__t) ((__t) == FS_EXISTS || (__t) == FS_IS_W || (__t) == FS_IS_R || (__t) == FS_IS_X || (__t) == FS_IS_FILE || (__t) == FS_IS_DIR || (__t) == FS_IS_LINK || (__t) == FS_LPERMS)
739#define IS_ABLE_CHECK(__t) ((__t) == FS_IS_R || (__t) == FS_IS_W || (__t) == FS_IS_X)
740#define IS_ACCESS_CHECK(__t) (IS_ABLE_CHECK(type) || (__t) == FS_EXISTS)
747 int flags = 0, rmask=S_IROTH, wmask=S_IWOTH, xmask=S_IXOTH;
748 const char *local =
NULL;
766 const char *file_path_to_check;
769 file_path_to_check = local;
808 if (filename ==
BG(CurrentLStatFile)
809 || (
BG(CurrentLStatFile)
810 && zend_string_equal_content(filename,
BG(CurrentLStatFile)))) {
811 stat_sb = &
BG(lssb).sb;
815 if (filename ==
BG(CurrentStatFile)
816 || (
BG(CurrentStatFile)
817 && zend_string_equal_content(filename,
BG(CurrentStatFile)))) {
818 stat_sb = &
BG(ssb).sb;
849 if (
BG(CurrentLStatFile)) {
850 zend_string_release(
BG(CurrentLStatFile));
852 BG(CurrentLStatFile) = zend_string_copy(filename);
857 if (
BG(CurrentStatFile)) {
858 zend_string_release(
BG(CurrentStatFile));
860 BG(CurrentStatFile) = zend_string_copy(filename);
866 if(stat_sb->st_uid==getuid()) {
870 }
else if(stat_sb->st_gid==getgid()) {
878 groups = getgroups(0,
NULL);
881 n=getgroups(groups, gids);
883 if(stat_sb->st_gid==gids[i]) {
925 if (
S_ISLNK(stat_sb->st_mode)) {
928 switch(stat_sb->st_mode & S_IFMT) {
934#if defined(S_IFSOCK) && !defined(PHP_WIN32)
957 char *stat_sb_names[] = {
958 "dev",
"ino",
"mode",
"nlink",
"uid",
"gid",
"rdev",
959 "size",
"atime",
"mtime",
"ctime",
"blksize",
"blocks"
961 zval stat_dev, stat_ino, stat_mode, stat_nlink, stat_uid, stat_gid, stat_rdev,
962 stat_size, stat_atime, stat_mtime, stat_ctime, stat_blksize, stat_blocks;
963 zval *stat_sb_addresses[] = {
964 &stat_dev, &stat_ino, &stat_mode, &stat_nlink, &stat_uid, &stat_gid, &stat_rdev,
965 &stat_size, &stat_atime, &stat_mtime, &stat_ctime, &stat_blksize, &stat_blocks
967 size_t i, size_stat_sb =
sizeof(stat_sb_addresses) /
sizeof(*stat_sb_addresses);
974 ZVAL_LONG(&stat_nlink, stat_sb->st_nlink);
977#ifdef HAVE_STRUCT_STAT_ST_RDEV
983 ZVAL_LONG(&stat_atime, stat_sb->st_atime);
984 ZVAL_LONG(&stat_mtime, stat_sb->st_mtime);
985 ZVAL_LONG(&stat_ctime, stat_sb->st_ctime);
986#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
987 ZVAL_LONG(&stat_blksize, stat_sb->st_blksize);
991#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
992 ZVAL_LONG(&stat_blocks, stat_sb->st_blocks);
996 for (i = 0; i < size_stat_sb; i++) {
1001 for (i = 0; i < size_stat_sb; i++) {
1016#define FileFunction(name, funcnum) \
1017ZEND_NAMED_FUNCTION(name) { \
1018 zend_string *filename; \
1020 ZEND_PARSE_PARAMETERS_START(1, 1) \
1021 Z_PARAM_STR(filename) \
1022 ZEND_PARSE_PARAMETERS_END(); \
1024 php_stat(filename, funcnum, return_value); \
1116 while(buckets <
end) {
1133 add_assoc_bool_ex(&entry,
"is_rvalid",
sizeof(
"is_rvalid") - 1, bucket->is_rvalid);
1134 add_assoc_bool_ex(&entry,
"is_wvalid",
sizeof(
"is_wvalid") - 1, bucket->is_wvalid);
1135 add_assoc_bool_ex(&entry,
"is_readable",
sizeof(
"is_readable") - 1, bucket->is_readable);
1136 add_assoc_bool_ex(&entry,
"is_writable",
sizeof(
"is_writable") - 1, bucket->is_writable);
1139 bucket = bucket->
next;
PHP_RINIT filestat(INIT_FUNC_ARGS_PASSTHRU)
is_executable(string $filename)
filegroup(string $filename)
fileowner(string $filename)
filemtime(string $filename)
is_link(string $filename)
file_exists(string $filename)
file(string $filename, int $flags=0, $context=null)
chown(string $filename, string|int $user)
chgrp(string $filename, string|int $group)
is_readable(string $filename)
fileatime(string $filename)
clearstatcache(bool $clear_realpath_cache=false, string $filename="")
is_writable(string $filename)
is_file(string $filename)
touch(string $filename, ?int $mtime=null, ?int $atime=null)
fileperms(string $filename)
chmod(string $filename, int $permissions)
disk_total_space(string $directory)
filectime(string $filename)
fileinode(string $filename)
filetype(string $filename)
lchgrp(string $filename, string|int $group)
disk_free_space(string $directory)
filesize(string $filename)
lchown(string $filename, string|int $user)
zend_ffi_ctype_name_buf buf
#define FileFunction(name, funcnum)
PHPAPI void php_stat(zend_string *filename, int type, zval *return_value)
#define IS_ACCESS_CHECK(__t)
#define IS_LINK_OPERATION(__t)
#define IS_ABLE_CHECK(__t)
PHPAPI void php_clear_stat_cache(bool clear_realpath_cache, const char *filename, size_t filename_len)
#define IS_EXISTS_CHECK(__t)
PHPAPI zend_result php_get_uid_by_name(const char *name, uid_t *uid)
PHPAPI zend_result php_get_gid_by_name(const char *name, gid_t *gid)
PHPAPI int php_check_open_basedir(const char *path)
PHPAPI char * expand_filepath(const char *filepath, char *real_path)
foreach($dp as $el) foreach( $dp as $el) if( $pass2< 2) echo ""
#define PHP_WIN32_IOUTIL_INIT_W(path)
#define PHP_WIN32_IOUTIL_CLEANUP_W()
PHPAPI ZEND_COLD void php_error_docref(const char *docref, int type, const char *format,...)
#define PHP_RINIT_FUNCTION
#define PHP_RSHUTDOWN_FUNCTION
unsigned const char * end
PHPAPI php_stream_wrapper php_plain_files_wrapper
struct _php_stream php_stream
#define PHP_STREAM_URL_STAT_IGNORE_OPEN_BASEDIR
#define php_stream_open_wrapper_ex(path, mode, options, opened, context)
#define php_stream_close(stream)
#define PHP_STREAM_URL_STAT_LINK
#define PHP_STREAM_META_TOUCH
struct _php_stream_wrapper php_stream_wrapper
PHPAPI php_stream_wrapper * php_stream_locate_url_wrapper(const char *path, const char **path_for_open, int options)
#define PHP_STREAM_URL_STAT_QUIET
struct _php_stream_statbuf php_stream_statbuf
#define PHP_STREAM_META_ACCESS
int(* url_stat)(php_stream_wrapper *wrapper, const char *url, int flags, php_stream_statbuf *ssb, php_stream_context *context)
int(* stream_metadata)(php_stream_wrapper *wrapper, const char *url, int options, void *value, php_stream_context *context)
const php_stream_wrapper_ops * wops
struct _realpath_cache_bucket * next
#define php_win_err_free(err)
#define INTERNAL_FUNCTION_PARAMETERS
#define INTERNAL_FUNCTION_PARAM_PASSTHRU
ZEND_API void add_assoc_bool_ex(zval *arg, const char *key, size_t key_len, bool b)
ZEND_API void add_assoc_double_ex(zval *arg, const char *key, size_t key_len, double d)
ZEND_API void add_assoc_stringl_ex(zval *arg, const char *key, size_t key_len, const char *str, size_t length)
ZEND_API void add_assoc_long_ex(zval *arg, const char *key, size_t key_len, zend_long n)
ZEND_API ZEND_COLD void zend_argument_value_error(uint32_t arg_num, const char *format,...)
#define CHECK_NULL_PATH(p, l)
#define ZEND_PARSE_PARAMETERS_END()
#define ZEND_PARSE_PARAMETERS_NONE()
#define ZEND_PARSE_PARAMETERS_START(min_num_args, max_num_args)
#define Z_PARAM_LONG(dest)
#define Z_PARAM_LONG_OR_NULL(dest, is_null)
#define Z_PARAM_STR_OR_LONG(dest_str, dest_long)
#define Z_PARAM_BOOL(dest)
#define Z_PARAM_PATH(dest, dest_len)
#define erealloc(ptr, size)
#define safe_emalloc(nmemb, size, offset)
#define strncasecmp(s1, s2, n)
ZEND_API zval *ZEND_FASTCALL zend_hash_next_index_insert(HashTable *ht, zval *pData)
ZEND_API zval *ZEND_FASTCALL zend_hash_str_update(HashTable *ht, const char *str, size_t len, zval *pData)
ZEND_API zval *ZEND_FASTCALL zend_hash_str_add_new(HashTable *ht, const char *str, size_t len, zval *pData)
struct _zend_string zend_string
#define EXPECTED(condition)
#define Z_ARRVAL_P(zval_p)
ZEND_RESULT_CODE zend_result
CWD_API void realpath_cache_del(const char *path, size_t path_len)
CWD_API zend_long realpath_cache_max_buckets(void)
CWD_API realpath_cache_bucket ** realpath_cache_get_buckets(void)
CWD_API void realpath_cache_clean(void)
CWD_API zend_long realpath_cache_size(void)
struct _realpath_cache_bucket realpath_cache_bucket
#define VCWD_FOPEN(path, mode)
#define VCWD_CHOWN(path, owner, group)
#define VCWD_CHMOD(path, mode)
#define VCWD_ACCESS(pathname, mode)