X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=unit.c;h=dbdd7e663e5783c020f0638071a5d13ad8fa4033;hb=0ae97ec11506cce808232abd0979e20aed2fd625;hp=88615319d7c28ee64e1db4a84abe0836c5ebddd9;hpb=fdf88f5f3383dc4fdd7358b954c6b79e4fe0791b;p=elogind.git diff --git a/unit.c b/unit.c index 88615319d..dbdd7e663 100644 --- a/unit.c +++ b/unit.c @@ -44,7 +44,7 @@ UnitType unit_name_to_type(const char *n) { "0123456789" \ "abcdefghijklmnopqrstuvwxyz" \ "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \ - "-_" + "-_.\\" bool unit_name_is_valid(const char *n) { UnitType t; @@ -155,6 +155,21 @@ int unit_add_name(Unit *u, const char *text) { return 0; } +int unit_choose_id(Unit *u, const char *name) { + char *s; + + assert(u); + assert(name); + + /* Selects one of the names of this unit as the id */ + + if (!(s = set_get(u->meta.names, (char*) name))) + return -ENOENT; + + u->meta.id = s; + return 0; +} + void unit_add_to_load_queue(Unit *u) { assert(u); @@ -372,7 +387,7 @@ void unit_dump(Unit *u, FILE *f, const char *prefix) { /* Common implementation for multiple backends */ int unit_load_fragment_and_dropin(Unit *u) { - int r; + int r, ret; assert(u); @@ -380,11 +395,13 @@ int unit_load_fragment_and_dropin(Unit *u) { if ((r = unit_load_fragment(u)) < 0) return r; + ret = r > 0; + /* Load drop-in directory data */ if ((r = unit_load_dropin(u)) < 0) return r; - return 0; + return ret; } int unit_load(Unit *u) { @@ -834,6 +851,19 @@ int unit_add_dependency(Unit *u, UnitDependency d, Unit *other) { return 0; } +int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name) { + Unit *other; + int r; + + if ((r = manager_load_unit(u->meta.manager, name, &other)) < 0) + return r; + + if ((r = unit_add_dependency(u, d, other)) < 0) + return r; + + return 0; +} + const char *unit_path(void) { char *e; @@ -872,3 +902,42 @@ int set_unit_path(const char *p) { return 0; } + +char *unit_name_escape_path(const char *path, const char *suffix) { + char *r, *t; + const char *f; + size_t a, b; + + assert(path); + assert(suffix); + + /* Takes a path and a util suffix and makes a nice unit name + * of it, escaping all weird chars on the way. + * + * / becomes _, and all chars not alloweed in a unit name get + * escaped as \xFF, including the _ and the \ itself, of + * course. This escaping is hence reversible. + */ + + a = strlen(path); + b = strlen(suffix); + + if (!(r = new(char, a*4+b+1))) + return NULL; + + for (f = path, t = r; *f; f++) { + if (*f == '/') + *(t++) = '_'; + else if (*f == '_' || *f == '\\' || !strchr(VALID_CHARS, *f)) { + *(t++) = '\\'; + *(t++) = 'x'; + *(t++) = hexchar(*f > 4); + *(t++) = hexchar(*f); + } else + *(t++) = *f; + } + + memcpy(t, suffix, b+1); + + return r; +}