gboolean
_rpmostree_file_load_contents_utf8_allow_noent (GFile          *path,
                                                char          **out_contents,
                                                GCancellable   *cancellable,
                                                GError        **error)
{
  gboolean ret = FALSE;
  GError *temp_error = NULL;
  gs_free char *ret_contents = NULL;

  ret_contents = gs_file_load_contents_utf8 (path, cancellable, &temp_error);
  if (!ret_contents)
    {
      if (g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
        {
          g_clear_error (&temp_error);
        }
      else
        {
          g_propagate_error (error, temp_error);
          goto out;
        }
    }

  ret = TRUE;
  gs_transfer_out_value (out_contents, &ret_contents);
 out:
  return ret;
}
Beispiel #2
0
static gboolean
parse_rev_file (OstreeRepo     *self,
                GFile          *f,
                char          **sha256,
                GError        **error)
{
  gboolean ret = FALSE;
  GError *temp_error = NULL;
  gs_free char *rev = NULL;

  if ((rev = gs_file_load_contents_utf8 (f, NULL, &temp_error)) == NULL)
    goto out;

  if (rev == NULL)
    {
      if (g_error_matches (temp_error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
        {
          g_clear_error (&temp_error);
        }
      else
        {
          g_propagate_error (error, temp_error);
          goto out;
        }
    }
  else
    {
      g_strchomp (rev);
    }

  if (g_str_has_prefix (rev, "ref: "))
    {
      gs_unref_object GFile *ref = NULL;
      char *ref_sha256;
      gboolean subret;

      ref = g_file_resolve_relative_path (self->local_heads_dir, rev + 5);
      subret = parse_rev_file (self, ref, &ref_sha256, error);
        
      if (!subret)
        {
          g_free (ref_sha256);
          goto out;
        }
      
      g_free (rev);
      rev = ref_sha256;
    }
  else 
    {
      if (!ostree_validate_checksum_string (rev, error))
        goto out;
    }

  ot_transfer_out_value(sha256, &rev);
  ret = TRUE;
 out:
  return ret;
}
gboolean
ostree_bootconfig_parser_parse (OstreeBootconfigParser  *self,
                                GFile           *path,
                                GCancellable    *cancellable,
                                GError         **error)
{
  gboolean ret = FALSE;
  gs_free char *contents = NULL;
  char **lines = NULL;
  char **iter = NULL;

  g_return_val_if_fail (!self->parsed, FALSE);

  contents = gs_file_load_contents_utf8 (path, cancellable, error);
  if (!contents)
    goto out;

  lines = g_strsplit (contents, "\n", -1);
  for (iter = lines; *iter; iter++)
    {
      const char *line = *iter;
      char *keyname = "";
      
      if (g_ascii_isalpha (*line))
        {
          char **items = NULL;
          items = g_strsplit_set (line, self->separators, 2);
          if (g_strv_length (items) == 2 && items[0][0] != '\0')
            {
              keyname = items[0];
              g_hash_table_insert (self->options, items[0], items[1]);
              g_free (items); /* Transfer ownership */
            }
          else
            {
              g_strfreev (items);
            }
        }
      g_ptr_array_add (self->lines, g_variant_new ("(ss)", keyname, line));
    }

  self->parsed = TRUE;
  
  ret = TRUE;
 out:
  g_strfreev (lines);
  return ret;
}
static gboolean
replace_nsswitch (GFile         *target_usretc,
                  GCancellable  *cancellable,
                  GError       **error)
{
    gboolean ret = FALSE;
    gs_unref_object GFile *nsswitch_conf =
        g_file_get_child (target_usretc, "nsswitch.conf");
    gs_free char *nsswitch_contents = NULL;
    gs_free char *new_nsswitch_contents = NULL;

    static gsize regex_initialized;
    static GRegex *passwd_regex;

    if (g_once_init_enter (&regex_initialized))
    {
        passwd_regex = g_regex_new ("^(passwd|group):\\s+files(.*)$",
                                    G_REGEX_MULTILINE, 0, NULL);
        g_assert (passwd_regex);
        g_once_init_leave (&regex_initialized, 1);
    }

    nsswitch_contents = gs_file_load_contents_utf8 (nsswitch_conf, cancellable, error);
    if (!nsswitch_contents)
        goto out;

    new_nsswitch_contents = g_regex_replace (passwd_regex,
                            nsswitch_contents, -1, 0,
                            "\\1: files altfiles\\2",
                            0, error);
    if (!new_nsswitch_contents)
        goto out;

    if (!g_file_replace_contents (nsswitch_conf, new_nsswitch_contents,
                                  strlen (new_nsswitch_contents),
                                  NULL, FALSE, 0, NULL,
                                  cancellable, error))
        goto out;

    ret = TRUE;
out:
    return ret;
}
Beispiel #5
0
static gboolean
resolve_refspec (OstreeRepo     *self,
                 const char     *remote,
                 const char     *ref,
                 gboolean        allow_noent,
                 char          **out_rev,
                 GError        **error)
{
    gboolean ret = FALSE;
    __attribute__((unused)) GCancellable *cancellable = NULL;
    GError *temp_error = NULL;
    g_autofree char *ret_rev = NULL;
    g_autoptr(GFile) child = NULL;

    g_return_val_if_fail (ref != NULL, FALSE);

    /* We intentionally don't allow a ref that looks like a checksum */
    if (ostree_validate_checksum_string (ref, NULL))
    {
        ret_rev = g_strdup (ref);
    }
    else if (remote != NULL)
    {
        child = ot_gfile_resolve_path_printf (self->remote_heads_dir, "%s/%s",
                                              remote, ref);
        if (!g_file_query_exists (child, NULL))
            g_clear_object (&child);
    }
    else
    {
        child = g_file_resolve_relative_path (self->local_heads_dir, ref);

        if (!g_file_query_exists (child, NULL))
        {
            g_clear_object (&child);

            child = g_file_resolve_relative_path (self->remote_heads_dir, ref);

            if (!g_file_query_exists (child, NULL))
            {
                g_clear_object (&child);

                if (!find_ref_in_remotes (self, ref, &child, error))
                    goto out;
            }
        }
    }

    if (child)
    {
        if ((ret_rev = gs_file_load_contents_utf8 (child, NULL, &temp_error)) == NULL)
        {
            g_propagate_error (error, temp_error);
            g_prefix_error (error, "Couldn't open ref '%s': ", gs_file_get_path_cached (child));
            goto out;
        }

        g_strchomp (ret_rev);
        if (!ostree_validate_checksum_string (ret_rev, error))
            goto out;
    }
    else
    {
        if (!resolve_refspec_fallback (self, remote, ref, allow_noent,
                                       &ret_rev, cancellable, error))
            goto out;
    }

    ot_transfer_out_value (out_rev, &ret_rev);
    ret = TRUE;
out:
    return ret;
}
static gboolean
_ostree_bootloader_syslinux_write_config (OstreeBootloader          *bootloader,
                                     int                    bootversion,
                                     GCancellable          *cancellable,
                                     GError               **error)
{
  gboolean ret = FALSE;
  OstreeBootloaderSyslinux *self = OSTREE_BOOTLOADER_SYSLINUX (bootloader);
  gs_unref_object GFile *new_config_path = NULL;
  gs_free char *config_contents = NULL;
  gs_free char *new_config_contents = NULL;
  gs_unref_ptrarray GPtrArray *new_lines = NULL;
  gs_unref_ptrarray GPtrArray *tmp_lines = NULL;
  gs_free char *kernel_arg = NULL;
  gboolean saw_default = FALSE;
  gboolean regenerate_default = FALSE;
  gboolean parsing_label = FALSE;
  char **lines = NULL;
  char **iter;
  guint i;

  new_config_path = ot_gfile_resolve_path_printf (self->sysroot->path, "boot/loader.%d/syslinux.cfg",
                                                  bootversion);

  /* This should follow the symbolic link to the current bootversion. */
  config_contents = gs_file_load_contents_utf8 (self->config_path, cancellable, error);
  if (!config_contents)
    goto out;

  lines = g_strsplit (config_contents, "\n", -1);
  new_lines = g_ptr_array_new_with_free_func (g_free);
  tmp_lines = g_ptr_array_new_with_free_func (g_free);
  
  /* Note special iteration condition here; we want to also loop one
   * more time at the end where line = NULL to ensure we finish off
   * processing the last LABEL.
   */
  iter = lines;
  while (TRUE)
    {
      char *line = *iter;
      gboolean skip = FALSE;

      if (parsing_label && 
          (line == NULL || !g_str_has_prefix (line, "\t")))
        {
          parsing_label = FALSE;
          if (kernel_arg == NULL)
            {
              g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                                   "No KERNEL argument found after LABEL");
              goto out;
            }

          /* If this is a non-ostree kernel, just emit the lines
           * we saw.
           */
          if (!g_str_has_prefix (kernel_arg, "/ostree/"))
            {
              for (i = 0; i < tmp_lines->len; i++)
                {
                  g_ptr_array_add (new_lines, tmp_lines->pdata[i]);
                  tmp_lines->pdata[i] = NULL; /* Transfer ownership */
                }
            }
          else
            {
              /* Otherwise, we drop the config on the floor - it
               * will be regenerated.
               */
              g_ptr_array_set_size (tmp_lines, 0);
            }
        }

      if (line == NULL)
        break;

      if (!parsing_label &&
          (g_str_has_prefix (line, "LABEL ")))
        {
          parsing_label = TRUE;
          g_ptr_array_set_size (tmp_lines, 0);
        }
      else if (parsing_label && g_str_has_prefix (line, "\tKERNEL "))
        {
          g_free (kernel_arg);
          kernel_arg = g_strdup (line + strlen ("\tKERNEL "));
        }
      else if (!parsing_label &&
               (g_str_has_prefix (line, "DEFAULT ")))
        {
          saw_default = TRUE;
          /* XXX Searching for patterns in the title is rather brittle,
           *     but this hack is at least noted in the code that builds
           *     the title to hopefully avoid regressions. */
          if (g_str_has_prefix (line, "DEFAULT ostree:") ||  /* old format */
              strstr (line, "(ostree") != NULL)              /* new format */
            {
              regenerate_default = TRUE;
            }
          skip = TRUE;
        }
      
      if (skip)
        {
          g_free (line);
        }
      else
        {
          if (parsing_label)
            {
              g_ptr_array_add (tmp_lines, line);
            }
          else
            {
              g_ptr_array_add (new_lines, line);
            }
        }
      /* Transfer ownership */
      *iter = NULL;
      iter++;
    }

  if (!saw_default)
    regenerate_default = TRUE;

  if (!append_config_from_boostree_loader_entries (self, regenerate_default,
                                               bootversion, new_lines,
                                               cancellable, error))
    goto out;

  new_config_contents = _ostree_sysroot_join_lines (new_lines);
  {
    gs_unref_bytes GBytes *new_config_contents_bytes =
      g_bytes_new_static (new_config_contents,
                          strlen (new_config_contents));

    if (!ot_gfile_replace_contents_fsync (new_config_path, new_config_contents_bytes,
                                          cancellable, error))
      goto out;
  }
  
  ret = TRUE;
 out:
  g_free (lines); /* Note we freed elements individually */
  return ret;
}