LCOV - code coverage report
Current view: top level - basic - dirent-util.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 17 37 45.9 %
Date: 2019-08-22 15:41:25 Functions: 3 4 75.0 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include <fcntl.h>
       4             : #include <sys/stat.h>
       5             : 
       6             : #include "dirent-util.h"
       7             : #include "path-util.h"
       8             : #include "string-util.h"
       9             : 
      10      579325 : int dirent_ensure_type(DIR *d, struct dirent *de) {
      11             :         struct stat st;
      12             : 
      13      579325 :         assert(d);
      14      579325 :         assert(de);
      15             : 
      16      579325 :         if (de->d_type != DT_UNKNOWN)
      17      579325 :                 return 0;
      18             : 
      19           0 :         if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
      20           0 :                 return -errno;
      21             : 
      22           0 :         de->d_type =
      23           0 :                 S_ISREG(st.st_mode)  ? DT_REG  :
      24           0 :                 S_ISDIR(st.st_mode)  ? DT_DIR  :
      25           0 :                 S_ISLNK(st.st_mode)  ? DT_LNK  :
      26           0 :                 S_ISFIFO(st.st_mode) ? DT_FIFO :
      27           0 :                 S_ISSOCK(st.st_mode) ? DT_SOCK :
      28           0 :                 S_ISCHR(st.st_mode)  ? DT_CHR  :
      29           0 :                 S_ISBLK(st.st_mode)  ? DT_BLK  :
      30             :                                        DT_UNKNOWN;
      31             : 
      32           0 :         return 0;
      33             : }
      34             : 
      35        1621 : bool dirent_is_file(const struct dirent *de) {
      36        1621 :         assert(de);
      37             : 
      38        1621 :         if (!IN_SET(de->d_type, DT_REG, DT_LNK, DT_UNKNOWN))
      39         811 :                 return false;
      40             : 
      41         810 :         if (hidden_or_backup_file(de->d_name))
      42           4 :                 return false;
      43             : 
      44         806 :         return true;
      45             : }
      46             : 
      47           0 : bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) {
      48           0 :         assert(de);
      49             : 
      50           0 :         if (!IN_SET(de->d_type, DT_REG, DT_LNK, DT_UNKNOWN))
      51           0 :                 return false;
      52             : 
      53           0 :         if (de->d_name[0] == '.')
      54           0 :                 return false;
      55             : 
      56           0 :         if (!suffix)
      57           0 :                 return true;
      58             : 
      59           0 :         return endswith(de->d_name, suffix);
      60             : }
      61             : 
      62       10138 : struct dirent* readdir_no_dot(DIR *dirp) {
      63             :         struct dirent* d;
      64             : 
      65             :         for (;;) {
      66       10138 :                 d = readdir(dirp);
      67       10138 :                 if (d && dot_or_dot_dot(d->d_name))
      68          26 :                         continue;
      69       10112 :                 return d;
      70             :         }
      71             : }

Generated by: LCOV version 1.14