Move GetFreeDiskSpace to SysInfo.

Patch from Pawel Hajdan Jr.
Review URL: http://codereview.chromium.org/3141

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@2359 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/base/sys_info.h b/base/sys_info.h
index 01e506c..00436f5 100644
--- a/base/sys_info.h
+++ b/base/sys_info.h
@@ -7,6 +7,8 @@
 
 #include "base/basictypes.h"
 
+#include <string>
+
 namespace base {
 
 class SysInfo {
@@ -21,6 +23,10 @@
   static int AmountOfPhysicalMemoryMB() {
     return static_cast<int>(AmountOfPhysicalMemory() / 1024 / 1024);
   }
+
+  // Return the available disk space in bytes on the volume containing |path|.
+  static int64 AmountOfFreeDiskSpace(const std::wstring& path);
+
 };
 
 }  // namespace base
diff --git a/base/sys_info_posix.cc b/base/sys_info_posix.cc
index a9a91cd..541a424 100644
--- a/base/sys_info_posix.cc
+++ b/base/sys_info_posix.cc
@@ -7,6 +7,7 @@
 
 #include <errno.h>
 #include <string.h>
+#include <sys/statvfs.h>
 #include <unistd.h>
 
 #if defined(OS_MACOSX)
@@ -15,6 +16,7 @@
 #endif
 
 #include "base/logging.h"
+#include "base/string_util.h"
 
 namespace base {
 
@@ -59,4 +61,13 @@
 #endif
 }
 
+// static
+int64 SysInfo::AmountOfFreeDiskSpace(const std::wstring& path) {
+  struct statvfs stats;
+  if (statvfs(WideToUTF8(path).c_str(), &stats) != 0) {
+    return 0;
+  }
+  return static_cast<int64>(stats.f_bavail) * stats.f_frsize;
+}
+
 }  // namespace base
diff --git a/base/sys_info_unittest.cc b/base/sys_info_unittest.cc
index edaf2aa..c10b1b7 100644
--- a/base/sys_info_unittest.cc
+++ b/base/sys_info_unittest.cc
@@ -2,6 +2,7 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
+#include "base/file_util.h"
 #include "base/sys_info.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
@@ -13,4 +14,12 @@
 TEST(SysInfoTest, AmountOfMem) {
   // We aren't actually testing that it's correct, just that it's sane.
   EXPECT_GT(base::SysInfo::AmountOfPhysicalMemory(), 0);
+  EXPECT_GT(base::SysInfo::AmountOfPhysicalMemoryMB(), 0);
+}
+
+TEST(SysInfoTest, AmountOfFreeDiskSpace) {
+  // We aren't actually testing that it's correct, just that it's sane.
+  std::wstring tmp_path;
+  ASSERT_TRUE(file_util::GetTempDir(&tmp_path));
+  EXPECT_GT(base::SysInfo::AmountOfFreeDiskSpace(tmp_path), 0);
 }
diff --git a/base/sys_info_win.cc b/base/sys_info_win.cc
index 8b6dfea1..a90f7b9 100644
--- a/base/sys_info_win.cc
+++ b/base/sys_info_win.cc
@@ -29,4 +29,13 @@
   return static_cast<int64>(memory_info.ullTotalPhys);
 }
 
+// static
+int64 SysInfo::AmountOfFreeDiskSpace(const std::wstring& path) {
+  ULARGE_INTEGER available, total, free;
+  if (!GetDiskFreeSpaceExW(path.c_str(), &available, &total, &free)) {
+    return 0;
+  }
+  return static_cast<int64>(available.QuadPart);
+}
+
 }  // namespace base
diff --git a/net/disk_cache/backend_impl.cc b/net/disk_cache/backend_impl.cc
index d321c83..05d017a 100644
--- a/net/disk_cache/backend_impl.cc
+++ b/net/disk_cache/backend_impl.cc
@@ -8,6 +8,7 @@
 #include "base/histogram.h"
 #include "base/message_loop.h"
 #include "base/string_util.h"
+#include "base/sys_info.h"
 #include "base/timer.h"
 #include "base/worker_pool.h"
 #include "net/disk_cache/cache_util.h"
@@ -760,7 +761,7 @@
     return;
 
   // The user is not setting the size, let's figure it out.
-  int64 available = GetFreeDiskSpace(path_);
+  int64 available = base::SysInfo::AmountOfFreeDiskSpace(path_);
   if (available < 0) {
     max_size_ = kDefaultCacheSize;
     return;
diff --git a/net/disk_cache/cache_util.h b/net/disk_cache/cache_util.h
index 9df9b38..7abebe05 100644
--- a/net/disk_cache/cache_util.h
+++ b/net/disk_cache/cache_util.h
@@ -11,10 +11,6 @@
 
 namespace disk_cache {
 
-// Returns the available disk space on the volume that contains |path|
-// (in bytes), or -1 on failure.
-int64 GetFreeDiskSpace(const std::wstring& path);
-
 // Moves the cache files from the given path to another location.
 // Returns true if successful, false otherwise.
 bool MoveCache(const std::wstring& from_path, const std::wstring& to_path);
diff --git a/net/disk_cache/cache_util_posix.cc b/net/disk_cache/cache_util_posix.cc
index 69281876..80134b4d 100644
--- a/net/disk_cache/cache_util_posix.cc
+++ b/net/disk_cache/cache_util_posix.cc
@@ -4,29 +4,12 @@
 
 #include "net/disk_cache/cache_util.h"
 
-#include <errno.h>
-#include <sys/statvfs.h>
-#include <unistd.h>
-
 #include "base/file_util.h"
 #include "base/logging.h"
 #include "base/string_util.h"
 
-#if defined(OS_MACOSX)
-#include <mach/mach_host.h>
-#include <mach/mach_init.h>
-#endif
-
 namespace disk_cache {
 
-int64 GetFreeDiskSpace(const std::wstring& path) {
-  struct statvfs stats;
-  if (statvfs(WideToUTF8(path).c_str(), &stats) != 0) {
-    return -1;
-  }
-  return static_cast<int64>(stats.f_bavail) * stats.f_frsize;
-}
-
 bool MoveCache(const std::wstring& from_path, const std::wstring& to_path) {
   // Just use the version from base.
   return file_util::Move(from_path.c_str(), to_path.c_str());
diff --git a/net/disk_cache/cache_util_win.cc b/net/disk_cache/cache_util_win.cc
index dd4de1d2..377cbbf 100644
--- a/net/disk_cache/cache_util_win.cc
+++ b/net/disk_cache/cache_util_win.cc
@@ -38,17 +38,6 @@
 
 namespace disk_cache {
 
-int64 GetFreeDiskSpace(const std::wstring& path) {
-  ULARGE_INTEGER available, total, free;
-  if (!GetDiskFreeSpaceExW(path.c_str(), &available, &total, &free)) {
-    return -1;
-  }
-  int64 rv = static_cast<int64>(available.QuadPart);
-  if (rv < 0)
-    rv = kint64max;
-  return rv;
-}
-
 bool MoveCache(const std::wstring& from_path, const std::wstring& to_path) {
   // I don't want to use the shell version of move because if something goes
   // wrong, that version will attempt to move file by file and fail at the end.