Add XdgAppDir and use

tingping/wmclass
Alexander Larsson 2014-12-18 10:14:46 +01:00
parent a640cd365b
commit 63859dc0ee
8 changed files with 357 additions and 49 deletions

View File

@ -15,6 +15,10 @@ xdg_app_SOURCES = \
xdg-app-main.c \
xdg-app-builtins.h \
xdg-app-builtins-add-repo.c \
xdg-app-dir.c \
xdg-app-dir.h \
xdg-app-utils.h \
xdg-app-utils.c \
$(NULL)
xdg_app_LDADD = $(OSTREE_LIBS)
xdg_app_CFLAGS = $(OSTREE_CFLAGS)

View File

@ -30,15 +30,14 @@ xdg_app_builtin_add_repo (int argc, char **argv, GCancellable *cancellable, GErr
{
GOptionContext *context;
gboolean ret = FALSE;
gs_unref_object OstreeRepo *repo = NULL;
gs_unref_object GFile *basedir = NULL;
gs_unref_object XdgAppDir *dir = NULL;
gs_unref_variant_builder GVariantBuilder *optbuilder = NULL;
const char *remote_name;
const char *remote_url;
context = g_option_context_new ("NAME URL - Add a remote repository");
if (!xdg_app_option_context_parse (context, options, &argc, &argv, 0, &repo, &basedir, cancellable, error))
if (!xdg_app_option_context_parse (context, options, &argc, &argv, 0, &dir, cancellable, error))
goto out;
if (argc < 3)
@ -57,8 +56,7 @@ xdg_app_builtin_add_repo (int argc, char **argv, GCancellable *cancellable, GErr
"gpg-verify",
g_variant_new_variant (g_variant_new_boolean (FALSE)));
if (!ostree_repo_remote_change (repo, NULL,
if (!ostree_repo_remote_change (xdg_app_dir_get_repo (dir), NULL,
opt_if_not_exists ? OSTREE_REPO_REMOTE_CHANGE_ADD_IF_NOT_EXISTS :
OSTREE_REPO_REMOTE_CHANGE_ADD,
remote_name, remote_url,

View File

@ -4,10 +4,12 @@
#include <ostree.h>
#include <gio/gio.h>
#include "xdg-app-dir.h"
G_BEGIN_DECLS
typedef enum {
XDG_APP_BUILTIN_FLAG_NO_USER = 1 << 0,
XDG_APP_BUILTIN_FLAG_NO_DIR = 1 << 0,
XDG_APP_BUILTIN_FLAG_NO_REPO = 1 << 1,
} XdgAppBuiltinFlags;
@ -16,8 +18,7 @@ gboolean xdg_app_option_context_parse (GOptionContext *context,
int *argc,
char ***argv,
XdgAppBuiltinFlags flags,
OstreeRepo **repo,
GFile **basedir,
XdgAppDir **out_dir,
GCancellable *cancellable,
GError **error);

243
xdg-app-dir.c 100644
View File

@ -0,0 +1,243 @@
#include "config.h"
#include <gio/gio.h>
#include "libgsystem.h"
#include "xdg-app-dir.h"
struct XdgAppDir {
GObject parent;
gboolean user;
GFile *basedir;
OstreeRepo *repo;
};
typedef struct {
GObjectClass parent_class;
} XdgAppDirClass;
G_DEFINE_TYPE (XdgAppDir, xdg_app_dir, G_TYPE_OBJECT)
enum {
PROP_0,
PROP_USER,
PROP_PATH
};
GFile *
xdg_app_get_system_base_dir_location (void)
{
return g_file_new_for_path (XDG_APP_BASEDIR);
}
GFile *
xdg_app_get_user_base_dir_location (void)
{
gs_free char *base = g_build_filename (g_get_user_data_dir (), "xdg-app", NULL);
return g_file_new_for_path (base);
}
static void
xdg_app_dir_finalize (GObject *object)
{
XdgAppDir *self = XDG_APP_DIR (object);
g_clear_object (&self->repo);
g_clear_object (&self->basedir);
G_OBJECT_CLASS (xdg_app_dir_parent_class)->finalize (object);
}
static void
xdg_app_dir_set_property(GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
XdgAppDir *self = XDG_APP_DIR (object);
switch (prop_id)
{
case PROP_PATH:
/* Canonicalize */
self->basedir = g_file_new_for_path (gs_file_get_path_cached (g_value_get_object (value)));
break;
case PROP_USER:
self->user = g_value_get_boolean (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
xdg_app_dir_get_property(GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
XdgAppDir *self = XDG_APP_DIR (object);
switch (prop_id)
{
case PROP_PATH:
g_value_set_object (value, self->basedir);
break;
case PROP_USER:
g_value_set_boolean (value, self->user);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
xdg_app_dir_class_init (XdgAppDirClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->get_property = xdg_app_dir_get_property;
object_class->set_property = xdg_app_dir_set_property;
object_class->finalize = xdg_app_dir_finalize;
g_object_class_install_property (object_class,
PROP_USER,
g_param_spec_boolean ("user",
"",
"",
FALSE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class,
PROP_PATH,
g_param_spec_object ("path",
"",
"",
G_TYPE_FILE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
}
static void
xdg_app_dir_init (XdgAppDir *self)
{
}
gboolean
xdg_app_dir_is_user (XdgAppDir *self)
{
return self->user;
}
GFile *
xdg_app_dir_get_path (XdgAppDir *self)
{
return self->basedir;
}
GFile *
xdg_app_dir_get_deploy_dir (XdgAppDir *self,
const char *ref)
{
return g_file_resolve_relative_path (self->basedir, ref);
}
OstreeRepo *
xdg_app_dir_get_repo (XdgAppDir *self)
{
return self->repo;
}
gboolean
xdg_app_dir_ensure_path (XdgAppDir *self,
GCancellable *cancellable,
GError **error)
{
return gs_file_ensure_directory (self->basedir, TRUE, cancellable, error);
}
gboolean
xdg_app_dir_ensure_repo (XdgAppDir *self,
GCancellable *cancellable,
GError **error)
{
gboolean ret = FALSE;
gs_unref_object GFile *repodir = NULL;
gs_unref_object OstreeRepo *repo = NULL;
if (self->repo == NULL)
{
if (!xdg_app_dir_ensure_path (self, cancellable, error))
goto out;
repodir = g_file_get_child (self->basedir, "repo");
repo = ostree_repo_new (repodir);
if (!g_file_query_exists (repodir, cancellable))
{
if (!ostree_repo_create (repo,
self->user ? OSTREE_REPO_MODE_BARE_USER : OSTREE_REPO_MODE_BARE,
cancellable, error))
{
gs_shutil_rm_rf (repodir, cancellable, NULL);
goto out;
}
}
else
{
if (!ostree_repo_open (repo, cancellable, error))
goto out;
}
self->repo = g_object_ref (repo);
}
ret = TRUE;
out:
return ret;
}
XdgAppDir*
xdg_app_dir_new (GFile *path, gboolean user)
{
return g_object_new (XDG_APP_TYPE_DIR, "path", path, "user", user, NULL);
}
XdgAppDir *
xdg_app_dir_get_system (void)
{
static XdgAppDir *system = NULL;
if (system == NULL)
{
gs_unref_object GFile *path = xdg_app_get_system_base_dir_location ();
system = xdg_app_dir_new (path, FALSE);
}
return g_object_ref (system);
}
XdgAppDir *
xdg_app_dir_get_user (void)
{
static XdgAppDir *user = NULL;
if (user == NULL)
{
gs_unref_object GFile *path = xdg_app_get_user_base_dir_location ();
user = xdg_app_dir_new (path, FALSE);
}
return g_object_ref (user);
}
XdgAppDir *
xdg_app_dir_get (gboolean user)
{
if (user)
return xdg_app_dir_get_user ();
else
return xdg_app_dir_get_system ();
}

34
xdg-app-dir.h 100644
View File

@ -0,0 +1,34 @@
#ifndef __XDG_APP_DIR_H__
#define __XDG_APP_DIR_H__
#include <ostree.h>
typedef struct XdgAppDir XdgAppDir;
#define XDG_APP_TYPE_DIR xdg_app_dir_get_type()
#define XDG_APP_DIR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), XDG_APP_TYPE_DIR, XdgAppDir))
#define XDG_APP_IS_DIR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), XDG_APP_TYPE_DIR))
GType xdg_app_dir_get_type (void);
GFile * xdg_app_get_system_base_dir_location (void);
GFile * xdg_app_get_user_base_dir_location (void);
XdgAppDir* xdg_app_dir_new (GFile *basedir,
gboolean user);
XdgAppDir *xdg_app_dir_get (gboolean user);
XdgAppDir *xdg_app_dir_get_system (void);
XdgAppDir *xdg_app_dir_get_user (void);
gboolean xdg_app_dir_is_user (XdgAppDir *self);
GFile * xdg_app_dir_get_path (XdgAppDir *self);
GFile * xdg_app_dir_get_deploy_dir (XdgAppDir *self,
const char *ref);
OstreeRepo *xdg_app_dir_get_repo (XdgAppDir *self);
gboolean xdg_app_dir_ensure_path (XdgAppDir *self,
GCancellable *cancellable,
GError **error);
gboolean xdg_app_dir_ensure_repo (XdgAppDir *self,
GCancellable *cancellable,
GError **error);
#endif /* __XDG_APP_DIR_H__ */

View File

@ -99,17 +99,14 @@ xdg_app_option_context_parse (GOptionContext *context,
int *argc,
char ***argv,
XdgAppBuiltinFlags flags,
OstreeRepo **out_repo,
GFile **out_basedir,
XdgAppDir **out_dir,
GCancellable *cancellable,
GError **error)
{
gboolean success = FALSE;
gs_unref_object GFile *basedir = NULL;
gs_unref_object GFile *repodir = NULL;
gs_unref_object OstreeRepo *repo = NULL;
gs_unref_object XdgAppDir *dir = NULL;
if (!(flags & XDG_APP_BUILTIN_FLAG_NO_USER))
if (!(flags & XDG_APP_BUILTIN_FLAG_NO_DIR))
g_option_context_add_main_entries (context, user_entries, NULL);
if (main_entries != NULL)
@ -126,49 +123,22 @@ xdg_app_option_context_parse (GOptionContext *context,
exit (EXIT_SUCCESS);
}
if (opt_user)
if (!(flags & XDG_APP_BUILTIN_FLAG_NO_DIR))
{
gs_free char *base = g_build_filename (g_get_user_data_dir (), "xdg-app", NULL);
basedir = g_file_new_for_path (base);
}
else
{
basedir = g_file_new_for_path (XDG_APP_BASEDIR);
}
dir = xdg_app_dir_get (opt_user);
if (!(flags & XDG_APP_BUILTIN_FLAG_NO_USER))
{
if (!gs_file_ensure_directory (basedir, TRUE, cancellable, error))
if (!xdg_app_dir_ensure_path (dir, cancellable, error))
goto out;
if (!(flags & XDG_APP_BUILTIN_FLAG_NO_REPO))
{
repodir = g_file_get_child (basedir, "repo");
repo = ostree_repo_new (repodir);
if (!g_file_query_exists (repodir, cancellable))
{
if (!ostree_repo_create (repo,
opt_user ? OSTREE_REPO_MODE_BARE_USER : OSTREE_REPO_MODE_BARE,
cancellable, error))
{
gs_shutil_rm_rf (repodir, cancellable, NULL);
goto out;
}
}
else
{
if (!ostree_repo_open (repo, cancellable, error))
goto out;
}
}
if (!(flags & XDG_APP_BUILTIN_FLAG_NO_REPO) &&
!xdg_app_dir_ensure_repo (dir, cancellable,error))
goto out;
}
if (opt_verbose)
g_log_set_handler (NULL, G_LOG_LEVEL_DEBUG, message_handler, NULL);
gs_transfer_out_value (out_repo, &repo);
gs_transfer_out_value (out_basedir, &basedir);
gs_transfer_out_value (out_dir, &dir);
success = TRUE;
out:
@ -232,7 +202,7 @@ xdg_app_run (int argc,
context = xdg_app_option_context_new_with_commands (commands);
/* This will not return for some options (e.g. --version). */
if (xdg_app_option_context_parse (context, NULL, &argc, &argv, XDG_APP_BUILTIN_FLAG_NO_USER, NULL, NULL, cancellable, &error))
if (xdg_app_option_context_parse (context, NULL, &argc, &argv, XDG_APP_BUILTIN_FLAG_NO_DIR, NULL, cancellable, &error))
{
if (command_name == NULL)
{

45
xdg-app-utils.c 100644
View File

@ -0,0 +1,45 @@
#include "config.h"
#include "xdg-app-utils.h"
#include <stdlib.h>
#include <sys/utsname.h>
const char *
xdg_app_get_arch (void)
{
static struct utsname buf;
static char *arch = NULL;
if (arch == NULL)
{
if (uname (&buf))
arch = "unknown";
else
arch = buf.machine;
}
return arch;
}
char *
xdg_app_build_runtime_ref (const char *runtime,
const char *branch,
const char *arch)
{
if (arch == NULL)
arch = xdg_app_get_arch ();
return g_build_filename ("runtime", runtime, arch, branch, NULL);
}
char *
xdg_app_build_app_ref (const char *app,
const char *branch,
const char *arch)
{
if (arch == NULL)
arch = xdg_app_get_arch ();
return g_build_filename ("app", app, arch, branch, NULL);
}

13
xdg-app-utils.h 100644
View File

@ -0,0 +1,13 @@
#ifndef __XDG_APP_UTILS_H__
#define __XDG_APP_UTILS_H__
const char * xdg_app_get_arch (void);
char * xdg_app_build_runtime_ref (const char *runtime,
const char *branch,
const char *arch);
char * xdg_app_build_app_ref (const char *app,
const char *branch,
const char *arch);
#endif /* __XDG_APP_UTILS_H__ */