]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/dirent-util.c
Merge pull request #2068 from grawity/cgls-error-v2
[thirdparty/systemd.git] / src / basic / dirent-util.c
CommitLineData
a0956174
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010-2012 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
22#include <fcntl.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <unistd.h>
26
27#include "dirent-util.h"
28#include "string-util.h"
29
30int dirent_ensure_type(DIR *d, struct dirent *de) {
31 struct stat st;
32
33 assert(d);
34 assert(de);
35
36 if (de->d_type != DT_UNKNOWN)
37 return 0;
38
39 if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
40 return -errno;
41
42 de->d_type =
43 S_ISREG(st.st_mode) ? DT_REG :
44 S_ISDIR(st.st_mode) ? DT_DIR :
45 S_ISLNK(st.st_mode) ? DT_LNK :
46 S_ISFIFO(st.st_mode) ? DT_FIFO :
47 S_ISSOCK(st.st_mode) ? DT_SOCK :
48 S_ISCHR(st.st_mode) ? DT_CHR :
49 S_ISBLK(st.st_mode) ? DT_BLK :
50 DT_UNKNOWN;
51
52 return 0;
53}
54
55bool dirent_is_file(const struct dirent *de) {
56 assert(de);
57
58 if (hidden_file(de->d_name))
59 return false;
60
61 if (de->d_type != DT_REG &&
62 de->d_type != DT_LNK &&
63 de->d_type != DT_UNKNOWN)
64 return false;
65
66 return true;
67}
68
69bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) {
70 assert(de);
71
72 if (de->d_type != DT_REG &&
73 de->d_type != DT_LNK &&
74 de->d_type != DT_UNKNOWN)
75 return false;
76
77 if (hidden_file_allow_backup(de->d_name))
78 return false;
79
80 return endswith(de->d_name, suffix);
81}