unit-name: various modernizations
This commit is contained in:
parent
6ef9eeed61
commit
b9a3302630
|
@ -28,6 +28,7 @@
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "unit-name.h"
|
#include "unit-name.h"
|
||||||
#include "def.h"
|
#include "def.h"
|
||||||
|
#include "strv.h"
|
||||||
|
|
||||||
#define VALID_CHARS \
|
#define VALID_CHARS \
|
||||||
DIGITS LETTERS \
|
DIGITS LETTERS \
|
||||||
|
@ -71,9 +72,11 @@ bool unit_name_is_valid(const char *n, enum template_valid template_ok) {
|
||||||
* string.suffix
|
* string.suffix
|
||||||
*/
|
*/
|
||||||
|
|
||||||
assert(n);
|
|
||||||
assert(IN_SET(template_ok, TEMPLATE_VALID, TEMPLATE_INVALID));
|
assert(IN_SET(template_ok, TEMPLATE_VALID, TEMPLATE_INVALID));
|
||||||
|
|
||||||
|
if (isempty(n))
|
||||||
|
return false;
|
||||||
|
|
||||||
if (strlen(n) >= UNIT_NAME_MAX)
|
if (strlen(n) >= UNIT_NAME_MAX)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
@ -105,36 +108,27 @@ bool unit_name_is_valid(const char *n, enum template_valid template_ok) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool unit_instance_is_valid(const char *i) {
|
bool unit_instance_is_valid(const char *i) {
|
||||||
assert(i);
|
|
||||||
|
|
||||||
/* The max length depends on the length of the string, so we
|
/* The max length depends on the length of the string, so we
|
||||||
* don't really check this here. */
|
* don't really check this here. */
|
||||||
|
|
||||||
if (i[0] == 0)
|
if (isempty(i))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
/* We allow additional @ in the instance string, we do not
|
/* We allow additional @ in the instance string, we do not
|
||||||
* allow them in the prefix! */
|
* allow them in the prefix! */
|
||||||
|
|
||||||
for (; *i; i++)
|
return in_charset(i, "@" VALID_CHARS);
|
||||||
if (!strchr("@" VALID_CHARS, *i))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool unit_prefix_is_valid(const char *p) {
|
bool unit_prefix_is_valid(const char *p) {
|
||||||
|
|
||||||
/* We don't allow additional @ in the instance string */
|
/* We don't allow additional @ in the instance string */
|
||||||
|
|
||||||
if (p[0] == 0)
|
if (isempty(p))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
for (; *p; p++)
|
return in_charset(p, VALID_CHARS);
|
||||||
if (!strchr(VALID_CHARS, *p))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int unit_name_to_instance(const char *n, char **instance) {
|
int unit_name_to_instance(const char *n, char **instance) {
|
||||||
|
@ -151,15 +145,18 @@ int unit_name_to_instance(const char *n, char **instance) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_se(d = strrchr(n, '.'));
|
d = strrchr(n, '.');
|
||||||
assert(p < d);
|
if (!d)
|
||||||
|
return -EINVAL;
|
||||||
|
if (d < p)
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
i = strndup(p+1, d-p-1);
|
i = strndup(p+1, d-p-1);
|
||||||
if (!i)
|
if (!i)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
*instance = i;
|
*instance = i;
|
||||||
return 0;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *unit_name_to_prefix_and_instance(const char *n) {
|
char *unit_name_to_prefix_and_instance(const char *n) {
|
||||||
|
@ -168,13 +165,14 @@ char *unit_name_to_prefix_and_instance(const char *n) {
|
||||||
assert(n);
|
assert(n);
|
||||||
|
|
||||||
assert_se(d = strrchr(n, '.'));
|
assert_se(d = strrchr(n, '.'));
|
||||||
|
|
||||||
return strndup(n, d - n);
|
return strndup(n, d - n);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *unit_name_to_prefix(const char *n) {
|
char *unit_name_to_prefix(const char *n) {
|
||||||
const char *p;
|
const char *p;
|
||||||
|
|
||||||
|
assert(n);
|
||||||
|
|
||||||
p = strchr(n, '@');
|
p = strchr(n, '@');
|
||||||
if (p)
|
if (p)
|
||||||
return strndup(n, p - n);
|
return strndup(n, p - n);
|
||||||
|
@ -187,7 +185,6 @@ char *unit_name_change_suffix(const char *n, const char *suffix) {
|
||||||
size_t a, b;
|
size_t a, b;
|
||||||
|
|
||||||
assert(n);
|
assert(n);
|
||||||
assert(unit_name_is_valid(n, TEMPLATE_VALID));
|
|
||||||
assert(suffix);
|
assert(suffix);
|
||||||
assert(suffix[0] == '.');
|
assert(suffix[0] == '.');
|
||||||
|
|
||||||
|
@ -199,16 +196,12 @@ char *unit_name_change_suffix(const char *n, const char *suffix) {
|
||||||
if (!r)
|
if (!r)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
memcpy(r, n, a);
|
strcpy(mempcpy(r, n, a), suffix);
|
||||||
memcpy(r+a, suffix, b+1);
|
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *unit_name_build(const char *prefix, const char *instance, const char *suffix) {
|
char *unit_name_build(const char *prefix, const char *instance, const char *suffix) {
|
||||||
assert(prefix);
|
assert(prefix);
|
||||||
assert(unit_prefix_is_valid(prefix));
|
|
||||||
assert(!instance || unit_instance_is_valid(instance));
|
|
||||||
assert(suffix);
|
assert(suffix);
|
||||||
|
|
||||||
if (!instance)
|
if (!instance)
|
||||||
|
@ -218,10 +211,13 @@ char *unit_name_build(const char *prefix, const char *instance, const char *suff
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *do_escape_char(char c, char *t) {
|
static char *do_escape_char(char c, char *t) {
|
||||||
|
assert(t);
|
||||||
|
|
||||||
*(t++) = '\\';
|
*(t++) = '\\';
|
||||||
*(t++) = 'x';
|
*(t++) = 'x';
|
||||||
*(t++) = hexchar(c >> 4);
|
*(t++) = hexchar(c >> 4);
|
||||||
*(t++) = hexchar(c);
|
*(t++) = hexchar(c);
|
||||||
|
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -250,6 +246,8 @@ static char *do_escape(const char *f, char *t) {
|
||||||
char *unit_name_escape(const char *f) {
|
char *unit_name_escape(const char *f) {
|
||||||
char *r, *t;
|
char *r, *t;
|
||||||
|
|
||||||
|
assert(f);
|
||||||
|
|
||||||
r = new(char, strlen(f)*4+1);
|
r = new(char, strlen(f)*4+1);
|
||||||
if (!r)
|
if (!r)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -294,7 +292,7 @@ char *unit_name_unescape(const char *f) {
|
||||||
}
|
}
|
||||||
|
|
||||||
char *unit_name_path_escape(const char *f) {
|
char *unit_name_path_escape(const char *f) {
|
||||||
_cleanup_free_ char *p;
|
_cleanup_free_ char *p = NULL;
|
||||||
|
|
||||||
assert(f);
|
assert(f);
|
||||||
|
|
||||||
|
@ -304,14 +302,14 @@ char *unit_name_path_escape(const char *f) {
|
||||||
|
|
||||||
path_kill_slashes(p);
|
path_kill_slashes(p);
|
||||||
|
|
||||||
if (streq(p, "/") || streq(p, ""))
|
if (STR_IN_SET(p, "/", ""))
|
||||||
return strdup("-");
|
return strdup("-");
|
||||||
|
|
||||||
return unit_name_escape(p[0] == '/' ? p + 1 : p);
|
return unit_name_escape(p[0] == '/' ? p + 1 : p);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *unit_name_path_unescape(const char *f) {
|
char *unit_name_path_unescape(const char *f) {
|
||||||
char *e;
|
char *e, *w;
|
||||||
|
|
||||||
assert(f);
|
assert(f);
|
||||||
|
|
||||||
|
@ -320,11 +318,8 @@ char *unit_name_path_unescape(const char *f) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (e[0] != '/') {
|
if (e[0] != '/') {
|
||||||
char *w;
|
|
||||||
|
|
||||||
w = strappend("/", e);
|
w = strappend("/", e);
|
||||||
free(e);
|
free(e);
|
||||||
|
|
||||||
return w;
|
return w;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -365,9 +360,36 @@ bool unit_name_is_instance(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) {
|
||||||
const char *p, *e;
|
const char *p, *e;
|
||||||
char *r, *k;
|
char *r;
|
||||||
size_t a, b;
|
size_t a, b;
|
||||||
|
|
||||||
|
assert(f);
|
||||||
|
assert(i);
|
||||||
|
|
||||||
|
p = strchr(f, '@');
|
||||||
|
if (!p)
|
||||||
|
return strdup(f);
|
||||||
|
|
||||||
|
e = strrchr(f, '.');
|
||||||
|
if (!e)
|
||||||
|
e = strchr(f, 0);
|
||||||
|
|
||||||
|
a = p - f;
|
||||||
|
b = strlen(i);
|
||||||
|
|
||||||
|
r = new(char, a + 1 + b + strlen(e) + 1);
|
||||||
|
if (!r)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
strcpy(mempcpy(mempcpy(r, f, a + 1), i, b), e);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *unit_name_template(const char *f) {
|
||||||
|
const char *p, *e;
|
||||||
|
char *r;
|
||||||
|
size_t a;
|
||||||
|
|
||||||
assert(f);
|
assert(f);
|
||||||
|
|
||||||
p = strchr(f, '@');
|
p = strchr(f, '@');
|
||||||
|
@ -376,39 +398,15 @@ char *unit_name_replace_instance(const char *f, const char *i) {
|
||||||
|
|
||||||
e = strrchr(f, '.');
|
e = strrchr(f, '.');
|
||||||
if (!e)
|
if (!e)
|
||||||
assert_se(e = strchr(f, 0));
|
e = strchr(f, 0);
|
||||||
|
|
||||||
a = p - f;
|
a = p - f;
|
||||||
b = strlen(i);
|
|
||||||
|
|
||||||
r = new(char, a + 1 + b + strlen(e) + 1);
|
r = new(char, a + 1 + strlen(e) + 1);
|
||||||
if (!r)
|
if (!r)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
k = mempcpy(r, f, a + 1);
|
strcpy(mempcpy(r, f, a + 1), e);
|
||||||
k = mempcpy(k, i, b);
|
|
||||||
strcpy(k, e);
|
|
||||||
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *unit_name_template(const char *f) {
|
|
||||||
const char *p, *e;
|
|
||||||
char *r;
|
|
||||||
size_t a;
|
|
||||||
|
|
||||||
p = strchr(f, '@');
|
|
||||||
if (!p)
|
|
||||||
return strdup(f);
|
|
||||||
|
|
||||||
assert_se(e = strrchr(f, '.'));
|
|
||||||
a = p - f + 1;
|
|
||||||
|
|
||||||
r = new(char, a + strlen(e) + 1);
|
|
||||||
if (!r)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
strcpy(mempcpy(r, f, a), e);
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -479,15 +477,13 @@ int unit_name_from_dbus_path(const char *path, char **name) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Try to turn a string that might not be a unit name into a
|
* Try to turn a string that might not be a unit name into a
|
||||||
* sensible unit name.
|
* sensible unit name.
|
||||||
*/
|
*/
|
||||||
char *unit_name_mangle(const char *name, enum unit_name_mangle allow_globs) {
|
char *unit_name_mangle(const char *name, enum unit_name_mangle allow_globs) {
|
||||||
|
const char *valid_chars, *f;
|
||||||
char *r, *t;
|
char *r, *t;
|
||||||
const char *f;
|
|
||||||
const char* valid_chars = allow_globs == MANGLE_GLOB ? "@" VALID_CHARS "[]!-*?" : "@" VALID_CHARS;
|
|
||||||
|
|
||||||
assert(name);
|
assert(name);
|
||||||
assert(IN_SET(allow_globs, MANGLE_GLOB, MANGLE_NOGLOB));
|
assert(IN_SET(allow_globs, MANGLE_GLOB, MANGLE_NOGLOB));
|
||||||
|
@ -501,6 +497,8 @@ char *unit_name_mangle(const char *name, enum unit_name_mangle allow_globs) {
|
||||||
/* We'll only escape the obvious characters here, to play
|
/* We'll only escape the obvious characters here, to play
|
||||||
* safe. */
|
* safe. */
|
||||||
|
|
||||||
|
valid_chars = allow_globs == MANGLE_GLOB ? "@" VALID_CHARS "[]!-*?" : "@" VALID_CHARS;
|
||||||
|
|
||||||
r = new(char, strlen(name) * 4 + strlen(".service") + 1);
|
r = new(char, strlen(name) * 4 + strlen(".service") + 1);
|
||||||
if (!r)
|
if (!r)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -522,7 +520,6 @@ char *unit_name_mangle(const char *name, enum unit_name_mangle allow_globs) {
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Similar to unit_name_mangle(), but is called when we know
|
* Similar to unit_name_mangle(), but is called when we know
|
||||||
* that this is about a specific unit type.
|
* that this is about a specific unit type.
|
||||||
|
@ -532,6 +529,7 @@ char *unit_name_mangle_with_suffix(const char *name, enum unit_name_mangle allow
|
||||||
const char *f;
|
const char *f;
|
||||||
|
|
||||||
assert(name);
|
assert(name);
|
||||||
|
assert(IN_SET(allow_globs, MANGLE_GLOB, MANGLE_NOGLOB));
|
||||||
assert(suffix);
|
assert(suffix);
|
||||||
assert(suffix[0] == '.');
|
assert(suffix[0] == '.');
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue