forked from alexlarsson/xdg-app
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxdg-app-builtins-update.c
204 lines (167 loc) · 5.47 KB
/
xdg-app-builtins-update.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
#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 char *opt_commit;
static gboolean opt_force_remove;
static GOptionEntry options[] = {
{ "arch", 0, 0, G_OPTION_ARG_STRING, &opt_arch, "Arch to update for", "ARCH" },
{ "commit", 0, 0, G_OPTION_ARG_STRING, &opt_commit, "Commit to deploy", "COMMIT" },
{ "force-remove", 0, 0, G_OPTION_ARG_NONE, &opt_force_remove, "Remove old files even if running", NULL },
{ NULL }
};
gboolean
xdg_app_builtin_update_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 *origin = NULL;
const char *runtime;
const char *branch = "master";
gs_free char *previous_deployment = NULL;
gs_free char *ref = NULL;
gs_free char *repository = NULL;
GError *my_error;
context = g_option_context_new ("RUNTIME [BRANCH] - Update 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;
}
runtime = argv[1];
if (argc >= 3)
branch = argv[2];
if (!xdg_app_is_valid_name (runtime))
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "'%s' is not a valid runtime name", runtime);
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 = xdg_app_build_runtime_ref (runtime, branch, opt_arch);
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, "Runtime %s branch %s not installed", runtime, branch);
goto out;
}
origin = g_file_get_child (deploy_base, "origin");
if (!g_file_load_contents (origin, cancellable, &repository, NULL, NULL, error))
goto out;
if (!xdg_app_dir_pull (dir, repository, ref,
cancellable, error))
goto out;
previous_deployment = xdg_app_dir_read_active (dir, ref, cancellable);
my_error = NULL;
if (!xdg_app_dir_deploy (dir, ref, opt_commit, cancellable, &my_error))
{
if (g_error_matches (my_error, XDG_APP_DIR_ERROR, XDG_APP_DIR_ERROR_ALREADY_DEPLOYED))
g_error_free (my_error);
else
{
g_propagate_error (error, my_error);
goto out;
}
}
if (previous_deployment != NULL)
{
if (!xdg_app_dir_undeploy (dir, ref, previous_deployment,
opt_force_remove,
cancellable, error))
goto out;
if (!xdg_app_dir_prune (dir, cancellable, error))
goto out;
}
ret = TRUE;
out:
if (context)
g_option_context_free (context);
return ret;
}
gboolean
xdg_app_builtin_update_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 *origin = NULL;
const char *app;
const char *branch = "master";
gs_free char *ref = NULL;
gs_free char *repository = NULL;
gs_free char *previous_deployment = NULL;
GError *my_error;
context = g_option_context_new ("APP [BRANCH] - Update 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;
}
app = argv[1];
if (argc >= 3)
branch = argv[2];
if (!xdg_app_is_valid_name (app))
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "'%s' is not a valid application name", app);
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 = xdg_app_build_app_ref (app, branch, opt_arch);
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, "App %s branch %s not installed", app, branch);
goto out;
}
origin = g_file_get_child (deploy_base, "origin");
if (!g_file_load_contents (origin, cancellable, &repository, NULL, NULL, error))
goto out;
if (!xdg_app_dir_pull (dir, repository, ref,
cancellable, error))
goto out;
previous_deployment = xdg_app_dir_read_active (dir, ref, cancellable);
my_error = NULL;
if (!xdg_app_dir_deploy (dir, ref, opt_commit, cancellable, &my_error))
{
if (g_error_matches (my_error, XDG_APP_DIR_ERROR, XDG_APP_DIR_ERROR_ALREADY_DEPLOYED))
g_error_free (my_error);
else
{
g_propagate_error (error, my_error);
goto out;
}
}
if (previous_deployment != NULL)
{
if (!xdg_app_dir_undeploy (dir, ref, previous_deployment,
opt_force_remove,
cancellable, error))
goto out;
if (!xdg_app_dir_prune (dir, cancellable, error))
goto out;
}
ret = TRUE;
out:
if (context)
g_option_context_free (context);
return ret;
}