Some initial work on triggers

This commit is contained in:
Colin Walters 2011-10-20 16:11:39 -04:00
parent 629b6293c1
commit 9d8522dfa1
15 changed files with 635 additions and 0 deletions

View File

@ -37,6 +37,8 @@ libostree_la_SOURCES = src/libostree/ostree.h \
src/libostree/ostree-core.h \
src/libostree/ostree-repo.c \
src/libostree/ostree-repo.h \
src/libostree/ostree-checkout.c \
src/libostree/ostree-checkout.h \
$(NULL)
libostree_la_CFLAGS = -I$(srcdir)/src/libostree -I$(srcdir)/src/libotutil -DLOCALEDIR=\"$(datadir)/locale\" $(GIO_UNIX_CFLAGS)
libostree_la_LIBADD = libotutil.la $(GIO_UNIX_LIBS)

29
Makefile-triggers.am Normal file
View File

@ -0,0 +1,29 @@
# Makefile for triggers
#
# Copyright (C) 2011 Colin Walters <walters@verbum.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
triggersdir = $(datadir)/ostree/triggers.d
triggers_DATA = \
triggers.d/desktop-database.trigger \
triggers.d/gdk-pixbuf.trigger \
triggers.d/glib.trigger \
triggers.d/gtk+.trigger \
triggers.d/immodules.trigger \
triggers.d/ldconfig.trigger \
triggers.d/mime-database.trigger \
triggers.d/pango.trigger \
$(NULL)

View File

@ -1,4 +1,5 @@
ACLOCAL_AMFLAGS = -I m4 ${ACLOCAL_FLAGS}
AM_CPPFLAGS = -DDATADIR='"$(datadir)"'
NULL =
BUILT_SOURCES =
@ -11,3 +12,4 @@ noinst_LTLIBRARIES =
noinst_PROGRAMS =
include Makefile-src.am
include Makefile-triggers.am

View File

