Blame src/builder-source-script.c

rpm-build c487f7
/* builder-source-script.c
rpm-build c487f7
 *
rpm-build c487f7
 * Copyright (C) 2015 Red Hat, Inc
rpm-build c487f7
 *
rpm-build c487f7
 * This script 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 script 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
#include "builder-utils.h"
rpm-build c487f7
#include "builder-source-script.h"
rpm-build c487f7
rpm-build c487f7
struct BuilderSourceScript
rpm-build c487f7
{
rpm-build c487f7
  BuilderSource parent;
rpm-build c487f7
rpm-build c487f7
  char        **commands;
rpm-build c487f7
  char         *dest_filename;
rpm-build c487f7
};
rpm-build c487f7
rpm-build c487f7
typedef struct
rpm-build c487f7
{
rpm-build c487f7
  BuilderSourceClass parent_class;
rpm-build c487f7
} BuilderSourceScriptClass;
rpm-build c487f7
rpm-build c487f7
G_DEFINE_TYPE (BuilderSourceScript, builder_source_script, BUILDER_TYPE_SOURCE);
rpm-build c487f7
rpm-build c487f7
enum {
rpm-build c487f7
  PROP_0,
rpm-build c487f7
  PROP_COMMANDS,
rpm-build c487f7
  PROP_DEST_FILENAME,
rpm-build c487f7
  LAST_PROP
rpm-build c487f7
};
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
builder_source_script_finalize (GObject *object)
rpm-build c487f7
{
rpm-build c487f7
  BuilderSourceScript *self = (BuilderSourceScript *) object;
rpm-build c487f7
rpm-build c487f7
  g_strfreev (self->commands);
rpm-build c487f7
  g_free (self->dest_filename);
rpm-build c487f7
rpm-build c487f7
  G_OBJECT_CLASS (builder_source_script_parent_class)->finalize (object);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
builder_source_script_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
  BuilderSourceScript *self = BUILDER_SOURCE_SCRIPT (object);
rpm-build c487f7
rpm-build c487f7
  switch (prop_id)
rpm-build c487f7
    {
rpm-build c487f7
    case PROP_COMMANDS:
rpm-build c487f7
      g_value_set_boxed (value, self->commands);
rpm-build c487f7
      break;
rpm-build c487f7
rpm-build c487f7
    case PROP_DEST_FILENAME:
rpm-build c487f7
      g_value_set_string (value, self->dest_filename);
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_script_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
  BuilderSourceScript *self = BUILDER_SOURCE_SCRIPT (object);
rpm-build c487f7
  gchar **tmp;
rpm-build c487f7
rpm-build c487f7
  switch (prop_id)
rpm-build c487f7
    {
rpm-build c487f7
    case PROP_COMMANDS:
rpm-build c487f7
      tmp = self->commands;
rpm-build c487f7
      self->commands = 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_DEST_FILENAME:
rpm-build c487f7
      g_free (self->dest_filename);
rpm-build c487f7
      self->dest_filename = 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
builder_source_script_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
  return TRUE;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static gboolean
rpm-build c487f7
builder_source_script_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
  BuilderSourceScript *self = BUILDER_SOURCE_SCRIPT (source);
rpm-build c487f7
rpm-build c487f7
  g_autoptr(GFile) dest_script = NULL;
rpm-build c487f7
  const char *dest_filename;
rpm-build c487f7
  g_autoptr(GString) script = NULL;
rpm-build c487f7
  int i;
rpm-build c487f7
  guint32 perms;
rpm-build c487f7
rpm-build c487f7
  script = g_string_new ("#!/bin/sh\n");
rpm-build c487f7
rpm-build c487f7
  if (self->commands)
rpm-build c487f7
    {
rpm-build c487f7
      for (i = 0; self->commands[i] != NULL; i++)
rpm-build c487f7
        {
rpm-build c487f7
          g_string_append (script, self->commands[i]);
rpm-build c487f7
          g_string_append_c (script, '\n');
rpm-build c487f7
        }
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  if (self->dest_filename)
rpm-build c487f7
    dest_filename = self->dest_filename;
rpm-build c487f7
  else
rpm-build c487f7
    dest_filename = "autogen.sh";
rpm-build c487f7
rpm-build c487f7
  dest_script = g_file_get_child (dest, dest_filename);
rpm-build c487f7
rpm-build c487f7
  if (!g_file_set_contents (flatpak_file_get_path_cached (dest_script),
rpm-build c487f7
                            script->str,
rpm-build c487f7
                            script->len,
rpm-build c487f7
                            error))
rpm-build c487f7
    return FALSE;
rpm-build c487f7
rpm-build c487f7
  perms = 0755;
rpm-build c487f7
  if (!g_file_set_attribute (dest_script,
rpm-build c487f7
                             G_FILE_ATTRIBUTE_UNIX_MODE,
rpm-build c487f7
                             G_FILE_ATTRIBUTE_TYPE_UINT32,
rpm-build c487f7
                             &perms,
rpm-build c487f7
                             G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
rpm-build c487f7
                             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_script_bundle (BuilderSource  *source,
rpm-build c487f7
                              BuilderContext *context,
rpm-build c487f7
                              GError        **error)
rpm-build c487f7
{
rpm-build c487f7
  /* no need to bundle anything here as this part
rpm-build c487f7
     can be reconstructed from the manifest */
rpm-build c487f7
  return TRUE;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
builder_source_script_checksum (BuilderSource  *source,
rpm-build c487f7
                                BuilderCache   *cache,
rpm-build c487f7
                                BuilderContext *context)
rpm-build c487f7
{
rpm-build c487f7
  BuilderSourceScript *self = BUILDER_SOURCE_SCRIPT (source);
rpm-build c487f7
rpm-build c487f7
  builder_cache_checksum_strv (cache, self->commands);
rpm-build c487f7
  builder_cache_checksum_str (cache, self->dest_filename);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
builder_source_script_class_init (BuilderSourceScriptClass *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_script_finalize;
rpm-build c487f7
  object_class->get_property = builder_source_script_get_property;
rpm-build c487f7
  object_class->set_property = builder_source_script_set_property;
rpm-build c487f7
rpm-build c487f7
  source_class->download = builder_source_script_download;
rpm-build c487f7
  source_class->extract = builder_source_script_extract;
rpm-build c487f7
  source_class->bundle = builder_source_script_bundle;
rpm-build c487f7
  source_class->checksum = builder_source_script_checksum;
rpm-build c487f7
rpm-build c487f7
  g_object_class_install_property (object_class,
rpm-build c487f7
                                   PROP_COMMANDS,
rpm-build c487f7
                                   g_param_spec_boxed ("commands",
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_DEST_FILENAME,
rpm-build c487f7
                                   g_param_spec_string ("dest-filename",
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_script_init (BuilderSourceScript *self)
rpm-build c487f7
{
rpm-build c487f7
}