forked from alexlarsson/xdg-app
-
Notifications
You must be signed in to change notification settings - Fork 1
/
xdg-app-builtins-build-export.c
245 lines (200 loc) · 6.94 KB
/
xdg-app-builtins-build-export.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
#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_subject;
static char *opt_body;
static GOptionEntry options[] = {
{ "subject", 's', 0, G_OPTION_ARG_STRING, &opt_subject, "One line subject", "SUBJECT" },
{ "body", 'b', 0, G_OPTION_ARG_STRING, &opt_body, "Full description", "BODY" },
{ NULL }
};
static gboolean
metadata_get_arch (GFile *file, char **out_arch, GError **error)
{
gboolean ret = FALSE;
gs_free char *path = NULL;
gs_unref_keyfile GKeyFile *keyfile = NULL;
gs_free char *runtime = NULL;
gs_strfreev char **parts = NULL;
keyfile = g_key_file_new ();
path = g_file_get_path (file);
if (!g_key_file_load_from_file (keyfile, path, G_KEY_FILE_NONE, error))
goto out;
runtime = g_key_file_get_string (keyfile, "Application", "runtime", error);
if (*error)
goto out;
parts = g_strsplit (runtime, "/", 0);
if (g_strv_length (parts) != 3)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Failed to determine arch from metadata runtime key: %s", runtime);
goto out;
}
*out_arch = g_strdup (parts[1]);
ret = TRUE;
out:
return ret;
}
static gboolean
is_empty_directory (GFile *file, GCancellable *cancellable)
{
gs_unref_object GFileEnumerator *file_enum = NULL;
gs_unref_object GFileInfo *child_info = NULL;
file_enum = g_file_enumerate_children (file, G_FILE_ATTRIBUTE_STANDARD_NAME,
G_FILE_QUERY_INFO_NONE,
cancellable, NULL);
if (!file_enum)
return FALSE;
child_info = g_file_enumerator_next_file (file_enum, cancellable, NULL);
if (child_info)
return FALSE;
return TRUE;
}
static OstreeRepoCommitFilterResult
commit_filter (OstreeRepo *repo,
const char *path,
GFileInfo *file_info,
gpointer user_data)
{
if (g_str_equal (path, "/") ||
g_str_equal (path, "/metadata") ||
g_str_has_prefix (path, "/files") ||
g_str_has_prefix (path, "/export"))
{
g_debug ("commit filter, allow: %s", path);
return OSTREE_REPO_COMMIT_FILTER_ALLOW;
}
else
{
g_debug ("commit filter, skip: %s", path);
return OSTREE_REPO_COMMIT_FILTER_SKIP;
}
}
gboolean
xdg_app_builtin_build_export (int argc, char **argv, GCancellable *cancellable, GError **error)
{
gboolean ret = FALSE;
GOptionContext *context;
gs_unref_object GFile *base = NULL;
gs_unref_object GFile *files = NULL;
gs_unref_object GFile *metadata = NULL;
gs_unref_object GFile *export = NULL;
gs_unref_object GFile *repofile = NULL;
gs_unref_object GFile *arg = NULL;
gs_unref_object GFile *root = NULL;
gs_unref_object OstreeRepo *repo = NULL;
const char *location;
const char *directory;
const char *name;
const char *branch;
gs_free char *arch = NULL;
gs_free char *full_branch = NULL;
gs_free char *parent = NULL;
gs_free char *commit_checksum = NULL;
gs_unref_object OstreeMutableTree *mtree = NULL;
char *subject = NULL;
gs_free char *body = NULL;
OstreeRepoTransactionStats stats;
OstreeRepoCommitModifier *modifier = NULL;
context = g_option_context_new ("LOCATION DIRECTORY NAME [BRANCH] - Create a repository from a build directory");
if (!xdg_app_option_context_parse (context, options, &argc, &argv, XDG_APP_BUILTIN_FLAG_NO_DIR, NULL, cancellable, error))
goto out;
if (argc < 4)
{
usage_error (context, "LOCATION, DIRECTORY and NAME must be specified", error);
goto out;
}
location = argv[1];
directory = argv[2];
name = argv[3];
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 (argc >= 5)
branch = argv[4];
else
branch = "master";
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;
}
base = g_file_new_for_commandline_arg (directory);
files = g_file_get_child (base, "files");
metadata = g_file_get_child (base, "metadata");
export = g_file_get_child (base, "export");
if (!g_file_query_exists (files, cancellable) ||
!g_file_query_exists (metadata, 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, cancellable))
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Build directory %s not finalized", directory);
goto out;
}
if (!metadata_get_arch (metadata, &arch, error))
goto out;
if (opt_subject)
subject = opt_subject;
else
subject = "Import an application build";
if (opt_body)
body = g_strdup (opt_body);
else
body = g_strconcat ("Name: ", name, "\nArch: ", arch, "\nBranch: ", branch, NULL);
full_branch = g_strconcat ("app/", name, "/", arch, "/", branch, NULL);
repofile = g_file_new_for_commandline_arg (location);
repo = ostree_repo_new (repofile);
if (g_file_query_exists (repofile, cancellable) &&
!is_empty_directory (repofile, cancellable))
{
if (!ostree_repo_open (repo, cancellable, error))
goto out;
if (!ostree_repo_resolve_rev (repo, full_branch, TRUE, &parent, error))
goto out;
}
else
{
if (!ostree_repo_create (repo, OSTREE_REPO_MODE_ARCHIVE_Z2, cancellable, error))
goto out;
}
if (!ostree_repo_prepare_transaction (repo, NULL, cancellable, error))
goto out;
mtree = ostree_mutable_tree_new ();
arg = g_file_new_for_commandline_arg (directory);
modifier = ostree_repo_commit_modifier_new (0, commit_filter, NULL, NULL);
if (!ostree_repo_write_directory_to_mtree (repo, arg, mtree, modifier, cancellable, error))
goto out;
if (!ostree_repo_write_mtree (repo, mtree, &root, cancellable, error))
goto out;
if (!ostree_repo_write_commit (repo, parent, subject, body, NULL,
OSTREE_REPO_FILE (root),
&commit_checksum, cancellable, error))
goto out;
ostree_repo_transaction_set_ref (repo, NULL, full_branch, commit_checksum);
if (!ostree_repo_commit_transaction (repo, &stats, cancellable, error))
goto out;
g_print ("Commit: %s\n", commit_checksum);
g_print ("Metadata Total: %u\n", stats.metadata_objects_total);
g_print ("Metadata Written: %u\n", stats.metadata_objects_written);
g_print ("Content Total: %u\n", stats.content_objects_total);
g_print ("Content Written: %u\n", stats.content_objects_written);
g_print ("Content Bytes Written: %" G_GUINT64_FORMAT "\n", stats.content_bytes_written);
ret = TRUE;
out:
if (repo)
ostree_repo_abort_transaction (repo, cancellable, NULL);
if (context)
g_option_context_free (context);
if (modifier)
ostree_repo_commit_modifier_unref (modifier);
return ret;
}