]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
escape: support --unescape with --template
authorLucas Werkmeister <mail@lucaswerkmeister.de>
Sun, 30 Oct 2016 14:43:01 +0000 (15:43 +0100)
committerLucas Werkmeister <mail@lucaswerkmeister.de>
Mon, 25 Jun 2018 09:13:38 +0000 (11:13 +0200)
man/systemd-escape.xml
src/escape/escape.c

index 41014abc735c44d1fbdb519c4134e6146b688a03..8329e5d7c457c58eca680702c8f2d900ccac3134 100644 (file)
 
         <listitem><para>Inserts the escaped strings in a unit name
         template. Takes a unit name template such as
-        <filename>foobar@.service</filename>. May not be used in
-        conjunction with <option>--suffix=</option>,
-        <option>--unescape</option> or
+        <filename>foobar@.service</filename>. With
+        <option>--unescape</option>, expects instantiated unit names
+        for this template and extracts and unescapes just the instance
+        part. May not be used in conjunction with
+        <option>--suffix=</option> or
         <option>--mangle</option>.</para></listitem>
       </varlistentry>
 
 
         <listitem><para>Instead of escaping the specified strings,
         undo the escaping, reversing the operation. May not be used in
-        conjunction with <option>--suffix=</option>,
-        <option>--template=</option> or
+        conjunction with <option>--suffix=</option> or
         <option>--mangle</option>.</para></listitem>
       </varlistentry>
 
@@ -141,6 +142,10 @@ tmp-waldi-foobar.mount</programlisting>
     <para>To generate instance names of three strings:</para>
     <programlisting>$ systemd-escape --template=systemd-nspawn@.service 'My Container 1' 'containerb' 'container/III'
 systemd-nspawn@My\x20Container\x201.service systemd-nspawn@containerb.service systemd-nspawn@container-III.service</programlisting>
+
+    <para>To extract the instance part of an instantiated unit:</para>
+    <programlisting>$ systemd-escape -u --template=systemd-nspawn@.service 'systemd-nspawn@My\x20Container\x201.service'
+My Container 1</programlisting>
   </refsect1>
 
   <refsect1>
index 371ddbe02bb9321094d76f53924f7585117a48a2..3cf4a20d1e45085356b111b914f3ab517571569c 100644 (file)
@@ -119,8 +119,13 @@ static int parse_argv(int argc, char *argv[]) {
                 return -EINVAL;
         }
 
-        if ((arg_template || arg_suffix) && arg_action != ACTION_ESCAPE) {
-                log_error("--suffix= and --template= are not compatible with --unescape or --mangle.");
+        if ((arg_template || arg_suffix) && arg_action == ACTION_MANGLE) {
+                log_error("--suffix= and --template= are not compatible with --mangle.");
+                return -EINVAL;
+        }
+
+        if (arg_suffix && arg_action == ACTION_UNESCAPE) {
+                log_error("--suffix is not compatible with --unescape.");
                 return -EINVAL;
         }
 
@@ -189,17 +194,51 @@ int main(int argc, char *argv[]) {
 
                         break;
 
-                case ACTION_UNESCAPE:
+                case ACTION_UNESCAPE: {
+                        _cleanup_free_ char *name = NULL;
+
+                        if (arg_template) {
+                                _cleanup_free_ char *template = NULL;
+
+                                r = unit_name_to_instance(*i, &name);
+                                if (r < 0) {
+                                        log_error_errno(r, "Failed to extract instance: %m");
+                                        goto finish;
+                                }
+                                if (isempty(name)) {
+                                        log_error("Unit %s is missing the instance name.", *i);
+                                        r = -EINVAL;
+                                        goto finish;
+                                }
+                                r = unit_name_template(*i, &template);
+                                if (r < 0) {
+                                        log_error_errno(r, "Failed to extract template: %m");
+                                        goto finish;
+                                }
+                                if (!streq(arg_template, template)) {
+                                        log_error("Unit %s template %s does not match specified template %s.", *i, template, arg_template);
+                                        r = -EINVAL;
+                                        goto finish;
+                                }
+                        } else {
+                                name = strdup(*i);
+                                if (!name) {
+                                        r = log_oom();
+                                        goto finish;
+                                }
+                        }
+
                         if (arg_path)
-                                r = unit_name_path_unescape(*i, &e);
+                                r = unit_name_path_unescape(name, &e);
                         else
-                                r = unit_name_unescape(*i, &e);
+                                r = unit_name_unescape(name, &e);
 
                         if (r < 0) {
                                 log_error_errno(r, "Failed to unescape string: %m");
                                 goto finish;
                         }
                         break;
+                }
 
                 case ACTION_MANGLE:
                         r = unit_name_mangle(*i, 0, &e);