From: Jon Parise Date: Tue, 15 May 2001 23:43:18 +0000 (+0000) Subject: @ - Added disk_total_space() to return the total size of a filesystem. X-Git-Tag: PRE_GRANULAR_GARBAGE_FIX~403 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=82b31fb7a3c28bf752c56ef8ba76f81859b42901;p=php @ - Added disk_total_space() to return the total size of a filesystem. @ (Patch from Steven Bower) --- diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index c73a073f5a..355e4cff8f 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -505,6 +505,7 @@ function_entry basic_functions[] = { PHP_FE(chmod, NULL) PHP_FE(touch, NULL) PHP_FE(clearstatcache, NULL) + PHP_FE(disk_total_space, NULL) PHP_FE(disk_free_space, NULL) PHP_FALIAS(diskfreespace, disk_free_space, NULL) diff --git a/ext/standard/filestat.c b/ext/standard/filestat.c index 54d1b0ca8b..ee19e0ceb2 100644 --- a/ext/standard/filestat.c +++ b/ext/standard/filestat.c @@ -124,6 +124,107 @@ PHP_RSHUTDOWN_FUNCTION(filestat) return SUCCESS; } +/* {{{ proto double disk_total_space(string path) + Get total disk space for filesystem that path is on */ +PHP_FUNCTION(disk_total_space) +{ + pval **path; +#ifdef WINDOWS + double bytestotal; + + HINSTANCE kernel32; + FARPROC gdfse; + typedef BOOL (WINAPI *gdfse_func)(LPCTSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER); + gdfse_func func; + + /* These are used by GetDiskFreeSpaceEx, if available. */ + ULARGE_INTEGER FreeBytesAvailableToCaller; + ULARGE_INTEGER TotalNumberOfBytes; + ULARGE_INTEGER TotalNumberOfFreeBytes; + + /* These are used by GetDiskFreeSpace otherwise. */ + DWORD SectorsPerCluster; + DWORD BytesPerSector; + 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_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((*path)->value.str.val)) 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. */ + kernel32 = LoadLibrary("kernel32.dll"); + if (kernel32) { + gdfse = GetProcAddress(kernel32, "GetDiskFreeSpaceExA"); + /* It's available, so we can call it. */ + if (gdfse) { + func = (gdfse_func)gdfse; + if (func((*path)->value.str.val, + &FreeBytesAvailableToCaller, + &TotalNumberOfBytes, + &TotalNumberOfFreeBytes) == 0) RETURN_FALSE; + + /* i know - this is ugly, but i works (thies@thieso.net) */ + bytestotal = TotalNumberOfBytes.HighPart * + (double) (((unsigned long)1) << 31) * 2.0 + + TotalNumberOfBytes.LowPart; + } + /* If it's not available, we just use GetDiskFreeSpace */ + else { + if (GetDiskFreeSpace((*path)->value.str.val, + &SectorsPerCluster, &BytesPerSector, + &NumberOfFreeClusters, &TotalNumberOfClusters) == 0) RETURN_FALSE; + bytestotal = (double)TotalNumberOfClusters * (double)SectorsPerCluster * (double)BytesPerSector; + } + } + else { + php_error(E_WARNING, "Unable to load kernel32.dll"); + RETURN_FALSE; + } + +#elif defined(OS2) + { + FSALLOCATE fsinfo; + char drive = (*path)->value.str.val[0] & 95; + + if (DosQueryFSInfo( drive ? drive - 64 : 0, FSIL_ALLOC, &fsinfo, sizeof( fsinfo ) ) == 0) + bytestotal = (double)fsinfo.cbSector * fsinfo.cSectorUnit * fsinfo.cUnit; + } +#else /* WINDOWS, OS/2 */ +#if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS) + if (statvfs((*path)->value.str.val,&buf)) RETURN_FALSE; + if (buf.f_frsize) { + bytestotal = (((double)buf.f_blocks) * ((double)buf.f_frsize)); + } else { + bytestotal = (((double)buf.f_blocks) * ((double)buf.f_bsize)); + } + +#elif defined(HAVE_SYS_STATFS_H) && defined(HAVE_STATFS) + if (statfs((*path)->value.str.val,&buf)) RETURN_FALSE; + bytestotal = (((double)buf.f_bsize) * ((double)buf.f_blocks)); +#endif +#endif /* WINDOWS */ + + RETURN_DOUBLE(bytestotal); +} +/* }}} */ + /* {{{ proto double disk_free_space(string path) Get free disk space for filesystem that path is on */ PHP_FUNCTION(disk_free_space) diff --git a/ext/standard/php_filestat.h b/ext/standard/php_filestat.h index 57e30c35b7..348324163f 100644 --- a/ext/standard/php_filestat.h +++ b/ext/standard/php_filestat.h @@ -43,6 +43,7 @@ PHP_FUNCTION(is_link); PHP_FUNCTION(file_exists); PHP_NAMED_FUNCTION(php_if_stat); PHP_NAMED_FUNCTION(php_if_lstat); +PHP_FUNCTION(disk_total_space); PHP_FUNCTION(disk_free_space); PHP_FUNCTION(chown); PHP_FUNCTION(chgrp);