sysdir.c 7.9 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * 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 "sysdir.h"
9

10 11 12 13 14 15
#include "global.h"
#include "buffer.h"
#include "path.h"
#include <ctype.h>
#if GIT_WIN32
#include "win32/findfile.h"
16 17 18
#else
#include <unistd.h>
#include <pwd.h>
19 20
#endif

21 22 23 24 25 26 27 28 29 30
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
}

31 32 33 34 35 36 37 38 39
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
}

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#ifndef GIT_WIN32
static int get_passwd_home(git_buf *out, uid_t uid)
{
	struct passwd pwd, *pwdptr;
	char *buf = NULL;
	long buflen;
	int error;

	assert(out);

	if ((buflen = sysconf(_SC_GETPW_R_SIZE_MAX)) == -1)
		buflen = 1024;

	do {
		buf = git__realloc(buf, buflen);
		error = getpwuid_r(uid, &pwd, buf, buflen, &pwdptr);
		buflen *= 2;
	} while (error == ERANGE && buflen <= 8192);

	if (error) {
60
		git_error_set(GIT_ERROR_OS, "failed to get passwd entry");
61 62 63 64
		goto out;
	}

	if (!pwdptr) {
65
		git_error_set(GIT_ERROR_OS, "no passwd entry found for user");
66 67 68 69 70 71 72 73 74 75 76 77
		goto out;
	}

	if ((error = git_buf_puts(out, pwdptr->pw_dir)) < 0)
		goto out;

out:
	git__free(buf);
	return error;
}
#endif

78 79 80 81 82
static int git_sysdir_guess_global_dirs(git_buf *out)
{
#ifdef GIT_WIN32
	return git_win32__find_global_dirs(out);
#else
83 84
	int error;
	uid_t uid, euid;
85
	const char *sandbox_id;
86 87 88 89

	uid = getuid();
	euid = geteuid();

90 91 92 93 94 95
	/**
	 * If APP_SANDBOX_CONTAINER_ID is set, we are running in a
	 * sandboxed environment on macOS.
	 */
	sandbox_id = getenv("APP_SANDBOX_CONTAINER_ID");

96 97 98
	/*
	 * In case we are running setuid, use the configuration
	 * of the effective user.
99 100 101
	 *
	 * If we are running in a sandboxed environment on macOS,
	 * we have to get the HOME dir from the password entry file.
102
	 */
103
	if (!sandbox_id && uid == euid)
104 105 106
	    error = git__getenv(out, "HOME");
	else
	    error = get_passwd_home(out, euid);
107 108

	if (error == GIT_ENOTFOUND) {
109
		git_error_clear();
110 111 112 113
		error = 0;
	}

	return error;
114 115 116 117 118 119 120 121
#endif
}

static int git_sysdir_guess_xdg_dirs(git_buf *out)
{
#ifdef GIT_WIN32
	return git_win32__find_xdg_dirs(out);
#else
122 123
	git_buf env = GIT_BUF_INIT;
	int error;
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
	uid_t uid, euid;

	uid = getuid();
	euid = geteuid();

	/*
	 * In case we are running setuid, only look up passwd
	 * directory of the effective user.
	 */
	if (uid == euid) {
		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");
	} else {
		if ((error = get_passwd_home(&env, euid)) == 0)
			error = git_buf_joinpath(out, env.ptr, ".config/git");
	}
143

144
	if (error == GIT_ENOTFOUND) {
145
		git_error_clear();
146 147
		error = 0;
	}
148

149
	git_buf_dispose(&env);
150
	return error;
151 152 153 154 155 156 157 158 159 160 161 162
#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
}

163 164 165 166
struct git_sysdir__dir {
	git_buf buf;
	int (*guess)(git_buf *out);
};
167

168 169 170 171 172 173
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 },
174 175
};

176 177 178 179 180
static void git_sysdir_global_shutdown(void)
{
	size_t i;

	for (i = 0; i < ARRAY_SIZE(git_sysdir__dirs); ++i)
181
		git_buf_dispose(&git_sysdir__dirs[i].buf);
182
}
183 184 185

