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