]> git.proxmox.com Git - libgit2.git/commitdiff
windows: Path conversion with better semantics
authorVicent Marti <tanoku@gmail.com>
Tue, 13 Aug 2013 07:15:39 +0000 (09:15 +0200)
committerVicent Marti <tanoku@gmail.com>
Tue, 13 Aug 2013 07:15:39 +0000 (09:15 +0200)
src/fileops.c
src/path.c
src/transports/winhttp.c
src/win32/dir.c
src/win32/dir.h
src/win32/findfile.c
src/win32/posix_w32.c
src/win32/utf-conv.h
tests-clar/clar_libgit2.c

index d62158db70b18773cf90770816ad95bf773004cb..cf77ad164931beda3cb100bce37565d2c083bbac 100644 (file)
@@ -58,9 +58,9 @@ int git_futils_creat_locked(const char *path, const mode_t mode)
        int fd;
 
 #ifdef GIT_WIN32
-       git_win32_path_utf16 buf;
+       git_win32_path buf;
 
-       git__win32_path_utf8_to_16(buf, path);
+       git__win32_path_from_c(buf, path);
        fd = _wopen(buf, O_WRONLY | O_CREAT | O_TRUNC |
                O_EXCL | O_BINARY | O_CLOEXEC, mode);
 #else
