forked from alexlarsson/xdg-app
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxdg-app-builtins-uninstall.c
306 lines (250 loc) · 8.04 KB
/
xdg-app-builtins-uninstall.c
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "config.h"
#include <locale.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "libgsystem.h"
#include "xdg-app-builtins.h"
#include "xdg-app-utils.h"
static char *opt_arch;
static gboolean opt_keep_ref;
static gboolean opt_force_remove;
static GOptionEntry options[] = {
{ "arch", 0, 0, G_OPTION_ARG_STRING, &opt_arch, "Arch to uninstall", "ARCH" },
{ "keep-ref", 0, 0, G_OPTION_ARG_NONE, &opt_keep_ref, "Keep ref in local repository", NULL },
{ "force-remove", 0, 0, G_OPTION_ARG_NONE, &opt_force_remove, "Remove files even if running", NULL },
{ NULL }
};
static gboolean
single_child_directory (GFile *dir, const char *name, GCancellable *cancellable)
{
gboolean ret = FALSE;
gs_unref_object GFileEnumerator *dir_enum = NULL;
gs_unref_object GFileInfo *child_info = NULL;
dir_enum = g_file_enumerate_children (dir, G_FILE_ATTRIBUTE_STANDARD_NAME,
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
cancellable, NULL);
if (!dir_enum)
goto out;
while ((child_info = g_file_enumerator_next_file (dir_enum, cancellable, NULL)))
{
if (strcmp (name, g_file_info_get_name (child_info)) == 0)
{
g_clear_object (&child_info);
continue;
}
goto out;
}
ret = TRUE;
out:
return ret;
}
gboolean
xdg_app_builtin_uninstall_runtime (int argc, char **argv, GCancellable *cancellable, GError **error)
{
gboolean ret = FALSE;
GOptionContext *context;
gs_unref_object XdgAppDir *dir = NULL;
gs_unref_object GFile *deploy_base = NULL;
gs_unref_object GFile *arch_dir = NULL;
gs_unref_object GFile *top_dir = NULL;
gs_unref_object GFile *origin = NULL;
gs_unref_object OstreeRepo *repo = NULL;
const char *name;
const char *arch;
const char *branch;
gs_free char *ref = NULL;
gs_free char *repository = NULL;
gs_strfreev char **deployed = NULL;
int i;
GError *temp_error = NULL;
context = g_option_context_new ("RUNTIME [BRANCH] - Uninstall a runtime");
if (!xdg_app_option_context_parse (context, options, &argc, &argv, 0, &dir, cancellable, error))
goto out;
if (argc < 2)
{
usage_error (context, "RUNTIME must be specified", error);
goto out;
}
name = argv[1];
if (argc > 2)
branch = argv[2];
else
branch = "master";
if (opt_arch)
arch = opt_arch;
else
arch = xdg_app_get_arch ();
if (!xdg_app_is_valid_name (name))
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "'%s' is not a valid runtime name", name);
goto out;
}
if (!xdg_app_is_valid_branch (branch))
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "'%s' is not a valid branch name", branch);
goto out;
}
/* TODO: look for apps, require --force */
ref = g_build_filename ("runtime", name, arch, branch, NULL);
deploy_base = xdg_app_dir_get_deploy_dir (dir, ref);
if (!g_file_query_exists (deploy_base, cancellable))
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Nothing to uninstall");
goto out;
}
origin = g_file_get_child (deploy_base, "origin");
if (!g_file_load_contents (origin, cancellable, &repository, NULL, NULL, error))
goto out;
g_debug ("dropping active ref");
if (!xdg_app_dir_set_active (dir, ref, NULL, cancellable, error))
goto out;
if (!xdg_app_dir_list_deployed (dir, ref, &deployed, cancellable, error))
goto out;
for (i = 0; deployed[i] != NULL; i++)
{
g_debug ("undeploying %s", deployed[i]);
if (!xdg_app_dir_undeploy (dir, ref, deployed[i], opt_force_remove, cancellable, error))
goto out;
}
g_debug ("removing deploy base");
if (!gs_shutil_rm_rf (deploy_base, cancellable, error))
goto out;
g_debug ("cleaning up empty directories");
arch_dir = g_file_get_parent (deploy_base);
if (!g_file_delete (arch_dir, cancellable, &temp_error))
{
if (!g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY))
{
g_propagate_error (error, temp_error);
goto out;
}
g_clear_error (&temp_error);
}
top_dir = g_file_get_parent (arch_dir);
if (!g_file_delete (top_dir, cancellable, &temp_error))
{
if (!g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY))
{
g_propagate_error (error, temp_error);
goto out;
}
g_clear_error (&temp_error);
}
if (!opt_keep_ref)
{
repo = xdg_app_dir_get_repo (dir);
if (!ostree_repo_set_ref_immediate (repo, repository, ref, NULL, cancellable, error))
goto out;
if (!xdg_app_dir_prune (dir, cancellable, error))
goto out;
}
xdg_app_dir_cleanup_removed (dir, cancellable, NULL);
ret = TRUE;
out:
if (context)
g_option_context_free (context);
return ret;
}
gboolean
xdg_app_builtin_uninstall_app (int argc, char **argv, GCancellable *cancellable, GError **error)
{
gboolean ret = FALSE;
GOptionContext *context;
gs_unref_object XdgAppDir *dir = NULL;
gs_unref_object GFile *deploy_base = NULL;
gs_unref_object GFile *arch_dir = NULL;
gs_unref_object GFile *top_dir = NULL;
gs_unref_object GFile *origin = NULL;
gs_unref_object OstreeRepo *repo = NULL;
const char *name;
const char *arch;
const char *branch;
gs_free char *ref = NULL;
gs_free char *repository = NULL;
gs_strfreev char **deployed = NULL;
int i;
GError *temp_error = NULL;
context = g_option_context_new ("APP [BRANCH] - Uninstall an application");
if (!xdg_app_option_context_parse (context, options, &argc, &argv, 0, &dir, cancellable, error))
goto out;
if (argc < 2)
{
usage_error (context, "APP must be specified", error);
goto out;
}
name = argv[1];
if (argc > 2)
branch = argv[2];
else
branch = "master";
if (opt_arch)
arch = opt_arch;
else
arch = xdg_app_get_arch ();
if (!xdg_app_is_valid_name (name))
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "'%s' is not a valid application name", name);
goto out;
}
if (!xdg_app_is_valid_branch (branch))
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "'%s' is not a valid branch name", branch);
goto out;
}
ref = g_build_filename ("app", name, arch, branch, NULL);
deploy_base = xdg_app_dir_get_deploy_dir (dir, ref);
if (!g_file_query_exists (deploy_base, cancellable))
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Nothing to uninstall");
goto out;
}
origin = g_file_get_child (deploy_base, "origin");
if (!g_file_load_contents (origin, cancellable, &repository, NULL, NULL, error))
goto out;
g_debug ("dropping active ref");
if (!xdg_app_dir_set_active (dir, ref, NULL, cancellable, error))
goto out;
if (!xdg_app_dir_list_deployed (dir, ref, &deployed, cancellable, error))
goto out;
for (i = 0; deployed[i] != NULL; i++)
{
g_debug ("undeploying %s", deployed[i]);
if (!xdg_app_dir_undeploy (dir, ref, deployed[i], opt_force_remove, cancellable, error))
goto out;
}
g_debug ("removing deploy base");
if (!gs_shutil_rm_rf (deploy_base, cancellable, error))
goto out;
g_debug ("cleaning up empty directories");
arch_dir = g_file_get_parent (deploy_base);
if (!g_file_delete (arch_dir, cancellable, &temp_error))
{
if (!g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY))
{
g_propagate_error (error, temp_error);
goto out;
}
g_clear_error (&temp_error);
}
top_dir = g_file_get_parent (arch_dir);
if (single_child_directory (top_dir, "data", cancellable))
{
if (!gs_shutil_rm_rf (top_dir, cancellable, error))
goto out;
}
if (!opt_keep_ref)
{
repo = xdg_app_dir_get_repo (dir);
if (!ostree_repo_set_ref_immediate (repo, repository, ref, NULL, cancellable, error))
goto out;
if (!xdg_app_dir_prune (dir, cancellable, error))
goto out;
}
xdg_app_dir_cleanup_removed (dir, cancellable, NULL);
ret = TRUE;
out:
if (context)
g_option_context_free (context);
return ret;
}