@ -0,0 +1,343 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2011 Colin Walters <walters@verbum.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Author: Colin Walters <walters@verbum.org>
*/
#include "config.h"
#include "ostree.h"
#include "otutil.h"
enum {
PROP_0,
PROP_REPO,
PROP_PATH
};
G_DEFINE_TYPE (OstreeCheckout, ostree_checkout, G_TYPE_OBJECT)
#define GET_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), OSTREE_TYPE_CHECKOUT, OstreeCheckoutPrivate))
typedef struct _OstreeCheckoutPrivate OstreeCheckoutPrivate;
struct _OstreeCheckoutPrivate {
OstreeRepo *repo;
char *path;
};
static void
ostree_checkout_finalize (GObject *object)
{
OstreeCheckout *self = OSTREE_CHECKOUT (object);
OstreeCheckoutPrivate *priv = GET_PRIVATE (self);
g_free (priv->path);
g_clear_object (&priv->repo);
G_OBJECT_CLASS (ostree_checkout_parent_class)->finalize (object);
}
static void
ostree_checkout_set_property(GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
OstreeCheckout *self = OSTREE_CHECKOUT (object);
OstreeCheckoutPrivate *priv = GET_PRIVATE (self);
switch (prop_id)
{
case PROP_PATH:
priv->path = g_value_dup_string (value);
break;
case PROP_REPO:
priv->repo = g_value_dup_object (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
ostree_checkout_get_property(GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
OstreeCheckout *self = OSTREE_CHECKOUT (object);
OstreeCheckoutPrivate *priv = GET_PRIVATE (self);
switch (prop_id)
{
case PROP_PATH:
g_value_set_string (value, priv->path);
break;
case PROP_REPO:
g_value_set_object (value, priv->repo);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static GObject *
ostree_checkout_constructor (GType gtype,
guint n_properties,
GObjectConstructParam *properties)
{
GObject *object;
GObjectClass *parent_class;
OstreeCheckoutPrivate *priv;
parent_class = G_OBJECT_CLASS (ostree_checkout_parent_class);
object = parent_class->constructor (gtype, n_properties, properties);
priv = GET_PRIVATE (object);
g_assert (priv->path != NULL);
return object;
}
static void
ostree_checkout_class_init (OstreeCheckoutClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (klass, sizeof (OstreeCheckoutPrivate));
object_class->constructor = ostree_checkout_constructor;
object_class->get_property = ostree_checkout_get_property;
object_class->set_property = ostree_checkout_set_property;
object_class->finalize = ostree_checkout_finalize;
g_object_class_install_property (object_class,
PROP_PATH,
g_param_spec_string ("path", "", "",
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class,
PROP_REPO,
g_param_spec_object ("repo", "", "",
OSTREE_TYPE_REPO,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
}
static void
ostree_checkout_init (OstreeCheckout *self)
{
}
OstreeCheckout*
ostree_checkout_new (OstreeRepo *repo,
const char *path)
{
return g_object_new (OSTREE_TYPE_CHECKOUT, "repo", repo, "path", path, NULL);
}
static gboolean
executable_exists_in_checkout (const char *path,
const char *executable)
{
int i;
const char *subdirs[] = {"bin", "sbin", "usr/bin", "usr/sbin"};
for (i = 0; i < G_N_ELEMENTS (subdirs); i++)
{
char *possible_path = g_build_filename (path, subdirs[i], executable, NULL);
gboolean exists;
exists = g_file_test (possible_path, G_FILE_TEST_EXISTS);
g_free (possible_path);
if (exists)
return TRUE;
}
return FALSE;
}
static gboolean
run_trigger (OstreeCheckout *self,
GFile *trigger,
gboolean requires_chroot,
GError **error)
{
OstreeCheckoutPrivate *priv = GET_PRIVATE (self);
gboolean ret = FALSE;
char *path = NULL;
char *basename = NULL;
GPtrArray *args = NULL;
int estatus;
path = g_file_get_path (trigger);
basename = g_path_get_basename (path);
if (requires_chroot)
{
g_ptr_array_add (args, "chroot");
g_ptr_array_add (args, priv->path);
g_ptr_array_add (args, path);
g_ptr_array_add (args, NULL);
}
else
{
g_ptr_array_add (args, path);
g_ptr_array_add (args, NULL);
}
if (!g_spawn_sync (priv->path,
(char**)args->pdata,
NULL,
0, NULL, NULL, NULL, NULL,
&estatus,
error))
{
g_prefix_error (error, "Failed to run trigger %s: ", basename);
goto out;
}
ret = TRUE;
out:
g_free (path);
g_free (basename);
if (args)
g_ptr_array_free (args, TRUE);
return ret;
}
static gboolean
check_trigger (OstreeCheckout *self,
GFile *trigger,
GError **error)
{
OstreeCheckoutPrivate *priv = GET_PRIVATE (self);
gboolean ret = FALSE;
GInputStream *instream = NULL;
GDataInputStream *datain = NULL;
GError *temp_error = NULL;
char *line;
gsize len;
gboolean requires_chroot = FALSE;
gboolean matches = FALSE;
instream = (GInputStream*)g_file_read (trigger, NULL, error);
if (!instream)
goto out;
datain = g_data_input_stream_new (instream);
while ((line = g_data_input_stream_read_line_utf8 (datain, &len, NULL, &temp_error)) != NULL)
{
if (g_str_has_prefix (line, "# IfExecutable: "))
{
char *executable = g_strdup (line + strlen ("# IfExecutable: "));
g_strchomp (executable);
matches = executable_exists_in_checkout (priv->path, executable);
g_free (executable);
}
if (g_str_has_prefix (line, "# RequiresChroot: "))
requires_chroot = TRUE;
g_free (line);
}
if (line == NULL && temp_error != NULL)
{
g_propagate_error (error, temp_error);
goto out;
}
if (matches)
{
if (!run_trigger (self, trigger, requires_chroot, error))
goto out;
}
ret = TRUE;
out:
g_clear_object (&instream);
g_clear_object (&datain);
return ret;
}
gboolean
ostree_checkout_run_triggers (OstreeCheckout *self,
GError **error)
{
gboolean ret = FALSE;
GError *temp_error = NULL;
char *triggerdir_path = NULL;
GFile *triggerdir = NULL;
GFileInfo *file_info = NULL;
GFileEnumerator *enumerator = NULL;
triggerdir_path = g_build_filename (DATADIR, "ostree", "triggers.d", NULL);
triggerdir = ot_util_new_file_for_path (triggerdir_path);
enumerator = g_file_enumerate_children (triggerdir, "standard::name,standard::type,unix::*",
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
NULL,
error);
if (!enumerator)
goto out;
while ((file_info = g_file_enumerator_next_file (enumerator, NULL, &temp_error)) != NULL)
{
const char *name;
guint32 type;
char *child_path = NULL;
GFile *child = NULL;
gboolean success;
name = g_file_info_get_attribute_byte_string (file_info, "standard::name");
type = g_file_info_get_attribute_uint32 (file_info, "standard::type");
if (type == G_FILE_TYPE_REGULAR && g_str_has_suffix (name, ".trigger"))
{
child_path = g_build_filename (triggerdir_path, name, NULL);
child = ot_util_new_file_for_path (child_path);
success = check_trigger (self, child, error);
}
else
success = TRUE;
g_object_unref (file_info);
g_free (child_path);
g_clear_object (&child);
if (!success)
goto out;
}
if (file_info == NULL && temp_error != NULL)
{
g_propagate_error (error, temp_error);
goto out;
}
ret = TRUE;
out:
g_free (triggerdir_path);
g_clear_object (&triggerdir);
g_clear_object (&enumerator);
return ret;
}

View File

@ -0,0 +1,59 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2011 Colin Walters <walters@verbum.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Author: Colin Walters <walters@verbum.org>
*/
#ifndef _OSTREE_CHECKOUT
#define _OSTREE_CHECKOUT
#include <ostree-repo.h>
G_BEGIN_DECLS
#define OSTREE_TYPE_CHECKOUT ostree_checkout_get_type()
#define OSTREE_CHECKOUT(obj) \
(G_TYPE_CHECK_INSTANCE_CAST ((obj), OSTREE_TYPE_CHECKOUT, OstreeCheckout))
#define OSTREE_CHECKOUT_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST ((klass), OSTREE_TYPE_CHECKOUT, OstreeCheckoutClass))
#define OSTREE_IS_CHECKOUT(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE ((obj), OSTREE_TYPE_CHECKOUT))
#define OSTREE_IS_CHECKOUT_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE ((klass), OSTREE_TYPE_CHECKOUT))
#define OSTREE_CHECKOUT_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS ((obj), OSTREE_TYPE_CHECKOUT, OstreeCheckoutClass))
typedef struct {
GObject parent;
} OstreeCheckout;
typedef struct {
GObjectClass parent_class;
} OstreeCheckoutClass;
GType ostree_checkout_get_type (void);
OstreeCheckout* ostree_checkout_new (OstreeRepo *repo,
const char *path);
gboolean ostree_checkout_run_triggers (OstreeCheckout *checkout,
GError **error);
G_END_DECLS
#endif /* _OSTREE_CHECKOUT */

View File

@ -23,5 +23,6 @@
#include <ostree-core.h>
#include <ostree-repo.h>
#include <ostree-checkout.h>
#endif

View File

@ -27,9 +27,11 @@
#include <glib/gi18n.h>
static char *repo_path;
static gboolean no_triggers;
static GOptionEntry options[] = {
{ "repo", 0, 0, G_OPTION_ARG_FILENAME, &repo_path, "Repository path", "repo" },
{ "no-triggers", 0, 0, G_OPTION_ARG_NONE, &no_triggers, "Do not run post-installation trigger scripts", NULL },
{ NULL }
};
@ -39,6 +41,7 @@ ostree_builtin_checkout (int argc, char **argv, const char *prefix, GError **err
GOptionContext *context;
gboolean ret = FALSE;
OstreeRepo *repo = NULL;
OstreeCheckout *checkout = NULL;
int i;
const char *commit;
const char *destination;
@ -72,10 +75,18 @@ ostree_builtin_checkout (int argc, char **argv, const char *prefix, GError **err
if (!ostree_repo_checkout (repo, commit, destination, error))
goto out;
if (!no_triggers)
{
checkout = ostree_checkout_new (repo, destination);
if (!ostree_checkout_run_triggers (checkout, error))
goto out;
}
ret = TRUE;
out:
if (context)
g_option_context_free (context);
g_clear_object (&repo);
g_clear_object (&checkout);
return ret;
}

View File

@ -0,0 +1,22 @@
# Post-installation hook for desktop files. -*- mode: sh -*-
#
# Written by Matthias Clasen <mclasen@redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# IfExecutable: update-desktop-database
# REMatch: /share/applications/.*/.*\.desktop
exec update-desktop-database -q ./usr/share/applications

View File

@ -0,0 +1,24 @@
# Post-installation hook for gdk-pixbuf. -*- mode: sh -*-
# Corresponds to gdk-pixbuf/gdk-pixbuf/Makefile.am:install-data-hook
#
# Written by Colin Walters <walters@verbum.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# IfExecutable: gdk-pixbuf-query-loaders
# RequiresChroot: true
# LiteralMatch: /gdk-pixbuf-2.0/2.10.0/loaders/
exec gdk-pixbuf-query-loaders --update-cache

22
triggers.d/glib.trigger Normal file
View File

@ -0,0 +1,22 @@
# Post-installation hook for glib/gschema. -*- mode: sh -*-
#
# Written by Colin Walters <walters@verbum.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# IfExecutable: glib-compile-schemas
# LiteralMatch: /share/glib-2.0/schemas/
exec glib-compile-schemas ./usr/share/glib-2.0/schemas

29
triggers.d/gtk+.trigger Normal file
View File

@ -0,0 +1,29 @@
# Post-installation hook for gtk icon cache. -*- mode: sh -*-
#
# Written by Colin Walters <walters@verbum.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# IfExecutable: gtk-update-icon-cache
# LiteralMatch: /share/icons/
for dir in ./usr/share/icons/*; do
if test -f $dir/index.theme; then
if ! gtk-update-icon-cache --quiet $dir; then
echo "Failed to run gtk-update-icon-cache for $dir"
exit 1
fi
fi
done

View File

@ -0,0 +1,22 @@
# Post-installation hook for GTK+ input method modules. -*- mode: sh -*-
#
# Written by Matthias Clasen <mclasen@redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# IfExecutable: gtk-query-immodules-3.0
# REMatch: /lib.*/gtk-3\.0/3\.0\.0/immodules/.*\.so
gtk-query-immodules-3.0 --update-cache

View File

@ -0,0 +1,22 @@
# Post-installation hook for shared libraries. -*- mode: sh -*-
#
# Written by Colin Walters <walters@verbum.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# IfExecutable: ldconfig
# REMatch: /lib.*/\.so.*
ldconfig -r .

View File

@ -0,0 +1,22 @@
# Post-installation hook for shared-mime-info. -*- mode: sh -*-
#
# Written by Matthias Clasen <mclasen@redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# IfExecutable: update-mime-database
# REMatch: /mime/packages/.*\.xml
exec update-mime-database ./usr/share/mime

25
triggers.d/pango.trigger Normal file
View File

@ -0,0 +1,25 @@
# Post-installation hook for pango. -*- mode: sh -*-
# Corresponds to gdk-pixbuf/gdk-pixbuf/Makefile.am:install-data-hook
#
# Written by Colin Walters <walters@verbum.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# IfExecutable: pango-querymodules
# REMatch: /lib.*/pango/.*/modules/.*\.so
OSTREE_ROOT=`pwd`
DEST=./usr/etc/pango/pango.modules
pango-querymodules > ${DEST}.tmp && mv ${DEST}.tmp ${DEST}