]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/stat-util.c
util-lib: split stat()/statfs()/stavfs() related calls into stat-util.[ch]
[thirdparty/systemd.git] / src / basic / stat-util.c
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 <linux/magic.h>
24 #include <sys/statvfs.h>
25 #include <unistd.h>
26
27 #include "dirent-util.h"
28 #include "fd-util.h"
29 #include "macro.h"
30 #include "missing.h"
31 #include "stat-util.h"
32 #include "string-util.h"
33
34 int is_symlink(const char *path) {
35 struct stat info;
36
37 assert(path);
38
39 if (lstat(path, &info) < 0)
40 return -errno;
41
42 return !!S_ISLNK(info.st_mode);
43 }
44
45 int is_dir(const char* path, bool follow) {
46 struct stat st;
47 int r;
48
49 assert(path);
50
51 if (follow)
52 r = stat(path, &st);
53 else
54 r = lstat(path, &st);
55 if (r < 0)
56 return -errno;
57
58 return !!S_ISDIR(st.st_mode);
59 }
60
61 int is_device_node(const char *path) {
62 struct stat info;
63
64 assert(path);
65
66 if (lstat(path, &info) < 0)
67 return -errno;
68
69 return !!(S_ISBLK(info.st_mode) || S_ISCHR(info.st_mode));
70 }
71
72 int dir_is_empty(const char *path) {
73 _cleanup_closedir_ DIR *d;
74 struct dirent *de;
75
76 d = opendir(path);
77 if (!d)
78 return -errno;
79
80 FOREACH_DIRENT(de, d, return -errno)
81 return 0;
82
83 return 1;
84 }
85
86 bool null_or_empty(struct stat *st) {
87 assert(st);
88
89 if (S_ISREG(st->st_mode) && st->st_size <= 0)
90 return true;
91
92 /* We don't want to hardcode the major/minor of /dev/null,
93 * hence we do a simpler "is this a device node?" check. */
94
95 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode))
96 return true;
97
98 return false;
99 }
100
101 int null_or_empty_path(const char *fn) {
102 struct stat st;
103
104 assert(fn);
105
106 if (stat(fn, &st) < 0)
107 return -errno;
108
109 return null_or_empty(&st);
110 }
111
112 int null_or_empty_fd(int fd) {
113 struct stat st;
114
115 assert(fd >= 0);
116
117 if (fstat(fd, &st) < 0)
118 return -errno;
119
120 return null_or_empty(&st);
121 }
122
123 int path_is_read_only_fs(const char *path) {
124 struct statvfs st;
125
126 assert(path);
127
128 if (statvfs(path, &st) < 0)
129 return -errno;
130
131 if (st.f_flag & ST_RDONLY)
132 return true;
133
134 /* On NFS, statvfs() might not reflect whether we can actually
135 * write to the remote share. Let's try again with
136 * access(W_OK) which is more reliable, at least sometimes. */
137 if (access(path, W_OK) < 0 && errno == EROFS)
138 return true;
139
140 return false;
141 }
142
143 int path_is_os_tree(const char *path) {
144 char *p;
145 int r;
146
147 assert(path);
148
149 /* We use /usr/lib/os-release as flag file if something is an OS */
150 p = strjoina(path, "/usr/lib/os-release");
151 r = access(p, F_OK);
152 if (r >= 0)
153 return 1;
154
155 /* Also check for the old location in /etc, just in case. */
156 p = strjoina(path, "/etc/os-release");
157 r = access(p, F_OK);
158
159 return r >= 0;
160 }
161
162 int files_same(const char *filea, const char *fileb) {
163 struct stat a, b;
164
165 assert(filea);
166 assert(fileb);
167
168 if (stat(filea, &a) < 0)
169 return -errno;
170
171 if (stat(fileb, &b) < 0)
172 return -errno;
173
174 return a.st_dev == b.st_dev &&
175 a.st_ino == b.st_ino;
176 }
177
178 bool is_fs_type(const struct statfs *s, statfs_f_type_t magic_value) {
179 assert(s);
180 assert_cc(sizeof(statfs_f_type_t) >= sizeof(s->f_type));
181
182 return F_TYPE_EQUAL(s->f_type, magic_value);
183 }
184
185 int fd_check_fstype(int fd, statfs_f_type_t magic_value) {
186 struct statfs s;
187
188 if (fstatfs(fd, &s) < 0)
189 return -errno;
190
191 return is_fs_type(&s, magic_value);
192 }
193
194 int path_check_fstype(const char *path, statfs_f_type_t magic_value) {
195 _cleanup_close_ int fd = -1;
196
197 fd = open(path, O_RDONLY);
198 if (fd < 0)
199 return -errno;
200
201 return fd_check_fstype(fd, magic_value);
202 }
203
204 bool is_temporary_fs(const struct statfs *s) {
205 return is_fs_type(s, TMPFS_MAGIC) ||
206 is_fs_type(s, RAMFS_MAGIC);
207 }
208
209 int fd_is_temporary_fs(int fd) {
210 struct statfs s;
211
212 if (fstatfs(fd, &s) < 0)
213 return -errno;
214
215 return is_temporary_fs(&s);
216 }