findfile.c 5.78 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

Russell Belfer committed
20
int git_win32__expand_path(struct git_win32__path *s_root, const wchar_t *templ)
21 22 23 24 25
{
	s_root->len = ExpandEnvironmentStringsW(templ, s_root->path, MAX_PATH);
	return s_root->len ? 0 : -1;
}

26
static int win32_path_to_8(git_buf *path_utf8, const wchar_t *path)
27 28 29
{
	char temp_utf8[GIT_PATH_MAX];

30
	git__utf16_to_8(temp_utf8, GIT_PATH_MAX, path);
31 32 33 34 35
	git_path_mkposix(temp_utf8);

	return git_buf_sets(path_utf8, temp_utf8);
}

Russell Belfer committed
36 37
int git_win32__find_file(
	git_buf *path, const struct git_win32__path *root, const char *filename)
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
{
	size_t len, alloc_len;
	wchar_t *file_utf16 = NULL;

	if (!root || !filename || (len = strlen(filename)) == 0)
		return GIT_ENOTFOUND;

	/* allocate space for wchar_t path to file */
	alloc_len = root->len + len + 2;
	file_utf16 = git__calloc(alloc_len, sizeof(wchar_t));
	GITERR_CHECK_ALLOC(file_utf16);

	/* append root + '\\' + filename as wchar_t */
	memcpy(file_utf16, root->path, root->len * sizeof(wchar_t));

	if (*filename == '/' || *filename == '\\')
		filename++;

56
	git__utf8_to_16(file_utf16 + root->len - 1, alloc_len - root->len, filename);
57 58 59 60 61 62 63

	/* check access */
	if (_waccess(file_utf16, F_OK) < 0) {
		git__free(file_utf16);
		return GIT_ENOTFOUND;
	}

64
	win32_path_to_8(path, file_utf16);
65
	git__free(file_utf16);
66

67 68 69
	return 0;
}

70
static wchar_t* win32_walkpath(wchar_t *path, wchar_t *buf, size_t buflen)
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
{
	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;
}

89
static int win32_find_git_in_path(git_buf *buf, const wchar_t *gitexe, const wchar_t *subdir)
90
{
91
	wchar_t *env = _wgetenv(L"PATH"), lastch;
Russell Belfer committed
92
	struct git_win32__path root;
93
	size_t gitexe_len = wcslen(gitexe);
94 95 96 97

	if (!env)
		return -1;

98 99 100 101 102 103 104 105 106
	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';
		}
107

108 109 110
		if (root.len + gitexe_len >= MAX_PATH)
			continue;
		wcscpy(&root.path[root.len], gitexe);
111

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

116
			win32_path_to_8(buf, root.path);
117
			return 0;
118 119
		}
	}
120

121 122 123
	return GIT_ENOTFOUND;
}

124
static int win32_find_git_in_registry(
125
	git_buf *buf, const HKEY hieve, const wchar_t *key, const wchar_t *subdir)
126
{
127 128
	HKEY hKey;
	DWORD dwType = REG_SZ;
Russell Belfer committed
129
	struct git_win32__path path16;
130 131

	assert(buf);
132

133
	path16.len = MAX_PATH;
134

135
	if (RegOpenKeyExW(hieve, key, 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
136 137 138 139 140 141 142 143 144 145
		if (RegQueryValueExW(hKey, L"InstallLocation", NULL, &dwType,
			(LPBYTE)&path16.path, &path16.len) == ERROR_SUCCESS)
		{
			/* InstallLocation points to the root of the git directory */

			if (path16.len + 4 > MAX_PATH) { /* 4 = wcslen(L"etc\\") */
				giterr_set(GITERR_OS, "Cannot locate git - path too long");
				return -1;
			}

146
			wcscat(path16.path, subdir);
147 148
			path16.len += 4;

149
			win32_path_to_8(buf, path16.path);
150
		}
151

152
		RegCloseKey(hKey);
153 154
	}

155
	return path16.len ? 0 : GIT_ENOTFOUND;
156 157
}

158
static int win32_find_existing_dirs(
nulltoken committed
159
	git_buf *out, const wchar_t *tmpl[])
160
{
Russell Belfer committed
161
	struct git_win32__path path16;
162 163
	git_buf buf = GIT_BUF_INIT;

164 165 166
	git_buf_clear(out);

	for (; *tmpl != NULL; tmpl++) {
Russell Belfer committed
167
		if (!git_win32__expand_path(&path16, *tmpl) &&
168 169 170
			path16.path[0] != L'%' &&
			!_waccess(path16.path, F_OK))
		{
171
			win32_path_to_8(&buf, path16.path);
172 173 174

			if (buf.size)
				git_buf_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr);
175 176 177
		}
	}

178 179 180
	git_buf_free(&buf);

	return (git_buf_oom(out) ? -1 : 0);
181
}
182

183
int git_win32__find_system_dirs(git_buf *out, const wchar_t *subdir)
184 185 186 187
{
	git_buf buf = GIT_BUF_INIT;

	/* directories where git.exe & git.cmd are found */
188
	if (!win32_find_git_in_path(&buf, L"git.exe", subdir) && buf.size)
189 190 191
		git_buf_set(out, buf.ptr, buf.size);
	else
		git_buf_clear(out);
192

193
	if (!win32_find_git_in_path(&buf, L"git.cmd", subdir) && buf.size)
194
		git_buf_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr);
195 196 197

	/* directories where git is installed according to registry */
	if (!win32_find_git_in_registry(
198
			&buf, HKEY_CURRENT_USER, REG_MSYSGIT_INSTALL_LOCAL, subdir) && buf.size)
199
		git_buf_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr);
200 201

	if (!win32_find_git_in_registry(
202
			&buf, HKEY_LOCAL_MACHINE, REG_MSYSGIT_INSTALL, subdir) && buf.size)
203 204 205
		git_buf_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr);

	git_buf_free(&buf);
206

207
	return (git_buf_oom(out) ? -1 : 0);
208 209
}

Russell Belfer committed
210
int git_win32__find_global_dirs(git_buf *out)
211 212 213 214 215 216 217 218
{
	static const wchar_t *global_tmpls[4] = {
		L"%HOME%\\",
		L"%HOMEDRIVE%%HOMEPATH%\\",
		L"%USERPROFILE%\\",
		NULL,
	};

nulltoken committed
219
	return win32_find_existing_dirs(out, global_tmpls);
220 221
}

Russell Belfer committed
222
int git_win32__find_xdg_dirs(git_buf *out)
223 224 225 226 227 228 229 230 231 232 233
{
	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
234
	return win32_find_existing_dirs(out, global_tmpls);
235
}