Blame src/ostree/ot-admin-builtin-set-origin.c

rpm-build 0fba15
/*
rpm-build 0fba15
 * Copyright (C) 2015 Colin Walters <walters@verbum.org>
rpm-build 0fba15
 *
rpm-build 0fba15
 * SPDX-License-Identifier: LGPL-2.0+
rpm-build 0fba15
 *
rpm-build 0fba15
 * This library is free software; you can redistribute it and/or
rpm-build 0fba15
 * modify it under the terms of the GNU Lesser General Public
rpm-build 0fba15
 * License as published by the Free Software Foundation; either
rpm-build 0fba15
 * version 2 of the License, or (at your option) any later version.
rpm-build 0fba15
 *
rpm-build 0fba15
 * This library is distributed in the hope that it will be useful,
rpm-build 0fba15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rpm-build 0fba15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
rpm-build 0fba15
 * Lesser General Public License for more details.
rpm-build 0fba15
 *
rpm-build 0fba15
 * You should have received a copy of the GNU Lesser General Public
rpm-build 0fba15
 * License along with this library; if not, write to the
rpm-build 0fba15
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
rpm-build 0fba15
 * Boston, MA 02111-1307, USA.
rpm-build 0fba15
 */
rpm-build 0fba15
rpm-build 0fba15
#include "config.h"
rpm-build 0fba15
rpm-build 0fba15
#include "ot-main.h"
rpm-build 0fba15
#include "ot-admin-builtins.h"
rpm-build 0fba15
#include "ot-admin-functions.h"
rpm-build 0fba15
#include "ostree.h"
rpm-build 0fba15
#include "otutil.h"
rpm-build 0fba15
rpm-build 0fba15
#include <unistd.h>
rpm-build 0fba15
#include <stdlib.h>
rpm-build 0fba15
#include <glib/gi18n.h>
rpm-build 0fba15
rpm-build 0fba15
static int opt_index = -1;
rpm-build 0fba15
static char **opt_set;
rpm-build 0fba15
rpm-build 0fba15
static GOptionEntry options[] = {
rpm-build 0fba15
  { "set", 's', 0, G_OPTION_ARG_STRING_ARRAY, &opt_set, "Set config option KEY=VALUE for remote", "KEY=VALUE" },
rpm-build 0fba15
  { "index", 0, 0, G_OPTION_ARG_INT, &opt_index, "Operate on the deployment INDEX, starting from zero", "INDEX" },
rpm-build 0fba15
  { NULL }
rpm-build 0fba15
};
rpm-build 0fba15
rpm-build 0fba15
gboolean
rpm-build 0fba15
ot_admin_builtin_set_origin (int argc, char **argv, OstreeCommandInvocation *invocation, GCancellable *cancellable, GError **error)
rpm-build 0fba15
{
rpm-build 0fba15
  gboolean ret = FALSE;
rpm-build 0fba15
  g_autoptr(GOptionContext) context = NULL;
rpm-build 0fba15
  const char *remotename = NULL;
rpm-build 0fba15
  const char *url = NULL;
rpm-build 0fba15
  const char *branch = NULL;
rpm-build 0fba15
  g_autoptr(OstreeRepo) repo = NULL;
rpm-build 0fba15
  g_autoptr(OstreeSysroot) sysroot = NULL;
rpm-build 0fba15
  g_autoptr(OstreeDeployment) target_deployment = NULL;
rpm-build 0fba15
rpm-build 0fba15
  context = g_option_context_new ("REMOTENAME URL [BRANCH]");
rpm-build 0fba15
rpm-build 0fba15
  if (!ostree_admin_option_context_parse (context, options, &argc, &argv,
rpm-build 0fba15
                                          OSTREE_ADMIN_BUILTIN_FLAG_SUPERUSER,
rpm-build 0fba15
                                          invocation, &sysroot, cancellable, error))
rpm-build 0fba15
    goto out;
rpm-build 0fba15
rpm-build 0fba15
  if (argc < 3)
rpm-build 0fba15
    {
rpm-build 0fba15
      ot_util_usage_error (context, "REMOTENAME and URL must be specified", error);
rpm-build 0fba15
      goto out;
rpm-build 0fba15
    }
rpm-build 0fba15
rpm-build 0fba15
  remotename = argv[1];
rpm-build 0fba15
  url = argv[2];
rpm-build 0fba15
  if (argc > 3)
rpm-build 0fba15
    branch = argv[3];
rpm-build 0fba15
rpm-build 0fba15
  if (!ostree_sysroot_get_repo (sysroot, &repo, cancellable, error))
rpm-build 0fba15
    goto out;
rpm-build 0fba15
rpm-build 0fba15
  if (opt_index == -1)
rpm-build 0fba15
    {
rpm-build 0fba15
      target_deployment = ostree_sysroot_get_booted_deployment (sysroot);
rpm-build 0fba15
      if (target_deployment == NULL)
rpm-build 0fba15
        {
rpm-build 0fba15
          g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
rpm-build 0fba15
                               "Not currently booted into an OSTree system");
rpm-build 0fba15
          goto out;
rpm-build 0fba15
        }
rpm-build 0fba15
      /* To match the below */
rpm-build 0fba15
      target_deployment = g_object_ref (target_deployment);
rpm-build 0fba15
    }
