summaryrefslogtreecommitdiff
path: root/src/fileops.c
diff options
context:
space:
mode:
authorRussell Belfer <rb@github.com>2012-10-19 15:40:43 -0700
committerRussell Belfer <rb@github.com>2012-10-19 15:40:43 -0700
commit0d422ec9c282added2aaa2a6bc0ea660e33f8f3a (patch)
tree3cb2471d1dd6b6e395ca8ed96f146ea661ea6178 /src/fileops.c
parent350b83b67d6dfc7f5cff5065aa0cf46f2bc00468 (diff)
downloadlibgit2-0d422ec9c282added2aaa2a6bc0ea660e33f8f3a.tar.gz
Fix env variable tests with new Win32 path rules
The new Win32 global path search was not working with the environment variable tests. But when I fixed the test, the new codes use of getenv() was causing more failures (presumably because of caching on Windows ???). This fixes the global file lookup to always go directly to the Win32 API in a predictable way.
Diffstat (limited to 'src/fileops.c')
-rw-r--r--src/fileops.c55
1 files changed, 25 insertions, 30 deletions
diff --git a/src/fileops.c b/src/fileops.c
index 3f9e987f0..6342b1679 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -404,44 +404,39 @@ int git_futils_find_system_file(git_buf *path, const char *filename)
int git_futils_find_global_file(git_buf *path, const char *filename)
{
- const char *home = getenv("HOME");
-
#ifdef GIT_WIN32
struct win32_path root;
-
- if (home != NULL) {
- if (git_buf_joinpath(path, home, filename) < 0)
- return -1;
-
- if (git_path_exists(path->ptr)) {
+ static const wchar_t *tmpls[4] = {
+ L"%HOME%\\",
+ L"%HOMEDRIVE%%HOMEPATH%\\",
+ L"%USERPROFILE%\\",
+ NULL,
+ };
+ const wchar_t **tmpl;
+
+ for (tmpl = tmpls; *tmpl != NULL; tmpl++) {
+ /* try to expand environment variable, skipping if not set */
+ if (win32_expand_path(&root, *tmpl) != 0 || root.path[0] == L'%')
+ continue;
+
+ /* try to look up file under path */
+ if (!win32_find_file(path, &root, filename))
return 0;
- }
- }
- if (getenv("HOMEPATH") != NULL) {
- if (win32_expand_path(&root, L"%HOMEDRIVE%%HOMEPATH%\\") < 0 ||
- root.path[0] == L'%') /* i.e. no expansion happened */
- {
- giterr_set(GITERR_OS, "Cannot locate the user's profile directory");
- return GIT_ENOTFOUND;
- }
- } else {
- if (win32_expand_path(&root, L"%USERPROFILE%\\") < 0 ||
- root.path[0] == L'%') /* i.e. no expansion happened */
- {
- giterr_set(GITERR_OS, "Cannot locate the user's profile directory");
- return GIT_ENOTFOUND;
- }
+ /* No error if file not found under %HOME%, b/c we don't trust it,
+ * but do error if another var is set and yet file is not found.
+ */
+ if (tmpl != tmpls)
+ break;
}
- if (win32_find_file(path, &root, filename) < 0) {
- giterr_set(GITERR_OS, "The global file '%s' doesn't exist", filename);
- git_buf_clear(path);
- return GIT_ENOTFOUND;
- }
+ giterr_set(GITERR_OS, "The global file '%s' doesn't exist", filename);
+ git_buf_clear(path);
- return 0;
+ return GIT_ENOTFOUND;
#else
+ const char *home = getenv("HOME");
+
if (home == NULL) {
giterr_set(GITERR_OS, "Global file lookup failed. "
"Cannot locate the user's home directory");