Bug 681660 - [win] GetDiskSpaceAvailable doesn't work on files.
authorMarco Bonardo <mbonardo@mozilla.com>
Fri, 26 Aug 2011 13:29:45 +0200
changeset 75941 7dab2bc1cc91ccc2edd17f21c002d4acba2ba412
parent 75940 1b6030e09f9a3a8c46dd61f57b2bbb71d7f62874
child 75942 2fd69e4236ea8be9296e1356300898d2fa7fdfaf
push id3
push userfelipc@gmail.com
push dateFri, 30 Sep 2011 20:09:13 +0000
bugs681660
milestone9.0a1
Bug 681660 - [win] GetDiskSpaceAvailable doesn't work on files. r=jimm
xpcom/io/nsLocalFileWin.cpp
xpcom/tests/unit/test_localfile.js
--- a/xpcom/io/nsLocalFileWin.cpp
+++ b/xpcom/io/nsLocalFileWin.cpp
@@ -2132,16 +2132,25 @@ nsLocalFile::GetDiskSpaceAvailable(PRInt
 {
     // Check we are correctly initialized.
     CHECK_mWorkingPath();
 
     NS_ENSURE_ARG(aDiskSpaceAvailable);
 
     ResolveAndStat();
 
+    if (mFileInfo64.type == PR_FILE_FILE) {
+      // Since GetDiskFreeSpaceExW works only on directories, use the parent.
+      nsCOMPtr<nsIFile> parent;
+      if (NS_SUCCEEDED(GetParent(getter_AddRefs(parent))) && parent) {
+        nsCOMPtr<nsILocalFile> localParent = do_QueryInterface(parent);
+        return localParent->GetDiskSpaceAvailable(aDiskSpaceAvailable);
+      }
+    }
+
     ULARGE_INTEGER liFreeBytesAvailableToCaller, liTotalNumberOfBytes;
     if (::GetDiskFreeSpaceExW(mResolvedPath.get(), &liFreeBytesAvailableToCaller, 
                               &liTotalNumberOfBytes, NULL))
     {
         *aDiskSpaceAvailable = liFreeBytesAvailableToCaller.QuadPart;
         return NS_OK;
     }
     *aDiskSpaceAvailable = 0;
--- a/xpcom/tests/unit/test_localfile.js
+++ b/xpcom/tests/unit/test_localfile.js
@@ -46,16 +46,17 @@ const MILLIS_PER_DAY      = 1000 * 60 * 
 var LocalFile = CC("@mozilla.org/file/local;1", "nsILocalFile", "initWithPath");
 
 function run_test()
 {
   test_toplevel_parent_is_null();
   test_normalize_crash_if_media_missing();
   test_file_modification_time();
   test_directory_modification_time();
+  test_diskSpaceAvailable();
 }
 
 function test_toplevel_parent_is_null()
 {
   try
   {
     var lf = new LocalFile("C:\\");
 
@@ -157,8 +158,26 @@ function test_directory_modification_tim
   dir.lastModifiedTime = tomorrow;
 
   diff = Math.abs(dir.lastModifiedTime - tomorrow);
   do_check_true(diff < MAX_TIME_DIFFERENCE);
 
   dir.remove(true);
 }
 
+function test_diskSpaceAvailable()
+{
+  let file = do_get_profile();
+  file.QueryInterface(Ci.nsILocalFile);
+
+  let bytes = file.diskSpaceAvailable;
+  do_check_true(bytes > 0);
+
+  file.append("testfile");
+  if (file.exists())
+    file.remove(true);
+  file.create(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt("644", 8));
+
+  bytes = file.diskSpaceAvailable;
+  do_check_true(bytes > 0);
+
+  file.remove(true);
+}