summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authordeanm@chromium.org <deanm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2008-09-18 12:34:24 +0000
committerdeanm@chromium.org <deanm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2008-09-18 12:34:24 +0000
commit0e91dd2a1cec40c6fa0390c890e10dc75398de34 (patch)
treef4385248c0abb048ec718cb555b5287847bc4429 /base
parentfadf97f2f16f6b3ad2756cd07657c4f47172da92 (diff)
downloadchromium_src-0e91dd2a1cec40c6fa0390c890e10dc75398de34.zip
chromium_src-0e91dd2a1cec40c6fa0390c890e10dc75398de34.tar.gz
chromium_src-0e91dd2a1cec40c6fa0390c890e10dc75398de34.tar.bz2
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
Diffstat (limited to 'base')
-rw-r--r--base/sys_info.h6
-rw-r--r--base/sys_info_posix.cc11
-rw-r--r--base/sys_info_unittest.cc9
-rw-r--r--base/sys_info_win.cc9
4 files changed, 35 insertions, 0 deletions
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 @@ class SysInfo {
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 @@ int64 SysInfo::AmountOfPhysicalMemory() {
#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, NumProcs) {
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 8b6dfea..a90f7b9 100644
--- a/base/sys_info_win.cc
+++ b/base/sys_info_win.cc
@@ -29,4 +29,13 @@ int64 SysInfo::AmountOfPhysicalMemory() {
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