xref: /PHP-7.0/ext/standard/filestat.c (revision 478f119a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2017 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Author:  Jim Winstead <jimw@php.net>                                 |
16    +----------------------------------------------------------------------+
17  */
18 
19 /* $Id$ */
20 
21 #include "php.h"
22 #include "fopen_wrappers.h"
23 #include "php_globals.h"
24 
25 #include <stdlib.h>
26 #include <sys/stat.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <ctype.h>
30 #include <time.h>
31 
32 #if HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
35 
36 #if HAVE_SYS_PARAM_H
37 # include <sys/param.h>
38 #endif
39 
40 #if HAVE_SYS_VFS_H
41 # include <sys/vfs.h>
42 #endif
43 
44 #ifdef OS2
45 #  define INCL_DOS
46 #  include <os2.h>
47 #endif
48 
49 #if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS)
50 # include <sys/statvfs.h>
51 #elif defined(HAVE_SYS_STATFS_H) && defined(HAVE_STATFS)
52 # include <sys/statfs.h>
53 #elif defined(HAVE_SYS_MOUNT_H) && defined(HAVE_STATFS)
54 # include <sys/mount.h>
55 #endif
56 
57 #if HAVE_PWD_H
58 # ifdef PHP_WIN32
59 #  include "win32/pwd.h"
60 # else
61 #  include <pwd.h>
62 # endif
63 #endif
64 
65 #if HAVE_GRP_H
66 # ifdef PHP_WIN32
67 #  include "win32/grp.h"
68 # else
69 #  include <grp.h>
70 # endif
71 #endif
72 
73 #if HAVE_UTIME
74 # ifdef PHP_WIN32
75 #  include <sys/utime.h>
76 # else
77 #  include <utime.h>
78 # endif
79 #endif
80 
81 #ifdef PHP_WIN32
82 #include "win32/winutil.h"
83 #endif
84 
85 #include "basic_functions.h"
86 #include "php_filestat.h"
87 
PHP_RINIT_FUNCTION(filestat)88 PHP_RINIT_FUNCTION(filestat) /* {{{ */
89 {
90 	BG(CurrentStatFile)=NULL;
91 	BG(CurrentLStatFile)=NULL;
92 	return SUCCESS;
93 }
94 /* }}} */
95 
PHP_RSHUTDOWN_FUNCTION(filestat)96 PHP_RSHUTDOWN_FUNCTION(filestat) /* {{{ */
97 {
98 	if (BG(CurrentStatFile)) {
99 		efree (BG(CurrentStatFile));
100 		BG(CurrentStatFile) = NULL;
101 	}
102 	if (BG(CurrentLStatFile)) {
103 		efree (BG(CurrentLStatFile));
104 		BG(CurrentLStatFile) = NULL;
105 	}
106 	return SUCCESS;
107 }
108 /* }}} */
109 
php_disk_total_space(char * path,double * space)110 static int php_disk_total_space(char *path, double *space) /* {{{ */
111 #if defined(WINDOWS) /* {{{ */
112 {
113 	double bytestotal = 0;
114 	HINSTANCE kernel32;
115 	FARPROC gdfse;
116 	typedef BOOL (WINAPI *gdfse_func)(LPCTSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER);
117 	gdfse_func func;
118 
119 	/* These are used by GetDiskFreeSpaceEx, if available. */
120 	ULARGE_INTEGER FreeBytesAvailableToCaller;
121 	ULARGE_INTEGER TotalNumberOfBytes;
122 	ULARGE_INTEGER TotalNumberOfFreeBytes;
123 
124 	/* These are used by GetDiskFreeSpace otherwise. */
125 	DWORD SectorsPerCluster;
126 	DWORD BytesPerSector;
127 	DWORD NumberOfFreeClusters;
128 	DWORD TotalNumberOfClusters;
129 
130 	/* GetDiskFreeSpaceEx is only available in NT and Win95 post-OSR2,
131 	   so we have to jump through some hoops to see if the function
132 	   exists. */
133 	kernel32 = LoadLibrary("kernel32.dll");
134 	if (kernel32) {
135 		gdfse = GetProcAddress(kernel32, "GetDiskFreeSpaceExA");
136 		/* It's available, so we can call it. */
137 		if (gdfse) {
138 			func = (gdfse_func)gdfse;
139 			if (func(path,
140 						&FreeBytesAvailableToCaller,
141 						&TotalNumberOfBytes,
142 						&TotalNumberOfFreeBytes) == 0) {
143 				php_error_docref(NULL, E_WARNING, "%s", php_win_err());
144 				return FAILURE;
145 			}
146 
147 			/* i know - this is ugly, but i works <thies@thieso.net> */
148 			bytestotal  = TotalNumberOfBytes.HighPart *
149 				(double) (((zend_ulong)1) << 31) * 2.0 +
150 				TotalNumberOfBytes.LowPart;
151 		} else { /* If it's not available, we just use GetDiskFreeSpace */
152 			if (GetDiskFreeSpace(path,
153 						&SectorsPerCluster, &BytesPerSector,
154 						&NumberOfFreeClusters, &TotalNumberOfClusters) == 0) {
155 				php_error_docref(NULL, E_WARNING, "%s", php_win_err());
156 				return FAILURE;
157 			}
158 			bytestotal = (double)TotalNumberOfClusters * (double)SectorsPerCluster * (double)BytesPerSector;
159 		}
160 	} else {
161 		php_error_docref(NULL, E_WARNING, "Unable to load kernel32.dll");
162 		return FAILURE;
163 	}
164 
165 	*space = bytestotal;
166 	return SUCCESS;
167 }
168 /* }}} */
169 #elif defined(OS2) /* {{{ */
170 {
171 	double bytestotal = 0;
172 	FSALLOCATE fsinfo;
173 	char drive = path[0] & 95;
174 
175 	if (DosQueryFSInfo( drive ? drive - 64 : 0, FSIL_ALLOC, &fsinfo, sizeof( fsinfo ) ) == 0) {
176 		bytestotal = (double)fsinfo.cbSector * fsinfo.cSectorUnit * fsinfo.cUnit;
177 		*space = bytestotal;
178 		return SUCCESS;
179 	}
180 	return FAILURE;
181 }
182 /* }}} */
183 #else /* {{{ if !defined(OS2) && !defined(WINDOWS) */
184 {
185 	double bytestotal = 0;
186 #if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS)
187 	struct statvfs buf;
188 #elif (defined(HAVE_SYS_STATFS_H) || defined(HAVE_SYS_MOUNT_H)) && defined(HAVE_STATFS)
189 	struct statfs buf;
190 #endif
191 
192 #if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS)
193 	if (statvfs(path, &buf)) {
194 		php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
195 		return FAILURE;
196 	}
197 	if (buf.f_frsize) {
198 		bytestotal = (((double)buf.f_blocks) * ((double)buf.f_frsize));
199 	} else {
200 		bytestotal = (((double)buf.f_blocks) * ((double)buf.f_bsize));
201 	}
202 
203 #elif (defined(HAVE_SYS_STATFS_H) || defined(HAVE_SYS_MOUNT_H)) && defined(HAVE_STATFS)
204 	if (statfs(path, &buf)) {
205 		php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
206 		return FAILURE;
207 	}
208 	bytestotal = (((double)buf.f_bsize) * ((double)buf.f_blocks));
209 #endif
210 
211 	*space = bytestotal;
212 	return SUCCESS;
213 }
214 #endif
215 /* }}} */
216 /* }}} */
217 
218 /* {{{ proto float disk_total_space(string path)
219    Get total disk space for filesystem that path is on */
PHP_FUNCTION(disk_total_space)220 PHP_FUNCTION(disk_total_space)
221 {
222 	double bytestotal;
223 	char *path;
224 	size_t path_len;
225 
226 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &path, &path_len) == FAILURE) {
227 		return;
228 	}
229 
230 	if (php_check_open_basedir(path)) {
231 		RETURN_FALSE;
232 	}
233 
234 	if (php_disk_total_space(path, &bytestotal) == SUCCESS) {
235 		RETURN_DOUBLE(bytestotal);
236 	}
237 	RETURN_FALSE;
238 }
239 /* }}} */
240 
php_disk_free_space(char * path,double * space)241 static int php_disk_free_space(char *path, double *space) /* {{{ */
242 #if defined(WINDOWS) /* {{{ */
243 {
244 	double bytesfree = 0;
245 
246 	HINSTANCE kernel32;
247 	FARPROC gdfse;
248 	typedef BOOL (WINAPI *gdfse_func)(LPCTSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER);
249 	gdfse_func func;
250 
251 	/* These are used by GetDiskFreeSpaceEx, if available. */
252 	ULARGE_INTEGER FreeBytesAvailableToCaller;
253 	ULARGE_INTEGER TotalNumberOfBytes;
254 	ULARGE_INTEGER TotalNumberOfFreeBytes;
255 
256 	/* These are used by GetDiskFreeSpace otherwise. */
257 	DWORD SectorsPerCluster;
258 	DWORD BytesPerSector;
259 	DWORD NumberOfFreeClusters;
260 	DWORD TotalNumberOfClusters;
261 
262 	/* GetDiskFreeSpaceEx is only available in NT and Win95 post-OSR2,
263 	   so we have to jump through some hoops to see if the function
264 	   exists. */
265 	kernel32 = LoadLibrary("kernel32.dll");
266 	if (kernel32) {
267 		gdfse = GetProcAddress(kernel32, "GetDiskFreeSpaceExA");
268 		/* It's available, so we can call it. */
269 		if (gdfse) {
270 			func = (gdfse_func)gdfse;
271 			if (func(path,
272 						&FreeBytesAvailableToCaller,
273 						&TotalNumberOfBytes,
274 						&TotalNumberOfFreeBytes) == 0) {
275 				php_error_docref(NULL, E_WARNING, "%s", php_win_err());
276 				return FAILURE;
277 			}
278 
279 			/* i know - this is ugly, but i works <thies@thieso.net> */
280 			bytesfree  = FreeBytesAvailableToCaller.HighPart *
281 				(double) (((zend_ulong)1) << 31) * 2.0 +
282 				FreeBytesAvailableToCaller.LowPart;
283 		} else { /* If it's not available, we just use GetDiskFreeSpace */
284 			if (GetDiskFreeSpace(path,
285 						&SectorsPerCluster, &BytesPerSector,
286 						&NumberOfFreeClusters, &TotalNumberOfClusters) == 0) {
287 				php_error_docref(NULL, E_WARNING, "%s", php_win_err());
288 				return FAILURE;
289 			}
290 			bytesfree = (double)NumberOfFreeClusters * (double)SectorsPerCluster * (double)BytesPerSector;
291 		}
292 	} else {
293 		php_error_docref(NULL, E_WARNING, "Unable to load kernel32.dll");
294 		return FAILURE;
295 	}
296 
297 	*space = bytesfree;
298 	return SUCCESS;
299 }
300 /* }}} */
301 #elif defined(OS2) /* {{{ */
302 {
303 	double bytesfree = 0;
304 	FSALLOCATE fsinfo;
305 	char drive = path[0] & 95;
306 
307 	if (DosQueryFSInfo( drive ? drive - 64 : 0, FSIL_ALLOC, &fsinfo, sizeof( fsinfo ) ) == 0) {
308 		bytesfree = (double)fsinfo.cbSector * fsinfo.cSectorUnit * fsinfo.cUnitAvail;
309 		*space = bytesfree;
310 		return SUCCESS;
311 	}
312 	return FAILURE;
313 }
314 /* }}} */
315 #else /* {{{ if !defined(OS2) && !defined(WINDOWS) */
316 {
317 	double bytesfree = 0;
318 #if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS)
319 	struct statvfs buf;
320 #elif (defined(HAVE_SYS_STATFS_H) || defined(HAVE_SYS_MOUNT_H)) && defined(HAVE_STATFS)
321 	struct statfs buf;
322 #endif
323 
324 #if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS)
325 	if (statvfs(path, &buf)) {
326 		php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
327 		return FAILURE;
328 	}
329 	if (buf.f_frsize) {
330 		bytesfree = (((double)buf.f_bavail) * ((double)buf.f_frsize));
331 	} else {
332 		bytesfree = (((double)buf.f_bavail) * ((double)buf.f_bsize));
333 	}
334 #elif (defined(HAVE_SYS_STATFS_H) || defined(HAVE_SYS_MOUNT_H)) && defined(HAVE_STATFS)
335 	if (statfs(path, &buf)) {
336 		php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
337 		return FAILURE;
338 	}
339 #ifdef NETWARE
340 	bytesfree = (((double)buf.f_bsize) * ((double)buf.f_bfree));
341 #else
342 	bytesfree = (((double)buf.f_bsize) * ((double)buf.f_bavail));
343 #endif
344 #endif
345 
346 	*space = bytesfree;
347 	return SUCCESS;
348 }
349 #endif
350 /* }}} */
351 /* }}} */
352 
353 /* {{{ proto float disk_free_space(string path)
354    Get free disk space for filesystem that path is on */
PHP_FUNCTION(disk_free_space)355 PHP_FUNCTION(disk_free_space)
356 {
357 	double bytesfree;
358 	char *path;
359 	size_t path_len;
360 
361 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &path, &path_len) == FAILURE) {
362 		return;
363 	}
364 
365 	if (php_check_open_basedir(path)) {
366 		RETURN_FALSE;
367 	}
368 
369 	if (php_disk_free_space(path, &bytesfree) == SUCCESS) {
370 		RETURN_DOUBLE(bytesfree);
371 	}
372 	RETURN_FALSE;
373 }
374 /* }}} */
375 
376 #if !defined(WINDOWS) && !defined(NETWARE)
php_get_gid_by_name(const char * name,gid_t * gid)377 PHPAPI int php_get_gid_by_name(const char *name, gid_t *gid)
378 {
379 #if defined(ZTS) && defined(HAVE_GETGRNAM_R) && defined(_SC_GETGR_R_SIZE_MAX)
380 		struct group gr;
381 		struct group *retgrptr;
382 		long grbuflen = sysconf(_SC_GETGR_R_SIZE_MAX);
383 		char *grbuf;
384 
385 		if (grbuflen < 1) {
386 			return FAILURE;
387 		}
388 
389 		grbuf = emalloc(grbuflen);
390 		if (getgrnam_r(name, &gr, grbuf, grbuflen, &retgrptr) != 0 || retgrptr == NULL) {
391 			efree(grbuf);
392 			return FAILURE;
393 		}
394 		efree(grbuf);
395 		*gid = gr.gr_gid;
396 #else
397 		struct group *gr = getgrnam(name);
398 
399 		if (!gr) {
400 			return FAILURE;
401 		}
402 		*gid = gr->gr_gid;
403 #endif
404 		return SUCCESS;
405 }
406 #endif
407 
php_do_chgrp(INTERNAL_FUNCTION_PARAMETERS,int do_lchgrp)408 static void php_do_chgrp(INTERNAL_FUNCTION_PARAMETERS, int do_lchgrp) /* {{{ */
409 {
410 	char *filename;
411 	size_t filename_len;
412 	zval *group;
413 #if !defined(WINDOWS)
414 	gid_t gid;
415 	int ret;
416 #endif
417 	php_stream_wrapper *wrapper;
418 
419 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "pz/", &filename, &filename_len, &group) == FAILURE) {
420 		RETURN_FALSE;
421 	}
422 
423 	wrapper = php_stream_locate_url_wrapper(filename, NULL, 0);
424 	if(wrapper != &php_plain_files_wrapper || strncasecmp("file://", filename, 7) == 0) {
425 		if(wrapper && wrapper->wops->stream_metadata) {
426 			int option;
427 			void *value;
428 			if (Z_TYPE_P(group) == IS_LONG) {
429 				option = PHP_STREAM_META_GROUP;
430 				value = &Z_LVAL_P(group);
431 			} else if (Z_TYPE_P(group) == IS_STRING) {
432 				option = PHP_STREAM_META_GROUP_NAME;
433 				value = Z_STRVAL_P(group);
434 			} else {
435 				php_error_docref(NULL, E_WARNING, "parameter 2 should be string or integer, %s given", zend_zval_type_name(group));
436 				RETURN_FALSE;
437 			}
438 			if(wrapper->wops->stream_metadata(wrapper, filename, option, value, NULL)) {
439 				RETURN_TRUE;
440 			} else {
441 				RETURN_FALSE;
442 			}
443 		} else {
444 #if !defined(WINDOWS)
445 /* On Windows, we expect regular chgrp to fail silently by default */
446 			php_error_docref(NULL, E_WARNING, "Can not call chgrp() for a non-standard stream");
447 #endif
448 			RETURN_FALSE;
449 		}
450 	}
451 
452 #if defined(WINDOWS)
453 	/* We have no native chgrp on Windows, nothing left to do if stream doesn't have own implementation */
454 	RETURN_FALSE;
455 #else
456 	if (Z_TYPE_P(group) == IS_LONG) {
457 		gid = (gid_t)Z_LVAL_P(group);
458 	} else if (Z_TYPE_P(group) == IS_STRING) {
459 		if(php_get_gid_by_name(Z_STRVAL_P(group), &gid) != SUCCESS) {
460 			php_error_docref(NULL, E_WARNING, "Unable to find gid for %s", Z_STRVAL_P(group));
461 			RETURN_FALSE;
462 		}
463 	} else {
464 		php_error_docref(NULL, E_WARNING, "parameter 2 should be string or integer, %s given", zend_zval_type_name(group));
465 		RETURN_FALSE;
466 	}
467 
468 	/* Check the basedir */
469 	if (php_check_open_basedir(filename)) {
470 		RETURN_FALSE;
471 	}
472 
473 	if (do_lchgrp) {
474 #if HAVE_LCHOWN
475 		ret = VCWD_LCHOWN(filename, -1, gid);
476 #endif
477 	} else {
478 		ret = VCWD_CHOWN(filename, -1, gid);
479 	}
480 	if (ret == -1) {
481 		php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
482 		RETURN_FALSE;
483 	}
484 	RETURN_TRUE;
485 #endif
486 }
487 /* }}} */
488 
489 #ifndef NETWARE
490 /* {{{ proto bool chgrp(string filename, mixed group)
491    Change file group */
PHP_FUNCTION(chgrp)492 PHP_FUNCTION(chgrp)
493 {
494 	php_do_chgrp(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
495 }
496 /* }}} */
497 
498 /* {{{ proto bool lchgrp(string filename, mixed group)
499    Change symlink group */
500 #if HAVE_LCHOWN
PHP_FUNCTION(lchgrp)501 PHP_FUNCTION(lchgrp)
502 {
503 # if !defined(WINDOWS)
504 	php_do_chgrp(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
505 # else
506 	RETURN_FALSE;
507 # endif
508 }
509 #endif
510 /* }}} */
511 #endif /* !NETWARE */
512 
513 #if !defined(WINDOWS) && !defined(NETWARE)
php_get_uid_by_name(const char * name,uid_t * uid)514 PHPAPI uid_t php_get_uid_by_name(const char *name, uid_t *uid)
515 {
516 #if defined(ZTS) && defined(_SC_GETPW_R_SIZE_MAX) && defined(HAVE_GETPWNAM_R)
517 		struct passwd pw;
518 		struct passwd *retpwptr = NULL;
519 		long pwbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
520 		char *pwbuf;
521 
522 		if (pwbuflen < 1) {
523 			return FAILURE;
524 		}
525 
526 		pwbuf = emalloc(pwbuflen);
527 		if (getpwnam_r(name, &pw, pwbuf, pwbuflen, &retpwptr) != 0 || retpwptr == NULL) {
528 			efree(pwbuf);
529 			return FAILURE;
530 		}
531 		efree(pwbuf);
532 		*uid = pw.pw_uid;
533 #else
534 		struct passwd *pw = getpwnam(name);
535 
536 		if (!pw) {
537 			return FAILURE;
538 		}
539 		*uid = pw->pw_uid;
540 #endif
541 		return SUCCESS;
542 }
543 #endif
544 
php_do_chown(INTERNAL_FUNCTION_PARAMETERS,int do_lchown)545 static void php_do_chown(INTERNAL_FUNCTION_PARAMETERS, int do_lchown) /* {{{ */
546 {
547 	char *filename;
548 	size_t filename_len;
549 	zval *user;
550 #if !defined(WINDOWS)
551 	uid_t uid;
552 	int ret;
553 #endif
554 	php_stream_wrapper *wrapper;
555 
556 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "pz/", &filename, &filename_len, &user) == FAILURE) {
557 		return;
558 	}
559 
560 	wrapper = php_stream_locate_url_wrapper(filename, NULL, 0);
561 	if(wrapper != &php_plain_files_wrapper || strncasecmp("file://", filename, 7) == 0) {
562 		if(wrapper && wrapper->wops->stream_metadata) {
563 			int option;
564 			void *value;
565 			if (Z_TYPE_P(user) == IS_LONG) {
566 				option = PHP_STREAM_META_OWNER;
567 				value = &Z_LVAL_P(user);
568 			} else if (Z_TYPE_P(user) == IS_STRING) {
569 				option = PHP_STREAM_META_OWNER_NAME;
570 				value = Z_STRVAL_P(user);
571 			} else {
572 				php_error_docref(NULL, E_WARNING, "parameter 2 should be string or integer, %s given", zend_zval_type_name(user));
573 				RETURN_FALSE;
574 			}
575 			if(wrapper->wops->stream_metadata(wrapper, filename, option, value, NULL)) {
576 				RETURN_TRUE;
577 			} else {
578 				RETURN_FALSE;
579 			}
580 		} else {
581 #if !defined(WINDOWS)
582 /* On Windows, we expect regular chown to fail silently by default */
583 			php_error_docref(NULL, E_WARNING, "Can not call chown() for a non-standard stream");
584 #endif
585 			RETURN_FALSE;
586 		}
587 	}
588 
589 #if defined(WINDOWS)
590 	/* We have no native chown on Windows, nothing left to do if stream doesn't have own implementation */
591 	RETURN_FALSE;
592 #else
593 
594 	if (Z_TYPE_P(user) == IS_LONG) {
595 		uid = (uid_t)Z_LVAL_P(user);
596 	} else if (Z_TYPE_P(user) == IS_STRING) {
597 		if(php_get_uid_by_name(Z_STRVAL_P(user), &uid) != SUCCESS) {
598 			php_error_docref(NULL, E_WARNING, "Unable to find uid for %s", Z_STRVAL_P(user));
599 			RETURN_FALSE;
600 		}
601 	} else {
602 		php_error_docref(NULL, E_WARNING, "parameter 2 should be string or integer, %s given", zend_zval_type_name(user));
603 		RETURN_FALSE;
604 	}
605 
606 	/* Check the basedir */
607 	if (php_check_open_basedir(filename)) {
608 		RETURN_FALSE;
609 	}
610 
611 	if (do_lchown) {
612 #if HAVE_LCHOWN
613 		ret = VCWD_LCHOWN(filename, uid, -1);
614 #endif
615 	} else {
616 		ret = VCWD_CHOWN(filename, uid, -1);
617 	}
618 	if (ret == -1) {
619 		php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
620 		RETURN_FALSE;
621 	}
622 	RETURN_TRUE;
623 #endif
624 }
625 /* }}} */
626 
627 
628 #ifndef NETWARE
629 /* {{{ proto bool chown (string filename, mixed user)
630    Change file owner */
PHP_FUNCTION(chown)631 PHP_FUNCTION(chown)
632 {
633 	php_do_chown(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
634 }
635 /* }}} */
636 
637 /* {{{ proto bool chown (string filename, mixed user)
638    Change file owner */
639 #if HAVE_LCHOWN
PHP_FUNCTION(lchown)640 PHP_FUNCTION(lchown)
641 {
642 # if !defined(WINDOWS)
643 	RETVAL_TRUE;
644 	php_do_chown(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
645 # else
646 	RETURN_FALSE;
647 # endif
648 }
649 #endif
650 /* }}} */
651 #endif /* !NETWARE */
652 
653 /* {{{ proto bool chmod(string filename, int mode)
654    Change file mode */
PHP_FUNCTION(chmod)655 PHP_FUNCTION(chmod)
656 {
657 	char *filename;
658 	size_t filename_len;
659 	zend_long mode;
660 	int ret;
661 	mode_t imode;
662 	php_stream_wrapper *wrapper;
663 
664 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "pl", &filename, &filename_len, &mode) == FAILURE) {
665 		return;
666 	}
667 
668 	wrapper = php_stream_locate_url_wrapper(filename, NULL, 0);
669 	if(wrapper != &php_plain_files_wrapper || strncasecmp("file://", filename, 7) == 0) {
670 		if(wrapper && wrapper->wops->stream_metadata) {
671 			if(wrapper->wops->stream_metadata(wrapper, filename, PHP_STREAM_META_ACCESS, &mode, NULL)) {
672 				RETURN_TRUE;
673 			} else {
674 				RETURN_FALSE;
675 			}
676 		} else {
677 			php_error_docref(NULL, E_WARNING, "Can not call chmod() for a non-standard stream");
678 			RETURN_FALSE;
679 		}
680 	}
681 
682 	/* Check the basedir */
683 	if (php_check_open_basedir(filename)) {
684 		RETURN_FALSE;
685 	}
686 
687 	imode = (mode_t) mode;
688 
689 	ret = VCWD_CHMOD(filename, imode);
690 	if (ret == -1) {
691 		php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
692 		RETURN_FALSE;
693 	}
694 	RETURN_TRUE;
695 }
696 /* }}} */
697 
698 #if HAVE_UTIME
699 /* {{{ proto bool touch(string filename [, int time [, int atime]])
700    Set modification time of file */
PHP_FUNCTION(touch)701 PHP_FUNCTION(touch)
702 {
703 	char *filename;
704 	size_t filename_len;
705 	zend_long filetime = 0, fileatime = 0;
706 	int ret, argc = ZEND_NUM_ARGS();
707 	FILE *file;
708 	struct utimbuf newtimebuf;
709 	struct utimbuf *newtime = &newtimebuf;
710 	php_stream_wrapper *wrapper;
711 
712 	if (zend_parse_parameters(argc, "p|ll", &filename, &filename_len, &filetime, &fileatime) == FAILURE) {
713 		return;
714 	}
715 
716 	if (!filename_len) {
717 		RETURN_FALSE;
718 	}
719 
720 	switch (argc) {
721 		case 1:
722 #ifdef HAVE_UTIME_NULL
723 			newtime = NULL;
724 #else
725 			newtime->modtime = newtime->actime = time(NULL);
726 #endif
727 			break;
728 		case 2:
729 			newtime->modtime = newtime->actime = filetime;
730 			break;
731 		case 3:
732 			newtime->modtime = filetime;
733 			newtime->actime = fileatime;
734 			break;
735 		default:
736 			/* Never reached */
737 			WRONG_PARAM_COUNT;
738 	}
739 
740 	wrapper = php_stream_locate_url_wrapper(filename, NULL, 0);
741 	if(wrapper != &php_plain_files_wrapper || strncasecmp("file://", filename, 7) == 0) {
742 		if(wrapper && wrapper->wops->stream_metadata) {
743 			if(wrapper->wops->stream_metadata(wrapper, filename, PHP_STREAM_META_TOUCH, newtime, NULL)) {
744 				RETURN_TRUE;
745 			} else {
746 				RETURN_FALSE;
747 			}
748 		} else {
749 			php_stream *stream;
750 			if(argc > 1) {
751 				php_error_docref(NULL, E_WARNING, "Can not call touch() for a non-standard stream");
752 				RETURN_FALSE;
753 			}
754 			stream = php_stream_open_wrapper_ex(filename, "c", REPORT_ERRORS, NULL, NULL);
755 			if(stream != NULL) {
756 				php_stream_close(stream);
757 				RETURN_TRUE;
758 			} else {
759 				RETURN_FALSE;
760 			}
761 		}
762 	}
763 
764 	/* Check the basedir */
765 	if (php_check_open_basedir(filename)) {
766 		RETURN_FALSE;
767 	}
768 
769 	/* create the file if it doesn't exist already */
770 	if (VCWD_ACCESS(filename, F_OK) != 0) {
771 		file = VCWD_FOPEN(filename, "w");
772 		if (file == NULL) {
773 			php_error_docref(NULL, E_WARNING, "Unable to create file %s because %s", filename, strerror(errno));
774 			RETURN_FALSE;
775 		}
776 		fclose(file);
777 	}
778 
779 	ret = VCWD_UTIME(filename, newtime);
780 	if (ret == -1) {
781 		php_error_docref(NULL, E_WARNING, "Utime failed: %s", strerror(errno));
782 		RETURN_FALSE;
783 	}
784 	RETURN_TRUE;
785 }
786 /* }}} */
787 #endif
788 
789 /* {{{ php_clear_stat_cache()
790 */
php_clear_stat_cache(zend_bool clear_realpath_cache,const char * filename,int filename_len)791 PHPAPI void php_clear_stat_cache(zend_bool clear_realpath_cache, const char *filename, int filename_len)
792 {
793 	/* always clear CurrentStatFile and CurrentLStatFile even if filename is not NULL
794 	 * as it may contain outdated data (e.g. "nlink" for a directory when deleting a file
795 	 * in this directory, as shown by lstat_stat_variation9.phpt) */
796 	if (BG(CurrentStatFile)) {
797 		efree(BG(CurrentStatFile));
798 		BG(CurrentStatFile) = NULL;
799 	}
800 	if (BG(CurrentLStatFile)) {
801 		efree(BG(CurrentLStatFile));
802 		BG(CurrentLStatFile) = NULL;
803 	}
804 	if (clear_realpath_cache) {
805 		if (filename != NULL) {
806 			realpath_cache_del(filename, filename_len);
807 		} else {
808 			realpath_cache_clean();
809 		}
810 	}
811 }
812 /* }}} */
813 
814 /* {{{ proto void clearstatcache([bool clear_realpath_cache[, string filename]])
815    Clear file stat cache */
PHP_FUNCTION(clearstatcache)816 PHP_FUNCTION(clearstatcache)
817 {
818 	zend_bool  clear_realpath_cache = 0;
819 	char      *filename             = NULL;
820 	size_t     filename_len         = 0;
821 
822 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|bp", &clear_realpath_cache, &filename, &filename_len) == FAILURE) {
823 		return;
824 	}
825 
826 	php_clear_stat_cache(clear_realpath_cache, filename, (int)filename_len);
827 }
828 /* }}} */
829 
830 #define IS_LINK_OPERATION(__t) ((__t) == FS_TYPE || (__t) == FS_IS_LINK || (__t) == FS_LSTAT)
831 #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)
832 #define IS_ABLE_CHECK(__t) ((__t) == FS_IS_R || (__t) == FS_IS_W || (__t) == FS_IS_X)
833 #define IS_ACCESS_CHECK(__t) (IS_ABLE_CHECK(type) || (__t) == FS_EXISTS)
834 
835 /* {{{ php_stat
836  */
php_stat(const char * filename,php_stat_len filename_length,int type,zval * return_value)837 PHPAPI void php_stat(const char *filename, php_stat_len filename_length, int type, zval *return_value)
838 {
839 	zval stat_dev, stat_ino, stat_mode, stat_nlink, stat_uid, stat_gid, stat_rdev,
840 		 stat_size, stat_atime, stat_mtime, stat_ctime, stat_blksize, stat_blocks;
841 	zend_stat_t *stat_sb;
842 	php_stream_statbuf ssb;
843 	int flags = 0, rmask=S_IROTH, wmask=S_IWOTH, xmask=S_IXOTH; /* access rights defaults to other */
844 	char *stat_sb_names[13] = {
845 		"dev", "ino", "mode", "nlink", "uid", "gid", "rdev",
846 		"size", "atime", "mtime", "ctime", "blksize", "blocks"
847 	};
848 	const char *local;
849 	php_stream_wrapper *wrapper;
850 
851 	if (!filename_length) {
852 		RETURN_FALSE;
853 	}
854 
855 	if ((wrapper = php_stream_locate_url_wrapper(filename, &local, 0)) == &php_plain_files_wrapper && php_check_open_basedir(local)) {
856 		RETURN_FALSE;
857 	}
858 
859 	if (IS_ACCESS_CHECK(type)) {
860 		if (wrapper == &php_plain_files_wrapper) {
861 
862 			switch (type) {
863 #ifdef F_OK
864 				case FS_EXISTS:
865 					RETURN_BOOL(VCWD_ACCESS(local, F_OK) == 0);
866 					break;
867 #endif
868 #ifdef W_OK
869 				case FS_IS_W:
870 					RETURN_BOOL(VCWD_ACCESS(local, W_OK) == 0);
871 					break;
872 #endif
873 #ifdef R_OK
874 				case FS_IS_R:
875 					RETURN_BOOL(VCWD_ACCESS(local, R_OK) == 0);
876 					break;
877 #endif
878 #ifdef X_OK
879 				case FS_IS_X:
880 					RETURN_BOOL(VCWD_ACCESS(local, X_OK) == 0);
881 					break;
882 #endif
883 			}
884 		}
885 	}
886 
887 	if (IS_LINK_OPERATION(type)) {
888 		flags |= PHP_STREAM_URL_STAT_LINK;
889 	}
890 	if (IS_EXISTS_CHECK(type)) {
891 		flags |= PHP_STREAM_URL_STAT_QUIET;
892 	}
893 
894 	if (php_stream_stat_path_ex((char *)filename, flags, &ssb, NULL)) {
895 		/* Error Occurred */
896 		if (!IS_EXISTS_CHECK(type)) {
897 			php_error_docref(NULL, E_WARNING, "%sstat failed for %s", IS_LINK_OPERATION(type) ? "L" : "", filename);
898 		}
899 		RETURN_FALSE;
900 	}
901 
902 	stat_sb = &ssb.sb;
903 
904 
905 #ifndef NETWARE
906 	if (type >= FS_IS_W && type <= FS_IS_X) {
907 		if(ssb.sb.st_uid==getuid()) {
908 			rmask=S_IRUSR;
909 			wmask=S_IWUSR;
910 			xmask=S_IXUSR;
911 		} else if(ssb.sb.st_gid==getgid()) {
912 			rmask=S_IRGRP;
913 			wmask=S_IWGRP;
914 			xmask=S_IXGRP;
915 		} else {
916 			int   groups, n, i;
917 			gid_t *gids;
918 
919 			groups = getgroups(0, NULL);
920 			if(groups > 0) {
921 				gids=(gid_t *)safe_emalloc(groups, sizeof(gid_t), 0);
922 				n=getgroups(groups, gids);
923 				for(i=0;i<n;i++){
924 					if(ssb.sb.st_gid==gids[i]) {
925 						rmask=S_IRGRP;
926 						wmask=S_IWGRP;
927 						xmask=S_IXGRP;
928 						break;
929 					}
930 				}
931 				efree(gids);
932 			}
933 		}
934 	}
935 #endif
936 
937 #ifndef NETWARE
938 	if (IS_ABLE_CHECK(type) && getuid() == 0) {
939 		/* root has special perms on plain_wrapper
940 		   But we don't know about root under Netware */
941 		if (wrapper == &php_plain_files_wrapper) {
942 			if (type == FS_IS_X) {
943 				xmask = S_IXROOT;
944 			} else {
945 				RETURN_TRUE;
946 			}
947 		}
948 	}
949 #endif
950 
951 	switch (type) {
952 	case FS_PERMS:
953 		RETURN_LONG((zend_long)ssb.sb.st_mode);
954 	case FS_INODE:
955 		RETURN_LONG((zend_long)ssb.sb.st_ino);
956 	case FS_SIZE:
957 		RETURN_LONG((zend_long)ssb.sb.st_size);
958 	case FS_OWNER:
959 		RETURN_LONG((zend_long)ssb.sb.st_uid);
960 	case FS_GROUP:
961 		RETURN_LONG((zend_long)ssb.sb.st_gid);
962 	case FS_ATIME:
963 		RETURN_LONG((zend_long)ssb.sb.st_atime);
964 	case FS_MTIME:
965 		RETURN_LONG((zend_long)ssb.sb.st_mtime);
966 	case FS_CTIME:
967 		RETURN_LONG((zend_long)ssb.sb.st_ctime);
968 	case FS_TYPE:
969 		if (S_ISLNK(ssb.sb.st_mode)) {
970 			RETURN_STRING("link");
971 		}
972 		switch(ssb.sb.st_mode & S_IFMT) {
973 		case S_IFIFO: RETURN_STRING("fifo");
974 		case S_IFCHR: RETURN_STRING("char");
975 		case S_IFDIR: RETURN_STRING("dir");
976 		case S_IFBLK: RETURN_STRING("block");
977 		case S_IFREG: RETURN_STRING("file");
978 #if defined(S_IFSOCK) && !defined(ZEND_WIN32)&&!defined(__BEOS__)
979 		case S_IFSOCK: RETURN_STRING("socket");
980 #endif
981 		}
982 		php_error_docref(NULL, E_NOTICE, "Unknown file type (%d)", ssb.sb.st_mode&S_IFMT);
983 		RETURN_STRING("unknown");
984 	case FS_IS_W:
985 		RETURN_BOOL((ssb.sb.st_mode & wmask) != 0);
986 	case FS_IS_R:
987 		RETURN_BOOL((ssb.sb.st_mode&rmask)!=0);
988 	case FS_IS_X:
989 		RETURN_BOOL((ssb.sb.st_mode&xmask)!=0 && !S_ISDIR(ssb.sb.st_mode));
990 	case FS_IS_FILE:
991 		RETURN_BOOL(S_ISREG(ssb.sb.st_mode));
992 	case FS_IS_DIR:
993 		RETURN_BOOL(S_ISDIR(ssb.sb.st_mode));
994 	case FS_IS_LINK:
995 		RETURN_BOOL(S_ISLNK(ssb.sb.st_mode));
996 	case FS_EXISTS:
997 		RETURN_TRUE; /* the false case was done earlier */
998 	case FS_LSTAT:
999 		/* FALLTHROUGH */
1000 	case FS_STAT:
1001 		array_init(return_value);
1002 
1003 		ZVAL_LONG(&stat_dev, stat_sb->st_dev);
1004 		ZVAL_LONG(&stat_ino, stat_sb->st_ino);
1005 		ZVAL_LONG(&stat_mode, stat_sb->st_mode);
1006 		ZVAL_LONG(&stat_nlink, stat_sb->st_nlink);
1007 		ZVAL_LONG(&stat_uid, stat_sb->st_uid);
1008 		ZVAL_LONG(&stat_gid, stat_sb->st_gid);
1009 #ifdef HAVE_ST_RDEV
1010 # ifdef PHP_WIN32
1011 	/* It is unsigned, so if a negative came from userspace, it'll
1012 	   convert to UINT_MAX, but we wan't to keep the userspace value.
1013 	   Almost the same as in php_if_fstat. */
1014 	if ((int)stat_sb->st_rdev < 0) {
1015 		ZVAL_LONG(&stat_rdev, (int)stat_sb->st_rdev);
1016 	} else {
1017 		ZVAL_LONG(&stat_rdev, stat_sb->st_rdev);
1018 	}
1019 # else
1020 	ZVAL_LONG(&stat_rdev, stat_sb->st_rdev);
1021 # endif
1022 #else
1023 		ZVAL_LONG(&stat_rdev, -1);
1024 #endif
1025 		ZVAL_LONG(&stat_size, stat_sb->st_size);
1026 		ZVAL_LONG(&stat_atime, stat_sb->st_atime);
1027 		ZVAL_LONG(&stat_mtime, stat_sb->st_mtime);
1028 		ZVAL_LONG(&stat_ctime, stat_sb->st_ctime);
1029 #ifdef HAVE_ST_BLKSIZE
1030 		ZVAL_LONG(&stat_blksize, stat_sb->st_blksize);
1031 #else
1032 		ZVAL_LONG(&stat_blksize,-1);
1033 #endif
1034 #ifdef HAVE_ST_BLOCKS
1035 		ZVAL_LONG(&stat_blocks, stat_sb->st_blocks);
1036 #else
1037 		ZVAL_LONG(&stat_blocks,-1);
1038 #endif
1039 		/* Store numeric indexes in proper order */
1040 		zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_dev);
1041 		zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_ino);
1042 		zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_mode);
1043 		zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_nlink);
1044 		zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_uid);
1045 		zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_gid);
1046 
1047 		zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_rdev);
1048 		zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_size);
1049 		zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_atime);
1050 		zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_mtime);
1051 		zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_ctime);
1052 		zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_blksize);
1053 		zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_blocks);
1054 
1055 		/* Store string indexes referencing the same zval*/
1056 		zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[0], strlen(stat_sb_names[0]), &stat_dev);
1057 		zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[1], strlen(stat_sb_names[1]), &stat_ino);
1058 		zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[2], strlen(stat_sb_names[2]), &stat_mode);
1059 		zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[3], strlen(stat_sb_names[3]), &stat_nlink);
1060 		zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[4], strlen(stat_sb_names[4]), &stat_uid);
1061 		zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[5], strlen(stat_sb_names[5]), &stat_gid);
1062 		zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[6], strlen(stat_sb_names[6]), &stat_rdev);
1063 		zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[7], strlen(stat_sb_names[7]), &stat_size);
1064 		zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[8], strlen(stat_sb_names[8]), &stat_atime);
1065 		zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[9], strlen(stat_sb_names[9]), &stat_mtime);
1066 		zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[10], strlen(stat_sb_names[10]), &stat_ctime);
1067 		zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[11], strlen(stat_sb_names[11]), &stat_blksize);
1068 		zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[12], strlen(stat_sb_names[12]), &stat_blocks);
1069 
1070 		return;
1071 	}
1072 	php_error_docref(NULL, E_WARNING, "Didn't understand stat call");
1073 	RETURN_FALSE;
1074 }
1075 /* }}} */
1076 
1077 /* another quickie macro to make defining similar functions easier */
1078 /* {{{ FileFunction(name, funcnum) */
1079 #define FileFunction(name, funcnum) \
1080 void name(INTERNAL_FUNCTION_PARAMETERS) { \
1081 	char *filename; \
1082 	size_t filename_len; \
1083 	\
1084 	ZEND_PARSE_PARAMETERS_START(1, 1) \
1085 		Z_PARAM_PATH(filename, filename_len) \
1086 	ZEND_PARSE_PARAMETERS_END(); \
1087 	\
1088 	php_stat(filename, (php_stat_len) filename_len, funcnum, return_value); \
1089 }
1090 /* }}} */
1091 
1092 /* {{{ proto int fileperms(string filename)
1093    Get file permissions */
FileFunction(PHP_FN (fileperms),FS_PERMS)1094 FileFunction(PHP_FN(fileperms), FS_PERMS)
1095 /* }}} */
1096 
1097 /* {{{ proto int fileinode(string filename)
1098    Get file inode */
1099 FileFunction(PHP_FN(fileinode), FS_INODE)
1100 /* }}} */
1101 
1102 /* {{{ proto int filesize(string filename)
1103    Get file size */
1104 FileFunction(PHP_FN(filesize), FS_SIZE)
1105 /* }}} */
1106 
1107 /* {{{ proto int fileowner(string filename)
1108    Get file owner */
1109 FileFunction(PHP_FN(fileowner), FS_OWNER)
1110 /* }}} */
1111 
1112 /* {{{ proto int filegroup(string filename)
1113    Get file group */
1114 FileFunction(PHP_FN(filegroup), FS_GROUP)
1115 /* }}} */
1116 
1117 /* {{{ proto int fileatime(string filename)
1118    Get last access time of file */
1119 FileFunction(PHP_FN(fileatime), FS_ATIME)
1120 /* }}} */
1121 
1122 /* {{{ proto int filemtime(string filename)
1123    Get last modification time of file */
1124 FileFunction(PHP_FN(filemtime), FS_MTIME)
1125 /* }}} */
1126 
1127 /* {{{ proto int filectime(string filename)
1128    Get inode modification time of file */
1129 FileFunction(PHP_FN(filectime), FS_CTIME)
1130 /* }}} */
1131 
1132 /* {{{ proto string filetype(string filename)
1133    Get file type */
1134 FileFunction(PHP_FN(filetype), FS_TYPE)
1135 /* }}} */
1136 
1137 /* {{{ proto bool is_writable(string filename)
1138    Returns true if file can be written */
1139 FileFunction(PHP_FN(is_writable), FS_IS_W)
1140 /* }}} */
1141 
1142 /* {{{ proto bool is_readable(string filename)
1143    Returns true if file can be read */
1144 FileFunction(PHP_FN(is_readable), FS_IS_R)
1145 /* }}} */
1146 
1147 /* {{{ proto bool is_executable(string filename)
1148    Returns true if file is executable */
1149 FileFunction(PHP_FN(is_executable), FS_IS_X)
1150 /* }}} */
1151 
1152 /* {{{ proto bool is_file(string filename)
1153    Returns true if file is a regular file */
1154 FileFunction(PHP_FN(is_file), FS_IS_FILE)
1155 /* }}} */
1156 
1157 /* {{{ proto bool is_dir(string filename)
1158    Returns true if file is directory */
1159 FileFunction(PHP_FN(is_dir), FS_IS_DIR)
1160 /* }}} */
1161 
1162 /* {{{ proto bool is_link(string filename)
1163    Returns true if file is symbolic link */
1164 FileFunction(PHP_FN(is_link), FS_IS_LINK)
1165 /* }}} */
1166 
1167 /* {{{ proto bool file_exists(string filename)
1168    Returns true if filename exists */
1169 FileFunction(PHP_FN(file_exists), FS_EXISTS)
1170 /* }}} */
1171 
1172 /* {{{ proto array lstat(string filename)
1173    Give information about a file or symbolic link */
1174 FileFunction(php_if_lstat, FS_LSTAT)
1175 /* }}} */
1176 
1177 /* {{{ proto array stat(string filename)
1178    Give information about a file */
1179 FileFunction(php_if_stat, FS_STAT)
1180 /* }}} */
1181 
1182 /* {{{ proto bool realpath_cache_size()
1183    Get current size of realpath cache */
1184 PHP_FUNCTION(realpath_cache_size)
1185 {
1186 	if (zend_parse_parameters_none() == FAILURE) {
1187 		return;
1188 	}
1189 	RETURN_LONG(realpath_cache_size());
1190 }
1191 
1192 /* {{{ proto bool realpath_cache_get()
1193    Get current size of realpath cache */
PHP_FUNCTION(realpath_cache_get)1194 PHP_FUNCTION(realpath_cache_get)
1195 {
1196 	realpath_cache_bucket **buckets = realpath_cache_get_buckets(), **end = buckets + realpath_cache_max_buckets();
1197 
1198 	if (zend_parse_parameters_none() == FAILURE) {
1199 		return;
1200 	}
1201 
1202 	array_init(return_value);
1203 	while(buckets < end) {
1204 		realpath_cache_bucket *bucket = *buckets;
1205 		while(bucket) {
1206 			zval entry;
1207 
1208 			array_init(&entry);
1209 
1210 			/* bucket->key is unsigned long */
1211 			if (ZEND_LONG_MAX >= bucket->key) {
1212 				add_assoc_long_ex(&entry, "key", sizeof("key") - 1, bucket->key);
1213 			} else {
1214 				add_assoc_double_ex(&entry, "key", sizeof("key") - 1, (double)bucket->key);
1215 			}
1216 			add_assoc_bool_ex(&entry, "is_dir", sizeof("is_dir") - 1, bucket->is_dir);
1217 			add_assoc_stringl_ex(&entry, "realpath", sizeof("realpath") - 1, bucket->realpath, bucket->realpath_len);
1218 			add_assoc_long_ex(&entry, "expires", sizeof("expires") - 1, bucket->expires);
1219 #ifdef PHP_WIN32
1220 			add_assoc_bool_ex(&entry, "is_rvalid", sizeof("is_rvalid") - 1, bucket->is_rvalid);
1221 			add_assoc_bool_ex(&entry, "is_wvalid", sizeof("is_wvalid") - 1, bucket->is_wvalid);
1222 			add_assoc_bool_ex(&entry, "is_readable", sizeof("is_readable") - 1, bucket->is_readable);
1223 			add_assoc_bool_ex(&entry, "is_writable", sizeof("is_writable") - 1, bucket->is_writable);
1224 #endif
1225 			zend_hash_str_update(Z_ARRVAL_P(return_value), bucket->path, bucket->path_len, &entry);
1226 			bucket = bucket->next;
1227 		}
1228 		buckets++;
1229 	}
1230 }
1231 
1232 /*
1233  * Local variables:
1234  * tab-width: 4
1235  * c-basic-offset: 4
1236  * End:
1237  * vim600: sw=4 ts=4 fdm=marker
1238  * vim<600: sw=4 ts=4
1239  */
1240