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
|
2016-07-29 18:27:49 +00:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2015-03-23 11:17:58 +00:00
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
*/
|
|
|
|
|
2016-05-06 14:37:47 +00:00
|
|
|
#ifndef __FLATPAK_UTILS_H__
|
|
|
|
#define __FLATPAK_UTILS_H__
|
2014-12-18 09:14:46 +00:00
|
|
|
|
2015-03-23 10:36:48 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "libglnx/libglnx.h"
|
2015-01-08 16:08:59 +00:00
|
|
|
#include <gio/gio.h>
|
2016-09-06 07:26:02 +00:00
|
|
|
#include <gio/gunixfdlist.h>
|
2015-03-20 15:21:19 +00:00
|
|
|
#include <libsoup/soup.h>
|
2015-02-09 14:21:23 +00:00
|
|
|
#include <ostree.h>
|
2016-11-29 09:14:33 +00:00
|
|
|
#include <json-glib/json-glib.h>
|
2018-05-03 07:28:27 +00:00
|
|
|
#include <curl/curl.h>
|
2015-01-08 16:08:59 +00:00
|
|
|
|
2016-09-01 18:53:45 +00:00
|
|
|
typedef enum {
|
|
|
|
FLATPAK_HOST_COMMAND_FLAGS_CLEAR_ENV = 1 << 0,
|
2019-04-19 04:52:49 +00:00
|
|
|
FLATPAK_HOST_COMMAND_FLAGS_WATCH_BUS = 1 << 1,
|
2016-09-01 18:53:45 +00:00
|
|
|
} FlatpakHostCommandFlags;
|
|
|
|
|
2017-04-24 19:23:46 +00:00
|
|
|
typedef void (*FlatpakLoadUriProgress) (guint64 downloaded_bytes,
|
|
|
|
gpointer user_data);
|
2016-09-01 18:53:45 +00:00
|
|
|
|
2017-05-03 09:17:41 +00:00
|
|
|
|
2017-08-24 13:48:55 +00:00
|
|
|
#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_MERGE_DIRS "merge-dirs"
|
|
|
|
#define FLATPAK_METADATA_KEY_NO_AUTODOWNLOAD "no-autodownload"
|
2017-09-21 15:05:12 +00:00
|
|
|
#define FLATPAK_METADATA_KEY_LOCALE_SUBSET "locale-subset"
|
2017-08-24 13:48:55 +00:00
|
|
|
#define FLATPAK_METADATA_KEY_SUBDIRECTORIES "subdirectories"
|
|
|
|
#define FLATPAK_METADATA_KEY_SUBDIRECTORY_SUFFIX "subdirectory-suffix"
|
|
|
|
#define FLATPAK_METADATA_KEY_VERSION "version"
|
|
|
|
#define FLATPAK_METADATA_KEY_VERSIONS "versions"
|
|
|
|
|
|
|
|
#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"
|
2016-12-12 11:23:13 +00:00
|
|
|
|
2018-04-19 11:22:18 +00:00
|
|
|
#define FLATPAK_METADATA_GROUP_BUILD "Build"
|
|
|
|
#define FLATPAK_METADATA_KEY_BUILD_EXTENSIONS "built-extensions"
|
|
|
|
|
2017-03-22 18:35:10 +00:00
|
|
|
/* https://github.com/GNOME/libglnx/pull/38
|
|
|
|
* Note by using #define rather than wrapping via a static inline, we
|
|
|
|
* don't have to re-define attributes like G_GNUC_PRINTF.
|
|
|
|
*/
|
|
|
|
#define flatpak_fail glnx_throw
|
2015-09-25 15:04:32 +00:00
|
|
|
|
2016-05-06 14:37:47 +00:00
|
|
|
const char * flatpak_path_match_prefix (const char *pattern,
|
2016-01-11 12:56:07 +00:00
|
|
|
const char *path);
|
|
|
|
|
2016-09-02 12:23:19 +00:00
|
|
|
gboolean flatpak_is_in_sandbox (void);
|
|
|
|
|
2016-05-06 14:37:47 +00:00
|
|
|
const char * flatpak_get_arch (void);
|
2016-06-30 09:41:12 +00:00
|
|
|
|
2017-02-09 11:08:12 +00:00
|
|
|
GFile *flatpak_file_new_tmp_in (GFile *dir,
|
|
|
|
const char *templatename,
|
|
|
|
GError **error);
|
2017-02-22 08:43:57 +00:00
|
|
|
gboolean flatpak_break_hardlink (GFile *file, GError **error);
|
2017-02-09 11:08:12 +00:00
|
|
|
|
2017-02-09 10:24:57 +00:00
|
|
|
gboolean flatpak_write_update_checksum (GOutputStream *out,
|
|
|
|
gconstpointer data,
|
|
|
|
gsize len,
|
|
|
|
gsize *out_bytes_written,
|
2017-12-07 14:40:04 +00:00
|
|
|
GChecksum **checksums,
|
|
|
|
gsize n_checksums,
|
2017-02-09 10:24:57 +00:00
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error);
|
|
|
|
|
2017-04-24 19:23:46 +00:00
|
|
|
|
2017-02-09 10:24:57 +00:00
|
|
|
gboolean flatpak_splice_update_checksum (GOutputStream *out,
|
|
|
|
GInputStream *in,
|
2017-12-07 14:40:04 +00:00
|
|
|
GChecksum **checksums,
|
|
|
|
gsize n_checksums,
|
2017-04-24 19:23:46 +00:00
|
|
|
FlatpakLoadUriProgress progress,
|
|
|
|
gpointer progress_data,
|
2017-02-09 10:24:57 +00:00
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error);
|
|
|
|
|
2016-01-13 14:34:08 +00:00
|
|
|
|
2016-05-06 14:37:47 +00:00
|
|
|
gboolean flatpak_has_name_prefix (const char *string,
|
2015-02-11 13:32:45 +00:00
|
|
|
const char *name);
|
2015-02-11 11:31:53 +00:00
|
|
|
|
2017-05-30 16:07:23 +00:00
|
|
|
char * flatpak_make_valid_id_prefix (const char *orig_id);
|
2015-10-16 13:47:34 +00:00
|
|
|
|
2016-05-06 14:37:47 +00:00
|
|
|
char * flatpak_compose_ref (gboolean app,
|
2015-12-15 13:55:29 +00:00
|
|
|
const char *name,
|
|
|
|
const char *branch,
|
2017-12-12 11:35:35 +00:00
|
|
|
const char *arch);
|
2015-12-15 13:55:29 +00:00
|
|
|
|
2016-05-06 14:37:47 +00:00
|
|
|
char * flatpak_build_untyped_ref (const char *runtime,
|
2015-01-08 15:54:45 +00:00
|
|
|
const char *branch,
|
|
|
|
const char *arch);
|
2016-05-06 14:37:47 +00:00
|
|
|
char * flatpak_build_runtime_ref (const char *runtime,
|
2014-12-18 09:14:46 +00:00
|
|
|
const char *branch,
|
|
|
|
const char *arch);
|
2016-05-06 14:37:47 +00:00
|
|
|
char * flatpak_build_app_ref (const char *app,
|
2014-12-18 09:14:46 +00:00
|
|
|
const char *branch,
|
|
|
|
const char *arch);
|
2016-05-06 14:03:27 +00:00
|
|
|
|
|
|
|
#if !GLIB_CHECK_VERSION (2, 40, 0)
|
2015-02-08 16:08:11 +00:00
|
|
|
static inline gboolean
|
2016-05-06 14:03:27 +00:00
|
|
|
g_key_file_save_to_file (GKeyFile *key_file,
|
|
|
|
const gchar *filename,
|
|
|
|
GError **error)
|
2015-02-08 16:08:11 +00:00
|
|
|
{
|
|
|
|
gchar *contents;
|
|
|
|
gboolean success;
|
|
|
|
gsize length;
|
|
|
|
|
|
|
|
contents = g_key_file_to_data (key_file, &length, NULL);
|
|
|
|
success = g_file_set_contents (filename, contents, length, error);
|
|
|
|
g_free (contents);
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-02-09 12:03:39 +00:00
|
|
|
/* Returns the first string in subset that is not in strv */
|
|
|
|
static inline const gchar *
|
|
|
|
g_strv_subset (const gchar * const *strv,
|
|
|
|
const gchar * const *subset)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; subset[i]; i++)
|
|
|
|
{
|
|
|
|
const char *key;
|
|
|
|
|
|
|
|
key = subset[i];
|
|
|
|
if (!g_strv_contains (strv, key))
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-05-06 14:03:27 +00:00
|
|
|
static inline void
|
2016-05-06 14:37:47 +00:00
|
|
|
flatpak_auto_unlock_helper (GMutex **mutex)
|
2015-08-27 17:42:15 +00:00
|
|
|
{
|
|
|
|
if (*mutex)
|
|
|
|
g_mutex_unlock (*mutex);
|
|
|
|
}
|
|
|
|
|
2016-05-06 14:03:27 +00:00
|
|
|
static inline GMutex *
|
2016-05-06 14:37:47 +00:00
|
|
|
flatpak_auto_lock_helper (GMutex *mutex)
|
2015-08-27 17:42:15 +00:00
|
|
|
{
|
|
|
|
if (mutex)
|
|
|
|
g_mutex_lock (mutex);
|
|
|
|
return mutex;
|
|
|
|
}
|
|
|
|
|
2017-02-21 17:56:11 +00:00
|
|
|
char * flatpak_quote_argv (const char *argv[]);
|
|
|
|
|
2016-05-06 14:37:47 +00:00
|
|
|
gboolean flatpak_spawn (GFile *dir,
|
2016-05-06 14:03:27 +00:00
|
|
|
char **output,
|
2017-10-12 14:19:54 +00:00
|
|
|
GSubprocessFlags flags,
|
2016-05-06 14:03:27 +00:00
|
|
|
GError **error,
|
|
|
|
const gchar *argv0,
|
|
|
|
va_list args);
|
2015-12-03 12:29:08 +00:00
|
|
|
|
2016-06-28 11:39:02 +00:00
|
|
|
gboolean flatpak_spawnv (GFile *dir,
|
|
|
|
char **output,
|
2017-08-18 08:56:15 +00:00
|
|
|
GSubprocessFlags flags,
|
2016-06-28 11:39:02 +00:00
|
|
|
GError **error,
|
|
|
|
const gchar * const *argv);
|
|
|
|
|
2016-08-22 08:20:19 +00:00
|
|
|
const char *flatpak_file_get_path_cached (GFile *file);
|
|
|
|
|
2017-04-21 16:22:52 +00:00
|
|
|
GFile *flatpak_build_file_va (GFile *base,
|
|
|
|
va_list args);
|
|
|
|
GFile *flatpak_build_file (GFile *base, ...) G_GNUC_NULL_TERMINATED;
|
|
|
|
|
2018-01-11 08:36:00 +00:00
|
|
|
gboolean flatpak_file_rename (GFile *from,
|
|
|
|
GFile *to,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error);
|
|
|
|
|
2016-01-22 10:43:58 +00:00
|
|
|
typedef enum {
|
2016-05-06 14:37:47 +00:00
|
|
|
FLATPAK_CP_FLAGS_NONE = 0,
|
|
|
|
FLATPAK_CP_FLAGS_MERGE = 1<<0,
|
|
|
|
FLATPAK_CP_FLAGS_NO_CHOWN = 1<<1,
|
|
|
|
FLATPAK_CP_FLAGS_MOVE = 1<<2,
|
|
|
|
} FlatpakCpFlags;
|
2016-01-22 10:43:58 +00:00
|
|
|
|
2016-05-06 14:37:47 +00:00
|
|
|
gboolean flatpak_cp_a (GFile *src,
|
|
|
|
GFile *dest,
|
|
|
|
FlatpakCpFlags flags,
|
2018-04-25 15:28:06 +00:00
|
|
|
GPtrArray *skip_files,
|
2016-05-06 14:37:47 +00:00
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error);
|
2016-01-22 10:43:58 +00:00
|
|
|
|
2016-06-08 09:11:01 +00:00
|
|
|
gboolean flatpak_zero_mtime (int parent_dfd,
|
|
|
|
const char *rel_path,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error);
|
2016-01-22 10:43:58 +00:00
|
|
|
|
2016-08-22 07:15:46 +00:00
|
|
|
gboolean flatpak_mkdir_p (GFile *dir,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error);
|
|
|
|
|
2016-08-22 08:06:08 +00:00
|
|
|
gboolean flatpak_rm_rf (GFile *dir,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error);
|
|
|
|
|
2016-01-15 11:04:46 +00:00
|
|
|
static inline void
|
2016-05-06 14:37:47 +00:00
|
|
|
flatpak_temp_dir_destroy (void *p)
|
2016-01-15 11:04:46 +00:00
|
|
|
{
|
|
|
|
GFile *dir = p;
|
|
|
|
|
|
|
|
if (dir)
|
|
|
|
{
|
2016-08-22 08:06:08 +00:00
|
|
|
flatpak_rm_rf (dir, NULL, NULL);
|
2016-01-15 11:04:46 +00:00
|
|
|
g_object_unref (dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-06 14:37:47 +00:00
|
|
|
typedef GFile FlatpakTempDir;
|
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC (FlatpakTempDir, flatpak_temp_dir_destroy)
|
2015-12-03 12:29:08 +00:00
|
|
|
|
2017-12-07 14:51:28 +00:00
|
|
|
static inline void
|
|
|
|
builder_object_list_destroy (void *p)
|
|
|
|
{
|
|
|
|
GList *list = p;
|
|
|
|
|
|
|
|
g_list_free_full (list, g_object_unref);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef GList BuilderObjectList;
|
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC (BuilderObjectList, builder_object_list_destroy)
|
2016-08-19 14:51:56 +00:00
|
|
|
|
2016-05-06 14:37:47 +00:00
|
|
|
#define AUTOLOCK(name) G_GNUC_UNUSED __attribute__((cleanup (flatpak_auto_unlock_helper))) GMutex * G_PASTE (auto_unlock, __LINE__) = flatpak_auto_lock_helper (&G_LOCK_NAME (name))
|
2015-08-27 17:42:15 +00:00
|
|
|
|
2017-03-30 07:42:56 +00:00
|
|
|
/* OSTREE_CHECK_VERSION was added immediately after the 2017.3 release */
|
2017-03-28 20:11:06 +00:00
|
|
|
#ifndef OSTREE_CHECK_VERSION
|
|
|
|
#define OSTREE_CHECK_VERSION(year, minor) (0)
|
|
|
|
#endif
|
2017-03-30 07:42:56 +00:00
|
|
|
/* Cleanups are always exported in 2017.4, and some git releases between 2017.3 and 2017.4.
|
|
|
|
We actually check against 2017.3 so that we work on the git releases *after* 2017.3
|
|
|
|
which is safe, because the real OSTREE_CHECK_VERSION macro was added after 2017.3
|
|
|
|
too. */
|
|
|
|
#if !OSTREE_CHECK_VERSION(2017, 3)
|
2016-05-06 14:03:27 +00:00
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC (OstreeRepo, g_object_unref)
|
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC (OstreeMutableTree, g_object_unref)
|
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC (OstreeAsyncProgress, g_object_unref)
|
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC (OstreeGpgVerifyResult, g_object_unref)
|
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC (OstreeRepoCommitModifier, ostree_repo_commit_modifier_unref)
|
2017-02-15 10:45:54 +00:00
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC (OstreeRepoDevInoCache, ostree_repo_devino_cache_unref)
|
2017-03-28 20:11:06 +00:00
|
|
|
#endif
|
2015-09-17 08:56:14 +00:00
|
|
|
|
2018-06-26 10:41:45 +00:00
|
|
|
#if !defined(SOUP_AUTOCLEANUPS_H) && !defined(__SOUP_AUTOCLEANUPS_H__)
|
2016-05-06 14:03:27 +00:00
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC (SoupSession, g_object_unref)
|
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC (SoupMessage, g_object_unref)
|
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC (SoupRequest, g_object_unref)
|
2016-10-25 14:14:44 +00:00
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC (SoupRequestHTTP, g_object_unref)
|
2016-05-06 14:03:27 +00:00
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC (SoupURI, soup_uri_free)
|
2015-09-17 08:56:14 +00:00
|
|
|
#endif
|
|
|
|
|
2016-11-30 10:30:40 +00:00
|
|
|
#if !JSON_CHECK_VERSION(1,1,2)
|
2016-11-29 09:14:33 +00:00
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC (JsonArray, json_array_unref)
|
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC (JsonBuilder, g_object_unref)
|
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC (JsonGenerator, g_object_unref)
|
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC (JsonNode, json_node_free)
|
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC (JsonObject, json_object_unref)
|
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC (JsonParser, g_object_unref)
|
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC (JsonPath, g_object_unref)
|
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC (JsonReader, g_object_unref)
|
|
|
|
#endif
|
|
|
|
|
2016-09-06 07:26:02 +00:00
|
|
|
#if !GLIB_CHECK_VERSION(2, 43, 4)
|
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC(GUnixFDList, g_object_unref)
|
|
|
|
#endif
|
|
|
|
|
2016-05-06 14:37:47 +00:00
|
|
|
gboolean flatpak_allocate_tmpdir (int tmpdir_dfd,
|
2016-05-06 14:03:27 +00:00
|
|
|
const char *tmpdir_relpath,
|
|
|
|
const char *tmpdir_prefix,
|
|
|
|
char **tmpdir_name_out,
|
|
|
|
int *tmpdir_fd_out,
|
2016-04-22 08:37:06 +00:00
|
|
|
GLnxLockFile *file_lock_out,
|
2016-05-06 14:03:27 +00:00
|
|
|
gboolean *reusing_dir_out,
|
2016-04-22 08:37:06 +00:00
|
|
|
GCancellable *cancellable,
|
2016-05-06 14:03:27 +00:00
|
|
|
GError **error);
|
2016-04-22 08:37:06 +00:00
|
|
|
|
|
|
|
|
2016-12-08 13:23:48 +00:00
|
|
|
SoupSession * flatpak_create_soup_session (const char *user_agent);
|
2018-05-03 07:28:27 +00:00
|
|
|
CURL * flatpak_create_curl_session (const char *user_agent);
|
2016-10-20 12:24:32 +00:00
|
|
|
|
2017-08-24 13:48:55 +00:00
|
|
|
typedef struct FlatpakContext FlatpakContext;
|
|
|
|
|
|
|
|
FlatpakContext *flatpak_context_new (void);
|
|
|
|
void flatpak_context_free (FlatpakContext *context);
|
|
|
|
GOptionGroup *flatpak_context_get_options (FlatpakContext *context);
|
|
|
|
void flatpak_context_to_args (FlatpakContext *context,
|
|
|
|
GPtrArray *args);
|
|
|
|
|
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC (FlatpakContext, flatpak_context_free)
|
|
|
|
|
2017-05-10 15:24:48 +00:00
|
|
|
|
2016-05-06 14:37:47 +00:00
|
|
|
#endif /* __FLATPAK_UTILS_H__ */
|