summaryrefslogtreecommitdiffstats
path: root/src/basic/path-util.c
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2018-06-01 07:11:37 +0200
committerYu Watanabe <watanabe.yu+github@gmail.com>2018-06-03 16:54:55 +0200
commit58a53adde559fced6c7ce909d03c61a21ccd73eb (patch)
tree66a46c1ba1d391c0fba95ca82f9531113df2ad26 /src/basic/path-util.c
parentpath-util: make path_make_relative() support path including dots (diff)
downloadsystemd-58a53adde559fced6c7ce909d03c61a21ccd73eb.tar.xz
systemd-58a53adde559fced6c7ce909d03c61a21ccd73eb.zip
path-util: introduce path_simplify_and_warn()
Diffstat (limited to 'src/basic/path-util.c')
-rw-r--r--src/basic/path-util.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/basic/path-util.c b/src/basic/path-util.c
index 00c30f35f1..690159d89c 100644
--- a/src/basic/path-util.c
+++ b/src/basic/path-util.c
@@ -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;
+}