1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010-2012 Lennart Poettering
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.
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.
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/>.
23 #include <linux/magic.h>
24 #include <sys/statvfs.h>
27 #include "dirent-util.h"
31 #include "stat-util.h"
32 #include "string-util.h"
34 #if 0 /// UNNEEDED by elogind
35 int is_symlink(const char *path) {
40 if (lstat(path, &info) < 0)
43 return !!S_ISLNK(info.st_mode);
47 int is_dir(const char* path, bool follow) {
60 return !!S_ISDIR(st.st_mode);
63 #if 0 /// UNNEEDED by elogind
64 int is_device_node(const char *path) {
69 if (lstat(path, &info) < 0)
72 return !!(S_ISBLK(info.st_mode) || S_ISCHR(info.st_mode));
75 int dir_is_empty(const char *path) {
76 _cleanup_closedir_ DIR *d;
83 FOREACH_DIRENT(de, d, return -errno)
90 bool null_or_empty(struct stat *st) {
93 if (S_ISREG(st->st_mode) && st->st_size <= 0)
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. */
99 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode))
105 int null_or_empty_path(const char *fn) {
110 if (stat(fn, &st) < 0)
113 return null_or_empty(&st);
116 #if 0 /// UNNEEDED by elogind
117 int null_or_empty_fd(int fd) {
122 if (fstat(fd, &st) < 0)
125 return null_or_empty(&st);
129 int path_is_read_only_fs(const char *path) {
134 if (statvfs(path, &st) < 0)
137 if (st.f_flag & ST_RDONLY)
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)
149 #if 0 /// UNNEEDED by elogind
150 int path_is_os_tree(const char *path) {
156 /* We use /usr/lib/os-release as flag file if something is an OS */
157 p = strjoina(path, "/usr/lib/os-release");
162 /* Also check for the old location in /etc, just in case. */
163 p = strjoina(path, "/etc/os-release");
170 int files_same(const char *filea, const char *fileb) {
176 if (stat(filea, &a) < 0)
179 if (stat(fileb, &b) < 0)
182 return a.st_dev == b.st_dev &&
183 a.st_ino == b.st_ino;
186 bool is_fs_type(const struct statfs *s, statfs_f_type_t magic_value) {
188 assert_cc(sizeof(statfs_f_type_t) >= sizeof(s->f_type));
190 return F_TYPE_EQUAL(s->f_type, magic_value);
193 #if 0 /// UNNEEDED by elogind
194 int fd_check_fstype(int fd, statfs_f_type_t magic_value) {
197 if (fstatfs(fd, &s) < 0)
200 return is_fs_type(&s, magic_value);
203 int path_check_fstype(const char *path, statfs_f_type_t magic_value) {
204 _cleanup_close_ int fd = -1;
206 fd = open(path, O_RDONLY);
210 return fd_check_fstype(fd, magic_value);
214 bool is_temporary_fs(const struct statfs *s) {
215 return is_fs_type(s, TMPFS_MAGIC) ||
216 is_fs_type(s, RAMFS_MAGIC);
219 int fd_is_temporary_fs(int fd) {
222 if (fstatfs(fd, &s) < 0)
225 return is_temporary_fs(&s);