chiark / gitweb /
tests: add tests for environment serialization
[elogind.git] / src / basic / stat-util.c
index 5bc11e4a32fb7aa7915cc6006eb83428d2f5c930..0280afeecd743a210e6840a3bd43bbead2248db3 100644 (file)
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
 /***
   This file is part of systemd.
 
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#include <dirent.h>
+#include <errno.h>
 #include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 #include <linux/magic.h>
 #include <sys/statvfs.h>
 #include <unistd.h>
@@ -148,35 +150,42 @@ 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;
+        /* Does the path exist at all? If not, generate an error immediately. This is useful so that a missing root dir
+         * always results in -ENOENT, and we can properly distuingish the case where the whole root doesn't exist from
+         * the case where just the os-release file is missing. */
+        if (laccess(path, F_OK) < 0)
+                return -errno;
 
-        /* Also check for the old location in /etc, just in case. */
-        p = strjoina(path, "/etc/os-release");
-        r = access(p, F_OK);
+        /* We use /usr/lib/os-release as flag file if something is an OS */
+        r = chase_symlinks("/usr/lib/os-release", path, CHASE_PREFIX_ROOT, NULL);
+        if (r == -ENOENT) {
+
+                /* Also check for the old location in /etc, just in case. */
+                r = chase_symlinks("/etc/os-release", path, CHASE_PREFIX_ROOT, NULL);
+                if (r == -ENOENT)
+                        return 0; /* We got nothing */
+        }
+        if (r < 0)
+                return r;
 
-        return r >= 0;
+        return 1;
 }
 #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 &&
@@ -203,7 +212,7 @@ int fd_check_fstype(int fd, statfs_f_type_t magic_value) {
 int path_check_fstype(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;
 
@@ -216,6 +225,7 @@ bool is_temporary_fs(const struct statfs *s) {
            is_fs_type(s, RAMFS_MAGIC);
 }
 
+#if 0 /// UNNEEDED by elogind
 int fd_is_temporary_fs(int fd) {
         struct statfs s;
 
@@ -224,3 +234,14 @@ int fd_is_temporary_fs(int fd) {
 
         return is_temporary_fs(&s);
 }
+
+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