forked from alexlarsson/xdg-app
-
Notifications
You must be signed in to change notification settings - Fork 1
/
xdg-app-builtins-install.c
178 lines (139 loc) · 4.77 KB
/
xdg-app-builtins-install.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
#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 GOptionEntry options[] = {
{ "arch", 0, 0, G_OPTION_ARG_STRING, &opt_arch, "Arch to install for", "ARCH" },
{ NULL }
};
gboolean
xdg_app_builtin_install_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 *repository;
const char *runtime;
const char *branch = "master";
gs_free char *ref = NULL;
gboolean created_deploy_base = FALSE;
context = g_option_context_new ("REPOSITORY RUNTIME [BRANCH] - Install a runtime");
if (!xdg_app_option_context_parse (context, options, &argc, &argv, 0, &dir, cancellable, error))
goto out;
if (argc < 3)
{
usage_error (context, "REPOSITORY and RUNTIME must be specified", error);
goto out;
}
repository = argv[1];
runtime = argv[2];
if (argc >= 4)
branch = argv[3];
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 already installed", runtime, branch);
goto out;
}
if (!xdg_app_dir_pull (dir, repository, ref,
cancellable, error))
goto out;
if (!g_file_make_directory_with_parents (deploy_base, cancellable, error))
goto out;
created_deploy_base = TRUE;
origin = g_file_get_child (deploy_base, "origin");
if (!g_file_replace_contents (origin, repository, strlen (repository), NULL, FALSE,
G_FILE_CREATE_NONE, NULL, cancellable, error))
goto out;
if (!xdg_app_dir_deploy (dir, ref, NULL, cancellable, error))
goto out;
xdg_app_dir_cleanup_removed (dir, cancellable, NULL);
ret = TRUE;
out:
if (created_deploy_base && !ret)
gs_shutil_rm_rf (deploy_base, cancellable, NULL);
if (context)
g_option_context_free (context);
return ret;
}
gboolean
xdg_app_builtin_install_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 *repository;
const char *app;
const char *branch = "master";
gs_free char *ref = NULL;
gboolean created_deploy_base = FALSE;
context = g_option_context_new ("REPOSITORY APP [BRANCH] - Install an application");
if (!xdg_app_option_context_parse (context, options, &argc, &argv, 0, &dir, cancellable, error))
goto out;
if (argc < 3)
{
usage_error (context, "REPOSITORY and APP must be specified", error);
goto out;
}
repository = argv[1];
app = argv[2];
if (argc >= 4)
branch = argv[3];
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 already installed", app, branch);
goto out;
}
if (!xdg_app_dir_pull (dir, repository, ref,
cancellable, error))
goto out;
if (!g_file_make_directory_with_parents (deploy_base, cancellable, error))
goto out;
created_deploy_base = TRUE;
origin = g_file_get_child (deploy_base, "origin");
if (!g_file_replace_contents (origin, repository, strlen (repository), NULL, FALSE,
G_FILE_CREATE_NONE, NULL, cancellable, error))
goto out;
if (!xdg_app_dir_deploy (dir, ref, NULL, cancellable, error))
goto out;
xdg_app_dir_cleanup_removed (dir, cancellable, NULL);
ret = TRUE;
out:
if (created_deploy_base && !ret)
gs_shutil_rm_rf (deploy_base, cancellable, NULL);
if (context)
g_option_context_free (context);
return ret;
}