From: Lennart Poettering Date: Thu, 22 Feb 2018 17:24:57 +0000 (+0100) Subject: unit-name: add new unit_name_build_from_type() helper X-Git-Tag: v239~415^2~7 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=72406c2f7d23455d9e25a395cbf674cc4aff2149;p=platform%2Fupstream%2Fsystemd.git unit-name: add new unit_name_build_from_type() helper The new helper is much like unit_name_build() but expects a UnitType value instead of a suffix. --- diff --git a/src/basic/unit-name.c b/src/basic/unit-name.c index 07a8082..6fb9cbc 100644 --- a/src/basic/unit-name.c +++ b/src/basic/unit-name.c @@ -241,25 +241,45 @@ int unit_name_change_suffix(const char *n, const char *suffix, char **ret) { } int unit_name_build(const char *prefix, const char *instance, const char *suffix, char **ret) { - char *s; + UnitType type; assert(prefix); assert(suffix); assert(ret); + if (isempty(suffix)) + return -EINVAL; + if (suffix[0] != '.') + return -EINVAL; + + type = unit_type_from_string(suffix + 1); + if (type < 0) + return -EINVAL; + + return unit_name_build_from_type(prefix, instance, type, ret); +} + +int unit_name_build_from_type(const char *prefix, const char *instance, UnitType type, char **ret) { + const char *ut; + char *s; + + assert(prefix); + assert(type >= 0); + assert(type < _UNIT_TYPE_MAX); + assert(ret); + if (!unit_prefix_is_valid(prefix)) return -EINVAL; if (instance && !unit_instance_is_valid(instance)) return -EINVAL; - if (!unit_suffix_is_valid(suffix)) - return -EINVAL; + ut = unit_type_to_string(type); if (!instance) - s = strappend(prefix, suffix); + s = strjoin(prefix, ".", ut); else - s = strjoin(prefix, "@", instance, suffix); + s = strjoin(prefix, "@", instance, ".", ut); if (!s) return -ENOMEM; diff --git a/src/basic/unit-name.h b/src/basic/unit-name.h index aadaa85..d3e9d3e 100644 --- a/src/basic/unit-name.h +++ b/src/basic/unit-name.h @@ -40,6 +40,7 @@ UnitType unit_name_to_type(const char *n) _pure_; int unit_name_change_suffix(const char *n, const char *suffix, char **ret); int unit_name_build(const char *prefix, const char *instance, const char *suffix, char **ret); +int unit_name_build_from_type(const char *prefix, const char *instance, UnitType, char **ret); char *unit_name_escape(const char *f); int unit_name_unescape(const char *f, char **ret); diff --git a/src/test/test-unit-name.c b/src/test/test-unit-name.c index d712212..061adc7 100644 --- a/src/test/test-unit-name.c +++ b/src/test/test-unit-name.c @@ -449,6 +449,26 @@ static void test_unit_name_path_unescape(void) { test_unit_name_path_unescape_one("", NULL, -EINVAL); } +static void test_unit_name_to_prefix_one(const char *input, int ret, const char *output) { + _cleanup_free_ char *k = NULL; + + assert_se(unit_name_to_prefix(input, &k) == ret); + assert_se(streq_ptr(k, output)); +} + +static void test_unit_name_to_prefix(void) { + test_unit_name_to_prefix_one("foobar.service", 0, "foobar"); + test_unit_name_to_prefix_one("", -EINVAL, NULL); + test_unit_name_to_prefix_one("foobar", -EINVAL, NULL); + test_unit_name_to_prefix_one(".service", -EINVAL, NULL); + test_unit_name_to_prefix_one("quux.quux", -EINVAL, NULL); + test_unit_name_to_prefix_one("quux.mount", 0, "quux"); + test_unit_name_to_prefix_one("quux-quux.mount", 0, "quux-quux"); + test_unit_name_to_prefix_one("quux@bar.mount", 0, "quux"); + test_unit_name_to_prefix_one("quux-@.mount", 0, "quux-"); + test_unit_name_to_prefix_one("@.mount", -EINVAL, NULL); +} + int main(int argc, char* argv[]) { _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL; int r, rc = 0; @@ -482,6 +502,7 @@ int main(int argc, char* argv[]) { test_unit_name_escape(); test_unit_name_template(); test_unit_name_path_unescape(); + test_unit_name_to_prefix(); return rc; }