Commit 6af24ce3 by Russell Belfer

Merge pull request #590 from arrbee/new-error-handling

Migrating diff to new error handling
parents e54d8d89 998f7b3d
...@@ -11,24 +11,19 @@ static void git_attr_rule__clear(git_attr_rule *rule); ...@@ -11,24 +11,19 @@ static void git_attr_rule__clear(git_attr_rule *rule);
int git_attr_file__new(git_attr_file **attrs_ptr) int git_attr_file__new(git_attr_file **attrs_ptr)
{ {
int error;
git_attr_file *attrs = NULL; git_attr_file *attrs = NULL;
attrs = git__calloc(1, sizeof(git_attr_file)); attrs = git__calloc(1, sizeof(git_attr_file));
if (attrs == NULL) GITERR_CHECK_ALLOC(attrs);
error = GIT_ENOMEM;
else
error = git_vector_init(&attrs->rules, 4, NULL);
if (error != GIT_SUCCESS) { if (git_vector_init(&attrs->rules, 4, NULL) < 0) {
git__rethrow(error, "Could not allocate attribute storage");
git__free(attrs); git__free(attrs);
attrs = NULL; attrs = NULL;
} }
*attrs_ptr = attrs; *attrs_ptr = attrs;
return error; return attrs ? 0 : -1;
} }
int git_attr_file__set_path( int git_attr_file__set_path(
...@@ -50,13 +45,15 @@ int git_attr_file__set_path( ...@@ -50,13 +45,15 @@ int git_attr_file__set_path(
file->path = git__strdup(path); file->path = git__strdup(path);
} }
return (file->path == NULL) ? GIT_ENOMEM : GIT_SUCCESS; GITERR_CHECK_ALLOC(file->path);
return 0;
} }
int git_attr_file__from_buffer( int git_attr_file__from_buffer(
git_repository *repo, const char *buffer, git_attr_file *attrs) git_repository *repo, const char *buffer, git_attr_file *attrs)
{ {
int error = GIT_SUCCESS; int error = 0;
const char *scan = NULL; const char *scan = NULL;
char *context = NULL; char *context = NULL;
git_attr_rule *rule = NULL; git_attr_rule *rule = NULL;
...@@ -68,13 +65,13 @@ int git_attr_file__from_buffer( ...@@ -68,13 +65,13 @@ int git_attr_file__from_buffer(
if (attrs->path && git__suffixcmp(attrs->path, GIT_ATTR_FILE) == 0) { if (attrs->path && git__suffixcmp(attrs->path, GIT_ATTR_FILE) == 0) {
context = git__strndup(attrs->path, context = git__strndup(attrs->path,
strlen(attrs->path) - strlen(GIT_ATTR_FILE)); strlen(attrs->path) - strlen(GIT_ATTR_FILE));
if (!context) error = GIT_ENOMEM; GITERR_CHECK_ALLOC(context);
} }
while (error == GIT_SUCCESS && *scan) { while (!error && *scan) {
/* allocate rule if needed */ /* allocate rule if needed */
if (!rule && !(rule = git__calloc(1, sizeof(git_attr_rule)))) { if (!rule && !(rule = git__calloc(1, sizeof(git_attr_rule)))) {
error = GIT_ENOMEM; error = -1;
break; break;
} }
...@@ -92,10 +89,10 @@ int git_attr_file__from_buffer( ...@@ -92,10 +89,10 @@ int git_attr_file__from_buffer(
} }
/* if the rule wasn't a pattern, on to the next */ /* if the rule wasn't a pattern, on to the next */
if (error != GIT_SUCCESS) { if (error < 0) {
git_attr_rule__clear(rule); /* reset rule contents */ git_attr_rule__clear(rule); /* reset rule contents */
if (error == GIT_ENOTFOUND) if (error == GIT_ENOTFOUND)
error = GIT_SUCCESS; error = 0;
} else { } else {
rule = NULL; /* vector now "owns" the rule */ rule = NULL; /* vector now "owns" the rule */
} }
...@@ -110,21 +107,20 @@ int git_attr_file__from_buffer( ...@@ -110,21 +107,20 @@ int git_attr_file__from_buffer(
int git_attr_file__from_file( int git_attr_file__from_file(
git_repository *repo, const char *path, git_attr_file *file) git_repository *repo, const char *path, git_attr_file *file)
{ {
int error = GIT_SUCCESS; int error;
git_buf fbuf = GIT_BUF_INIT; git_buf fbuf = GIT_BUF_INIT;
assert(path && file); assert(path && file);
if (file->path == NULL) if (file->path == NULL && git_attr_file__set_path(repo, path, file) < 0)
error = git_attr_file__set_path(repo, path, file); return -1;
if (git_futils_readbuffer(&fbuf, path) < 0)
return -1;
if (error == GIT_SUCCESS &&
(error = git_futils_readbuffer(&fbuf, path)) == GIT_SUCCESS)
error = git_attr_file__from_buffer(repo, fbuf.ptr, file); error = git_attr_file__from_buffer(repo, fbuf.ptr, file);
git_buf_free(&fbuf); git_buf_free(&fbuf);
if (error != GIT_SUCCESS)
git__rethrow(error, "Could not open attribute file '%s'", path);
return error; return error;
} }
...@@ -176,7 +172,6 @@ int git_attr_file__lookup_one( ...@@ -176,7 +172,6 @@ int git_attr_file__lookup_one(
git_attr_file__foreach_matching_rule(file, path, i, rule) { git_attr_file__foreach_matching_rule(file, path, i, rule) {
int pos = git_vector_bsearch(&rule->assigns, &name); int pos = git_vector_bsearch(&rule->assigns, &name);
git_clearerror(); /* okay if search failed */
if (pos >= 0) { if (pos >= 0) {
*value = ((git_attr_assignment *) *value = ((git_attr_assignment *)
...@@ -230,7 +225,6 @@ git_attr_assignment *git_attr_rule__lookup_assignment( ...@@ -230,7 +225,6 @@ git_attr_assignment *git_attr_rule__lookup_assignment(
key.name_hash = git_attr_file__name_hash(name); key.name_hash = git_attr_file__name_hash(name);
pos = git_vector_bsearch(&rule->assigns, &key); pos = git_vector_bsearch(&rule->assigns, &key);
git_clearerror(); /* okay if search failed */
return (pos >= 0) ? git_vector_get(&rule->assigns, pos) : NULL; return (pos >= 0) ? git_vector_get(&rule->assigns, pos) : NULL;
} }
...@@ -248,16 +242,15 @@ int git_attr_path__init( ...@@ -248,16 +242,15 @@ int git_attr_path__init(
if (base != NULL && git_path_root(path) < 0) { if (base != NULL && git_path_root(path) < 0) {
git_buf full_path = GIT_BUF_INIT; git_buf full_path = GIT_BUF_INIT;
int error = git_buf_joinpath(&full_path, base, path); if (git_buf_joinpath(&full_path, base, path) < 0)
if (error == GIT_SUCCESS) return -1;
info->is_dir = (int)git_path_isdir(full_path.ptr); info->is_dir = (int)git_path_isdir(full_path.ptr);
git_buf_free(&full_path); git_buf_free(&full_path);
return error; return 0;
} }
info->is_dir = (int)git_path_isdir(path); info->is_dir = (int)git_path_isdir(path);
return GIT_SUCCESS; return 0;
} }
...@@ -374,7 +367,7 @@ int git_attr_fnmatch__parse( ...@@ -374,7 +367,7 @@ int git_attr_fnmatch__parse(
if (!spec->pattern) { if (!spec->pattern) {
*base = git__next_line(pattern); *base = git__next_line(pattern);
return GIT_ENOMEM; return -1;
} else { } else {
/* strip '\' that might have be used for internal whitespace */ /* strip '\' that might have be used for internal whitespace */
char *to = spec->pattern; char *to = spec->pattern;
...@@ -390,7 +383,7 @@ int git_attr_fnmatch__parse( ...@@ -390,7 +383,7 @@ int git_attr_fnmatch__parse(
} }
} }
return GIT_SUCCESS; return 0;
} }
static int sort_by_hash_and_name(const void *a_raw, const void *b_raw) static int sort_by_hash_and_name(const void *a_raw, const void *b_raw)
...@@ -434,7 +427,7 @@ int git_attr_assignment__parse( ...@@ -434,7 +427,7 @@ int git_attr_assignment__parse(
git_vector *assigns, git_vector *assigns,
const char **base) const char **base)
{ {
int error = GIT_SUCCESS; int error;
const char *scan = *base; const char *scan = *base;
git_attr_assignment *assign = NULL; git_attr_assignment *assign = NULL;
...@@ -442,7 +435,7 @@ int git_attr_assignment__parse( ...@@ -442,7 +435,7 @@ int git_attr_assignment__parse(
assigns->_cmp = sort_by_hash_and_name; assigns->_cmp = sort_by_hash_and_name;
while (*scan && *scan != '\n' && error == GIT_SUCCESS) { while (*scan && *scan != '\n') {
const char *name_start, *value_start; const char *name_start, *value_start;
/* skip leading blanks */ /* skip leading blanks */
...@@ -451,10 +444,7 @@ int git_attr_assignment__parse( ...@@ -451,10 +444,7 @@ int git_attr_assignment__parse(
/* allocate assign if needed */ /* allocate assign if needed */
if (!assign) { if (!assign) {
assign = git__calloc(1, sizeof(git_attr_assignment)); assign = git__calloc(1, sizeof(git_attr_assignment));
if (!assign) { GITERR_CHECK_ALLOC(assign);
error = GIT_ENOMEM;
break;
}
GIT_REFCOUNT_INC(assign); GIT_REFCOUNT_INC(assign);
} }
...@@ -489,10 +479,7 @@ int git_attr_assignment__parse( ...@@ -489,10 +479,7 @@ int git_attr_assignment__parse(
/* allocate permanent storage for name */ /* allocate permanent storage for name */
assign->name = git__strndup(name_start, scan - name_start); assign->name = git__strndup(name_start, scan - name_start);
if (!assign->name) { GITERR_CHECK_ALLOC(assign->name);
error = GIT_ENOMEM;
break;
}
/* if there is an equals sign, find the value */ /* if there is an equals sign, find the value */
if (*scan == '=') { if (*scan == '=') {
...@@ -501,14 +488,10 @@ int git_attr_assignment__parse( ...@@ -501,14 +488,10 @@ int git_attr_assignment__parse(
/* if we found a value, allocate permanent storage for it */ /* if we found a value, allocate permanent storage for it */
if (scan > value_start) { if (scan > value_start) {
assign->value = git__strndup(value_start, scan - value_start); assign->value = git__strndup(value_start, scan - value_start);
if (!assign->value) { GITERR_CHECK_ALLOC(assign->value);
error = GIT_ENOMEM;
break;
} else {
assign->is_allocated = 1; assign->is_allocated = 1;
} }
} }
}
/* expand macros (if given a repo with a macro cache) */ /* expand macros (if given a repo with a macro cache) */
if (repo != NULL && assign->value == git_attr__true) { if (repo != NULL && assign->value == git_attr__true) {
...@@ -524,35 +507,27 @@ int git_attr_assignment__parse( ...@@ -524,35 +507,27 @@ int git_attr_assignment__parse(
error = git_vector_insert_sorted( error = git_vector_insert_sorted(
assigns, massign, &merge_assignments); assigns, massign, &merge_assignments);
if (error < 0 && error != GIT_EEXISTS)
if (error == GIT_EEXISTS) return error;
error = GIT_SUCCESS;
else if (error != GIT_SUCCESS)
break;
} }
} }
} }
/* insert allocated assign into vector */ /* insert allocated assign into vector */
error = git_vector_insert_sorted(assigns, assign, &merge_assignments); error = git_vector_insert_sorted(assigns, assign, &merge_assignments);
if (error == GIT_EEXISTS) if (error < 0 && error != GIT_EEXISTS)
error = GIT_SUCCESS; return error;
else if (error < GIT_SUCCESS)
break;
/* clear assign since it is now "owned" by the vector */ /* clear assign since it is now "owned" by the vector */
assign = NULL; assign = NULL;
} }
if (!assigns->length)
error = git__throw(GIT_ENOTFOUND, "No attribute assignments found for rule");
if (assign != NULL) if (assign != NULL)
git_attr_assignment__free(assign); git_attr_assignment__free(assign);
*base = git__next_line(scan); *base = git__next_line(scan);
return error; return (assigns->length == 0) ? GIT_ENOTFOUND : 0;
} }
static void git_attr_rule__clear(git_attr_rule *rule) static void git_attr_rule__clear(git_attr_rule *rule)
......
...@@ -123,6 +123,8 @@ void giterr_set(int error_class, const char *string, ...) ...@@ -123,6 +123,8 @@ void giterr_set(int error_class, const char *string, ...)
char error_str[1024]; char error_str[1024];
va_list arglist; va_list arglist;
git_error *error; git_error *error;
const char *oserr =
(error_class == GITERR_OS && errno != 0) ? strerror(errno) : NULL;
error = &GIT_GLOBAL->error_t; error = &GIT_GLOBAL->error_t;
free(error->message); free(error->message);
...@@ -131,6 +133,13 @@ void giterr_set(int error_class, const char *string, ...) ...@@ -131,6 +133,13 @@ void giterr_set(int error_class, const char *string, ...)
p_vsnprintf(error_str, sizeof(error_str), string, arglist); p_vsnprintf(error_str, sizeof(error_str), string, arglist);
va_end(arglist); va_end(arglist);
/* automatically suffix strerror(errno) for GITERR_OS errors */
if (oserr != NULL) {
strncat(error_str, ": ", sizeof(error_str));
strncat(error_str, oserr, sizeof(error_str));
errno = 0;
}
error->message = git__strdup(error_str); error->message = git__strdup(error_str);
error->klass = error_class; error->klass = error_class;
......
...@@ -45,8 +45,8 @@ static int lock_file(git_filebuf *file, int flags) ...@@ -45,8 +45,8 @@ static int lock_file(git_filebuf *file, int flags)
source = p_open(file->path_original, O_RDONLY); source = p_open(file->path_original, O_RDONLY);
if (source < 0) { if (source < 0) {
giterr_set(GITERR_OS, giterr_set(GITERR_OS,
"Failed to open file '%s' for reading: %s", "Failed to open file '%s' for reading",
file->path_original, strerror(errno)); file->path_original);
return -1; return -1;
} }
......
...@@ -37,7 +37,7 @@ int git_futils_mktmp(git_buf *path_out, const char *filename) ...@@ -37,7 +37,7 @@ int git_futils_mktmp(git_buf *path_out, const char *filename)
if ((fd = p_mkstemp(path_out->ptr)) < 0) { if ((fd = p_mkstemp(path_out->ptr)) < 0) {
giterr_set(GITERR_OS, giterr_set(GITERR_OS,
"Failed to create temporary file '%s': %s", path_out->ptr, strerror(errno)); "Failed to create temporary file '%s'", path_out->ptr);
return -1; return -1;
} }
...@@ -53,8 +53,7 @@ int git_futils_creat_withpath(const char *path, const mode_t dirmode, const mode ...@@ -53,8 +53,7 @@ int git_futils_creat_withpath(const char *path, const mode_t dirmode, const mode
fd = p_creat(path, mode); fd = p_creat(path, mode);
if (fd < 0) { if (fd < 0) {
giterr_set(GITERR_OS, giterr_set(GITERR_OS, "Failed to create file '%s'", path);
"Failed to create file '%s': %s", path, strerror(errno));
return -1; return -1;
} }
...@@ -65,8 +64,7 @@ int git_futils_creat_locked(const char *path, const mode_t mode) ...@@ -65,8 +64,7 @@ int git_futils_creat_locked(const char *path, const mode_t mode)
{ {
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_EXCL, mode); int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_EXCL, mode);
if (fd < 0) { if (fd < 0) {
giterr_set(GITERR_OS, giterr_set(GITERR_OS, "Failed to create locked file '%s'", path);
"Failed to create locked file '%s': %s", path, strerror(errno));
return -1; return -1;
} }
...@@ -115,10 +113,8 @@ int git_futils_readbuffer_updated(git_buf *buf, const char *path, time_t *mtime, ...@@ -115,10 +113,8 @@ int git_futils_readbuffer_updated(git_buf *buf, const char *path, time_t *mtime,
if (updated != NULL) if (updated != NULL)
*updated = 0; *updated = 0;
if ((fd = p_open(path, O_RDONLY)) < 0) { if ((fd = git_futils_open_ro(path)) < 0)
giterr_set(GITERR_OS, "Failed to read file '%s': %s", path, strerror(errno)); return fd;
return (errno == ENOENT) ? GIT_ENOTFOUND : -1;
}
if (p_fstat(fd, &st) < 0 || S_ISDIR(st.st_mode) || !git__is_sizet(st.st_size+1)) { if (p_fstat(fd, &st) < 0 || S_ISDIR(st.st_mode) || !git__is_sizet(st.st_size+1)) {
close(fd); close(fd);
...@@ -154,8 +150,7 @@ int git_futils_readbuffer_updated(git_buf *buf, const char *path, time_t *mtime, ...@@ -154,8 +150,7 @@ int git_futils_readbuffer_updated(git_buf *buf, const char *path, time_t *mtime,
if (read_size < 0) { if (read_size < 0) {
close(fd); close(fd);
giterr_set(GITERR_OS, "Failed to read descriptor for %s: %s", giterr_set(GITERR_OS, "Failed to read descriptor for '%s'", path);
path, strerror(errno));
return -1; return -1;
} }
......
...@@ -77,6 +77,20 @@ extern int git_futils_mktmp(git_buf *path_out, const char *filename); ...@@ -77,6 +77,20 @@ extern int git_futils_mktmp(git_buf *path_out, const char *filename);
extern int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode); extern int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode);
/** /**
* Open a file readonly and set error if needed
*/
GIT_INLINE(int) git_futils_open_ro(const char *path)
{
int fd = p_open(path, O_RDONLY);
if (fd < 0) {
if (errno == ENOENT)
fd = GIT_ENOTFOUND;
giterr_set(GITERR_OS, "Failed to open '%s'", path);
}
return fd;
}
/**
* Get the filesize in bytes of a file * Get the filesize in bytes of a file
*/ */
extern git_off_t git_futils_filesize(git_file fd); extern git_off_t git_futils_filesize(git_file fd);
......
...@@ -544,7 +544,7 @@ const git_index_entry_unmerged *git_index_get_unmerged_bypath(git_index *index, ...@@ -544,7 +544,7 @@ const git_index_entry_unmerged *git_index_get_unmerged_bypath(git_index *index,
if (!index->unmerged.length) if (!index->unmerged.length)
return NULL; return NULL;
if ((pos = git_vector_bsearch2(&index->unmerged, unmerged_srch, path)) < GIT_SUCCESS) if ((pos = git_vector_bsearch2(&index->unmerged, unmerged_srch, path)) < 0)
return NULL; return NULL;
return git_vector_get(&index->unmerged, pos); return git_vector_get(&index->unmerged, pos);
......
...@@ -499,7 +499,7 @@ int git_path_direach( ...@@ -499,7 +499,7 @@ int git_path_direach(
wd_len = path->size; wd_len = path->size;
dir = opendir(path->ptr); dir = opendir(path->ptr);
if (!dir) { 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; return -1;
} }
......
...@@ -1021,8 +1021,7 @@ static int reference_delete(git_reference *ref) ...@@ -1021,8 +1021,7 @@ static int reference_delete(git_reference *ref)
git_buf_free(&full_path); /* done with path at this point */ git_buf_free(&full_path); /* done with path at this point */
if (result < 0) { if (result < 0) {
giterr_set(GITERR_OS, giterr_set(GITERR_OS, "Failed to unlink '%s'", full_path.ptr);
"Failed to unlink '%s': %s", full_path.ptr, strerror(errno));
return -1; return -1;
} }
......
...@@ -355,23 +355,26 @@ int git__bsearch( ...@@ -355,23 +355,26 @@ int git__bsearch(
int (*compare)(const void *, const void *), int (*compare)(const void *, const void *),
size_t *position) size_t *position)
{ {
int lim, cmp; int lim, cmp = -1;
void **part, **base = array; void **part, **base = array;
for (lim = array_len; lim != 0; lim >>= 1) { for (lim = array_len; lim != 0; lim >>= 1) {
part = base + (lim >> 1); part = base + (lim >> 1);
cmp = (*compare)(key, *part); cmp = (*compare)(key, *part);
if (cmp == 0) { if (cmp == 0) {
*position = (part - array); base = part;
return GIT_SUCCESS; break;
} else if (cmp > 0) { /* key > p; take right partition */ }
if (cmp > 0) { /* key > p; take right partition */
base = part + 1; base = part + 1;
lim--; lim--;
} /* else take left partition */ } /* else take left partition */
} }
if (position)
*position = (base - array); *position = (base - array);
return GIT_ENOTFOUND;
return (cmp == 0) ? 0 : -1;
} }
/** /**
......
...@@ -115,6 +115,11 @@ extern int git__fnmatch(const char *pattern, const char *name, int flags); ...@@ -115,6 +115,11 @@ extern int git__fnmatch(const char *pattern, const char *name, int flags);
extern void git__tsort(void **dst, size_t size, int (*cmp)(const void *, const void *)); extern void git__tsort(void **dst, size_t size, int (*cmp)(const void *, const void *));
/**
* @param position If non-NULL, this will be set to the position where the
* element is or would be inserted if not found.
* @return pos (>=0) if found or -1 if not found
*/
extern int git__bsearch( extern int git__bsearch(
void **array, void **array,
size_t array_len, size_t array_len,
......
...@@ -19,10 +19,9 @@ static int resize_vector(git_vector *v) ...@@ -19,10 +19,9 @@ static int resize_vector(git_vector *v)
v->_alloc_size = minimum_size; v->_alloc_size = minimum_size;
v->contents = git__realloc(v->contents, v->_alloc_size * sizeof(void *)); v->contents = git__realloc(v->contents, v->_alloc_size * sizeof(void *));
if (v->contents == NULL) GITERR_CHECK_ALLOC(v->contents);
return GIT_ENOMEM;
return GIT_SUCCESS; return 0;
} }
void git_vector_free(git_vector *v) void git_vector_free(git_vector *v)
...@@ -52,30 +51,29 @@ int git_vector_init(git_vector *v, unsigned int initial_size, git_vector_cmp cmp ...@@ -52,30 +51,29 @@ int git_vector_init(git_vector *v, unsigned int initial_size, git_vector_cmp cmp
v->sorted = 1; v->sorted = 1;
v->contents = git__malloc(v->_alloc_size * sizeof(void *)); v->contents = git__malloc(v->_alloc_size * sizeof(void *));
if (v->contents == NULL) GITERR_CHECK_ALLOC(v->contents);
return GIT_ENOMEM;
return GIT_SUCCESS; return 0;
} }
int git_vector_insert(git_vector *v, void *element) int git_vector_insert(git_vector *v, void *element)
{ {
assert(v); assert(v);
if (v->length >= v->_alloc_size) { if (v->length >= v->_alloc_size &&
if (resize_vector(v) < 0) resize_vector(v) < 0)
return GIT_ENOMEM; return -1;
}
v->contents[v->length++] = element; v->contents[v->length++] = element;
v->sorted = 0; v->sorted = 0;
return GIT_SUCCESS; return 0;
} }
int git_vector_insert_sorted(git_vector *v, void *element, int (*on_dup)(void **old, void *new)) int git_vector_insert_sorted(
git_vector *v, void *element, int (*on_dup)(void **old, void *new))
{ {
int error = GIT_SUCCESS; int result;
size_t pos; size_t pos;
assert(v && v->_cmp); assert(v && v->_cmp);
...@@ -83,22 +81,18 @@ int git_vector_insert_sorted(git_vector *v, void *element, int (*on_dup)(void ** ...@@ -83,22 +81,18 @@ int git_vector_insert_sorted(git_vector *v, void *element, int (*on_dup)(void **
if (!v->sorted) if (!v->sorted)
git_vector_sort(v); git_vector_sort(v);
if (v->length >= v->_alloc_size) { if (v->length >= v->_alloc_size &&
if (resize_vector(v) < 0) resize_vector(v) < 0)
return GIT_ENOMEM; return -1;
}
error = git__bsearch(v->contents, v->length, element, v->_cmp, &pos);
/* If we found the element and have a duplicate handler callback, /* If we find the element and have a duplicate handler callback,
* invoke it. If it returns an error, then cancel insert, otherwise * invoke it. If it returns non-zero, then cancel insert, otherwise
* proceed with normal insert. * proceed with normal insert.
*/ */
if (error == GIT_SUCCESS && on_dup != NULL) { if (git__bsearch(v->contents, v->length, element, v->_cmp, &pos) >= 0 &&
error = on_dup(&v->contents[pos], element); on_dup != NULL &&
if (error != GIT_SUCCESS) (result = on_dup(&v->contents[pos], element)) < 0)
return error; return result;
}
/* shift elements to the right */ /* shift elements to the right */
if (pos < v->length) { if (pos < v->length) {
...@@ -108,8 +102,7 @@ int git_vector_insert_sorted(git_vector *v, void *element, int (*on_dup)(void ** ...@@ -108,8 +102,7 @@ int git_vector_insert_sorted(git_vector *v, void *element, int (*on_dup)(void **
v->contents[pos] = element; v->contents[pos] = element;
v->length++; v->length++;
return 0;
return GIT_SUCCESS;
} }
void git_vector_sort(git_vector *v) void git_vector_sort(git_vector *v)
...@@ -130,16 +123,14 @@ int git_vector_bsearch2(git_vector *v, git_vector_cmp key_lookup, const void *ke ...@@ -130,16 +123,14 @@ int git_vector_bsearch2(git_vector *v, git_vector_cmp key_lookup, const void *ke
assert(v && key && key_lookup); assert(v && key && key_lookup);
/* need comparison function to sort the vector */ /* need comparison function to sort the vector */
if (v->_cmp == NULL) assert(v->_cmp != NULL);
return git__throw(GIT_ENOTFOUND, "Can't sort vector. No comparison function set");
git_vector_sort(v); git_vector_sort(v);
if (git__bsearch(v->contents, v->length, key, key_lookup, if (git__bsearch(v->contents, v->length, key, key_lookup, &pos) >= 0)
&pos) == GIT_SUCCESS)
return (int)pos; return (int)pos;
return git__throw(GIT_ENOTFOUND, "Can't find element"); return GIT_ENOTFOUND;
} }
int git_vector_search2(git_vector *v, git_vector_cmp key_lookup, const void *key) int git_vector_search2(git_vector *v, git_vector_cmp key_lookup, const void *key)
...@@ -153,7 +144,7 @@ int git_vector_search2(git_vector *v, git_vector_cmp key_lookup, const void *key ...@@ -153,7 +144,7 @@ int git_vector_search2(git_vector *v, git_vector_cmp key_lookup, const void *key
return i; return i;
} }
return git__throw(GIT_ENOTFOUND, "Can't find element"); return GIT_ENOTFOUND;
} }
static int strict_comparison(const void *a, const void *b) static int strict_comparison(const void *a, const void *b)
...@@ -178,13 +169,13 @@ int git_vector_remove(git_vector *v, unsigned int idx) ...@@ -178,13 +169,13 @@ int git_vector_remove(git_vector *v, unsigned int idx)
assert(v); assert(v);
if (idx >= v->length || v->length == 0) if (idx >= v->length || v->length == 0)
return git__throw(GIT_ENOTFOUND, "Can't remove element. Index out of bounds"); return GIT_ENOTFOUND;
for (i = idx; i < v->length - 1; ++i) for (i = idx; i < v->length - 1; ++i)
v->contents[i] = v->contents[i + 1]; v->contents[i] = v->contents[i + 1];
v->length--; v->length--;
return GIT_SUCCESS; return 0;
} }
void git_vector_pop(git_vector *v) void git_vector_pop(git_vector *v)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment