Get user's home dir in UTF-16 clean manner On Windows, we are having problems with home directories that have non-ascii characters in them. This rewrites the relevant code to fetch environment variables as UTF-16 and then explicitly map then into UTF-8 for our internal usage.
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
diff --git a/src/fileops.c b/src/fileops.c
index ee9d421..76adccf 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -345,13 +345,48 @@ int git_futils_rmdir_r(const char *path, git_directory_removal_type removal_type
return error;
}
+#ifdef GIT_WIN32
+static char *win32_getenv(const wchar_t *name)
+{
+ char *val_utf8;
+ wchar_t *val_utf16;
+ DWORD len = GetEnvironmentVariableW(name, NULL, 0);
+
+ if (len <= 0)
+ return NULL;
+
+ val_utf16 = git__calloc(len, sizeof(wchar_t));
+ if (!val_utf16)
+ return NULL;
+
+ if (GetEnvironmentVariableW(name, val_utf16, len) != len - 1) {
+ giterr_set(GITERR_OS, "Could not read environment variable");
+ git__free(val_utf16);
+ return NULL;
+ }
+
+ val_utf8 = gitwin_from_utf16(val_utf16);
+
+ git__free(val_utf16);
+
+ return val_utf8;
+}
+#endif
+
int git_futils_find_global_file(git_buf *path, const char *filename)
{
- const char *home = getenv("HOME");
+ char *home;
#ifdef GIT_WIN32
- if (home == NULL)
- home = getenv("USERPROFILE");
+ home = win32_getenv(L"HOME");
+
+ if (!home)
+ home = win32_getenv(L"USERPROFILE");
+
+ if (home)
+ git_path_mkposix(home);
+#else
+ home = getenv("HOME");
#endif
if (home == NULL) {
@@ -363,6 +398,10 @@ int git_futils_find_global_file(git_buf *path, const char *filename)
if (git_buf_joinpath(path, home, filename) < 0)
return -1;
+#ifdef GIT_WIN32
+ git__free(home);
+#endif
+
if (git_path_exists(path->ptr) == false) {
git_buf_clear(path);
return GIT_ENOTFOUND;
diff --git a/tests-clar/core/env.c b/tests-clar/core/env.c
new file mode 100644
index 0000000..bd1a942
--- /dev/null
+++ b/tests-clar/core/env.c
@@ -0,0 +1,132 @@
+#include "clar_libgit2.h"
+#include "fileops.h"
+#include "path.h"
+
+#ifdef GIT_WIN32
+
+#include "win32/utf-conv.h"
+
+static char *cl_getenv(const char *name)
+{
+ wchar_t *name_utf16 = gitwin_to_utf16(name);
+ DWORD value_len, alloc_len;
+ wchar_t *value_utf16;
+ char *value_utf8;
+
+ cl_assert(name_utf16);
+ alloc_len = GetEnvironmentVariableW(name_utf16, NULL, 0);
+ if (alloc_len < 0)
+ return NULL;
+ cl_assert(value_utf16 = git__calloc(alloc_len, sizeof(wchar_t)));
+ value_len = GetEnvironmentVariableW(name_utf16, value_utf16, alloc_len);
+ cl_assert_equal_i(value_len, alloc_len - 1);
+ cl_assert(value_utf8 = gitwin_from_utf16(value_utf16));
+ git__free(value_utf16);
+
+ return value_utf8;
+}
+
+static int cl_setenv(const char *name, const char *value)
+{
+ wchar_t *name_utf16 = gitwin_to_utf16(name);
+ wchar_t *value_utf16 = value ? gitwin_to_utf16(value) : NULL;
+
+ cl_assert(name_utf16);
+ cl_assert(SetEnvironmentVariableW(name_utf16, value_utf16));
+
+ git__free(name_utf16);
+ git__free(value_utf16);
+
+ return 0;
+
+}
+#else
+
+#include <stdlib.h>
+#define cl_getenv(n) getenv(n)
+#define cl_setenv(n,v) (v) ? setenv((n),(v),1) : unsetenv(n)
+
+#endif
+
+static char *env_home = NULL;
+#ifdef GIT_WIN32
+static char *env_userprofile = NULL;
+#endif
+
+void test_core_env__initialize(void)
+{
+ env_home = cl_getenv("HOME");
+#ifdef GIT_WIN32
+ env_userprofile = cl_getenv("USERPROFILE");
+#endif
+}
+
+void test_core_env__cleanup(void)
+{
+ cl_setenv("HOME", env_home);
+#ifdef GIT_WIN32
+ cl_setenv("USERPROFILE", env_userprofile);
+
+ git__free(env_home);
+ git__free(env_userprofile);
+#endif
+}
+
+void test_core_env__0(void)
+{
+ static char *home_values[] = {
+ "fake_home",
+ "fáke_hõme", /* all in latin-1 supplement */
+ "fĀke_Ĥome", /* latin extended */
+ "fακε_hοmέ", /* having fun with greek */
+ "faงe_นome", /* now I have no idea, but thai characters */
+ "f\xe1\x9cx80ke_\xe1\x9c\x91ome", /* tagalog characters */
+ "\xe1\xb8\x9fẢke_hoṁe", /* latin extended additional */
+ "\xf0\x9f\x98\x98\xf0\x9f\x98\x82", /* emoticons */
+ NULL
+ };
+ git_buf path = GIT_BUF_INIT, found = GIT_BUF_INIT;
+ char **val;
+ char *check;
+
+ for (val = home_values; *val != NULL; val++) {
+
+ if (p_mkdir(*val, 0777) == 0) {
+ /* if we can't make the directory, let's just assume
+ * we are on a filesystem that doesn't support the
+ * characters in question and skip this test...
+ */
+ cl_git_pass(git_path_prettify(&path, *val, NULL));
+
+ cl_git_pass(cl_setenv("HOME", path.ptr));
+
+ /* do a quick check that it was set correctly */
+ check = cl_getenv("HOME");
+ cl_assert_equal_s(path.ptr, check);
+#ifdef GIT_WIN32
+ git__free(check);
+
+ cl_git_pass(cl_setenv("USERPROFILE", path.ptr));
+
+ /* do a quick check that it was set correctly */
+ check = cl_getenv("USERPROFILE");
+ cl_assert_equal_s(path.ptr, check);
+ git__free(check);
+#endif
+
+ cl_git_pass(git_buf_puts(&path, "/testfile"));
+ cl_git_mkfile(path.ptr, "find me");
+
+ cl_git_pass(git_futils_find_global_file(&found, "testfile"));
+
+#ifdef GIT_WIN32
+ /* do another check with HOME unset */
+ cl_git_pass(cl_setenv("HOME", NULL));
+ cl_git_pass(git_futils_find_global_file(&found, "testfile"));
+#endif
+ }
+ }
+
+ git_buf_free(&path);
+ git_buf_free(&found);
+}