chiark / gitweb /
tree-wide: remove Lennart's copyright lines
[elogind.git] / src / basic / stat-util.c
index 5bc11e4a32fb7aa7915cc6006eb83428d2f5c930..ee064d2ae24130ce722d4798a70476f099325108 100644 (file)
@@ -1,31 +1,18 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
-  This file is part of systemd.
-
-  Copyright 2010-2012 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
+/* SPDX-License-Identifier: LGPL-2.1+ */
 
+#include <dirent.h>
+#include <errno.h>
 #include <fcntl.h>
 #include <linux/magic.h>
+//#include <sched.h>
+//#include <sys/stat.h>
 #include <sys/statvfs.h>
+//#include <sys/types.h>
 #include <unistd.h>
 
 #include "dirent-util.h"
 #include "fd-util.h"
+#include "fs-util.h"
 #include "macro.h"
 #include "missing.h"
 #include "stat-util.h"
@@ -147,36 +134,17 @@ int path_is_read_only_fs(const char *path) {
 }
 
 #if 0 /// UNNEEDED by elogind
-int path_is_os_tree(const char *path) {
-        char *p;
-        int r;
-
-        assert(path);
-
-        /* We use /usr/lib/os-release as flag file if something is an OS */
-        p = strjoina(path, "/usr/lib/os-release");
-        r = access(p, F_OK);
-        if (r >= 0)
-                return 1;
-
-        /* Also check for the old location in /etc, just in case. */
-        p = strjoina(path, "/etc/os-release");
-        r = access(p, F_OK);
-
-        return r >= 0;
-}
 #endif // 0
-
-int files_same(const char *filea, const char *fileb) {
+int files_same(const char *filea, const char *fileb, int flags) {
         struct stat a, b;
 
         assert(filea);
         assert(fileb);
 
-        if (stat(filea, &a) < 0)
+        if (fstatat(AT_FDCWD, filea, &a, flags) < 0)
                 return -errno;
 
-        if (stat(fileb, &b) < 0)
+        if (fstatat(AT_FDCWD, fileb, &b, flags) < 0)
                 return -errno;
 
         return a.st_dev == b.st_dev &&
@@ -190,8 +158,7 @@ bool is_fs_type(const struct statfs *s, statfs_f_type_t magic_value) {
         return F_TYPE_EQUAL(s->f_type, magic_value);
 }
 
-#if 0 /// UNNEEDED by elogind
-int fd_check_fstype(int fd, statfs_f_type_t magic_value) {
+int fd_is_fs_type(int fd, statfs_f_type_t magic_value) {
         struct statfs s;
 
         if (fstatfs(fd, &s) < 0)
@@ -200,22 +167,35 @@ int fd_check_fstype(int fd, statfs_f_type_t magic_value) {
         return is_fs_type(&s, magic_value);
 }
 
-int path_check_fstype(const char *path, statfs_f_type_t magic_value) {
+#if 0 /// UNNEEDED by elogind
+int path_is_fs_type(const char *path, statfs_f_type_t magic_value) {
         _cleanup_close_ int fd = -1;
 
-        fd = open(path, O_RDONLY);
+        fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_PATH);
         if (fd < 0)
                 return -errno;
 
-        return fd_check_fstype(fd, magic_value);
+        return fd_is_fs_type(fd, magic_value);
 }
 #endif // 0
 
 bool is_temporary_fs(const struct statfs *s) {
-    return is_fs_type(s, TMPFS_MAGIC) ||
-           is_fs_type(s, RAMFS_MAGIC);
+        return is_fs_type(s, TMPFS_MAGIC) ||
+                is_fs_type(s, RAMFS_MAGIC);
 }
 
+bool is_network_fs(const struct statfs *s) {
+        return is_fs_type(s, CIFS_MAGIC_NUMBER) ||
+                is_fs_type(s, CODA_SUPER_MAGIC) ||
+                is_fs_type(s, NCP_SUPER_MAGIC) ||
+                is_fs_type(s, NFS_SUPER_MAGIC) ||
+                is_fs_type(s, SMB_SUPER_MAGIC) ||
+                is_fs_type(s, V9FS_MAGIC) ||
+                is_fs_type(s, AFS_SUPER_MAGIC) ||
+                is_fs_type(s, OCFS2_SUPER_MAGIC);
+}
+
+#if 0 /// UNNEEDED by elogind
 int fd_is_temporary_fs(int fd) {
         struct statfs s;
 
@@ -224,3 +204,66 @@ int fd_is_temporary_fs(int fd) {
 
         return is_temporary_fs(&s);
 }
+
+int fd_is_network_fs(int fd) {
+        struct statfs s;
+
+        if (fstatfs(fd, &s) < 0)
+                return -errno;
+
+        return is_network_fs(&s);
+}
+
+int fd_is_network_ns(int fd) {
+        int r;
+
+        r = fd_is_fs_type(fd, NSFS_MAGIC);
+        if (r <= 0)
+                return r;
+
+        r = ioctl(fd, NS_GET_NSTYPE);
+        if (r < 0)
+                return -errno;
+
+        return r == CLONE_NEWNET;
+}
+
+int path_is_temporary_fs(const char *path) {
+        _cleanup_close_ int fd = -1;
+
+        fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_PATH);
+        if (fd < 0)
+                return -errno;
+
+        return fd_is_temporary_fs(fd);
+}
+#endif // 0
+
+int stat_verify_regular(const struct stat *st) {
+        assert(st);
+
+        /* Checks whether the specified stat() structure refers to a regular file. If not returns an appropriate error
+         * code. */
+
+        if (S_ISDIR(st->st_mode))
+                return -EISDIR;
+
+        if (S_ISLNK(st->st_mode))
+                return -ELOOP;
+
+        if (!S_ISREG(st->st_mode))
+                return -EBADFD;
+
+        return 0;
+}
+
+int fd_verify_regular(int fd) {
+        struct stat st;
+
+        assert(fd >= 0);
+
+        if (fstat(fd, &st) < 0)
+                return -errno;
+
+        return stat_verify_regular(&st);
+}