X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fshared%2Funit-name.c;h=21b66913c9d1dae539f17db31dc2a8bd3f83a99e;hp=a2d62d31513285e16aa88802238698211fff1dd3;hb=cc13b327297bb616f8adca10c7503581c8bc5b93;hpb=b9a3302630cce36badb286201031b4cff2da6a74 diff --git a/src/shared/unit-name.c b/src/shared/unit-name.c index a2d62d315..21b66913c 100644 --- a/src/shared/unit-name.c +++ b/src/shared/unit-name.c @@ -243,6 +243,30 @@ static char *do_escape(const char *f, char *t) { return t; } +static char *do_escape_mangle(const char *f, enum unit_name_mangle allow_globs, char *t) { + const char *valid_chars; + + assert(f); + assert(IN_SET(allow_globs, MANGLE_GLOB, MANGLE_NOGLOB)); + assert(t); + + /* We'll only escape the obvious characters here, to play + * safe. */ + + valid_chars = allow_globs == MANGLE_GLOB ? "@" VALID_CHARS "[]!-*?" : "@" VALID_CHARS; + + for (; *f; f++) { + if (*f == '/') + *(t++) = '-'; + else if (!strchr(valid_chars, *f)) + t = do_escape_char(*f, t); + else + *(t++) = *f; + } + + return t; +} + char *unit_name_escape(const char *f) { char *r, *t; @@ -478,15 +502,18 @@ int unit_name_from_dbus_path(const char *path, char **name) { } /** - * Try to turn a string that might not be a unit name into a - * sensible unit name. + * Convert a string to a unit name. /dev/blah is converted to dev-blah.device, + * /blah/blah is converted to blah-blah.mount, anything else is left alone, + * except that @suffix is appended if a valid unit suffix is not present. + * + * If @allow_globs, globs characters are preserved. Otherwise they are escaped. */ -char *unit_name_mangle(const char *name, enum unit_name_mangle allow_globs) { - const char *valid_chars, *f; +char *unit_name_mangle_with_suffix(const char *name, enum unit_name_mangle allow_globs, const char *suffix) { char *r, *t; assert(name); - assert(IN_SET(allow_globs, MANGLE_GLOB, MANGLE_NOGLOB)); + assert(suffix); + assert(suffix[0] == '.'); if (is_device_path(name)) return unit_name_from_path(name, ".device"); @@ -494,59 +521,13 @@ char *unit_name_mangle(const char *name, enum unit_name_mangle allow_globs) { if (path_is_absolute(name)) return unit_name_from_path(name, ".mount"); - /* We'll only escape the obvious characters here, to play - * safe. */ - - valid_chars = allow_globs == MANGLE_GLOB ? "@" VALID_CHARS "[]!-*?" : "@" VALID_CHARS; - - r = new(char, strlen(name) * 4 + strlen(".service") + 1); - if (!r) - return NULL; - - for (f = name, t = r; *f; f++) { - if (*f == '/') - *(t++) = '-'; - else if (!strchr(valid_chars, *f)) - t = do_escape_char(*f, t); - else - *(t++) = *f; - } - - if (unit_name_to_type(name) < 0) - strcpy(t, ".service"); - else - *t = 0; - - return r; -} - -/** - * Similar to unit_name_mangle(), but is called when we know - * that this is about a specific unit type. - */ -char *unit_name_mangle_with_suffix(const char *name, enum unit_name_mangle allow_globs, const char *suffix) { - char *r, *t; - const char *f; - - assert(name); - assert(IN_SET(allow_globs, MANGLE_GLOB, MANGLE_NOGLOB)); - assert(suffix); - assert(suffix[0] == '.'); - r = new(char, strlen(name) * 4 + strlen(suffix) + 1); if (!r) return NULL; - for (f = name, t = r; *f; f++) { - if (*f == '/') - *(t++) = '-'; - else if (!strchr(VALID_CHARS, *f)) - t = do_escape_char(*f, t); - else - *(t++) = *f; - } + t = do_escape_mangle(name, allow_globs, r); - if (!endswith(name, suffix)) + if (unit_name_to_type(name) < 0) strcpy(t, suffix); else *t = 0; @@ -592,3 +573,32 @@ int build_subslice(const char *slice, const char*name, char **subslice) { *subslice = ret; return 0; } + +static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = { + [UNIT_REQUIRES] = "Requires", + [UNIT_REQUIRES_OVERRIDABLE] = "RequiresOverridable", + [UNIT_REQUISITE] = "Requisite", + [UNIT_REQUISITE_OVERRIDABLE] = "RequisiteOverridable", + [UNIT_WANTS] = "Wants", + [UNIT_BINDS_TO] = "BindsTo", + [UNIT_PART_OF] = "PartOf", + [UNIT_REQUIRED_BY] = "RequiredBy", + [UNIT_REQUIRED_BY_OVERRIDABLE] = "RequiredByOverridable", + [UNIT_WANTED_BY] = "WantedBy", + [UNIT_BOUND_BY] = "BoundBy", + [UNIT_CONSISTS_OF] = "ConsistsOf", + [UNIT_CONFLICTS] = "Conflicts", + [UNIT_CONFLICTED_BY] = "ConflictedBy", + [UNIT_BEFORE] = "Before", + [UNIT_AFTER] = "After", + [UNIT_ON_FAILURE] = "OnFailure", + [UNIT_TRIGGERS] = "Triggers", + [UNIT_TRIGGERED_BY] = "TriggeredBy", + [UNIT_PROPAGATES_RELOAD_TO] = "PropagatesReloadTo", + [UNIT_RELOAD_PROPAGATED_FROM] = "ReloadPropagatedFrom", + [UNIT_JOINS_NAMESPACE_OF] = "JoinsNamespaceOf", + [UNIT_REFERENCES] = "References", + [UNIT_REFERENCED_BY] = "ReferencedBy", +}; + +DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency);