config.c 8.96 KB
Newer Older
1
/*
Vicent Marti committed
2
 * Copyright (C) 2009-2011 the libgit2 contributors
3
 *
Vicent Marti committed
4 5
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
6 7 8 9 10 11
 */

#include "common.h"
#include "fileops.h"
#include "hashtable.h"
#include "config.h"
12
#include "git2/config.h"
13
#include "vector.h"
14 15 16
#if GIT_WIN32
# include <windows.h>
#endif
17 18 19

#include <ctype.h>

20
typedef struct {
21
	git_config_file *file;
22
	int priority;
23
} file_internal;
24

25
void git_config_free(git_config *cfg)
26
{
27
	unsigned int i;
28 29
	git_config_file *file;
	file_internal *internal;
30

31 32 33
	if (cfg == NULL)
		return;

34 35 36 37
	for(i = 0; i < cfg->files.length; ++i){
		internal = git_vector_get(&cfg->files, i);
		file = internal->file;
		file->free(file);
38
		free(internal);
39
	}
40

41
	git_vector_free(&cfg->files);
42
	free(cfg);
43 44
}

45
static int config_backend_cmp(const void *a, const void *b)
46
{
47 48
	const file_internal *bk_a = (const file_internal *)(a);
	const file_internal *bk_b = (const file_internal *)(b);
49 50

	return bk_b->priority - bk_a->priority;
51 52
}

53
int git_config_new(git_config **out)
54 55 56 57 58 59 60 61 62
{
	git_config *cfg;

	cfg = git__malloc(sizeof(git_config));
	if (cfg == NULL)
		return GIT_ENOMEM;

	memset(cfg, 0x0, sizeof(git_config));

63
	if (git_vector_init(&cfg->files, 3, config_backend_cmp) < 0) {
64 65
		free(cfg);
		return GIT_ENOMEM;
66
	}
67

68
	*out = cfg;
69

70 71
	return GIT_SUCCESS;
}
72

73 74 75 76 77 78 79 80 81 82 83
int git_config_add_file_ondisk(git_config *cfg, const char *path, int priority)
{
	git_config_file *file = NULL;
	int error;

	error = git_config_file__ondisk(&file, path);
	if (error < GIT_SUCCESS)
		return error;

	error = git_config_add_file(cfg, file, priority);
	if (error < GIT_SUCCESS) {
Vicent Marti committed
84 85 86 87 88
		/*
		 * free manually; the file is not owned by the config
		 * instance yet and will not be freed on cleanup
		 */
		file->free(file);
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
		return error;
	}

	return GIT_SUCCESS;
}

int git_config_open_ondisk(git_config **cfg, const char *path)
{
	int error;

	error = git_config_new(cfg);
	if (error < GIT_SUCCESS)
		return error;

	error = git_config_add_file_ondisk(*cfg, path, 1);
	if (error < GIT_SUCCESS)
		git_config_free(*cfg);

	return error;
}

110
int git_config_add_file(git_config *cfg, git_config_file *file, int priority)
111
{
112
	file_internal *internal;
113
	int error;
114

115
	assert(cfg && file);
116

117
	if ((error = file->open(file)) < GIT_SUCCESS)
118
		return git__rethrow(error, "Failed to open config file");
119

120
	internal = git__malloc(sizeof(file_internal));
121 122
	if (internal == NULL)
		return GIT_ENOMEM;
123

124
	internal->file = file;
125
	internal->priority = priority;
126

127
	if (git_vector_insert(&cfg->files, internal) < 0) {
128 129 130
		free(internal);
		return GIT_ENOMEM;
	}
131

132 133
	git_vector_sort(&cfg->files);
	internal->file->cfg = cfg;
134

135
	return GIT_SUCCESS;
136 137
}

138 139 140 141
/*
 * Loop over all the variables
 */

142
int git_config_foreach(git_config *cfg, int (*fn)(const char *, const char *, void *), void *data)
143 144
{
	int ret = GIT_SUCCESS;
145
	unsigned int i;
146 147
	file_internal *internal;
	git_config_file *file;
148

149 150 151 152
	for(i = 0; i < cfg->files.length && ret == 0; ++i) {
		internal = git_vector_get(&cfg->files, i);
		file = internal->file;
		ret = file->foreach(file, fn, data);
153
	}
154 155 156 157

	return ret;
}

