Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 struct got_object_id;
19 struct got_blob_object;
21 struct got_tree_entry {
22 SIMPLEQ_ENTRY(got_tree_entry) entry;
23 mode_t mode;
24 char *name;
25 struct got_object_id *id;
26 };
28 struct got_tree_object {
29 int nentries;
30 SIMPLEQ_HEAD(, got_tree_entry) entries;
31 };
33 struct got_object_qid {
34 SIMPLEQ_ENTRY(got_object_qid) entry;
35 struct got_object_id *id;
36 };
38 SIMPLEQ_HEAD(got_object_id_queue, got_object_qid);
40 struct got_commit_object {
41 struct got_object_id *tree_id;
42 unsigned int nparents;
43 struct got_object_id_queue parent_ids;
44 char *author;
45 struct tm tm_author; /* UTC */
46 char *committer;
47 struct tm tm_committer; /* UTC */
48 char *logmsg;
49 };
51 /* A generic object. Used as a handle which holds an ID and an object type. */
52 struct got_object;
53 #define GOT_OBJ_TYPE_COMMIT 1
54 #define GOT_OBJ_TYPE_TREE 2
55 #define GOT_OBJ_TYPE_BLOB 3
56 #define GOT_OBJ_TYPE_TAG 4
57 /* 5 is reserved */
58 #define GOT_OBJ_TYPE_OFFSET_DELTA 6
59 #define GOT_OBJ_TYPE_REF_DELTA 7
61 struct got_repository;
63 /*
64 * Obtain a string representation of an object ID. The output depends on
65 * the hash function used by the repository format (currently SHA1).
66 */
67 const struct got_error *got_object_id_str(char **, struct got_object_id *);
69 /*
70 * Compare two object IDs. Return value behaves like memcmp(3).
71 */
72 int got_object_id_cmp(struct got_object_id *, struct got_object_id *);
74 /*
75 * Created a newly allocated copy of an object ID.
76 * The caller should dispose of it with free(3).
77 */
78 struct got_object_id *got_object_id_dup(struct got_object_id *);
80 /*
81 * Get a newly allocated copy of an object's ID.
82 * The caller should dispose of it with free(3).
83 */
84 struct got_object_id *got_object_get_id(struct got_object *);
86 /*
87 * Get a newly allocated copy of an object's ID string.
88 * The caller should dispose of it with free(3).
89 */
90 const struct got_error *got_object_get_id_str(char **, struct got_object *);
92 /*
93 * Obtain the type of an object.
94 * Returns one of the GOT_OBJ_TYPE_x values (see above).
95 */
96 int got_object_get_type(struct got_object *);
98 /*
99 * Attempt to open the object in a repository with the provided ID.
100 * Caller must dispose of it with got_object_close().
101 */
102 const struct got_error *got_object_open(struct got_object **,
103 struct got_repository *, struct got_object_id *);
105 /*
106 * Attempt to map the provided ID string to an object ID and then
107 * attempt to open the object in a repository with this ID.
108 * The form of an ID string depends on the hash function used by the
109 * repository format (currently SHA1).
110 * Caller must dispose of the object with got_object_close().
111 */
112 const struct got_error *got_object_open_by_id_str(struct got_object **,
113 struct got_repository *, const char *);
115 /* Dispose of an object. */
116 void got_object_close(struct got_object *);
118 /*
119 * Attempt to open a commit object in a repository.
120 * The provided object must be of type GOT_OBJ_TYPE_COMMIT.
121 * The caller must dispose of the commit with got_object_commit_close().
122 */
123 const struct got_error *got_object_commit_open(struct got_commit_object **,
124 struct got_repository *, struct got_object *);
126 /* Dispose of a commit object. */
127 void got_object_commit_close(struct got_commit_object *);
129 /*
130 * Attempt to open a tree object in a repository.
131 * The provided object must be of type GOT_OBJ_TYPE_TREE.
132 * The caller must dispose of the tree with got_object_tree_close().
133 */
134 const struct got_error *got_object_tree_open(struct got_tree_object **,
135 struct got_repository *, struct got_object *);
137 /* Dispose of a tree object. */
138 void got_object_tree_close(struct got_tree_object *);
140 /*
141 * Attempt to open a blob object in a repository.
142 * The provided object must be of type GOT_OBJ_TYPE_BLOB.
143 * The size_t argument specifies the block size of an associated read buffer.
144 * The caller must dispose of the blob with got_object_blob_close().
145 */
146 const struct got_error *got_object_blob_open(struct got_blob_object **,
147 struct got_repository *, struct got_object *, size_t);
149 /* Dispose of a blob object. */
150 void got_object_blob_close(struct got_blob_object *);
152 /*
153 * Write the string representation of the object ID of a blob to a buffer.
154 * The size_t argument speficies the size of the buffer. In the current
155 * implementation, it must be at least SHA1_DIGEST_STRING_LENGTH bytes.
156 * XXX This is a bad API, use got_object_id_str() instead.
157 */
158 char *got_object_blob_id_str(struct got_blob_object*, char *, size_t);
160 /*
161 * Get the length of header data at the beginning of the blob's read buffer.
162 * Note that header data is only present upon the first invocation of
163 * got_object_blob_read_block() after the blob is opened.
164 */
165 size_t got_object_blob_get_hdrlen(struct got_blob_object *);
167 /*
168 * Get a pointer to the blob's read buffer.
169 * The read buffer is filled by got_object_blob_read_block().
170 */
171 const uint8_t *got_object_blob_get_read_buf(struct got_blob_object *);
173 /*
174 * Read the next chunk of data from a blob, up to the blob's read buffer
175 * block size. The size_t output argument indicates how many bytes have
176 * been read into the blob's read buffer. Zero bytes will be reported if
177 * all data in the blob has been read.
178 */
179 const struct got_error *got_object_blob_read_block(size_t *,
180 struct got_blob_object *);
182 /*
183 * Read the entire content of a blob and write it to the specified file.
184 * Flush and rewind the file as well, and indicate the amount of bytes
185 * written in the size_t output argument.
186 */
187 const struct got_error *got_object_blob_dump_to_file(size_t *, FILE *,
188 struct got_blob_object *);
190 const struct got_error *
191 got_object_open_as_commit(struct got_commit_object **,
192 struct got_repository *, struct got_object_id *);
193 const struct got_error *
194 got_object_open_as_tree(struct got_tree_object **,
195 struct got_repository *, struct got_object_id *);
197 const struct got_error *
198 got_object_open_by_path(struct got_object **, struct got_repository *,
199 struct got_object_id *, const char *);
201 const struct got_error *got_object_commit_add_parent(struct got_commit_object *,
202 const char *);