flatpak-builder/app/flatpak-builtins-build-init.c

269 lines
9.5 KiB
C
Raw Normal View History

2015-03-23 11:17:58 +00:00
/*
* 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 <alexl@redhat.com>
*/
2015-01-08 15:54:45 +00:00
#include "config.h"
#include <locale.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "libgsystem.h"
2015-03-20 15:21:19 +00:00
#include "libglnx/libglnx.h"
2015-01-08 15:54:45 +00:00
2016-05-06 16:03:47 +00:00
#include "flatpak-builtins.h"
#include "flatpak-utils.h"
2015-01-08 15:54:45 +00:00
static char *opt_arch;
static char *opt_var;
2016-01-18 14:22:56 +00:00
static char *opt_sdk_dir;
2016-01-20 11:36:37 +00:00
static char **opt_sdk_extensions;
2016-02-17 13:50:34 +00:00
static char **opt_tags;
2015-12-15 18:16:43 +00:00
static gboolean opt_writable_sdk;
2016-01-18 14:22:56 +00:00
static gboolean opt_update;
2015-01-08 15:54:45 +00:00
static GOptionEntry options[] = {
{ "arch", 0, 0, G_OPTION_ARG_STRING, &opt_arch, "Arch to use", "ARCH" },
{ "var", 'v', 0, G_OPTION_ARG_STRING, &opt_var, "Initialize var from named runtime", "RUNTIME" },
2015-12-15 18:16:43 +00:00
{ "writable-sdk", 'w', 0, G_OPTION_ARG_NONE, &opt_writable_sdk, "Initialize /usr with a writable copy of the sdk", },
2016-02-17 13:50:34 +00:00
{ "tag", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_tags, "Add a tag", },
2016-01-20 11:36:37 +00:00
{ "sdk-extension", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_sdk_extensions, "include this sdk extension in /usr", "EXTENSION"},
2016-01-18 14:22:56 +00:00
{ "sdk-dir", 0, 0, G_OPTION_ARG_STRING, &opt_sdk_dir, "Where to store sdk (defaults to 'usr')", "DIR" },
{ "update", 0, 0, G_OPTION_ARG_NONE, &opt_update, "Re-initialize the sdk/var", },
2015-01-08 15:54:45 +00:00
{ NULL }
};
gboolean
flatpak_builtin_build_init (int argc, char **argv, GCancellable *cancellable, GError **error)
2015-01-08 15:54:45 +00:00
{
g_autoptr(GOptionContext) context = NULL;
2015-03-20 15:21:19 +00:00
g_autoptr(GFile) var_deploy_base = NULL;
g_autoptr(GFile) var_deploy_files = NULL;
g_autoptr(GFile) base = NULL;
g_autoptr(GFile) files_dir = NULL;
2015-12-15 18:16:43 +00:00
g_autoptr(GFile) usr_dir = NULL;
2015-03-20 15:21:19 +00:00
g_autoptr(GFile) var_dir = NULL;
g_autoptr(GFile) var_tmp_dir = NULL;
g_autoptr(GFile) var_run_dir = NULL;
g_autoptr(GFile) metadata_file = NULL;
2016-02-17 13:50:34 +00:00
g_autoptr(GString) metadata_contents = NULL;
const char *app_id;
2015-01-08 15:54:45 +00:00
const char *directory;
const char *sdk;
const char *runtime;
const char *branch = "master";
2015-03-20 15:21:19 +00:00
g_autofree char *runtime_ref = NULL;
g_autofree char *var_ref = NULL;
g_autofree char *sdk_ref = NULL;
2016-01-20 11:36:37 +00:00
int i;
2015-01-08 15:54:45 +00:00
context = g_option_context_new ("DIRECTORY APPNAME SDK RUNTIME [BRANCH] - Initialize a directory for building");
2015-01-08 15:54:45 +00:00
if (!flatpak_option_context_parse (context, options, &argc, &argv, FLATPAK_BUILTIN_FLAG_NO_DIR, NULL, cancellable, error))
return FALSE;
2015-01-08 15:54:45 +00:00
if (argc < 5)
return usage_error (context, "RUNTIME must be specified", error);
2015-01-08 15:54:45 +00:00
directory = argv[1];
app_id = argv[2];
sdk = argv[3];
runtime = argv[4];
if (argc >= 6)
branch = argv[5];
if (!flatpak_is_valid_name (app_id))
return flatpak_fail (error, "'%s' is not a valid application name", app_id);
2015-01-08 15:54:45 +00:00
if (!flatpak_is_valid_name (runtime))
return flatpak_fail (error, "'%s' is not a valid runtime name", runtime);
if (!flatpak_is_valid_name (sdk))
return flatpak_fail (error, "'%s' is not a valid sdk name", sdk);
if (!flatpak_is_valid_branch (branch))
return flatpak_fail (error, "'%s' is not a valid branch name", branch);
runtime_ref = flatpak_build_untyped_ref (runtime, branch, opt_arch);
sdk_ref = flatpak_build_untyped_ref (sdk, branch, opt_arch);
2015-01-08 15:54:45 +00:00
base = g_file_new_for_commandline_arg (directory);
if (!gs_file_ensure_directory (base, TRUE, cancellable, error))
return FALSE;
2015-01-08 15:54:45 +00:00
files_dir = g_file_get_child (base, "files");
2016-01-18 14:22:56 +00:00
if (opt_sdk_dir)
usr_dir = g_file_get_child (base, opt_sdk_dir);
else
usr_dir = g_file_get_child (base, "usr");
2015-01-08 15:54:45 +00:00
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");
2016-01-18 14:22:56 +00:00
if (!opt_update &&
g_file_query_exists (files_dir, cancellable))
return flatpak_fail (error, "Build directory %s already initialized", directory);
2015-01-08 15:54:45 +00:00
2015-12-15 18:16:43 +00:00
if (opt_writable_sdk)
{
g_autofree char *full_sdk_ref = g_strconcat ("runtime/", sdk_ref, NULL);
2016-01-18 14:22:56 +00:00
g_autoptr(GError) my_error = NULL;
2016-01-20 11:36:37 +00:00
g_autoptr(GFile) sdk_deploy_files = NULL;
g_autoptr(FlatpakDeploy) sdk_deploy = NULL;
2015-12-15 18:16:43 +00:00
sdk_deploy = flatpak_find_deploy_for_ref (full_sdk_ref, cancellable, error);
2016-01-20 11:36:37 +00:00
if (sdk_deploy == NULL)
2015-12-15 18:16:43 +00:00
return FALSE;
2016-01-18 14:22:56 +00:00
if (!gs_shutil_rm_rf (usr_dir, NULL, &my_error))
{
if (!g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
{
2016-02-08 15:16:38 +00:00
g_propagate_error (error, g_steal_pointer (&my_error));
2016-01-18 14:22:56 +00:00
return FALSE;
}
g_clear_error (&my_error);
}
sdk_deploy_files = flatpak_deploy_get_files (sdk_deploy);
if (!flatpak_cp_a (sdk_deploy_files, usr_dir, FLATPAK_CP_FLAGS_NO_CHOWN, cancellable, error))
2015-12-15 18:16:43 +00:00
return FALSE;
2016-01-20 11:36:37 +00:00
if (opt_sdk_extensions)
{
g_autoptr(GKeyFile) metakey = flatpak_deploy_get_metadata (sdk_deploy);
2016-01-20 11:36:37 +00:00
GList *extensions = NULL, *l;
/* We leak this on failure, as we have no autoptr for deep lists.. */
extensions = flatpak_list_extensions (metakey,
2016-01-20 11:36:37 +00:00
opt_arch,
branch);
for (i = 0; opt_sdk_extensions[i] != NULL; i++)
{
const char *requested_extension = opt_sdk_extensions[i];
gboolean found = FALSE;
for (l = extensions; l != NULL; l = l->next)
{
FlatpakExtension *ext = l->data;
2016-01-20 11:36:37 +00:00
if (strcmp (ext->installed_id, requested_extension) == 0 ||
strcmp (ext->id, requested_extension) == 0)
{
g_autoptr(GFile) ext_deploy_dir = flatpak_find_deploy_dir_for_ref (ext->ref, cancellable, NULL);
2016-01-20 11:36:37 +00:00
if (ext_deploy_dir != NULL)
{
g_autoptr(GFile) ext_deploy_files = g_file_get_child (ext_deploy_dir, "files");
g_autoptr(GFile) target = g_file_resolve_relative_path (usr_dir, ext->directory);
g_autoptr(GFile) target_parent = g_file_get_parent (target);
if (!gs_file_ensure_directory (target_parent, TRUE, cancellable, error))
return FALSE;
/* An extension overrides whatever is there before, so we clean up first */
if (!gs_shutil_rm_rf (target, cancellable, error))
return FALSE;
if (!flatpak_cp_a (ext_deploy_files, target, FLATPAK_CP_FLAGS_NO_CHOWN, cancellable, error))
2016-01-20 11:36:37 +00:00
return FALSE;
found = TRUE;
2016-01-20 11:36:37 +00:00
}
else
{
g_list_free_full (extensions, (GDestroyNotify) flatpak_extension_free);
return flatpak_fail (error, "Requested extension %s not installed\n", requested_extension);
2016-01-20 11:36:37 +00:00
}
}
}
if (!found)
return flatpak_fail (error, "No extension %s in sdk\n", requested_extension);
2016-01-20 11:36:37 +00:00
}
g_list_free_full (extensions, (GDestroyNotify) flatpak_extension_free);
2016-01-20 11:36:37 +00:00
}
2015-12-15 18:16:43 +00:00
}
2015-01-08 15:54:45 +00:00
if (opt_var)
{
var_ref = flatpak_build_runtime_ref (opt_var, branch, opt_arch);
2015-01-08 15:54:45 +00:00
var_deploy_base = flatpak_find_deploy_dir_for_ref (var_ref, cancellable, error);
2015-01-08 15:54:45 +00:00
if (var_deploy_base == NULL)
return FALSE;
2015-01-08 15:54:45 +00:00
var_deploy_files = g_file_get_child (var_deploy_base, "files");
}
2016-01-18 14:22:56 +00:00
if (opt_update)
return TRUE;
2015-01-08 15:54:45 +00:00
if (!g_file_make_directory (files_dir, cancellable, error))
return FALSE;
2015-01-08 15:54:45 +00:00
if (var_deploy_files)
{
if (!gs_shutil_cp_a (var_deploy_files, var_dir, cancellable, error))
return FALSE;
2015-01-08 15:54:45 +00:00
}
else
{
if (!g_file_make_directory (var_dir, cancellable, error))
return FALSE;
2015-01-08 15:54:45 +00:00
}
if (!gs_file_ensure_directory (var_tmp_dir, FALSE, cancellable, error))
return FALSE;
2015-01-08 15:54:45 +00:00
if (!g_file_query_exists (var_run_dir, cancellable) &&
!g_file_make_symbolic_link (var_run_dir, "/run", cancellable, error))
return FALSE;
2015-01-08 15:54:45 +00:00
2016-02-17 13:50:34 +00:00
metadata_contents = g_string_new ("[Application]\n");
g_string_append_printf (metadata_contents,
"name=%s\n"
"runtime=%s\n"
"sdk=%s\n",
app_id, runtime_ref, sdk_ref);
2016-02-17 13:50:34 +00:00
if (opt_tags != NULL)
{
g_string_append (metadata_contents, "tags=");
for (i = 0; opt_tags[i] != NULL; i++)
{
g_string_append (metadata_contents, opt_tags[i]);
g_string_append_c (metadata_contents, ';');
}
g_string_append_c (metadata_contents, '\n');
}
2015-01-08 15:54:45 +00:00
if (!g_file_replace_contents (metadata_file,
2016-02-17 13:50:34 +00:00
metadata_contents->str, metadata_contents->len, NULL, FALSE,
2015-01-08 15:54:45 +00:00
G_FILE_CREATE_REPLACE_DESTINATION,
NULL, cancellable, error))
return FALSE;
2015-01-08 15:54:45 +00:00
return TRUE;
2015-01-08 15:54:45 +00:00
}