158
int git_config_delete(git_config *cfg, const char *name)
159
{
Vicent Marti committed
160
	return git_config_set_string(cfg, name, NULL);
161
}
162

163
/**************
164
 * Setters
165
 **************/
166

167
int git_config_set_int64(git_config *cfg, const char *name, int64_t value)
168
{
Vicent Marti committed
169
	char str_value[32]; /* All numbers should fit in here */
170
	p_snprintf(str_value, sizeof(str_value), "%" PRId64, value);
Vicent Marti committed
171
	return git_config_set_string(cfg, name, str_value);
172
}
173

174
int git_config_set_int32(git_config *cfg, const char *name, int32_t value)
175
{
176
	return git_config_set_int64(cfg, name, (int64_t)value);
177 178
}

179 180
int git_config_set_bool(git_config *cfg, const char *name, int value)
{
Vicent Marti committed
181
	return git_config_set_string(cfg, name, value ? "true" : "false");
182
}
183

184 185
int git_config_set_string(git_config *cfg, const char *name, const char *value)
{
186 187
	file_internal *internal;
	git_config_file *file;
188

189 190
	if (cfg->files.length == 0)
		return git__throw(GIT_EINVALIDARGS, "Cannot set variable value; no files open in the `git_config` instance");
191

192 193
	internal = git_vector_get(&cfg->files, 0);
	file = internal->file;
194

195
	return file->set(file, name, value);
196 197
}

198 199 200
/***********
 * Getters
 ***********/
201

202
int git_config_get_int64(git_config *cfg, const char *name, int64_t *out)
203
{
204
	const char *value, *num_end;
205
	int ret;
206
	int64_t num;
207

208
	ret = git_config_get_string(cfg, name, &value);
209
	if (ret < GIT_SUCCESS)
210
		return git__rethrow(ret, "Failed to retrieve value for '%s'", name);
211

212
	ret = git__strtol64(&num, value, &num_end, 0);
213
	if (ret < GIT_SUCCESS)
214
		return git__rethrow(ret, "Failed to convert value for '%s'", name);
215 216

	switch (*num_end) {
217 218
	case 'g':
	case 'G':
219
		num *= 1024;
220 221
		/* fallthrough */

222
	case 'm':
223
	case 'M':
224 225
		num *= 1024;
		/* fallthrough */
226

227 228 229
	case 'k':
	case 'K':
		num *= 1024;
230

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
		/* check that that there are no more characters after the
		 * given modifier suffix */
		if (num_end[1] != '\0')
			return git__throw(GIT_EINVALIDTYPE,
				"Failed to get value for '%s'. Invalid type suffix", name);

		/* fallthrough */

	case '\0':
		*out = num;
		return GIT_SUCCESS;

	default:
		return git__throw(GIT_EINVALIDTYPE,
			"Failed to get value for '%s'. Value is of invalid type", name);
	}
247 248
}

249
int git_config_get_int32(git_config *cfg, const char *name, int32_t *out)
250
{
251 252 253
	int64_t tmp_long;
	int32_t tmp_int;
	int ret;
254

255
	ret = git_config_get_int64(cfg, name, &tmp_long);
256 257 258 259 260 261
	if (ret < GIT_SUCCESS)
		return git__rethrow(ret, "Failed to convert value for '%s'", name);
	
	tmp_int = tmp_long & 0xFFFFFFFF;
	if (tmp_int != tmp_long)
		return git__throw(GIT_EOVERFLOW, "Value for '%s' is too large", name);
262

263
	*out = tmp_int;
264 265 266 267

	return ret;
}

