Home | History | Annotate | Download | only in diskusage
      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 
     22 #include <diskusage/dirsize.h>
     23 
     24 int64_t stat_size(struct stat *s)
     25 {
     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;
     29 
     30     if (blksize) {
     31         /* round up to filesystem block size */
     32         size = (size + blksize - 1) & (~(blksize - 1));
     33     }
     34 
     35     return size;
     36 }
     37 
     38 int64_t calculate_dir_size(int dfd)
     39 {
     40     int64_t size = 0;
     41     struct stat s;
     42     DIR *d;
     43     struct dirent *de;
     44 
     45     d = fdopendir(dfd);
     46     if (d == NULL) {
     47         close(dfd);
     48         return 0;
     49     }
     50 
     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);
     55         }
     56         if (de->d_type == DT_DIR) {
     57             int subfd;
     58 
     59             /* always skip "." and ".." */
     60             if (name[0] == '.') {
     61                 if (name[1] == 0)
     62                     continue;
     63                 if ((name[1] == '.') && (name[2] == 0))
     64                     continue;
     65             }
     66 
     67             subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
     68             if (subfd >= 0) {
     69                 size += calculate_dir_size(subfd);
     70             }
     71         }
     72     }
     73     closedir(d);
     74     return size;
     75 }
     76