From 66f1984420ff4539bcb730f7724f5dc8c773c642 Mon Sep 17 00:00:00 2001 From: "dougt%netscape.com" Date: Thu, 9 Sep 1999 19:20:35 +0000 Subject: [PATCH] nsFileSpec::GetDiskSpace now returns a PRUint64. (12479) thanks to Chet Murphy for the windows code! git-svn-id: svn://10.0.0.236/trunk@46590 18797224-902f-48f8-a5cc-f745e15eee43 --- .../pref-migrator/src/nsPrefMigration.cpp | 2 +- .../src/xpwidgets/nsFileSpecWithUIImpl.h | 2 +- .../src/xpwidgets/nsFileWidgetWithUIImpl.cpp | 2 +- mozilla/xpcom/io/nsFileSpec.h | 4 +- mozilla/xpcom/io/nsFileSpecBeOS.cpp | 16 +++-- mozilla/xpcom/io/nsFileSpecImpl.cpp | 2 +- mozilla/xpcom/io/nsFileSpecMac.cpp | 11 +++- mozilla/xpcom/io/nsFileSpecOS2.cpp | 8 ++- mozilla/xpcom/io/nsFileSpecUnix.cpp | 13 ++-- mozilla/xpcom/io/nsFileSpecWin.cpp | 62 +++++++++++++------ mozilla/xpcom/io/nsIFile.idl | 2 +- mozilla/xpinstall/src/nsInstall.cpp | 4 +- mozilla/xpinstall/src/nsInstall.h | 4 +- mozilla/xpinstall/src/nsJSInstall.cpp | 4 +- mozilla/xpinstall/src/nsSoftwareUpdateRun.cpp | 2 +- 15 files changed, 90 insertions(+), 48 deletions(-) diff --git a/mozilla/profile/pref-migrator/src/nsPrefMigration.cpp b/mozilla/profile/pref-migrator/src/nsPrefMigration.cpp index d33d98a972b..6fdbc3cc39d 100644 --- a/mozilla/profile/pref-migrator/src/nsPrefMigration.cpp +++ b/mozilla/profile/pref-migrator/src/nsPrefMigration.cpp @@ -536,7 +536,7 @@ nsPrefMigration::CheckForSpace(nsFileSpec newProfilePath, PRFloat64 requiredSpac { // nsFileSpec drive(newProfilePath); - if (newProfilePath.GetDiskSpaceAvailable() < requiredSpace) + if (LL_CMP(newProfilePath.GetDiskSpaceAvailable(), <, requiredSpace)) return NS_ERROR_FAILURE; return NS_OK; } diff --git a/mozilla/widget/src/xpwidgets/nsFileSpecWithUIImpl.h b/mozilla/widget/src/xpwidgets/nsFileSpecWithUIImpl.h index 0d3bb4467ec..e8c1c33d29d 100644 --- a/mozilla/widget/src/xpwidgets/nsFileSpecWithUIImpl.h +++ b/mozilla/widget/src/xpwidgets/nsFileSpecWithUIImpl.h @@ -143,7 +143,7 @@ class nsFileSpecWithUIImpl { return mBaseFileSpec ? mBaseFileSpec->GetFileSize(aFileSize) : NS_ERROR_NOT_INITIALIZED; } /* readonly attribute unsigned long DiskSpaceAvailable; */ - NS_IMETHOD GetDiskSpaceAvailable(PRUint32 *aDiskSpaceAvailable) + NS_IMETHOD GetDiskSpaceAvailable(PRUint64 *aDiskSpaceAvailable) { return mBaseFileSpec ? mBaseFileSpec->GetDiskSpaceAvailable(aDiskSpaceAvailable) : NS_ERROR_NOT_INITIALIZED; } /* nsIFileSpec AppendRelativeUnixPath (in string relativePath); */ diff --git a/mozilla/widget/src/xpwidgets/nsFileWidgetWithUIImpl.cpp b/mozilla/widget/src/xpwidgets/nsFileWidgetWithUIImpl.cpp index 30583fabc71..831a347bba0 100644 --- a/mozilla/widget/src/xpwidgets/nsFileWidgetWithUIImpl.cpp +++ b/mozilla/widget/src/xpwidgets/nsFileWidgetWithUIImpl.cpp @@ -93,7 +93,7 @@ class nsFileSpecWithUIImpl NS_IMETHOD GetFileSize(PRUint32 *aFileSize); /* readonly attribute unsigned long DiskSpaceAvailable; */ - NS_IMETHOD GetDiskSpaceAvailable(PRUint32 *aDiskSpaceAvailable); + NS_IMETHOD GetDiskSpaceAvailable(PRUint64 *aDiskSpaceAvailable); /* nsIFileSpec AppendRelativeUnixPath (in string relativePath); */ NS_IMETHOD AppendRelativeUnixPath(const char *relativePath); diff --git a/mozilla/xpcom/io/nsFileSpec.h b/mozilla/xpcom/io/nsFileSpec.h index 90c2c0380b6..dba01edb2f8 100644 --- a/mozilla/xpcom/io/nsFileSpec.h +++ b/mozilla/xpcom/io/nsFileSpec.h @@ -112,7 +112,7 @@ #include "nsError.h" #include "nsString.h" #include "nsCRT.h" - +#include "prtypes.h" //======================================================================================== // Compiler-specific macros, as needed //======================================================================================== @@ -418,7 +418,7 @@ class NS_COM nsFileSpec } PRUint32 GetFileSize() const; - PRUint32 GetDiskSpaceAvailable() const; + PRUint64 GetDiskSpaceAvailable() const; nsFileSpec operator + (const char* inRelativeUnixPath) const; nsFileSpec operator + (const nsString& inRelativeUnixPath) const diff --git a/mozilla/xpcom/io/nsFileSpecBeOS.cpp b/mozilla/xpcom/io/nsFileSpecBeOS.cpp index ece08d54507..0a131c8c691 100644 --- a/mozilla/xpcom/io/nsFileSpecBeOS.cpp +++ b/mozilla/xpcom/io/nsFileSpecBeOS.cpp @@ -421,33 +421,37 @@ nsresult nsFileSpec::Execute(const char* inArgs ) const } // nsFileSpec::Execute //---------------------------------------------------------------------------------------- -PRUint32 nsFileSpec::GetDiskSpaceAvailable() const +PRUint64 nsFileSpec::GetDiskSpaceAvailable() const //---------------------------------------------------------------------------------------- { + PRUint64 int64; + + LL_I2L(int64 , ULONG_MAX); + char curdir [MAXPATHLEN]; if (!mPath || !*mPath) { (void) getcwd(curdir, MAXPATHLEN); if (!curdir) - return ULONG_MAX; /* hope for the best as we did in cheddar */ + return int64; /* hope for the best as we did in cheddar */ } else sprintf(curdir, "%.200s", (const char*)mPath); BEntry e(curdir); if(e.InitCheck() != B_OK) - return ULONG_MAX; /* hope for the best as we did in cheddar */ + return int64; /* hope for the best as we did in cheddar */ entry_ref ref; e.GetRef(&ref); BVolume v(ref.device); // HACK!!! - PRUint32 space = v.FreeBytes() > (int64)(ULONG_MAX) ? ULONG_MAX : (int32)v.FreeBytes(); - + LL_I2L(int64 , (v.FreeBytes() > (int64)(ULONG_MAX) ? ULONG_MAX : (int32)v.FreeBytes())); + #ifdef DEBUG_DISK_SPACE printf("DiskSpaceAvailable: %d bytes\n", space); #endif - return space; + return int64; } // nsFileSpec::GetDiskSpace() //======================================================================================== diff --git a/mozilla/xpcom/io/nsFileSpecImpl.cpp b/mozilla/xpcom/io/nsFileSpecImpl.cpp index 75a473f9f21..fd5de52f805 100644 --- a/mozilla/xpcom/io/nsFileSpecImpl.cpp +++ b/mozilla/xpcom/io/nsFileSpecImpl.cpp @@ -344,7 +344,7 @@ NS_IMETHODIMP nsFileSpecImpl::GetFileSize(PRUint32 *aFileSize) } //---------------------------------------------------------------------------------------- -NS_IMETHODIMP nsFileSpecImpl::GetDiskSpaceAvailable(PRUint32 *aDiskSpaceAvailable) +NS_IMETHODIMP nsFileSpecImpl::GetDiskSpaceAvailable(PRUint64 *aDiskSpaceAvailable) //---------------------------------------------------------------------------------------- { TEST_OUT_PTR(aDiskSpaceAvailable) diff --git a/mozilla/xpcom/io/nsFileSpecMac.cpp b/mozilla/xpcom/io/nsFileSpecMac.cpp index 69a007d7b31..0bdc12916c6 100644 --- a/mozilla/xpcom/io/nsFileSpecMac.cpp +++ b/mozilla/xpcom/io/nsFileSpecMac.cpp @@ -1105,9 +1105,13 @@ OSErr nsFileSpec::GetFileTypeAndCreator(OSType* type, OSType* creator) //---------------------------------------------------------------------------------------- -PRUint32 nsFileSpec::GetDiskSpaceAvailable() const +PRUint64 nsFileSpec::GetDiskSpaceAvailable() const //---------------------------------------------------------------------------------------- { + PRUint64 int64; + + LL_I2L(int64 , ULONG_MAX); + HVolumeParam pb; pb.ioCompletion = nsnull; pb.ioVolIndex = 0; @@ -1117,8 +1121,9 @@ PRUint32 nsFileSpec::GetDiskSpaceAvailable() const OSErr err = PBHGetVInfoSync( (HParmBlkPtr)&pb ); if ( err == noErr ) - return pb.ioVFrBlk * pb.ioVAlBlkSiz; - return ULONG_MAX; + LL_I2L(int64 , (pb.ioVFrBlk * pb.ioVAlBlkSiz)); + + return int64; } // nsFileSpec::GetDiskSpace() //---------------------------------------------------------------------------------------- diff --git a/mozilla/xpcom/io/nsFileSpecOS2.cpp b/mozilla/xpcom/io/nsFileSpecOS2.cpp index 1b119fb523f..d5311f7035b 100644 --- a/mozilla/xpcom/io/nsFileSpecOS2.cpp +++ b/mozilla/xpcom/io/nsFileSpecOS2.cpp @@ -244,7 +244,7 @@ PRUint32 nsFileSpec::GetFileSize() const // Okay, this is a really weird place to put this method! // And it ought to return a PRUint64. // -PRUint32 nsFileSpec::GetDiskSpaceAvailable() const +PRUint64 nsFileSpec::GetDiskSpaceAvailable() const { ULONG ulDriveNo = toupper(mPath[0]) + 1 - 'A'; FSALLOCATE fsAllocate = { 0 }; @@ -261,8 +261,12 @@ PRUint32 nsFileSpec::GetDiskSpaceAvailable() const fsAllocate.cSectorUnit * fsAllocate.cbSector; } + + PRUint64 int64; - return cbAvail; + LL_I2L(int64 , cbAvail); + + return int64; } void nsFileSpec::GetParent( nsFileSpec &outSpec) const diff --git a/mozilla/xpcom/io/nsFileSpecUnix.cpp b/mozilla/xpcom/io/nsFileSpecUnix.cpp index a81a326b79d..40d145d6924 100644 --- a/mozilla/xpcom/io/nsFileSpecUnix.cpp +++ b/mozilla/xpcom/io/nsFileSpecUnix.cpp @@ -474,9 +474,11 @@ nsresult nsFileSpec::Execute(const char* inArgs ) const } // nsFileSpec::Execute //---------------------------------------------------------------------------------------- -PRUint32 nsFileSpec::GetDiskSpaceAvailable() const +PRUint64 nsFileSpec::GetDiskSpaceAvailable() const //---------------------------------------------------------------------------------------- { + PRUint64 int64; + LL_I2L(int64 , ULONG_MAX); #if defined(HAVE_SYS_STATFS_H) || defined(HAVE_SYS_STATVFS_H) @@ -485,7 +487,7 @@ PRUint32 nsFileSpec::GetDiskSpaceAvailable() const { (void) getcwd(curdir, MAXPATHLEN); if (!curdir) - return ULONG_MAX; /* hope for the best as we did in cheddar */ + return int64; /* hope for the best as we did in cheddar */ } else sprintf(curdir, "%.200s", (const char*)mPath); @@ -496,21 +498,22 @@ PRUint32 nsFileSpec::GetDiskSpaceAvailable() const #else if (STATFS(curdir, &fs_buf) < 0) #endif - return ULONG_MAX; /* hope for the best as we did in cheddar */ + return int64; /* hope for the best as we did in cheddar */ #ifdef DEBUG_DISK_SPACE printf("DiskSpaceAvailable: %d bytes\n", fs_buf.f_bsize * (fs_buf.f_bavail - 1)); #endif - return fs_buf.f_bsize * (fs_buf.f_bavail - 1); + LL_I2L( int64 , (fs_buf.f_bsize * (fs_buf.f_bavail - 1) ) ); + return int64 #else /* ** This platform doesn't have statfs or statvfs, so we don't have much ** choice but to "hope for the best as we did in cheddar". */ - return ULONG_MAX; + return int64; #endif /* HAVE_SYS_STATFS_H or HAVE_SYS_STATVFS_H */ } // nsFileSpec::GetDiskSpace() diff --git a/mozilla/xpcom/io/nsFileSpecWin.cpp b/mozilla/xpcom/io/nsFileSpecWin.cpp index ec9b3483cef..3ac69bce320 100644 --- a/mozilla/xpcom/io/nsFileSpecWin.cpp +++ b/mozilla/xpcom/io/nsFileSpecWin.cpp @@ -549,11 +549,16 @@ nsresult nsFileSpec::Execute(const char* inArgs ) const return NS_FILE_FAILURE; } // nsFileSpec::Execute + //---------------------------------------------------------------------------------------- -PRUint32 nsFileSpec::GetDiskSpaceAvailable() const +PRUint64 nsFileSpec::GetDiskSpaceAvailable() const //---------------------------------------------------------------------------------------- { - char aDrive[_MAX_DRIVE + 2]; + PRUint64 int64; + + LL_I2L(int64 , ULONG_MAX); + + char aDrive[_MAX_DRIVE + 2]; _splitpath( (const char*)mPath, aDrive, NULL, NULL, NULL); if (aDrive[0] == '\0') @@ -569,28 +574,49 @@ PRUint32 nsFileSpec::GetDiskSpaceAvailable() const else { // Return bogus large number and hope for the best - return ULONG_MAX; + return int64; } } strcat(aDrive, "\\"); - DWORD dwSectorsPerCluster = 0; - DWORD dwBytesPerSector = 0; - DWORD dwFreeClusters = 0; - DWORD dwTotalClusters = 0; - if (!GetDiskFreeSpace(aDrive, - &dwSectorsPerCluster, - &dwBytesPerSector, - &dwFreeClusters, - &dwTotalClusters)) - { - return ULONG_MAX; // Return bogus large number and hope for the best - } + // Check disk space + DWORD dwSecPerClus, dwBytesPerSec, dwFreeClus, dwTotalClus; + ULARGE_INTEGER liFreeBytesAvailableToCaller, liTotalNumberOfBytes, liTotalNumberOfFreeBytes; + double nBytes = 0; + + BOOL (WINAPI* getDiskFreeSpaceExA)(LPCTSTR lpDirectoryName, + PULARGE_INTEGER lpFreeBytesAvailableToCaller, + PULARGE_INTEGER lpTotalNumberOfBytes, + PULARGE_INTEGER lpTotalNumberOfFreeBytes) = NULL; + + HINSTANCE hInst = LoadLibrary("KERNEL32.DLL"); + NS_ASSERTION(hInst != NULL, "COULD NOT LOAD KERNEL32.DLL"); + if (hInst != NULL) + { + getDiskFreeSpaceExA = (BOOL (WINAPI*)(LPCTSTR lpDirectoryName, + PULARGE_INTEGER lpFreeBytesAvailableToCaller, + PULARGE_INTEGER lpTotalNumberOfBytes, + PULARGE_INTEGER lpTotalNumberOfFreeBytes)) + GetProcAddress(hInst, "GetDiskFreeSpaceExA"); + FreeLibrary(hInst); + } + + if (getDiskFreeSpaceExA && (*getDiskFreeSpaceExA)(aDrive, + &liFreeBytesAvailableToCaller, + &liTotalNumberOfBytes, + &liTotalNumberOfFreeBytes)) + { + nBytes = (double)(signed __int64)liFreeBytesAvailableToCaller.QuadPart; + } + else if ( GetDiskFreeSpace(aDrive, &dwSecPerClus, &dwBytesPerSec, &dwFreeClus, &dwTotalClus)) + { + nBytes = (double)dwFreeClus*(double)dwSecPerClus*(double) dwBytesPerSec; + } + return (PRUint64)nBytes; +} + - // We can now figure free disk space. - return dwFreeClusters * dwSectorsPerCluster * dwBytesPerSector; -} // nsFileSpec::GetDiskSpaceAvailable() //======================================================================================== // nsDirectoryIterator diff --git a/mozilla/xpcom/io/nsIFile.idl b/mozilla/xpcom/io/nsIFile.idl index 8c82f3bc09d..17e3ef6f70c 100644 --- a/mozilla/xpcom/io/nsIFile.idl +++ b/mozilla/xpcom/io/nsIFile.idl @@ -157,7 +157,7 @@ interface nsIFile : nsISupports readonly attribute unsigned long fileSizeOfLink; - readonly attribute unsigned long diskSpaceAvailable; // maybe we should put this somewhere else. + readonly attribute unsigned long long diskSpaceAvailable; // maybe we should put this somewhere else. /** * Parent will be nsnull when this is at the top of the volume. diff --git a/mozilla/xpinstall/src/nsInstall.cpp b/mozilla/xpinstall/src/nsInstall.cpp index e95021dc256..48bd895625f 100644 --- a/mozilla/xpinstall/src/nsInstall.cpp +++ b/mozilla/xpinstall/src/nsInstall.cpp @@ -647,7 +647,7 @@ nsInstall::DeleteFile(const nsString& aFolder, const nsString& aRelativeFileName } PRInt32 -nsInstall::DiskSpaceAvailable(const nsString& aFolder, PRUint32* aReturn) +nsInstall::DiskSpaceAvailable(const nsString& aFolder, PRUint64* aReturn) { nsFileSpec fsFolder(aFolder); @@ -1574,7 +1574,7 @@ nsInstall::FileOpFileGetNativeVersion(nsFileSpec& aTarget, nsString* aReturn) } PRInt32 -nsInstall::FileOpFileGetDiskSpaceAvailable(nsFileSpec& aTarget, PRUint32* aReturn) +nsInstall::FileOpFileGetDiskSpaceAvailable(nsFileSpec& aTarget, PRUint64* aReturn) { *aReturn = aTarget.GetDiskSpaceAvailable(); return NS_OK; diff --git a/mozilla/xpinstall/src/nsInstall.h b/mozilla/xpinstall/src/nsInstall.h index f5a72859465..07200028674 100644 --- a/mozilla/xpinstall/src/nsInstall.h +++ b/mozilla/xpinstall/src/nsInstall.h @@ -180,7 +180,7 @@ class nsInstall PRInt32 DeleteComponent(const nsString& aRegistryName, PRInt32* aReturn); PRInt32 DeleteFile(const nsString& aFolder, const nsString& aRelativeFileName, PRInt32* aReturn); - PRInt32 DiskSpaceAvailable(const nsString& aFolder, PRUint32* aReturn); + PRInt32 DiskSpaceAvailable(const nsString& aFolder, PRUint64* aReturn); PRInt32 Execute(const nsString& aJarSource, const nsString& aArgs, PRInt32* aReturn); PRInt32 Execute(const nsString& aJarSource, PRInt32* aReturn); PRInt32 FinalizeInstall(PRInt32* aReturn); @@ -209,7 +209,7 @@ class nsInstall PRInt32 FileOpFileExists(nsFileSpec& aTarget, PRBool* aReturn); PRInt32 FileOpFileExecute(nsFileSpec& aTarget, nsString& aParams, PRInt32* aReturn); PRInt32 FileOpFileGetNativeVersion(nsFileSpec& aTarget, nsString* aReturn); - PRInt32 FileOpFileGetDiskSpaceAvailable(nsFileSpec& aTarget, PRUint32* aReturn); + PRInt32 FileOpFileGetDiskSpaceAvailable(nsFileSpec& aTarget, PRUint64* aReturn); PRInt32 FileOpFileGetModDate(nsFileSpec& aTarget, nsFileSpec::TimeStamp* aReturn); PRInt32 FileOpFileGetSize(nsFileSpec& aTarget, PRUint32* aReturn); PRInt32 FileOpFileIsDirectory(nsFileSpec& aTarget, PRBool* aReturn); diff --git a/mozilla/xpinstall/src/nsJSInstall.cpp b/mozilla/xpinstall/src/nsJSInstall.cpp index 46e38421327..1a52ff654de 100644 --- a/mozilla/xpinstall/src/nsJSInstall.cpp +++ b/mozilla/xpinstall/src/nsJSInstall.cpp @@ -640,7 +640,7 @@ PR_STATIC_CALLBACK(JSBool) InstallDiskSpaceAvailable(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { nsInstall *nativeThis = (nsInstall*)JS_GetPrivate(cx, obj); - PRUint32 nativeRet; + PRUint64 nativeRet; nsAutoString b0; *rval = JSVAL_NULL; @@ -1748,7 +1748,7 @@ PR_STATIC_CALLBACK(JSBool) InstallFileOpFileGetDiskSpaceAvailable(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { nsInstall* nativeThis = (nsInstall*)JS_GetPrivate(cx, obj); - PRUint32 nativeRet; + PRUint64 nativeRet; nsAutoString b0; *rval = JSVAL_NULL; diff --git a/mozilla/xpinstall/src/nsSoftwareUpdateRun.cpp b/mozilla/xpinstall/src/nsSoftwareUpdateRun.cpp index cb358121999..818fa2c4974 100644 --- a/mozilla/xpinstall/src/nsSoftwareUpdateRun.cpp +++ b/mozilla/xpinstall/src/nsSoftwareUpdateRun.cpp @@ -353,7 +353,7 @@ extern "C" void RunInstallOnThread(void *data) args.GetUnicode(), &rt, &cx, &glob); if (NS_FAILED(rv)) - goto bail; + goto bail; // TODO need to log that this failed! // Go ahead and run!! jsval rval;