268
int git_config_get_bool(git_config *cfg, const char *name, int *out)
269
{
270
	const char *value;
271 272
	int error = GIT_SUCCESS;

273
	error = git_config_get_string(cfg, name, &value);
274
	if (error < GIT_SUCCESS)
Vicent Marti committed
275
		return git__rethrow(error, "Failed to get value for %s", name);
276 277 278 279 280

	/* A missing value means true */
	if (value == NULL) {
		*out = 1;
		return GIT_SUCCESS;
281 282
	}

283 284
	if (!strcasecmp(value, "true") ||
		!strcasecmp(value, "yes") ||
285
		!strcasecmp(value, "on")) {
286 287 288 289 290
		*out = 1;
		return GIT_SUCCESS;
	}
	if (!strcasecmp(value, "false") ||
		!strcasecmp(value, "no") ||
291
		!strcasecmp(value, "off")) {
292 293 294 295 296
		*out = 0;
		return GIT_SUCCESS;
	}

	/* Try to parse it as an integer */
297
	error = git_config_get_int32(cfg, name, out);
298 299
	if (error == GIT_SUCCESS)
		*out = !!(*out);
300

Vicent Marti committed
301 302
	if (error < GIT_SUCCESS)
		return git__rethrow(error, "Failed to get value for %s", name);
303 304 305
	return error;
}

306 307
int git_config_get_string(git_config *cfg, const char *name, const char **out)
{
308 309
	file_internal *internal;
	git_config_file *file;
310 311
	int error = GIT_ENOTFOUND;
	unsigned int i;
312

313 314
	if (cfg->files.length == 0)
		return git__throw(GIT_EINVALIDARGS, "Cannot get variable value; no files open in the `git_config` instance");
315

316 317 318
	for (i = 0; i < cfg->files.length; ++i) {
		internal = git_vector_get(&cfg->files, i);
		file = internal->file;
319 320
		if ((error = file->get(file, name, out)) == GIT_SUCCESS)
			return GIT_SUCCESS;
321
	}
322

323
	return git__throw(error, "Config value '%s' not found", name);
324 325
}

326 327
int git_config_find_global(char *global_config_path)
{
328
	const char *home;
329

330
	home = getenv("HOME");
331 332 333

#ifdef GIT_WIN32
	if (home == NULL)
334
		home = getenv("USERPROFILE");
335 336
#endif

337
	if (home == NULL)
338 339
		return git__throw(GIT_EOSERR, "Failed to open global config file. Cannot locate the user's home directory");

Vicent Marti committed
340
	git_path_join(global_config_path, home, GIT_CONFIG_FILENAME);
341

Vicent Marti committed
342
	if (git_futils_exists(global_config_path) < GIT_SUCCESS)
343 344 345 346 347
		return git__throw(GIT_EOSERR, "Failed to open global config file. The file does not exist");

	return GIT_SUCCESS;
}

348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364


#if GIT_WIN32
static int win32_find_system(char *system_config_path)
{
	const wchar_t *query = L"%PROGRAMFILES%\\Git\\etc\\gitconfig";
	wchar_t *apphome_utf16;
	char *apphome_utf8;
	DWORD size, ret;

	size = ExpandEnvironmentStringsW(query, NULL, 0);
	/* The function gave us the full size of the buffer in chars, including NUL */
	apphome_utf16 = git__malloc(size * sizeof(wchar_t));
	if (apphome_utf16 == NULL)
		return GIT_ENOMEM;

	ret = ExpandEnvironmentStringsW(query, apphome_utf16, size);
365
	if (ret != size)
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
		return git__throw(GIT_ERROR, "Failed to expand environment strings");

	if (_waccess(apphome_utf16, F_OK) < 0) {
		free(apphome_utf16);
		return GIT_ENOTFOUND;
	}

	apphome_utf8 = conv_utf16_to_utf8(apphome_utf16);
	free(apphome_utf16);

	if (strlen(apphome_utf8) >= GIT_PATH_MAX) {
		free(apphome_utf8);
		return git__throw(GIT_ESHORTBUFFER, "Path is too long");
	}

	strcpy(system_config_path, apphome_utf8);
	free(apphome_utf8);
	return GIT_SUCCESS;
}
#endif

int git_config_find_system(char *system_config_path)
{
	const char *etc = "/etc/gitconfig";

	if (git_futils_exists(etc) == GIT_SUCCESS) {
		memcpy(system_config_path, etc, strlen(etc) + 1);
		return GIT_SUCCESS;
	}

#if GIT_WIN32
	return win32_find_system(system_config_path);
#else
	return GIT_ENOTFOUND;
#endif
}

403 404 405 406 407 408 409 410 411 412 413
int git_config_open_global(git_config **out)
{
	int error;
	char global_path[GIT_PATH_MAX];

	if ((error = git_config_find_global(global_path)) < GIT_SUCCESS)
		return error;

	return git_config_open_ondisk(out, global_path);
}