Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(104)

Side by Side Diff: base/sys_info_posix.cc

Issue 2198283002: Return correct disk free/available size when FS is mounted with size = 0 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Switch to statfs() instead of statvfs() as f_type is not exposed via latter. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/sys_info.h" 5 #include "base/sys_info.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 #include <string.h> 10 #include <string.h>
11 #include <sys/param.h> 11 #include <sys/param.h>
12 #include <sys/resource.h> 12 #include <sys/resource.h>
13 #include <sys/utsname.h> 13 #include <sys/utsname.h>
14 #include <unistd.h> 14 #include <unistd.h>
15 15
16 #include "base/files/file_util.h" 16 #include "base/files/file_util.h"
17 #include "base/lazy_instance.h" 17 #include "base/lazy_instance.h"
18 #include "base/logging.h" 18 #include "base/logging.h"
19 #include "base/strings/utf_string_conversions.h" 19 #include "base/strings/utf_string_conversions.h"
20 #include "base/sys_info_internal.h" 20 #include "base/sys_info_internal.h"
21 #include "base/threading/thread_restrictions.h" 21 #include "base/threading/thread_restrictions.h"
22 #include "build/build_config.h" 22 #include "build/build_config.h"
23 23
24 #if defined(OS_ANDROID)
Lei Zhang 2016/08/02 00:11:48 Ok, hope this works.
Sriram 2016/08/02 20:51:56 Acknowledged.
25 #include <sys/vfs.h> 24 #include <sys/vfs.h>
26 #define statvfs statfs // Android uses a statvfs-like statfs struct and call. 25 #include <linux/magic.h>
Lei Zhang 2016/08/02 00:11:48 Now that you have removed the #if, move these up t
27 #else
28 #include <sys/statvfs.h>
29 #endif
30 26
31 namespace { 27 namespace {
32 28
33 #if !defined(OS_OPENBSD) 29 #if !defined(OS_OPENBSD)
34 int NumberOfProcessors() { 30 int NumberOfProcessors() {
35 // sysconf returns the number of "logical" (not "physical") processors on both 31 // sysconf returns the number of "logical" (not "physical") processors on both
36 // Mac and Linux. So we get the number of max available "logical" processors. 32 // Mac and Linux. So we get the number of max available "logical" processors.
37 // 33 //
38 // Note that the number of "currently online" processors may be fewer than the 34 // Note that the number of "currently online" processors may be fewer than the
39 // returned value of NumberOfProcessors(). On some platforms, the kernel may 35 // returned value of NumberOfProcessors(). On some platforms, the kernel may
(...skipping 26 matching lines...) Expand all
66 NOTREACHED(); 62 NOTREACHED();
67 return 0; 63 return 0;
68 } 64 }
69 return limit.rlim_cur == RLIM_INFINITY ? 0 : limit.rlim_cur; 65 return limit.rlim_cur == RLIM_INFINITY ? 0 : limit.rlim_cur;
70 } 66 }
71 67
72 base::LazyInstance< 68 base::LazyInstance<
73 base::internal::LazySysInfoValue<int64_t, AmountOfVirtualMemory>>::Leaky 69 base::internal::LazySysInfoValue<int64_t, AmountOfVirtualMemory>>::Leaky
74 g_lazy_virtual_memory = LAZY_INSTANCE_INITIALIZER; 70 g_lazy_virtual_memory = LAZY_INSTANCE_INITIALIZER;
75 71
72 #if defined(OS_LINUX)
73 bool IsStatsZeroIfUnlimited(unsigned long int fstype) {
74 // On Linux, stats.f_blocks is 0 when memory based file system (like tmpfs,
75 // ramfs, or hugetlbfs), is mounted without any size limit (i.e. size set to
76 // 0).
77 return fstype == TMPFS_MAGIC || fstype == HUGETLBFS_MAGIC ||
78 fstype == RAMFS_MAGIC;
79 }
80 #endif
81
76 bool GetDiskSpaceInfo(const base::FilePath& path, 82 bool GetDiskSpaceInfo(const base::FilePath& path,
77 int64_t* available_bytes, 83 int64_t* available_bytes,
78 int64_t* total_bytes) { 84 int64_t* total_bytes) {
79 struct statvfs stats; 85 struct statfs stats;
80 if (HANDLE_EINTR(statvfs(path.value().c_str(), &stats)) != 0) 86
87 if (HANDLE_EINTR(statfs(path.value().c_str(), &stats)) != 0)
81 return false; 88 return false;
82 89
83 if (available_bytes) 90 #if defined(OS_LINUX)
84 *available_bytes = static_cast<int64_t>(stats.f_bavail) * stats.f_frsize; 91 const bool zero_size_means_unlimited =
85 if (total_bytes) 92 stats.f_blocks == 0 && IsStatsZeroIfUnlimited(stats.f_type);
86 *total_bytes = static_cast<int64_t>(stats.f_blocks) * stats.f_frsize; 93 #else
94 const bool zero_size_means_unlimited = false;
95 #endif
96
97 if (available_bytes) {
98 *available_bytes =
99 zero_size_means_unlimited
100 ? std::numeric_limits<int64_t>::max()
101 : static_cast<int64_t>(stats.f_bavail) * stats.f_frsize;
102 }
103
104 if (total_bytes) {
105 *total_bytes = zero_size_means_unlimited
106 ? std::numeric_limits<int64_t>::max()
107 : static_cast<int64_t>(stats.f_blocks) * stats.f_frsize;
108 }
109
87 return true; 110 return true;
88 } 111 }
89 112
90 } // namespace 113 } // namespace
91 114
92 namespace base { 115 namespace base {
93 116
94 #if !defined(OS_OPENBSD) 117 #if !defined(OS_OPENBSD)
95 int SysInfo::NumberOfProcessors() { 118 int SysInfo::NumberOfProcessors() {
96 return g_lazy_number_of_processors.Get().value(); 119 return g_lazy_number_of_processors.Get().value();
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 } 184 }
162 return arch; 185 return arch;
163 } 186 }
164 187
165 // static 188 // static
166 size_t SysInfo::VMAllocationGranularity() { 189 size_t SysInfo::VMAllocationGranularity() {
167 return getpagesize(); 190 return getpagesize();
168 } 191 }
169 192
170 } // namespace base 193 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698