Blame src/builder-source-patch.c

rpm-build c487f7
/* builder-source-patch.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-flatpak-utils.h"
rpm-build c487f7
rpm-build c487f7
#include "builder-utils.h"
rpm-build c487f7
#include "builder-source-patch.h"
rpm-build c487f7
rpm-build c487f7
struct BuilderSourcePatch
rpm-build c487f7
{
rpm-build c487f7
  BuilderSource parent;
rpm-build c487f7
rpm-build c487f7
  char         *path;
rpm-build c487f7
  char        **paths;
rpm-build c487f7
  guint         strip_components;
rpm-build c487f7
  gboolean      use_git;
rpm-build c487f7
  gboolean      use_git_am;
rpm-build c487f7
  char        **options;
rpm-build c487f7
};
rpm-build c487f7
rpm-build c487f7
typedef struct
rpm-build c487f7
{
rpm-build c487f7
  BuilderSourceClass parent_class;
rpm-build c487f7
} BuilderSourcePatchClass;
rpm-build c487f7
rpm-build c487f7
G_DEFINE_TYPE (BuilderSourcePatch, builder_source_patch, BUILDER_TYPE_SOURCE);
rpm-build c487f7
rpm-build c487f7
enum {
rpm-build c487f7
  PROP_0,
rpm-build c487f7
  PROP_PATH,
rpm-build c487f7
  PROP_PATHS,
rpm-build c487f7
  PROP_STRIP_COMPONENTS,
rpm-build c487f7
  PROP_USE_GIT,
rpm-build c487f7
  PROP_OPTIONS,
rpm-build c487f7
  PROP_USE_GIT_AM,
rpm-build c487f7
  LAST_PROP
rpm-build c487f7
};
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
builder_source_patch_finalize (GObject *object)
rpm-build c487f7
{
rpm-build c487f7
  BuilderSourcePatch *self = (BuilderSourcePatch *) object;
rpm-build c487f7
rpm-build c487f7
  g_free (self->path);
rpm-build c487f7
  g_strfreev (self->paths);
rpm-build c487f7
  g_strfreev (self->options);
rpm-build c487f7
rpm-build c487f7
  G_OBJECT_CLASS (builder_source_patch_parent_class)->finalize (object);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
builder_source_patch_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
  BuilderSourcePatch *self = BUILDER_SOURCE_PATCH (object);
rpm-build c487f7
rpm-build c487f7
  switch (prop_id)
rpm-build c487f7
    {
rpm-build c487f7
    case PROP_PATH:
rpm-build c487f7
      g_value_set_string (value, self->path);
rpm-build c487f7
      break;
rpm-build c487f7
rpm-build c487f7
    case PROP_PATHS:
rpm-build c487f7
      g_value_set_boxed (value, self->paths);
rpm-build c487f7
      break;
rpm-build c487f7
rpm-build c487f7
    case PROP_STRIP_COMPONENTS:
rpm-build c487f7
      g_value_set_uint (value, self->strip_components);
rpm-build c487f7
      break;
rpm-build c487f7
rpm-build c487f7
    case PROP_USE_GIT:
rpm-build c487f7
      g_value_set_boolean (value, self->use_git);
rpm-build c487f7
      break;
rpm-build c487f7
rpm-build c487f7
    case PROP_OPTIONS:
rpm-build c487f7
      g_value_set_boxed (value, self->options);
rpm-build c487f7
      break;
rpm-build c487f7
rpm-build c487f7
    case PROP_USE_GIT_AM:
rpm-build c487f7
      g_value_set_boolean (value, self->use_git_am);
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_patch_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
  BuilderSourcePatch *self = BUILDER_SOURCE_PATCH (object);
rpm-build c487f7
  gchar **tmp;
rpm-build c487f7
rpm-build c487f7
  switch (prop_id)
rpm-build c487f7
    {
rpm-build c487f7
    case PROP_PATH:
rpm-build c487f7
      g_free (self->path);
rpm-build c487f7
      self->path = g_value_dup_string (value);
rpm-build c487f7
      break;
rpm-build c487f7
rpm-build c487f7
    case PROP_PATHS:
rpm-build c487f7
      tmp = self->paths;
rpm-build c487f7
      self->paths = 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_STRIP_COMPONENTS:
rpm-build c487f7
      self->strip_components = g_value_get_uint (value);
rpm-build c487f7
      break;
rpm-build c487f7
rpm-build c487f7
    case PROP_USE_GIT:
rpm-build c487f7
      self->use_git = g_value_get_boolean (value);
rpm-build c487f7
      break;
rpm-build c487f7
rpm-build c487f7
    case PROP_OPTIONS:
rpm-build c487f7
      tmp = self->options;
rpm-build c487f7
      self->options = 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_USE_GIT_AM:
rpm-build c487f7
      self->use_git_am = g_value_get_boolean (value);
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 GPtrArray *
rpm-build c487f7
get_source_files (BuilderSourcePatch *self,
rpm-build c487f7
                 BuilderContext     *context,
rpm-build c487f7
                 GError            **error)
rpm-build c487f7
{
rpm-build c487f7
  g_autoptr(GPtrArray) res = g_ptr_array_new_with_free_func (g_object_unref);
rpm-build c487f7
  GFile *base_dir = BUILDER_SOURCE (self)->base_dir;
rpm-build c487f7
  int i;
rpm-build c487f7
rpm-build c487f7
  if ((self->path != NULL && self->path[0] != 0))
rpm-build c487f7
    g_ptr_array_add (res, g_file_resolve_relative_path (base_dir, self->path));
rpm-build c487f7
rpm-build c487f7
  if (self->paths != NULL)
rpm-build c487f7
    {
rpm-build c487f7
      for (i = 0; self->paths[i] != NULL; i++)
rpm-build c487f7
        g_ptr_array_add (res, g_file_resolve_relative_path (base_dir, self->paths[i]));
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  if (res->len == 0)
rpm-build c487f7
    {
rpm-build c487f7
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "path not specified");
rpm-build c487f7
      return NULL;
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  return g_steal_pointer (&res;;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static gboolean
rpm-build c487f7
builder_source_patch_show_deps (BuilderSource  *source,
rpm-build c487f7
                                GError        **error)
rpm-build c487f7
{
rpm-build c487f7
  BuilderSourcePatch *self = BUILDER_SOURCE_PATCH (source);
rpm-build c487f7
  int i;
rpm-build c487f7
rpm-build c487f7
  if (self->path && self->path[0] != 0)
rpm-build c487f7
    g_print ("%s\n", self->path);
rpm-build c487f7
rpm-build c487f7
  for (i = 0; self->paths != NULL && self->paths[i] != NULL; i++)
rpm-build c487f7
    g_print ("%s\n", self->paths[i]);
rpm-build c487f7
rpm-build c487f7
  return TRUE;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static gboolean
rpm-build c487f7
builder_source_patch_download (BuilderSource  *source,
rpm-build c487f7
                               gboolean        update_vcs,
rpm-build c487f7
                               BuilderContext *context,
rpm-build c487f7
                               GError        **error)
rpm-build c487f7
{
rpm-build c487f7
  BuilderSourcePatch *self = BUILDER_SOURCE_PATCH (source);
rpm-build c487f7
  g_autoptr(GPtrArray) srcs = NULL;
rpm-build c487f7
  int i;
rpm-build c487f7
rpm-build c487f7
  srcs = get_source_files (self, context, error);
rpm-build c487f7
  if (srcs == NULL)
rpm-build c487f7
    return FALSE;
rpm-build c487f7
rpm-build c487f7
  for (i = 0; i < srcs->len; i++)
rpm-build c487f7
    {
rpm-build c487f7
      GFile *src = g_ptr_array_index (srcs, i);
rpm-build c487f7
      if (!g_file_query_exists (src, NULL))
rpm-build c487f7
        {
rpm-build c487f7
          g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Can't find file at %s", self->path);
rpm-build c487f7
          return FALSE;
rpm-build c487f7
        }
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  return TRUE;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static gboolean
rpm-build c487f7
patch (GFile      *dir,
rpm-build c487f7
       gboolean    use_git,
rpm-build c487f7
       gboolean    use_git_am,
rpm-build c487f7
       const char *patch_path,
rpm-build c487f7
       char      **extra_options,
rpm-build c487f7
       GError    **error,
rpm-build c487f7
       ...)
rpm-build c487f7
{
rpm-build c487f7
  gboolean res;
rpm-build c487f7
  GPtrArray *args;
rpm-build c487f7
  const gchar *arg;
rpm-build c487f7
  va_list ap;
rpm-build c487f7
  int i;
rpm-build c487f7
rpm-build c487f7
  va_start(ap, error);
rpm-build c487f7
rpm-build c487f7
  args = g_ptr_array_new ();
rpm-build c487f7
  if (use_git) {
rpm-build c487f7
    g_ptr_array_add (args, "git");
rpm-build c487f7
    g_ptr_array_add (args, "apply");
rpm-build c487f7
    g_ptr_array_add (args, "-v");
rpm-build c487f7
  } else if (use_git_am) {
rpm-build c487f7
    g_ptr_array_add (args, "git");
rpm-build c487f7
    g_ptr_array_add (args, "am");
rpm-build c487f7
    g_ptr_array_add (args, "--keep-cr");
rpm-build c487f7
  } else {
rpm-build c487f7
    g_ptr_array_add (args, "patch");
rpm-build c487f7
  }
rpm-build c487f7
  for (i = 0; extra_options != NULL && extra_options[i] != NULL; i++)
rpm-build c487f7
    g_ptr_array_add (args, (gchar *) extra_options[i]);
rpm-build c487f7
  while ((arg = va_arg (ap, const gchar *)))
rpm-build c487f7
    g_ptr_array_add (args, (gchar *) arg);
rpm-build c487f7
  if (use_git || use_git_am) {
rpm-build c487f7
    g_ptr_array_add (args, (char *) patch_path);
rpm-build c487f7
  } else {
rpm-build c487f7
    g_ptr_array_add (args, "-i");
rpm-build c487f7
    g_ptr_array_add (args, (char *) patch_path);
rpm-build c487f7
  }
rpm-build c487f7
  g_ptr_array_add (args, NULL);
rpm-build c487f7
rpm-build c487f7
  res = flatpak_spawnv (dir, NULL, 0, error, (const char **) args->pdata);
rpm-build c487f7
rpm-build c487f7
  g_ptr_array_free (args, TRUE);
rpm-build c487f7
rpm-build c487f7
  va_end (ap);
rpm-build c487f7
rpm-build c487f7
  return res;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static gboolean
rpm-build c487f7
builder_source_patch_extract (BuilderSource  *source,
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
  BuilderSourcePatch *self = BUILDER_SOURCE_PATCH (source);
rpm-build c487f7
  g_autofree char *strip_components = NULL;
rpm-build c487f7
  g_autoptr(GPtrArray) srcs = NULL;
rpm-build c487f7
  int i;
rpm-build c487f7
rpm-build c487f7
  if (self->use_git && self->use_git_am)
rpm-build c487f7
    {
rpm-build c487f7
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
rpm-build c487f7
                   "Patch '%s' cannot be applied: Both 'use-git' and 'use-git-am' are set. Only one can be set at a time",
rpm-build c487f7
                   self->path);
rpm-build c487f7
      return FALSE;
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  srcs = get_source_files (self, context, error);
rpm-build c487f7
  if (srcs == NULL)
rpm-build c487f7
    return FALSE;
rpm-build c487f7
rpm-build c487f7
  strip_components = g_strdup_printf ("-p%u", self->strip_components);
rpm-build c487f7
rpm-build c487f7
  for (i = 0; i < srcs->len; i++)
rpm-build c487f7
    {
rpm-build c487f7
      GFile *patchfile = g_ptr_array_index (srcs, i);
rpm-build c487f7
      g_autofree char *basename = g_file_get_basename (patchfile);
rpm-build c487f7
      g_autofree char *patch_path = g_file_get_path (patchfile);
rpm-build c487f7
rpm-build c487f7
      g_print ("Applying patch %s\n", basename);
rpm-build c487f7
      if (!patch (dest, self->use_git, self->use_git_am, patch_path, self->options, error, strip_components, NULL))
rpm-build c487f7
        return FALSE;
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  return TRUE;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static gboolean
rpm-build c487f7
builder_source_patch_bundle (BuilderSource  *source,
rpm-build c487f7
                             BuilderContext *context,
rpm-build c487f7
                             GError        **error)
rpm-build c487f7
{
rpm-build c487f7
  BuilderSourcePatch *self = BUILDER_SOURCE_PATCH (source);
rpm-build c487f7
  GFile *manifest_base_dir = builder_context_get_base_dir (context);
rpm-build c487f7
  g_autoptr(GPtrArray) srcs = NULL;
rpm-build c487f7
  int i;
rpm-build c487f7
rpm-build c487f7
  srcs = get_source_files (self, context, error);
rpm-build c487f7
  if (srcs == NULL)
rpm-build c487f7
    return FALSE;
rpm-build c487f7
rpm-build c487f7
  for (i = 0; i < srcs->len; i++)
rpm-build c487f7
    {
rpm-build c487f7
      GFile *src = g_ptr_array_index (srcs, i);
rpm-build c487f7
      g_autofree char *rel_path = NULL;
rpm-build c487f7
      g_autoptr(GFile) destination_file = NULL;
rpm-build c487f7
      g_autoptr(GFile) destination_dir = NULL;
rpm-build c487f7
rpm-build c487f7
      rel_path = g_file_get_relative_path (manifest_base_dir, src);
rpm-build c487f7
      if (rel_path == NULL)
rpm-build c487f7
        {
rpm-build c487f7
          g_warning ("Patch %s is outside manifest tree, not bundling", flatpak_file_get_path_cached (src));
rpm-build c487f7
          return TRUE;
rpm-build c487f7
        }
rpm-build c487f7
rpm-build c487f7
      destination_file = flatpak_build_file (builder_context_get_app_dir (context),
rpm-build c487f7
                                             "sources/manifest", rel_path, NULL);
rpm-build c487f7
rpm-build c487f7
      destination_dir = g_file_get_parent (destination_file);
rpm-build c487f7
      if (!flatpak_mkdir_p (destination_dir, NULL, error))
rpm-build c487f7
        return FALSE;
rpm-build c487f7
rpm-build c487f7
      if (!g_file_copy (src, destination_file,
rpm-build c487f7
                        G_FILE_COPY_OVERWRITE,
rpm-build c487f7
                        NULL,
rpm-build c487f7
                        NULL, NULL,
rpm-build c487f7
                        error))
rpm-build c487f7
        return FALSE;
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  return TRUE;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
builder_source_patch_checksum (BuilderSource  *source,
rpm-build c487f7
                               BuilderCache   *cache,
rpm-build c487f7
                               BuilderContext *context)
rpm-build c487f7
{
rpm-build c487f7
  BuilderSourcePatch *self = BUILDER_SOURCE_PATCH (source);
rpm-build c487f7
  g_autoptr(GPtrArray) srcs = NULL;
rpm-build c487f7
  int i;
rpm-build c487f7
rpm-build c487f7
  srcs = get_source_files (self, context, NULL);
rpm-build c487f7
rpm-build c487f7
  for (i = 0; srcs != NULL && i < srcs->len; i++)
rpm-build c487f7
    {
rpm-build c487f7
      GFile *src = g_ptr_array_index (srcs, i);
rpm-build c487f7
      g_autofree char *data = NULL;
rpm-build c487f7
      gsize len;
rpm-build c487f7
rpm-build c487f7
      if (g_file_load_contents (src, NULL, &data, &len, NULL, NULL))
rpm-build c487f7
        builder_cache_checksum_data (cache, (guchar *) data, len);
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  builder_cache_checksum_str (cache, self->path);
rpm-build c487f7
  builder_cache_checksum_compat_strv (cache, self->paths);
rpm-build c487f7
  builder_cache_checksum_uint32 (cache, self->strip_components);
rpm-build c487f7
  builder_cache_checksum_strv (cache, self->options);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
builder_source_patch_class_init (BuilderSourcePatchClass *klass)
rpm-build c487f7
{
rpm-build c487f7
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
rpm-build c487f7
  BuilderSourceClass *source_class = BUILDER_SOURCE_CLASS (klass);
rpm-build c487f7
rpm-build c487f7
  object_class->finalize = builder_source_patch_finalize;
rpm-build c487f7
  object_class->get_property = builder_source_patch_get_property;
rpm-build c487f7
  object_class->set_property = builder_source_patch_set_property;
rpm-build c487f7
rpm-build c487f7
  source_class->show_deps = builder_source_patch_show_deps;
rpm-build c487f7
  source_class->download = builder_source_patch_download;
rpm-build c487f7
  source_class->extract = builder_source_patch_extract;
rpm-build c487f7
  source_class->bundle = builder_source_patch_bundle;
rpm-build c487f7
  source_class->checksum = builder_source_patch_checksum;
rpm-build c487f7
rpm-build c487f7
  g_object_class_install_property (object_class,
rpm-build c487f7
                                   PROP_PATH,
rpm-build c487f7
                                   g_param_spec_string ("path",
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_PATHS,
rpm-build c487f7
                                   g_param_spec_boxed ("paths",
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_STRIP_COMPONENTS,
rpm-build c487f7
                                   g_param_spec_uint ("strip-components",
rpm-build c487f7
                                                      "",
rpm-build c487f7
                                                      "",
rpm-build c487f7
                                                      0, G_MAXUINT,
rpm-build c487f7
                                                      1,
rpm-build c487f7
                                                      G_PARAM_READWRITE));
rpm-build c487f7
  g_object_class_install_property (object_class,
rpm-build c487f7
                                   PROP_USE_GIT,
rpm-build c487f7
                                   g_param_spec_boolean ("use-git",
rpm-build c487f7
                                                         "",
rpm-build c487f7
                                                         "",
rpm-build c487f7
                                                         FALSE,
rpm-build c487f7
                                                         G_PARAM_READWRITE));
rpm-build c487f7
  g_object_class_install_property (object_class,
rpm-build c487f7
                                   PROP_OPTIONS,
rpm-build c487f7
                                   g_param_spec_boxed ("options",
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_USE_GIT_AM,
rpm-build c487f7
                                   g_param_spec_boolean ("use-git-am",
rpm-build c487f7
                                                         "",
rpm-build c487f7
                                                         "",
rpm-build c487f7
                                                         FALSE,
rpm-build c487f7
                                                         G_PARAM_READWRITE));
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
builder_source_patch_init (BuilderSourcePatch *self)
rpm-build c487f7
{
rpm-build c487f7
  self->strip_components = 1;
rpm-build c487f7
}