summaryrefslogtreecommitdiff
path: root/src/escape
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2021-09-13 15:40:00 +0200
committerYu Watanabe <watanabe.yu+github@gmail.com>2021-09-14 03:04:57 +0900
commit99db797bc612c0c3b43240800946a9500bc39de5 (patch)
treebcc84c7d1f59a550301995a08d1e5c70f0edaf43 /src/escape
parentf90eea7d18d9ebe88e6a66cd7a86b618def8945d (diff)
downloadsystemd-99db797bc612c0c3b43240800946a9500bc39de5.tar.gz
escape: improve logging when escaping paths that are slightly non-conforming
Fixes: #20663
Diffstat (limited to 'src/escape')
-rw-r--r--src/escape/escape.c31
1 files changed, 30 insertions, 1 deletions
diff --git a/src/escape/escape.c b/src/escape/escape.c
index 167305cb03..676b7dce54 100644
--- a/src/escape/escape.c
+++ b/src/escape/escape.c
@@ -7,6 +7,7 @@
#include "alloc-util.h"
#include "log.h"
#include "main-func.h"
+#include "path-util.h"
#include "pretty-print.h"
#include "string-util.h"
#include "strv.h"
@@ -171,8 +172,36 @@ static int run(int argc, char *argv[]) {
case ACTION_ESCAPE:
if (arg_path) {
r = unit_name_path_escape(*i, &e);
- if (r < 0)
+ if (r < 0) {
+ if (r == -EINVAL) {
+ /* If escaping failed because the string was invalid, let's print a
+ * friendly message about it. Catch these specific error cases
+ * explicitly. */
+
+ if (!path_is_valid(*i))
+ return log_error_errno(r, "Input '%s' is not a valid file system path, failed to escape.", *i);
+ if (!path_is_absolute(*i))
+ return log_error_errno(r, "Input '%s' is not an absolute file system path, failed to escape.", *i);
+ if (!path_is_normalized(*i))
+ return log_error_errno(r, "Input '%s' is not a normalized file system path, failed to escape.", *i);
+ }
+
+ /* All other error cases. */
return log_error_errno(r, "Failed to escape string: %m");
+ }
+
+ /* If the escaping worked, then still warn if the path is not like we'd like
+ * it. Because that means escaping is not necessarily reversible. */
+
+ if (!path_is_valid(*i))
+ log_warning("Input '%s' is not a valid file system path, escaping is likely not going be reversible.", *i);
+ else if (!path_is_absolute(*i))
+ log_warning("Input '%s' is not an absolute file system path, escaping is likely not going to be reversible.", *i);
+
+ /* Note that we don't complain about paths not being normalized here, because
+ * some forms of non-normalization is actually OK, such as a series // and
+ * unit_name_path_escape() will clean those up silently, and the reversal is
+ * "close enough" to be OK. */
} else {
e = unit_name_escape(*i);
if (!e)