From ec56af08a9aa9ae261325f5037ec0d5114517d55 Mon Sep 17 00:00:00 2001 From: Sven Strickroth Date: Thu, 31 Jan 2013 17:37:20 +0100 Subject: [PATCH 1/4] Refactored: Move msysgit registry detection to it's own function Signed-off-by: Sven Strickroth --- src/win32/findfile.c | 51 +++++++++++++++++++++++++------------------- src/win32/findfile.h | 1 + 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/win32/findfile.c b/src/win32/findfile.c index 8c4fc7a4a..c93b0727e 100644 --- a/src/win32/findfile.c +++ b/src/win32/findfile.c @@ -113,28 +113,7 @@ int win32_find_system_file_using_registry(git_buf *path, const char *filename) { struct win32_path root; - HKEY hKey; - DWORD dwType = REG_SZ; - DWORD dwSize = MAX_PATH; - - root.len = 0; - if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, REG_MSYSGIT_INSTALL, 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) - { - if (RegQueryValueExW(hKey, L"InstallLocation", NULL, &dwType,(LPBYTE)&root.path, &dwSize) == ERROR_SUCCESS) - { - // InstallLocation points to the root of the msysgit directory - if (dwSize + 4 > MAX_PATH) // 4 = wcslen(L"etc\\") - { - giterr_set(GITERR_OS, "Cannot locate the system's msysgit directory - path too long"); - return -1; - } - wcscat(root.path, L"etc\\"); - root.len = (DWORD)wcslen(root.path) + 1; - } - } - RegCloseKey(hKey); - - if (!root.len) { + if (win32_find_msysgit_in_registry(&root, HKEY_LOCAL_MACHINE, REG_MSYSGIT_INSTALL)) { giterr_set(GITERR_OS, "Cannot locate the system's msysgit directory"); return -1; } @@ -147,3 +126,31 @@ int win32_find_system_file_using_registry(git_buf *path, const char *filename) return 0; } + +int win32_find_msysgit_in_registry(struct win32_path *root, const HKEY hieve, const wchar_t *key) +{ + HKEY hKey; + DWORD dwType = REG_SZ; + DWORD dwSize = MAX_PATH; + + assert(root); + + root->len = 0; + if (RegOpenKeyExW(hieve, key, 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) + { + if (RegQueryValueExW(hKey, L"InstallLocation", NULL, &dwType, (LPBYTE)&root->path, &dwSize) == ERROR_SUCCESS) + { + // InstallLocation points to the root of the msysgit directory + if (dwSize + 4 > MAX_PATH) // 4 = wcslen(L"etc\\") + { + giterr_set(GITERR_OS, "Cannot locate the system's msysgit directory - path too long"); + return -1; + } + wcscat(root->path, L"etc\\"); + root->len = (DWORD)wcslen(root->path) + 1; + } + } + RegCloseKey(hKey); + + return root->len ? 0 : GIT_ENOTFOUND; +} diff --git a/src/win32/findfile.h b/src/win32/findfile.h index 918991cf9..47fe71596 100644 --- a/src/win32/findfile.h +++ b/src/win32/findfile.h @@ -18,6 +18,7 @@ int win32_expand_path(struct win32_path *s_root, const wchar_t *templ); int win32_find_file(git_buf *path, const struct win32_path *root, const char *filename); int win32_find_system_file_using_path(git_buf *path, const char *filename); int win32_find_system_file_using_registry(git_buf *path, const char *filename); +int win32_find_msysgit_in_registry(struct win32_path *root, const HKEY hieve, const wchar_t *key); #endif From c55378fce5d0164c87ccf3a6dba2f56b4ade9341 Mon Sep 17 00:00:00 2001 From: Sven Strickroth Date: Thu, 31 Jan 2013 17:43:59 +0100 Subject: [PATCH 2/4] Detect msysgit installation of users without admin rights Signed-off-by: Sven Strickroth --- src/win32/findfile.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/win32/findfile.c b/src/win32/findfile.c index c93b0727e..10a1c4d40 100644 --- a/src/win32/findfile.c +++ b/src/win32/findfile.c @@ -9,8 +9,9 @@ #include "path.h" #include "findfile.h" +#define REG_MSYSGIT_INSTALL_LOCAL L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Git_is1" #ifndef _WIN64 -#define REG_MSYSGIT_INSTALL L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Git_is1" +#define REG_MSYSGIT_INSTALL REG_MSYSGIT_INSTALL_LOCAL #else #define REG_MSYSGIT_INSTALL L"SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Git_is1" #endif @@ -113,9 +114,11 @@ int win32_find_system_file_using_registry(git_buf *path, const char *filename) { struct win32_path root; - if (win32_find_msysgit_in_registry(&root, HKEY_LOCAL_MACHINE, REG_MSYSGIT_INSTALL)) { - giterr_set(GITERR_OS, "Cannot locate the system's msysgit directory"); - return -1; + if (win32_find_msysgit_in_registry(&root, HKEY_CURRENT_USER, REG_MSYSGIT_INSTALL_LOCAL)) { + if (win32_find_msysgit_in_registry(&root, HKEY_LOCAL_MACHINE, REG_MSYSGIT_INSTALL)) { + giterr_set(GITERR_OS, "Cannot locate the system's msysgit directory"); + return -1; + } } if (win32_find_file(path, &root, filename) < 0) { From 01e7128f392157f3580d9bbca5c2b80fb56001ee Mon Sep 17 00:00:00 2001 From: Sven Strickroth Date: Thu, 31 Jan 2013 17:44:40 +0100 Subject: [PATCH 3/4] Added Sven Strickroth to AUTHORS Signed-off-by: Sven Strickroth --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index 3803e7bec..587da249d 100644 --- a/AUTHORS +++ b/AUTHORS @@ -64,6 +64,7 @@ Sergey Nikishin Shawn O. Pearce Shuhei Tanuma Steve Frécinaux +Sven Strickroth Tim Branyen Tim Clem Tim Harder From 45792c923ba85d42618b4b246c0a8eec8b16128e Mon Sep 17 00:00:00 2001 From: Sven Strickroth Date: Fri, 1 Feb 2013 10:32:05 +0100 Subject: [PATCH 4/4] Stick to coding style: Move up braces Signed-off-by: Sven Strickroth --- src/win32/findfile.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/win32/findfile.c b/src/win32/findfile.c index 10a1c4d40..6fc7c7513 100644 --- a/src/win32/findfile.c +++ b/src/win32/findfile.c @@ -139,13 +139,10 @@ int win32_find_msysgit_in_registry(struct win32_path *root, const HKEY hieve, co assert(root); root->len = 0; - if (RegOpenKeyExW(hieve, key, 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) - { - if (RegQueryValueExW(hKey, L"InstallLocation", NULL, &dwType, (LPBYTE)&root->path, &dwSize) == ERROR_SUCCESS) - { + if (RegOpenKeyExW(hieve, key, 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) { + if (RegQueryValueExW(hKey, L"InstallLocation", NULL, &dwType, (LPBYTE)&root->path, &dwSize) == ERROR_SUCCESS) { // InstallLocation points to the root of the msysgit directory - if (dwSize + 4 > MAX_PATH) // 4 = wcslen(L"etc\\") - { + if (dwSize + 4 > MAX_PATH) {// 4 = wcslen(L"etc\\") giterr_set(GITERR_OS, "Cannot locate the system's msysgit directory - path too long"); return -1; }