config_entries.c 5.46 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * 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.
 */

#include "config_entries.h"

10 11 12 13 14 15
typedef struct config_entry_list {
	struct config_entry_list *next;
	struct config_entry_list *last;
	git_config_entry *entry;
} config_entry_list;

16 17 18 19 20
typedef struct {
	git_config_entry *entry;
	bool multivar;
} config_entry_map_head;

21 22
typedef struct config_entries_iterator {
	git_config_iterator parent;
23
	git_config_entries *entries;
24 25 26
	config_entry_list *head;
} config_entries_iterator;

27 28 29 30 31 32
struct git_config_entries {
	git_refcount rc;
	git_strmap *map;
	config_entry_list *list;
};

33
int git_config_entries_new(git_config_entries **out)
34
{
35
	git_config_entries *entries;
36 37
	int error;

38
	entries = git__calloc(1, sizeof(git_config_entries));
39
	GIT_ERROR_CHECK_ALLOC(entries);
40
	GIT_REFCOUNT_INC(entries);
41

42
	if ((error = git_strmap_new(&entries->map)) < 0)
43 44 45 46 47 48 49
		git__free(entries);
	else
		*out = entries;

	return error;
}

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
int git_config_entries_dup_entry(git_config_entries *entries, const git_config_entry *entry)
{
	git_config_entry *duplicated;
	int error;

	duplicated = git__calloc(1, sizeof(git_config_entry));
	GIT_ERROR_CHECK_ALLOC(duplicated);

	duplicated->name = git__strdup(entry->name);
	GIT_ERROR_CHECK_ALLOC(duplicated->name);

	if (entry->value) {
		duplicated->value = git__strdup(entry->value);
		GIT_ERROR_CHECK_ALLOC(duplicated->value);
	}
	duplicated->level = entry->level;
	duplicated->include_depth = entry->include_depth;

	if ((error = git_config_entries_append(entries, duplicated)) < 0)
		goto out;

out:
	if (error && duplicated) {
		git__free((char *) duplicated->name);
		git__free((char *) duplicated->value);
		git__free(duplicated);
	}
	return error;
}

80 81 82 83 84 85 86 87 88
int git_config_entries_dup(git_config_entries **out, git_config_entries *entries)
{
	git_config_entries *result = NULL;
	config_entry_list *head;
	int error;

	if ((error = git_config_entries_new(&result)) < 0)
		goto out;

89 90
	for (head = entries->list; head; head = head->next)
		if ((git_config_entries_dup_entry(result, head->entry)) < 0)
91 92 93 94 95 96 97 98 99 100
			goto out;

	*out = result;
	result = NULL;

out:
	git_config_entries_free(result);
	return error;
}

101
void git_config_entries_incref(git_config_entries *entries)
102
{
103 104
	GIT_REFCOUNT_INC(entries);
}
105

106 107 108
static void config_entries_free(git_config_entries *entries)
{
	config_entry_list *list = NULL, *next;
109
	config_entry_map_head *head;
110

111 112 113
	git_strmap_foreach_value(entries->map, head,
		git__free((char *) head->entry->name); git__free(head)
	);
114 115 116 117 118
	git_strmap_free(entries->map);

	list = entries->list;
	while (list != NULL) {
		next = list->next;
119 120
		git__free((char *) list->entry->value);
		git__free(list->entry);
121 122 123 124 125 126 127
		git__free(list);
		list = next;
	}

	git__free(entries);
}

128 129 130 131 132 133
void git_config_entries_free(git_config_entries *entries)
{
	if (entries)
		GIT_REFCOUNT_DEC(entries, config_entries_free);
}

134
int git_config_entries_append(git_config_entries *entries, git_config_entry *entry)
135
{
136 137 138 139 140 141 142 143 144 145 146
	config_entry_list *list_head;
	config_entry_map_head *map_head;

	if ((map_head = git_strmap_get(entries->map, entry->name)) != NULL) {
		map_head->multivar = true;
		/*
		 * This is a micro-optimization for configuration files
		 * with a lot of same keys. As for multivars the entry's
		 * key will be the same for all entries, we can just free
		 * all except the first entry's name and just re-use it.
		 */
147
		git__free((char *) entry->name);
148
		entry->name = map_head->entry->name;
149
	} else {
150 151 152
		map_head = git__calloc(1, sizeof(*map_head));
		if ((git_strmap_set(entries->map, entry->name, map_head)) < 0)
			return -1;
153
	}
154 155 156 157 158
	map_head->entry = entry;

	list_head = git__calloc(1, sizeof(config_entry_list));
	GIT_ERROR_CHECK_ALLOC(list_head);
	list_head->entry = entry;
159

160
	if (entries->list)
161
		entries->list->last->next = list_head;
162
	else
163 164
		entries->list = list_head;
	entries->list->last = list_head;
165 166 167 168 169 170

	return 0;
}

int git_config_entries_get(git_config_entry **out, git_config_entries *entries, const char *key)
{
171
	config_entry_map_head *entry;
172 173 174
	if ((entry = git_strmap_get(entries->map, key)) == NULL)
		return GIT_ENOTFOUND;
	*out = entry->entry;
175 176 177 178 179
	return 0;
}

int git_config_entries_get_unique(git_config_entry **out, git_config_entries *entries, const char *key)
{
180
	config_entry_map_head *entry;
181

182 183
	if ((entry = git_strmap_get(entries->map, key)) == NULL)
		return GIT_ENOTFOUND;
184

185
	if (entry->multivar) {
186
		git_error_set(GIT_ERROR_CONFIG, "entry is not unique due to being a multivar");
187 188 189 190
		return -1;
	}

	if (entry->entry->include_depth) {
191
		git_error_set(GIT_ERROR_CONFIG, "entry is not unique due to being included");
192 193 194 195 196 197 198 199
		return -1;
	}

	*out = entry->entry;

	return 0;
}

200
static void config_iterator_free(git_config_iterator *iter)
201
{
202 203 204
	config_entries_iterator *it = (config_entries_iterator *) iter;
	git_config_entries_free(it->entries);
	git__free(it);
205 206
}

207
static int config_iterator_next(
208 209 210
	git_config_entry **entry,
	git_config_iterator *iter)
{
211
	config_entries_iterator *it = (config_entries_iterator *) iter;
212 213 214 215 216 217 218 219 220

	if (!it->head)
		return GIT_ITEROVER;

	*entry = it->head->entry;
	it->head = it->head->next;

	return 0;
}
221

222
int git_config_entries_iterator_new(git_config_iterator **out, git_config_entries *entries)
223 224 225 226
{
	config_entries_iterator *it;

	it = git__calloc(1, sizeof(config_entries_iterator));
227
	GIT_ERROR_CHECK_ALLOC(it);
228 229
	it->parent.next = config_iterator_next;
	it->parent.free = config_iterator_free;
230 231
	it->head = entries->list;
	it->entries = entries;
232

233
	git_config_entries_incref(entries);
234 235 236 237
	*out = &it->parent;

	return 0;
}