sysdir.c 6.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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 "common.h"
#include "sysdir.h"
#include "global.h"
#include "buffer.h"
#include "path.h"
#include <ctype.h>
#if GIT_WIN32
#include "win32/findfile.h"
#endif

18 19 20 21 22 23 24 25 26 27
static int git_sysdir_guess_programdata_dirs(git_buf *out)
{
#ifdef GIT_WIN32
	return git_win32__find_programdata_dirs(out);
#else
	git_buf_clear(out);
	return 0;
#endif
}

28 29 30 31 32 33 34 35 36 37 38 39 40 41
static int git_sysdir_guess_system_dirs(git_buf *out)
{
#ifdef GIT_WIN32
	return git_win32__find_system_dirs(out, L"etc\\");
#else
	return git_buf_sets(out, "/etc");
#endif
}

static int git_sysdir_guess_global_dirs(git_buf *out)
{
#ifdef GIT_WIN32
	return git_win32__find_global_dirs(out);
#else
42 43 44 45 46 47 48 49
	int error = git__getenv(out, "HOME");

	if (error == GIT_ENOTFOUND) {
		giterr_clear();
		error = 0;
	}

	return error;
50 51 52 53 54 55 56 57
#endif
}

static int git_sysdir_guess_xdg_dirs(git_buf *out)
{
#ifdef GIT_WIN32
	return git_win32__find_xdg_dirs(out);
#else
58 59 60 61 62 63 64 65
	git_buf env = GIT_BUF_INIT;
	int error;

	if ((error = git__getenv(&env, "XDG_CONFIG_HOME")) == 0)
		error = git_buf_joinpath(out, env.ptr, "git");

	if (error == GIT_ENOTFOUND && (error = git__getenv(&env, "HOME")) == 0)
		error = git_buf_joinpath(out, env.ptr, ".config/git");
66

67 68 69 70
	if (error == GIT_ENOTFOUND) {
		giterr_clear();
		error = 0;
	}
71

72 73
	git_buf_free(&env);
	return error;
74 75 76 77 78 79 80 81 82 83 84 85
#endif
}

static int git_sysdir_guess_template_dirs(git_buf *out)
{
#ifdef GIT_WIN32
	return git_win32__find_system_dirs(out, L"share\\git-core\\templates");
#else
	return git_buf_sets(out, "/usr/share/git-core/templates");
#endif
}

86 87 88 89
struct git_sysdir__dir {
	git_buf buf;
	int (*guess)(git_buf *out);
};
90

91 92 93 94 95 96
static struct git_sysdir__dir git_sysdir__dirs[] = {
	{ GIT_BUF_INIT, git_sysdir_guess_system_dirs },
	{ GIT_BUF_INIT, git_sysdir_guess_global_dirs },
	{ GIT_BUF_INIT, git_sysdir_guess_xdg_dirs },
	{ GIT_BUF_INIT, git_sysdir_guess_programdata_dirs },
	{ GIT_BUF_INIT, git_sysdir_guess_template_dirs },
97 98
};

99 100 101 102 103 104 105
static void git_sysdir_global_shutdown(void)
{
	size_t i;

	for (i = 0; i < ARRAY_SIZE(git_sysdir__dirs); ++i)
		git_buf_free(&git_sysdir__dirs[i].buf);
}
106 107 108

int git_sysdir_global_init(void)
{
109
	size_t i;
110 111
	int error = 0;

112 113
	for (i = 0; !error && i < ARRAY_SIZE(git_sysdir__dirs); i++)
		error = git_sysdir__dirs[i].guess(&git_sysdir__dirs[i].buf);
114

115
	git__on_shutdown(git_sysdir_global_shutdown);
116

117
	return error;
118 119 120 121
}

static int git_sysdir_check_selector(git_sysdir_t which)
{
122
	if (which < ARRAY_SIZE(git_sysdir__dirs))
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
		return 0;

	giterr_set(GITERR_INVALID, "config directory selector out of range");
	return -1;
}


int git_sysdir_get(const git_buf **out, git_sysdir_t which)
{
	assert(out);

	*out = NULL;

	GITERR_CHECK_ERROR(git_sysdir_check_selector(which));

138
	*out = &git_sysdir__dirs[which].buf;
139 140 141 142 143 144 145 146 147 148 149 150 151 152
	return 0;
}

int git_sysdir_get_str(
	char *out,
	size_t outlen,
	git_sysdir_t which)
{
	const git_buf *path = NULL;

	GITERR_CHECK_ERROR(git_sysdir_check_selector(which));
	GITERR_CHECK_ERROR(git_sysdir_get(&path, which));

	if (!out || path->size >= outlen) {
153
		giterr_set(GITERR_NOMEMORY, "buffer is too short for the path");
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
		return GIT_EBUFS;
	}

	git_buf_copy_cstr(out, outlen, path);
	return 0;
}

