diff options
| author | Vicent Marti <vicent@github.com> | 2013-12-13 06:20:19 -0800 |
|---|---|---|
| committer | Vicent Marti <vicent@github.com> | 2013-12-13 06:20:19 -0800 |
| commit | 79194bcdc956406979cd27ac99198826860d3f20 (patch) | |
| tree | 407594b97b6e39ef3ac0723dc33aa3162ce933cc /src/blob.c | |
| parent | 25a1fab0a96fd87e4ebc4ec195ac59a4213e92ad (diff) | |
| parent | 7a16d54b5457aa9f60c25a204277ae0ce609ad2e (diff) | |
| download | libgit2-79194bcdc956406979cd27ac99198826860d3f20.tar.gz | |
Merge pull request #1986 from libgit2/rb/error-handling-cleanups
Clean up some error handling and change callback error behavior
Diffstat (limited to 'src/blob.c')
| -rw-r--r-- | src/blob.c | 34 |
1 files changed, 21 insertions, 13 deletions
diff --git a/src/blob.c b/src/blob.c index 2c6d52800..ab344ae98 100644 --- a/src/blob.c +++ b/src/blob.c @@ -272,37 +272,44 @@ int git_blob_create_fromchunks( int (*source_cb)(char *content, size_t max_length, void *payload), void *payload) { - int error = -1, read_bytes; + int error; char *content = NULL; git_filebuf file = GIT_FILEBUF_INIT; git_buf path = GIT_BUF_INIT; - if (git_buf_joinpath( - &path, git_repository_path(repo), GIT_OBJECTS_DIR "streamed") < 0) + assert(oid && repo && source_cb); + + if ((error = git_buf_joinpath( + &path, git_repository_path(repo), GIT_OBJECTS_DIR "streamed")) < 0) goto cleanup; content = git__malloc(BUFFER_SIZE); GITERR_CHECK_ALLOC(content); - if (git_filebuf_open(&file, git_buf_cstr(&path), GIT_FILEBUF_TEMPORARY, 0666) < 0) + if ((error = git_filebuf_open( + &file, git_buf_cstr(&path), GIT_FILEBUF_TEMPORARY, 0666)) < 0) goto cleanup; while (1) { - read_bytes = source_cb(content, BUFFER_SIZE, payload); - - assert(read_bytes <= BUFFER_SIZE); + int read_bytes = source_cb(content, BUFFER_SIZE, payload); - if (read_bytes <= 0) + if (!read_bytes) break; - if (git_filebuf_write(&file, content, read_bytes) < 0) + if (read_bytes > BUFFER_SIZE) { + giterr_set(GITERR_OBJECT, "Invalid chunk size while creating blob"); + error = GIT_EBUFS; + } else if (read_bytes < 0) { + error = giterr_set_after_callback(read_bytes); + } else { + error = git_filebuf_write(&file, content, read_bytes); + } + + if (error < 0) goto cleanup; } - if (read_bytes < 0) - goto cleanup; - - if (git_filebuf_flush(&file) < 0) + if ((error = git_filebuf_flush(&file)) < 0) goto cleanup; error = git_blob__create_from_paths( @@ -312,6 +319,7 @@ cleanup: git_buf_free(&path); git_filebuf_cleanup(&file); git__free(content); + return error; } |
