Skip to content

Commit 39e750b

Browse files
committed
builder-main: Commit mirrored media as screenshot ref to exported repo
This is needed to store the application icons and the mirrored screenshots (as done by appstream) to the ostree repo. This can be useful in many ways, the primary being flat-manager extracts [1] these screenshot refs and stores them in the server so that application store frontends can use their own mirrored screenshot and icon URLs. This has been for the longest time done at various downstream levels such as in buildbot [2], flatpak-github-actions [3] and vorarbeiter [4] (the successor to buildbot) but it's more user friendly to have flatpak-builder do this extra work. [1]: flatpak/flat-manager@eff4300 [2]: https://github.com/flathub-infra/buildbot/blob/9d8e2b5483b2e446389548446ebf9c3934380682/master/buildbot/flathub_master.py#L1081 [3]: https://github.com/flatpak/flatpak-github-actions/blob/6684584b07d86113b1acfc80b3e4667f16f617c3/flatpak-builder/index.js#L280-L286 [4]: https://github.com/flathub-infra/vorarbeiter/blob/7f2f04a562e73ba4f46044dae7c99f8e9e64a39d/justfile#L185-L189
1 parent cf2b785 commit 39e750b

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

doc/flatpak-builder.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,9 @@
528528
in the appstream xml to the specified URL. The resulting files
529529
will be stored in the "screenshots" subdirectory in the app directory
530530
for versions earlier than 1.3.4 and "files/share/app-info/media"
531-
subdirectory for newer versions.
531+
subdirectory for newer versions. Since version 1.4.5 this
532+
will also create a screenshot ref in the exported OSTree
533+
repo for each architecture containing the mirrored media.
532534
</para></listitem>
533535
</varlistentry>
534536

src/builder-main.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include <glib/gi18n.h>
2929
#include <gio/gio.h>
30+
#include <ostree.h>
3031
#include "libglnx.h"
3132

3233
#include "builder-flatpak-utils.h"
@@ -263,6 +264,80 @@ do_export (BuilderContext *build_context,
263264
(const gchar * const *) args->pdata, NULL);
264265
}
265266

267+
static gboolean
268+
commit_screenshot_ref (const char *repo_path,
269+
const char *app_dir_path,
270+
const char *arch,
271+
GError **error)
272+
{
273+
g_autofree char *media_dir_path = NULL;
274+
g_autoptr(GFile) media_dir = NULL;
275+
g_autoptr(GFile) repo_file = NULL;
276+
g_autoptr(OstreeRepo) repo = NULL;
277+
g_autoptr(OstreeMutableTree) tree = NULL;
278+
g_autoptr(GFile) tree_root = NULL;
279+
g_autofree char *commit_checksum = NULL;
280+
g_autofree char *ref_name = NULL;
281+
g_autoptr(OstreeRepoCommitModifier) modifier = NULL;
282+
gboolean ret = FALSE;
283+
284+
media_dir_path = g_build_filename (app_dir_path, "files/share/app-info/media", NULL);
285+
media_dir = g_file_new_for_path (media_dir_path);
286+
287+
if (!g_file_query_exists (media_dir, NULL)) {
288+
g_print ("Media directory does not exist, skipping commit\n");
289+
return TRUE;
290+
}
291+
292+
ref_name = g_strdup_printf ("screenshots/%s", arch);
293+
294+
repo_file = g_file_new_for_path (repo_path);
295+
repo = ostree_repo_new (repo_file);
296+
if (!ostree_repo_open (repo, NULL, error)) {
297+
return FALSE;
298+
}
299+
300+
modifier = ostree_repo_commit_modifier_new (OSTREE_REPO_COMMIT_MODIFIER_FLAGS_CANONICAL_PERMISSIONS,
301+
NULL, NULL, NULL);
302+
if (!ostree_repo_prepare_transaction (repo, NULL, NULL, error)) {
303+
return FALSE;
304+
}
305+
306+
tree = ostree_mutable_tree_new ();
307+
308+
if (!ostree_repo_write_directory_to_mtree (repo, media_dir, tree, modifier, NULL, error))
309+
goto out;
310+
311+
if (!ostree_repo_write_mtree (repo, tree, &tree_root, NULL, error))
312+
goto out;
313+
314+
if (!ostree_repo_write_commit (repo,
315+
NULL,
316+
NULL,
317+
NULL,
318+
NULL,
319+
OSTREE_REPO_FILE (tree_root),
320+
&commit_checksum,
321+
NULL, error))
322+
goto out;
323+
324+
ostree_repo_transaction_set_ref (repo, NULL, ref_name, commit_checksum);
325+
326+
if (!ostree_repo_commit_transaction (repo, NULL, NULL, error))
327+
goto out;
328+
329+
g_print ("Committed screenshot ref: %s (%s)\n", ref_name, commit_checksum);
330+
ret = TRUE;
331+
332+
out:
333+
if (!ret && repo) {
334+
if (!ostree_repo_abort_transaction (repo, NULL, NULL))
335+
g_warning ("Failed to abort ostree transaction");
336+
}
337+
338+
return ret;
339+
}
340+
266341
static gboolean
267342
do_install (BuilderContext *build_context,
268343
const gchar *repodir,
@@ -974,6 +1049,17 @@ main (int argc,
9741049
return 1;
9751050
}
9761051

1052+
if (opt_mirror_screenshots_url) {
1053+
if (!commit_screenshot_ref (flatpak_file_get_path_cached (export_repo),
1054+
app_dir_path,
1055+
builder_context_get_arch (build_context),
1056+
&error)) {
1057+
1058+
g_printerr ("Failed to commit screenshot ref: %s\n", error->message);
1059+
return 1;
1060+
}
1061+
}
1062+
9771063
/* Export regular locale extensions */
9781064
dir_enum = g_file_enumerate_children (app_dir, "standard::name,standard::type",
9791065
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,

0 commit comments

Comments
 (0)