Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to export multiple commands #5404

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Next Next commit
Add a way to export multiple commands
Add and --export-command=COMMAND option to the
build-finish command, export it under the exported-commands
key in the metadata file, and generate
APPID-command shell wrappers for each of them.
  • Loading branch information
Matthias Clasen committed May 2, 2023
commit 501a797287a966465e31ca3718a5514e45338352
20 changes: 20 additions & 0 deletions app/flatpak-builtins-build-finish.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ static gboolean opt_no_inherit_permissions;
static int opt_extension_prio = G_MININT;
static char *opt_sdk;
static char *opt_runtime;
static char **opt_export_commands;

static GOptionEntry options[] = {
{ "command", 0, 0, G_OPTION_ARG_STRING, &opt_command, N_("Command to set"), N_("COMMAND") },
Expand All @@ -59,6 +60,7 @@ static GOptionEntry options[] = {
{ "runtime", 0, 0, G_OPTION_ARG_STRING, &opt_runtime, N_("Change the runtime used for the app"), N_("RUNTIME") },
{ "metadata", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_metadata, N_("Set generic metadata option"), N_("GROUP=KEY[=VALUE]") },
{ "no-inherit-permissions", 0, 0, G_OPTION_ARG_NONE, &opt_no_inherit_permissions, N_("Don't inherit permissions from runtime"), NULL },
{ "export-command", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_export_commands, N_("Command to export (can be repeated)"), N_("COMMAND") },
{ NULL }
};

Expand Down Expand Up @@ -454,6 +456,24 @@ update_metadata (GFile *base, FlatpakContext *arg_context, gboolean is_runtime,
}
}

if (g_key_file_has_key (keyfile, group, FLATPAK_METADATA_KEY_EXPORT_COMMANDS, NULL))
{
g_info ("Export-commands key is present");

if (opt_export_commands)
g_key_file_set_string_list (keyfile, group, FLATPAK_METADATA_KEY_EXPORT_COMMANDS,
(const char * const *)opt_export_commands, g_strv_length (opt_export_commands));
}
else if (opt_export_commands)
{
char *cmds = g_strjoinv (" ", opt_export_commands);
g_info ("Using explicitly provided export-commands %s", cmds);
g_free (cmds);

g_key_file_set_string_list (keyfile, group, FLATPAK_METADATA_KEY_EXPORT_COMMANDS,
(const char * const *)opt_export_commands, g_strv_length (opt_export_commands));
}

/* Inherit permissions from runtime by default */
if (!opt_no_inherit_permissions)
{
Expand Down
37 changes: 37 additions & 0 deletions common/flatpak-dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -8952,6 +8952,43 @@ flatpak_dir_deploy (FlatpakDir *self,
while (G_UNLIKELY (r == -1 && errno == EINTR));
if (r == -1)
return glnx_throw_errno_prefix (error, "fchmodat");

if (g_key_file_has_key (keyfile, FLATPAK_METADATA_GROUP_APPLICATION, FLATPAK_METADATA_KEY_EXPORT_COMMANDS, NULL))
{
g_auto(GStrv) commands = NULL;

commands = g_key_file_get_string_list (keyfile,
FLATPAK_METADATA_GROUP_APPLICATION,
FLATPAK_METADATA_KEY_EXPORT_COMMANDS,
NULL,
NULL);

for (unsigned int i = 0; commands && commands[i]; i++)
{
g_autofree char *filename = NULL;

g_set_object (&wrapper, NULL);
g_set_str (&escaped_app, NULL);
g_set_str (&bin_data, NULL);

filename = g_strconcat (ref_id, "-", commands[i], NULL);
matthiasclasen marked this conversation as resolved.
Show resolved Hide resolved
wrapper = g_file_get_child (bindir, filename);
escaped_app = maybe_quote (commands[i]);

bin_data = g_strdup_printf ("#!/bin/sh\nexec %s run --branch=%s --arch=%s %s \"$@\"\n",
matthiasclasen marked this conversation as resolved.
Show resolved Hide resolved
flatpak, escaped_branch, escaped_arch, escaped_app);

if (!g_file_replace_contents (wrapper, bin_data, strlen (bin_data), NULL, FALSE,
G_FILE_CREATE_REPLACE_DESTINATION, NULL, cancellable, error))
return FALSE;

do
r = fchmodat (AT_FDCWD, flatpak_file_get_path_cached (wrapper), 0755, 0);
while (G_UNLIKELY (r == -1 && errno == EINTR));
if (r == -1)
return glnx_throw_errno_prefix (error, "fchmodat");
}
}
}

deploy_data = flatpak_dir_new_deploy_data (self,
Expand Down
1 change: 1 addition & 0 deletions common/flatpak-run-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ gboolean flatpak_run_in_transient_unit (const char *app_id,
#define FLATPAK_METADATA_KEY_RUNTIME "runtime"
#define FLATPAK_METADATA_KEY_SDK "sdk"
#define FLATPAK_METADATA_KEY_TAGS "tags"
#define FLATPAK_METADATA_KEY_EXPORT_COMMANDS "export-commands"

#define FLATPAK_METADATA_GROUP_CONTEXT "Context"
#define FLATPAK_METADATA_KEY_SHARED "shared"
Expand Down