tony2001 Mon Feb 26 20:35:41 2007 UTC Modified files: /php-src/ext/standard filestat.c Log: simplify disk_*_space() functions sources by moving ifdef'ed implementations into separate functions
http://cvs.php.net/viewvc.cgi/php-src/ext/standard/filestat.c?r1=1.159&r2=1.160&diff_format=u Index: php-src/ext/standard/filestat.c diff -u php-src/ext/standard/filestat.c:1.159 php-src/ext/standard/filestat.c:1.160 --- php-src/ext/standard/filestat.c:1.159 Mon Feb 26 14:11:14 2007 +++ php-src/ext/standard/filestat.c Mon Feb 26 20:35:41 2007 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: filestat.c,v 1.159 2007/02/26 14:11:14 tony2001 Exp $ */ +/* $Id: filestat.c,v 1.160 2007/02/26 20:35:41 tony2001 Exp $ */ #include "php.h" #include "fopen_wrappers.h" @@ -120,16 +120,10 @@ } /* }}} */ -/* {{{ proto float disk_total_space(string path) U - 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) /* {{{ */ { - char *path; - int path_len; - zend_uchar path_type; -#ifdef WINDOWS - double bytestotal; - + double bytestotal = 0; HINSTANCE kernel32; FARPROC gdfse; typedef BOOL (WINAPI *gdfse_func)(LPCTSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER); @@ -146,31 +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_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "t", &path, &path_len, &path_type) == FAILURE) { - return; - } - - if (path_type == IS_UNICODE) { - if (FAILURE == php_stream_path_encode(NULL, &path, &path_len, (UChar*)path, path_len, REPORT_ERRORS, FG(default_context))) { - RETURN_FALSE; - } - } - - if (php_check_open_basedir(path TSRMLS_CC)) { - goto totalspace_failure; - } - -/* OS Selection */ -#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. */ @@ -181,50 +150,62 @@ if (gdfse) { func = (gdfse_func)gdfse; if (func(path, - &FreeBytesAvailableToCaller, - &TotalNumberOfBytes, - &TotalNumberOfFreeBytes) == 0) { + &FreeBytesAvailableToCaller, + &TotalNumberOfBytes, + &TotalNumberOfFreeBytes) == 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", php_win_err()); - goto totalspace_failure; + 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 { + } else { /* If it's not available, we just use GetDiskFreeSpace */ if (GetDiskFreeSpace(path, - &SectorsPerCluster, &BytesPerSector, - &NumberOfFreeClusters, &TotalNumberOfClusters) == 0) { + &SectorsPerCluster, &BytesPerSector, + &NumberOfFreeClusters, &TotalNumberOfClusters) == 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", php_win_err()); - goto totalspace_failure; + return FAILURE; } bytestotal = (double)TotalNumberOfClusters * (double)SectorsPerCluster * (double)BytesPerSector; } - } - else { + } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to load kernel32.dll"); - goto totalspace_failure; + return FAILURE; } -#elif defined(OS2) - { - FSALLOCATE fsinfo; - char drive = path[0] & 0x5F; /* ascii ucase */ + *space = bytestotal; + return SUCCESS; +} +/* }}} */ +#elif defined(OS2) /* {{{ */ +{ + double bytestotal = 0; + FSALLOCATE fsinfo; + char drive = 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 -/* *nix Implmentations */ -# if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS) - if (statvfs(path, &buf)) { +#if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS) + if (statvfs(path, &buf)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno)); - goto totalspace_failure; + return FAILURE; } if (buf.f_frsize) { bytestotal = (((double)buf.f_blocks) * ((double)buf.f_frsize)); @@ -232,24 +213,50 @@ bytestotal = (((double)buf.f_blocks) * ((double)buf.f_bsize)); } -# elif (defined(HAVE_SYS_STATFS_H) || defined(HAVE_SYS_MOUNT_H)) && defined(HAVE_STATFS) - if (statfs(path, &buf)) { +#elif (defined(HAVE_SYS_STATFS_H) || defined(HAVE_SYS_MOUNT_H)) && defined(HAVE_STATFS) + if (statfs(path, &buf)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno)); - goto totalspace_failure; + return FAILURE; } bytestotal = (((double)buf.f_bsize) * ((double)buf.f_blocks)); -# else /* No implementation avail */ - php_error_docref(NULL TSRMLS_CC, E_WARNING, "System does not support disk_total_space()"); - goto totalspace_failure; -# endif /* *nix Implementations */ +#endif + *space = bytestotal; + return SUCCESS; +} +#endif +/* }}} */ +/* }}} */ + +/* {{{ proto float disk_total_space(string path) U + Get total disk space for filesystem that path is on */ +PHP_FUNCTION(disk_total_space) +{ + char *path; + int path_len; + zend_uchar path_type; + double bytestotal; -#endif /* OS selection */ + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "t", &path, &path_len, &path_type) == FAILURE) { + return; + } if (path_type == IS_UNICODE) { - efree(path); + if (FAILURE == php_stream_path_encode(NULL, &path, &path_len, (UChar*)path, path_len, REPORT_ERRORS, FG(default_context))) { + RETURN_FALSE; + } + } + + if (php_check_open_basedir(path TSRMLS_CC)) { + goto totalspace_failure; + } + + if (php_disk_total_space(path, &bytestotal TSRMLS_CC) == SUCCESS) { + if (path_type == IS_UNICODE) { + efree(path); + } + RETURN_DOUBLE(bytestotal); } - RETURN_DOUBLE(bytestotal); totalspace_failure: if (path_type == IS_UNICODE) { @@ -259,15 +266,10 @@ } /* }}} */ -/* {{{ proto float disk_free_space(string path) U - Get free disk space for filesystem that path is on */ -PHP_FUNCTION(disk_free_space) +static int php_disk_free_space(char *path, double *space TSRMLS_DC) /* {{{ */ +#if defined(WINDOWS) /* {{{ */ { - char *path; - int path_len; - zend_uchar path_type; -#ifdef WINDOWS - double bytesfree; + double bytesfree = 0; HINSTANCE kernel32; FARPROC gdfse; @@ -285,31 +287,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_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "t", &path, &path_len, &path_type) == FAILURE) { - return; - } - - if (path_type == IS_UNICODE) { - if (FAILURE == php_stream_path_encode(NULL, &path, &path_len, (UChar*)path, path_len, REPORT_ERRORS, FG(default_context))) { - RETURN_FALSE; - } - } - - if (php_check_open_basedir(path TSRMLS_CC)) { - goto freespace_failure; - } - -/* OS Selection */ -#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. */ @@ -320,79 +297,117 @@ if (gdfse) { func = (gdfse_func)gdfse; if (func(path, - &FreeBytesAvailableToCaller, - &TotalNumberOfBytes, - &TotalNumberOfFreeBytes) == 0) { + &FreeBytesAvailableToCaller, + &TotalNumberOfBytes, + &TotalNumberOfFreeBytes) == 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", php_win_err()); - goto freespace_failure; + 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 { + } else { /* If it's not available, we just use GetDiskFreeSpace */ if (GetDiskFreeSpace(path, - &SectorsPerCluster, &BytesPerSector, - &NumberOfFreeClusters, &TotalNumberOfClusters) == 0) { + &SectorsPerCluster, &BytesPerSector, + &NumberOfFreeClusters, &TotalNumberOfClusters) == 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", php_win_err()); - goto freespace_failure; + return FAILURE; } bytesfree = (double)NumberOfFreeClusters * (double)SectorsPerCluster * (double)BytesPerSector; } - } - else { + } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to load kernel32.dll"); - goto freespace_failure; + return FAILURE; } -#elif defined(OS2) - { - FSALLOCATE fsinfo; - char drive = path[0] & 0x5F; /* ascii ucase */ - - if (DosQueryFSInfo( drive ? drive - 64 : 0, FSIL_ALLOC, &fsinfo, sizeof( fsinfo ) ) == 0) - bytesfree = (double)fsinfo.cbSector * fsinfo.cSectorUnit * fsinfo.cUnitAvail; - } -#else /* !WINDOWS, !OS/2 */ - -/* *nix Implementation */ -# if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS) - if (statvfs(path, &buf)) { + *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 defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS) + if (statvfs(path, &buf)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno)); - goto freespace_failure; + return FAILURE; } if (buf.f_frsize) { bytesfree = (((double)buf.f_bavail) * ((double)buf.f_frsize)); } else { bytesfree = (((double)buf.f_bavail) * ((double)buf.f_bsize)); } -# elif (defined(HAVE_SYS_STATFS_H) || defined(HAVE_SYS_MOUNT_H)) && defined(HAVE_STATFS) +#elif (defined(HAVE_SYS_STATFS_H) || defined(HAVE_SYS_MOUNT_H)) && defined(HAVE_STATFS) if (statfs(path, &buf)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno)); - goto freespace_failure; + return FAILURE; } -# ifdef NETWARE +#ifdef NETWARE bytesfree = (((double)buf.f_bsize) * ((double)buf.f_bfree)); -# else +#else bytesfree = (((double)buf.f_bsize) * ((double)buf.f_bavail)); -# endif +#endif +#endif -# else /* No *nix Implemetation */ - php_error_docref(NULL TSRMLS_CC, E_WARNING, "System does not support disk_free_space()"); - goto freespace_failure; + *space = bytesfree; + return SUCCESS; +} +#endif +/* }}} */ +/* }}} */ -# endif /* *nix Implementation */ +/* {{{ proto float disk_free_space(string path) U + Get free disk space for filesystem that path is on */ +PHP_FUNCTION(disk_free_space) +{ + char *path; + int path_len; + zend_uchar path_type; + double bytesfree; -#endif /* OS Selection */ + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "t", &path, &path_len, &path_type) == FAILURE) { + return; + } if (path_type == IS_UNICODE) { - efree(path); + if (FAILURE == php_stream_path_encode(NULL, &path, &path_len, (UChar*)path, path_len, REPORT_ERRORS, FG(default_context))) { + RETURN_FALSE; + } + } + + if (php_check_open_basedir(path TSRMLS_CC)) { + goto freespace_failure; + } + + if (php_disk_free_space(path, &bytesfree TSRMLS_CC) == SUCCESS) { + if (path_type == IS_UNICODE) { + efree(path); + } + RETURN_DOUBLE(bytesfree); } - RETURN_DOUBLE(bytesfree); freespace_failure: if (path_type == IS_UNICODE) {
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php