summaryrefslogtreecommitdiff
path: root/src/blob.c
diff options
context:
space:
mode:
authorRussell Belfer <arrbee@arrbee.com>2012-03-14 17:36:15 -0700
committerRussell Belfer <arrbee@arrbee.com>2012-03-14 17:36:15 -0700
commitdeafee7bd7a9e2efcdff90627b6094d8c1519319 (patch)
tree4b11910d7d315a6db667cc4af4c6749630612ed3 /src/blob.c
parentab43ad2fd822504446e7876d6352c968a74beb53 (diff)
downloadlibgit2-deafee7bd7a9e2efcdff90627b6094d8c1519319.tar.gz
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.
Diffstat (limited to 'src/blob.c')
-rw-r--r--src/blob.c110
1 files changed, 47 insertions, 63 deletions
diff --git a/src/blob.c b/src/blob.c
index 60a6b55d6..20dcece74 100644
--- a/src/blob.c
+++ b/src/blob.c
@@ -42,7 +42,7 @@ int git_blob__parse(git_blob *blob, git_odb_object *odb_obj)
assert(blob);
git_cached_obj_incref((git_cached_obj *)odb_obj);
blob->odb_object = odb_obj;
- return GIT_SUCCESS;
+ return 0;
}
int git_blob_create_frombuffer(git_oid *oid, git_repository *repo, const void *buffer, size_t len)
@@ -51,58 +51,50 @@ int git_blob_create_frombuffer(git_oid *oid, git_repository *repo, const void *b
git_odb *odb;
git_odb_stream *stream;
- error = git_repository_odb__weakptr(&odb, repo);
- if (error < GIT_SUCCESS)
+ if ((error = git_repository_odb__weakptr(&odb, repo)) < 0 ||
+ (error = git_odb_open_wstream(&stream, odb, len, GIT_OBJ_BLOB)) < 0)
return error;
- if ((error = git_odb_open_wstream(&stream, odb, len, GIT_OBJ_BLOB)) < GIT_SUCCESS)
- return git__rethrow(error, "Failed to create blob");
+ if ((error = stream->write(stream, buffer, len)) == 0)
+ error = stream->finalize_write(oid, stream);
- if ((error = stream->write(stream, buffer, len)) < GIT_SUCCESS) {
- stream->free(stream);
- return error;
- }
-
- error = stream->finalize_write(oid, stream);
stream->free(stream);
-
- if (error < GIT_SUCCESS)
- return git__rethrow(error, "Failed to create blob");
-
- return GIT_SUCCESS;
+ return error;
}
-static int write_file_stream(git_oid *oid, git_odb *odb, const char *path, git_off_t file_size)
+static int write_file_stream(
+ git_oid *oid, git_odb *odb, const char *path, git_off_t file_size)
{
int fd, error;
char buffer[4096];
git_odb_stream *stream = NULL;
- if ((error = git_odb_open_wstream(&stream, odb, (size_t)file_size, GIT_OBJ_BLOB)) < GIT_SUCCESS)
+ if ((error = git_odb_open_wstream(
+ &stream, odb, (size_t)file_size, GIT_OBJ_BLOB)) < 0)
return error;
- if ((fd = p_open(path, O_RDONLY)) < 0) {
- error = git__throw(GIT_ENOTFOUND, "Failed to create blob. Could not open '%s'", path);
- goto cleanup;
+ if ((fd = git_futils_open_ro(path)) < 0) {
+ stream->free(stream);
+ return -1;
}
- while (file_size > 0) {
+ while (!error && file_size > 0) {
ssize_t read_len = p_read(fd, buffer, sizeof(buffer));
if (read_len < 0) {
- error = git__throw(GIT_EOSERR, "Failed to create blob. Can't read full file");
- p_close(fd);
- goto cleanup;
+ giterr_set(
+ GITERR_OS, "Failed to create blob. Can't read whole file");
+ error = -1;
}
-
- stream->write(stream, buffer, read_len);
- file_size -= read_len;
+ else if (!(error = stream->write(stream, buffer, read_len)))
+ file_size -= read_len;
}
p_close(fd);
- error = stream->finalize_write(oid, stream);
-cleanup:
+ if (!error)
+ error = stream->finalize_write(oid, stream);
+
stream->free(stream);
return error;
}
@@ -117,8 +109,7 @@ static int write_file_filtered(
git_buf source = GIT_BUF_INIT;
git_buf dest = GIT_BUF_INIT;
- error = git_futils_readbuffer(&source, full_path);
- if (error < GIT_SUCCESS)
+ if ((error = git_futils_readbuffer(&source, full_path)) < 0)
return error;
error = git_filters_apply(&dest, &source, filters);
@@ -127,30 +118,29 @@ static int write_file_filtered(
* and we don't want to ODB write to choke */
git_buf_free(&source);
- if (error == GIT_SUCCESS) {
- /* Write the file to disk if it was properly filtered */
+ /* Write the file to disk if it was properly filtered */
+ if (!error)
error = git_odb_write(oid, odb, dest.ptr, dest.size, GIT_OBJ_BLOB);
- }
git_buf_free(&dest);
- return GIT_SUCCESS;
+ return error;
}
-static int write_symlink(git_oid *oid, git_odb *odb, const char *path, size_t link_size)
+static int write_symlink(
+ git_oid *oid, git_odb *odb, const char *path, size_t link_size)
{
char *link_data;
ssize_t read_len;
int error;
link_data = git__malloc(link_size);
- if (!link_data)
- return GIT_ENOMEM;
+ GITERR_CHECK_ALLOC(link_data);
read_len = p_readlink(path, link_data, link_size);
-
if (read_len != (ssize_t)link_size) {
+ giterr_set(GITERR_OS, "Failed to create blob. Can't read symlink '%s'", path);
free(link_data);
- return git__throw(GIT_EOSERR, "Failed to create blob. Can't read symlink");
+ return -1;
}
error = git_odb_write(oid, odb, (void *)link_data, link_size, GIT_OBJ_BLOB);
@@ -168,25 +158,18 @@ int git_blob_create_fromfile(git_oid *oid, git_repository *repo, const char *pat
git_odb *odb = NULL;
workdir = git_repository_workdir(repo);
- if (workdir == NULL)
- return git__throw(GIT_ENOTFOUND, "Failed to create blob. (No working directory found)");
+ assert(workdir); /* error to call this on bare repo */
- error = git_buf_joinpath(&full_path, workdir, path);
- if (error < GIT_SUCCESS)
+ if ((error = git_buf_joinpath(&full_path, workdir, path)) < 0 ||
+ (error = git_path_lstat(full_path.ptr, &st)) < 0 ||
+ (error = git_repository_odb__weakptr(&odb, repo)) < 0)
+ {
+ git_buf_free(&full_path);
return error;
-
- error = p_lstat(full_path.ptr, &st);
- if (error < 0) {
- error = git__throw(GIT_EOSERR, "Failed to stat blob. %s", strerror(errno));
- goto cleanup;
}
size = st.st_size;
- error = git_repository_odb__weakptr(&odb, repo);
- if (error < GIT_SUCCESS)
- goto cleanup;
-
if (S_ISLNK(st.st_mode)) {
error = write_symlink(oid, odb, full_path.ptr, (size_t)size);
} else {
@@ -194,12 +177,12 @@ int git_blob_create_fromfile(git_oid *oid, git_repository *repo, const char *pat
int filter_count;
/* Load the filters for writing this file to the ODB */
- filter_count = git_filters_load(&write_filters, repo, path, GIT_FILTER_TO_ODB);
+ filter_count = git_filters_load(
+ &write_filters, repo, path, GIT_FILTER_TO_ODB);
if (filter_count < 0) {
/* Negative value means there was a critical error */
error = filter_count;
- goto cleanup;
} else if (filter_count == 0) {
/* No filters need to be applied to the document: we can stream
* directly from disk */
@@ -212,19 +195,20 @@ int git_blob_create_fromfile(git_oid *oid, git_repository *repo, const char *pat
git_filters_free(&write_filters);
/*
- * TODO: eventually support streaming filtered files, for files which are bigger
- * than a given threshold. This is not a priority because applying a filter in
- * streaming mode changes the final size of the blob, and without knowing its
- * final size, the blob cannot be written in stream mode to the ODB.
+ * TODO: eventually support streaming filtered files, for files
+ * which are bigger than a given threshold. This is not a priority
+ * because applying a filter in streaming mode changes the final
+ * size of the blob, and without knowing its final size, the blob
+ * cannot be written in stream mode to the ODB.
*
- * The plan is to do streaming writes to a tempfile on disk and then opening
- * streaming that file to the ODB, using `write_file_stream`.
+ * The plan is to do streaming writes to a tempfile on disk and then
+ * opening streaming that file to the ODB, using
+ * `write_file_stream`.
*
* CAREFULLY DESIGNED APIS YO
*/
}
-cleanup:
git_buf_free(&full_path);
return error;
}