git
[Ported from github.com/git/git] Sync with Git 2.56.1
Brought by drakestapleton
| 1 | /* |
| 2 | * Copyright (c) 2011, Google Inc. |
| 3 | */ |
| 4 | #ifndef STREAMING_H |
| 5 | #define STREAMING_H 1 |
| 6 | |
| 7 | #include "object.h" |
| 8 | #include "odb.h" |
| 9 | |
| 10 | struct object_database; |
| 11 | struct odb_stream; |
| 12 | struct stream_filter; |
| 13 | |
| 14 | typedef int (*odb_stream_close_fn)(struct odb_stream *); |
| 15 | typedef ssize_t (*odb_stream_read_fn)(struct odb_stream *, char *, size_t); |
| 16 | |
| 17 | /* |
| 18 | * A stream that can be used to read an object from or write an object into the |
| 19 | * object database without loading all of it into memory. |
| 20 | */ |
| 21 | struct odb_stream { |
| 22 | odb_stream_close_fn close; |
| 23 | odb_stream_read_fn read; |
| 24 | enum object_type type; |
| 25 | size_t size; /* inflated size of full object */ |
| 26 | }; |
| 27 | |
| 28 | /* |
| 29 | * Create a new object stream for the given object. An optional filter can be |
| 30 | * used to transform the object's content. |
| 31 | * |
| 32 | * Returns the stream on success, a `NULL` pointer otherwise. |
| 33 | */ |
| 34 | struct odb_stream *odb_stream_from_object(struct object_database *odb, |
| 35 | const struct object_id *oid, |
| 36 | struct stream_filter *filter); |
| 37 | |
| 38 | /* |
| 39 | * Create a new object stream for the given file descriptor. This can be used |
| 40 | * to, for example, stream an object into the object database. This function |
| 41 | * does _not_ take ownership of the file descriptor. It's the responsibility of |
| 42 | * the caller to close it after the stream has been closed. |
| 43 | */ |
| 44 | struct odb_stream *odb_stream_from_fd(int fd, size_t size, enum object_type type); |
| 45 | |
| 46 | /* |
| 47 | * Close the given object stream and release all resources associated with it. |
| 48 | * Returns 0 on success, a negative error code otherwise. |
| 49 | */ |
| 50 | int odb_stream_close(struct odb_stream *stream); |
| 51 | |
| 52 | /* |
| 53 | * Read data from the stream into the buffer. Returns 0 on EOF and the number |
| 54 | * of bytes read on success. Returns a negative error code in case reading from |
| 55 | * the stream fails. |
| 56 | */ |
| 57 | ssize_t odb_stream_read(struct odb_stream *stream, void *buf, size_t len); |
| 58 | |
| 59 | /* |
| 60 | * Look up the object by its ID and write the full contents to the file |
| 61 | * descriptor. The object must be a blob, or the function will fail. When |
| 62 | * provided, the filter is used to transform the blob contents. |
| 63 | * |
| 64 | * `can_seek` should be set to 1 in case the given file descriptor can be |
| 65 | * seek(3p)'d on. This is used to support files with holes in case a |
| 66 | * significant portion of the blob contains NUL bytes. |
| 67 | * |
| 68 | * Returns a negative error code on failure, 0 on success. |
| 69 | */ |
| 70 | int odb_stream_blob_to_fd(struct object_database *odb, |
| 71 | int fd, |
| 72 | const struct object_id *oid, |
| 73 | struct stream_filter *filter, |
| 74 | int can_seek); |
| 75 | |
| 76 | #endif /* STREAMING_H */ |