Fix uninstall of widgets without icons
[src/app-framework-main.git] / src / wgtpkg-uninstall.c
1 /*
2  Copyright (C) 2015-2020 IoT.bzh
3  Copyright (C) 2020 Konsulko Group
4
5  author: José Bollo <jose.bollo@iot.bzh>
6
7  Licensed under the Apache License, Version 2.0 (the "License");
8  you may not use this file except in compliance with the License.
9  You may obtain a copy of the License at
10
11      http://www.apache.org/licenses/LICENSE-2.0
12
13  Unless required by applicable law or agreed to in writing, software
14  distributed under the License is distributed on an "AS IS" BASIS,
15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  See the License for the specific language governing permissions and
17  limitations under the License.
18 */
19
20 #define _GNU_SOURCE
21
22 #include <limits.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <assert.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "verbose.h"
31 #include "utils-dir.h"
32 #include "secmgr-wrap.h"
33 #include "wgtpkg-unit.h"
34 #include "wgt.h"
35 #include "wgt-info.h"
36
37 /* uninstall the widget of idaver */
38 int uninstall_widget(const char *idaver, const char *root)
39 {
40 #if DISTINCT_VERSIONS
41         char *id;
42         char *ver;
43         const char *at;
44 #endif
45         char path[PATH_MAX];
46         int rc;
47         struct unitconf uconf;
48         struct wgt_info *ifo;
49
50         NOTICE("-- UNINSTALLING widget of id %s from %s --", idaver, root);
51
52         /* find the last '@' of the id */
53 #if DISTINCT_VERSIONS
54         at = strrchr(idaver, '@');
55         if (at == NULL) {
56                 ERROR("bad widget id '%s', no @", idaver);
57                 errno = EINVAL;
58                 return -1;
59         }
60         id = strndupa(idaver, (size_t)(at - idaver));
61         ver = strdupa(at + 1);
62
63         /* compute the path */
64         rc = snprintf(path, sizeof path, "%s/%s/%s", root, id, ver);
65 #else
66         rc = snprintf(path, sizeof path, "%s/%s", root, idaver);
67 #endif
68
69         if (rc >= (int)sizeof path) {
70                 ERROR("bad widget id '%s', too long", idaver);
71                 errno = EINVAL;
72                 return -1;
73         }
74
75         /* removes the units */
76         ifo = wgt_info_createat(AT_FDCWD, path, 1, 1, 1);
77         if (!ifo) {
78                 ERROR("can't read widget config in directory '%s': %m", path);
79                 return -1;
80         }
81         uconf.installdir = path;
82         uconf.icondir = FWK_ICON_DIR;
83         uconf.new_afid = 0;
84         uconf.base_http_ports = 0;
85         unit_uninstall(ifo, &uconf);
86         wgt_info_unref(ifo);
87
88         /* removes the directory of the application */
89         rc = remove_directory(path, 1);
90         if (rc < 0) {
91                 ERROR("while removing directory '%s': %m", path);
92                 return -1;
93         }
94
95         /* removes the icon of the application */
96         rc = snprintf(path, sizeof path, "%s/%s", FWK_ICON_DIR, idaver);
97         assert(rc < (int)sizeof path);
98         rc = unlink(path);
99         if (rc < 0 && errno != ENOENT) {
100                 ERROR("can't remove '%s': %m", path);
101                 return -1;
102         }
103
104 #if DISTINCT_VERSIONS
105         /* removes the parent directory if empty */
106         rc = snprintf(path, sizeof path, "%s/%s", root, id);
107         assert(rc < (int)sizeof path);
108         rc = rmdir(path);
109         if (rc < 0 && errno == ENOTEMPTY)
110                 return rc;
111         if (rc < 0) {
112                 ERROR("while removing directory '%s': %m", path);
113                 return -1;
114         }
115
116         /*
117          * parent directory removed: last occurrence of the application
118          * uninstall it for the security-manager
119          */
120         rc = secmgr_init(id);
121 #else
122         rc = secmgr_init(idaver);
123 #endif
124         if (rc) {
125                 ERROR("can't init security manager context");
126                 return -1;
127         }
128         rc = secmgr_uninstall();
129         if (rc) {
130                 ERROR("can't uninstall security manager context");
131                 return -1;
132         }
133         return 0;
134 }