filestat.c 34 KB

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