aa3faacb0c505956a9501524fb231ad2a112a750
[src/app-framework-main.git] / src / wgtpkg-uninstall.c
1 /*
2  Copyright (C) 2015-2019 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         char *id;
40         char *ver;
41         char path[PATH_MAX];
42         const char *at;
43         int rc, rc2;
44         struct unitconf uconf;
45         struct wgt_info *ifo;
46
47         NOTICE("-- UNINSTALLING widget of id %s from %s --", idaver, root);
48
49         /* find the last '@' of the id */
50         at = strrchr(idaver, '@');
51         if (at == NULL) {
52                 ERROR("bad widget id '%s', no @", idaver);
53                 errno = EINVAL;
54                 return -1;
55         }
56         id = strndupa(idaver, (size_t)(at - idaver));
57         ver = strdupa(at + 1);
58
59         /* compute the path */
60         rc = snprintf(path, sizeof path, "%s/%s/%s", root, id, ver);
61         if (rc >= (int)sizeof path) {
62                 ERROR("bad widget id '%s', too long", idaver);
63                 errno = EINVAL;
64                 return -1;
65         }
66
67         /* removes the units */
68         ifo = wgt_info_createat(AT_FDCWD, path, 1, 1, 1);
69         if (!ifo) {
70                 ERROR("can't read widget config in directory '%s': %m", path);
71                 return -1;
72         }
73         uconf.installdir = path;
74         uconf.icondir = FWK_ICON_DIR;
75         uconf.port = 0;
76         unit_uninstall(ifo, &uconf);
77         wgt_info_unref(ifo);
78
79         /* removes the directory of the application */
80         rc = remove_directory(path, 1);
81         if (rc < 0) {
82                 ERROR("while removing directory '%s': %m", path);
83                 return -1;
84         }
85
86         /* removes the icon of the application */
87         rc = snprintf(path, sizeof path, "%s/%s", FWK_ICON_DIR, idaver);
88         assert(rc < (int)sizeof path);
89         rc = unlink(path);
90         if (rc < 0 && errno != ENOENT)
91                 ERROR("can't remove '%s': %m", path);
92
93         /* removes the parent directory if empty */
94         rc2 = snprintf(path, sizeof path, "%s/%s", root, id);
95         assert(rc2 < (int)sizeof path);
96         rc2 = rmdir(path);
97         if (rc < 0 && errno == ENOTEMPTY)
98                 return rc;
99         if (rc < 0) {
100                 ERROR("while removing directory '%s': %m", path);
101                 return -1;
102         }
103
104         /*
105          * parent directory removed: last occurrence of the application
106          * uninstall it for the security-manager
107          */
108         rc2 = secmgr_init(id);
109         if (rc2) {
110                 ERROR("can't init security manager context");
111                 return -1;
112         }
113         rc2 = secmgr_uninstall();
114         if (rc2) {
115                 ERROR("can't uninstall security manager context");
116                 return -1;
117         }
118         return rc;
119 }
120