findfile.c 5.58 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3 4 5 6 7
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

8 9
#include "findfile.h"

10
#include "path_w32.h"
11
#include "utf-conv.h"
12
#include "fs_path.h"
13

14
#define REG_MSYSGIT_INSTALL_LOCAL L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Git_is1"
15

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

22 23 24 25
typedef struct {
	git_win32_path path;
	DWORD len;
} _findfile_path;
26

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

31 32
	if (!dest->len || dest->len > ARRAY_SIZE(dest->path))
		return -1;
33

34
	return 0;
35 36
}

37
static int win32_path_to_8(git_str *dest, const wchar_t *src)
38
{
39
	git_win32_utf8_path utf8_path;
40

41
	if (git_win32_path_to_utf8(utf8_path, src) < 0) {
42
		git_error_set(GIT_ERROR_OS, "unable to convert path to UTF-8");
43
		return -1;
44 45
	}

46
	/* Convert backslashes to forward slashes */
47
	git_fs_path_mkposix(utf8_path);
48

49
	return git_str_sets(dest, utf8_path);
50 51
}

52
static wchar_t *win32_walkpath(wchar_t *path, wchar_t *buf, size_t buflen)
53 54 55
{
	wchar_t term, *base = path;

Edward Thomson committed
56 57 58
	GIT_ASSERT_ARG_WITH_RETVAL(path, NULL);
	GIT_ASSERT_ARG_WITH_RETVAL(buf, NULL);
	GIT_ASSERT_ARG_WITH_RETVAL(buflen, NULL);
59 60 61 62 63 64 65 66 67 68 69 70 71 72

	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;
}

73
static int win32_find_git_in_path(git_str *buf, const wchar_t *gitexe, const wchar_t *subdir)
74
{
75
	wchar_t *env = _wgetenv(L"PATH"), lastch;
76
	_findfile_path root;
77
	size_t gitexe_len = wcslen(gitexe);
78 79 80 81

	if (!env)
		return -1;

82 83 84 85 86 87 88 89 90
	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';
		}
91

92 93 94
		if (root.len + gitexe_len >= MAX_PATH)
			continue;
		wcscpy(&root.path[root.len], gitexe);
95

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

100
			win32_path_to_8(buf, root.path);
101
			return 0;
102 103
		}
	}
104

105 106 107
	return GIT_ENOTFOUND;
}

108
static int win32_find_git_in_registry(
109
	git_str *buf, const HKEY hive, const wchar_t *key, const wchar_t *subdir)
110
{
111
	HKEY hKey;
112
	int error = GIT_ENOTFOUND;
113

Edward Thomson committed
114
	GIT_ASSERT_ARG(buf);
115

116 117 118
	if (!RegOpenKeyExW(hive, key, 0, KEY_READ, &hKey)) {
		DWORD dwType, cbData;
		git_win32_path path;
119

120 121 122
		/* 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));
123

124 125
		/* InstallLocation points to the root of the git directory */
		if (!RegQueryValueExW(hKey, L"InstallLocation", NULL, &dwType, (LPBYTE)path, &cbData) &&
126
			dwType == REG_SZ) {
127

128 129
			/* Append the suffix */
			wcscat(path, subdir);
130

131 132 133 134
			/* Convert to UTF-8, with forward slashes, and output the path
			 * to the provided buffer */
			if (!win32_path_to_8(buf, path))
				error = 0;
135
		}
136

137
		RegCloseKey(hKey);
138 139
	}

140
	return error;
141 142
}

143
static int win32_find_existing_dirs(
144
	git_str *out, const wchar_t *tmpl[])
145
{
146
	_findfile_path path16;
147
	git_str buf = GIT_STR_INIT;
148

149
	git_str_clear(out);
150 151

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

			if (buf.size)
159
				git_str_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr);
160 161 162
		}
	}

163
	git_str_dispose(&buf);
164

165
	return (git_str_oom(out) ? -1 : 0);
166
}
167

168
int git_win32__find_system_dirs(git_str *out, const wchar_t *subdir)
169
{
170
	git_str buf = GIT_STR_INIT;
171 172

	/* directories where git.exe & git.cmd are found */
173
	if (!win32_find_git_in_path(&buf, L"git.exe", subdir) && buf.size)
174
		git_str_set(out, buf.ptr, buf.size);
175
	else
176
		git_str_clear(out);
177

178
	if (!win32_find_git_in_path(&buf, L"git.cmd", subdir) && buf.size)
179
		git_str_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr);
180 181 182

	/* directories where git is installed according to registry */
	if (!win32_find_git_in_registry(
183
			&buf, HKEY_CURRENT_USER, REG_MSYSGIT_INSTALL_LOCAL, subdir) && buf.size)
184
		git_str_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr);
185 186

	if (!win32_find_git_in_registry(
187
			&buf, HKEY_LOCAL_MACHINE, REG_MSYSGIT_INSTALL, subdir) && buf.size)
188
		git_str_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr);
189

190
	git_str_dispose(&buf);
191

192
	return (git_str_oom(out) ? -1 : 0);
193 194
}

195
int git_win32__find_global_dirs(git_str *out)
196 197 198 199 200 201 202 203
{
	static const wchar_t *global_tmpls[4] = {
		L"%HOME%\\",
		L"%HOMEDRIVE%%HOMEPATH%\\",
		L"%USERPROFILE%\\",
		NULL,
	};

nulltoken committed
204
	return win32_find_existing_dirs(out, global_tmpls);
205 206
}

207
int git_win32__find_xdg_dirs(git_str *out)
208 209 210 211 212 213 214 215 216 217 218
{
	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
219
	return win32_find_existing_dirs(out, global_tmpls);
220
}
221

222
int git_win32__find_programdata_dirs(git_str *out)
223 224 225 226 227 228 229 230
{
	static const wchar_t *programdata_tmpls[2] = {
		L"%PROGRAMDATA%\\Git",
		NULL,
	};

	return win32_find_existing_dirs(out, programdata_tmpls);
}