This repository has been archived by the owner on Oct 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathxdg-app-builtins-build.c
188 lines (153 loc) · 5.77 KB
/
xdg-app-builtins-build.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
/*
* Copyright © 2014 Red Hat, Inc
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Alexander Larsson <[email protected]>
*/
#include "config.h"
#include <locale.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "libgsystem.h"
#include "libglnx/libglnx.h"
#include "xdg-app-builtins.h"
#include "xdg-app-utils.h"
#include "xdg-app-run.h"
static gboolean opt_runtime;
static GOptionEntry options[] = {
{ "runtime", 'r', 0, G_OPTION_ARG_NONE, &opt_runtime, "Use non-devel runtime", NULL },
{ NULL }
};
gboolean
xdg_app_builtin_build (int argc, char **argv, GCancellable *cancellable, GError **error)
{
GOptionContext *context;
gboolean ret = FALSE;
g_autoptr(XdgAppDir) user_dir = NULL;
g_autoptr(XdgAppDeploy) runtime_deploy = NULL;
g_autoptr(GVariantBuilder) optbuilder = NULL;
g_autoptr(GFile) deploy_base = NULL;
g_autoptr(GFile) var = NULL;
g_autoptr(GFile) var_tmp = NULL;
g_autoptr(GFile) var_run = NULL;
g_autoptr(GFile) app_deploy = NULL;
g_autoptr(GFile) app_files = NULL;
g_autoptr(GFile) runtime_files = NULL;
g_autoptr(GFile) metadata = NULL;
g_autofree char *metadata_contents = NULL;
g_autofree char *runtime = NULL;
g_autofree char *default_command = NULL;
g_autofree char *runtime_ref = NULL;
g_autofree char *app_ref = NULL;
g_autoptr(GKeyFile) metakey = NULL;
g_autoptr(GKeyFile) runtime_metakey = NULL;
g_autoptr (GError) my_error = NULL;
g_autoptr (GError) my_error2 = NULL;
g_autoptr(GPtrArray) argv_array = NULL;
glnx_strfreev char **envp = NULL;
gsize metadata_size;
const char *directory = NULL;
const char *command = "/bin/sh";
g_autofree char *app_id = NULL;
int i;
int rest_argv_start, rest_argc;
g_autoptr(XdgAppContext) arg_context = NULL;
g_autoptr(XdgAppContext) app_context = NULL;
context = g_option_context_new ("DIRECTORY [COMMAND [args...]] - Build in directory");
rest_argc = 0;
for (i = 1; i < argc; i++)
{
/* The non-option is the directory, take it out of the arguments */
if (argv[i][0] != '-')
{
rest_argv_start = i;
rest_argc = argc - i;
argc = i;
break;
}
}
arg_context = xdg_app_context_new ();
g_option_context_add_group (context, xdg_app_context_get_options (arg_context));
if (!xdg_app_option_context_parse (context, options, &argc, &argv, XDG_APP_BUILTIN_FLAG_NO_DIR, NULL, cancellable, error))
goto out;
if (rest_argc == 0)
{
usage_error (context, "DIRECTORY must be specified", error);
goto out;
}
directory = argv[rest_argv_start];
if (rest_argc >= 2)
command = argv[rest_argv_start+1];
app_deploy = g_file_new_for_commandline_arg (directory);
metadata = g_file_get_child (app_deploy, "metadata");
if (!g_file_load_contents (metadata, cancellable, &metadata_contents, &metadata_size, NULL, error))
goto out;
metakey = g_key_file_new ();
if (!g_key_file_load_from_data (metakey, metadata_contents, metadata_size, 0, error))
goto out;
app_id = g_key_file_get_string (metakey, "Application", "name", error);
if (app_id == NULL)
goto out;
runtime = g_key_file_get_string (metakey, "Application", opt_runtime ? "runtime" : "sdk", error);
if (runtime == NULL)
goto out;
runtime_ref = g_build_filename ("runtime", runtime, NULL);
runtime_deploy = xdg_app_find_deploy_for_ref (runtime_ref, cancellable, error);
if (runtime_deploy == NULL)
goto out;
runtime_metakey = xdg_app_deploy_get_metadata (runtime_deploy);
var = g_file_get_child (app_deploy, "var");
if (!gs_file_ensure_directory (var, TRUE, cancellable, error))
goto out;
app_files = g_file_get_child (app_deploy, "files");
runtime_files = xdg_app_deploy_get_files (runtime_deploy);
argv_array = g_ptr_array_new_with_free_func (g_free);
g_ptr_array_add (argv_array, g_strdup (HELPER));
g_ptr_array_add (argv_array, g_strdup ("-wr"));
app_context = xdg_app_context_new ();
if (!xdg_app_context_load_metadata (app_context, runtime_metakey, error))
goto out;
if (!xdg_app_context_load_metadata (app_context, metakey, error))
goto out;
xdg_app_context_merge (app_context, arg_context);
xdg_app_context_allow_host_fs (app_context);
xdg_app_run_add_environment_args (argv_array, NULL, app_id,
app_context);
g_ptr_array_add (argv_array, g_strdup ("-a"));
g_ptr_array_add (argv_array, g_file_get_path (app_files));
g_ptr_array_add (argv_array, g_strdup ("-v"));
g_ptr_array_add (argv_array, g_file_get_path (var));
g_ptr_array_add (argv_array, g_file_get_path (runtime_files));
g_ptr_array_add (argv_array, g_strdup (command));
for (i = 2; i < rest_argc; i++)
g_ptr_array_add (argv_array, g_strdup (argv[rest_argv_start + i]));
g_ptr_array_add (argv_array, NULL);
envp = xdg_app_run_get_minimal_env (TRUE);
envp = xdg_app_run_apply_env_vars (envp, app_context);
if (!execve (HELPER, (char **)argv_array->pdata, envp))
{
g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "Unable to start app");
goto out;
}
/* Not actually reached... */
ret = TRUE;
out:
if (context)
g_option_context_free (context);
return ret;
}