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