core: Add a config file with repo version

This should let us expand later.
This commit is contained in:
Colin Walters 2011-10-26 22:21:00 -04:00
parent 1aac09fee9
commit ac99188c23
2 changed files with 56 additions and 3 deletions

View File

@ -58,9 +58,12 @@ struct _OstreeRepoPrivate {
GFile *repo_file;
char *head_ref_path;
char *objects_path;
char *config_path;
gboolean inited;
char *current_head;
GKeyFile *config;
};
static void
@ -73,7 +76,10 @@ ostree_repo_finalize (GObject *object)
g_clear_object (&priv->repo_file);
g_free (priv->head_ref_path);
g_free (priv->objects_path);
g_free (priv->config_path);
g_free (priv->current_head);
if (priv->config)
g_key_file_free (priv->config);
G_OBJECT_CLASS (ostree_repo_parent_class)->finalize (object);
}
@ -138,6 +144,7 @@ ostree_repo_constructor (GType gtype,
priv->head_ref_path = g_build_filename (priv->path, "HEAD", NULL);
priv->objects_path = g_build_filename (priv->path, "objects", NULL);
priv->config_path = g_build_filename (priv->path, "config", NULL);
return object;
}
@ -231,6 +238,8 @@ gboolean
ostree_repo_check (OstreeRepo *self, GError **error)
{
OstreeRepoPrivate *priv = GET_PRIVATE (self);
gboolean ret = FALSE;
char *version = NULL;;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
@ -241,12 +250,36 @@ ostree_repo_check (OstreeRepo *self, GError **error)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Couldn't find objects directory '%s'", priv->objects_path);
return FALSE;
goto out;
}
priv->inited = TRUE;
if (!parse_checksum_file (self, priv->head_ref_path, &priv->current_head, error))
goto out;
return parse_checksum_file (self, priv->head_ref_path, &priv->current_head, error);
priv->config = g_key_file_new ();
if (!g_key_file_load_from_file (priv->config, priv->config_path, 0, error))
{
g_prefix_error (error, "Couldn't parse config file: ");
goto out;
}
version = g_key_file_get_value (priv->config, "core", "repo_version", error);
if (!version)
goto out;
if (strcmp (version, "0") != 0)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Invalid repository version '%s'", version);
goto out;
}
priv->inited = TRUE;
ret = TRUE;
out:
g_free (version);
return ret;
}
static gboolean

View File

@ -32,6 +32,10 @@ static GOptionEntry options[] = {
{ NULL }
};
#define DEFAULT_CONFIG_CONTENTS ("[core]\n" \
"repo_version=0\n")
gboolean
ostree_builtin_init (int argc, char **argv, const char *prefix, GError **error)
{
@ -39,8 +43,10 @@ ostree_builtin_init (int argc, char **argv, const char *prefix, GError **error)
gboolean ret = FALSE;
char *otdir_path = NULL;
char *objects_path = NULL;
char *config_path = NULL;
GFile *otdir = NULL;
GFile *objects_dir = NULL;
GFile *configf = NULL;
context = g_option_context_new ("- Initialize a new empty repository");
g_option_context_add_main_entries (context, options, NULL);
@ -55,12 +61,26 @@ ostree_builtin_init (int argc, char **argv, const char *prefix, GError **error)
objects_dir = g_file_new_for_path (objects_path);
if (!g_file_make_directory (objects_dir, NULL, error))
goto out;
config_path = g_build_filename (repo_path, "config", NULL);
configf = g_file_new_for_path (config_path);
if (!g_file_replace_contents (configf,
DEFAULT_CONFIG_CONTENTS,
strlen (DEFAULT_CONFIG_CONTENTS),
NULL, FALSE, 0, NULL,
NULL, error))
goto out;
ret = TRUE;
out:
if (context)
g_option_context_free (context);
g_free (otdir_path);
g_free (objects_path);
g_free (config_path);
g_clear_object (&otdir);
g_clear_object (&objects_dir);
g_clear_object (&configf);
return ret;
}