Blame src/builder-source-bzr.c

rpm-build c487f7
/* builder-source-bzr.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
rpm-build c487f7
#include "builder-source-bzr.h"
rpm-build c487f7
#include "builder-utils.h"
rpm-build c487f7
#include "builder-flatpak-utils.h"
rpm-build c487f7
rpm-build c487f7
struct BuilderSourceBzr
rpm-build c487f7
{
rpm-build c487f7
  BuilderSource parent;
rpm-build c487f7
rpm-build c487f7
  char         *url;
rpm-build c487f7
  char         *revision;
rpm-build c487f7
};
rpm-build c487f7
rpm-build c487f7
typedef struct
rpm-build c487f7
{
rpm-build c487f7
  BuilderSourceClass parent_class;
rpm-build c487f7
} BuilderSourceBzrClass;
rpm-build c487f7
rpm-build c487f7
G_DEFINE_TYPE (BuilderSourceBzr, builder_source_bzr, BUILDER_TYPE_SOURCE);
rpm-build c487f7
rpm-build c487f7
enum {
rpm-build c487f7
  PROP_0,
rpm-build c487f7
  PROP_URL,
rpm-build c487f7
  PROP_REVISION,
rpm-build c487f7
  LAST_PROP
rpm-build c487f7
};
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
builder_source_bzr_finalize (GObject *object)
rpm-build c487f7
{
rpm-build c487f7
  BuilderSourceBzr *self = (BuilderSourceBzr *) object;
rpm-build c487f7
rpm-build c487f7
  g_free (self->url);
rpm-build c487f7
  g_free (self->revision);
rpm-build c487f7
rpm-build c487f7
  G_OBJECT_CLASS (builder_source_bzr_parent_class)->finalize (object);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
builder_source_bzr_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
  BuilderSourceBzr *self = BUILDER_SOURCE_BZR (object);
rpm-build c487f7
rpm-build c487f7
  switch (prop_id)
rpm-build c487f7
    {
rpm-build c487f7
    case PROP_URL:
rpm-build c487f7
      g_value_set_string (value, self->url);
rpm-build c487f7
      break;
rpm-build c487f7
rpm-build c487f7
    case PROP_REVISION:
rpm-build c487f7
      g_value_set_string (value, self->revision);
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_bzr_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
  BuilderSourceBzr *self = BUILDER_SOURCE_BZR (object);
rpm-build c487f7
rpm-build c487f7
  switch (prop_id)
rpm-build c487f7
    {
rpm-build c487f7
    case PROP_URL:
rpm-build c487f7
      g_free (self->url);
rpm-build c487f7
      self->url = g_value_dup_string (value);
rpm-build c487f7
      break;
rpm-build c487f7
rpm-build c487f7
    case PROP_REVISION:
rpm-build c487f7
      g_free (self->revision);
rpm-build c487f7
      self->revision = g_value_dup_string (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 gboolean
rpm-build c487f7
bzr (GFile   *dir,
rpm-build c487f7
     char   **output,
rpm-build c487f7
     GError **error,
rpm-build c487f7
     ...)
rpm-build c487f7
{
rpm-build c487f7
  gboolean res;
rpm-build c487f7
  va_list ap;
rpm-build c487f7
rpm-build c487f7
  va_start (ap, error);
rpm-build c487f7
  res = flatpak_spawn (dir, output, 0, error, "bzr", ap);
rpm-build c487f7
  va_end (ap);
rpm-build c487f7
rpm-build c487f7
  return res;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static GFile *
rpm-build c487f7
get_mirror_dir (BuilderSourceBzr *self, BuilderContext *context)
rpm-build c487f7
{
rpm-build c487f7
  g_autoptr(GFile) bzr_dir = NULL;
rpm-build c487f7
  g_autofree char *filename = NULL;
rpm-build c487f7
  g_autofree char *bzr_dir_path = NULL;
rpm-build c487f7
rpm-build c487f7
  bzr_dir = g_file_get_child (builder_context_get_state_dir (context),
rpm-build c487f7
                              "bzr");
rpm-build c487f7
rpm-build c487f7
  bzr_dir_path = g_file_get_path (bzr_dir);
rpm-build c487f7
  g_mkdir_with_parents (bzr_dir_path, 0755);
rpm-build c487f7
rpm-build c487f7
  filename = builder_uri_to_filename (self->url);
rpm-build c487f7
  return g_file_get_child (bzr_dir, filename);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static char *
rpm-build c487f7
get_current_commit (BuilderSourceBzr *self, BuilderContext *context, GError **error)
rpm-build c487f7
{
rpm-build c487f7
  g_autoptr(GFile) mirror_dir = NULL;
rpm-build c487f7
  char *output = NULL;
rpm-build c487f7
rpm-build c487f7
  mirror_dir = get_mirror_dir (self, context);
rpm-build c487f7
rpm-build c487f7
  if (!bzr (mirror_dir, &output, error,
rpm-build c487f7
            "revno", NULL))
rpm-build c487f7
    return NULL;
rpm-build c487f7
rpm-build c487f7
  return output;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static gboolean
rpm-build c487f7
builder_source_bzr_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
  BuilderSourceBzr *self = BUILDER_SOURCE_BZR (source);
rpm-build c487f7
rpm-build c487f7
  g_autoptr(GFile) mirror_dir = NULL;
rpm-build c487f7
rpm-build c487f7
  if (self->url == NULL)
rpm-build c487f7
    {
rpm-build c487f7
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "URL not specified");
rpm-build c487f7
      return FALSE;
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  mirror_dir = get_mirror_dir (self, context);
rpm-build c487f7
rpm-build c487f7
  if (!g_file_query_exists (mirror_dir, NULL))
rpm-build c487f7
    {
rpm-build c487f7
      g_autofree char *filename = g_file_get_basename (mirror_dir);
rpm-build c487f7
      g_autoptr(GFile) parent = g_file_get_parent (mirror_dir);
rpm-build c487f7
      g_autofree char *mirror_path = g_file_get_path (mirror_dir);
rpm-build c487f7
      g_autofree char *path_tmp = g_strconcat (mirror_path, ".clone_XXXXXX", NULL);
rpm-build c487f7
      g_autofree char *filename_tmp = NULL;
rpm-build c487f7
      g_autoptr(GFile) mirror_dir_tmp = NULL;
rpm-build c487f7
      g_autoptr(GFile) cached_bzr_dir = NULL;
rpm-build c487f7
      const char *branch_source;
rpm-build c487f7
rpm-build c487f7
      g_print ("Getting bzr repo %s\n", self->url);
rpm-build c487f7
rpm-build c487f7
      if (g_mkdtemp_full (path_tmp, 0755) == NULL)
rpm-build c487f7
        return flatpak_fail (error, "Can't create temporary directory");
rpm-build c487f7
rpm-build c487f7
      /* bzr can't check out to the empty dir, so remove it */
