1 /*
2  *
3  * Copyright (C) 2008, The Android Open Source Project
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17 
18 #include <dirent.h>
19 #include <fcntl.h>
20 #include <sys/stat.h>
21 #include <unistd.h>
22 
23 #include <diskusage/dirsize.h>
24 
stat_size(struct stat * s)25 int64_t stat_size(struct stat *s)
26 {
27     int64_t blksize = s->st_blksize;
28     // count actual blocks used instead of nominal file size
29     int64_t size = s->st_blocks * 512;
30 
31     if (blksize) {
32         /* round up to filesystem block size */
33         size = (size + blksize - 1) & (~(blksize - 1));
34     }
35 
36     return size;
37 }
38 
calculate_dir_size(int dfd)39 int64_t calculate_dir_size(int dfd)
40 {
41     int64_t size = 0;
42     struct stat s;
43     DIR *d;
44     struct dirent *de;
45 
46     d = fdopendir(dfd);
47     if (d == NULL) {
48         close(dfd);
49         return 0;
50     }
51 
52     while ((de = readdir(d))) {
53         const char *name = de->d_name;
54         if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
55             size += stat_size(&s);
56         }
57         if (de->d_type == DT_DIR) {
58             int subfd;
59 
60             /* always skip "." and ".." */
61             if (name[0] == '.') {
62                 if (name[1] == 0)
63                     continue;
64                 if ((name[1] == '.') && (name[2] == 0))
65                     continue;
66             }
67 
68             subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
69             if (subfd >= 0) {
70                 size += calculate_dir_size(subfd);
71             }
72         }
73     }
74     closedir(d);
75     return size;
76 }
77