From 45d387ac78bcf3167d69b736d0b322717bc492d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicent=20Mart=C3=AD?= Date: Wed, 15 Feb 2012 16:54:17 +0100 Subject: refs: Error handling rework. WIP --- src/path.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'src/path.c') diff --git a/src/path.c b/src/path.c index d2c292bf2..c882fe387 100644 --- a/src/path.c +++ b/src/path.c @@ -486,20 +486,23 @@ GIT_INLINE(int) is_dot_or_dotdot(const char *name) int git_path_direach( git_buf *path, - int (*fn)(void *, git_buf *), - void *arg) + int (*fn)(void *, git_buf *, git_error **), + void *arg, + git_error **error) { ssize_t wd_len; DIR *dir; struct dirent de_buf, *de; - if (git_path_to_dir(path) < GIT_SUCCESS) - return git_buf_lasterror(path); + if (git_path_to_dir(path, error) < 0) + return -1; wd_len = path->size; dir = opendir(path->ptr); - if (!dir) - return git__throw(GIT_EOSERR, "Failed to process `%s` tree structure. An error occured while opening the directory", path->ptr); + if (!dir) { + giterr_set(error, GITERR_OS, "Failed to `opendir` %s: %s", path->ptr, strerror(errno)); + return -1; + } while (p_readdir_r(dir, &de_buf, &de) == 0 && de != NULL) { int result; @@ -507,16 +510,18 @@ int git_path_direach( if (is_dot_or_dotdot(de->d_name)) continue; - if (git_buf_puts(path, de->d_name) < GIT_SUCCESS) - return git_buf_lasterror(path); + if (git_buf_puts(path, de->d_name) < 0) { + giterr_set_oom(error); + return -1; + } - result = fn(arg, path); + result = fn(arg, path, error); git_buf_truncate(path, wd_len); /* restore path */ - if (result != GIT_SUCCESS) { + if (result < 0) { closedir(dir); - return result; /* The callee is reponsible for setting the correct error message */ + return -1; } } -- cgit v1.2.1 From 1a48112342932e9fcd45a1ff5935f1c9c53b83d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicent=20Mart=C3=AD?= Date: Fri, 17 Feb 2012 00:13:34 +0100 Subject: error-handling: References Yes, this is error handling solely for `refs.c`, but some of the abstractions leak all ofer the code base. --- src/path.c | 74 ++++++++++++++++++++++++++++---------------------------------- 1 file changed, 33 insertions(+), 41 deletions(-) (limited to 'src/path.c') diff --git a/src/path.c b/src/path.c index c882fe387..5d35e0ef2 100644 --- a/src/path.c +++ b/src/path.c @@ -354,80 +354,75 @@ int git_path_walk_up( return error; } -int git_path_exists(const char *path) +bool git_path_exists(const char *path) { assert(path); - return p_access(path, F_OK); + return p_access(path, F_OK) == 0; } -int git_path_isdir(const char *path) +bool git_path_isdir(const char *path) { #ifdef GIT_WIN32 DWORD attr = GetFileAttributes(path); if (attr == INVALID_FILE_ATTRIBUTES) - return GIT_ERROR; + return false; - return (attr & FILE_ATTRIBUTE_DIRECTORY) ? GIT_SUCCESS : GIT_ERROR; + return (attr & FILE_ATTRIBUTE_DIRECTORY) != 0; #else struct stat st; - if (p_stat(path, &st) < GIT_SUCCESS) - return GIT_ERROR; + if (p_stat(path, &st) < 0) + return false; - return S_ISDIR(st.st_mode) ? GIT_SUCCESS : GIT_ERROR; + return S_ISDIR(st.st_mode) != 0; #endif } -int git_path_isfile(const char *path) +bool git_path_isfile(const char *path) { struct stat st; - int stat_error; assert(path); - stat_error = p_stat(path, &st); + if (p_stat(path, &st) < 0) + return false; - if (stat_error < GIT_SUCCESS) - return -1; - - if (!S_ISREG(st.st_mode)) - return -1; - - return 0; + return S_ISREG(st.st_mode) != 0; } -static int _check_dir_contents( +static bool _check_dir_contents( git_buf *dir, const char *sub, - int (*predicate)(const char *)) + bool (*predicate)(const char *)) { - int error = GIT_SUCCESS; + bool result; size_t dir_size = dir->size; size_t sub_size = strlen(sub); - /* separate allocation and join, so we can always leave git_buf valid */ - if ((error = git_buf_try_grow(dir, dir_size + sub_size + 2)) < GIT_SUCCESS) - return error; + /* leave base valid even if we could not make space for subdir */ + if (git_buf_try_grow(dir, dir_size + sub_size + 2) < 0) + return false; + + /* save excursion */ git_buf_joinpath(dir, dir->ptr, sub); - error = (*predicate)(dir->ptr); + result = predicate(dir->ptr); /* restore path */ git_buf_truncate(dir, dir_size); - - return error; + return result; } -int git_path_contains(git_buf *dir, const char *item) +bool git_path_contains(git_buf *dir, const char *item) { return _check_dir_contents(dir, item, &git_path_exists); } -int git_path_contains_dir(git_buf *base, const char *subdir) +bool git_path_contains_dir(git_buf *base, const char *subdir) { return _check_dir_contents(base, subdir, &git_path_isdir); } -int git_path_contains_file(git_buf *base, const char *file) +bool git_path_contains_file(git_buf *base, const char *file) { return _check_dir_contents(base, file, &git_path_isfile); } @@ -448,7 +443,7 @@ int git_path_find_dir(git_buf *dir, const char *path, const char *base) } /* call dirname if this is not a directory */ - if (error == GIT_SUCCESS && git_path_isdir(dir->ptr) != GIT_SUCCESS) + if (error == GIT_SUCCESS && git_path_isdir(dir->ptr) == false) if (git_path_dirname_r(dir, dir->ptr) < GIT_SUCCESS) error = git_buf_lasterror(dir); @@ -486,21 +481,20 @@ GIT_INLINE(int) is_dot_or_dotdot(const char *name) int git_path_direach( git_buf *path, - int (*fn)(void *, git_buf *, git_error **), - void *arg, - git_error **error) + int (*fn)(void *, git_buf *), + void *arg) { ssize_t wd_len; DIR *dir; struct dirent de_buf, *de; - if (git_path_to_dir(path, error) < 0) + if (git_path_to_dir(path) < 0) return -1; wd_len = path->size; dir = opendir(path->ptr); if (!dir) { - giterr_set(error, GITERR_OS, "Failed to `opendir` %s: %s", path->ptr, strerror(errno)); + giterr_set(GITERR_OS, "Failed to `opendir` %s: %s", path->ptr, strerror(errno)); return -1; } @@ -510,12 +504,10 @@ int git_path_direach( if (is_dot_or_dotdot(de->d_name)) continue; - if (git_buf_puts(path, de->d_name) < 0) { - giterr_set_oom(error); + if (git_buf_puts(path, de->d_name) < 0) return -1; - } - result = fn(arg, path, error); + result = fn(arg, path); git_buf_truncate(path, wd_len); /* restore path */ @@ -526,7 +518,7 @@ int git_path_direach( } closedir(dir); - return GIT_SUCCESS; + return 0; } int git_path_dirload( -- cgit v1.2.1 From cb8a79617b15e347f26d21cedde0f2b8670c1876 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicent=20Mart=C3=AD?= Date: Wed, 7 Mar 2012 00:02:55 +0100 Subject: error-handling: Repository This also includes droping `git_buf_lasterror` because it makes no sense in the new system. Note that in most of the places were it has been dropped, the code needs cleanup. I.e. GIT_ENOMEM is going away, so instead it should return a generic `-1` and obviously not throw anything. --- src/path.c | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) (limited to 'src/path.c') diff --git a/src/path.c b/src/path.c index 5d35e0ef2..8d0cf288f 100644 --- a/src/path.c +++ b/src/path.c @@ -56,7 +56,7 @@ Exit: if (buffer != NULL) { if (git_buf_set(buffer, startp, len) < GIT_SUCCESS) - return git__rethrow(git_buf_lasterror(buffer), + return git__rethrow(GIT_ENOMEM, "Could not get basename of '%s'", path); } @@ -116,7 +116,7 @@ Exit: if (buffer != NULL) { if (git_buf_set(buffer, path, len) < GIT_SUCCESS) - return git__rethrow(git_buf_lasterror(buffer), + return git__rethrow(GIT_ENOMEM, "Could not get dirname of '%s'", path); } @@ -185,34 +185,36 @@ int git_path_root(const char *path) int git_path_prettify(git_buf *path_out, const char *path, const char *base) { - int error = GIT_SUCCESS; char buf[GIT_PATH_MAX]; + assert(path && path_out); git_buf_clear(path_out); /* construct path if needed */ if (base != NULL && git_path_root(path) < 0) { - if ((error = git_buf_joinpath(path_out, base, path)) < GIT_SUCCESS) - return error; + if (git_buf_joinpath(path_out, base, path) < 0) + return -1; + path = path_out->ptr; } - if (path == NULL || p_realpath(path, buf) == NULL) - error = GIT_EOSERR; - else - error = git_buf_sets(path_out, buf); + if (p_realpath(path, buf) == NULL) { + giterr_set(GITERR_OS, "Failed to resolve path '%s': %s", path, strerror(errno)); + return (errno == ENOENT) ? GIT_ENOTFOUND : -1; + } - return error; + if (git_buf_sets(path_out, buf) < 0) + return -1; + + return 0; } int git_path_prettify_dir(git_buf *path_out, const char *path, const char *base) { - int error = git_path_prettify(path_out, path, base); - - if (error == GIT_SUCCESS) - error = git_path_to_dir(path_out); + if (git_path_prettify(path_out, path, base) < 0) + return -1; - return error; + return git_path_to_dir(path_out); } int git_path_to_dir(git_buf *path) @@ -222,7 +224,10 @@ int git_path_to_dir(git_buf *path) path->ptr[path->size - 1] != '/') git_buf_putc(path, '/'); - return git_buf_lasterror(path); + if (git_buf_oom(path)) + return -1; + + return 0; } void git_path_string_to_dir(char* path, size_t size) @@ -445,7 +450,7 @@ int git_path_find_dir(git_buf *dir, const char *path, const char *base) /* call dirname if this is not a directory */ if (error == GIT_SUCCESS && git_path_isdir(dir->ptr) == false) if (git_path_dirname_r(dir, dir->ptr) < GIT_SUCCESS) - error = git_buf_lasterror(dir); + error = GIT_ENOMEM; if (error == GIT_SUCCESS) error = git_path_to_dir(dir); -- cgit v1.2.1 From ae9e29fde7e7d1c0c3e95bdabbb5c96fc71b1c71 Mon Sep 17 00:00:00 2001 From: Russell Belfer Date: Tue, 6 Mar 2012 16:14:31 -0800 Subject: Migrating diff to new error handling Ended up migrating a bunch of upstream functions as well including vector, attr_file, and odb in order to get this to work right. --- src/path.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/path.c') diff --git a/src/path.c b/src/path.c index 8d0cf288f..1ff257a98 100644 --- a/src/path.c +++ b/src/path.c @@ -499,7 +499,7 @@ int git_path_direach( wd_len = path->size; dir = opendir(path->ptr); if (!dir) { - giterr_set(GITERR_OS, "Failed to `opendir` %s: %s", path->ptr, strerror(errno)); + giterr_set(GITERR_OS, "Failed to 'opendir' %s", path->ptr); return -1; } -- cgit v1.2.1 From ab43ad2fd822504446e7876d6352c968a74beb53 Mon Sep 17 00:00:00 2001 From: Russell Belfer Date: Wed, 14 Mar 2012 11:07:14 -0700 Subject: Convert attr and other files to new errors This continues to add other files to the new error handling style. I think the only real concerns here are that there are a couple of error return cases that I have converted to asserts, but I think that it was the correct thing to do given the new error style. --- src/path.c | 127 +++++++++++++++++++++++++++++-------------------------------- 1 file changed, 60 insertions(+), 67 deletions(-) (limited to 'src/path.c') diff --git a/src/path.c b/src/path.c index 1ff257a98..d1f094a1a 100644 --- a/src/path.c +++ b/src/path.c @@ -54,11 +54,8 @@ int git_path_basename_r(git_buf *buffer, const char *path) Exit: result = len; - if (buffer != NULL) { - if (git_buf_set(buffer, startp, len) < GIT_SUCCESS) - return git__rethrow(GIT_ENOMEM, - "Could not get basename of '%s'", path); - } + if (buffer != NULL && git_buf_set(buffer, startp, len) < 0) + return -1; return result; } @@ -114,11 +111,8 @@ int git_path_dirname_r(git_buf *buffer, const char *path) Exit: result = len; - if (buffer != NULL) { - if (git_buf_set(buffer, path, len) < GIT_SUCCESS) - return git__rethrow(GIT_ENOMEM, - "Could not get dirname of '%s'", path); - } + if (buffer != NULL && git_buf_set(buffer, path, len) < 0) + return -1; return result; } @@ -199,7 +193,8 @@ int git_path_prettify(git_buf *path_out, const char *path, const char *base) } if (p_realpath(path, buf) == NULL) { - giterr_set(GITERR_OS, "Failed to resolve path '%s': %s", path, strerror(errno)); + giterr_set(GITERR_OS, "Failed to resolve path '%s': %s", + path, strerror(errno)); return (errno == ENOENT) ? GIT_ENOTFOUND : -1; } @@ -211,10 +206,8 @@ int git_path_prettify(git_buf *path_out, const char *path, const char *base) int git_path_prettify_dir(git_buf *path_out, const char *path, const char *base) { - if (git_path_prettify(path_out, path, base) < 0) - return -1; - - return git_path_to_dir(path_out); + int error = git_path_prettify(path_out, path, base); + return (error < 0) ? error : git_path_to_dir(path_out); } int git_path_to_dir(git_buf *path) @@ -224,10 +217,7 @@ int git_path_to_dir(git_buf *path) path->ptr[path->size - 1] != '/') git_buf_putc(path, '/'); - if (git_buf_oom(path)) - return -1; - - return 0; + return git_buf_oom(path) ? -1 : 0; } void git_path_string_to_dir(char* path, size_t size) @@ -242,7 +232,7 @@ void git_path_string_to_dir(char* path, size_t size) int git__percent_decode(git_buf *decoded_out, const char *input) { - int len, hi, lo, i, error = GIT_SUCCESS; + int len, hi, lo, i; assert(decoded_out && input); len = strlen(input); @@ -268,24 +258,27 @@ int git__percent_decode(git_buf *decoded_out, const char *input) i += 2; append: - error = git_buf_putc(decoded_out, c); - if (error < GIT_SUCCESS) - return git__rethrow(error, "Failed to percent decode '%s'.", input); + if (git_buf_putc(decoded_out, c) < 0) + return -1; } - return error; + return 0; +} + +static int error_invalid_local_file_uri(const char *uri) +{ + giterr_set(GITERR_CONFIG, "'%s' is not a valid local file URI", uri); + return -1; } int git_path_fromurl(git_buf *local_path_out, const char *file_url) { - int error = GIT_SUCCESS, offset = 0, len; + int offset = 0, len; assert(local_path_out && file_url); if (git__prefixcmp(file_url, "file://") != 0) - return git__throw(GIT_EINVALIDPATH, - "Parsing of '%s' failed. A file Uri is expected (ie. with 'file://' scheme).", - file_url); + return error_invalid_local_file_uri(file_url); offset += 7; len = strlen(file_url); @@ -295,12 +288,10 @@ int git_path_fromurl(git_buf *local_path_out, const char *file_url) else if (offset < len && git__prefixcmp(file_url + offset, "localhost/") == 0) offset += 10; else - return git__throw(GIT_EINVALIDPATH, - "Parsing of '%s' failed. A local file Uri is expected.", file_url); + return error_invalid_local_file_uri(file_url); if (offset >= len || file_url[offset] == '/') - return git__throw(GIT_EINVALIDPATH, - "Parsing of '%s' failed. Invalid file Uri format.", file_url); + return error_invalid_local_file_uri(file_url); #ifndef _MSC_VER offset--; /* A *nix absolute path starts with a forward slash */ @@ -308,11 +299,7 @@ int git_path_fromurl(git_buf *local_path_out, const char *file_url) git_buf_clear(local_path_out); - error = git__percent_decode(local_path_out, file_url + offset); - if (error < GIT_SUCCESS) - return git__rethrow(error, "Parsing of '%s' failed.", file_url); - - return error; + return git__percent_decode(local_path_out, file_url + offset); } int git_path_walk_up( @@ -321,7 +308,7 @@ int git_path_walk_up( int (*cb)(void *data, git_buf *), void *data) { - int error = GIT_SUCCESS; + int error = 0; git_buf iter; ssize_t stop = 0, scan; char oldc = '\0'; @@ -341,7 +328,7 @@ int git_path_walk_up( iter.asize = path->asize; while (scan >= stop) { - if ((error = cb(data, &iter)) < GIT_SUCCESS) + if ((error = cb(data, &iter)) < 0) break; iter.ptr[scan] = oldc; scan = git_buf_rfind_next(&iter, '/'); @@ -434,25 +421,24 @@ bool git_path_contains_file(git_buf *base, const char *file) int git_path_find_dir(git_buf *dir, const char *path, const char *base) { - int error = GIT_SUCCESS; + int error; if (base != NULL && git_path_root(path) < 0) error = git_buf_joinpath(dir, base, path); else error = git_buf_sets(dir, path); - if (error == GIT_SUCCESS) { + if (!error) { char buf[GIT_PATH_MAX]; if (p_realpath(dir->ptr, buf) != NULL) error = git_buf_sets(dir, buf); } /* call dirname if this is not a directory */ - if (error == GIT_SUCCESS && git_path_isdir(dir->ptr) == false) - if (git_path_dirname_r(dir, dir->ptr) < GIT_SUCCESS) - error = GIT_ENOMEM; + if (!error && git_path_isdir(dir->ptr) == false) + error = git_path_dirname_r(dir, dir->ptr); - if (error == GIT_SUCCESS) + if (!error) error = git_path_to_dir(dir); return error; @@ -497,9 +483,9 @@ int git_path_direach( return -1; wd_len = path->size; - dir = opendir(path->ptr); - if (!dir) { - giterr_set(GITERR_OS, "Failed to 'opendir' %s", path->ptr); + + if ((dir = opendir(path->ptr)) == NULL) { + giterr_set(GITERR_OS, "Failed to open directory '%s'", path->ptr); return -1; } @@ -541,9 +527,10 @@ int git_path_dirload( path_len = strlen(path); assert(path_len > 0 && path_len >= prefix_len); - if ((dir = opendir(path)) == NULL) - return git__throw(GIT_EOSERR, "Failed to process `%s` tree structure." - " An error occured while opening the directory", path); + if ((dir = opendir(path)) == NULL) { + giterr_set(GITERR_OS, "Failed to open directory '%s'", path); + return -1; + } path += prefix_len; path_len -= prefix_len; @@ -560,8 +547,7 @@ int git_path_dirload( entry_path = git__malloc( path_len + need_slash + entry_len + 1 + alloc_extra); - if (entry_path == NULL) - return GIT_ENOMEM; + GITERR_CHECK_ALLOC(entry_path); if (path_len) memcpy(entry_path, path, path_len); @@ -570,19 +556,16 @@ int git_path_dirload( memcpy(&entry_path[path_len + need_slash], de->d_name, entry_len); entry_path[path_len + need_slash + entry_len] = '\0'; - if ((error = git_vector_insert(contents, entry_path)) < GIT_SUCCESS) { - git__free(entry_path); - return error; - } + if (git_vector_insert(contents, entry_path) < 0) + return -1; } closedir(dir); - if (error != GIT_SUCCESS) - return git__throw( - GIT_EOSERR, "Failed to process directory entry in `%s`", path); + if (error != 0) + giterr_set(GITERR_OS, "Failed to process directory entry in '%s'", path); - return GIT_SUCCESS; + return error; } int git_path_with_stat_cmp(const void *a, const void *b) @@ -601,11 +584,12 @@ int git_path_dirload_with_stat( git_path_with_stat *ps; git_buf full = GIT_BUF_INIT; - if ((error = git_buf_set(&full, path, prefix_len)) != GIT_SUCCESS) - return error; + if (git_buf_set(&full, path, prefix_len) < 0) + return -1; - if ((error = git_path_dirload(path, prefix_len, - sizeof(git_path_with_stat) + 1, contents)) != GIT_SUCCESS) { + error = git_path_dirload( + path, prefix_len, sizeof(git_path_with_stat) + 1, contents); + if (error < 0) { git_buf_free(&full); return error; } @@ -616,8 +600,17 @@ int git_path_dirload_with_stat( memmove(ps->path, ps, path_len + 1); ps->path_len = path_len; - git_buf_joinpath(&full, full.ptr, ps->path); - p_lstat(full.ptr, &ps->st); + if (git_buf_joinpath(&full, full.ptr, ps->path) < 0) { + error = -1; + break; + } + + if (p_lstat(full.ptr, &ps->st) < 0) { + giterr_set(GITERR_OS, "Failed to stat file '%s'", full.ptr); + error = -1; + break; + } + git_buf_truncate(&full, prefix_len); if (S_ISDIR(ps->st.st_mode)) { -- cgit v1.2.1 From deafee7bd7a9e2efcdff90627b6094d8c1519319 Mon Sep 17 00:00:00 2001 From: Russell Belfer Date: Wed, 14 Mar 2012 17:36:15 -0700 Subject: Continue error conversion This converts blob.c, fileops.c, and all of the win32 files. Also, various minor cleanups throughout the code. Plus, in testing the win32 build, I cleaned up a bunch (although not all) of the warnings with the 64-bit build. --- src/path.c | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) (limited to 'src/path.c') diff --git a/src/path.c b/src/path.c index d1f094a1a..0f45d7130 100644 --- a/src/path.c +++ b/src/path.c @@ -49,7 +49,8 @@ int git_path_basename_r(git_buf *buffer, const char *path) while (startp > path && *(startp - 1) != '/') startp--; - len = endp - startp +1; + /* Cast is safe because max path < max int */ + len = (int)(endp - startp + 1); Exit: result = len; @@ -96,7 +97,8 @@ int git_path_dirname_r(git_buf *buffer, const char *path) endp--; } while (endp > path && *endp == '/'); - len = endp - path +1; + /* Cast is safe because max path < max int */ + len = (int)(endp - path + 1); #ifdef GIT_WIN32 /* Mimic unix behavior where '/.git' returns '/': 'C:/.git' will return @@ -146,7 +148,7 @@ char *git_path_basename(const char *path) const char *git_path_topdir(const char *path) { size_t len; - int i; + ssize_t i; assert(path); len = strlen(path); @@ -154,7 +156,7 @@ const char *git_path_topdir(const char *path) if (!len || path[len - 1] != '/') return NULL; - for (i = len - 2; i >= 0; --i) + for (i = (ssize_t)len - 2; i >= 0; --i) if (path[i] == '/') break; @@ -235,7 +237,7 @@ int git__percent_decode(git_buf *decoded_out, const char *input) int len, hi, lo, i; assert(decoded_out && input); - len = strlen(input); + len = (int)strlen(input); git_buf_clear(decoded_out); for(i = 0; i < len; i++) @@ -281,7 +283,7 @@ int git_path_fromurl(git_buf *local_path_out, const char *file_url) return error_invalid_local_file_uri(file_url); offset += 7; - len = strlen(file_url); + len = (int)strlen(file_url); if (offset < len && file_url[offset] == '/') offset++; @@ -381,6 +383,18 @@ bool git_path_isfile(const char *path) return S_ISREG(st.st_mode) != 0; } +int git_path_lstat(const char *path, struct stat *st) +{ + int err = 0; + + if (p_lstat(path, st) < 0) { + err = (errno == ENOENT) ? GIT_ENOTFOUND : -1; + giterr_set(GITERR_OS, "Failed to stat file '%s'", path); + } + + return err; +} + static bool _check_dir_contents( git_buf *dir, const char *sub, @@ -600,16 +614,9 @@ int git_path_dirload_with_stat( memmove(ps->path, ps, path_len + 1); ps->path_len = path_len; - if (git_buf_joinpath(&full, full.ptr, ps->path) < 0) { - error = -1; + if ((error = git_buf_joinpath(&full, full.ptr, ps->path)) < 0 || + (error = git_path_lstat(full.ptr, &ps->st)) < 0) break; - } - - if (p_lstat(full.ptr, &ps->st) < 0) { - giterr_set(GITERR_OS, "Failed to stat file '%s'", full.ptr); - error = -1; - break; - } git_buf_truncate(&full, prefix_len); -- cgit v1.2.1 From 7b93079b5b7c5f58de321bb9846e93b1717d3e4c Mon Sep 17 00:00:00 2001 From: nulltoken Date: Fri, 16 Mar 2012 15:16:52 +0100 Subject: Make git_path_root() cope with windows network paths Fix libgit2/libgit2sharp#125 --- src/path.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/path.c') diff --git a/src/path.c b/src/path.c index 0f45d7130..45cc94e82 100644 --- a/src/path.c +++ b/src/path.c @@ -171,6 +171,15 @@ int git_path_root(const char *path) /* Does the root of the path look like a windows drive ? */ if (isalpha(path[0]) && (path[1] == ':')) offset += 2; + + /* Are we dealing with a network path? */ + else if (path[0] == '/' && path[1] == '/') { + offset += 2; + + /* Skip the computer name segment */ + while (*(path + offset) && *(path + offset) != '/') + offset++; + } #endif if (*(path + offset) == '/') -- cgit v1.2.1 From 0d0fa7c3681e4ef3d0452666a9bc97d4b08391c9 Mon Sep 17 00:00:00 2001 From: Russell Belfer Date: Fri, 16 Mar 2012 15:56:01 -0700 Subject: Convert attr, ignore, mwindow, status to new errors Also cleaned up some previously converted code that still had little things to polish. --- src/path.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/path.c') diff --git a/src/path.c b/src/path.c index 45cc94e82..31d2e72f9 100644 --- a/src/path.c +++ b/src/path.c @@ -327,7 +327,7 @@ int git_path_walk_up( assert(path && cb); if (ceiling != NULL) { - if (git__prefixcmp(path->ptr, ceiling) == GIT_SUCCESS) + if (git__prefixcmp(path->ptr, ceiling) == 0) stop = (ssize_t)strlen(ceiling); else stop = path->size; -- cgit v1.2.1 From 7784bcbbee972d1f00ea88655a5592fb44ca767d Mon Sep 17 00:00:00 2001 From: Russell Belfer Date: Wed, 11 Apr 2012 11:52:59 -0700 Subject: Refactor git_repository_open with new options Add a new command `git_repository_open_ext` with extended options that control how searching for a repository will be done. The existing `git_repository_open` and `git_repository_discover` are reimplemented on top of it. We may want to change the default behavior of `git_repository_open` but this commit does not do that. Improve support for "gitdir" files where the work dir is separate from the repo and support for the "separate-git-dir" config. Also, add support for opening repos created with `git-new-workdir` script (although I have only confirmed that they can be opened, not that all functions work correctly). There are also a few minor changes that came up: - Fix `git_path_prettify` to allow in-place prettifying. - Fix `git_path_root` to support backslashes on Win32. This fix should help many repo open/discover scenarios - it is the one function called when opening before prettifying the path. - Tweak `git_config_get_string` to set the "out" pointer to NULL if the config value is not found. Allows some other cleanup. - Fix a couple places that should have been calling `git_repository_config__weakptr` and were not. - Fix `cl_git_sandbox_init` clar helper to support bare repos. --- src/path.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) (limited to 'src/path.c') diff --git a/src/path.c b/src/path.c index 31d2e72f9..3c1a723ea 100644 --- a/src/path.c +++ b/src/path.c @@ -172,20 +172,22 @@ int git_path_root(const char *path) if (isalpha(path[0]) && (path[1] == ':')) offset += 2; - /* Are we dealing with a network path? */ - else if (path[0] == '/' && path[1] == '/') { + /* Are we dealing with a windows network path? */ + else if ((path[0] == '/' && path[1] == '/') || + (path[0] == '\\' && path[1] == '\\')) + { offset += 2; - + /* Skip the computer name segment */ - while (*(path + offset) && *(path + offset) != '/') + while (path[offset] && path[offset] != '/' && path[offset] != '\\') offset++; } #endif - if (*(path + offset) == '/') + if (path[offset] == '/' || path[offset] == '\\') return offset; - return -1; /* Not a real error. Rather a signal than the path is not rooted */ + return -1; /* Not a real error - signals that path is not rooted */ } int git_path_prettify(git_buf *path_out, const char *path, const char *base) @@ -193,26 +195,21 @@ int git_path_prettify(git_buf *path_out, const char *path, const char *base) char buf[GIT_PATH_MAX]; assert(path && path_out); - git_buf_clear(path_out); /* construct path if needed */ if (base != NULL && git_path_root(path) < 0) { if (git_buf_joinpath(path_out, base, path) < 0) return -1; - path = path_out->ptr; } if (p_realpath(path, buf) == NULL) { - giterr_set(GITERR_OS, "Failed to resolve path '%s': %s", - path, strerror(errno)); + giterr_set(GITERR_OS, "Failed to resolve path '%s'", path); + git_buf_clear(path_out); return (errno == ENOENT) ? GIT_ENOTFOUND : -1; } - if (git_buf_sets(path_out, buf) < 0) - return -1; - - return 0; + return git_buf_sets(path_out, buf); } int git_path_prettify_dir(git_buf *path_out, const char *path, const char *base) -- cgit v1.2.1 From 44ef8b1b300f0cd3d8572fa1b40d257462f28240 Mon Sep 17 00:00:00 2001 From: Russell Belfer Date: Fri, 13 Apr 2012 13:00:10 -0700 Subject: Fix warnings on 64-bit windows builds This fixes all the warnings on win64 except those in deps, which come from the regex code. --- src/path.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'src/path.c') diff --git a/src/path.c b/src/path.c index 3c1a723ea..a7cf4402f 100644 --- a/src/path.c +++ b/src/path.c @@ -464,21 +464,22 @@ int git_path_find_dir(git_buf *dir, const char *path, const char *base) return error; } -int git_path_cmp(const char *name1, int len1, int isdir1, - const char *name2, int len2, int isdir2) +int git_path_cmp( + const char *name1, size_t len1, int isdir1, + const char *name2, size_t len2, int isdir2) { - int len = len1 < len2 ? len1 : len2; + size_t len = len1 < len2 ? len1 : len2; int cmp; cmp = memcmp(name1, name2, len); if (cmp) return cmp; if (len1 < len2) - return ((!isdir1 && !isdir2) ? -1 : - (isdir1 ? '/' - name2[len1] : name2[len1] - '/')); + return (!isdir1 && !isdir2) ? -1 : + (isdir1 ? '/' - name2[len1] : name2[len1] - '/'); if (len1 > len2) - return ((!isdir1 && !isdir2) ? 1 : - (isdir2 ? name1[len2] - '/' : '/' - name1[len2])); + return (!isdir1 && !isdir2) ? 1 : + (isdir2 ? name1[len2] - '/' : '/' - name1[len2]); return 0; } -- cgit v1.2.1 From fa6420f73e8a621cc04e95820b625097b5c2fbf2 Mon Sep 17 00:00:00 2001 From: nulltoken Date: Sun, 29 Apr 2012 21:46:33 +0200 Subject: buf: deploy git_buf_len() --- src/path.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/path.c') diff --git a/src/path.c b/src/path.c index a7cf4402f..f562b0b9e 100644 --- a/src/path.c +++ b/src/path.c @@ -221,8 +221,8 @@ int git_path_prettify_dir(git_buf *path_out, const char *path, const char *base) int git_path_to_dir(git_buf *path) { if (path->asize > 0 && - path->size > 0 && - path->ptr[path->size - 1] != '/') + git_buf_len(path) > 0 && + path->ptr[git_buf_len(path) - 1] != '/') git_buf_putc(path, '/'); return git_buf_oom(path) ? -1 : 0; @@ -327,12 +327,12 @@ int git_path_walk_up( if (git__prefixcmp(path->ptr, ceiling) == 0) stop = (ssize_t)strlen(ceiling); else - stop = path->size; + stop = git_buf_len(path); } - scan = path->size; + scan = git_buf_len(path); iter.ptr = path->ptr; - iter.size = path->size; + iter.size = git_buf_len(path); iter.asize = path->asize; while (scan >= stop) { @@ -407,7 +407,7 @@ static bool _check_dir_contents( bool (*predicate)(const char *)) { bool result; - size_t dir_size = dir->size; + size_t dir_size = git_buf_len(dir); size_t sub_size = strlen(sub); /* leave base valid even if we could not make space for subdir */ @@ -503,7 +503,7 @@ int git_path_direach( if (git_path_to_dir(path) < 0) return -1; - wd_len = path->size; + wd_len = git_buf_len(path); if ((dir = opendir(path->ptr)) == NULL) { giterr_set(GITERR_OS, "Failed to open directory '%s'", path->ptr); -- cgit v1.2.1