findfile.c 5.25 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3 4 5 6 7 8 9 10 11
 *
 * 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 "utf-conv.h"
#include "path.h"
#include "findfile.h"

12
#define REG_MSYSGIT_INSTALL_LOCAL L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Git_is1"
13

14
#ifndef _WIN64
15
#define REG_MSYSGIT_INSTALL REG_MSYSGIT_INSTALL_LOCAL
16 17 18 19
#else
#define REG_MSYSGIT_INSTALL L"SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Git_is1"
#endif

20 21 22 23
typedef struct {
	git_win32_path path;
	DWORD len;
} _findfile_path;
24

25
static int git_win32__expand_path(_findfile_path *dest, const wchar_t *src)
26
{
27
	dest->len = ExpandEnvironmentStringsW(src, dest->path, ARRAY_SIZE(dest->path));
28

29 30
	if (!dest->len || dest->len > ARRAY_SIZE(dest->path))
		return -1;
31

32
	return 0;
33 34
}

35
static int win32_path_to_8(git_buf *dest, const wchar_t *src)
36
{
37
	git_win32_utf8_path utf8_path;
38

39 40 41
	if (git_win32_path_to_utf8(utf8_path, src) < 0) {
		giterr_set(GITERR_OS, "Unable to convert path to UTF-8");
		return -1;
42 43
	}

44 45
	/* Convert backslashes to forward slashes */
	git_path_mkposix(utf8_path);
46

47
	return git_buf_sets(dest, utf8_path);
48 49
}

50
static wchar_t* win32_walkpath(wchar_t *path, wchar_t *buf, size_t buflen)
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
{
	wchar_t term, *base = path;

	assert(path && buf && buflen);

	term = (*path == L'"') ? *path++ : L';';

	for (buflen--; *path && *path != term && buflen; buflen--)
		*buf++ = *path++;

	*buf = L'\0'; /* reserved a byte via initial subtract */

	while (*path == term || *path == L';')
		path++;

	return (path != base) ? path : NULL;
}

69
static int win32_find_git_in_path(git_buf *buf, const wchar_t *gitexe, const wchar_t *subdir)
70
{
71
	wchar_t *env = _wgetenv(L"PATH"), lastch;
72
	_findfile_path root;
73
	size_t gitexe_len = wcslen(gitexe);
74 75 76 77

	if (!env)
		return -1;

78 79 80 81 82 83 84 85 86
	while ((env = win32_walkpath(env, root.path, MAX_PATH-1)) && *root.path) {
		root.len = (DWORD)wcslen(root.path);
		lastch = root.path[root.len - 1];

		/* ensure trailing slash (MAX_PATH-1 to walkpath guarantees space) */
		if (lastch != L'/' && lastch != L'\\') {
			root.path[root.len++] = L'\\';
			root.path[root.len]   = L'\0';
		}
87

88 89 90
		if (root.len + gitexe_len >= MAX_PATH)
			continue;
		wcscpy(&root.path[root.len], gitexe);
91

92
		if (_waccess(root.path, F_OK) == 0 && root.len > 5) {
93 94
			/* replace "bin\\" or "cmd\\" with subdir */
			wcscpy(&root.path[root.len - 4], subdir);
95

96
			win32_path_to_8(buf, root.path);
97
			return 0;
98 99
		}
	}
100

101 102 103
	return GIT_ENOTFOUND;
}

104
static int win32_find_git_in_registry(
105
	git_buf *buf, const HKEY hive, const wchar_t *key, const wchar_t *subdir)
106
{
107
	HKEY hKey;
108
	int error = GIT_ENOTFOUND;
109 110

	assert(buf);
111

112 113 114
	if (!RegOpenKeyExW(hive, key, 0, KEY_READ, &hKey)) {
		DWORD dwType, cbData;
		git_win32_path path;
115

116 117 118
		/* Ensure that the buffer is big enough to have the suffix attached
		 * after we receive the result. */
		cbData = (DWORD)(sizeof(path) - wcslen(subdir) * sizeof(wchar_t));
119

120 121
		/* InstallLocation points to the root of the git directory */
		if (!RegQueryValueExW(hKey, L"InstallLocation", NULL, &dwType, (LPBYTE)path, &cbData) &&
122
			dwType == REG_SZ) {
123

124 125
			/* Append the suffix */
			wcscat(path, subdir);
126

127 128 129 130
			/* Convert to UTF-8, with forward slashes, and output the path
			 * to the provided buffer */
			if (!win32_path_to_8(buf, path))
				error = 0;
131
		}
132

133
		RegCloseKey(hKey);
134 135
	}

136
	return error;
137 138
}

139
static int win32_find_existing_dirs(
nulltoken committed
140
	git_buf *out, const wchar_t *tmpl[])
141
{
142
	_findfile_path path16;
143 144
	git_buf buf = GIT_BUF_INIT;

145 146 147
	git_buf_clear(out);

	for (; *tmpl != NULL; tmpl++) {
Russell Belfer committed
148
		if (!git_win32__expand_path(&path16, *tmpl) &&
149 150 151
			path16.path[0] != L'%' &&
			!_waccess(path16.path, F_OK))
		{
152
			win32_path_to_8(&buf, path16.path);
153 154 155

			if (buf.size)
				git_buf_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr);
156 157 158
		}
	}

159 160 161
	git_buf_free(&buf);

	return (git_buf_oom(out) ? -1 : 0);
162
}
163

164
int git_win32__find_system_dirs(git_buf *out, const wchar_t *subdir)
165 166 167 168
{
	git_buf buf = GIT_BUF_INIT;

	/* directories where git.exe & git.cmd are found */
169
	if (!win32_find_git_in_path(&buf, L"git.exe", subdir) && buf.size)
170 171 172
		git_buf_set(out, buf.ptr, buf.size);
	else
		git_buf_clear(out);
173

174
	if (!win32_find_git_in_path(&buf, L"git.cmd", subdir) && buf.size)
175
		git_buf_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr);
176 177 178

	/* directories where git is installed according to registry */
	if (!win32_find_git_in_registry(
179
			&buf, HKEY_CURRENT_USER, REG_MSYSGIT_INSTALL_LOCAL, subdir) && buf.size)
180
		git_buf_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr);
181 182

	if (!win32_find_git_in_registry(
183
			&buf, HKEY_LOCAL_MACHINE, REG_MSYSGIT_INSTALL, subdir) && buf.size)
184 185 186
		git_buf_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr);

	git_buf_free(&buf);
187

188
	return (git_buf_oom(out) ? -1 : 0);
189 190
}

Russell Belfer committed
191
int git_win32__find_global_dirs(git_buf *out)
192 193 194 195 196 197 198 199
{
	static const wchar_t *global_tmpls[4] = {
		L"%HOME%\\",
		L"%HOMEDRIVE%%HOMEPATH%\\",
		L"%USERPROFILE%\\",
		NULL,
	};

nulltoken committed
200
	return win32_find_existing_dirs(out, global_tmpls);
201 202
}

Russell Belfer committed
203
int git_win32__find_xdg_dirs(git_buf *out)
204 205 206 207 208 209 210 211 212 213 214
{
	static const wchar_t *global_tmpls[7] = {
		L"%XDG_CONFIG_HOME%\\git",
		L"%APPDATA%\\git",
		L"%LOCALAPPDATA%\\git",
		L"%HOME%\\.config\\git",
		L"%HOMEDRIVE%%HOMEPATH%\\.config\\git",
		L"%USERPROFILE%\\.config\\git",
		NULL,
	};

nulltoken committed
215
	return win32_find_existing_dirs(out, global_tmpls);
216
}