]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
path-util: introduce path_simplify_and_warn()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 1 Jun 2018 05:11:37 +0000 (14:11 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 3 Jun 2018 14:54:55 +0000 (23:54 +0900)
src/basic/path-util.c
src/basic/path-util.h

index 00c30f35f18592086d0dbfd5d6ce711c452f25d8..690159d89cfeb1c468d1b3f686c710e7df0d310b 100644 (file)
@@ -32,6 +32,7 @@
 #include "string-util.h"
 #include "strv.h"
 #include "time-util.h"
+#include "utf8.h"
 
 bool path_is_absolute(const char *p) {
         return p[0] == '/';
@@ -989,3 +990,52 @@ bool empty_or_root(const char *root) {
 
         return root[strspn(root, "/")] == 0;
 }
+
+int path_simplify_and_warn(
+                char *path,
+                unsigned flag,
+                const char *unit,
+                const char *filename,
+                unsigned line,
+                const char *lvalue) {
+
+        bool fatal, absolute;
+
+        assert((flag & (PATH_CHECK_ABSOLUTE | PATH_CHECK_RELATIVE)) != (PATH_CHECK_ABSOLUTE | PATH_CHECK_RELATIVE));
+
+        fatal = flag & PATH_CHECK_FATAL;
+
+        if (!utf8_is_valid(path)) {
+                log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, path);
+                return -EINVAL;
+        }
+
+        if (flag & (PATH_CHECK_ABSOLUTE | PATH_CHECK_RELATIVE)) {
+                absolute = path_is_absolute(path);
+
+                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);
+                        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);
+                        return -EINVAL;
+                }
+        }
+
+        path_simplify(path, true);
+
+        if (!path_is_normalized(path)) {
+                log_syntax(unit, LOG_ERR, filename, line, 0,
+                           "%s= path is not normalized%s: %s",
+                           fatal ? "" : ", ignoring", lvalue, path);
+                return -EINVAL;
+        }
+
+        return 0;
+}
index fd6dc1ed1aa7a8f013fbc636c6859947d43a5d1b..d8a923d3cd7511d26032f1a8994322673f21c932 100644 (file)
@@ -167,3 +167,11 @@ bool empty_or_root(const char *root);
 static inline const char *empty_to_root(const char *path) {
         return isempty(path) ? "/" : path;
 }
+
+enum {
+        PATH_CHECK_FATAL    = 1 << 0,  /* If not set, then error message is appended with 'ignoring'. */
+        PATH_CHECK_ABSOLUTE = 1 << 1,
+        PATH_CHECK_RELATIVE = 1 << 2,
+};
+
+int path_simplify_and_warn(char *path, unsigned flag, const char *unit, const char *filename, unsigned line, const char *lvalue);