wgtpkg-unit: fix last empty continuation
[src/app-framework-main.git] / src / wgtpkg-unit.c
index f141766..e2813ba 100644 (file)
@@ -116,12 +116,13 @@ static size_t pack(char *text, char purge)
        char cont;     /* flag telling whether the line continues the previous one */
        char nextcont; /* flag telling whether the line will continues the next one */
 
-       cont = 0;
+       nextcont = 0;
        c = *(write = read = text);
 
        /* iteration over lines */
        while (c) {
                /* computes emit, nextcont, emit and start for the current line */
+               cont = nextcont;
                emit = nextcont = 0;
                start = NULL;
                begin = read;
@@ -140,7 +141,7 @@ static size_t pack(char *text, char purge)
                if (c)
                        c = *++read;
                /* emit the line if not empty */
-               if (emit) {
+               if (emit || (cont && !nextcont)) {
                        /* removes the blanks on the left of not continuing lines */
                        if (!cont && start)
                                begin = start;
@@ -155,7 +156,6 @@ static size_t pack(char *text, char purge)
                                        *write++ = *begin++;
                        }
                }
-               cont = nextcont;
        }
        *write = 0;
        return (size_t)(write - text);
@@ -419,28 +419,63 @@ static int get_wants_target(char *path, size_t pathlen, const struct unitdesc *d
        return rc;
 }
 
+static int do_send_reload(const struct unitdesc descs[], unsigned count)
+{
+       unsigned i;
+       int reloadsys, reloadusr;
+
+       reloadsys = reloadusr = 0;
+       for (i = 0 ; i < count ; i++) {
+               if (descs[i].wanted_by != NULL) {
+                       switch (descs[i].scope) {
+                       case unitscope_user:
+                               reloadusr = 1;
+                               break;
+                       case unitscope_system:
+                               reloadsys = 1;
+                               break;
+                       default:
+                               break;
+                       }
+               }
+       }
+
+       if (reloadusr)
+               reloadusr = systemd_daemon_reload(1);
+       if (reloadsys)
+               reloadsys = systemd_daemon_reload(0);
+       return reloadsys ? : reloadusr ? : 0;
+}
+
 static int do_uninstall_units(void *closure, const struct unitdesc descs[], unsigned count)
 {
        int rc, rc2;
        unsigned i;
        char path[PATH_MAX];
 
+       rc = 0;
        for (i = 0 ; i < count ; i++) {
-               rc = check_unit_desc(&descs[i], 0);
-               if (rc == 0) {
-                       rc = get_unit_path(path, sizeof path, &descs[i]);
-                       if (rc >= 0) {
-                               rc = unlink(path);
+               rc2 = check_unit_desc(&descs[i], 0);
+               if (rc2 == 0) {
+                       rc2 = get_unit_path(path, sizeof path, &descs[i]);
+                       if (rc2 >= 0) {
+                               rc2 = unlink(path);
                        }
+                       if (rc2 < 0 && rc == 0)
+                               rc = rc2;
                        if (descs[i].wanted_by != NULL) {
                                rc2 = get_wants_path(path, sizeof path, &descs[i]);
                                if (rc2 >= 0)
                                        rc2 = unlink(path);
-                               rc = rc < 0 ? rc : rc2;
                        }
                }
+               if (rc2 < 0 && rc == 0)
+                       rc = rc2;
        }
-       return 0;
+       rc2 = do_send_reload(descs, count);
+       if (rc2 < 0 && rc == 0)
+               rc = rc2;
+       return rc;
 }
 
 static int do_install_units(void *closure, const struct unitdesc descs[], unsigned count)
@@ -469,12 +504,16 @@ static int do_install_units(void *closure, const struct unitdesc descs[], unsign
                                i++;
                        }
                }
-               if (rc < 0) {
-                       do_uninstall_units(closure, descs, i);
-                       return rc;
-               }
+               if (rc < 0)
+                       goto error;
        }
+       rc = do_send_reload(descs, count);
+       if (rc < 0)
+               goto error;
        return 0;
+error:
+       do_uninstall_units(closure, descs, i);
+       return rc;
 }
 
 static int add_metadata(struct json_object *jdesc, const char *installdir, const char *icondir, int port)