Blame src/builder-source.c

rpm-build c487f7
/* builder-source.c
rpm-build c487f7
 *
rpm-build c487f7
 * Copyright (C) 2015 Red Hat, Inc
rpm-build c487f7
 *
rpm-build c487f7
 * This file is free software; you can redistribute it and/or modify it
rpm-build c487f7
 * under the terms of the GNU Lesser General Public License as
rpm-build c487f7
 * published by the Free Software Foundation; either version 2 of the
rpm-build c487f7
 * License, or (at your option) any later version.
rpm-build c487f7
 *
rpm-build c487f7
 * This file is distributed in the hope that it will be useful, but
rpm-build c487f7
 * WITHOUT ANY WARRANTY; without even the implied warranty of
rpm-build c487f7
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
rpm-build c487f7
 * Lesser General Public License for more details.
rpm-build c487f7
 *
rpm-build c487f7
 * You should have received a copy of the GNU Lesser General Public License
rpm-build c487f7
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
rpm-build c487f7
 *
rpm-build c487f7
 * Authors:
rpm-build c487f7
 *       Alexander Larsson <alexl@redhat.com>
rpm-build c487f7
 */
rpm-build c487f7
rpm-build c487f7
#include "config.h"
rpm-build c487f7
rpm-build c487f7
#include <string.h>
rpm-build c487f7
#include <fcntl.h>
rpm-build c487f7
#include <stdio.h>
rpm-build c487f7
#include <stdlib.h>
rpm-build c487f7
#include <sys/statfs.h>
rpm-build c487f7
rpm-build c487f7
#include "builder-utils.h"
rpm-build c487f7
#include "builder-source.h"
rpm-build c487f7
#include "builder-source-archive.h"
rpm-build c487f7
#include "builder-source-patch.h"
rpm-build c487f7
#include "builder-source-git.h"
rpm-build c487f7
#include "builder-source-bzr.h"
rpm-build c487f7
#include "builder-source-svn.h"
rpm-build c487f7
#include "builder-source-file.h"
rpm-build c487f7
#include "builder-source-dir.h"
rpm-build c487f7
#include "builder-source-script.h"
rpm-build c487f7
#include "builder-source-shell.h"
rpm-build c487f7
#include "builder-source-extra-data.h"
rpm-build c487f7
rpm-build c487f7
static void serializable_iface_init (JsonSerializableIface *serializable_iface);
rpm-build c487f7
rpm-build c487f7
G_DEFINE_TYPE_WITH_CODE (BuilderSource, builder_source, G_TYPE_OBJECT,
rpm-build c487f7
                         G_IMPLEMENT_INTERFACE (JSON_TYPE_SERIALIZABLE, serializable_iface_init));
