1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010-2012 Lennart Poettering
11 #include <linux/magic.h>
13 //#include <sys/stat.h>
14 #include <sys/statvfs.h>
15 //#include <sys/types.h>
18 #include "dirent-util.h"
23 #include "stat-util.h"
24 #include "string-util.h"
26 #if 0 /// UNNEEDED by elogind
27 int is_symlink(const char *path) {
32 if (lstat(path, &info) < 0)
35 return !!S_ISLNK(info.st_mode);
39 int is_dir(const char* path, bool follow) {
52 return !!S_ISDIR(st.st_mode);
55 #if 0 /// UNNEEDED by elogind
56 int is_device_node(const char *path) {
61 if (lstat(path, &info) < 0)
64 return !!(S_ISBLK(info.st_mode) || S_ISCHR(info.st_mode));
67 int dir_is_empty(const char *path) {
68 _cleanup_closedir_ DIR *d;
75 FOREACH_DIRENT(de, d, return -errno)
82 bool null_or_empty(struct stat *st) {
85 if (S_ISREG(st->st_mode) && st->st_size <= 0)
88 /* We don't want to hardcode the major/minor of /dev/null,
89 * hence we do a simpler "is this a device node?" check. */
91 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode))
97 int null_or_empty_path(const char *fn) {
102 if (stat(fn, &st) < 0)
105 return null_or_empty(&st);
108 #if 0 /// UNNEEDED by elogind
109 int null_or_empty_fd(int fd) {
114 if (fstat(fd, &st) < 0)
117 return null_or_empty(&st);
121 int path_is_read_only_fs(const char *path) {
126 if (statvfs(path, &st) < 0)
129 if (st.f_flag & ST_RDONLY)
132 /* On NFS, statvfs() might not reflect whether we can actually
133 * write to the remote share. Let's try again with
134 * access(W_OK) which is more reliable, at least sometimes. */
135 if (access(path, W_OK) < 0 && errno == EROFS)
141 #if 0 /// UNNEEDED by elogind
143 int files_same(const char *filea, const char *fileb, int flags) {
149 if (fstatat(AT_FDCWD, filea, &a, flags) < 0)
152 if (fstatat(AT_FDCWD, fileb, &b, flags) < 0)
155 return a.st_dev == b.st_dev &&
156 a.st_ino == b.st_ino;
159 bool is_fs_type(const struct statfs *s, statfs_f_type_t magic_value) {
161 assert_cc(sizeof(statfs_f_type_t) >= sizeof(s->f_type));
163 return F_TYPE_EQUAL(s->f_type, magic_value);
166 int fd_is_fs_type(int fd, statfs_f_type_t magic_value) {
169 if (fstatfs(fd, &s) < 0)
172 return is_fs_type(&s, magic_value);
175 #if 0 /// UNNEEDED by elogind
176 int path_is_fs_type(const char *path, statfs_f_type_t magic_value) {
177 _cleanup_close_ int fd = -1;
179 fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_PATH);
183 return fd_is_fs_type(fd, magic_value);
187 bool is_temporary_fs(const struct statfs *s) {
188 return is_fs_type(s, TMPFS_MAGIC) ||
189 is_fs_type(s, RAMFS_MAGIC);
192 bool is_network_fs(const struct statfs *s) {
193 return is_fs_type(s, CIFS_MAGIC_NUMBER) ||
194 is_fs_type(s, CODA_SUPER_MAGIC) ||
195 is_fs_type(s, NCP_SUPER_MAGIC) ||
196 is_fs_type(s, NFS_SUPER_MAGIC) ||
197 is_fs_type(s, SMB_SUPER_MAGIC) ||
198 is_fs_type(s, V9FS_MAGIC) ||
199 is_fs_type(s, AFS_SUPER_MAGIC) ||
200 is_fs_type(s, OCFS2_SUPER_MAGIC);
203 #if 0 /// UNNEEDED by elogind
204 int fd_is_temporary_fs(int fd) {
207 if (fstatfs(fd, &s) < 0)
210 return is_temporary_fs(&s);
213 int fd_is_network_fs(int fd) {
216 if (fstatfs(fd, &s) < 0)
219 return is_network_fs(&s);
222 int fd_is_network_ns(int fd) {
225 r = fd_is_fs_type(fd, NSFS_MAGIC);
229 r = ioctl(fd, NS_GET_NSTYPE);
233 return r == CLONE_NEWNET;
236 int path_is_temporary_fs(const char *path) {
237 _cleanup_close_ int fd = -1;
239 fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_PATH);
243 return fd_is_temporary_fs(fd);
247 int stat_verify_regular(const struct stat *st) {
250 /* Checks whether the specified stat() structure refers to a regular file. If not returns an appropriate error
253 if (S_ISDIR(st->st_mode))
256 if (S_ISLNK(st->st_mode))
259 if (!S_ISREG(st->st_mode))
265 int fd_verify_regular(int fd) {
270 if (fstat(fd, &st) < 0)
273 return stat_verify_regular(&st);