config_snapshot.c 5.19 KB
Newer Older
1 2 3 4 5 6 7
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

8
#include "config_backend.h"
9

10
#include "config.h"
11 12 13 14 15 16 17
#include "config_entries.h"

typedef struct {
	git_config_backend parent;
	git_mutex values_mutex;
	git_config_entries *entries;
	git_config_backend *source;
18
} config_snapshot_backend;
19 20 21 22 23 24 25

static int config_error_readonly(void)
{
	git_error_set(GIT_ERROR_CONFIG, "this backend is read-only");
	return -1;
}

26
static int config_snapshot_iterator(
27 28 29
	git_config_iterator **iter,
	struct git_config_backend *backend)
{
30
	config_snapshot_backend *b = GIT_CONTAINER_OF(backend, config_snapshot_backend, parent);
31 32 33 34 35 36 37 38 39 40 41 42 43 44
	git_config_entries *entries = NULL;
	int error;

	if ((error = git_config_entries_dup(&entries, b->entries)) < 0 ||
	    (error = git_config_entries_iterator_new(iter, entries)) < 0)
		goto out;

out:
	/* Let iterator delete duplicated entries when it's done */
	git_config_entries_free(entries);
	return error;
}

/* release the map containing the entry as an equivalent to freeing it */
45
static void config_snapshot_entry_free(git_config_entry *entry)
46 47 48 49 50
{
	git_config_entries *entries = (git_config_entries *) entry->payload;
	git_config_entries_free(entries);
}

51
static int config_snapshot_get(git_config_backend *cfg, const char *key, git_config_entry **out)
52
{
53
	config_snapshot_backend *b = GIT_CONTAINER_OF(cfg, config_snapshot_backend, parent);
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	git_config_entries *entries = NULL;
	git_config_entry *entry;
	int error = 0;

	if (git_mutex_lock(&b->values_mutex) < 0) {
	    git_error_set(GIT_ERROR_OS, "failed to lock config backend");
	    return -1;
	}

	entries = b->entries;
	git_config_entries_incref(entries);
	git_mutex_unlock(&b->values_mutex);

	if ((error = (git_config_entries_get(&entry, entries, key))) < 0) {
		git_config_entries_free(entries);
		return error;
	}

72
	entry->free = config_snapshot_entry_free;
73 74 75 76 77 78
	entry->payload = entries;
	*out = entry;

	return 0;
}

79
static int config_snapshot_set(git_config_backend *cfg, const char *name, const char *value)
80 81 82 83 84 85 86 87
{
	GIT_UNUSED(cfg);
	GIT_UNUSED(name);
	GIT_UNUSED(value);

	return config_error_readonly();
}

88
static int config_snapshot_set_multivar(
89 90 91 92 93 94 95 96 97 98
	git_config_backend *cfg, const char *name, const char *regexp, const char *value)
{
	GIT_UNUSED(cfg);
	GIT_UNUSED(name);
	GIT_UNUSED(regexp);
	GIT_UNUSED(value);

	return config_error_readonly();
}

99
static int config_snapshot_delete_multivar(git_config_backend *cfg, const char *name, const char *regexp)
100 101 102 103 104 105 106 107
{
	GIT_UNUSED(cfg);
	GIT_UNUSED(name);
	GIT_UNUSED(regexp);

	return config_error_readonly();
}

108
static int config_snapshot_delete(git_config_backend *cfg, const char *name)
109 110 111 112 113 114 115
{
	GIT_UNUSED(cfg);
	GIT_UNUSED(name);

	return config_error_readonly();
}

116
static int config_snapshot_lock(git_config_backend *_cfg)
117 118 119 120 121 122
{
	GIT_UNUSED(_cfg);

	return config_error_readonly();
}

123
static int config_snapshot_unlock(git_config_backend *_cfg, int success)
124 125 126 127 128 129 130
{
	GIT_UNUSED(_cfg);
	GIT_UNUSED(success);

	return config_error_readonly();
}

131
static void config_snapshot_free(git_config_backend *_backend)
132
{
133
	config_snapshot_backend *backend = GIT_CONTAINER_OF(_backend, config_snapshot_backend, parent);
134 135 136 137 138 139 140 141 142

	if (backend == NULL)
		return;

	git_config_entries_free(backend->entries);
	git_mutex_free(&backend->values_mutex);
	git__free(backend);
}

143
static int config_snapshot_open(git_config_backend *cfg, git_config_level_t level, const git_repository *repo)
144
{
145
	config_snapshot_backend *b = GIT_CONTAINER_OF(cfg, config_snapshot_backend, parent);
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
	git_config_entries *entries = NULL;
	git_config_iterator *it = NULL;
	git_config_entry *entry;
	int error;

	/* We're just copying data, don't care about the level or repo*/
	GIT_UNUSED(level);
	GIT_UNUSED(repo);

	if ((error = git_config_entries_new(&entries)) < 0 ||
	    (error = b->source->iterator(&it, b->source)) < 0)
		goto out;

	while ((error = git_config_next(&entry, it)) == 0)
		if ((error = git_config_entries_dup_entry(entries, entry)) < 0)
			goto out;

	if (error < 0) {
		if (error != GIT_ITEROVER)
			goto out;
		error = 0;
	}

	b->entries = entries;

out:
	git_config_iterator_free(it);
	if (error)
		git_config_entries_free(entries);
	return error;
}

int git_config_backend_snapshot(git_config_backend **out, git_config_backend *source)
{
180
	config_snapshot_backend *backend;
181

182
	backend = git__calloc(1, sizeof(config_snapshot_backend));
183 184 185 186 187 188 189 190 191
	GIT_ERROR_CHECK_ALLOC(backend);

	backend->parent.version = GIT_CONFIG_BACKEND_VERSION;
	git_mutex_init(&backend->values_mutex);

	backend->source = source;

	backend->parent.readonly = 1;
	backend->parent.version = GIT_CONFIG_BACKEND_VERSION;
192 193 194 195
	backend->parent.open = config_snapshot_open;
	backend->parent.get = config_snapshot_get;
	backend->parent.set = config_snapshot_set;
	backend->parent.set_multivar = config_snapshot_set_multivar;
196
	backend->parent.snapshot = git_config_backend_snapshot;
197 198 199 200 201 202
	backend->parent.del = config_snapshot_delete;
	backend->parent.del_multivar = config_snapshot_delete_multivar;
	backend->parent.iterator = config_snapshot_iterator;
	backend->parent.lock = config_snapshot_lock;
	backend->parent.unlock = config_snapshot_unlock;
	backend->parent.free = config_snapshot_free;
203 204 205 206 207

	*out = &backend->parent;

	return 0;
}