diff options
| author | Vicent Martà <vicent@github.com> | 2012-10-16 08:36:55 -0700 |
|---|---|---|
| committer | Vicent Martà <vicent@github.com> | 2012-10-16 08:36:55 -0700 |
| commit | 52748f7b9f039b94e212c41d8a4f46b50b917962 (patch) | |
| tree | af95f6f22dc75f72f5520339c38c8a9c5a9ef3e5 /src/fileops.c | |
| parent | 9e37305aad734efb83f22b2e292a7591bbf49f15 (diff) | |
| parent | 997579bed1806f217c910da05092ffdf9f4523b4 (diff) | |
| download | libgit2-52748f7b9f039b94e212c41d8a4f46b50b917962.tar.gz | |
Merge pull request #952 from csware/config-locations
Config location fixes
Diffstat (limited to 'src/fileops.c')
| -rw-r--r-- | src/fileops.c | 108 |
1 files changed, 35 insertions, 73 deletions
diff --git a/src/fileops.c b/src/fileops.c index 8ccf063d5..763537a46 100644 --- a/src/fileops.c +++ b/src/fileops.c @@ -7,6 +7,9 @@ #include "common.h" #include "fileops.h" #include <ctype.h> +#if GIT_WIN32 +#include "win32/findfile.h" +#endif int git_futils_mkpath2file(const char *file_path, const mode_t mode) { @@ -371,98 +374,59 @@ int git_futils_rmdir_r(const char *path, git_directory_removal_type removal_type return error; } -#ifdef GIT_WIN32 -struct win32_path { - wchar_t path[MAX_PATH]; - DWORD len; -}; - -static int win32_expand_path(struct win32_path *s_root, const wchar_t *templ) -{ - s_root->len = ExpandEnvironmentStringsW(templ, s_root->path, MAX_PATH); - return s_root->len ? 0 : -1; -} - -static int win32_find_file(git_buf *path, const struct win32_path *root, const char *filename) -{ - size_t len, alloc_len; - wchar_t *file_utf16 = NULL; - char file_utf8[GIT_PATH_MAX]; - - if (!root || !filename || (len = strlen(filename)) == 0) - return GIT_ENOTFOUND; - - /* allocate space for wchar_t path to file */ - alloc_len = root->len + len + 2; - file_utf16 = git__calloc(alloc_len, sizeof(wchar_t)); - GITERR_CHECK_ALLOC(file_utf16); - - /* append root + '\\' + filename as wchar_t */ - memcpy(file_utf16, root->path, root->len * sizeof(wchar_t)); - - if (*filename == '/' || *filename == '\\') - filename++; - - git__utf8_to_16(file_utf16 + root->len - 1, alloc_len, filename); - - /* check access */ - if (_waccess(file_utf16, F_OK) < 0) { - git__free(file_utf16); - return GIT_ENOTFOUND; - } - - git__utf16_to_8(file_utf8, file_utf16); - git_path_mkposix(file_utf8); - git_buf_sets(path, file_utf8); - - git__free(file_utf16); - return 0; -} -#endif - 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 || - root.path[0] == L'%') /* i.e. no expansion happened */ - { - giterr_set(GITERR_OS, "Cannot locate the system's Program Files directory"); - return -1; - } - - if (win32_find_file(path, &root, filename) < 0) { - giterr_set(GITERR_OS, "The system file '%s' doesn't exist", filename); - git_buf_clear(path); - return GIT_ENOTFOUND; - } - - return 0; + // try to find git.exe/git.cmd on path + if (!win32_find_system_file_using_path(path, filename)) + return 0; + // try to find msysgit installation path using registry + if (!win32_find_system_file_using_registry(path, filename)) + return 0; #else if (git_buf_joinpath(path, "/etc", filename) < 0) return -1; if (git_path_exists(path->ptr) == true) return 0; +#endif git_buf_clear(path); giterr_set(GITERR_OS, "The system file '%s' doesn't exist", filename); return GIT_ENOTFOUND; -#endif } 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 (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 -1; + if (home != NULL) { + if (git_buf_joinpath(path, home, filename) < 0) + return -1; + + if (git_path_exists(path->ptr)) { + 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 -1; + } + } 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 -1; + } } if (win32_find_file(path, &root, filename) < 0) { @@ -473,8 +437,6 @@ int git_futils_find_global_file(git_buf *path, const char *filename) return 0; #else - const char *home = getenv("HOME"); - if (home == NULL) { giterr_set(GITERR_OS, "Global file lookup failed. " "Cannot locate the user's home directory"); |
