Commit a4452eb1 by Vicent Martí

Merge pull request #727 from libgit2/env-expansion

windows: Properly expand all environment variables
parents 5f60fd00 9cde607c
...@@ -354,110 +354,22 @@ int git_futils_rmdir_r(const char *path, git_directory_removal_type removal_type ...@@ -354,110 +354,22 @@ int git_futils_rmdir_r(const char *path, git_directory_removal_type removal_type
} }
#ifdef GIT_WIN32 #ifdef GIT_WIN32
static char *win32_getenv(const wchar_t *name) struct win32_path {
{ wchar_t path[MAX_PATH];
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)
{
char *home;
#ifdef GIT_WIN32
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) {
giterr_set(GITERR_OS, "Global file lookup failed. "
"Cannot locate the user's home directory");
return -1;
}
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;
}
return 0;
}
#ifdef GIT_WIN32
typedef struct {
wchar_t *path;
DWORD len; DWORD len;
} win32_path; };
static const win32_path *win32_system_root(void) static int win32_expand_path(struct win32_path *s_root, const wchar_t *templ)
{ {
static win32_path s_root = { 0, 0 }; s_root->len = ExpandEnvironmentStringsW(templ, s_root->path, MAX_PATH);
return s_root->len ? 0 : -1;
if (s_root.path == NULL) {
const wchar_t *root_tmpl = L"%PROGRAMFILES%\\Git\\etc\\";
s_root.len = ExpandEnvironmentStringsW(root_tmpl, NULL, 0);
if (s_root.len <= 0) {
giterr_set(GITERR_OS, "Failed to expand environment strings");
return NULL;
}
s_root.path = git__calloc(s_root.len, sizeof(wchar_t));
if (s_root.path == NULL)
return NULL;
if (ExpandEnvironmentStringsW(root_tmpl, s_root.path, s_root.len) != s_root.len) {
giterr_set(GITERR_OS, "Failed to expand environment strings");
git__free(s_root.path);
s_root.path = NULL;
return NULL;
}
}
return &s_root;
} }
static int win32_find_system_file(git_buf *path, const char *filename) static int win32_find_file(git_buf *path, const struct win32_path *root, const char *filename)
{ {
int error = 0; int error = 0;
const win32_path *root = win32_system_root();
size_t len; size_t len;
wchar_t *file_utf16 = NULL, *scan; wchar_t *file_utf16 = NULL;
char *file_utf8 = NULL; char *file_utf8 = NULL;
if (!root || !filename || (len = strlen(filename)) == 0) if (!root || !filename || (len = strlen(filename)) == 0)
...@@ -479,10 +391,6 @@ static int win32_find_system_file(git_buf *path, const char *filename) ...@@ -479,10 +391,6 @@ static int win32_find_system_file(git_buf *path, const char *filename)
goto cleanup; goto cleanup;
} }
for (scan = file_utf16; *scan; scan++)
if (*scan == L'/')
*scan = L'\\';
/* check access */ /* check access */
if (_waccess(file_utf16, F_OK) < 0) { if (_waccess(file_utf16, F_OK) < 0) {
error = GIT_ENOTFOUND; error = GIT_ENOTFOUND;
...@@ -499,13 +407,24 @@ static int win32_find_system_file(git_buf *path, const char *filename) ...@@ -499,13 +407,24 @@ static int win32_find_system_file(git_buf *path, const char *filename)
cleanup: cleanup:
git__free(file_utf16); git__free(file_utf16);
return error; return error;
} }
#endif #endif
int git_futils_find_system_file(git_buf *path, const char *filename) int git_futils_find_system_file(git_buf *path, const char *filename)
{ {
#ifdef GIT_WIN32
struct win32_path root;
if (win32_expand_path(&root, L"%PROGRAMFILES%\\Git\\etc\\") < 0 ||
win32_find_file(path, &root, filename) < 0) {
giterr_set(GITERR_OS, "Cannot find the system's Program Files directory");
return -1;
}
return 0;
#else
if (git_buf_joinpath(path, "/etc", filename) < 0) if (git_buf_joinpath(path, "/etc", filename) < 0)
return -1; return -1;
...@@ -513,10 +432,39 @@ int git_futils_find_system_file(git_buf *path, const char *filename) ...@@ -513,10 +432,39 @@ int git_futils_find_system_file(git_buf *path, const char *filename)
return 0; return 0;
git_buf_clear(path); git_buf_clear(path);
return GIT_ENOTFOUND;
#endif
}
int git_futils_find_global_file(git_buf *path, const char *filename)
{
#ifdef GIT_WIN32 #ifdef GIT_WIN32
return win32_find_system_file(path, filename); struct win32_path root;
if (win32_expand_path(&root, L"%USERPROFILE%\\") < 0 ||
win32_find_file(path, &root, filename) < 0) {
giterr_set(GITERR_OS, "Failed to lookup the current user's Windows profile");
return -1;
}
return 0;
#else #else
const char *home = getenv("HOME");
if (home == NULL) {
giterr_set(GITERR_OS, "Global file lookup failed. "
"Cannot locate the user's home directory");
return -1;
}
if (git_buf_joinpath(path, home, filename) < 0)
return -1;
if (git_path_exists(path->ptr) == false) {
git_buf_clear(path);
return GIT_ENOTFOUND; return GIT_ENOTFOUND;
}
return 0;
#endif #endif
} }
...@@ -53,26 +53,24 @@ static int cl_setenv(const char *name, const char *value) ...@@ -53,26 +53,24 @@ static int cl_setenv(const char *name, const char *value)
#endif #endif
static char *env_home = NULL; static char *env_home = NULL;
#ifdef GIT_WIN32
static char *env_userprofile = NULL; static char *env_userprofile = NULL;
#endif
void test_core_env__initialize(void) void test_core_env__initialize(void)
{ {
env_home = cl_getenv("HOME");
#ifdef GIT_WIN32 #ifdef GIT_WIN32
env_userprofile = cl_getenv("USERPROFILE"); env_userprofile = cl_getenv("USERPROFILE");
#else
env_home = cl_getenv("HOME");
#endif #endif
} }
void test_core_env__cleanup(void) void test_core_env__cleanup(void)
{ {
cl_setenv("HOME", env_home);
#ifdef GIT_WIN32 #ifdef GIT_WIN32
cl_setenv("USERPROFILE", env_userprofile); cl_setenv("USERPROFILE", env_userprofile);
git__free(env_home);
git__free(env_userprofile); git__free(env_userprofile);
#else
cl_setenv("HOME", env_home);
#endif #endif
} }
...@@ -102,32 +100,25 @@ void test_core_env__0(void) ...@@ -102,32 +100,25 @@ void test_core_env__0(void)
*/ */
cl_git_pass(git_path_prettify(&path, *val, NULL)); 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 #ifdef GIT_WIN32
git__free(check);
cl_git_pass(cl_setenv("USERPROFILE", path.ptr)); cl_git_pass(cl_setenv("USERPROFILE", path.ptr));
/* do a quick check that it was set correctly */ /* do a quick check that it was set correctly */
check = cl_getenv("USERPROFILE"); check = cl_getenv("USERPROFILE");
cl_assert_equal_s(path.ptr, check); cl_assert_equal_s(path.ptr, check);
git__free(check); git__free(check);
#else
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);
#endif #endif
cl_git_pass(git_buf_puts(&path, "/testfile")); cl_git_pass(git_buf_puts(&path, "/testfile"));
cl_git_mkfile(path.ptr, "find me"); cl_git_mkfile(path.ptr, "find me");
cl_git_pass(git_futils_find_global_file(&found, "testfile")); 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
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment