Blame src/builder-source-dir.c

rpm-build c487f7
/* builder-source-dir.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-dir.h"
rpm-build c487f7
rpm-build c487f7
struct BuilderSourceDir
rpm-build c487f7
{
rpm-build c487f7
  BuilderSource parent;
rpm-build c487f7
rpm-build c487f7
  char         *path;
rpm-build c487f7
  char        **skip;
rpm-build c487f7
};
rpm-build c487f7
rpm-build c487f7
typedef struct
rpm-build c487f7
{
rpm-build c487f7
  BuilderSourceClass parent_class;
rpm-build c487f7
} BuilderSourceDirClass;
rpm-build c487f7
rpm-build c487f7
G_DEFINE_TYPE (BuilderSourceDir, builder_source_dir, BUILDER_TYPE_SOURCE);
rpm-build c487f7
rpm-build c487f7
enum {
rpm-build c487f7
  PROP_0,
rpm-build c487f7
  PROP_PATH,
rpm-build c487f7
  PROP_SKIP,
rpm-build c487f7
  LAST_PROP
rpm-build c487f7
};
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
builder_source_dir_finalize (GObject *object)
rpm-build c487f7
{
rpm-build c487f7
  BuilderSourceDir *self = (BuilderSourceDir *) object;
rpm-build c487f7
rpm-build c487f7
  g_free (self->path);
rpm-build c487f7
  g_strfreev (self->skip);
rpm-build c487f7
rpm-build c487f7
  G_OBJECT_CLASS (builder_source_dir_parent_class)->finalize (object);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
builder_source_dir_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
  BuilderSourceDir *self = BUILDER_SOURCE_DIR (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_SKIP:
rpm-build c487f7
      g_value_set_boxed (value, self->skip);
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_dir_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
  BuilderSourceDir *self = BUILDER_SOURCE_DIR (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_SKIP:
rpm-build c487f7
      tmp = self->skip;
rpm-build c487f7
      self->skip = 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
rpm-build c487f7
static GFile *
rpm-build c487f7
get_source_file (BuilderSourceDir *self,
rpm-build c487f7
                 BuilderContext    *context,
rpm-build c487f7
                 GError           **error)
rpm-build c487f7
{
rpm-build c487f7
  GFile *base_dir = BUILDER_SOURCE (self)->base_dir;
rpm-build c487f7
rpm-build c487f7
  if (self->path != NULL && self->path[0] != 0)
rpm-build c487f7
    {
rpm-build c487f7
      return g_file_resolve_relative_path (base_dir, self->path);
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "source dir path not specified");
rpm-build c487f7
  return NULL;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static gboolean
rpm-build c487f7
builder_source_dir_show_deps (BuilderSource  *source,
rpm-build c487f7
                              GError        **error)
rpm-build c487f7
{
rpm-build c487f7
  BuilderSourceDir *self = BUILDER_SOURCE_DIR (source);
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
  return TRUE;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static gboolean
rpm-build c487f7
builder_source_dir_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
  BuilderSourceDir *self = BUILDER_SOURCE_DIR (source);
rpm-build c487f7
  g_autoptr(GFile) file = NULL;
rpm-build c487f7
rpm-build c487f7
  file = get_source_file (self, context, error);
rpm-build c487f7
  if (file == NULL)
rpm-build c487f7
    return FALSE;
rpm-build c487f7
rpm-build c487f7
  if (g_file_query_file_type (file, G_FILE_QUERY_INFO_NONE, NULL) != G_FILE_TYPE_DIRECTORY)
rpm-build c487f7
    return flatpak_fail (error, "Can't find directory at %s", self->path);
rpm-build c487f7
rpm-build c487f7
  return TRUE;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static GPtrArray *
rpm-build c487f7
builder_source_dir_get_skip (BuilderSource  *source,
rpm-build c487f7
                             BuilderContext *context)
rpm-build c487f7
{
rpm-build c487f7
  BuilderSourceDir *self = BUILDER_SOURCE_DIR (source);
rpm-build c487f7
  g_autoptr(GPtrArray) skip = g_ptr_array_new_with_free_func (g_object_unref);
rpm-build c487f7
  int i;
rpm-build c487f7
rpm-build c487f7
  g_ptr_array_add (skip, g_object_ref (builder_context_get_app_dir_raw (context)));
rpm-build c487f7
  g_ptr_array_add (skip, g_object_ref (builder_context_get_state_dir (context)));
rpm-build c487f7
rpm-build c487f7
  if (self->skip)
rpm-build c487f7
    {
rpm-build c487f7
      g_autoptr(GFile) source_dir = get_source_file (self, context, NULL);
rpm-build c487f7
rpm-build c487f7
      for (i = 0; source_dir != NULL && self->skip[i] != NULL; i++)
rpm-build c487f7
        {
rpm-build c487f7
          GFile *f = g_file_resolve_relative_path (source_dir, self->skip[i]);
rpm-build c487f7
          if (f)
rpm-build c487f7
            g_ptr_array_add (skip, f);
rpm-build c487f7
        }
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  return g_steal_pointer (&skip);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static gboolean
rpm-build c487f7
builder_source_dir_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
  BuilderSourceDir *self = BUILDER_SOURCE_DIR (source);
rpm-build c487f7
  g_autoptr(GFile) src = NULL;
rpm-build c487f7
  g_autoptr(GPtrArray) skip = NULL;
rpm-build c487f7
rpm-build c487f7
  src = get_source_file (self, context, error);
rpm-build c487f7
  if (src == NULL)
rpm-build c487f7
    return FALSE;
rpm-build c487f7
rpm-build c487f7
  skip = builder_source_dir_get_skip (source, context);
rpm-build c487f7
  g_mkdir_with_parents (flatpak_file_get_path_cached (src), 0755);
rpm-build c487f7
  if (!flatpak_cp_a (src, dest,
rpm-build c487f7
                     FLATPAK_CP_FLAGS_MERGE|FLATPAK_CP_FLAGS_NO_CHOWN,
rpm-build c487f7
                     skip, 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 gboolean
rpm-build c487f7
builder_source_dir_bundle (BuilderSource  *source,
rpm-build c487f7
                            BuilderContext *context,
rpm-build c487f7
                            GError        **error)
rpm-build c487f7
{
rpm-build c487f7
  BuilderSourceDir *self = BUILDER_SOURCE_DIR (source);
rpm-build c487f7
  g_autoptr(GFile) src = NULL;
rpm-build c487f7
  g_autoptr(GFile) dest = NULL;
rpm-build c487f7
  GFile *manifest_base_dir = builder_context_get_base_dir (context);
rpm-build c487f7
  g_autofree char *rel_path = NULL;
rpm-build c487f7
  g_autoptr(GPtrArray) skip = NULL;
rpm-build c487f7
rpm-build c487f7
  src = get_source_file (self, context, error);
rpm-build c487f7
  if (src == NULL)
rpm-build c487f7
    return FALSE;
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 ("Local file %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
  dest = flatpak_build_file (builder_context_get_app_dir (context),
rpm-build c487f7
                             "sources/manifest", rel_path, NULL);
rpm-build c487f7
rpm-build c487f7
  skip = builder_source_dir_get_skip (source, context);
rpm-build c487f7
  g_mkdir_with_parents (flatpak_file_get_path_cached (dest), 0755);
rpm-build c487f7
  if (!flatpak_cp_a (src, dest,
rpm-build c487f7
                     FLATPAK_CP_FLAGS_MERGE|FLATPAK_CP_FLAGS_NO_CHOWN,
rpm-build c487f7
                     skip, 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 gboolean
rpm-build c487f7
builder_source_dir_update (BuilderSource  *source,
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_dir_checksum (BuilderSource  *source,
rpm-build c487f7
                              BuilderCache   *cache,
rpm-build c487f7
                              BuilderContext *context)
rpm-build c487f7
{
rpm-build c487f7
  /* We can't realistically checksum a directory, so always rebuild */
rpm-build c487f7
  builder_cache_checksum_random (cache);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
builder_source_dir_class_init (BuilderSourceDirClass *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_dir_finalize;
rpm-build c487f7
  object_class->get_property = builder_source_dir_get_property;
rpm-build c487f7
  object_class->set_property = builder_source_dir_set_property;
rpm-build c487f7
rpm-build c487f7
  source_class->show_deps = builder_source_dir_show_deps;
rpm-build c487f7
  source_class->download = builder_source_dir_download;
rpm-build c487f7
  source_class->extract = builder_source_dir_extract;
rpm-build c487f7
  source_class->bundle = builder_source_dir_bundle;
rpm-build c487f7
  source_class->update = builder_source_dir_update;
rpm-build c487f7
  source_class->checksum = builder_source_dir_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_SKIP,
rpm-build c487f7
                                   g_param_spec_boxed ("skip",
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_dir_init (BuilderSourceDir *self)
rpm-build c487f7
{
rpm-build c487f7
}