rpm-build 0fba15
  else
rpm-build 0fba15
    {
rpm-build 0fba15
      target_deployment = ot_admin_get_indexed_deployment (sysroot, opt_index, error);
rpm-build 0fba15
      if (!target_deployment)
rpm-build 0fba15
        goto out;
rpm-build 0fba15
    }
rpm-build 0fba15
rpm-build 0fba15
  { char **iter;
rpm-build 0fba15
    g_autoptr(GVariantBuilder) optbuilder =
rpm-build 0fba15
      g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
rpm-build 0fba15
    g_autoptr(GVariant) options = NULL;
rpm-build 0fba15
rpm-build 0fba15
    for (iter = opt_set; iter && *iter; iter++)
rpm-build 0fba15
      {
rpm-build 0fba15
        const char *keyvalue = *iter;
rpm-build 0fba15
        g_autofree char *subkey = NULL;
rpm-build 0fba15
        g_autofree char *subvalue = NULL;
rpm-build 0fba15
rpm-build 0fba15
        if (!ot_parse_keyvalue (keyvalue, &subkey, &subvalue, error))
rpm-build 0fba15
          goto out;
rpm-build 0fba15
rpm-build 0fba15
        g_variant_builder_add (optbuilder, "{s@v}",
rpm-build 0fba15
                               subkey, g_variant_new_variant (g_variant_new_string (subvalue)));
rpm-build 0fba15
      }
rpm-build 0fba15
rpm-build 0fba15
    options = g_variant_ref_sink (g_variant_builder_end (optbuilder));
rpm-build 0fba15
rpm-build 0fba15
    if (!ostree_repo_remote_change (repo, NULL,
rpm-build 0fba15
                                    OSTREE_REPO_REMOTE_CHANGE_ADD_IF_NOT_EXISTS, 
rpm-build 0fba15
                                    remotename, url,
rpm-build 0fba15
                                    options,
rpm-build 0fba15
                                    cancellable, error))
rpm-build 0fba15
      goto out;
rpm-build 0fba15
  }
rpm-build 0fba15
  
rpm-build 0fba15
  { GKeyFile *old_origin = ostree_deployment_get_origin (target_deployment);
rpm-build 0fba15
    g_autofree char *origin_refspec = g_key_file_get_string (old_origin, "origin", "refspec", NULL);
rpm-build 0fba15
    g_autofree char *origin_remote = NULL;
rpm-build 0fba15
    g_autofree char *origin_ref = NULL;
rpm-build 0fba15
  
rpm-build 0fba15
    if (!ostree_parse_refspec (origin_refspec, &origin_remote, &origin_ref, error))
rpm-build 0fba15
      goto out;
rpm-build 0fba15
rpm-build 0fba15
    { g_autofree char *new_refspec = g_strconcat (remotename, ":", branch ? branch : origin_ref, NULL);
rpm-build 0fba15
      g_autoptr(GKeyFile) new_origin = NULL;
rpm-build 0fba15
      
rpm-build 0fba15
      new_origin = ostree_sysroot_origin_new_from_refspec (sysroot, new_refspec);
rpm-build 0fba15
rpm-build 0fba15
      if (!ostree_sysroot_write_origin_file (sysroot, target_deployment, new_origin,
rpm-build 0fba15
                                             cancellable, error))
rpm-build 0fba15
        goto out;
rpm-build 0fba15
    }
rpm-build 0fba15
  }
rpm-build 0fba15
rpm-build 0fba15
  ret = TRUE;
rpm-build 0fba15
 out:
rpm-build 0fba15
  return ret;
rpm-build 0fba15
}