int git_sysdir_global_init(void)
{
186
	size_t i;
187 188
	int error = 0;

189 190
	for (i = 0; !error && i < ARRAY_SIZE(git_sysdir__dirs); i++)
		error = git_sysdir__dirs[i].guess(&git_sysdir__dirs[i].buf);
191

192
	git__on_shutdown(git_sysdir_global_shutdown);
193

194
	return error;
195 196 197 198
}

static int git_sysdir_check_selector(git_sysdir_t which)
{
199
	if (which < ARRAY_SIZE(git_sysdir__dirs))
200 201
		return 0;

202
	git_error_set(GIT_ERROR_INVALID, "config directory selector out of range");
203 204 205 206 207 208 209 210 211 212
	return -1;
}


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

	*out = NULL;

213
	GIT_ERROR_CHECK_ERROR(git_sysdir_check_selector(which));
214

215
	*out = &git_sysdir__dirs[which].buf;
216 217 218 219 220 221 222 223 224 225
	return 0;
}

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

226 227
	GIT_ERROR_CHECK_ERROR(git_sysdir_check_selector(which));
	GIT_ERROR_CHECK_ERROR(git_sysdir_get(&path, which));
228 229

	if (!out || path->size >= outlen) {
230
		git_error_set(GIT_ERROR_NOMEMORY, "buffer is too short for the path");
231 232 233 234 235 236 237 238 239 240 241 242 243 244
		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;

245
	GIT_ERROR_CHECK_ERROR(git_sysdir_check_selector(which));
246 247 248 249

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

250
	/* reset the default if this path has been cleared */
251
	if (!search_path)
252
		git_sysdir__dirs[which].guess(&git_sysdir__dirs[which].buf);
253 254

	/* if $PATH is not referenced, then just set the path */
255 256 257 258 259 260
	if (!expand_path) {
		if (search_path)
			git_buf_sets(&git_sysdir__dirs[which].buf, search_path);

		goto done;
	}
261 262 263 264 265

	/* 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);

266
	if (git_buf_len(&git_sysdir__dirs[which].buf))
267
		git_buf_join(&merge, GIT_PATH_LIST_SEPARATOR,
268
			merge.ptr, git_sysdir__dirs[which].buf.ptr);
269 270 271 272 273

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

274
	git_buf_swap(&git_sysdir__dirs[which].buf, &merge);
275
	git_buf_dispose(&merge);
276

277 278 279 280 281
done:
	if (git_buf_oom(&git_sysdir__dirs[which].buf))
		return -1;

	return 0;
282 283 284 285 286 287 288 289 290 291 292 293
}

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;

294
	GIT_ERROR_CHECK_ERROR(git_sysdir_get(&syspath, which));
295 296
	if (!syspath || !git_buf_len(syspath))
		goto done;
297 298

	for (scan = git_buf_cstr(syspath); scan; scan = next) {
299 300 301 302 303 304
		/* find unescaped separator or end of string */
		for (next = scan; *next; ++next) {
			if (*next == GIT_PATH_LIST_SEPARATOR &&
				(next <= scan || next[-1] != '\\'))
				break;
		}
305

306 307
		len = (size_t)(next - scan);
		next = (*next ? next + 1 : NULL);
308 309 310
		if (!len)
			continue;

311
		GIT_ERROR_CHECK_ERROR(git_buf_set(path, scan, len));
312
		if (name)
313
			GIT_ERROR_CHECK_ERROR(git_buf_joinpath(path, path->ptr, name));
314 315 316 317 318

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

319
done:
320
	git_buf_dispose(path);
321
	git_error_set(GIT_ERROR_OS, "the %s file '%s' doesn't exist", label, name);
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
	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");
}

343 344 345 346 347 348
int git_sysdir_find_programdata_file(git_buf *path, const char *filename)
{
	return git_sysdir_find_in_dirlist(
		path, filename, GIT_SYSDIR_PROGRAMDATA, "ProgramData");
}

349 350 351 352 353 354
int git_sysdir_find_template_dir(git_buf *path)
{
	return git_sysdir_find_in_dirlist(
		path, NULL, GIT_SYSDIR_TEMPLATE, "template");
}

355 356 357 358 359 360 361 362 363 364 365
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;
}