path: introduce git_fs_path_find_executable Provide a helper function to find an executable in the current process's PATH.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
diff --git a/src/fs_path.c b/src/fs_path.c
index c9f03a7..f9da304 100644
--- a/src/fs_path.c
+++ b/src/fs_path.c
@@ -1851,3 +1851,59 @@ cleanup:
return ret;
#endif
}
+
+int git_fs_path_find_executable(git_str *fullpath, const char *executable)
+{
+#ifdef GIT_WIN32
+ git_win32_path fullpath_w, executable_w;
+ int error;
+
+ if (git__utf8_to_16(executable_w, GIT_WIN_PATH_MAX, executable) < 0)
+ return -1;
+
+ error = git_win32_path_find_executable(fullpath_w, executable_w);
+
+ if (error == 0)
+ error = git_str_put_w(fullpath, fullpath_w, wcslen(fullpath_w));
+
+ return error;
+#else
+ git_str path = GIT_STR_INIT;
+ const char *current_dir, *term;
+ bool found = false;
+
+ if (git__getenv(&path, "PATH") < 0)
+ return -1;
+
+ current_dir = path.ptr;
+
+ while (*current_dir) {
+ if (! (term = strchr(current_dir, GIT_PATH_LIST_SEPARATOR)))
+ term = strchr(current_dir, '\0');
+
+ git_str_clear(fullpath);
+ if (git_str_put(fullpath, current_dir, (term - current_dir)) < 0 ||
+ git_str_putc(fullpath, '/') < 0 ||
+ git_str_puts(fullpath, executable) < 0)
+ return -1;
+
+ if (git_fs_path_isfile(fullpath->ptr)) {
+ found = true;
+ break;
+ }
+
+ current_dir = term;
+
+ while (*current_dir == GIT_PATH_LIST_SEPARATOR)
+ current_dir++;
+ }
+
+ git_str_dispose(&path);
+
+ if (found)
+ return 0;
+
+ git_str_clear(fullpath);
+ return GIT_ENOTFOUND;
+#endif
+}
diff --git a/src/fs_path.h b/src/fs_path.h
index 9720d34..222c44a 100644
--- a/src/fs_path.h
+++ b/src/fs_path.h
@@ -743,4 +743,10 @@ bool git_fs_path_supports_symlinks(const char *dir);
*/
int git_fs_path_validate_system_file_ownership(const char *path);
+/**
+ * Search the current PATH for the given executable, returning the full
+ * path if it is found.
+ */
+int git_fs_path_find_executable(git_str *fullpath, const char *executable);
+
#endif
diff --git a/src/win32/path_w32.c b/src/win32/path_w32.c
index 0e6aff7..d9fc829 100644
--- a/src/win32/path_w32.c
+++ b/src/win32/path_w32.c
@@ -151,6 +151,137 @@ int git_win32_path_canonicalize(git_win32_path path)
return (int)(to - path);
}
+static int git_win32_path_join(
+ git_win32_path dest,
+ const wchar_t *one,
+ size_t one_len,
+ const wchar_t *two,
+ size_t two_len)
+{
+ size_t backslash = 0;
+
+ if (one_len && two_len && one[one_len - 1] != L'\\')
+ backslash = 1;
+
+ if (one_len + two_len + backslash > MAX_PATH) {
+ git_error_set(GIT_ERROR_INVALID, "path too long");
+ return -1;
+ }
+
+ memmove(dest, one, one_len * sizeof(wchar_t));
+
+ if (backslash)
+ dest[one_len] = L'\\';
+
+ memcpy(dest + one_len + backslash, two, two_len * sizeof(wchar_t));
+ dest[one_len + backslash + two_len] = L'\0';
+
+ return 0;
+}
+
+struct win32_path_iter {
+ wchar_t *env;
+ const wchar_t *current_dir;
+};
+
+static int win32_path_iter_init(struct win32_path_iter *iter)
+{
+ DWORD len = GetEnvironmentVariableW(L"PATH", NULL, 0);
+
+ if (!len && GetLastError() == ERROR_ENVVAR_NOT_FOUND) {
+ iter->env = NULL;
+ iter->current_dir = NULL;
+ return 0;
+ } else if (!len) {
+ git_error_set(GIT_ERROR_OS, "could not load PATH");
+ return -1;
+ }
+
+ iter->env = git__malloc(len * sizeof(wchar_t));
+ GIT_ERROR_CHECK_ALLOC(iter->env);
+
+ len = GetEnvironmentVariableW(L"PATH", iter->env, len);
+
+ if (len == 0) {
+ git_error_set(GIT_ERROR_OS, "could not load PATH");
+ return -1;
+ }
+
+ iter->current_dir = iter->env;
+ return 0;
+}
+
+static int win32_path_iter_next(
+ const wchar_t **out,
+ size_t *out_len,
+ struct win32_path_iter *iter)
+{
+ const wchar_t *start;
+ wchar_t term;
+ size_t len = 0;
+
+ if (!iter->current_dir || !*iter->current_dir)
+ return GIT_ITEROVER;
+
+ term = (*iter->current_dir == L'"') ? *iter->current_dir++ : L';';
+ start = iter->current_dir;
+
+ while (*iter->current_dir && *iter->current_dir != term) {
+ iter->current_dir++;
+ len++;
+ }
+
+ *out = start;
+ *out_len = len;
+
+ if (term == L'"' && *iter->current_dir)
+ iter->current_dir++;
+
+ while (*iter->current_dir == L';')
+ iter->current_dir++;
+
+ return 0;
+}
+
+static void win32_path_iter_dispose(struct win32_path_iter *iter)
+{
+ if (!iter)
+ return;
+
+ git__free(iter->env);
+ iter->env = NULL;
+ iter->current_dir = NULL;
+}
+
+int git_win32_path_find_executable(git_win32_path fullpath, wchar_t *exe)
+{
+ struct win32_path_iter path_iter;
+ const wchar_t *dir;
+ size_t dir_len, exe_len = wcslen(exe);
+ bool found = false;
+
+ if (win32_path_iter_init(&path_iter) < 0)
+ return -1;
+
+ while (win32_path_iter_next(&dir, &dir_len, &path_iter) != GIT_ITEROVER) {
+ if (git_win32_path_join(fullpath, dir, dir_len, exe, exe_len) < 0)
+ continue;
+
+ if (_waccess(fullpath, 0) == 0) {
+ found = true;
+ break;
+ }
+ }
+
+ win32_path_iter_dispose(&path_iter);
+
+ if (found)
+ return 0;
+
+ fullpath[0] = L'\0';
+ return GIT_ENOTFOUND;
+}
+
static int win32_path_cwd(wchar_t *out, size_t len)
{
int cwd_len;
diff --git a/src/win32/path_w32.h b/src/win32/path_w32.h
index 4fadf8d..837b11e 100644
--- a/src/win32/path_w32.h
+++ b/src/win32/path_w32.h
@@ -86,4 +86,6 @@ size_t git_win32_path_trim_end(wchar_t *str, size_t len);
*/
size_t git_win32_path_remove_namespace(wchar_t *str, size_t len);
+int git_win32_path_find_executable(git_win32_path fullpath, wchar_t* exe);
+
#endif
diff --git a/tests/core/path.c b/tests/core/path.c
index 563dcd2..a0ae77f 100644
--- a/tests/core/path.c
+++ b/tests/core/path.c
@@ -2,6 +2,20 @@
#include "futils.h"
#include "fs_path.h"
+static char *path_save;
+
+void test_core_path__initialize(void)
+{
+ path_save = cl_getenv("PATH");
+}
+
+void test_core_path__cleanup(void)
+{
+ cl_setenv("PATH", path_save);
+ git__free(path_save);
+ path_save = NULL;
+}
+
static void
check_dirname(const char *A, const char *B)
{
@@ -60,6 +74,20 @@ check_joinpath_n(
git_str_dispose(&joined_path);
}
+static void check_setenv(const char* name, const char* value)
+{
+ char* check;
+
+ cl_git_pass(cl_setenv(name, value));
+ check = cl_getenv(name);
+
+ if (value)
+ cl_assert_equal_s(value, check);
+ else
+ cl_assert(check == NULL);
+
+ git__free(check);
+}
/* get the dirname of a path */
void test_core_path__00_dirname(void)
@@ -651,3 +679,61 @@ void test_core_path__16_resolve_relative(void)
assert_common_dirlen(6, "a/b/c/foo.txt", "a/b/c/d/e/bar.txt");
assert_common_dirlen(7, "/a/b/c/foo.txt", "/a/b/c/d/e/bar.txt");
}
+
+static void fix_path(git_str *s)
+{
+#ifndef GIT_WIN32
+ GIT_UNUSED(s);
+#else
+ char* c;
+
+ for (c = s->ptr; *c; c++) {
+ if (*c == '/')
+ *c = '\\';
+ }
+#endif
+}
+
+void test_core_path__find_exe_in_path(void)
+{
+ char *orig_path;
+ git_str sandbox_path = GIT_STR_INIT;
+ git_str new_path = GIT_STR_INIT, full_path = GIT_STR_INIT,
+ dummy_path = GIT_STR_INIT;
+
+#ifdef GIT_WIN32
+ static const char *bogus_path_1 = "c:\\does\\not\\exist\\";
+ static const char *bogus_path_2 = "e:\\non\\existent";
+#else
+ static const char *bogus_path_1 = "/this/path/does/not/exist/";
+ static const char *bogus_path_2 = "/non/existent";
+#endif
+
+ orig_path = cl_getenv("PATH");
+
+ git_str_puts(&sandbox_path, clar_sandbox_path());
+ git_str_joinpath(&dummy_path, sandbox_path.ptr, "dummmmmmmy_libgit2_file");
+ cl_git_rewritefile(dummy_path.ptr, "this is a dummy file");
+
+ fix_path(&sandbox_path);
+ fix_path(&dummy_path);
+
+ cl_git_pass(git_str_printf(&new_path, "%s%c%s%c%s%c%s",
+ bogus_path_1, GIT_PATH_LIST_SEPARATOR,
+ orig_path, GIT_PATH_LIST_SEPARATOR,
+ sandbox_path.ptr, GIT_PATH_LIST_SEPARATOR,
+ bogus_path_2));
+
+ check_setenv("PATH", new_path.ptr);
+
+ cl_git_fail_with(GIT_ENOTFOUND, git_fs_path_find_executable(&full_path, "this_file_does_not_exist"));
+ cl_git_pass(git_fs_path_find_executable(&full_path, "dummmmmmmy_libgit2_file"));
+
+ cl_assert_equal_s(full_path.ptr, dummy_path.ptr);
+
+ git_str_dispose(&full_path);
+ git_str_dispose(&new_path);
+ git_str_dispose(&dummy_path);
+ git_str_dispose(&sandbox_path);
+ git__free(orig_path);
+}