]> git.proxmox.com Git - libgit2.git/commitdiff
odb: wrap the stream reading and writing functions
authorCarlos Martín Nieto <cmn@dwim.me>
Thu, 15 Aug 2013 11:48:35 +0000 (13:48 +0200)
committerCarlos Martín Nieto <cmn@dwim.me>
Thu, 15 Aug 2013 12:29:27 +0000 (14:29 +0200)
This is in preparation for moving the hashing to the frontend, which
requires us to handle the incoming data before passing it to the
backend's stream.

include/git2/odb.h
src/blob.c
src/odb.c
src/tag.c
src/transports/local.c
tests-clar/object/raw/write.c

index b3e9a57a6f0e7adcd3d2a9aa9b66f3d1856574ad..3e93a932ca1f9c323590148f9d5823d4b007650b 100644 (file)
@@ -219,16 +219,9 @@ GIT_EXTERN(int) git_odb_write(git_oid *out, git_odb *odb, const void *data, size
  * The type and final length of the object must be specified
  * when opening the stream.
  *
- * The returned stream will be of type `GIT_STREAM_WRONLY` and
- * will have the following methods:
- *
- *             - stream->write: write `n` bytes into the stream
- *             - stream->finalize_write: close the stream and store the object in
- *                     the odb
- *             - stream->free: free the stream
- *
- * The streaming write won't be effective until `stream->finalize_write`
- * is called and returns without an error
+ * The returned stream will be of type `GIT_STREAM_WRONLY`, and it
+ * won't be effective until `git_odb_stream_finalize_write` is called
+ * and returns without an error
  *
  * The stream must always be free'd or will leak memory.
  *
@@ -242,6 +235,42 @@ GIT_EXTERN(int) git_odb_write(git_oid *out, git_odb *odb, const void *data, size
  */
 GIT_EXTERN(int) git_odb_open_wstream(git_odb_stream **out, git_odb *db, size_t size, git_otype type);
 
+/**
+ * Write to an odb stream
+ *
+ * @param stream the stream
+ * @param buffer the data to write
+ * @param len the buffer's length
+ * @return 0 if the write succeeded; error code otherwise
+ */
+GIT_EXTERN(int) git_odb_stream_write(git_odb_stream *stream, const char *buffer, size_t len);
+
+/**
+ * Finish writing to an odb stream
+ *
+ * The object will take its final name and will be available to the
+ * odb.
+ *
+ * @param out pointer to store the resulting object's id
+ * @param stream the stream
+ * @return 0 on success; an error code otherwise
+ */
+GIT_EXTERN(int) git_odb_stream_finalize_write(git_oid *out, git_odb_stream *stream);
+
+/**
+ * Read from an odb stream
+ *
+ * Most backends don't implement streaming reads
+ */
+GIT_EXTERN(int) git_odb_stream_read(git_odb_stream *stream, char *buffer, size_t len);
+
+/**
+ * Free an odb stream
+ *
+ * @param stream the stream to free
+ */
+GIT_EXTERN(void) git_odb_stream_free(git_odb_stream *stream);
+
 /**
  * Open a stream to read an object from the ODB
  *
index 5bb51f7cf7b300a063062dd7b84d6983054085da..6a289f43b4fc2b6715559c34e4eecb938a38e385 100644 (file)
@@ -60,10 +60,10 @@ int git_blob_create_frombuffer(git_oid *oid, git_repository *repo, const void *b
                (error = git_odb_open_wstream(&stream, odb, len, GIT_OBJ_BLOB)) < 0)
                return error;
 
-       if ((error = stream->write(stream, buffer, len)) == 0)
-               error = stream->finalize_write(oid, stream);
+       if ((error = git_odb_stream_write(stream, buffer, len)) == 0)
+               error = git_odb_stream_finalize_write(oid, stream);
 
-       stream->free(stream);
+       git_odb_stream_free(stream);
        return error;
 }
 
@@ -80,12 +80,12 @@ static int write_file_stream(
                return error;
 
        if ((fd = git_futils_open_ro(path)) < 0) {
-               stream->free(stream);
+               git_odb_stream_free(stream);
                return -1;
        }
 
        while (!error && (read_len = p_read(fd, buffer, sizeof(buffer))) > 0) {
-               error = stream->write(stream, buffer, read_len);
+               error = git_odb_stream_write(stream, buffer, read_len);
                written += read_len;
        }
 
@@ -97,9 +97,9 @@ static int write_file_stream(
        }
 
        if (!error)
-               error = stream->finalize_write(oid, stream);
+               error = git_odb_stream_finalize_write(oid, stream);
 
-       stream->free(stream);
+       git_odb_stream_free(stream);
        return error;
 }
 
index 6969cf7720360604ab4a7de3cb70bdf901157b08..1581596620bb60d4ded97f49db1d5bbf527efe16 100644 (file)
--- a/src/odb.c
+++ b/src/odb.c
@@ -864,9 +864,9 @@ int git_odb_write(
        if ((error = git_odb_open_wstream(&stream, db, len, type)) != 0)
                return error;
 
-       stream->write(stream, data, len);
-       error = stream->finalize_write(oid, stream);
-       stream->free(stream);
+       git_odb_stream_write(stream, data, len);
+       error = git_odb_stream_finalize_write(oid, stream);
+       git_odb_stream_free(stream);
 
        return error;
 }
@@ -904,6 +904,26 @@ int git_odb_open_wstream(
        return error;
 }
 
+int git_odb_stream_write(git_odb_stream *stream, const char *buffer, size_t len)
+{
+       return stream->write(stream, buffer, len);
+}
+
+int git_odb_stream_finalize_write(git_oid *out, git_odb_stream *stream)
+{
+       return stream->finalize_write(out, stream);
+}
+
+int git_odb_stream_read(git_odb_stream *stream, char *buffer, size_t len)
+{
+       return stream->read(stream, buffer, len);
+}
+
+void git_odb_stream_free(git_odb_stream *stream)
+{
+       stream->free(stream);
+}
+
 int git_odb_open_rstream(git_odb_stream **stream, git_odb *db, const git_oid *oid)
 {
        size_t i, reads = 0;
index 71f4c1eb1f0f8bf3c3d6f04d4c71e9693ad4b37c..31a3c8b80fd53cf487bd28d9f6fba2d0b0ff8519 100644 (file)
--- a/src/tag.c
+++ b/src/tag.c
@@ -366,10 +366,10 @@ int git_tag_create_frombuffer(git_oid *oid, git_repository *repo, const char *bu
        if (git_odb_open_wstream(&stream, odb, strlen(buffer), GIT_OBJ_TAG) < 0)
                return -1;
 
-       stream->write(stream, buffer, strlen(buffer));
+       git_odb_stream_write(stream, buffer, strlen(buffer));
 
-       error = stream->finalize_write(oid, stream);
-       stream->free(stream);
+       error = git_odb_stream_finalize_write(oid, stream);
+       git_odb_stream_free(stream);
 
        if (error < 0) {
                git_buf_free(&ref_name);
index a9da8146c9dd66d7acf3c63d2dd44afe78405804..8a75de727a17c88f28bfa20aadd76983fe11b41c 100644 (file)
@@ -287,9 +287,9 @@ static int local_push_copy_object(
                odb_obj_size, odb_obj_type)) < 0)
                goto on_error;
 
-       if (odb_stream->write(odb_stream, (char *)git_odb_object_data(odb_obj),
+       if (git_odb_stream_write(odb_stream, (char *)git_odb_object_data(odb_obj),
                odb_obj_size) < 0 ||
-               odb_stream->finalize_write(&remote_odb_obj_oid, odb_stream) < 0) {
+               git_odb_stream_finalize_write(&remote_odb_obj_oid, odb_stream) < 0) {
                error = -1;
        } else if (git_oid__cmp(&obj->id, &remote_odb_obj_oid) != 0) {
                giterr_set(GITERR_ODB, "Error when writing object to remote odb "
@@ -298,7 +298,7 @@ static int local_push_copy_object(
                error = -1;
        }
 
-       odb_stream->free(odb_stream);
+       git_odb_stream_free(odb_stream);
 
 on_error:
        git_odb_object_free(odb_obj);
index 9709c0302cd57f17f89cbb73a39f28208dbf87c2..273f08f2c3608b3c02ba97acf71b1d4aca979b6d 100644 (file)
@@ -31,9 +31,9 @@ static void streaming_write(git_oid *oid, git_odb *odb, git_rawobj *raw)
    int error;
 
    cl_git_pass(git_odb_open_wstream(&stream, odb, raw->len, raw->type));
-   stream->write(stream, raw->data, raw->len);
-   error = stream->finalize_write(oid, stream);
-   stream->free(stream);
+   git_odb_stream_write(stream, raw->data, raw->len);
+   error = git_odb_stream_finalize_write(oid, stream);
+   git_odb_stream_free(stream);
    cl_git_pass(error);
 }