forked from alexlarsson/xdg-app
-
Notifications
You must be signed in to change notification settings - Fork 1
/
xdg-app-builtins-build-finish.c
274 lines (227 loc) · 7.67 KB
/
xdg-app-builtins-build-finish.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
#include "config.h"
#include <locale.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ftw.h>
#include "libgsystem.h"
#include "xdg-app-builtins.h"
#include "xdg-app-utils.h"
static char *opt_command;
static char **opt_allow;
static GOptionEntry options[] = {
{ "command", 0, 0, G_OPTION_ARG_STRING, &opt_command, "Command to set", "COMMAND" },
{ "allow", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_allow, "Environment options to set to true", "KEY" },
{ NULL }
};
static GFile *show_export_base;
static int
show_export (const char *fpath, const struct stat *sb, int typeflag)
{
if (typeflag == FTW_F)
{
gs_unref_object GFile *file;
gs_free char *relpath;
file = g_file_new_for_path (fpath);
relpath = g_file_get_relative_path (show_export_base, file);
g_print ("Exporting %s\n", relpath);
}
return 0;
}
static gboolean
collect_exports (GFile *base, GCancellable *cancellable, GError **error)
{
gboolean ret = FALSE;
gs_unref_object GFile *files = NULL;
gs_unref_object GFile *export = NULL;
const char *paths[] = {
"share/applications", /* Copy desktop files */
"share/icons/hicolor", /* Icons */
"share/dbus-1/services", /* D-Bus service files */
"share/gnome-shell/search-providers", /* Search providers */
NULL,
};
int i;
const char *path;
files = g_file_get_child (base, "files");
export = g_file_get_child (base, "export");
if (!gs_file_ensure_directory (export, TRUE, cancellable, error))
goto out;
for (i = 0; paths[i]; i++)
{
gs_unref_object GFile *src = NULL;
src = g_file_resolve_relative_path (files, paths[i]);
if (g_file_query_exists (src, cancellable))
{
g_debug ("Exporting from %s", paths[i]);
gs_unref_object GFile *dest = NULL;
gs_unref_object GFile *dest_parent = NULL;
dest = g_file_resolve_relative_path (export, paths[i]);
dest_parent = g_file_get_parent (dest);
g_debug ("Ensuring export/%s parent exists", paths[i]);
if (!gs_file_ensure_directory (dest_parent, TRUE, cancellable, error))
goto out;
g_debug ("Copying from files/%s", paths[i]);
if (!gs_shutil_cp_a (src, dest, cancellable, error))
goto out;
}
}
path = g_file_get_path (export);
show_export_base = export;
ftw (path, show_export, 10);
ret = TRUE;
g_assert_no_error (*error);
out:
return ret;
}
static gboolean
update_metadata (GFile *base, GCancellable *cancellable, GError **error)
{
gboolean ret = FALSE;
gs_unref_object GFile *metadata = NULL;
gs_free char *path = NULL;
gs_unref_keyfile GKeyFile *keyfile = NULL;
GError *temp_error = NULL;
const char *environment_keys[] = {
"x11", "wayland", "ipc", "pulseaudio", "system-dbus", "session-dbus",
"network", "host-fs", "homedir", NULL
};
const char *key;
int i;
metadata = g_file_get_child (base, "metadata");
if (!g_file_query_exists (metadata, cancellable))
goto out;
path = g_file_get_path (metadata);
keyfile = g_key_file_new ();
if (!g_key_file_load_from_file (keyfile, path, G_KEY_FILE_NONE, error))
goto out;
if (g_key_file_has_key (keyfile, "Application", "command", NULL))
{
g_debug ("Command key is present");
if (opt_command)
g_key_file_set_string (keyfile, "Application", "command", opt_command);
}
else if (opt_command)
{
g_debug ("Using explicitly provided command %s", opt_command);
g_key_file_set_string (keyfile, "Application", "command", opt_command);
}
else
{
gs_free char *command = NULL;
gs_unref_object GFile *bin_dir = NULL;
gs_unref_object GFileEnumerator *bin_enum = NULL;
gs_unref_object GFileInfo *child_info = NULL;
g_debug ("Looking for executables");
bin_dir = g_file_resolve_relative_path (base, "files/bin");
if (g_file_query_exists (bin_dir, cancellable))
{
bin_enum = g_file_enumerate_children (bin_dir, G_FILE_ATTRIBUTE_STANDARD_NAME,
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
cancellable, error);
if (!bin_enum)
goto out;
while ((child_info = g_file_enumerator_next_file (bin_enum, cancellable, &temp_error)))
{
if (command != NULL)
{
g_print ("More than one executable found\n");
break;
}
command = g_strdup (g_file_info_get_name (child_info));
}
if (temp_error != NULL)
goto out;
}
if (command)
{
g_print ("Using %s as command\n", command);
g_key_file_set_string (keyfile, "Application", "command", command);
}
else
{
g_print ("No executable found\n");
}
}
g_debug ("Setting environment");
for (i = 0; environment_keys[i]; i++)
{
key = environment_keys[i];
g_key_file_set_boolean (keyfile, "Environment", key, FALSE);
}
if (opt_allow)
{
for (i = 0; opt_allow[i]; i++)
{
key = opt_allow[i];
if (!g_strv_contains (environment_keys, key))
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Unknown Environment key %s", key);
goto out;
}
g_key_file_set_boolean (keyfile, "Environment", key, TRUE);
}
}
if (!g_key_file_save_to_file (keyfile, path, error))
goto out;
ret = TRUE;
out:
if (temp_error != NULL)
g_propagate_error (error, temp_error);
return ret;
}
gboolean
xdg_app_builtin_build_finish (int argc, char **argv, GCancellable *cancellable, GError **error)
{
gboolean ret = FALSE;
GOptionContext *context;
gs_unref_object GFile *base = NULL;
gs_unref_object GFile *files_dir = NULL;
gs_unref_object GFile *export_dir = NULL;
gs_unref_object GFile *var_dir = NULL;
gs_unref_object GFile *var_tmp_dir = NULL;
gs_unref_object GFile *var_run_dir = NULL;
gs_unref_object GFile *metadata_file = NULL;
gs_unref_object XdgAppDir *user_dir = NULL;
gs_unref_object XdgAppDir *system_dir = NULL;
const char *directory;
context = g_option_context_new ("DIRECTORY - Convert a directory to a bundle");
if (!xdg_app_option_context_parse (context, options, &argc, &argv, XDG_APP_BUILTIN_FLAG_NO_DIR, NULL, cancellable, error))
goto out;
if (argc < 2)
{
usage_error (context, "DIRECTORY must be specified", error);
goto out;
}
directory = argv[1];
base = g_file_new_for_commandline_arg (directory);
files_dir = g_file_get_child (base, "files");
export_dir = g_file_get_child (base, "export");
var_dir = g_file_get_child (base, "var");
var_tmp_dir = g_file_get_child (var_dir, "tmp");
var_run_dir = g_file_get_child (var_dir, "run");
metadata_file = g_file_get_child (base, "metadata");
if (!g_file_query_exists (files_dir, cancellable) ||
!g_file_query_exists (metadata_file, cancellable))
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Build directory %s not initialized", directory);
goto out;
}
if (g_file_query_exists (export_dir, cancellable))
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Build directory %s already finalized", directory);
goto out;
}
g_debug ("Collecting exports");
if (!collect_exports (base, cancellable, error))
goto out;
g_debug ("Updating metadata");
if (!update_metadata (base, cancellable, error))
goto out;
g_print ("Please review the exported files and the metadata\n");
ret = TRUE;
out:
if (context)
g_option_context_free (context);
return ret;
}