Adds 2017 to copyrights
[src/app-framework-main.git] / src / wgtpkg-uninstall.c
1 /*
2  Copyright 2015, 2016, 2017 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
28 #include "verbose.h"
29 #include "utils-dir.h"
30 #include "secmgr-wrap.h"
31
32 /* uninstall the widget of idaver */
33 int uninstall_widget(const char *idaver, const char *root)
34 {
35         char *id;
36         char *ver;
37         char path[PATH_MAX];
38         const char *at;
39         int rc, rc2;
40
41         NOTICE("-- UNINSTALLING widget of id %s from %s --", idaver, root);
42
43         /* find the last '@' of the id */
44         at = strrchr(idaver, '@');
45         if (at == NULL) {
46                 ERROR("bad widget id '%s', no @", idaver);
47                 errno = EINVAL;
48                 return -1;
49         }
50         id = strndupa(idaver, (size_t)(at - idaver));
51         ver = strdupa(at + 1);
52
53         /* compute the path */
54         rc = snprintf(path, sizeof path, "%s/%s/%s", root, id, ver);
55         if (rc >= (int)sizeof path) {
56                 ERROR("bad widget id '%s', too long", idaver);
57                 errno = EINVAL;
58                 return -1;
59         }
60
61         /* removes the directory of the application */
62         rc = remove_directory(path, 1);
63         if (rc < 0) {
64                 ERROR("error while removing directory '%s': %m", path);
65                 return -1;
66         }
67
68         /* removes the icon of the application */
69         rc = snprintf(path, sizeof path, "%s/%s", FWK_ICON_DIR, idaver);
70         assert(rc < (int)sizeof path);
71         rc = unlink(path);
72         if (rc < 0)
73                 ERROR("can't removing '%s': %m", path);
74
75         /* removes the parent directory if empty */
76         rc2 = snprintf(path, sizeof path, "%s/%s", root, id);
77         assert(rc2 < (int)sizeof path);
78         rc2 = rmdir(path);
79         if (rc < 0 && errno == ENOTEMPTY)
80                 return rc;
81         if (rc < 0) {
82                 ERROR("error while removing directory '%s': %m", path);
83                 return -1;
84         }
85
86         /*
87          * parent directory removed: last occurrence of the application
88          * uninstall it for the security-manager
89          */
90         rc2 = secmgr_init(id);
91         if (rc2) {
92                 ERROR("can't init security manager context");
93                 return -1;
94         }
95         rc2 = secmgr_uninstall();
96         if (rc2) {
97                 ERROR("can't uninstall security manager context");
98                 return -1;
99         }
100         return rc;
101 }
102