1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010-2012 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include <linux/magic.h>
27 #include <sys/statvfs.h>
28 #include <sys/types.h>
31 #include "dirent-util.h"
36 #include "stat-util.h"
37 #include "string-util.h"
39 int is_symlink(const char *path
) {
44 if (lstat(path
, &info
) < 0)
47 return !!S_ISLNK(info
.st_mode
);
50 int is_dir(const char* path
, bool follow
) {
63 return !!S_ISDIR(st
.st_mode
);
66 int is_device_node(const char *path
) {
71 if (lstat(path
, &info
) < 0)
74 return !!(S_ISBLK(info
.st_mode
) || S_ISCHR(info
.st_mode
));
77 int dir_is_empty(const char *path
) {
78 _cleanup_closedir_
DIR *d
;
85 FOREACH_DIRENT(de
, d
, return -errno
)
91 bool null_or_empty(struct stat
*st
) {
94 if (S_ISREG(st
->st_mode
) && st
->st_size
<= 0)
97 /* We don't want to hardcode the major/minor of /dev/null,
98 * hence we do a simpler "is this a device node?" check. */
100 if (S_ISCHR(st
->st_mode
) || S_ISBLK(st
->st_mode
))
106 int null_or_empty_path(const char *fn
) {
111 if (stat(fn
, &st
) < 0)
114 return null_or_empty(&st
);
117 int null_or_empty_fd(int fd
) {
122 if (fstat(fd
, &st
) < 0)
125 return null_or_empty(&st
);
128 int path_is_read_only_fs(const char *path
) {
133 if (statvfs(path
, &st
) < 0)
136 if (st
.f_flag
& ST_RDONLY
)
139 /* On NFS, statvfs() might not reflect whether we can actually
140 * write to the remote share. Let's try again with
141 * access(W_OK) which is more reliable, at least sometimes. */
142 if (access(path
, W_OK
) < 0 && errno
== EROFS
)
148 int path_is_os_tree(const char *path
) {
153 /* Does the path exist at all? If not, generate an error immediately. This is useful so that a missing root dir
154 * always results in -ENOENT, and we can properly distuingish the case where the whole root doesn't exist from
155 * the case where just the os-release file is missing. */
156 if (laccess(path
, F_OK
) < 0)
159 /* We use /usr/lib/os-release as flag file if something is an OS */
160 r
= chase_symlinks("/usr/lib/os-release", path
, CHASE_PREFIX_ROOT
, NULL
);
163 /* Also check for the old location in /etc, just in case. */
164 r
= chase_symlinks("/etc/os-release", path
, CHASE_PREFIX_ROOT
, NULL
);
166 return 0; /* We got nothing */
174 int files_same(const char *filea
, const char *fileb
, int flags
) {
180 if (fstatat(AT_FDCWD
, filea
, &a
, flags
) < 0)
183 if (fstatat(AT_FDCWD
, fileb
, &b
, flags
) < 0)
186 return a
.st_dev
== b
.st_dev
&&
187 a
.st_ino
== b
.st_ino
;
190 bool is_fs_type(const struct statfs
*s
, statfs_f_type_t magic_value
) {
192 assert_cc(sizeof(statfs_f_type_t
) >= sizeof(s
->f_type
));
194 return F_TYPE_EQUAL(s
->f_type
, magic_value
);
197 int fd_is_fs_type(int fd
, statfs_f_type_t magic_value
) {
200 if (fstatfs(fd
, &s
) < 0)
203 return is_fs_type(&s
, magic_value
);
206 int path_is_fs_type(const char *path
, statfs_f_type_t magic_value
) {
207 _cleanup_close_
int fd
= -1;
209 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_PATH
);
213 return fd_is_fs_type(fd
, magic_value
);
216 bool is_temporary_fs(const struct statfs
*s
) {
217 return is_fs_type(s
, TMPFS_MAGIC
) ||
218 is_fs_type(s
, RAMFS_MAGIC
);
221 int fd_is_temporary_fs(int fd
) {
224 if (fstatfs(fd
, &s
) < 0)
227 return is_temporary_fs(&s
);
230 int fd_is_network_ns(int fd
) {
233 r
= fd_is_fs_type(fd
, NSFS_MAGIC
);
236 r
= ioctl(fd
, NS_GET_NSTYPE
);
239 return r
== CLONE_NEWNET
;
242 int path_is_temporary_fs(const char *path
) {
243 _cleanup_close_
int fd
= -1;
245 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_PATH
);
249 return fd_is_temporary_fs(fd
);