aboutsummaryrefslogtreecommitdiffstats
path: root/src/wgtpkg-uninstall.c
blob: dba667c2777058a36f40a24feb1ba8252cf1ef91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 Copyright (C) 2015-2020 IoT.bzh

 author: José Bollo <jose.bollo@iot.bzh>

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

#define _GNU_SOURCE

#include <limits.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <fcntl.h>

#include "verbose.h"
#include "utils-dir.h"
#include "secmgr-wrap.h"
#include "wgtpkg-unit.h"
#include "wgt.h"
#include "wgt-info.h"

/* uninstall the widget of idaver */
int uninstall_widget(const char *idaver, const char *root)
{
#if DISTINCT_VERSIONS
	char *id;
	char *ver;
	const char *at;
#endif
	char path[PATH_MAX];
	int rc, rc2;
	struct unitconf uconf;
	struct wgt_info *ifo;

	NOTICE("-- UNINSTALLING widget of id %s from %s --", idaver, root);

	/* find the last '@' of the id */
#if DISTINCT_VERSIONS
	at = strrchr(idaver, '@');
	if (at == NULL) {
		ERROR("bad widget id '%s', no @", idaver);
		errno = EINVAL;
		return -1;
	}
	id = strndupa(idaver, (size_t)(at - idaver));
	ver = strdupa(at + 1);

	/* compute the path */
	rc = snprintf(path, sizeof path, "%s/%s/%s", root, id, ver);
#else
	rc = snprintf(path, sizeof path, "%s/%s", root, idaver);
#endif

	if (rc >= (int)sizeof path) {
		ERROR("bad widget id '%s', too long", idaver);
		errno = EINVAL;
		return -1;
	}

	/* removes the units */
	ifo = wgt_info_createat(AT_FDCWD, path, 1, 1, 1);
	if (!ifo) {
		ERROR("can't read widget config in directory '%s': %m", path);
		return -1;
	}
	uconf.installdir = path;
	uconf.icondir = FWK_ICON_DIR;
	uconf.new_afid = 0;
	uconf.base_http_ports = 0;
	unit_uninstall(ifo, &uconf);
	wgt_info_unref(ifo);

	/* removes the directory of the application */
	rc = remove_directory(path, 1);
	if (rc < 0) {
		ERROR("while removing directory '%s': %m", path);
		return -1;
	}

	/* removes the icon of the application */
	rc = snprintf(path, sizeof path, "%s/%s", FWK_ICON_DIR, idaver);
	assert(rc < (int)sizeof path);
	rc = unlink(path);
	if (rc < 0 && errno != ENOENT)
		ERROR("can't remove '%s': %m", path);

#if DISTINCT_VERSIONS
	/* removes the parent directory if empty */
	rc2 = snprintf(path, sizeof path, "%s/%s", root, id);
	assert(rc2 < (int)sizeof path);
	rc2 = rmdir(path);
	if (rc < 0 && errno == ENOTEMPTY)
		return rc;
	if (rc < 0) {
		ERROR("while removing directory '%s': %m", path);
		return -1;
	}

	/*
	 * parent directory removed: last occurrence of the application
	 * uninstall it for the security-manager
	 */
	rc2 = secmgr_init(id);
#else
	rc2 = secmgr_init(idaver);
#endif
	if (rc2) {
		ERROR("can't init security manager context");
		return -1;
	}
	rc2 = secmgr_uninstall();
	if (rc2) {
		ERROR("can't uninstall security manager context");
		return -1;
	}
	return rc;
}