rpm-build c487f7
rpm-build c487f7
enum {
rpm-build c487f7
  PROP_0,
rpm-build c487f7
  PROP_DEST,
rpm-build c487f7
  PROP_ONLY_ARCHES,
rpm-build c487f7
  PROP_SKIP_ARCHES,
rpm-build c487f7
  LAST_PROP
rpm-build c487f7
};
rpm-build c487f7
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
builder_source_finalize (GObject *object)
rpm-build c487f7
{
rpm-build c487f7
  BuilderSource *self = BUILDER_SOURCE (object);
rpm-build c487f7
rpm-build c487f7
  g_clear_object (&self->base_dir);
rpm-build c487f7
  g_free (self->dest);
rpm-build c487f7
rpm-build c487f7
  G_OBJECT_CLASS (builder_source_parent_class)->finalize (object);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
void
rpm-build c487f7
builder_source_set_base_dir (BuilderSource  *self,
rpm-build c487f7
                             GFile          *base_dir)
rpm-build c487f7
{
rpm-build c487f7
  g_set_object (&self->base_dir, base_dir);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
builder_source_get_property (GObject    *object,
rpm-build c487f7
                             guint       prop_id,
rpm-build c487f7
                             GValue     *value,
rpm-build c487f7
                             GParamSpec *pspec)
rpm-build c487f7
{
rpm-build c487f7
  BuilderSource *self = BUILDER_SOURCE (object);
rpm-build c487f7
rpm-build c487f7
  switch (prop_id)
rpm-build c487f7
    {
rpm-build c487f7
    case PROP_DEST:
rpm-build c487f7
      g_value_set_string (value, self->dest);
rpm-build c487f7
      break;
rpm-build c487f7
rpm-build c487f7
    case PROP_ONLY_ARCHES:
rpm-build c487f7
      g_value_set_boxed (value, self->only_arches);
rpm-build c487f7
      break;
rpm-build c487f7
rpm-build c487f7
    case PROP_SKIP_ARCHES:
rpm-build c487f7
      g_value_set_boxed (value, self->skip_arches);
rpm-build c487f7
      break;
rpm-build c487f7
rpm-build c487f7
    default:
rpm-build c487f7
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
rpm-build c487f7
    }
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
builder_source_set_property (GObject      *object,
rpm-build c487f7
                             guint         prop_id,
rpm-build c487f7
                             const GValue *value,
rpm-build c487f7
                             GParamSpec   *pspec)
rpm-build c487f7
{
rpm-build c487f7
  BuilderSource *self = BUILDER_SOURCE (object);
rpm-build c487f7
  gchar **tmp;
rpm-build c487f7
rpm-build c487f7
  switch (prop_id)
rpm-build c487f7
    {
rpm-build c487f7
    case PROP_DEST:
rpm-build c487f7
      g_free (self->dest);
rpm-build c487f7
      self->dest = g_value_dup_string (value);
rpm-build c487f7
      break;
rpm-build c487f7
rpm-build c487f7
    case PROP_ONLY_ARCHES:
rpm-build c487f7
      tmp = self->only_arches;
rpm-build c487f7
      self->only_arches = g_strdupv (g_value_get_boxed (value));
rpm-build c487f7
      g_strfreev (tmp);
rpm-build c487f7
      break;
rpm-build c487f7
rpm-build c487f7
    case PROP_SKIP_ARCHES:
rpm-build c487f7
      tmp = self->skip_arches;
rpm-build c487f7
      self->skip_arches = g_strdupv (g_value_get_boxed (value));
rpm-build c487f7
      g_strfreev (tmp);
rpm-build c487f7
      break;
rpm-build c487f7
rpm-build c487f7
    default:
rpm-build c487f7
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
rpm-build c487f7
    }
rpm-build c487f7
}
rpm-build c487f7
static gboolean
rpm-build c487f7
builder_source_real_show_deps (BuilderSource  *self,
rpm-build c487f7
                               GError        **error)
rpm-build c487f7
{
rpm-build c487f7
  return TRUE;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static gboolean
rpm-build c487f7
builder_source_real_download (BuilderSource  *self,
rpm-build c487f7
                              gboolean        update_vcs,
rpm-build c487f7
                              BuilderContext *context,
rpm-build c487f7
                              GError        **error)
rpm-build c487f7
{
rpm-build c487f7
  g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
rpm-build c487f7
               "Download not implemented for type %s", g_type_name_from_instance ((GTypeInstance *) self));
rpm-build c487f7
  return FALSE;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static gboolean
rpm-build c487f7
builder_source_real_extract (BuilderSource  *self,
rpm-build c487f7
                             GFile          *dest,
rpm-build c487f7
                             BuilderOptions *build_options,
rpm-build c487f7
                             BuilderContext *context,
rpm-build c487f7
                             GError        **error)
rpm-build c487f7
{
rpm-build c487f7
  g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
rpm-build c487f7
               "Extract not implemented for type %s", g_type_name_from_instance ((GTypeInstance *) self));
rpm-build c487f7
  return FALSE;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static gboolean
rpm-build c487f7
builder_source_real_bundle (BuilderSource  *self,
rpm-build c487f7
                            BuilderContext *context,
rpm-build c487f7
                            GError        **error)
rpm-build c487f7
{
rpm-build c487f7
  g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
rpm-build c487f7
               "Bundle not implemented for type %s", g_type_name_from_instance ((GTypeInstance *) self));
rpm-build c487f7
  return FALSE;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static gboolean
rpm-build c487f7
builder_source_real_update (BuilderSource  *self,
rpm-build c487f7
                            BuilderContext *context,
rpm-build c487f7
                            GError        **error)
rpm-build c487f7
{
rpm-build c487f7
  return TRUE;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
builder_source_class_init (BuilderSourceClass *klass)
rpm-build c487f7
{
rpm-build c487f7
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
rpm-build c487f7
rpm-build c487f7
  object_class->finalize = builder_source_finalize;
rpm-build c487f7
  object_class->get_property = builder_source_get_property;
rpm-build c487f7
  object_class->set_property = builder_source_set_property;
rpm-build c487f7
rpm-build c487f7
  klass->show_deps = builder_source_real_show_deps;
rpm-build c487f7
  klass->download = builder_source_real_download;
rpm-build c487f7
  klass->extract = builder_source_real_extract;
rpm-build c487f7
  klass->bundle = builder_source_real_bundle;
rpm-build c487f7
  klass->update = builder_source_real_update;
rpm-build c487f7
rpm-build c487f7
  g_object_class_install_property (object_class,
rpm-build c487f7
                                   PROP_DEST,
rpm-build c487f7
                                   g_param_spec_string ("dest",
rpm-build c487f7
                                                        "",
rpm-build c487f7
                                                        "",
rpm-build c487f7
                                                        NULL,
rpm-build c487f7
                                                        G_PARAM_READWRITE));
rpm-build c487f7
  g_object_class_install_property (object_class,
rpm-build c487f7
                                   PROP_ONLY_ARCHES,
rpm-build c487f7
                                   g_param_spec_boxed ("only-arches",
rpm-build c487f7
                                                       "",
rpm-build c487f7
                                                       "",
rpm-build c487f7
                                                       G_TYPE_STRV,
rpm-build c487f7
                                                       G_PARAM_READWRITE));
rpm-build c487f7
  g_object_class_install_property (object_class,
rpm-build c487f7
                                   PROP_SKIP_ARCHES,
rpm-build c487f7
                                   g_param_spec_boxed ("skip-arches",
rpm-build c487f7
                                                       "",
rpm-build c487f7
                                                       "",
rpm-build c487f7
                                                       G_TYPE_STRV,
rpm-build c487f7
                                                       G_PARAM_READWRITE));
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
builder_source_init (BuilderSource *self)
rpm-build c487f7
{
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static GParamSpec *
rpm-build c487f7
builder_source_find_property (JsonSerializable *serializable,
rpm-build c487f7
                              const char       *name)
rpm-build c487f7
{
rpm-build c487f7
  if (strcmp (name, "type") == 0)
rpm-build c487f7
    return NULL;
rpm-build c487f7
  return builder_serializable_find_property (serializable, name);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
serializable_iface_init (JsonSerializableIface *serializable_iface)
rpm-build c487f7
{
rpm-build c487f7
  serializable_iface->serialize_property = builder_serializable_serialize_property;
rpm-build c487f7
  serializable_iface->deserialize_property = builder_serializable_deserialize_property;
rpm-build c487f7
  serializable_iface->find_property = builder_source_find_property;
rpm-build c487f7
  serializable_iface->list_properties = builder_serializable_list_properties;
rpm-build c487f7
  serializable_iface->set_property = builder_serializable_set_property;
rpm-build c487f7
  serializable_iface->get_property = builder_serializable_get_property;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
JsonNode *
rpm-build c487f7
builder_source_to_json (BuilderSource *self)
rpm-build c487f7
{
rpm-build c487f7
  JsonNode *node;
rpm-build c487f7
  JsonObject *object;
rpm-build c487f7
  const gchar *type = NULL;
rpm-build c487f7
rpm-build c487f7
  node = json_gobject_serialize (G_OBJECT (self));
rpm-build c487f7
  object = json_node_get_object (node);
rpm-build c487f7
rpm-build c487f7
  if (BUILDER_IS_SOURCE_ARCHIVE (self))
rpm-build c487f7
    type = "archive";
rpm-build c487f7
  else if (BUILDER_IS_SOURCE_FILE (self))
rpm-build c487f7
    type = "file";
rpm-build c487f7
  else if (BUILDER_IS_SOURCE_DIR (self))
rpm-build c487f7
    type = "dir";
rpm-build c487f7
  else if (BUILDER_IS_SOURCE_SCRIPT (self))
rpm-build c487f7
    type = "script";
rpm-build c487f7
  else if (BUILDER_IS_SOURCE_SHELL (self))
rpm-build c487f7
    type = "shell";
rpm-build c487f7
  else if (BUILDER_IS_SOURCE_EXTRA_DATA (self))
rpm-build c487f7
    type = "extra-data";
rpm-build c487f7
  else if (BUILDER_IS_SOURCE_PATCH (self))
rpm-build c487f7
    type = "patch";
rpm-build c487f7
  else if (BUILDER_IS_SOURCE_GIT (self))
rpm-build c487f7
    type = "git";
rpm-build c487f7
  else if (BUILDER_IS_SOURCE_BZR (self))
rpm-build c487f7
    type = "bzr";
rpm-build c487f7
  else if (BUILDER_IS_SOURCE_SVN (self))
rpm-build c487f7
    type = "svn";
rpm-build c487f7
  else
rpm-build c487f7
    g_warning ("Unknown source type");
rpm-build c487f7
rpm-build c487f7
  if (type)
rpm-build c487f7
    json_object_set_string_member (object, "type", type);
rpm-build c487f7
rpm-build c487f7
  return node;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
BuilderSource *
rpm-build c487f7
builder_source_from_json (JsonNode *node)
rpm-build c487f7
{
rpm-build c487f7
  JsonObject *object = json_node_get_object (node);
rpm-build c487f7
  const gchar *type;
rpm-build c487f7
rpm-build c487f7
  type = json_object_get_string_member (object, "type");
rpm-build c487f7
rpm-build c487f7
  if (type == NULL)
rpm-build c487f7
    g_warning ("Missing source type");
rpm-build c487f7
  else if (strcmp (type, "archive") == 0)
rpm-build c487f7
    return (BuilderSource *) json_gobject_deserialize (BUILDER_TYPE_SOURCE_ARCHIVE, node);
rpm-build c487f7
  else if (strcmp (type, "file") == 0)
rpm-build c487f7
    return (BuilderSource *) json_gobject_deserialize (BUILDER_TYPE_SOURCE_FILE, node);
rpm-build c487f7
  else if (strcmp (type, "dir") == 0)
rpm-build c487f7
    return (BuilderSource *) json_gobject_deserialize (BUILDER_TYPE_SOURCE_DIR, node);
rpm-build c487f7
  else if (strcmp (type, "script") == 0)
rpm-build c487f7
    return (BuilderSource *) json_gobject_deserialize (BUILDER_TYPE_SOURCE_SCRIPT, node);
rpm-build c487f7
  else if (strcmp (type, "shell") == 0)
rpm-build c487f7
    return (BuilderSource *) json_gobject_deserialize (BUILDER_TYPE_SOURCE_SHELL, node);
rpm-build c487f7
  else if (strcmp (type, "extra-data") == 0)
rpm-build c487f7
    return (BuilderSource *) json_gobject_deserialize (BUILDER_TYPE_SOURCE_EXTRA_DATA, node);
rpm-build c487f7
  else if (strcmp (type, "patch") == 0)
rpm-build c487f7
    return (BuilderSource *) json_gobject_deserialize (BUILDER_TYPE_SOURCE_PATCH, node);
rpm-build c487f7
  else if (strcmp (type, "git") == 0)
rpm-build c487f7
    return (BuilderSource *) json_gobject_deserialize (BUILDER_TYPE_SOURCE_GIT, node);
rpm-build c487f7
  else if (strcmp (type, "bzr") == 0)
rpm-build c487f7
    return (BuilderSource *) json_gobject_deserialize (BUILDER_TYPE_SOURCE_BZR, node);
rpm-build c487f7
  else if (strcmp (type, "svn") == 0)
rpm-build c487f7
    return (BuilderSource *) json_gobject_deserialize (BUILDER_TYPE_SOURCE_SVN, node);
rpm-build c487f7
  else
rpm-build c487f7
    g_warning ("Unknown source type %s", type);
rpm-build c487f7
rpm-build c487f7
  return NULL;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
gboolean
rpm-build c487f7
builder_source_show_deps (BuilderSource  *self,
rpm-build c487f7
                          GError        **error)
rpm-build c487f7
{
rpm-build c487f7
  BuilderSourceClass *class;
rpm-build c487f7
rpm-build c487f7
  class = BUILDER_SOURCE_GET_CLASS (self);
rpm-build c487f7
rpm-build c487f7
  return class->show_deps (self, error);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
gboolean
rpm-build c487f7
builder_source_download (BuilderSource  *self,
rpm-build c487f7
                         gboolean        update_vcs,
rpm-build c487f7
                         BuilderContext *context,
rpm-build c487f7
                         GError        **error)
rpm-build c487f7
{
rpm-build c487f7
  BuilderSourceClass *class;
rpm-build c487f7
rpm-build c487f7
  class = BUILDER_SOURCE_GET_CLASS (self);
rpm-build c487f7
rpm-build c487f7
  return class->download (self, update_vcs, context, error);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
gboolean
rpm-build c487f7
builder_source_extract (BuilderSource  *self,
rpm-build c487f7
                        GFile          *dest,
rpm-build c487f7
                        BuilderOptions *build_options,
rpm-build c487f7
                        BuilderContext *context,
rpm-build c487f7
                        GError        **error)
rpm-build c487f7
{
rpm-build c487f7
  BuilderSourceClass *class;
rpm-build c487f7
rpm-build c487f7
  g_autoptr(GFile) real_dest = NULL;
rpm-build c487f7
rpm-build c487f7
  class = BUILDER_SOURCE_GET_CLASS (self);
rpm-build c487f7
rpm-build c487f7
  if (self->dest != NULL)
rpm-build c487f7
    {
rpm-build c487f7
      real_dest = g_file_resolve_relative_path (dest, self->dest);
rpm-build c487f7
rpm-build c487f7
      if (!g_file_query_exists (real_dest, NULL) &&
rpm-build c487f7
          !g_file_make_directory_with_parents (real_dest, NULL, error))
rpm-build c487f7
        return FALSE;
rpm-build c487f7
    }
rpm-build c487f7
  else
rpm-build c487f7
    {
rpm-build c487f7
      real_dest = g_object_ref (dest);
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
rpm-build c487f7
  return class->extract (self, real_dest, build_options, context, error);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
gboolean
rpm-build c487f7
builder_source_bundle (BuilderSource  *self,
rpm-build c487f7
                       BuilderContext *context,
rpm-build c487f7
                       GError        **error)
rpm-build c487f7
{
rpm-build c487f7
  BuilderSourceClass *class;
rpm-build c487f7
  class = BUILDER_SOURCE_GET_CLASS (self);
rpm-build c487f7
rpm-build c487f7
  return class->bundle (self, context, error);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
gboolean
rpm-build c487f7
builder_source_update (BuilderSource  *self,
rpm-build c487f7
                       BuilderContext *context,
rpm-build c487f7
                       GError        **error)
rpm-build c487f7
{
rpm-build c487f7
  BuilderSourceClass *class = BUILDER_SOURCE_GET_CLASS (self);
rpm-build c487f7
rpm-build c487f7
  return class->update (self, context, error);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
void
rpm-build c487f7
builder_source_checksum (BuilderSource  *self,
rpm-build c487f7
                         BuilderCache   *cache,
rpm-build c487f7
                         BuilderContext *context)
rpm-build c487f7
{
rpm-build c487f7
  BuilderSourceClass *class;
rpm-build c487f7
rpm-build c487f7
  class = BUILDER_SOURCE_GET_CLASS (self);
rpm-build c487f7
rpm-build c487f7
  builder_cache_checksum_str (cache, self->dest);
rpm-build c487f7
  builder_cache_checksum_strv (cache, self->only_arches);
rpm-build c487f7
  builder_cache_checksum_strv (cache, self->skip_arches);
rpm-build c487f7
rpm-build c487f7
  class->checksum (self, cache, context);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
void
rpm-build c487f7
builder_source_finish (BuilderSource  *self,
rpm-build c487f7
                       GPtrArray      *args,
rpm-build c487f7
                       BuilderContext *context)
rpm-build c487f7
{
rpm-build c487f7
  BuilderSourceClass *class = BUILDER_SOURCE_GET_CLASS (self);
rpm-build c487f7
rpm-build c487f7
  if (class->finish)
rpm-build c487f7
    class->finish (self, args, context);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
gboolean
rpm-build c487f7
builder_source_is_enabled (BuilderSource *self,
rpm-build c487f7
                           BuilderContext *context)
rpm-build c487f7
{
rpm-build c487f7
  if (self->only_arches != NULL &&
rpm-build c487f7
      self->only_arches[0] != NULL &&
rpm-build c487f7
      !g_strv_contains ((const char * const *) self->only_arches, builder_context_get_arch (context)))
rpm-build c487f7
    return FALSE;
rpm-build c487f7
rpm-build c487f7
  if (self->skip_arches != NULL &&
rpm-build c487f7
      g_strv_contains ((const char * const *)self->skip_arches, builder_context_get_arch (context)))
rpm-build c487f7
    return FALSE;
rpm-build c487f7
rpm-build c487f7
  return TRUE;
rpm-build c487f7
}