-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk_usage_proxy.cc
209 lines (182 loc) · 6.78 KB
/
disk_usage_proxy.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright 2021 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "spaced/disk_usage_proxy.h"
#include <memory>
#include <string>
#include <utility>
#include <base/files/scoped_file.h>
#include <base/functional/callback.h>
#include <base/functional/callback_helpers.h>
#include <base/logging.h>
namespace spaced {
namespace {
void LogOnSignalConnected(const std::string& interface_name,
const std::string& signal_name,
bool success) {
if (!success) {
LOG(ERROR) << "Failed to connect to signal " << signal_name
<< " of interface " << interface_name;
}
}
} // namespace
DiskUsageProxy::DiskUsageProxy(
std::unique_ptr<org::chromium::SpacedProxyInterface> spaced_proxy)
: spaced_proxy_(std::move(spaced_proxy)) {}
std::unique_ptr<DiskUsageProxy> DiskUsageProxy::Generate() {
dbus::Bus::Options options;
options.bus_type = dbus::Bus::SYSTEM;
scoped_refptr<dbus::Bus> bus = new dbus::Bus(options);
if (!bus->Connect()) {
LOG(ERROR) << "D-Bus system bus is not ready";
return nullptr;
}
return std::make_unique<DiskUsageProxy>(
std::make_unique<org::chromium::SpacedProxy>(bus));
}
int64_t DiskUsageProxy::GetFreeDiskSpace(const base::FilePath& path) {
int64_t free_disk_space;
brillo::ErrorPtr error;
// Return -1 if call fails.
if (!spaced_proxy_->GetFreeDiskSpace(path.value(), &free_disk_space,
&error)) {
LOG(ERROR) << "Failed to call GetFreeDiskSpace, error: "
<< error->GetMessage();
return -1;
}
return free_disk_space;
}
void DiskUsageProxy::GetFreeDiskSpaceAsync(
const base::FilePath& path, base::OnceCallback<void(int64_t)> callback) {
auto splitted_callbacks = base::SplitOnceCallback(std::move(callback));
spaced_proxy_->GetFreeDiskSpaceAsync(
path.value(), std::move(splitted_callbacks.first),
base::BindOnce(
[](base::OnceCallback<void(int64_t)> callback, brillo::Error* error) {
LOG(ERROR) << "Failed to GetFreeDiskSpaceAsync: "
<< error->GetMessage();
std::move(callback).Run(-1);
},
std::move(splitted_callbacks.second)));
}
int64_t DiskUsageProxy::GetTotalDiskSpace(const base::FilePath& path) {
int64_t total_disk_space;
brillo::ErrorPtr error;
// Return -1 if call fails.
if (!spaced_proxy_->GetTotalDiskSpace(path.value(), &total_disk_space,
&error)) {
LOG(ERROR) << "Failed to call GetTotalDiskSpace, error: "
<< error->GetMessage();
return -1;
}
return total_disk_space;
}
int64_t DiskUsageProxy::GetRootDeviceSize() {
int64_t root_device_size;
brillo::ErrorPtr error;
// Return -1 if call fails.
if (!spaced_proxy_->GetRootDeviceSize(&root_device_size, &error)) {
LOG(ERROR) << "Failed to call GetRootDeviceSize, error: "
<< error->GetMessage();
return -1;
}
return root_device_size;
}
bool DiskUsageProxy::IsQuotaSupported(const base::FilePath& path) {
bool is_supported = false;
brillo::ErrorPtr error;
// Return false if call fails.
if (!spaced_proxy_->IsQuotaSupported(path.value(), &is_supported, &error)) {
LOG(ERROR) << "Failed to call IsQuotaSupported, error: "
<< error->GetMessage();
return false;
}
return is_supported;
}
int64_t DiskUsageProxy::GetQuotaCurrentSpaceForUid(const base::FilePath& path,
uint32_t uid) {
int64_t current_space = 0;
brillo::ErrorPtr error;
// Return -1 if call fails.
if (!spaced_proxy_->GetQuotaCurrentSpaceForUid(path.value(), uid,
¤t_space, &error)) {
LOG(ERROR) << "Failed to call GetQuotaCurrentSpaceForUid, error: "
<< error->GetMessage();
return -1;
}
return current_space;
}
int64_t DiskUsageProxy::GetQuotaCurrentSpaceForGid(const base::FilePath& path,
uint32_t gid) {
int64_t current_space = 0;
brillo::ErrorPtr error;
// Return -1 if call fails.
if (!spaced_proxy_->GetQuotaCurrentSpaceForGid(path.value(), gid,
¤t_space, &error)) {
LOG(ERROR) << "Failed to call GetQuotaCurrentSpaceForGid, error: "
<< error->GetMessage();
return -1;
}
return current_space;
}
int64_t DiskUsageProxy::GetQuotaCurrentSpaceForProjectId(
const base::FilePath& path, uint32_t project_id) {
int64_t current_space = 0;
brillo::ErrorPtr error;
// Return -1 if call fails.
if (!spaced_proxy_->GetQuotaCurrentSpaceForGid(path.value(), project_id,
¤t_space, &error)) {
LOG(ERROR) << "Failed to call GetQuotaCurrentSpaceForProjectId, error: "
<< error->GetMessage();
return -1;
}
return current_space;
}
bool DiskUsageProxy::SetProjectId(const base::ScopedFD& fd,
uint32_t project_id,
int* out_error) {
SetProjectIdReply result;
brillo::ErrorPtr error;
// Return false if call fails.
if (!spaced_proxy_->SetProjectId(fd, project_id, &result, &error)) {
*out_error = result.error();
LOG(ERROR) << "Failed to call SetProjectId, error: " << error->GetMessage();
return false;
}
return result.success();
}
bool DiskUsageProxy::SetProjectInheritanceFlag(const base::ScopedFD& fd,
bool enable,
int* out_error) {
SetProjectInheritanceFlagReply result;
brillo::ErrorPtr error;
// Return false if call fails.
if (!spaced_proxy_->SetProjectInheritanceFlag(fd, enable, &result, &error)) {
*out_error = result.error();
LOG(ERROR) << "Failed to call SetProjectInheritanceFlag, error: "
<< error->GetMessage();
return false;
}
return result.success();
}
void DiskUsageProxy::OnStatefulDiskSpaceUpdate(
const StatefulDiskSpaceUpdate& update) {
for (SpacedObserverInterface& observer : observer_list_) {
observer.OnStatefulDiskSpaceUpdate(update);
}
}
void DiskUsageProxy::AddObserver(SpacedObserverInterface* observer) {
CHECK(observer) << "Invalid observer";
observer_list_.AddObserver(observer);
}
void DiskUsageProxy::RemoveObserver(SpacedObserverInterface* observer) {
CHECK(observer) << "Invalid observer";
observer_list_.RemoveObserver(observer);
}
void DiskUsageProxy::StartMonitoring() {
spaced_proxy_->RegisterStatefulDiskSpaceUpdateSignalHandler(
base::BindRepeating(&DiskUsageProxy::OnStatefulDiskSpaceUpdate,
base::Unretained(this)),
base::BindOnce(LogOnSignalConnected));
}
} // namespace spaced