unit: introduce %f specifier to decode file names

This commit is contained in:
Lennart Poettering 2010-10-20 16:16:45 +02:00
parent 980d87505c
commit 9fc507041e
5 changed files with 49 additions and 9 deletions

View File

@ -182,13 +182,17 @@
<literal>%i</literal> specifier in many of the <literal>%i</literal> specifier in many of the
configuration options. Other specifiers that may be configuration options. Other specifiers that may be
used are <literal>%n</literal>, <literal>%N</literal>, used are <literal>%n</literal>, <literal>%N</literal>,
<literal>%p</literal>, <literal>%P</literal> and <literal>%p</literal>, <literal>%P</literal>,
<literal>%I</literal>, for the full unit name, the <literal>%I</literal> and <literal>%f</literal>, for
unescaped unit name, the prefix name, the unescaped the full unit name, the unescaped unit name, the
prefix name and the unescaped instance name, prefix name, the unescaped prefix name, the unescaped
respectively. The prefix name here refers to the instance name and the unescaped filename,
string before the @, i.e. "getty" in the example respectively. The unescaped filename is either the
above, where "tty3" is the instance name.</para> unescaped instance name (if set) with / prepended (if
necessary), or the prefix name similarly prepended
with /. The prefix name here refers to the string
before the @, i.e. "getty" in the example above, where
"tty3" is the instance name.</para>
<para>If a unit file is empty (i.e. has the file size <para>If a unit file is empty (i.e. has the file size
0) or is symlinked to <filename>/dev/null</filename> 0) or is symlinked to <filename>/dev/null</filename>

View File

@ -427,3 +427,26 @@ char *unit_name_to_path(const char *name) {
return e; return e;
} }
char *unit_name_path_unescape(const char *f) {
char *e;
assert(f);
if (!(e = unit_name_unescape(f)))
return NULL;
if (e[0] != '/') {
char *w;
w = strappend("/", e);
free(e);
if (!w)
return NULL;
e = w;
}
return e;
}

View File

@ -42,6 +42,8 @@ char *unit_name_build_escape(const char *prefix, const char *instance, const cha
char *unit_name_escape(const char *f); char *unit_name_escape(const char *f);
char *unit_name_unescape(const char *f); char *unit_name_unescape(const char *f);
char *unit_name_path_unescape(const char *f);
bool unit_name_is_template(const char *n); bool unit_name_is_template(const char *n);
char *unit_name_replace_instance(const char *f, const char *i); char *unit_name_replace_instance(const char *f, const char *i);

View File

@ -1880,6 +1880,16 @@ static char *specifier_instance_unescaped(char specifier, void *data, void *user
return strdup(""); return strdup("");
} }
static char *specifier_filename(char specifier, void *data, void *userdata) {
Unit *u = userdata;
assert(u);
if (u->meta.instance)
return unit_name_path_unescape(u->meta.instance);
return unit_name_to_path(u->meta.instance);
}
char *unit_name_printf(Unit *u, const char* format) { char *unit_name_printf(Unit *u, const char* format) {
/* /*
@ -1918,6 +1928,7 @@ char *unit_full_printf(Unit *u, const char *format) {
{ 'P', specifier_prefix_unescaped, NULL }, { 'P', specifier_prefix_unescaped, NULL },
{ 'i', specifier_string, u->meta.instance }, { 'i', specifier_string, u->meta.instance },
{ 'I', specifier_instance_unescaped, NULL }, { 'I', specifier_instance_unescaped, NULL },
{ 'f', specifier_filename, NULL },
{ 0, NULL, NULL } { 0, NULL, NULL }
}; };

View File

@ -6,7 +6,7 @@
# (at your option) any later version. # (at your option) any later version.
[Unit] [Unit]
Description=File System Check on %I Description=File System Check on %f
DefaultDependencies=no DefaultDependencies=no
Requires=%i.device Requires=%i.device
After=systemd-readahead-collect.service systemd-readahead-replay.service %i.device After=systemd-readahead-collect.service systemd-readahead-replay.service %i.device
@ -15,4 +15,4 @@ Before=local-fs.target shutdown.target
[Service] [Service]
Type=oneshot Type=oneshot
RemainAfterExit=no RemainAfterExit=no
ExecStart=@rootlibexecdir@/systemd-fsck %I ExecStart=@rootlibexecdir@/systemd-fsck %f