3 * Copyright (C) 2008, The Android Open Source Project
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
22 #include <diskusage/dirsize.h>
24 int64_t stat_size(struct stat *s)
26 int64_t blksize = s->st_blksize;
27 // count actual blocks used instead of nominal file size
28 int64_t size = s->st_blocks * 512;
31 /* round up to filesystem block size */
32 size = (size + blksize - 1) & (~(blksize - 1));
38 int64_t calculate_dir_size(int dfd)
51 while ((de = readdir(d))) {
52 const char *name = de->d_name;
53 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
54 size += stat_size(&s);
56 if (de->d_type == DT_DIR) {
59 /* always skip "." and ".." */
63 if ((name[1] == '.') && (name[2] == 0))
67 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
69 size += calculate_dir_size(subfd);