tony2001 Fri Apr 6 22:10:57 2007 UTC Modified files: (Branch: PHP_5_2) /php-src/ext/standard filestat.c Log: MFB: clean & refactored disk_*() funcs implementation Ilia, feel free to make these functions public, though I don't see why would want to do that
http://cvs.php.net/viewvc.cgi/php-src/ext/standard/filestat.c?r1=1.136.2.8.2.11&r2=1.136.2.8.2.12&diff_format=u Index: php-src/ext/standard/filestat.c diff -u php-src/ext/standard/filestat.c:1.136.2.8.2.11 php-src/ext/standard/filestat.c:1.136.2.8.2.12 --- php-src/ext/standard/filestat.c:1.136.2.8.2.11 Mon Feb 26 14:11:34 2007 +++ php-src/ext/standard/filestat.c Fri Apr 6 22:10:56 2007 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: filestat.c,v 1.136.2.8.2.11 2007/02/26 14:11:34 tony2001 Exp $ */ +/* $Id: filestat.c,v 1.136.2.8.2.12 2007/04/06 22:10:56 tony2001 Exp $ */ #include "php.h" #include "safe_mode.h" @@ -120,14 +120,10 @@ } /* }}} */ -/* {{{ proto float disk_total_space(string path) - Get total disk space for filesystem that path is on */ -PHP_FUNCTION(disk_total_space) +static int php_disk_total_space(char *path, double *space TSRMLS_DC) /* {{{ */ +#if defined(WINDOWS) /* {{{ */ { - zval **path; -#ifdef WINDOWS - double bytestotal; - + double bytestotal = 0; HINSTANCE kernel32; FARPROC gdfse; typedef BOOL (WINAPI *gdfse_func)(LPCTSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER); @@ -144,26 +140,6 @@ DWORD NumberOfFreeClusters; DWORD TotalNumberOfClusters; -#else /* not - WINDOWS */ -#if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS) - struct statvfs buf; -#elif (defined(HAVE_SYS_STATFS_H) || defined(HAVE_SYS_MOUNT_H)) && defined(HAVE_STATFS) - struct statfs buf; -#endif - double bytestotal = 0; -#endif /* WINDOWS */ - - if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &path)==FAILURE) { - WRONG_PARAM_COUNT; - } - - convert_to_string_ex(path); - - if (php_check_open_basedir(Z_STRVAL_PP(path) TSRMLS_CC)) { - RETURN_FALSE; - } - -#ifdef WINDOWS /* GetDiskFreeSpaceEx is only available in NT and Win95 post-OSR2, so we have to jump through some hoops to see if the function exists. */ @@ -173,48 +149,63 @@ /* It's available, so we can call it. */ if (gdfse) { func = (gdfse_func)gdfse; - if (func(Z_STRVAL_PP(path), - &FreeBytesAvailableToCaller, - &TotalNumberOfBytes, - &TotalNumberOfFreeBytes) == 0) { + if (func(path, + &FreeBytesAvailableToCaller, + &TotalNumberOfBytes, + &TotalNumberOfFreeBytes) == 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", php_win_err()); - RETURN_FALSE; + return FAILURE; } /* i know - this is ugly, but i works <[EMAIL PROTECTED]> */ bytestotal = TotalNumberOfBytes.HighPart * (double) (((unsigned long)1) << 31) * 2.0 + TotalNumberOfBytes.LowPart; - } - /* If it's not available, we just use GetDiskFreeSpace */ - else { - if (GetDiskFreeSpace(Z_STRVAL_PP(path), - &SectorsPerCluster, &BytesPerSector, - &NumberOfFreeClusters, &TotalNumberOfClusters) == 0) { + } else { /* If it's not available, we just use GetDiskFreeSpace */ + if (GetDiskFreeSpace(path, + &SectorsPerCluster, &BytesPerSector, + &NumberOfFreeClusters, &TotalNumberOfClusters) == 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", php_win_err()); - RETURN_FALSE; + return FAILURE; } bytestotal = (double)TotalNumberOfClusters * (double)SectorsPerCluster * (double)BytesPerSector; } - } - else { + } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to load kernel32.dll"); - RETURN_FALSE; + return FAILURE; } + + *space = bytestotal; + return SUCCESS; +} +/* }}} */ +#elif defined(OS2) /* {{{ */ +{ + double bytestotal = 0; + FSALLOCATE fsinfo; + char drive = path[0] & 95; -#elif defined(OS2) - { - FSALLOCATE fsinfo; - char drive = Z_STRVAL_PP(path)[0] & 95; - - if (DosQueryFSInfo( drive ? drive - 64 : 0, FSIL_ALLOC, &fsinfo, sizeof( fsinfo ) ) == 0) - bytestotal = (double)fsinfo.cbSector * fsinfo.cSectorUnit * fsinfo.cUnit; + if (DosQueryFSInfo( drive ? drive - 64 : 0, FSIL_ALLOC, &fsinfo, sizeof( fsinfo ) ) == 0) { + bytestotal = (double)fsinfo.cbSector * fsinfo.cSectorUnit * fsinfo.cUnit; + *space = bytestotal; + return SUCCESS; } -#else /* WINDOWS, OS/2 */ + return FAILURE; +} +/* }}} */ +#else /* {{{ if !defined(OS2) && !defined(WINDOWS) */ +{ + double bytestotal = 0; +#if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS) + struct statvfs buf; +#elif (defined(HAVE_SYS_STATFS_H) || defined(HAVE_SYS_MOUNT_H)) && defined(HAVE_STATFS) + struct statfs buf; +#endif + #if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS) - if (statvfs(Z_STRVAL_PP(path), &buf)) { + if (statvfs(path, &buf)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno)); - RETURN_FALSE; + return FAILURE; } if (buf.f_frsize) { bytestotal = (((double)buf.f_blocks) * ((double)buf.f_frsize)); @@ -223,25 +214,47 @@ } #elif (defined(HAVE_SYS_STATFS_H) || defined(HAVE_SYS_MOUNT_H)) && defined(HAVE_STATFS) - if (statfs(Z_STRVAL_PP(path), &buf)) { + if (statfs(path, &buf)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno)); - RETURN_FALSE; + return FAILURE; } bytestotal = (((double)buf.f_bsize) * ((double)buf.f_blocks)); #endif -#endif /* WINDOWS */ - RETURN_DOUBLE(bytestotal); + *space = bytestotal; + return SUCCESS; } +#endif +/* }}} */ /* }}} */ -/* {{{ proto float disk_free_space(string path) - Get free disk space for filesystem that path is on */ -PHP_FUNCTION(disk_free_space) +/* {{{ proto float disk_total_space(string path) + Get total disk space for filesystem that path is on */ +PHP_FUNCTION(disk_total_space) { - zval **path; -#ifdef WINDOWS - double bytesfree; + double bytestotal; + char *path; + int path_len; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &path, &path_len) == FAILURE) { + return; + } + + if (php_check_open_basedir(path TSRMLS_CC)) { + RETURN_FALSE; + } + + if (php_disk_total_space(path, &bytestotal TSRMLS_CC) == SUCCESS) { + RETURN_DOUBLE(bytestotal); + } + RETURN_FALSE; +} +/* }}} */ + +static int php_disk_free_space(char *path, double *space TSRMLS_DC) /* {{{ */ +#if defined(WINDOWS) /* {{{ */ +{ + double bytesfree = 0; HINSTANCE kernel32; FARPROC gdfse; @@ -259,26 +272,6 @@ DWORD NumberOfFreeClusters; DWORD TotalNumberOfClusters; -#else /* not - WINDOWS */ -#if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS) - struct statvfs buf; -#elif (defined(HAVE_SYS_STATFS_H) || defined(HAVE_SYS_MOUNT_H)) && defined(HAVE_STATFS) - struct statfs buf; -#endif - double bytesfree = 0; -#endif /* WINDOWS */ - - if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &path)==FAILURE) { - WRONG_PARAM_COUNT; - } - - convert_to_string_ex(path); - - if (php_check_open_basedir(Z_STRVAL_PP(path) TSRMLS_CC)) { - RETURN_FALSE; - } - -#ifdef WINDOWS /* GetDiskFreeSpaceEx is only available in NT and Win95 post-OSR2, so we have to jump through some hoops to see if the function exists. */ @@ -288,48 +281,63 @@ /* It's available, so we can call it. */ if (gdfse) { func = (gdfse_func)gdfse; - if (func(Z_STRVAL_PP(path), + if (func(path, &FreeBytesAvailableToCaller, &TotalNumberOfBytes, &TotalNumberOfFreeBytes) == 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", php_win_err()); - RETURN_FALSE; + return FAILURE; } /* i know - this is ugly, but i works <[EMAIL PROTECTED]> */ bytesfree = FreeBytesAvailableToCaller.HighPart * (double) (((unsigned long)1) << 31) * 2.0 + FreeBytesAvailableToCaller.LowPart; - } - /* If it's not available, we just use GetDiskFreeSpace */ - else { - if (GetDiskFreeSpace(Z_STRVAL_PP(path), + } else { /* If it's not available, we just use GetDiskFreeSpace */ + if (GetDiskFreeSpace(path, &SectorsPerCluster, &BytesPerSector, &NumberOfFreeClusters, &TotalNumberOfClusters) == 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", php_win_err()); - RETURN_FALSE; + return FAILURE; } bytesfree = (double)NumberOfFreeClusters * (double)SectorsPerCluster * (double)BytesPerSector; } - } - else { + } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to load kernel32.dll"); - RETURN_FALSE; + return FAILURE; } -#elif defined(OS2) - { - FSALLOCATE fsinfo; - char drive = Z_STRVAL_PP(path)[0] & 95; + *space = bytesfree; + return SUCCESS; +} +/* }}} */ +#elif defined(OS2) /* {{{ */ +{ + double bytesfree = 0; + FSALLOCATE fsinfo; + char drive = path[0] & 95; + + if (DosQueryFSInfo( drive ? drive - 64 : 0, FSIL_ALLOC, &fsinfo, sizeof( fsinfo ) ) == 0) { + bytesfree = (double)fsinfo.cbSector * fsinfo.cSectorUnit * fsinfo.cUnitAvail; + *space = bytesfree; + return SUCCESS; + } + return FAILURE; +} +/* }}} */ +#else /* {{{ if !defined(OS2) && !defined(WINDOWS) */ +{ + double bytesfree = 0; +#if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS) + struct statvfs buf; +#elif (defined(HAVE_SYS_STATFS_H) || defined(HAVE_SYS_MOUNT_H)) && defined(HAVE_STATFS) + struct statfs buf; +#endif - if (DosQueryFSInfo( drive ? drive - 64 : 0, FSIL_ALLOC, &fsinfo, sizeof( fsinfo ) ) == 0) - bytesfree = (double)fsinfo.cbSector * fsinfo.cSectorUnit * fsinfo.cUnitAvail; - } -#else /* WINDOWS, OS/2 */ #if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS) - if (statvfs(Z_STRVAL_PP(path), &buf)) { + if (statvfs(path, &buf)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno)); - RETURN_FALSE; + return FAILURE; } if (buf.f_frsize) { bytesfree = (((double)buf.f_bavail) * ((double)buf.f_frsize)); @@ -337,9 +345,9 @@ bytesfree = (((double)buf.f_bavail) * ((double)buf.f_bsize)); } #elif (defined(HAVE_SYS_STATFS_H) || defined(HAVE_SYS_MOUNT_H)) && defined(HAVE_STATFS) - if (statfs(Z_STRVAL_PP(path), &buf)) { + if (statfs(path, &buf)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno)); - RETURN_FALSE; + return FAILURE; } #ifdef NETWARE bytesfree = (((double)buf.f_bsize) * ((double)buf.f_bfree)); @@ -347,9 +355,34 @@ bytesfree = (((double)buf.f_bsize) * ((double)buf.f_bavail)); #endif #endif -#endif /* WINDOWS */ + + *space = bytesfree; + return SUCCESS; +} +#endif +/* }}} */ +/* }}} */ + +/* {{{ proto float disk_free_space(string path) + Get free disk space for filesystem that path is on */ +PHP_FUNCTION(disk_free_space) +{ + double bytesfree; + char *path; + int path_len; - RETURN_DOUBLE(bytesfree); + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &path, &path_len) == FAILURE) { + return; + } + + if (php_check_open_basedir(path TSRMLS_CC)) { + RETURN_FALSE; + } + + if (php_disk_free_space(path, &bytesfree TSRMLS_CC) == SUCCESS) { + RETURN_DOUBLE(bytesfree); + } + RETURN_FALSE; } /* }}} */
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php