]> granicus.if.org Git - php/commitdiff
MFB: clean & refactored disk_*() funcs implementation
authorAntony Dovgal <tony2001@php.net>
Fri, 6 Apr 2007 22:10:56 +0000 (22:10 +0000)
committerAntony Dovgal <tony2001@php.net>
Fri, 6 Apr 2007 22:10:56 +0000 (22:10 +0000)
Ilia, feel free to make these functions public, though I don't see why would want to do that

ext/standard/filestat.c

index cba61271bdeec496d291762ab1e68ed63166c717..db25b9df6545f9b3b80d571fa75065ab29a9da5c 100644 (file)
@@ -120,14 +120,10 @@ PHP_RSHUTDOWN_FUNCTION(filestat) /* {{{ */
 }
 /* }}} */
 
-/* {{{ 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 @@ PHP_FUNCTION(disk_total_space)
        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 @@ PHP_FUNCTION(disk_total_space)
                /* 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 <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(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 @@ PHP_FUNCTION(disk_total_space)
        }
 
 #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 @@ PHP_FUNCTION(disk_free_space)
        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 @@ PHP_FUNCTION(disk_free_space)
                /* 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 <thies@thieso.net> */
                        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 @@ PHP_FUNCTION(disk_free_space)
                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 @@ PHP_FUNCTION(disk_free_space)
        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;
 }
 /* }}} */