cvs: php4 /ext/standard basic_functions.c filestat.c php_filestat.h

From: Date: Tue, 15 May 2001 23:43:18 +0000
Subject: cvs: php4 /ext/standard basic_functions.c filestat.c php_filestat.h
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-5764@lists.php.net to get a copy of this message
jon Tue May 15 16:43:18 2001 EDT Modified files: /php4/ext/standard basic_functions.c filestat.c php_filestat.h Log: @ - Added disk_total_space() to return the total size of a filesystem. @ (Patch from Steven Bower) Index: php4/ext/standard/basic_functions.c diff -u php4/ext/standard/basic_functions.c:1.338 php4/ext/standard/basic_functions.c:1.339 --- php4/ext/standard/basic_functions.c:1.338 Tue May 15 16:28:17 2001 +++ php4/ext/standard/basic_functions.c Tue May 15 16:43:18 2001 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: basic_functions.c,v 1.338 2001/05/15 23:28:17 jon Exp $ */ +/* $Id: basic_functions.c,v 1.339 2001/05/15 23:43:18 jon Exp $ */ #include "php.h" #include "php_main.h" @@ -505,6 +505,7 @@ 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) Index: php4/ext/standard/filestat.c diff -u php4/ext/standard/filestat.c:1.66 php4/ext/standard/filestat.c:1.67 --- php4/ext/standard/filestat.c:1.66 Tue May 15 16:28:17 2001 +++ php4/ext/standard/filestat.c Tue May 15 16:43:18 2001 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: filestat.c,v 1.66 2001/05/15 23:28:17 jon Exp $ */ +/* $Id: filestat.c,v 1.67 2001/05/15 23:43:18 jon Exp $ */ #include "php.h" #include "safe_mode.h" @@ -123,6 +123,107 @@ } 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 */ Index: php4/ext/standard/php_filestat.h diff -u php4/ext/standard/php_filestat.h:1.11 php4/ext/standard/php_filestat.h:1.12 --- php4/ext/standard/php_filestat.h:1.11 Tue May 15 16:28:17 2001 +++ php4/ext/standard/php_filestat.h Tue May 15 16:43:18 2001 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_filestat.h,v 1.11 2001/05/15 23:28:17 jon Exp $ */ +/* $Id: php_filestat.h,v 1.12 2001/05/15 23:43:18 jon Exp $ */ #ifndef PHP_FILESTAT_H #define PHP_FILESTAT_H @@ -43,6 +43,7 @@ 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);

« previous php.cvs (#5764) next »