Raw
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_read_stream;
12 struct stream_filter;
13
14 typedef int (*odb_read_stream_close_fn)(struct odb_read_stream *);
15 typedef ssize_t (*odb_read_stream_read_fn)(struct odb_read_stream *, char *, size_t);
16
17 /*
18 * A stream that can be used to read an object from the object database without
19 * loading all of it into memory.
20 */
21 struct odb_read_stream {
22 odb_read_stream_close_fn close;
23 odb_read_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 database. An optional filter
30 * can be used to transform the object's content.
31 *
32 * Returns the stream on success, a `NULL` pointer otherwise.
33 */
34 struct odb_read_stream *odb_read_stream_open(struct object_database *odb,
35 const struct object_id *oid,
36 struct stream_filter *filter);
37
38 /*
39 * Close the given read stream and release all resources associated with it.
40 * Returns 0 on success, a negative error code otherwise.
41 */
42 int odb_read_stream_close(struct odb_read_stream *stream);
43
44 /*
45 * Read data from the stream into the buffer. Returns 0 on EOF and the number
46 * of bytes read on success. Returns a negative error code in case reading from
47 * the stream fails.
48 */
49 ssize_t odb_read_stream_read(struct odb_read_stream *stream, void *buf, size_t len);
50
51 /*
52 * A stream that provides an object to be written to the object database without
53 * loading all of it into memory.
54 */
55 struct odb_write_stream {
56 ssize_t (*read)(struct odb_write_stream *, unsigned char *, size_t);
57 void *data;
58 int is_finished;
59 };
60
61 /*
62 * Read data from the stream into the buffer. Returns 0 when finished and the
63 * number of bytes read on success. Returns a negative error code in case
64 * reading from the stream fails.
65 */
66 ssize_t odb_write_stream_read(struct odb_write_stream *stream, void *buf,
67 size_t len);
68
69 /*
70 * Releases memory allocated for underlying stream data.
71 */
72 void odb_write_stream_release(struct odb_write_stream *stream);
73
74 /*
75 * Look up the object by its ID and write the full contents to the file
76 * descriptor. The object must be a blob, or the function will fail. When
77 * provided, the filter is used to transform the blob contents.
78 *
79 * `can_seek` should be set to 1 in case the given file descriptor can be
80 * seek(3p)'d on. This is used to support files with holes in case a
81 * significant portion of the blob contains NUL bytes.
82 *
83 * Returns a negative error code on failure, 0 on success.
84 */
85 int odb_stream_blob_to_fd(struct object_database *odb,
86 int fd,
87 const struct object_id *oid,
88 struct stream_filter *filter,
89 int can_seek);
90
91 /*
92 * Sets up an ODB write stream that reads from an fd.
93 */
94 void odb_write_stream_from_fd(struct odb_write_stream *stream, int fd,
95 size_t size);
96
97 #endif /* STREAMING_H */