2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2015-10-26 20:07:55 +01:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include "dirent-util.h"
|
2015-11-30 21:43:37 +01:00
|
|
|
#include "path-util.h"
|
2015-10-26 20:07:55 +01:00
|
|
|
#include "string-util.h"
|
|
|
|
|
|
|
|
int dirent_ensure_type(DIR *d, struct dirent *de) {
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
assert(d);
|
|
|
|
assert(de);
|
|
|
|
|
|
|
|
if (de->d_type != DT_UNKNOWN)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
de->d_type =
|
|
|
|
S_ISREG(st.st_mode) ? DT_REG :
|
|
|
|
S_ISDIR(st.st_mode) ? DT_DIR :
|
|
|
|
S_ISLNK(st.st_mode) ? DT_LNK :
|
|
|
|
S_ISFIFO(st.st_mode) ? DT_FIFO :
|
|
|
|
S_ISSOCK(st.st_mode) ? DT_SOCK :
|
|
|
|
S_ISCHR(st.st_mode) ? DT_CHR :
|
|
|
|
S_ISBLK(st.st_mode) ? DT_BLK :
|
|
|
|
DT_UNKNOWN;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool dirent_is_file(const struct dirent *de) {
|
|
|
|
assert(de);
|
|
|
|
|
2016-04-27 15:24:59 +02:00
|
|
|
if (!IN_SET(de->d_type, DT_REG, DT_LNK, DT_UNKNOWN))
|
2015-10-26 20:07:55 +01:00
|
|
|
return false;
|
|
|
|
|
2016-04-27 15:24:59 +02:00
|
|
|
if (hidden_or_backup_file(de->d_name))
|
2015-10-26 20:07:55 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) {
|
|
|
|
assert(de);
|
|
|
|
|
2016-04-27 14:59:12 +02:00
|
|
|
if (!IN_SET(de->d_type, DT_REG, DT_LNK, DT_UNKNOWN))
|
2015-10-26 20:07:55 +01:00
|
|
|
return false;
|
|
|
|
|
2016-04-27 14:59:12 +02:00
|
|
|
if (de->d_name[0] == '.')
|
2015-10-26 20:07:55 +01:00
|
|
|
return false;
|
|
|
|
|
2017-02-08 03:06:38 +01:00
|
|
|
if (!suffix)
|
|
|
|
return true;
|
|
|
|
|
2015-10-26 20:07:55 +01:00
|
|
|
return endswith(de->d_name, suffix);
|
|
|
|
}
|
2017-04-26 05:44:34 +02:00
|
|
|
|
|
|
|
struct dirent* readdir_no_dot(DIR *dirp) {
|
|
|
|
struct dirent* d;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
d = readdir(dirp);
|
|
|
|
if (d && dot_or_dot_dot(d->d_name))
|
|
|
|
continue;
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
}
|