index d0cecc07018c6434b3631a954f56b81fd32d2cd6..cf9a3776a12ee60f13644f60a0c058eb2d72258a 100644 (file)
@@ -486,14 +486,14 @@ bool git_path_is_empty_dir(const char *path)
 {
        git_buf pathbuf = GIT_BUF_INIT;
        HANDLE hFind = INVALID_HANDLE_VALUE;
-       git_win32_path_utf16 wbuf;
+       git_win32_path wbuf;
        WIN32_FIND_DATAW ffd;
        bool retval = true;
 
        if (!git_path_isdir(path)) return false;
 
        git_buf_printf(&pathbuf, "%s\\*", path);
-       git__win32_path_utf8_to_16(wbuf, git_buf_cstr(&pathbuf));
+       git__win32_path_from_c(wbuf, git_buf_cstr(&pathbuf));
 
        hFind = FindFirstFileW(wbuf, &ffd);
        if (INVALID_HANDLE_VALUE == hFind) {
index 5c55c10f1c260be10daca2ceadeba63cc655bbcf..6f182c94cf2a771c7f91f75ca442aae2816b2386 100644 (file)
@@ -893,7 +893,7 @@ static int winhttp_connect(
        const char *url)
 {
        wchar_t *ua = L"git/1.0 (libgit2 " WIDEN(LIBGIT2_VERSION) L")";
-       git_win32_path_utf16 host;
+       git_win32_path host;
        int32_t port;
        const char *default_port = "80";
        int ret;
@@ -920,7 +920,7 @@ static int winhttp_connect(
                return -1;
 
        /* Prepare host */
-       git__win32_path_utf8_to_16(host, t->host);
+       git__win32_path_from_c(host, t->host);
 
        /* Establish session */
        t->session = WinHttpOpen(
index 85f793d62926fda01f50b4b5aa4235a137be5e54..f75b8d57e3dc24eda6132815121f4f7d8547d2ad 100644 (file)
@@ -26,7 +26,7 @@ static int init_filter(char *filter, size_t n, const char *dir)
 git__DIR *git__opendir(const char *dir)
 {
        char filter[GIT_WIN_PATH_UTF8];
-       git_win32_path_utf16 filter_w;
+       git_win32_path filter_w;
        git__DIR *new = NULL;
 
        if (!dir || !init_filter(filter, sizeof(filter), dir))
@@ -40,7 +40,7 @@ git__DIR *git__opendir(const char *dir)
        if (!new->dir)
                goto fail;
 
-       git__win32_path_utf8_to_16(filter_w, filter);
+       git__win32_path_from_c(filter_w, filter);
        new->h = FindFirstFileW(filter_w, &new->f);
 
        if (new->h == INVALID_HANDLE_VALUE) {
@@ -80,7 +80,7 @@ int git__readdir_ext(
        if (wcslen(d->f.cFileName) >= sizeof(entry->d_name))
                return -1;
 
-       git__win32_path_utf16_to_8(entry->d_name, d->f.cFileName);
+       git__win32_path_from_c(entry->d_name, d->f.cFileName);
        entry->d_ino = 0;
 
        *result = entry;
@@ -102,7 +102,7 @@ struct git__dirent *git__readdir(git__DIR *d)
 void git__rewinddir(git__DIR *d)
 {
        char filter[GIT_WIN_PATH_UTF8];
-       git_win32_path_utf16 filter_w;
+       git_win32_path filter_w;
 
        if (!d)
                return;
@@ -116,7 +116,7 @@ void git__rewinddir(git__DIR *d)
        if (!init_filter(filter, sizeof(filter), d->dir))
                return;
 
-       git__win32_path_utf8_to_16(filter_w, filter);
+       git__win32_path_from_c(filter_w, filter);
        d->h = FindFirstFileW(filter_w, &d->f);
 
        if (d->h == INVALID_HANDLE_VALUE)
index a3e154972369e444e359e206551bac1975dc603a..3875ab1590ef47c6cc76e26179d03d967b65317d 100644 (file)
@@ -11,7 +11,7 @@
 
 struct git__dirent {
        int d_ino;
-       char d_name[260*4+1];
+       char d_name[GIT_WIN_PATH_UTF8];
 };
 
 typedef struct {
index 64b9bb537538358ec695c1c88a533523d6d208f7..a1c11fcfb705bc095f0db61cf44d368fd07f6962 100644 (file)
@@ -23,11 +23,11 @@ int git_win32__expand_path(struct git_win32__path *s_root, const wchar_t *templ)
        return s_root->len ? 0 : -1;
 }
 
-static int win32_path_utf16_to_8(git_buf *path_utf8, const wchar_t *path_utf16)
+static int win32_path_to_8(git_buf *path_utf8, const wchar_t *path)
 {
        char temp_utf8[GIT_PATH_MAX];
 
-       git__utf16_to_8(temp_utf8, GIT_PATH_MAX, path_utf16);
+       git__utf16_to_8(temp_utf8, GIT_PATH_MAX, path);
        git_path_mkposix(temp_utf8);
 
        return git_buf_sets(path_utf8, temp_utf8);
@@ -61,7 +61,7 @@ int git_win32__find_file(
                return GIT_ENOTFOUND;
        }
 
-       win32_path_utf16_to_8(path, file_utf16);
+       win32_path_to_8(path, file_utf16);
        git__free(file_utf16);
 
        return 0;
@@ -113,7 +113,7 @@ static int win32_find_git_in_path(git_buf *buf, const wchar_t *gitexe)
                        /* replace "bin\\" or "cmd\\" with "etc\\" */
                        wcscpy(&root.path[root.len - 4], L"etc\\");
 
-                       win32_path_utf16_to_8(buf, root.path);
+                       win32_path_to_8(buf, root.path);
                        return 0;
                }
        }
@@ -146,7 +146,7 @@ static int win32_find_git_in_registry(
                        wcscat(path16.path, L"etc\\");
                        path16.len += 4;
 
-                       win32_path_utf16_to_8(buf, path16.path);
+                       win32_path_to_8(buf, path16.path);
                }
 
                RegCloseKey(hKey);
@@ -168,7 +168,7 @@ static int win32_find_existing_dirs(
                        path16.path[0] != L'%' &&
                        !_waccess(path16.path, F_OK))
                {
-                       win32_path_utf16_to_8(&buf, path16.path);
+                       win32_path_to_8(&buf, path16.path);
 
                        if (buf.size)
                                git_buf_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr);
index d9a68f284744939ded81978292364b0ad87e6447..437ded284496a9bb9283463b0b023780e24ae896 100644 (file)
@@ -16,8 +16,8 @@
 
 int p_unlink(const char *path)
 {
-       git_win32_path_utf16 buf;
-       git__win32_path_utf8_to_16(buf, path);
+       git_win32_path buf;
+       git__win32_path_from_c(buf, path);
        _wchmod(buf, 0666);
        return _wunlink(buf);
 }
@@ -59,11 +59,11 @@ static int do_lstat(
        const char *file_name, struct stat *buf, int posix_enotdir)
 {
        WIN32_FILE_ATTRIBUTE_DATA fdata;
-       git_win32_path_utf16 fbuf;
+       git_win32_path fbuf;
        wchar_t lastch;
        int flen;
 
-       flen = git__win32_path_utf8_to_16(fbuf, file_name);
+       flen = git__win32_path_from_c(fbuf, file_name);
 
        /* truncate trailing slashes */
        for (; flen > 0; --flen) {
@@ -166,7 +166,7 @@ int p_readlink(const char *link, char *target, size_t target_len)
        static fpath_func pGetFinalPath = NULL;
        HANDLE hFile;
        DWORD dwRet;
-       git_win32_path_utf16 link_w;
+       git_win32_path link_w;
        wchar_t* target_w;
        int error = 0;
 
@@ -189,7 +189,7 @@ int p_readlink(const char *link, char *target, size_t target_len)
                }
        }
 
-       git__win32_path_utf8_to_16(link_w, link);
+       git__win32_path_from_c(link_w, link);
 
        hFile = CreateFileW(link_w,                     // file to open
                        GENERIC_READ,                   // open for reading
@@ -255,10 +255,10 @@ int p_symlink(const char *old, const char *new)
 
 int p_open(const char *path, int flags, ...)
 {
-       git_win32_path_utf16 buf;
+       git_win32_path buf;
        mode_t mode = 0;
 
-       git__win32_path_utf8_to_16(buf, path);
+       git__win32_path_from_c(buf, path);
 
        if (flags & O_CREAT) {
                va_list arg_list;
@@ -273,8 +273,8 @@ int p_open(const char *path, int flags, ...)
 
 int p_creat(const char *path, mode_t mode)
 {
-       git_win32_path_utf16 buf;
-       git__win32_path_utf8_to_16(buf, path);
+       git_win32_path buf;
+       git__win32_path_from_c(buf, path);
        return _wopen(buf, _O_WRONLY | _O_CREAT | _O_TRUNC | _O_BINARY, mode);
 }
 
@@ -316,23 +316,23 @@ int p_stat(const char* path, struct stat* buf)
 
 int p_chdir(const char* path)
 {
-       git_win32_path_utf16 buf;
-       git__win32_path_utf8_to_16(buf, path);
+       git_win32_path buf;
+       git__win32_path_from_c(buf, path);
        return _wchdir(buf);
 }
 
 int p_chmod(const char* path, mode_t mode)
 {
-       git_win32_path_utf16 buf;
-       git__win32_path_utf8_to_16(buf, path);
+       git_win32_path buf;
+       git__win32_path_from_c(buf, path);
        return _wchmod(buf, mode);
 }
 
 int p_rmdir(const char* path)
 {
        int error;
-       git_win32_path_utf16 buf;
-       git__win32_path_utf8_to_16(buf, path);
+       git_win32_path buf;
+       git__win32_path_from_c(buf, path);
 
        error = _wrmdir(buf);
 
@@ -348,18 +348,18 @@ int p_rmdir(const char* path)
 
 int p_hide_directory__w32(const char *path)
 {
-       git_win32_path_utf16 buf;
-       git__win32_path_utf8_to_16(buf, path);
+       git_win32_path buf;
+       git__win32_path_from_c(buf, path);
        return (SetFileAttributesW(buf, FILE_ATTRIBUTE_HIDDEN) != 0) ? 0 : -1;
 }
 
 char *p_realpath(const char *orig_path, char *buffer)
 {
        int ret;
-       git_win32_path_utf16 orig_path_w;
-       git_win32_path_utf16 buffer_w;
+       git_win32_path orig_path_w;
+       git_win32_path buffer_w;
 
-       git__win32_path_utf8_to_16(orig_path_w, orig_path);
+       git__win32_path_from_c(orig_path_w, orig_path);
 
        /* Implicitly use GetCurrentDirectory which can be a threading issue */
        ret = GetFullPathNameW(orig_path_w, GIT_WIN_PATH_UTF16, buffer_w, NULL);
@@ -449,18 +449,18 @@ int p_setenv(const char* name, const char* value, int overwrite)
 
 int p_access(const char* path, mode_t mode)
 {
-       git_win32_path_utf16 buf;
-       git__win32_path_utf8_to_16(buf, path);
+       git_win32_path buf;
+       git__win32_path_from_c(buf, path);
        return _waccess(buf, mode);
 }
 
 int p_rename(const char *from, const char *to)
 {
-       git_win32_path_utf16 wfrom;
-       git_win32_path_utf16 wto;
+       git_win32_path wfrom;
+       git_win32_path wto;
 
-       git__win32_path_utf8_to_16(wfrom, from);
-       git__win32_path_utf8_to_16(wto, to);
+       git__win32_path_from_c(wfrom, from);
+       git__win32_path_from_c(wto, to);
        return MoveFileExW(wfrom, wto, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED) ? 0 : -1;
 }
 
index 66422e17c6a22f08c8704196f7e5227891d33881..3cfb3514e4b4b7a0749ff59b0f130b3f76d6825a 100644 (file)
 #define GIT_WIN_PATH_UTF16 (260 + 1)
 #define GIT_WIN_PATH_UTF8  (260 * 4 + 1)
 
-typedef wchar_t git_win32_path_utf16[GIT_WIN_PATH_UTF16];
-typedef char git_win32_path_utf8[GIT_WIN_PATH_UTF8];
+typedef wchar_t git_win32_path[GIT_WIN_PATH_UTF16];
 
 // dest_size is the size of dest in wchar_t's
 int git__utf8_to_16(wchar_t * dest, size_t dest_size, const char *src);
 // dest_size is the size of dest in char's
 int git__utf16_to_8(char *dest, size_t dest_size, const wchar_t *src);
 
-GIT_INLINE(int) git__win32_path_utf8_to_16(git_win32_path_utf16 dest, const char *src)
+GIT_INLINE(int) git__win32_path_from_c(git_win32_path dest, const char *src)
 {
        return git__utf8_to_16(dest, GIT_WIN_PATH_UTF16, src);
 }
 
-GIT_INLINE(int) git__win32_path_utf16_to_8(git_win32_path_utf8 dest, const wchar_t *src)
+GIT_INLINE(int) git__win32_path_to_c(char *dest, const git_win32_path *src)
 {
        return git__utf16_to_8(dest, GIT_WIN_PATH_UTF8, src);
 }
index 6c741accd0f8fc308bbe025f54bc07daf27393d1..9e0fbfbaceda65742f84e6cf54493372d1839e29 100644 (file)
@@ -56,12 +56,12 @@ void cl_git_rewritefile(const char *filename, const char *new_content)
 
 char *cl_getenv(const char *name)
 {
-       git_win32_path_utf16 name_utf16;
+       git_win32_path name_utf16;
        DWORD alloc_len;
        wchar_t *value_utf16;
        char *value_utf8;
 
-       git__win32_path_utf8_to_16(name_utf16, name);
+       git__win32_path_from_c(name_utf16, name);
        alloc_len = GetEnvironmentVariableW(name_utf16, NULL, 0);
        if (alloc_len <= 0)
                return NULL;
@@ -72,7 +72,7 @@ char *cl_getenv(const char *name)
        GetEnvironmentVariableW(name_utf16, value_utf16, alloc_len);
 
        cl_assert(value_utf8 = git__malloc(alloc_len));
-       git__win32_path_utf16_to_8(value_utf8, value_utf16);
+       git__utf16_to_8(value_utf8, alloc_len, value_utf16);
 
        git__free(value_utf16);
 
@@ -81,13 +81,13 @@ char *cl_getenv(const char *name)
 
 int cl_setenv(const char *name, const char *value)
 {
-       git_win32_path_utf16 name_utf16;
-       git_win32_path_utf16 value_utf16;
+       git_win32_path name_utf16;
+       git_win32_path value_utf16;
 
-       git__win32_path_utf8_to_16(name_utf16, name);
+       git__win32_path_from_c(name_utf16, name);
 
        if (value) {
-               git__win32_path_utf8_to_16(value_utf16, value);
+               git__win32_path_from_c(value_utf16, value);
                cl_assert(SetEnvironmentVariableW(name_utf16, value_utf16));
        } else {
                /* Windows XP returns 0 (failed) when passing NULL for lpValue when
@@ -107,12 +107,12 @@ int cl_setenv(const char *name, const char *value)
  * the source is a directory, a child of the source). */
 int cl_rename(const char *source, const char *dest)
 {
-       git_win32_path_utf16 source_utf16;
-       git_win32_path_utf16 dest_utf16;
+       git_win32_path source_utf16;
+       git_win32_path dest_utf16;
        unsigned retries = 1;
 
-       git__win32_path_utf8_to_16(source_utf16, source);
-       git__win32_path_utf8_to_16(dest_utf16, dest);
+       git__win32_path_from_c(source_utf16, source);
+       git__win32_path_from_c(dest_utf16, dest);
 
        while (!MoveFileW(source_utf16, dest_utf16)) {
                /* Only retry if the error is ERROR_ACCESS_DENIED;