rpm-build c487f7
      rmdir (path_tmp);
rpm-build c487f7
rpm-build c487f7
      mirror_dir_tmp = g_file_new_for_path (path_tmp);
rpm-build c487f7
      filename_tmp = g_file_get_basename (mirror_dir_tmp);
rpm-build c487f7
rpm-build c487f7
      branch_source = self->url;
rpm-build c487f7
rpm-build c487f7
      cached_bzr_dir = builder_context_find_in_sources_dirs (context, "bzr", filename, NULL);
rpm-build c487f7
      if (cached_bzr_dir != NULL)
rpm-build c487f7
        branch_source = flatpak_file_get_path_cached (cached_bzr_dir);
rpm-build c487f7
rpm-build c487f7
      if (!bzr (parent, NULL, error,
rpm-build c487f7
                "branch", branch_source,  filename_tmp, NULL))
rpm-build c487f7
        return FALSE;
rpm-build c487f7
rpm-build c487f7
      /* Rewrite to real url if we used the cache */
rpm-build c487f7
      if (cached_bzr_dir != NULL)
rpm-build c487f7
        {
rpm-build c487f7
          g_autofree char *setting = g_strdup_printf ("parent_location=%s", self->url);
rpm-build c487f7
          if (!bzr (mirror_dir_tmp, NULL, error,
rpm-build c487f7
                    "config", setting, NULL))
rpm-build c487f7
            return FALSE;
rpm-build c487f7
rpm-build c487f7
          if (update_vcs &&
rpm-build c487f7
              !bzr (mirror_dir_tmp, NULL, error,
rpm-build c487f7
                    "pull", NULL))
rpm-build c487f7
            return FALSE;
rpm-build c487f7
        }
