chiark / gitweb /
tree-wide: drop 'This file is part of systemd' blurb
[elogind.git] / src / basic / path-util.c
index 7246bc8c0296efedad397d5fcc50690f87a5a391..a0a2857d35f17d470878c2c0b8c4e2ad9d176061 100644 (file)
@@ -1,7 +1,5 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 /***
-  This file is part of systemd.
-
   Copyright 2010-2012 Lennart Poettering
 ***/
 
@@ -895,17 +893,36 @@ bool hidden_or_backup_file(const char *filename) {
 #if 0 /// UNNEEDED by elogind
 bool is_device_path(const char *path) {
 
-        /* Returns true on paths that refer to a device, either in
-         * sysfs or in /dev */
+        /* Returns true on paths that likely refer to a device, either by path in sysfs or to something in /dev */
 
-        return path_startswith(path, "/dev/") ||
-               path_startswith(path, "/sys/");
+        return PATH_STARTSWITH_SET(path, "/dev/", "/sys/");
 }
 
-bool is_deviceallow_pattern(const char *path) {
-        return path_startswith(path, "/dev/") ||
-               startswith(path, "block-") ||
-               startswith(path, "char-");
+bool valid_device_node_path(const char *path) {
+
+        /* Some superficial checks whether the specified path is a valid device node path, all without looking at the
+         * actual device node. */
+
+        if (!PATH_STARTSWITH_SET(path, "/dev/", "/run/systemd/inaccessible/"))
+                return false;
+
+        if (endswith(path, "/")) /* can't be a device node if it ends in a slash */
+                return false;
+
+        return path_is_normalized(path);
+}
+
+bool valid_device_allow_pattern(const char *path) {
+        assert(path);
+
+        /* Like valid_device_node_path(), but also allows full-subsystem expressions, like DeviceAllow= and DeviceDeny=
+         * accept it */
+
+        if (startswith(path, "block-") ||
+            startswith(path, "char-"))
+                return true;
+
+        return valid_device_node_path(path);
 }
 
 int systemd_installation_has_version(const char *root, unsigned minimal_version) {
@@ -1009,11 +1026,9 @@ int path_simplify_and_warn(
                 unsigned line,
                 const char *lvalue) {
 
-        bool fatal, absolute;
-
-        assert((flag & (PATH_CHECK_ABSOLUTE | PATH_CHECK_RELATIVE)) != (PATH_CHECK_ABSOLUTE | PATH_CHECK_RELATIVE));
+        bool absolute, fatal = flag & PATH_CHECK_FATAL;
 
-        fatal = flag & PATH_CHECK_FATAL;
+        assert(!FLAGS_SET(flag, PATH_CHECK_ABSOLUTE | PATH_CHECK_RELATIVE));
 
         if (!utf8_is_valid(path)) {
                 log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, path);
@@ -1026,14 +1041,14 @@ int path_simplify_and_warn(
                 if (!absolute && (flag & PATH_CHECK_ABSOLUTE)) {
                         log_syntax(unit, LOG_ERR, filename, line, 0,
                                    "%s= path is not absolute%s: %s",
-                                   fatal ? "" : ", ignoring", lvalue, path);
+                                   lvalue, fatal ? "" : ", ignoring", path);
                         return -EINVAL;
                 }
 
                 if (absolute && (flag & PATH_CHECK_RELATIVE)) {
                         log_syntax(unit, LOG_ERR, filename, line, 0,
                                    "%s= path is absolute%s: %s",
-                                   fatal ? "" : ", ignoring", lvalue, path);
+                                   lvalue, fatal ? "" : ", ignoring", path);
                         return -EINVAL;
                 }
         }
@@ -1043,7 +1058,7 @@ int path_simplify_and_warn(
         if (!path_is_normalized(path)) {
                 log_syntax(unit, LOG_ERR, filename, line, 0,
                            "%s= path is not normalized%s: %s",
-                           fatal ? "" : ", ignoring", lvalue, path);
+                           lvalue, fatal ? "" : ", ignoring", path);
                 return -EINVAL;
         }