Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 17 | #include "sehandle.h" |
| 18 | #include "Utils.h" |
| 19 | #include "Process.h" |
| 20 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 21 | #include <base/logging.h> |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 22 | #include <base/stringprintf.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 23 | #include <cutils/fs.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 24 | #include <private/android_filesystem_config.h> |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 25 | #include <logwrap/logwrap.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 26 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 27 | #include <mutex> |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 28 | #include <dirent.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 29 | #include <fcntl.h> |
| 30 | #include <linux/fs.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <sys/mount.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/stat.h> |
| 35 | #include <sys/wait.h> |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 36 | #include <sys/statvfs.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 37 | |
| 38 | #ifndef UMOUNT_NOFOLLOW |
| 39 | #define UMOUNT_NOFOLLOW 0x00000008 /* Don't follow symlink on umount */ |
| 40 | #endif |
| 41 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 42 | using android::base::StringPrintf; |
| 43 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 44 | namespace android { |
| 45 | namespace vold { |
| 46 | |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 47 | security_context_t sBlkidContext = nullptr; |
| 48 | security_context_t sBlkidUntrustedContext = nullptr; |
| 49 | security_context_t sFsckContext = nullptr; |
| 50 | security_context_t sFsckUntrustedContext = nullptr; |
| 51 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 52 | static const char* kBlkidPath = "/system/bin/blkid"; |
| 53 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 54 | status_t CreateDeviceNode(const std::string& path, dev_t dev) { |
| 55 | const char* cpath = path.c_str(); |
| 56 | status_t res = 0; |
| 57 | |
| 58 | char* secontext = nullptr; |
| 59 | if (sehandle) { |
| 60 | if (!selabel_lookup(sehandle, &secontext, cpath, S_IFBLK)) { |
| 61 | setfscreatecon(secontext); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | mode_t mode = 0660 | S_IFBLK; |
| 66 | if (mknod(cpath, mode, dev) < 0) { |
| 67 | if (errno != EEXIST) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 68 | PLOG(ERROR) << "Failed to create device node for " << major(dev) |
| 69 | << ":" << minor(dev) << " at " << path; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 70 | res = -errno; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | if (secontext) { |
| 75 | setfscreatecon(nullptr); |
| 76 | freecon(secontext); |
| 77 | } |
| 78 | |
| 79 | return res; |
| 80 | } |
| 81 | |
| 82 | status_t DestroyDeviceNode(const std::string& path) { |
| 83 | const char* cpath = path.c_str(); |
| 84 | if (TEMP_FAILURE_RETRY(unlink(cpath))) { |
| 85 | return -errno; |
| 86 | } else { |
| 87 | return OK; |
| 88 | } |
| 89 | } |
| 90 | |
Jeff Sharkey | f0121c5 | 2015-04-06 14:08:45 -0700 | [diff] [blame] | 91 | status_t PrepareDir(const std::string& path, mode_t mode, uid_t uid, gid_t gid) { |
| 92 | const char* cpath = path.c_str(); |
| 93 | |
| 94 | char* secontext = nullptr; |
| 95 | if (sehandle) { |
| 96 | if (!selabel_lookup(sehandle, &secontext, cpath, S_IFDIR)) { |
| 97 | setfscreatecon(secontext); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | int res = fs_prepare_dir(cpath, mode, uid, gid); |
| 102 | |
| 103 | if (secontext) { |
| 104 | setfscreatecon(nullptr); |
| 105 | freecon(secontext); |
| 106 | } |
| 107 | |
| 108 | if (res == 0) { |
| 109 | return OK; |
| 110 | } else { |
| 111 | return -errno; |
| 112 | } |
| 113 | } |
| 114 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 115 | status_t ForceUnmount(const std::string& path) { |
| 116 | const char* cpath = path.c_str(); |
| 117 | if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) { |
| 118 | return OK; |
| 119 | } |
Jeff Sharkey | f0121c5 | 2015-04-06 14:08:45 -0700 | [diff] [blame] | 120 | PLOG(WARNING) << "Failed to unmount " << path; |
| 121 | |
| 122 | sleep(5); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 123 | Process::killProcessesWithOpenFiles(cpath, SIGINT); |
| 124 | |
| 125 | if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) { |
| 126 | return OK; |
| 127 | } |
| 128 | PLOG(WARNING) << "Failed to unmount " << path; |
| 129 | |
| 130 | sleep(5); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 131 | Process::killProcessesWithOpenFiles(cpath, SIGTERM); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 132 | |
| 133 | if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) { |
| 134 | return OK; |
| 135 | } |
Jeff Sharkey | f0121c5 | 2015-04-06 14:08:45 -0700 | [diff] [blame] | 136 | PLOG(WARNING) << "Failed to unmount " << path; |
| 137 | |
| 138 | sleep(5); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 139 | Process::killProcessesWithOpenFiles(cpath, SIGKILL); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 140 | |
| 141 | if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) { |
| 142 | return OK; |
| 143 | } |
Jeff Sharkey | f0121c5 | 2015-04-06 14:08:45 -0700 | [diff] [blame] | 144 | PLOG(ERROR) << "Failed to unmount " << path; |
| 145 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 146 | return -errno; |
| 147 | } |
| 148 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 149 | status_t BindMount(const std::string& source, const std::string& target) { |
| 150 | if (::mount(source.c_str(), target.c_str(), "", MS_BIND, NULL)) { |
| 151 | PLOG(ERROR) << "Failed to bind mount " << source << " to " << target; |
| 152 | return -errno; |
| 153 | } |
| 154 | return OK; |
| 155 | } |
| 156 | |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 157 | static status_t readMetadata(const std::string& path, std::string& fsType, |
| 158 | std::string& fsUuid, std::string& fsLabel, bool untrusted) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 159 | fsType.clear(); |
| 160 | fsUuid.clear(); |
| 161 | fsLabel.clear(); |
| 162 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 163 | std::vector<std::string> cmd; |
| 164 | cmd.push_back(kBlkidPath); |
| 165 | cmd.push_back("-c"); |
| 166 | cmd.push_back("/dev/null"); |
| 167 | cmd.push_back(path); |
| 168 | |
| 169 | std::vector<std::string> output; |
| 170 | status_t res = ForkExecvp(cmd, output, untrusted ? sBlkidUntrustedContext : sBlkidContext); |
| 171 | if (res != OK) { |
| 172 | LOG(WARNING) << "blkid failed to identify " << path; |
| 173 | return res; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 176 | char value[128]; |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 177 | for (auto line : output) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 178 | // Extract values from blkid output, if defined |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 179 | const char* cline = line.c_str(); |
| 180 | char* start = strstr(cline, "TYPE="); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 181 | if (start != nullptr && sscanf(start + 5, "\"%127[^\"]\"", value) == 1) { |
| 182 | fsType = value; |
| 183 | } |
| 184 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 185 | start = strstr(cline, "UUID="); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 186 | if (start != nullptr && sscanf(start + 5, "\"%127[^\"]\"", value) == 1) { |
| 187 | fsUuid = value; |
| 188 | } |
| 189 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 190 | start = strstr(cline, "LABEL="); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 191 | if (start != nullptr && sscanf(start + 6, "\"%127[^\"]\"", value) == 1) { |
| 192 | fsLabel = value; |
| 193 | } |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 196 | return OK; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 199 | status_t ReadMetadata(const std::string& path, std::string& fsType, |
| 200 | std::string& fsUuid, std::string& fsLabel) { |
| 201 | return readMetadata(path, fsType, fsUuid, fsLabel, false); |
| 202 | } |
| 203 | |
| 204 | status_t ReadMetadataUntrusted(const std::string& path, std::string& fsType, |
| 205 | std::string& fsUuid, std::string& fsLabel) { |
| 206 | return readMetadata(path, fsType, fsUuid, fsLabel, true); |
| 207 | } |
| 208 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 209 | status_t ForkExecvp(const std::vector<std::string>& args) { |
| 210 | return ForkExecvp(args, nullptr); |
| 211 | } |
| 212 | |
| 213 | status_t ForkExecvp(const std::vector<std::string>& args, security_context_t context) { |
| 214 | size_t argc = args.size(); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 215 | char** argv = (char**) calloc(argc, sizeof(char*)); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 216 | for (size_t i = 0; i < argc; i++) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 217 | argv[i] = (char*) args[i].c_str(); |
| 218 | if (i == 0) { |
| 219 | LOG(VERBOSE) << args[i]; |
| 220 | } else { |
| 221 | LOG(VERBOSE) << " " << args[i]; |
| 222 | } |
| 223 | } |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 224 | |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 225 | if (setexeccon(context)) { |
| 226 | LOG(ERROR) << "Failed to setexeccon"; |
| 227 | abort(); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 228 | } |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 229 | status_t res = android_fork_execvp(argc, argv, NULL, false, true); |
| 230 | if (setexeccon(nullptr)) { |
| 231 | LOG(ERROR) << "Failed to setexeccon"; |
| 232 | abort(); |
| 233 | } |
| 234 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 235 | free(argv); |
| 236 | return res; |
| 237 | } |
| 238 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 239 | status_t ForkExecvp(const std::vector<std::string>& args, |
| 240 | std::vector<std::string>& output) { |
| 241 | return ForkExecvp(args, output, nullptr); |
| 242 | } |
| 243 | |
| 244 | status_t ForkExecvp(const std::vector<std::string>& args, |
| 245 | std::vector<std::string>& output, security_context_t context) { |
| 246 | std::string cmd; |
| 247 | for (size_t i = 0; i < args.size(); i++) { |
| 248 | cmd += args[i] + " "; |
| 249 | if (i == 0) { |
| 250 | LOG(VERBOSE) << args[i]; |
| 251 | } else { |
| 252 | LOG(VERBOSE) << " " << args[i]; |
| 253 | } |
| 254 | } |
| 255 | output.clear(); |
| 256 | |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 257 | if (setexeccon(context)) { |
| 258 | LOG(ERROR) << "Failed to setexeccon"; |
| 259 | abort(); |
| 260 | } |
| 261 | FILE* fp = popen(cmd.c_str(), "r"); |
| 262 | if (setexeccon(nullptr)) { |
| 263 | LOG(ERROR) << "Failed to setexeccon"; |
| 264 | abort(); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | if (!fp) { |
| 268 | PLOG(ERROR) << "Failed to popen " << cmd; |
| 269 | return -errno; |
| 270 | } |
| 271 | char line[1024]; |
| 272 | while (fgets(line, sizeof(line), fp) != nullptr) { |
| 273 | LOG(VERBOSE) << line; |
| 274 | output.push_back(std::string(line)); |
| 275 | } |
| 276 | if (pclose(fp) != 0) { |
| 277 | PLOG(ERROR) << "Failed to pclose " << cmd; |
| 278 | return -errno; |
| 279 | } |
| 280 | |
| 281 | return OK; |
| 282 | } |
| 283 | |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 284 | pid_t ForkExecvpAsync(const std::vector<std::string>& args) { |
| 285 | size_t argc = args.size(); |
| 286 | char** argv = (char**) calloc(argc + 1, sizeof(char*)); |
| 287 | for (size_t i = 0; i < argc; i++) { |
| 288 | argv[i] = (char*) args[i].c_str(); |
| 289 | if (i == 0) { |
| 290 | LOG(VERBOSE) << args[i]; |
| 291 | } else { |
| 292 | LOG(VERBOSE) << " " << args[i]; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | pid_t pid = fork(); |
| 297 | if (pid == 0) { |
| 298 | close(STDIN_FILENO); |
| 299 | close(STDOUT_FILENO); |
| 300 | close(STDERR_FILENO); |
| 301 | |
| 302 | if (execvp(argv[0], argv)) { |
| 303 | PLOG(ERROR) << "Failed to exec"; |
| 304 | } |
| 305 | |
| 306 | _exit(1); |
| 307 | } |
| 308 | |
| 309 | if (pid == -1) { |
| 310 | PLOG(ERROR) << "Failed to exec"; |
| 311 | } |
| 312 | |
| 313 | free(argv); |
| 314 | return pid; |
| 315 | } |
| 316 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 317 | status_t ReadRandomBytes(size_t bytes, std::string& out) { |
| 318 | out.clear(); |
| 319 | |
| 320 | int fd = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW)); |
| 321 | if (fd == -1) { |
| 322 | return -errno; |
| 323 | } |
| 324 | |
| 325 | char buf[BUFSIZ]; |
| 326 | size_t n; |
| 327 | while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], std::min(sizeof(buf), bytes)))) > 0) { |
| 328 | out.append(buf, n); |
| 329 | bytes -= n; |
| 330 | } |
| 331 | TEMP_FAILURE_RETRY(close(fd)); |
| 332 | |
| 333 | if (bytes == 0) { |
| 334 | return OK; |
| 335 | } else { |
| 336 | return -EIO; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | status_t HexToStr(const std::string& hex, std::string& str) { |
| 341 | str.clear(); |
| 342 | bool even = true; |
| 343 | char cur = 0; |
| 344 | for (size_t i = 0; i < hex.size(); i++) { |
| 345 | int val = 0; |
| 346 | switch (hex[i]) { |
| 347 | case ' ': case '-': case ':': continue; |
| 348 | case 'f': case 'F': val = 15; break; |
| 349 | case 'e': case 'E': val = 14; break; |
| 350 | case 'd': case 'D': val = 13; break; |
| 351 | case 'c': case 'C': val = 12; break; |
| 352 | case 'b': case 'B': val = 11; break; |
| 353 | case 'a': case 'A': val = 10; break; |
| 354 | case '9': val = 9; break; |
| 355 | case '8': val = 8; break; |
| 356 | case '7': val = 7; break; |
| 357 | case '6': val = 6; break; |
| 358 | case '5': val = 5; break; |
| 359 | case '4': val = 4; break; |
| 360 | case '3': val = 3; break; |
| 361 | case '2': val = 2; break; |
| 362 | case '1': val = 1; break; |
| 363 | case '0': val = 0; break; |
| 364 | default: return -EINVAL; |
| 365 | } |
| 366 | |
| 367 | if (even) { |
| 368 | cur = val << 4; |
| 369 | } else { |
| 370 | cur += val; |
| 371 | str.push_back(cur); |
| 372 | cur = 0; |
| 373 | } |
| 374 | even = !even; |
| 375 | } |
| 376 | return even ? OK : -EINVAL; |
| 377 | } |
| 378 | |
| 379 | static const char* kLookup = "0123456789abcdef"; |
| 380 | |
| 381 | status_t StrToHex(const std::string& str, std::string& hex) { |
| 382 | hex.clear(); |
| 383 | for (size_t i = 0; i < str.size(); i++) { |
| 384 | hex.push_back(kLookup[str[i] >> 4]); |
| 385 | hex.push_back(kLookup[str[i] & 0x0F]); |
| 386 | } |
| 387 | return OK; |
| 388 | } |
| 389 | |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 390 | uint64_t GetFreeBytes(const std::string& path) { |
| 391 | struct statvfs sb; |
| 392 | if (statvfs(path.c_str(), &sb) == 0) { |
| 393 | return sb.f_bfree * sb.f_bsize; |
| 394 | } else { |
| 395 | return -1; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | // TODO: borrowed from frameworks/native/libs/diskusage/ which should |
| 400 | // eventually be migrated into system/ |
| 401 | static int64_t stat_size(struct stat *s) { |
| 402 | int64_t blksize = s->st_blksize; |
| 403 | // count actual blocks used instead of nominal file size |
| 404 | int64_t size = s->st_blocks * 512; |
| 405 | |
| 406 | if (blksize) { |
| 407 | /* round up to filesystem block size */ |
| 408 | size = (size + blksize - 1) & (~(blksize - 1)); |
| 409 | } |
| 410 | |
| 411 | return size; |
| 412 | } |
| 413 | |
| 414 | // TODO: borrowed from frameworks/native/libs/diskusage/ which should |
| 415 | // eventually be migrated into system/ |
| 416 | int64_t calculate_dir_size(int dfd) { |
| 417 | int64_t size = 0; |
| 418 | struct stat s; |
| 419 | DIR *d; |
| 420 | struct dirent *de; |
| 421 | |
| 422 | d = fdopendir(dfd); |
| 423 | if (d == NULL) { |
| 424 | close(dfd); |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | while ((de = readdir(d))) { |
| 429 | const char *name = de->d_name; |
| 430 | if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { |
| 431 | size += stat_size(&s); |
| 432 | } |
| 433 | if (de->d_type == DT_DIR) { |
| 434 | int subfd; |
| 435 | |
| 436 | /* always skip "." and ".." */ |
| 437 | if (name[0] == '.') { |
| 438 | if (name[1] == 0) |
| 439 | continue; |
| 440 | if ((name[1] == '.') && (name[2] == 0)) |
| 441 | continue; |
| 442 | } |
| 443 | |
| 444 | subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY); |
| 445 | if (subfd >= 0) { |
| 446 | size += calculate_dir_size(subfd); |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | closedir(d); |
| 451 | return size; |
| 452 | } |
| 453 | |
| 454 | uint64_t GetTreeBytes(const std::string& path) { |
| 455 | int dirfd = open(path.c_str(), O_DIRECTORY, O_RDONLY); |
| 456 | if (dirfd < 0) { |
| 457 | PLOG(WARNING) << "Failed to open " << path; |
| 458 | return -1; |
| 459 | } else { |
| 460 | uint64_t res = calculate_dir_size(dirfd); |
| 461 | close(dirfd); |
| 462 | return res; |
| 463 | } |
| 464 | } |
| 465 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 466 | } // namespace vold |
| 467 | } // namespace android |