#define PATH_MAGIC "$PATH"

int git_sysdir_set(git_sysdir_t which, const char *search_path)
{
	const char *expand_path = NULL;
	git_buf merge = GIT_BUF_INIT;

	GITERR_CHECK_ERROR(git_sysdir_check_selector(which));

	if (search_path != NULL)
		expand_path = strstr(search_path, PATH_MAGIC);

173
	/* reset the default if this path has been cleared */
174
	if (!search_path)
175
		git_sysdir__dirs[which].guess(&git_sysdir__dirs[which].buf);
176 177

	/* if $PATH is not referenced, then just set the path */
178 179 180 181 182 183
	if (!expand_path) {
		if (search_path)
			git_buf_sets(&git_sysdir__dirs[which].buf, search_path);

		goto done;
	}
184 185 186 187 188

	/* otherwise set to join(before $PATH, old value, after $PATH) */
	if (expand_path > search_path)
		git_buf_set(&merge, search_path, expand_path - search_path);

189
	if (git_buf_len(&git_sysdir__dirs[which].buf))
190
		git_buf_join(&merge, GIT_PATH_LIST_SEPARATOR,
191
			merge.ptr, git_sysdir__dirs[which].buf.ptr);
192 193 194 195 196

	expand_path += strlen(PATH_MAGIC);
	if (*expand_path)
		git_buf_join(&merge, GIT_PATH_LIST_SEPARATOR, merge.ptr, expand_path);

197
	git_buf_swap(&git_sysdir__dirs[which].buf, &merge);
198 199
	git_buf_free(&merge);

200 201 202 203 204
done:
	if (git_buf_oom(&git_sysdir__dirs[which].buf))
		return -1;

	return 0;
205 206 207 208 209 210 211 212 213 214 215 216 217
}

static int git_sysdir_find_in_dirlist(
	git_buf *path,
	const char *name,
	git_sysdir_t which,
	const char *label)
{
	size_t len;
	const char *scan, *next = NULL;
	const git_buf *syspath;

	GITERR_CHECK_ERROR(git_sysdir_get(&syspath, which));
218 219
	if (!syspath || !git_buf_len(syspath))
		goto done;
220 221

	for (scan = git_buf_cstr(syspath); scan; scan = next) {
222 223 224 225 226 227
		/* find unescaped separator or end of string */
		for (next = scan; *next; ++next) {
			if (*next == GIT_PATH_LIST_SEPARATOR &&
				(next <= scan || next[-1] != '\\'))
				break;
		}
228

229 230
		len = (size_t)(next - scan);
		next = (*next ? next + 1 : NULL);
231 232 233 234 235 236 237 238 239 240 241
		if (!len)
			continue;

		GITERR_CHECK_ERROR(git_buf_set(path, scan, len));
		if (name)
			GITERR_CHECK_ERROR(git_buf_joinpath(path, path->ptr, name));

		if (git_path_exists(path->ptr))
			return 0;
	}

242
done:
243
	git_buf_free(path);
244
	giterr_set(GITERR_OS, "the %s file '%s' doesn't exist", label, name);
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
	return GIT_ENOTFOUND;
}

int git_sysdir_find_system_file(git_buf *path, const char *filename)
{
	return git_sysdir_find_in_dirlist(
		path, filename, GIT_SYSDIR_SYSTEM, "system");
}

int git_sysdir_find_global_file(git_buf *path, const char *filename)
{
	return git_sysdir_find_in_dirlist(
		path, filename, GIT_SYSDIR_GLOBAL, "global");
}

int git_sysdir_find_xdg_file(git_buf *path, const char *filename)
{
	return git_sysdir_find_in_dirlist(
		path, filename, GIT_SYSDIR_XDG, "global/xdg");
}

266 267 268 269 270 271
int git_sysdir_find_programdata_file(git_buf *path, const char *filename)
{
	return git_sysdir_find_in_dirlist(
		path, filename, GIT_SYSDIR_PROGRAMDATA, "ProgramData");
}

272 273 274 275 276 277
int git_sysdir_find_template_dir(git_buf *path)
{
	return git_sysdir_find_in_dirlist(
		path, NULL, GIT_SYSDIR_TEMPLATE, "template");
}

278 279 280 281 282 283 284 285 286 287 288
int git_sysdir_expand_global_file(git_buf *path, const char *filename)
{
	int error;

	if ((error = git_sysdir_find_global_file(path, NULL)) == 0) {
		if (filename)
			error = git_buf_joinpath(path, path->ptr, filename);
	}

	return error;
}