diff options
Diffstat (limited to 'src/win32/posix_w32.c')
-rw-r--r-- | src/win32/posix_w32.c | 273 |
1 files changed, 168 insertions, 105 deletions
diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c index 3786f0162..10de70da8 100644 --- a/src/win32/posix_w32.c +++ b/src/win32/posix_w32.c @@ -1,10 +1,10 @@ /* - * Copyright (C) 2009-2011 the libgit2 contributors + * Copyright (C) 2009-2012 the libgit2 contributors * * This file is part of libgit2, distributed under the GNU GPL v2 with * a Linking Exception. For full terms see the included COPYING file. */ -#include "posix.h" +#include "../posix.h" #include "path.h" #include "utf-conv.h" #include <errno.h> @@ -17,10 +17,11 @@ int p_unlink(const char *path) int ret = 0; wchar_t* buf; - buf = gitwin_to_utf16(path); - _wchmod(buf, 0666); - ret = _wunlink(buf); - git__free(buf); + if ((buf = gitwin_to_utf16(path)) != NULL) { + _wchmod(buf, 0666); + ret = _wunlink(buf); + git__free(buf); + } return ret; } @@ -60,6 +61,8 @@ static int do_lstat(const char *file_name, struct stat *buf) { WIN32_FILE_ATTRIBUTE_DATA fdata; wchar_t* fbuf = gitwin_to_utf16(file_name); + if (!fbuf) + return -1; if (GetFileAttributesExW(fbuf, GetFileExInfoStandard, &fdata)) { int fMode = S_IREAD; @@ -87,54 +90,43 @@ static int do_lstat(const char *file_name, struct stat *buf) buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime)); git__free(fbuf); - return GIT_SUCCESS; + return 0; } git__free(fbuf); - - switch (GetLastError()) { - case ERROR_ACCESS_DENIED: - case ERROR_SHARING_VIOLATION: - case ERROR_LOCK_VIOLATION: - case ERROR_SHARING_BUFFER_EXCEEDED: - return GIT_EOSERR; - - case ERROR_BUFFER_OVERFLOW: - case ERROR_NOT_ENOUGH_MEMORY: - return GIT_ENOMEM; - - default: - return GIT_EINVALIDPATH; - } + return -1; } int p_lstat(const char *file_name, struct stat *buf) { - int namelen, error; - char alt_name[GIT_PATH_MAX]; + int error; + size_t namelen; + char *alt_name; - if ((error = do_lstat(file_name, buf)) == GIT_SUCCESS) - return GIT_SUCCESS; + if (do_lstat(file_name, buf) == 0) + return 0; /* if file_name ended in a '/', Windows returned ENOENT; * try again without trailing slashes */ - if (error != GIT_EINVALIDPATH) - return git__throw(GIT_EOSERR, "Failed to lstat file"); - namelen = strlen(file_name); if (namelen && file_name[namelen-1] != '/') - return git__throw(GIT_EOSERR, "Failed to lstat file"); + return -1; while (namelen && file_name[namelen-1] == '/') --namelen; - if (!namelen || namelen >= GIT_PATH_MAX) - return git__throw(GIT_ENOMEM, "Failed to lstat file"); + if (!namelen) + return -1; + + alt_name = git__strndup(file_name, namelen); + if (!alt_name) + return -1; + + error = do_lstat(alt_name, buf); - memcpy(alt_name, file_name, namelen); - alt_name[namelen] = 0; - return do_lstat(alt_name, buf); + git__free(alt_name); + return error; } int p_readlink(const char *link, char *target, size_t target_len) @@ -145,6 +137,9 @@ int p_readlink(const char *link, char *target, size_t target_len) DWORD dwRet; wchar_t* link_w; wchar_t* target_w; + int error = 0; + + assert(link && target && target_len > 0); /* * Try to load the pointer to pGetFinalPath dynamically, because @@ -156,12 +151,15 @@ int p_readlink(const char *link, char *target, size_t target_len) if (library != NULL) pGetFinalPath = (fpath_func)GetProcAddress(library, "GetFinalPathNameByHandleW"); - if (pGetFinalPath == NULL) - return git__throw(GIT_EOSERR, + if (pGetFinalPath == NULL) { + giterr_set(GITERR_OS, "'GetFinalPathNameByHandleW' is not available in this platform"); + return -1; + } } link_w = gitwin_to_utf16(link); + GITERR_CHECK_ALLOC(link_w); hFile = CreateFileW(link_w, // file to open GENERIC_READ, // open for reading @@ -173,58 +171,72 @@ int p_readlink(const char *link, char *target, size_t target_len) git__free(link_w); - if (hFile == INVALID_HANDLE_VALUE) - return GIT_EOSERR; - - if (target_len <= 0) { - return GIT_EINVALIDARGS; + if (hFile == INVALID_HANDLE_VALUE) { + giterr_set(GITERR_OS, "Cannot open '%s' for reading", link); + return -1; } target_w = (wchar_t*)git__malloc(target_len * sizeof(wchar_t)); + GITERR_CHECK_ALLOC(target_w); - dwRet = pGetFinalPath(hFile, target_w, target_len, 0x0); - if (dwRet >= target_len) { - git__free(target_w); - CloseHandle(hFile); - return GIT_ENOMEM; - } - - if (!WideCharToMultiByte(CP_UTF8, 0, target_w, -1, target, target_len * sizeof(char), NULL, NULL)) { - git__free(target_w); - return GIT_EOSERR; - } + dwRet = pGetFinalPath(hFile, target_w, (DWORD)target_len, 0x0); + if (dwRet == 0 || + dwRet >= target_len || + !WideCharToMultiByte(CP_UTF8, 0, target_w, -1, target, + (int)(target_len * sizeof(char)), NULL, NULL)) + error = -1; git__free(target_w); CloseHandle(hFile); - if (dwRet > 4) { - /* Skip first 4 characters if they are "\\?\" */ - if (target[0] == '\\' && target[1] == '\\' && target[2] == '?' && target[3] == '\\') { - char tmp[GIT_PATH_MAX]; - unsigned int offset = 4; - dwRet -= 4; - - /* \??\UNC\ */ - if (dwRet > 7 && target[4] == 'U' && target[5] == 'N' && target[6] == 'C') { - offset += 2; - dwRet -= 2; - target[offset] = '\\'; - } - - memcpy(tmp, target + offset, dwRet); - memcpy(target, tmp, dwRet); + if (error) + return error; + + /* Skip first 4 characters if they are "\\?\" */ + if (dwRet > 4 && + target[0] == '\\' && target[1] == '\\' && + target[2] == '?' && target[3] == '\\') + { + unsigned int offset = 4; + dwRet -= 4; + + /* \??\UNC\ */ + if (dwRet > 7 && + target[4] == 'U' && target[5] == 'N' && target[6] == 'C') + { + offset += 2; + dwRet -= 2; + target[offset] = '\\'; } + + memmove(target, target + offset, dwRet); } target[dwRet] = '\0'; + return dwRet; } -int p_open(const char *path, int flags) +int p_open(const char *path, int flags, ...) { int fd; - wchar_t* buf = gitwin_to_utf16(path); - fd = _wopen(buf, flags | _O_BINARY); + wchar_t* buf; + mode_t mode = 0; + + buf = gitwin_to_utf16(path); + if (!buf) + return -1; + + if (flags & O_CREAT) + { + va_list arg_list; + + va_start(arg_list, flags); + mode = va_arg(arg_list, mode_t); + va_end(arg_list); + } + + fd = _wopen(buf, flags | _O_BINARY, mode); git__free(buf); return fd; @@ -234,24 +246,31 @@ int p_creat(const char *path, mode_t mode) { int fd; wchar_t* buf = gitwin_to_utf16(path); + if (!buf) + return -1; fd = _wopen(buf, _O_WRONLY | _O_CREAT | _O_TRUNC | _O_BINARY, mode); - git__free(buf); return fd; } int p_getcwd(char *buffer_out, size_t size) { - wchar_t* buf = (wchar_t*)git__malloc(sizeof(wchar_t) * (int)size); + int ret; + wchar_t* buf; + + if ((size_t)((int)size) != size) + return -1; + + buf = (wchar_t*)git__malloc(sizeof(wchar_t) * (int)size); + GITERR_CHECK_ALLOC(buf); + _wgetcwd(buf, (int)size); - if (!WideCharToMultiByte(CP_UTF8, 0, buf, -1, buffer_out, size, NULL, NULL)) { - git__free(buf); - return GIT_EOSERR; - } + ret = WideCharToMultiByte( + CP_UTF8, 0, buf, -1, buffer_out, (int)size, NULL, NULL); git__free(buf); - return GIT_SUCCESS; + return !ret ? -1 : 0; } int p_stat(const char* path, struct stat* buf) @@ -262,8 +281,10 @@ int p_stat(const char* path, struct stat* buf) int p_chdir(const char* path) { wchar_t* buf = gitwin_to_utf16(path); - int ret = _wchdir(buf); - + int ret; + if (!buf) + return -1; + ret = _wchdir(buf); git__free(buf); return ret; } @@ -271,8 +292,10 @@ int p_chdir(const char* path) int p_chmod(const char* path, mode_t mode) { wchar_t* buf = gitwin_to_utf16(path); - int ret = _wchmod(buf, mode); - + int ret; + if (!buf) + return -1; + ret = _wchmod(buf, mode); git__free(buf); return ret; } @@ -280,44 +303,47 @@ int p_chmod(const char* path, mode_t mode) int p_rmdir(const char* path) { wchar_t* buf = gitwin_to_utf16(path); - int ret = _wrmdir(buf); - + int ret; + if (!buf) + return -1; + ret = _wrmdir(buf); git__free(buf); return ret; } int p_hide_directory__w32(const char *path) { - int error; + int res; wchar_t* buf = gitwin_to_utf16(path); + if (!buf) + return -1; - error = SetFileAttributesW(buf, FILE_ATTRIBUTE_HIDDEN) != 0 ? - GIT_SUCCESS : GIT_ERROR; /* MSDN states a "non zero" value indicates a success */ - + res = SetFileAttributesW(buf, FILE_ATTRIBUTE_HIDDEN); git__free(buf); - if (error < GIT_SUCCESS) - error = git__throw(GIT_EOSERR, "Failed to hide directory '%s'", path); - - return error; + return (res != 0) ? 0 : -1; /* MSDN states a "non zero" value indicates a success */ } char *p_realpath(const char *orig_path, char *buffer) { - int ret; + int ret, buffer_sz = 0; wchar_t* orig_path_w = gitwin_to_utf16(orig_path); wchar_t* buffer_w = (wchar_t*)git__malloc(GIT_PATH_MAX * sizeof(wchar_t)); + if (!orig_path_w || !buffer_w) + return NULL; + ret = GetFullPathNameW(orig_path_w, GIT_PATH_MAX, buffer_w, NULL); git__free(orig_path_w); - if (!ret || ret > GIT_PATH_MAX) { + /* According to MSDN, a return value equals to zero means a failure. */ + if (ret == 0 || ret > GIT_PATH_MAX) { buffer = NULL; goto done; } if (buffer == NULL) { - int buffer_sz = WideCharToMultiByte(CP_UTF8, 0, buffer_w, -1, NULL, 0, NULL, NULL); + buffer_sz = WideCharToMultiByte(CP_UTF8, 0, buffer_w, -1, NULL, 0, NULL, NULL); if (!buffer_sz || !(buffer = (char *)git__malloc(buffer_sz)) || @@ -325,10 +351,22 @@ char *p_realpath(const char *orig_path, char *buffer) { git__free(buffer); buffer = NULL; + goto done; } } else { - if (!WideCharToMultiByte(CP_UTF8, 0, buffer_w, -1, buffer, GIT_PATH_MAX, NULL, NULL)) + if (!WideCharToMultiByte(CP_UTF8, 0, buffer_w, -1, buffer, GIT_PATH_MAX, NULL, NULL)) { buffer = NULL; + goto done; + } + } + + if (!git_path_exists(buffer)) + { + if (buffer_sz > 0) + git__free(buffer); + + buffer = NULL; + errno = ENOENT; } done: @@ -341,8 +379,12 @@ done: int p_vsnprintf(char *buffer, size_t count, const char *format, va_list argptr) { #ifdef _MSC_VER - int len = _vsnprintf(buffer, count, format, argptr); - return (len < 0) ? _vscprintf(format, argptr) : len; + int len; + + if (count == 0 || (len = _vsnprintf(buffer, count, format, argptr)) < 0) + return _vscprintf(format, argptr); + + return len; #else /* MinGW */ return vsnprintf(buffer, count, format, argptr); #endif @@ -366,10 +408,10 @@ int p_mkstemp(char *tmp_path) { #if defined(_MSC_VER) if (_mktemp_s(tmp_path, strlen(tmp_path) + 1) != 0) - return GIT_EOSERR; + return -1; #else if (_mktemp(tmp_path) == NULL) - return GIT_EOSERR; + return -1; #endif return p_creat(tmp_path, 0744); @@ -378,15 +420,17 @@ int p_mkstemp(char *tmp_path) int p_setenv(const char* name, const char* value, int overwrite) { if (overwrite != 1) - return EINVAL; + return -1; - return (SetEnvironmentVariableA(name, value) == 0 ? GIT_EOSERR : GIT_SUCCESS); + return (SetEnvironmentVariableA(name, value) == 0 ? -1 : 0); } int p_access(const char* path, mode_t mode) { wchar_t *buf = gitwin_to_utf16(path); int ret; + if (!buf) + return -1; ret = _waccess(buf, mode); git__free(buf); @@ -394,16 +438,35 @@ int p_access(const char* path, mode_t mode) return ret; } -extern int p_rename(const char *from, const char *to) +int p_rename(const char *from, const char *to) { wchar_t *wfrom = gitwin_to_utf16(from); wchar_t *wto = gitwin_to_utf16(to); int ret; - ret = MoveFileExW(wfrom, wto, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED) ? GIT_SUCCESS : GIT_EOSERR; + if (!wfrom || !wto) + return -1; + + ret = MoveFileExW(wfrom, wto, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED) ? 0 : -1; git__free(wfrom); git__free(wto); return ret; } + +int p_recv(GIT_SOCKET socket, void *buffer, size_t length, int flags) +{ + if ((size_t)((int)length) != length) + return -1; /* giterr_set will be done by caller */ + + return recv(socket, buffer, (int)length, flags); +} + +int p_send(GIT_SOCKET socket, const void *buffer, size_t length, int flags) +{ + if ((size_t)((int)length) != length) + return -1; /* giterr_set will be done by caller */ + + return send(socket, buffer, (int)length, flags); +} |