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

utils: Refactoring #5416

Merged
merged 8 commits into from
Jul 3, 2023
1 change: 1 addition & 0 deletions app/flatpak-builtins-install.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "flatpak-transaction-private.h"
#include "flatpak-cli-transaction.h"
#include "flatpak-quiet-transaction.h"
#include "flatpak-utils-http-private.h"
#include "flatpak-utils-private.h"
#include "flatpak-error.h"
#include "flatpak-chain-input-stream-private.h"
Expand Down
1 change: 1 addition & 0 deletions app/flatpak-builtins-remote-add.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "flatpak-builtins.h"
#include "flatpak-builtins-utils.h"
#include "flatpak-utils-http-private.h"
#include "flatpak-utils-private.h"

static gboolean opt_no_gpg_verify;
Expand Down
1 change: 1 addition & 0 deletions common/Makefile.am.inc
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ libflatpak_common_la_SOURCES = \
common/flatpak-json-oci.c \
common/flatpak-json-private.h \
common/flatpak-json.c \
common/flatpak-metadata-private.h \
common/flatpak-oci-registry-private.h \
common/flatpak-oci-registry.c \
common/flatpak-portal-error.c \
Expand Down
3 changes: 3 additions & 0 deletions common/flatpak-context-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,7 @@ gboolean flatpak_context_parse_env_fd (FlatpakContext *context,

G_DEFINE_AUTOPTR_CLEANUP_FUNC (FlatpakContext, flatpak_context_free)

GFile *flatpak_get_user_base_dir_location (void);
GFile *flatpak_get_data_dir (const char *app_id);

#endif /* __FLATPAK_CONTEXT_H__ */
49 changes: 44 additions & 5 deletions common/flatpak-context.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

#include "config.h"
#include "flatpak-context-private.h"

#include <string.h>
#include <fcntl.h>
Expand All @@ -37,11 +38,9 @@
#include <gio/gio.h>
#include "libglnx.h"

#include "flatpak-run-private.h"
#include "flatpak-utils-private.h"
#include "flatpak-dir-private.h"
#include "flatpak-systemd-dbus-generated.h"
#include "flatpak-error.h"
#include "flatpak-metadata-private.h"
#include "flatpak-utils-private.h"

/* Same order as enum */
const char *flatpak_context_shares[] = {
Expand Down Expand Up @@ -2759,6 +2758,15 @@ flatpak_context_export (FlatpakContext *context,
}
}

GFile *
flatpak_get_data_dir (const char *app_id)
{
g_autoptr(GFile) home = g_file_new_for_path (g_get_home_dir ());
g_autoptr(GFile) var_app = g_file_resolve_relative_path (home, ".var/app");

return g_file_get_child (var_app, app_id);
}

FlatpakExports *
flatpak_context_get_exports (FlatpakContext *context,
const char *app_id)
Expand Down Expand Up @@ -2834,6 +2842,37 @@ flatpak_context_get_exports_full (FlatpakContext *context,
return g_steal_pointer (&exports);
}

static void
flatpak_context_apply_env_appid (FlatpakBwrap *bwrap,
GFile *app_dir)
{
g_autoptr(GFile) app_dir_data = NULL;
g_autoptr(GFile) app_dir_config = NULL;
g_autoptr(GFile) app_dir_cache = NULL;
g_autoptr(GFile) app_dir_state = NULL;

app_dir_data = g_file_get_child (app_dir, "data");
app_dir_config = g_file_get_child (app_dir, "config");
app_dir_cache = g_file_get_child (app_dir, "cache");
/* Yes, this is inconsistent with data, config and cache. However, using
* this path lets apps provide backwards-compatibility with older Flatpak
* versions by using `--persist=.local/state --unset-env=XDG_STATE_DIR`. */
app_dir_state = g_file_get_child (app_dir, ".local/state");
flatpak_bwrap_set_env (bwrap, "XDG_DATA_HOME", flatpak_file_get_path_cached (app_dir_data), TRUE);
flatpak_bwrap_set_env (bwrap, "XDG_CONFIG_HOME", flatpak_file_get_path_cached (app_dir_config), TRUE);
flatpak_bwrap_set_env (bwrap, "XDG_CACHE_HOME", flatpak_file_get_path_cached (app_dir_cache), TRUE);
flatpak_bwrap_set_env (bwrap, "XDG_STATE_HOME", flatpak_file_get_path_cached (app_dir_state), TRUE);

if (g_getenv ("XDG_DATA_HOME"))
flatpak_bwrap_set_env (bwrap, "HOST_XDG_DATA_HOME", g_getenv ("XDG_DATA_HOME"), TRUE);
if (g_getenv ("XDG_CONFIG_HOME"))
flatpak_bwrap_set_env (bwrap, "HOST_XDG_CONFIG_HOME", g_getenv ("XDG_CONFIG_HOME"), TRUE);
if (g_getenv ("XDG_CACHE_HOME"))
flatpak_bwrap_set_env (bwrap, "HOST_XDG_CACHE_HOME", g_getenv ("XDG_CACHE_HOME"), TRUE);
if (g_getenv ("XDG_STATE_HOME"))
flatpak_bwrap_set_env (bwrap, "HOST_XDG_STATE_HOME", g_getenv ("XDG_STATE_HOME"), TRUE);
}

void
flatpak_context_append_bwrap_filesystem (FlatpakContext *context,
FlatpakBwrap *bwrap,
Expand All @@ -2847,7 +2886,7 @@ flatpak_context_append_bwrap_filesystem (FlatpakContext *context,
gpointer key, value;

if (app_id_dir != NULL)
flatpak_run_apply_env_appid (bwrap, app_id_dir);
flatpak_context_apply_env_appid (bwrap, app_id_dir);

if (!home_access)
{
Expand Down
1 change: 0 additions & 1 deletion common/flatpak-dir-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,6 @@ GQuark flatpak_dir_error_quark (void);
GPtrArray *flatpak_get_system_base_dir_locations (GCancellable *cancellable,
GError **error);
GFile * flatpak_get_system_default_base_dir_location (void);
GFile * flatpak_get_user_base_dir_location (void);

GKeyFile * flatpak_load_override_keyfile (const char *app_id,
gboolean user,
Expand Down
2 changes: 2 additions & 0 deletions common/flatpak-exports-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,6 @@ void flatpak_exports_take_host_fd (FlatpakExports *exports,
void flatpak_exports_set_test_flags (FlatpakExports *exports,
FlatpakExportsTestFlags flags);

extern const char * const *flatpak_abs_usrmerged_dirs;

#endif /* __FLATPAK_EXPORTS_H__ */
14 changes: 13 additions & 1 deletion common/flatpak-exports.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,24 @@
#include "libglnx.h"

#include "flatpak-exports-private.h"
#include "flatpak-run-private.h"
#include "flatpak-metadata-private.h"
#include "flatpak-utils-base-private.h"
#include "flatpak-utils-private.h"
#include "flatpak-dir-private.h"
#include "flatpak-systemd-dbus-generated.h"
#include "flatpak-error.h"

static const char * const abs_usrmerged_dirs[] =
{
"/bin",
"/lib",
"/lib32",
"/lib64",
"/sbin",
NULL
};
const char * const *flatpak_abs_usrmerged_dirs = abs_usrmerged_dirs;

/* We don't want to export paths pointing into these, because they are readonly
(so we can't create mountpoints there) and don't match what's on the host anyway.
flatpak_abs_usrmerged_dirs get the same treatment without having to be listed
Expand Down
2 changes: 1 addition & 1 deletion common/flatpak-instance.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
#include <unistd.h>

#include "flatpak-json-backports-private.h"
#include "flatpak-metadata-private.h"
#include "flatpak-utils-base-private.h"
#include "flatpak-utils-private.h"
#include "flatpak-run-private.h"
#include "flatpak-instance.h"
#include "flatpak-instance-private.h"
#include "flatpak-enum-types.h"
Expand Down
107 changes: 107 additions & 0 deletions common/flatpak-metadata-private.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright 2014 Red Hat, Inc
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* 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.1 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:https://www.gnu.org/licenses/>.
*/

#pragma once

#include "libglnx.h"

#include <glib.h>

G_BEGIN_DECLS

/* See flatpak-metadata(5) */

#define FLATPAK_METADATA_GROUP_APPLICATION "Application"
#define FLATPAK_METADATA_GROUP_RUNTIME "Runtime"
#define FLATPAK_METADATA_KEY_COMMAND "command"
#define FLATPAK_METADATA_KEY_NAME "name"
#define FLATPAK_METADATA_KEY_REQUIRED_FLATPAK "required-flatpak"
#define FLATPAK_METADATA_KEY_RUNTIME "runtime"
#define FLATPAK_METADATA_KEY_SDK "sdk"
#define FLATPAK_METADATA_KEY_TAGS "tags"

#define FLATPAK_METADATA_GROUP_CONTEXT "Context"
#define FLATPAK_METADATA_KEY_SHARED "shared"
#define FLATPAK_METADATA_KEY_SOCKETS "sockets"
#define FLATPAK_METADATA_KEY_FILESYSTEMS "filesystems"
#define FLATPAK_METADATA_KEY_PERSISTENT "persistent"
#define FLATPAK_METADATA_KEY_DEVICES "devices"
#define FLATPAK_METADATA_KEY_FEATURES "features"
#define FLATPAK_METADATA_KEY_UNSET_ENVIRONMENT "unset-environment"

#define FLATPAK_METADATA_GROUP_INSTANCE "Instance"
#define FLATPAK_METADATA_KEY_INSTANCE_PATH "instance-path"
#define FLATPAK_METADATA_KEY_INSTANCE_ID "instance-id"
#define FLATPAK_METADATA_KEY_ORIGINAL_APP_PATH "original-app-path"
#define FLATPAK_METADATA_KEY_APP_PATH "app-path"
#define FLATPAK_METADATA_KEY_APP_COMMIT "app-commit"
#define FLATPAK_METADATA_KEY_APP_EXTENSIONS "app-extensions"
#define FLATPAK_METADATA_KEY_ARCH "arch"
#define FLATPAK_METADATA_KEY_BRANCH "branch"
#define FLATPAK_METADATA_KEY_FLATPAK_VERSION "flatpak-version"
#define FLATPAK_METADATA_KEY_ORIGINAL_RUNTIME_PATH "original-runtime-path"
#define FLATPAK_METADATA_KEY_RUNTIME_PATH "runtime-path"
#define FLATPAK_METADATA_KEY_RUNTIME_COMMIT "runtime-commit"
#define FLATPAK_METADATA_KEY_RUNTIME_EXTENSIONS "runtime-extensions"
#define FLATPAK_METADATA_KEY_SESSION_BUS_PROXY "session-bus-proxy"
#define FLATPAK_METADATA_KEY_SYSTEM_BUS_PROXY "system-bus-proxy"
#define FLATPAK_METADATA_KEY_EXTRA_ARGS "extra-args"
#define FLATPAK_METADATA_KEY_SANDBOX "sandbox"
#define FLATPAK_METADATA_KEY_BUILD "build"
#define FLATPAK_METADATA_KEY_DEVEL "devel"

#define FLATPAK_METADATA_GROUP_SESSION_BUS_POLICY "Session Bus Policy"
#define FLATPAK_METADATA_GROUP_SYSTEM_BUS_POLICY "System Bus Policy"
#define FLATPAK_METADATA_GROUP_PREFIX_POLICY "Policy "
#define FLATPAK_METADATA_GROUP_ENVIRONMENT "Environment"

#define FLATPAK_METADATA_GROUP_PREFIX_EXTENSION "Extension "
#define FLATPAK_METADATA_KEY_ADD_LD_PATH "add-ld-path"
#define FLATPAK_METADATA_KEY_AUTODELETE "autodelete"
#define FLATPAK_METADATA_KEY_DIRECTORY "directory"
#define FLATPAK_METADATA_KEY_DOWNLOAD_IF "download-if"
#define FLATPAK_METADATA_KEY_ENABLE_IF "enable-if"
#define FLATPAK_METADATA_KEY_AUTOPRUNE_UNLESS "autoprune-unless"
#define FLATPAK_METADATA_KEY_MERGE_DIRS "merge-dirs"
#define FLATPAK_METADATA_KEY_NO_AUTODOWNLOAD "no-autodownload"
#define FLATPAK_METADATA_KEY_SUBDIRECTORIES "subdirectories"
#define FLATPAK_METADATA_KEY_SUBDIRECTORY_SUFFIX "subdirectory-suffix"
#define FLATPAK_METADATA_KEY_LOCALE_SUBSET "locale-subset"
#define FLATPAK_METADATA_KEY_VERSION "version"
#define FLATPAK_METADATA_KEY_VERSIONS "versions"

#define FLATPAK_METADATA_KEY_COLLECTION_ID "collection-id"

#define FLATPAK_METADATA_GROUP_EXTRA_DATA "Extra Data"
#define FLATPAK_METADATA_KEY_EXTRA_DATA_CHECKSUM "checksum"
#define FLATPAK_METADATA_KEY_EXTRA_DATA_INSTALLED_SIZE "installed-size"
#define FLATPAK_METADATA_KEY_EXTRA_DATA_NAME "name"
#define FLATPAK_METADATA_KEY_EXTRA_DATA_SIZE "size"
#define FLATPAK_METADATA_KEY_EXTRA_DATA_URI "uri"
#define FLATPAK_METADATA_KEY_NO_RUNTIME "NoRuntime"

#define FLATPAK_METADATA_GROUP_EXTENSION_OF "ExtensionOf"
#define FLATPAK_METADATA_KEY_PRIORITY "priority"
#define FLATPAK_METADATA_KEY_REF "ref"
#define FLATPAK_METADATA_KEY_TAG "tag"

#define FLATPAK_METADATA_GROUP_DCONF "X-DConf"
#define FLATPAK_METADATA_KEY_DCONF_PATHS "paths"
#define FLATPAK_METADATA_KEY_DCONF_MIGRATE_PATH "migrate-path"

G_END_DECLS
1 change: 1 addition & 0 deletions common/flatpak-oci-registry-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <gio/gio.h>
#include <archive.h>
#include "flatpak-json-oci-private.h"
#include "flatpak-utils-http-private.h"
#include "flatpak-utils-private.h"

#define FLATPAK_TYPE_OCI_REGISTRY flatpak_oci_registry_get_type ()
Expand Down
1 change: 0 additions & 1 deletion common/flatpak-ref-utils-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ gboolean flatpak_id_has_subref_suffix (const char *id,

gboolean flatpak_is_app_runtime_or_appstream_ref (const char *ref);
char * flatpak_get_arch_for_ref (const char *ref);
const char *flatpak_get_compat_arch_reverse (const char *compat_arch);

FlatpakKinds flatpak_kinds_from_kind (FlatpakRefKind kind);

Expand Down
12 changes: 11 additions & 1 deletion common/flatpak-ref-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@

#include <glib.h>
#include "flatpak-ref-utils-private.h"
#include "flatpak-run-private.h"

#include "flatpak-error.h"
#include "flatpak-metadata-private.h"
#include "flatpak-utils-private.h"

FlatpakKinds
Expand Down Expand Up @@ -1687,3 +1688,12 @@ flatpak_build_app_ref (const char *app,
return g_build_filename ("app", app, arch, branch, NULL);
}

gboolean
flatpak_is_app_runtime_or_appstream_ref (const char *ref)
{
return
g_str_has_prefix (ref, "appstream/") ||
g_str_has_prefix (ref, "appstream2/") ||
g_str_has_prefix (ref, "app/") ||
g_str_has_prefix (ref, "runtime/");
}