diff options
Diffstat (limited to 'src/blob.c')
-rw-r--r-- | src/blob.c | 51 |
1 files changed, 29 insertions, 22 deletions
diff --git a/src/blob.c b/src/blob.c index 87f5686af..7497ba7bf 100644 --- a/src/blob.c +++ b/src/blob.c @@ -67,12 +67,13 @@ int git_blob_create_frombuffer(git_oid *oid, git_repository *repo, const void *b int git_blob_create_fromfile(git_oid *oid, git_repository *repo, const char *path) { - int error, islnk; + int error = GIT_SUCCESS; + int islnk = 0; int fd = 0; - char full_path[GIT_PATH_MAX]; + git_buf full_path = GIT_BUF_INIT; char buffer[2048]; git_off_t size; - git_odb_stream *stream; + git_odb_stream *stream = NULL; struct stat st; const char *workdir; git_odb *odb; @@ -81,11 +82,14 @@ int git_blob_create_fromfile(git_oid *oid, git_repository *repo, const char *pat if (workdir == NULL) return git__throw(GIT_ENOTFOUND, "Failed to create blob. (No working directory found)"); - git_path_join(full_path, workdir, path); + error = git_buf_joinpath(&full_path, workdir, path); + if (error < GIT_SUCCESS) + return error; - error = p_lstat(full_path, &st); + error = p_lstat(full_path.ptr, &st); if (error < 0) { - return git__throw(GIT_EOSERR, "Failed to stat blob. %s", strerror(errno)); + error = git__throw(GIT_EOSERR, "Failed to stat blob. %s", strerror(errno)); + goto cleanup; } islnk = S_ISLNK(st.st_mode); @@ -93,18 +97,18 @@ int git_blob_create_fromfile(git_oid *oid, git_repository *repo, const char *pat error = git_repository_odb__weakptr(&odb, repo); if (error < GIT_SUCCESS) - return error; + goto cleanup; if (!islnk) { - if ((fd = p_open(full_path, O_RDONLY)) < 0) - return git__throw(GIT_ENOTFOUND, "Failed to create blob. Could not open '%s'", full_path); + if ((fd = p_open(full_path.ptr, O_RDONLY)) < 0) { + error = git__throw(GIT_ENOTFOUND, "Failed to create blob. Could not open '%s'", full_path.ptr +); + goto cleanup; + } } - if ((error = git_odb_open_wstream(&stream, odb, (size_t)size, GIT_OBJ_BLOB)) < GIT_SUCCESS) { - if (!islnk) - p_close(fd); - return git__rethrow(error, "Failed to create blob"); - } + if ((error = git_odb_open_wstream(&stream, odb, (size_t)size, GIT_OBJ_BLOB)) < GIT_SUCCESS) + goto cleanup; while (size > 0) { ssize_t read_len; @@ -112,13 +116,11 @@ int git_blob_create_fromfile(git_oid *oid, git_repository *repo, const char *pat if (!islnk) read_len = p_read(fd, buffer, sizeof(buffer)); else - read_len = p_readlink(full_path, buffer, sizeof(buffer)); + read_len = p_readlink(full_path.ptr, buffer, sizeof(buffer)); if (read_len < 0) { - if (!islnk) - p_close(fd); - stream->free(stream); - return git__throw(GIT_EOSERR, "Failed to create blob. Can't read full file"); + error = git__throw(GIT_EOSERR, "Failed to create blob. Can't read full file"); + goto cleanup; } stream->write(stream, buffer, read_len); @@ -126,10 +128,15 @@ int git_blob_create_fromfile(git_oid *oid, git_repository *repo, const char *pat } error = stream->finalize_write(oid, stream); - stream->free(stream); - if (!islnk) + +cleanup: + if (stream) + stream->free(stream); + if (!islnk && fd) p_close(fd); + git_buf_free(&full_path); - return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to create blob"); + return error == GIT_SUCCESS ? GIT_SUCCESS : + git__rethrow(error, "Failed to create blob"); } |