Move Deployment and BootconfigParser into libostree

As part of moving admin functionality there.  While we are doing this,
rename OtConfigParser to OstreeBootConfig parser since it's a better
name.
This commit is contained in:
Colin Walters committed 2013-09-15 15:06:31 -04:00
1 parent af0f888057
commit 35bab87691
21 files changed
+370 -378

No files matched your search

+228
View File
@@ -0,0 +1,228 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2013 Colin Walters <walters@verbum.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2 of the licence or (at
* your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include "ostree-bootconfig-parser.h"
#include "libgsystem.h"
struct _OstreeBootconfigParser
{
GObject parent_instance;
gboolean parsed;
char *separators;
GHashTable *options;
GPtrArray *lines;
};
typedef GObjectClass OstreeBootconfigParserClass;
G_DEFINE_TYPE (OstreeBootconfigParser, ostree_bootconfig_parser, G_TYPE_OBJECT)
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;
}
void
ostree_bootconfig_parser_set (OstreeBootconfigParser *self,
const char *key,
const char *value)
{
g_hash_table_replace (self->options, g_strdup (key), g_strdup (value));
}
const char *
ostree_bootconfig_parser_get (OstreeBootconfigParser *self,
const char *key)
{
return g_hash_table_lookup (self->options, key);
}
static gboolean
write_key (OstreeBootconfigParser *self,
GDataOutputStream *out,
const char *key,
const char *value,
GCancellable *cancellable,
GError **error)
{
gboolean ret = FALSE;
if (!g_data_output_stream_put_string (out, key, cancellable, error))
goto out;
if (!g_data_output_stream_put_byte (out, self->separators[0], cancellable, error))
goto out;
if (!g_data_output_stream_put_string (out, value, cancellable, error))
goto out;
if (!g_data_output_stream_put_byte (out, '\n', cancellable, error))
goto out;
ret = TRUE;
out:
return ret;
}
gboolean
ostree_bootconfig_parser_write (OstreeBootconfigParser *self,
GFile *output,
GCancellable *cancellable,
GError **error)
{
gboolean ret = FALSE;
GHashTableIter hashiter;
gpointer hashkey, hashvalue;
gs_unref_object GOutputStream *out = NULL;
gs_unref_object GDataOutputStream *dataout = NULL;
guint i;
gs_unref_hashtable GHashTable *written_overrides = NULL;
written_overrides = g_hash_table_new (g_str_hash, g_str_equal);
out = (GOutputStream*)g_file_replace (output, NULL, FALSE, 0, cancellable, error);
if (!out)
goto out;
dataout = g_data_output_stream_new (out);
for (i = 0; i < self->lines->len; i++)
{
GVariant *linedata = self->lines->pdata[i];
const char *key;
const char *value;
const char *line;
g_variant_get (linedata, "(&s&s)", &key, &line);
value = g_hash_table_lookup (self->options, key);
if (value == NULL)
{
if (!g_data_output_stream_put_string (dataout, line, cancellable, error))
goto out;
if (!g_data_output_stream_put_byte (dataout, '\n', cancellable, error))
goto out;
}
else
{
if (!write_key (self, dataout, key, value, cancellable, error))
goto out;
g_hash_table_insert (written_overrides, (gpointer)key, (gpointer)key);
}
}
g_hash_table_iter_init (&hashiter, self->options);
while (g_hash_table_iter_next (&hashiter, &hashkey, &hashvalue))
{
if (g_hash_table_lookup (written_overrides, hashkey))
continue;
if (!write_key (self, dataout, hashkey, hashvalue, cancellable, error))
goto out;
}
if (!g_output_stream_close ((GOutputStream*)dataout, cancellable, error))
goto out;
ret = TRUE;
out:
return ret;
}
static void
ostree_bootconfig_parser_finalize (GObject *object)
{
OstreeBootconfigParser *self = OSTREE_BOOTCONFIG_PARSER (object);
g_hash_table_unref (self->options);
g_ptr_array_unref (self->lines);
g_free (self->separators);
G_OBJECT_CLASS (ostree_bootconfig_parser_parent_class)->finalize (object);
}
static void
ostree_bootconfig_parser_init (OstreeBootconfigParser *self)
{
self->options = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
self->lines = g_ptr_array_new ();
}
void
ostree_bootconfig_parser_class_init (OstreeBootconfigParserClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
object_class->finalize = ostree_bootconfig_parser_finalize;
}
OstreeBootconfigParser *
ostree_bootconfig_parser_new (void)
{
OstreeBootconfigParser *self = NULL;
self = g_object_new (OSTREE_TYPE_BOOTCONFIG_PARSER, NULL);
self->separators = g_strdup (" \t");
return self;
}
+56
View File
@@ -0,0 +1,56 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2013 Colin Walters <walters@verbum.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2 of the licence or (at
* your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*/
#pragma once
#include <gio/gio.h>
G_BEGIN_DECLS
#define OSTREE_TYPE_BOOTCONFIG_PARSER (ostree_bootconfig_parser_get_type ())
#define OSTREE_BOOTCONFIG_PARSER(inst) (G_TYPE_CHECK_INSTANCE_CAST ((inst), OSTREE_TYPE_BOOTCONFIG_PARSER, OstreeBootconfigParser))
#define OSTREE_IS_BOOTCONFIG_PARSER(inst) (G_TYPE_CHECK_INSTANCE_TYPE ((inst), OSTREE_TYPE_BOOTCONFIG_PARSER))
typedef struct _OstreeBootconfigParser OstreeBootconfigParser;
GType ostree_bootconfig_parser_get_type (void) G_GNUC_CONST;
OstreeBootconfigParser * ostree_bootconfig_parser_new (void);
gboolean ostree_bootconfig_parser_parse (OstreeBootconfigParser *self,
GFile *path,
GCancellable *cancellable,
GError **error);
gboolean ostree_bootconfig_parser_write (OstreeBootconfigParser *self,
GFile *output,
GCancellable *cancellable,
GError **error);
void ostree_bootconfig_parser_set (OstreeBootconfigParser *self,
const char *key,
const char *value);
const char *ostree_bootconfig_parser_get (OstreeBootconfigParser *self,
const char *key);
G_END_DECLS
+210
View File
@@ -0,0 +1,210 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2013 Colin Walters <walters@verbum.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2 of the licence or (at
* your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include "ostree-deployment.h"
#include "libgsystem.h"
struct _OstreeDeployment
{
GObject parent_instance;
int index; /* Global offset */
char *osname; /* osname */
char *csum; /* OSTree checksum of tree */
int deployserial; /* How many times this particular csum appears in deployment list */
char *bootcsum; /* Checksum of kernel+initramfs */
int bootserial; /* An integer assigned to this tree per its ${bootcsum} */
OstreeBootconfigParser *bootconfig; /* Bootloader configuration */
GKeyFile *origin; /* How to construct an upgraded version of this tree */
};
typedef GObjectClass OstreeDeploymentClass;
G_DEFINE_TYPE (OstreeDeployment, ostree_deployment, G_TYPE_OBJECT)
const char *
ostree_deployment_get_csum (OstreeDeployment *self)
{
return self->csum;
}
const char *
ostree_deployment_get_bootcsum (OstreeDeployment *self)
{
return self->bootcsum;
}
const char *
ostree_deployment_get_osname (OstreeDeployment *self)
{
return self->osname;
}
int
ostree_deployment_get_deployserial (OstreeDeployment *self)
{
return self->deployserial;
}
int
ostree_deployment_get_bootserial (OstreeDeployment *self)
{
return self->bootserial;
}
OstreeBootconfigParser *
ostree_deployment_get_bootconfig (OstreeDeployment *self)
{
return self->bootconfig;
}
GKeyFile *
ostree_deployment_get_origin (OstreeDeployment *self)
{
return self->origin;
}
int
ostree_deployment_get_index (OstreeDeployment *self)
{
return self->index;
}
void
ostree_deployment_set_index (OstreeDeployment *self, int index)
{
self->index = index;
}
void
ostree_deployment_set_bootserial (OstreeDeployment *self, int index)
{
self->bootserial = index;
}
void
ostree_deployment_set_bootconfig (OstreeDeployment *self, OstreeBootconfigParser *bootconfig)
{
g_clear_object (&self->bootconfig);
if (bootconfig)
self->bootconfig = g_object_ref (bootconfig);
}
void
ostree_deployment_set_origin (OstreeDeployment *self, GKeyFile *origin)
{
g_clear_pointer (&self->origin, g_key_file_unref);
if (origin)
self->origin = g_key_file_ref (origin);
}
OstreeDeployment *
ostree_deployment_clone (OstreeDeployment *self)
{
OstreeDeployment *ret = ostree_deployment_new (self->index, self->osname, self->csum,
self->deployserial,
self->bootcsum, self->bootserial);
ostree_deployment_set_bootconfig (ret, self->bootconfig);
ostree_deployment_set_origin (ret, self->origin);
return ret;
}
guint
ostree_deployment_hash (gconstpointer v)
{
OstreeDeployment *d = (OstreeDeployment*)v;
return g_str_hash (ostree_deployment_get_osname (d)) +
g_str_hash (ostree_deployment_get_csum (d)) +
ostree_deployment_get_deployserial (d);
}
gboolean
ostree_deployment_equal (gconstpointer ap, gconstpointer bp)
{
OstreeDeployment *a = (OstreeDeployment*)ap;
OstreeDeployment *b = (OstreeDeployment*)bp;
if (a == NULL && b == NULL)
return TRUE;
else if (a != NULL && b != NULL)
return g_str_equal (ostree_deployment_get_osname (a),
ostree_deployment_get_osname (b)) &&
g_str_equal (ostree_deployment_get_csum (a),
ostree_deployment_get_csum (b)) &&
ostree_deployment_get_deployserial (a) == ostree_deployment_get_deployserial (b);
else
return FALSE;
}
static void
ostree_deployment_finalize (GObject *object)
{
OstreeDeployment *self = OSTREE_DEPLOYMENT (object);
g_free (self->osname);
g_free (self->csum);
g_free (self->bootcsum);
g_clear_object (&self->bootconfig);
g_clear_pointer (&self->origin, g_key_file_unref);
G_OBJECT_CLASS (ostree_deployment_parent_class)->finalize (object);
}
void
ostree_deployment_init (OstreeDeployment *self)
{
}
void
ostree_deployment_class_init (OstreeDeploymentClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
object_class->finalize = ostree_deployment_finalize;
}
OstreeDeployment *
ostree_deployment_new (int index,
const char *osname,
const char *csum,
int deployserial,
const char *bootcsum,
int bootserial)
{
OstreeDeployment *self;
/* index may be -1 */
g_return_val_if_fail (osname != NULL, NULL);
g_return_val_if_fail (csum != NULL, NULL);
g_return_val_if_fail (deployserial >= 0, NULL);
/* We can have "disconnected" deployments that don't have a
bootcsum/serial */
self = g_object_new (OSTREE_TYPE_DEPLOYMENT, NULL);
self->index = index;
self->osname = g_strdup (osname);
self->csum = g_strdup (csum);
self->deployserial = deployserial;
self->bootcsum = g_strdup (bootcsum);
self->bootserial = bootserial;
return self;
}
+63
View File
@@ -0,0 +1,63 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2013 Colin Walters <walters@verbum.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2 of the licence or (at
* your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*/
#pragma once
#include "ostree-bootconfig-parser.h"
G_BEGIN_DECLS
#define OSTREE_TYPE_DEPLOYMENT (ostree_deployment_get_type ())
#define OSTREE_DEPLOYMENT(inst) (G_TYPE_CHECK_INSTANCE_CAST ((inst), OSTREE_TYPE_DEPLOYMENT, OstreeDeployment))
#define OSTREE_IS_DEPLOYMENT(inst) (G_TYPE_CHECK_INSTANCE_TYPE ((inst), OSTREE_TYPE_DEPLOYMENT))
typedef struct _OstreeDeployment OstreeDeployment;
GType ostree_deployment_get_type (void) G_GNUC_CONST;
guint ostree_deployment_hash (gconstpointer v);
gboolean ostree_deployment_equal (gconstpointer a, gconstpointer b);
OstreeDeployment * ostree_deployment_new (int index,
const char *osname,
const char *csum,
int deployserial,
const char *bootcsum,
int bootserial);
int ostree_deployment_get_index (OstreeDeployment *self);
const char *ostree_deployment_get_osname (OstreeDeployment *self);
int ostree_deployment_get_deployserial (OstreeDeployment *self);
const char *ostree_deployment_get_csum (OstreeDeployment *self);
const char *ostree_deployment_get_bootcsum (OstreeDeployment *self);
int ostree_deployment_get_bootserial (OstreeDeployment *self);
OstreeBootconfigParser *ostree_deployment_get_bootconfig (OstreeDeployment *self);
GKeyFile *ostree_deployment_get_origin (OstreeDeployment *self);
void ostree_deployment_set_index (OstreeDeployment *self, int index);
void ostree_deployment_set_bootserial (OstreeDeployment *self, int index);
void ostree_deployment_set_bootconfig (OstreeDeployment *self, OstreeBootconfigParser *bootconfig);
void ostree_deployment_set_origin (OstreeDeployment *self, GKeyFile *origin);
OstreeDeployment *ostree_deployment_clone (OstreeDeployment *self);
G_END_DECLS
+2
View File
@@ -27,4 +27,6 @@
#include <ostree-mutable-tree.h>
#include <ostree-repo-file.h>
#include <ostree-sysroot.h>
#include <ostree-deployment.h>
#include <ostree-bootconfig-parser.h>
#include <ostree-diff.h>