forked from Mirrors/flatpak-builder
Initial version
commit
a640cd365b
|
@ -0,0 +1,28 @@
|
|||
*.la
|
||||
*.o
|
||||
*.lo
|
||||
.deps
|
||||
.libs
|
||||
INSTALL
|
||||
Makefile
|
||||
Makefile.in
|
||||
aclocal.m4
|
||||
autom4te.cache/
|
||||
compile
|
||||
config.guess
|
||||
config.h
|
||||
config.log
|
||||
config.status
|
||||
config.sub
|
||||
configure
|
||||
depcomp
|
||||
install-sh
|
||||
libtool
|
||||
ltmain.sh
|
||||
m4
|
||||
missing
|
||||
stamp-h1
|
||||
config.h.in
|
||||
stamp-*
|
||||
xdg-app
|
||||
xdg-app-helper
|
|
@ -0,0 +1,24 @@
|
|||
NULL =
|
||||
|
||||
AM_CPPFLAGS = \
|
||||
-DXDG_APP_BASEDIR=\"$(datadir)/xdg-app\" \
|
||||
$(NULL)
|
||||
|
||||
bin_PROGRAMS = \
|
||||
xdg-app-helper \
|
||||
xdg-app \
|
||||
$(NULL)
|
||||
|
||||
xdg_app_helper_SOURCES = xdg-app-helper.c
|
||||
|
||||
xdg_app_SOURCES = \
|
||||
xdg-app-main.c \
|
||||
xdg-app-builtins.h \
|
||||
xdg-app-builtins-add-repo.c \
|
||||
$(NULL)
|
||||
xdg_app_LDADD = $(OSTREE_LIBS)
|
||||
xdg_app_CFLAGS = $(OSTREE_CFLAGS)
|
||||
|
||||
install-exec-hook:
|
||||
chown root $(bindir)/xdg-app-helper
|
||||
chmod u+s $(bindir)/xdg-app-helper
|
|
@ -0,0 +1,24 @@
|
|||
#!/bin/sh
|
||||
# Run this to generate all the initial makefiles, etc.
|
||||
|
||||
test -n "$srcdir" || srcdir=`dirname "$0"`
|
||||
test -n "$srcdir" || srcdir=.
|
||||
|
||||
olddir=`pwd`
|
||||
cd "$srcdir"
|
||||
|
||||
AUTORECONF=`which autoreconf`
|
||||
if test -z $AUTORECONF; then
|
||||
echo "*** No autoreconf found, please install it ***"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# INSTALL are required by automake, but may be deleted by clean
|
||||
# up rules. to get automake to work, simply touch these here, they will be
|
||||
# regenerated from their corresponding *.in files by ./configure anyway.
|
||||
touch INSTALL
|
||||
|
||||
autoreconf --force --install --verbose || exit $?
|
||||
|
||||
cd "$olddir"
|
||||
test -n "$NOCONFIGURE" || "$srcdir/configure" "$@"
|
|
@ -0,0 +1,35 @@
|
|||
AC_PREREQ([2.63])
|
||||
|
||||
AC_INIT([xdg-app],[0.0.1])
|
||||
|
||||
AC_PROG_CC
|
||||
AM_PROG_CC_C_O
|
||||
AC_DISABLE_STATIC
|
||||
|
||||
LT_PREREQ([2.2.6])
|
||||
LT_INIT([disable-static])
|
||||
|
||||
AC_CONFIG_SRCDIR([xdg-app-helper.c])
|
||||
AC_CONFIG_HEADERS([config.h])
|
||||
AC_CONFIG_MACRO_DIR([m4])
|
||||
AM_INIT_AUTOMAKE([1.11 no-define no-dist-gzip dist-bzip2 tar-ustar foreign])
|
||||
|
||||
# Enable silent rules is available
|
||||
AM_SILENT_RULES([yes])
|
||||
AM_MAINTAINER_MODE([enable])
|
||||
|
||||
if test "x$GCC" = "xyes"; then
|
||||
case " $CFLAGS " in
|
||||
*[[\ \ ]]-Wall[[\ \ ]]*) ;;
|
||||
*) CFLAGS="$CFLAGS -Wall" ;;
|
||||
esac
|
||||
fi
|
||||
|
||||
PKG_CHECK_MODULES(OSTREE, [glib-2.0 libgsystem gio-2.0 ostree-1])
|
||||
AC_SUBST(OSTREE_CFLAGS)
|
||||
AC_SUBST(OSTREE_LIBS)
|
||||
|
||||
AC_CONFIG_FILES([
|
||||
Makefile
|
||||
])
|
||||
AC_OUTPUT
|
|
@ -0,0 +1,75 @@
|
|||
#include "config.h"
|
||||
|
||||
#include <locale.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "libgsystem.h"
|
||||
|
||||
#include "xdg-app-builtins.h"
|
||||
|
||||
static gboolean opt_no_gpg_verify;
|
||||
static gboolean opt_if_not_exists;
|
||||
|
||||
static GOptionEntry options[] = {
|
||||
{ "no-gpg-verify", 0, 0, G_OPTION_ARG_NONE, &opt_no_gpg_verify, "Disable GPG verification", NULL },
|
||||
{ "if-not-exists", 0, 0, G_OPTION_ARG_NONE, &opt_if_not_exists, "Do nothing if the provided remote exists", NULL },
|
||||
{ NULL }
|
||||
};
|
||||
|
||||
static void
|
||||
usage_error (GOptionContext *context, const char *message, GError **error)
|
||||
{
|
||||
gs_free gchar *help = g_option_context_get_help (context, TRUE, NULL);
|
||||
g_printerr ("%s", help);
|
||||
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, message);
|
||||
}
|
||||
|
||||
gboolean
|
||||
xdg_app_builtin_add_repo (int argc, char **argv, GCancellable *cancellable, GError **error)
|
||||
{
|
||||
GOptionContext *context;
|
||||
gboolean ret = FALSE;
|
||||
gs_unref_object OstreeRepo *repo = NULL;
|
||||
gs_unref_object GFile *basedir = 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))
|
||||
goto out;
|
||||
|
||||
if (argc < 3)
|
||||
{
|
||||
usage_error (context, "NAME and URL must be specified", error);
|
||||
goto out;
|
||||
}
|
||||
|
||||
remote_name = argv[1];
|
||||
remote_url = argv[2];
|
||||
|
||||
optbuilder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
|
||||
|
||||
if (opt_no_gpg_verify)
|
||||
g_variant_builder_add (optbuilder, "{s@v}",
|
||||
"gpg-verify",
|
||||
g_variant_new_variant (g_variant_new_boolean (FALSE)));
|
||||
|
||||
|
||||
if (!ostree_repo_remote_change (repo, NULL,
|
||||
opt_if_not_exists ? OSTREE_REPO_REMOTE_CHANGE_ADD_IF_NOT_EXISTS :
|
||||
OSTREE_REPO_REMOTE_CHANGE_ADD,
|
||||
remote_name, remote_url,
|
||||
g_variant_builder_end (optbuilder),
|
||||
cancellable, error))
|
||||
goto out;
|
||||
|
||||
ret = TRUE;
|
||||
|
||||
out:
|
||||
if (context)
|
||||
g_option_context_free (context);
|
||||
return ret;
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
#ifndef __XDG_APP_BUILTINS_H__
|
||||
#define __XDG_APP_BUILTINS_H__
|
||||
|
||||
#include <ostree.h>
|
||||
#include <gio/gio.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
typedef enum {
|
||||
XDG_APP_BUILTIN_FLAG_NO_USER = 1 << 0,
|
||||
XDG_APP_BUILTIN_FLAG_NO_REPO = 1 << 1,
|
||||
} XdgAppBuiltinFlags;
|
||||
|
||||
gboolean xdg_app_option_context_parse (GOptionContext *context,
|
||||
const GOptionEntry *main_entries,
|
||||
int *argc,
|
||||
char ***argv,
|
||||
XdgAppBuiltinFlags flags,
|
||||
OstreeRepo **repo,
|
||||
GFile **basedir,
|
||||
GCancellable *cancellable,
|
||||
GError **error);
|
||||
|
||||
#define BUILTINPROTO(name) gboolean xdg_app_builtin_ ## name (int argc, char **argv, GCancellable *cancellable, GError **error)
|
||||
|
||||
BUILTINPROTO(add_repo);
|
||||
|
||||
#undef BUILTINPROTO
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* __XDG_APP_BUILTINS_H__ */
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,317 @@
|
|||
#include "config.h"
|
||||
|
||||
#include <locale.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <gio/gio.h>
|
||||
#include "libgsystem.h"
|
||||
|
||||
#include "xdg-app-builtins.h"
|
||||
|
||||
static gboolean opt_verbose;
|
||||
static gboolean opt_version;
|
||||
static gboolean opt_user;
|
||||
|
||||
typedef struct {
|
||||
const char *name;
|
||||
gboolean (*fn) (int argc, char **argv, GCancellable *cancellable, GError **error);
|
||||
} XdgAppCommand;
|
||||
|
||||
static XdgAppCommand commands[] = {
|
||||
{ "add-repo", xdg_app_builtin_add_repo },
|
||||
{ NULL }
|
||||
};
|
||||
|
||||
static GOptionEntry global_entries[] = {
|
||||
{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &opt_verbose, "Print debug information during command processing", NULL },
|
||||
{ "version", 0, 0, G_OPTION_ARG_NONE, &opt_version, "Print version information and exit", NULL },
|
||||
{ NULL }
|
||||
};
|
||||
|
||||
static GOptionEntry user_entries[] = {
|
||||
{ "user", 0, 0, G_OPTION_ARG_NONE, &opt_user, "Work on user installed apps", NULL },
|
||||
{ NULL }
|
||||
};
|
||||
|
||||
static void
|
||||
message_handler (const gchar *log_domain,
|
||||
GLogLevelFlags log_level,
|
||||
const gchar *message,
|
||||
gpointer user_data)
|
||||
{
|
||||
/* Make this look like normal console output */
|
||||
if (log_level & G_LOG_LEVEL_DEBUG)
|
||||
g_printerr ("XA: %s\n", message);
|
||||
else
|
||||
g_printerr ("%s: %s\n", g_get_prgname (), message);
|
||||
}
|
||||
|
||||
GOptionContext *
|
||||
xdg_app_option_context_new_with_commands (XdgAppCommand *commands)
|
||||
{
|
||||
GOptionContext *context;
|
||||
GString *summary;
|
||||
|
||||
context = g_option_context_new ("COMMAND");
|
||||
|
||||
summary = g_string_new ("Builtin Commands:");
|
||||
|
||||
while (commands->name != NULL)
|
||||
{
|
||||
g_string_append_printf (summary, "\n %s", commands->name);
|
||||
commands++;
|
||||
}
|
||||
|
||||
g_option_context_set_summary (context, summary->str);
|
||||
|
||||
g_string_free (summary, TRUE);
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
int
|
||||
xdg_app_usage (XdgAppCommand *commands,
|
||||
gboolean is_error)
|
||||
{
|
||||
GOptionContext *context;
|
||||
gs_free char *help;
|
||||
|
||||
context = xdg_app_option_context_new_with_commands (commands);
|
||||
|
||||
g_option_context_add_main_entries (context, global_entries, NULL);
|
||||
|
||||
help = g_option_context_get_help (context, FALSE, NULL);
|
||||
|
||||
if (is_error)
|
||||
g_printerr ("%s", help);
|
||||
else
|
||||
g_print ("%s", help);
|
||||
|
||||
g_option_context_free (context);
|
||||
|
||||
return (is_error ? 1 : 0);
|
||||
}
|
||||
|
||||
gboolean
|
||||
xdg_app_option_context_parse (GOptionContext *context,
|
||||
const GOptionEntry *main_entries,
|
||||
int *argc,
|
||||
char ***argv,
|
||||
XdgAppBuiltinFlags flags,
|
||||
OstreeRepo **out_repo,
|
||||
GFile **out_basedir,
|
||||
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;
|
||||
|
||||
if (!(flags & XDG_APP_BUILTIN_FLAG_NO_USER))
|
||||
g_option_context_add_main_entries (context, user_entries, NULL);
|
||||
|
||||
if (main_entries != NULL)
|
||||
g_option_context_add_main_entries (context, main_entries, NULL);
|
||||
|
||||
g_option_context_add_main_entries (context, global_entries, NULL);
|
||||
|
||||
if (!g_option_context_parse (context, argc, argv, error))
|
||||
return FALSE;
|
||||
|
||||
if (opt_version)
|
||||
{
|
||||
g_print ("%s\n", PACKAGE_STRING);
|
||||
exit (EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
if (opt_user)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
if (!(flags & XDG_APP_BUILTIN_FLAG_NO_USER))
|
||||
{
|
||||
if (!gs_file_ensure_directory (basedir, TRUE, 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 (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);
|
||||
|
||||
success = TRUE;
|
||||
out:
|
||||
return success;
|
||||
}
|
||||
|
||||
int
|
||||
xdg_app_run (int argc,
|
||||
char **argv,
|
||||
XdgAppCommand *commands,
|
||||
GError **res_error)
|
||||
{
|
||||
XdgAppCommand *command;
|
||||
GError *error = NULL;
|
||||
GCancellable *cancellable = NULL;
|
||||
const char *command_name = NULL;
|
||||
gs_free char *prgname = NULL;
|
||||
gboolean success = FALSE;
|
||||
int in, out;
|
||||
|
||||
/*
|
||||
* Parse the global options. We rearrange the options as
|
||||
* necessary, in order to pass relevant options through
|
||||
* to the commands, but also have them take effect globally.
|
||||
*/
|
||||
for (in = 1, out = 1; in < argc; in++, out++)
|
||||
{
|
||||
/* The non-option is the command, take it out of the arguments */
|
||||
if (argv[in][0] != '-')
|
||||
{
|
||||
if (command_name == NULL)
|
||||
{
|
||||
command_name = argv[in];
|
||||
out--;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if (g_str_equal (argv[in], "--"))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
argv[out] = argv[in];
|
||||
}
|
||||
|
||||
argc = out;
|
||||
|
||||
command = commands;
|
||||
while (command->name)
|
||||
{
|
||||
if (g_strcmp0 (command_name, command->name) == 0)
|
||||
break;
|
||||
command++;
|
||||
}
|
||||
|
||||
if (!command->fn)
|
||||
{
|
||||
GOptionContext *context;
|
||||
gs_free char *help;
|
||||
|
||||
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 (command_name == NULL)
|
||||
{
|
||||
g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_FAILED,
|
||||
"No command specified");
|
||||
}
|
||||
else
|
||||
{
|
||||
g_set_error (&error, G_IO_ERROR, G_IO_ERROR_FAILED,
|
||||
"Unknown command '%s'", command_name);
|
||||
}
|
||||
}
|
||||
|
||||
help = g_option_context_get_help (context, FALSE, NULL);
|
||||
g_printerr ("%s", help);
|
||||
|
||||
g_option_context_free (context);
|
||||
|
||||
goto out;
|
||||
}
|
||||
|
||||
prgname = g_strdup_printf ("%s %s", g_get_prgname (), command_name);
|
||||
g_set_prgname (prgname);
|
||||
|
||||
if (!command->fn (argc, argv, cancellable, &error))
|
||||
goto out;
|
||||
|
||||
success = TRUE;
|
||||
out:
|
||||
g_assert (success || error);
|
||||
|
||||
if (error)
|
||||
{
|
||||
g_propagate_error (res_error, error);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc,
|
||||
char **argv)
|
||||
{
|
||||
GError *error = NULL;
|
||||
gs_free const char *old_env = NULL;
|
||||
int ret;
|
||||
|
||||
setlocale (LC_ALL, "");
|
||||
|
||||
g_log_set_handler (NULL, G_LOG_LEVEL_MESSAGE, message_handler, NULL);
|
||||
|
||||
g_set_prgname (argv[0]);
|
||||
|
||||
/* avoid gvfs (http://bugzilla.gnome.org/show_bug.cgi?id=526454) */
|
||||
old_env = g_strdup (g_getenv ("GIO_USE_VFS"));
|
||||
g_setenv ("GIO_USE_VFS", "local", TRUE);
|
||||
g_vfs_get_default ();
|
||||
if (old_env)
|
||||
g_setenv ("GIO_USE_VFS", old_env, TRUE);
|
||||
else
|
||||
g_unsetenv ("GIO_USE_VFS");
|
||||
|
||||
ret = xdg_app_run (argc, argv, commands, &error);
|
||||
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
|
||||
xdg_app_usage (commands, TRUE);
|
||||
|
||||
if (error != NULL)
|
||||
{
|
||||
int is_tty = isatty (1);
|
||||
const char *prefix = "";
|
||||
const char *suffix = "";
|
||||
if (is_tty)
|
||||
{
|
||||
prefix = "\x1b[31m\x1b[1m"; /* red, bold */
|
||||
suffix = "\x1b[22m\x1b[0m"; /* bold off, color reset */
|
||||
}
|
||||
g_printerr ("%serror: %s%s\n", prefix, suffix, error->message);
|
||||
g_error_free (error);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
Loading…
Reference in New Issue