X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fwgtpkg-unit.c;h=9f143469fc2ca3530fa1f83683f01bca3e6910b5;hb=765d08087bf422c821556400a68e1fb4868f85f3;hp=8607eb40c5803229a6b2ad1baa8fa645623987e0;hpb=c5f9125c75bd2aa7db9dfad6e2dc178fff2d2f73;p=src%2Fapp-framework-main.git diff --git a/src/wgtpkg-unit.c b/src/wgtpkg-unit.c index 8607eb4..9f14346 100644 --- a/src/wgtpkg-unit.c +++ b/src/wgtpkg-unit.c @@ -1,5 +1,5 @@ /* - Copyright 2016, 2017 IoT.bzh + Copyright (C) 2016, 2017, 2018 IoT.bzh author: José Bollo @@ -28,7 +28,7 @@ #include #include - + #include "verbose.h" #include "utils-file.h" @@ -38,6 +38,7 @@ #include "utils-systemd.h" #include "wgtpkg-unit.h" +#include "wgt-strings.h" #if 0 #include @@ -54,7 +55,7 @@ static char *template; * When returning 1 and 'after' isn't NULL, the pointer to the * first character after the pettern in 'text' is stored in 'after'. * The characters '\n' and ' ' have a special meaning in the search: - * * '\n': matches any space or tabs (including none) followed + * * '\n': matches any space or tabs (including none) followed * either by '\n' or '\0' (end of the string) * * ' ': matches any space or tabs but at least one. */ @@ -249,7 +250,7 @@ static int process_one_unit(char *spec, struct unitdesc *desc) * with its given 'closure' and the array descripbing the units. * Return 0 in case of success or a negative value in case of error. */ -static int process_all_units(char *corpus, const struct unitconf *conf, int (*process)(void *closure, const struct generatedesc *desc), void *closure) +static int process_all_units(char *corpus, const struct unitconf *conf, int (*process)(void *closure, const struct generatedesc *desc), void *closure, struct json_object *jdesc) { int rc, rc2; char *beg, *end, *befbeg, *aftend; @@ -257,6 +258,7 @@ static int process_all_units(char *corpus, const struct unitconf *conf, int (*pr struct generatedesc gdesc; gdesc.conf = conf; + gdesc.desc = jdesc; gdesc.units = NULL; gdesc.nunits = 0; rc = rc2 = 0; @@ -343,7 +345,7 @@ static int process_all_units(char *corpus, const struct unitconf *conf, int (*pr /* * Clear the unit generator */ -void unit_generator_off() +void unit_generator_close_template() { free(template); template = NULL; @@ -353,13 +355,13 @@ void unit_generator_off() * Initialises the unit generator with the content of the file of path 'filename'. * Returns 0 in case of success or a negative number in case of error. */ -int unit_generator_on(const char *filename) +int unit_generator_open_template(const char *filename) { size_t size; char *tmp; int rc; - unit_generator_off(); + unit_generator_close_template(); rc = getfile(filename ? : FWK_UNIT_CONF, &template, NULL); if (!rc) { size = pack(template, ';'); @@ -372,14 +374,30 @@ int unit_generator_on(const char *filename) static int add_metadata(struct json_object *jdesc, const struct unitconf *conf) { + struct json_object *targets, *targ; char portstr[30]; + int port, i, n; + + if (json_object_object_get_ex(jdesc, string_targets, &targets)) { + n = json_object_array_length(targets); + for (i = 0 ; i < n ; i++) { + targ = json_object_array_get_idx(targets, i); + if (!conf->port) + strcpy(portstr, "0"); + else { + port = conf->port ? conf->port() : 0; + if (port < 0) + return port; + sprintf(portstr, "%d", port); + } + if (!j_add_string_m(targ, "#metatarget.http-port", portstr)) + return -1; + } + } - sprintf(portstr, "%d", conf->port); return j_add_many_strings_m(jdesc, "#metadata.install-dir", conf->installdir, - "#metadata.app-data-dir", "%h/app-data", "#metadata.icons-dir", conf->icondir, - "#metadata.http-port", portstr, NULL) ? 0 : -1; } @@ -387,7 +405,7 @@ static int add_metadata(struct json_object *jdesc, const struct unitconf *conf) * Applies the object 'jdesc' augmented of meta data coming * from 'conf' to the current unit generator. * The current unit generator will be set to the default one if not unit - * was previously set using the function 'unit_generator_on'. + * was previously set using the function 'unit_generator_open_template'. * The callback function 'process' is then called with the * unit descriptors array and the expected closure. * Return what returned process in case of success or a negative @@ -403,12 +421,12 @@ int unit_generator_process(struct json_object *jdesc, const struct unitconf *con if (rc) ERROR("can't set the metadata. %m"); else { - rc = template ? 0 : unit_generator_on(NULL); + rc = template ? 0 : unit_generator_open_template(NULL); if (!rc) { instance = NULL; rc = apply_mustach(template, jdesc, &instance, &size); if (!rc) - rc = process_all_units(instance, conf, process, closure); + rc = process_all_units(instance, conf, process, closure, jdesc); free(instance); } }