rpm-build c487f7
rpm-build c487f7
      if (!g_file_move (mirror_dir_tmp, mirror_dir, 0, NULL, NULL, NULL, error))
rpm-build c487f7
        return FALSE;
rpm-build c487f7
    }
rpm-build c487f7
  else if (update_vcs)
rpm-build c487f7
    {
rpm-build c487f7
      g_print ("Updating bzr repo %s\n", self->url);
rpm-build c487f7
rpm-build c487f7
      if (!bzr (mirror_dir, NULL, error,
rpm-build c487f7
                "pull", 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_bzr_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
  BuilderSourceBzr *self = BUILDER_SOURCE_BZR (source);
rpm-build c487f7
rpm-build c487f7
  g_autoptr(GFile) mirror_dir = NULL;
rpm-build c487f7
  g_autofree char *mirror_dir_path = NULL;
rpm-build c487f7
  g_autofree char *dest_path = NULL;
rpm-build c487f7
rpm-build c487f7
  mirror_dir = get_mirror_dir (self, context);
rpm-build c487f7
rpm-build c487f7
  mirror_dir_path = g_file_get_path (mirror_dir);
rpm-build c487f7
  dest_path = g_file_get_path (dest);
rpm-build c487f7
rpm-build c487f7
  if (!bzr (NULL, NULL, error,
rpm-build c487f7
            "branch", "--stacked", mirror_dir_path, dest_path, "--use-existing-dir", NULL))
rpm-build c487f7
    return FALSE;
rpm-build c487f7
rpm-build c487f7
  if (self->revision)
rpm-build c487f7
    {
rpm-build c487f7
      g_autofree char *revarg = g_strdup_printf ("-r%s", self->revision);
rpm-build c487f7
      if (!bzr (dest, NULL, error,
rpm-build c487f7
                "revert", revarg, 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_bzr_bundle (BuilderSource  *source,
rpm-build c487f7
                           BuilderContext *context,
rpm-build c487f7
                           GError        **error)
rpm-build c487f7
{
rpm-build c487f7
  BuilderSourceBzr *self = BUILDER_SOURCE_BZR (source);
rpm-build c487f7
rpm-build c487f7
  g_autoptr(GFile) sources_dir = NULL;
rpm-build c487f7
  g_autoptr(GFile) dest_dir = NULL;
rpm-build c487f7
  g_autoptr(GFile) dest_dir_tmp = NULL;
rpm-build c487f7
  g_autoptr(GFile) bzr_sources_dir = NULL;
rpm-build c487f7
rpm-build c487f7
  g_autofree char *sources_dir_path = NULL;
rpm-build c487f7
rpm-build c487f7
  g_autofree char *base_name = NULL;
rpm-build c487f7
  g_autofree char *base_name_tmp = NULL;
rpm-build c487f7
rpm-build c487f7
  sources_dir = get_mirror_dir (self, context);
rpm-build c487f7
rpm-build c487f7
  if (sources_dir == NULL) {
rpm-build c487f7
    g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Can't locate repo with URL '%s'", self->url);
rpm-build c487f7
    return FALSE;
rpm-build c487f7
  }
rpm-build c487f7
rpm-build c487f7
  base_name = g_file_get_basename (sources_dir);
rpm-build c487f7
rpm-build c487f7
  bzr_sources_dir = flatpak_build_file (builder_context_get_app_dir (context),
rpm-build c487f7
                                        "sources/bzr",
rpm-build c487f7
                                        NULL);
rpm-build c487f7
  if (!flatpak_mkdir_p (bzr_sources_dir, NULL, error))
rpm-build c487f7
    return FALSE;
rpm-build c487f7
rpm-build c487f7
  base_name_tmp = g_strconcat (base_name, ".clone_tmp", NULL);
rpm-build c487f7
  dest_dir_tmp = flatpak_build_file (bzr_sources_dir,
rpm-build c487f7
                                          base_name_tmp,
rpm-build c487f7
                                          NULL);
rpm-build c487f7
  dest_dir = flatpak_build_file (bzr_sources_dir,
rpm-build c487f7
                                 base_name,
rpm-build c487f7
                                 NULL);
rpm-build c487f7
rpm-build c487f7
  sources_dir_path = g_file_get_path (sources_dir);
rpm-build c487f7
  if (!bzr (bzr_sources_dir, NULL, error,
rpm-build c487f7
            "branch", sources_dir_path, base_name_tmp, NULL) ||
rpm-build c487f7
      !g_file_move (dest_dir_tmp, dest_dir, 0, NULL, NULL, NULL, error))
rpm-build c487f7
    return FALSE;
rpm-build c487f7
rpm-build c487f7
  return TRUE;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
builder_source_bzr_checksum (BuilderSource  *source,
rpm-build c487f7
                             BuilderCache   *cache,
rpm-build c487f7
                             BuilderContext *context)
rpm-build c487f7
{
rpm-build c487f7
  BuilderSourceBzr *self = BUILDER_SOURCE_BZR (source);
rpm-build c487f7
  g_autofree char *current_commit = NULL;
rpm-build c487f7
rpm-build c487f7
  g_autoptr(GError) error = NULL;
rpm-build c487f7
rpm-build c487f7
  builder_cache_checksum_str (cache, self->url);
rpm-build c487f7
  builder_cache_checksum_str (cache, self->revision);
rpm-build c487f7
rpm-build c487f7
  current_commit = get_current_commit (self, context, &error);
rpm-build c487f7
  if (current_commit)
rpm-build c487f7
    builder_cache_checksum_str (cache, current_commit);
rpm-build c487f7
  else if (error)
rpm-build c487f7
    g_warning ("Failed to get current bzr checksum: %s", error->message);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static gboolean
rpm-build c487f7
builder_source_bzr_update (BuilderSource  *source,
rpm-build c487f7
                           BuilderContext *context,
rpm-build c487f7
                           GError        **error)
rpm-build c487f7
{
rpm-build c487f7
  BuilderSourceBzr *self = BUILDER_SOURCE_BZR (source);
rpm-build c487f7
  char *current_commit;
rpm-build c487f7
rpm-build c487f7
  current_commit = get_current_commit (self, context, NULL);
rpm-build c487f7
  if (current_commit)
rpm-build c487f7
    {
rpm-build c487f7
      g_free (self->revision);
rpm-build c487f7
      self->revision = current_commit;
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_bzr_class_init (BuilderSourceBzrClass *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_bzr_finalize;
rpm-build c487f7
  object_class->get_property = builder_source_bzr_get_property;
rpm-build c487f7
  object_class->set_property = builder_source_bzr_set_property;
rpm-build c487f7
rpm-build c487f7
  source_class->download = builder_source_bzr_download;
rpm-build c487f7
  source_class->extract = builder_source_bzr_extract;
rpm-build c487f7
  source_class->bundle = builder_source_bzr_bundle;
rpm-build c487f7
  source_class->update = builder_source_bzr_update;
rpm-build c487f7
  source_class->checksum = builder_source_bzr_checksum;
rpm-build c487f7
rpm-build c487f7
  g_object_class_install_property (object_class,
rpm-build c487f7
                                   PROP_URL,
rpm-build c487f7
                                   g_param_spec_string ("url",
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_REVISION,
rpm-build c487f7
                                   g_param_spec_string ("revision",
rpm-build c487f7
                                                        "",
rpm-build c487f7
                                                        "",
rpm-build c487f7
                                                        NULL,
rpm-build c487f7
                                                        G_PARAM_READWRITE));
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
builder_source_bzr_init (BuilderSourceBzr *self)
rpm-build c487f7
{
rpm-build c487f7
}