]> granicus.if.org Git - php/commitdiff
simplify disk_*_space() functions sources by moving ifdef'ed implementations into...
authorAntony Dovgal <tony2001@php.net>
Mon, 26 Feb 2007 20:35:41 +0000 (20:35 +0000)
committerAntony Dovgal <tony2001@php.net>
Mon, 26 Feb 2007 20:35:41 +0000 (20:35 +0000)
ext/standard/filestat.c

index e8e282cf57c9f91eb99b902ecfa297e480f0338c..f0bd70e33c2ee16032a27378dc42f72aded38290 100644 (file)
@@ -120,16 +120,10 @@ PHP_RSHUTDOWN_FUNCTION(filestat) /* {{{ */
 }
 /* }}} */
 
-/* {{{ 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 @@ 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_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 @@ PHP_FUNCTION(disk_total_space)
                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 <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 {
+               } 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 @@ PHP_FUNCTION(disk_total_space)
                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 @@ totalspace_failure:
 }
 /* }}} */
 
-/* {{{ 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 @@ 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_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 @@ PHP_FUNCTION(disk_free_space)
                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 <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 {
+               } 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 */
+       *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;
+       if (DosQueryFSInfo( drive ? drive - 64 : 0, FSIL_ALLOC, &fsinfo, sizeof( fsinfo ) ) == 0) {
+               bytesfree = (double)fsinfo.cbSector * fsinfo.cSectorUnit * fsinfo.cUnitAvail;
+               *space = bytesfree;
+               return SUCCESS;
        }
-#else /* !WINDOWS, !OS/2 */
+       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
 
-/* *nix Implementation */
-# 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 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) {