diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2020-04-06 10:33:28 +0100 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2020-04-06 10:33:28 +0100 |
commit | 6862a55ddafecfdc097665bfa9b8eff83b74753b (patch) | |
tree | e99100e5aae6790a896ee3df2ec1e461806d9d26 | |
parent | e9b0cfc006ceebda563d1084b1cb6604ac13a15f (diff) | |
download | libgit2-6862a55ddafecfdc097665bfa9b8eff83b74753b.tar.gz |
errors: move system error message getter to util
Provide the system error message functionality (strerror or the string
handling for GetLastError on Windows) into a utility class.
-rw-r--r-- | src/errors.c | 23 | ||||
-rw-r--r-- | src/util.c | 30 | ||||
-rw-r--r-- | src/util.h | 5 |
3 files changed, 37 insertions, 21 deletions
diff --git a/src/errors.c b/src/errors.c index b34aa3abb..6cabb97ee 100644 --- a/src/errors.c +++ b/src/errors.c @@ -60,10 +60,6 @@ void git_error_set(int error_class, const char *fmt, ...) void git_error_vset(int error_class, const char *fmt, va_list ap) { -#ifdef GIT_WIN32 - DWORD win32_error_code = (error_class == GIT_ERROR_OS) ? GetLastError() : 0; -#endif - int error_code = (error_class == GIT_ERROR_OS) ? errno : 0; git_buf *buf = &GIT_GLOBAL->error_buf; git_buf_clear(buf); @@ -73,23 +69,8 @@ void git_error_vset(int error_class, const char *fmt, va_list ap) git_buf_PUTS(buf, ": "); } - if (error_class == GIT_ERROR_OS) { -#ifdef GIT_WIN32 - char * win32_error = git_win32_get_error_message(win32_error_code); - if (win32_error) { - git_buf_puts(buf, win32_error); - git__free(win32_error); - - SetLastError(0); - } - else -#endif - if (error_code) - git_buf_puts(buf, strerror(error_code)); - - if (error_code) - errno = 0; - } + if (error_class == GIT_ERROR_OS) + git__system_errmsg(buf); if (!git_buf_oom(buf)) set_error_from_buffer(error_class); diff --git a/src/util.c b/src/util.c index 859e0a82b..51537f0e2 100644 --- a/src/util.c +++ b/src/util.c @@ -926,3 +926,33 @@ int git__getenv(git_buf *out, const char *name) return git_buf_puts(out, val); } #endif + +int git__system_errmsg(git_buf *out) +{ +#ifdef GIT_WIN32 + if (GetLastError() != 0) { + char *msg = git_win32_get_error_message(GetLastError()); + + if (msg == NULL) { + out->ptr = git_buf__oom; + return -1; + } + + git_buf_puts(out, msg); + git__free(msg); + + SetLastError(0); + return 0; + } +#endif + + if (errno) { + git_buf_puts(out, strerror(errno)); + + errno = 0; + return 0; + } + + git_buf_puts(out, "no error"); + return 0; +} diff --git a/src/util.h b/src/util.h index b49850d23..39c935c09 100644 --- a/src/util.h +++ b/src/util.h @@ -414,6 +414,11 @@ GIT_INLINE(double) git__timer(void) extern int git__getenv(git_buf *out, const char *name); +/* + * Places the system error message into the `git_buf`. + */ +extern int git__system_errmsg(git_buf *out); + #include "alloc.h" #endif |