Blame


1 d71d75ad 2017-11-05 stsp /*
2 a1fd68d8 2018-01-12 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 d71d75ad 2017-11-05 stsp *
4 d71d75ad 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 d71d75ad 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 d71d75ad 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
7 d71d75ad 2017-11-05 stsp *
8 d71d75ad 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 d71d75ad 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 d71d75ad 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 d71d75ad 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 d71d75ad 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 d71d75ad 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 d71d75ad 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 d71d75ad 2017-11-05 stsp */
16 d71d75ad 2017-11-05 stsp
17 2178c42e 2018-04-22 stsp #include <sys/types.h>
18 0ffeb3c2 2017-11-26 stsp #include <sys/stat.h>
19 d1cda826 2017-11-06 stsp #include <sys/queue.h>
20 2178c42e 2018-04-22 stsp #include <sys/uio.h>
21 2178c42e 2018-04-22 stsp #include <sys/socket.h>
22 2178c42e 2018-04-22 stsp #include <sys/wait.h>
23 d1cda826 2017-11-06 stsp
24 a1fd68d8 2018-01-12 stsp #include <errno.h>
25 2178c42e 2018-04-22 stsp #include <fcntl.h>
26 d71d75ad 2017-11-05 stsp #include <stdio.h>
27 ab9a70b2 2017-11-06 stsp #include <stdlib.h>
28 ab9a70b2 2017-11-06 stsp #include <string.h>
29 2178c42e 2018-04-22 stsp #include <stdint.h>
30 d71d75ad 2017-11-05 stsp #include <sha1.h>
31 ab9a70b2 2017-11-06 stsp #include <zlib.h>
32 ab9a70b2 2017-11-06 stsp #include <ctype.h>
33 ab9a70b2 2017-11-06 stsp #include <limits.h>
34 2178c42e 2018-04-22 stsp #include <imsg.h>
35 d71d75ad 2017-11-05 stsp
36 ab9a70b2 2017-11-06 stsp #include "got_error.h"
37 d71d75ad 2017-11-05 stsp #include "got_object.h"
38 ab9a70b2 2017-11-06 stsp #include "got_repository.h"
39 d71d75ad 2017-11-05 stsp
40 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
41 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
42 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
43 2178c42e 2018-04-22 stsp #include "got_lib_path.h"
44 718b3ab0 2018-03-17 stsp #include "got_lib_zbuf.h"
45 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
46 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
47 1411938b 2018-02-12 stsp
48 ab9a70b2 2017-11-06 stsp #ifndef MIN
49 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
50 ab9a70b2 2017-11-06 stsp #endif
51 ab9a70b2 2017-11-06 stsp
52 ab9a70b2 2017-11-06 stsp #ifndef nitems
53 ab9a70b2 2017-11-06 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
54 ab9a70b2 2017-11-06 stsp #endif
55 ab9a70b2 2017-11-06 stsp
56 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_COMMIT "commit"
57 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_TREE "tree"
58 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_BLOB "blob"
59 ab9a70b2 2017-11-06 stsp
60 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_TREE "tree "
61 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_PARENT "parent "
62 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_AUTHOR "author "
63 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_COMMITTER "committer "
64 d1cda826 2017-11-06 stsp
65 ef0981d5 2018-02-12 stsp const struct got_error *
66 ef0981d5 2018-02-12 stsp got_object_id_str(char **outbuf, struct got_object_id *id)
67 d71d75ad 2017-11-05 stsp {
68 ef0981d5 2018-02-12 stsp static const size_t len = SHA1_DIGEST_STRING_LENGTH;
69 ef0981d5 2018-02-12 stsp
70 ef0981d5 2018-02-12 stsp *outbuf = calloc(1, len);
71 ef0981d5 2018-02-12 stsp if (*outbuf == NULL)
72 0a585a0d 2018-03-17 stsp return got_error_from_errno();
73 ef0981d5 2018-02-12 stsp
74 ef0981d5 2018-02-12 stsp if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
75 ef0981d5 2018-02-12 stsp free(*outbuf);
76 ef0981d5 2018-02-12 stsp *outbuf = NULL;
77 ef0981d5 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
78 ef0981d5 2018-02-12 stsp }
79 ef0981d5 2018-02-12 stsp
80 ef0981d5 2018-02-12 stsp return NULL;
81 59ece79d 2018-02-12 stsp }
82 59ece79d 2018-02-12 stsp
83 a1fd68d8 2018-01-12 stsp int
84 a1fd68d8 2018-01-12 stsp got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
85 a1fd68d8 2018-01-12 stsp {
86 a1fd68d8 2018-01-12 stsp return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
87 8bf5b3c9 2018-03-17 stsp }
88 8bf5b3c9 2018-03-17 stsp
89 8bf5b3c9 2018-03-17 stsp struct got_object_id *
90 8bf5b3c9 2018-03-17 stsp got_object_id_dup(struct got_object_id *id1)
91 8bf5b3c9 2018-03-17 stsp {
92 8bf5b3c9 2018-03-17 stsp struct got_object_id *id2;
93 8bf5b3c9 2018-03-17 stsp
94 8bf5b3c9 2018-03-17 stsp id2 = malloc(sizeof(*id2));
95 8bf5b3c9 2018-03-17 stsp if (id2 == NULL)
96 8bf5b3c9 2018-03-17 stsp return NULL;
97 8bf5b3c9 2018-03-17 stsp memcpy(id2, id1, sizeof(*id2));
98 8bf5b3c9 2018-03-17 stsp return id2;
99 3235492e 2018-04-01 stsp }
100 3235492e 2018-04-01 stsp
101 3235492e 2018-04-01 stsp struct got_object_id *
102 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
103 3235492e 2018-04-01 stsp {
104 3235492e 2018-04-01 stsp return got_object_id_dup(&obj->id);
105 a1fd68d8 2018-01-12 stsp }
106 d71d75ad 2017-11-05 stsp
107 b107e67f 2018-01-19 stsp int
108 b107e67f 2018-01-19 stsp got_object_get_type(struct got_object *obj)
109 a1fd68d8 2018-01-12 stsp {
110 b107e67f 2018-01-19 stsp switch (obj->type) {
111 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
112 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
113 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
114 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
115 b107e67f 2018-01-19 stsp return obj->type;
116 96f5e8b3 2018-01-23 stsp default:
117 96f5e8b3 2018-01-23 stsp abort();
118 96f5e8b3 2018-01-23 stsp break;
119 d71d75ad 2017-11-05 stsp }
120 d71d75ad 2017-11-05 stsp
121 96f5e8b3 2018-01-23 stsp /* not reached */
122 96f5e8b3 2018-01-23 stsp return 0;
123 d71d75ad 2017-11-05 stsp }
124 ab9a70b2 2017-11-06 stsp
125 ab9a70b2 2017-11-06 stsp static const struct got_error *
126 d1cda826 2017-11-06 stsp parse_object_header(struct got_object **obj, char *buf, size_t len)
127 ab9a70b2 2017-11-06 stsp {
128 ab9a70b2 2017-11-06 stsp const char *obj_tags[] = {
129 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_COMMIT,
130 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_TREE,
131 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_BLOB
132 ab9a70b2 2017-11-06 stsp };
133 ab9a70b2 2017-11-06 stsp const int obj_types[] = {
134 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_COMMIT,
135 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_TREE,
136 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_BLOB,
137 ab9a70b2 2017-11-06 stsp };
138 ab9a70b2 2017-11-06 stsp int type = 0;
139 d1cda826 2017-11-06 stsp size_t size = 0, hdrlen = 0;
140 ab9a70b2 2017-11-06 stsp int i;
141 ab9a70b2 2017-11-06 stsp char *p = strchr(buf, '\0');
142 ab9a70b2 2017-11-06 stsp
143 ab9a70b2 2017-11-06 stsp if (p == NULL)
144 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
145 ab9a70b2 2017-11-06 stsp
146 d1cda826 2017-11-06 stsp hdrlen = strlen(buf) + 1 /* '\0' */;
147 d1cda826 2017-11-06 stsp
148 ab9a70b2 2017-11-06 stsp for (i = 0; i < nitems(obj_tags); i++) {
149 ab9a70b2 2017-11-06 stsp const char *tag = obj_tags[i];
150 63323519 2017-11-06 stsp size_t tlen = strlen(tag);
151 ab9a70b2 2017-11-06 stsp const char *errstr;
152 ab9a70b2 2017-11-06 stsp
153 63323519 2017-11-06 stsp if (strncmp(buf, tag, tlen) != 0)
154 ab9a70b2 2017-11-06 stsp continue;
155 ab9a70b2 2017-11-06 stsp
156 ab9a70b2 2017-11-06 stsp type = obj_types[i];
157 63323519 2017-11-06 stsp if (len <= tlen)
158 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
159 63323519 2017-11-06 stsp size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
160 ab9a70b2 2017-11-06 stsp if (errstr != NULL)
161 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
162 ab9a70b2 2017-11-06 stsp break;
163 ab9a70b2 2017-11-06 stsp }
164 ab9a70b2 2017-11-06 stsp
165 ab9a70b2 2017-11-06 stsp if (type == 0)
166 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
167 ab9a70b2 2017-11-06 stsp
168 ab9a70b2 2017-11-06 stsp *obj = calloc(1, sizeof(**obj));
169 1db76ab5 2018-01-26 mpi if (*obj == NULL)
170 0a585a0d 2018-03-17 stsp return got_error_from_errno();
171 ab9a70b2 2017-11-06 stsp (*obj)->type = type;
172 d1cda826 2017-11-06 stsp (*obj)->hdrlen = hdrlen;
173 ab9a70b2 2017-11-06 stsp (*obj)->size = size;
174 ab9a70b2 2017-11-06 stsp return NULL;
175 ab9a70b2 2017-11-06 stsp }
176 ab9a70b2 2017-11-06 stsp
177 ab9a70b2 2017-11-06 stsp static const struct got_error *
178 2178c42e 2018-04-22 stsp read_object_header(struct got_object **obj, FILE *f)
179 ab9a70b2 2017-11-06 stsp {
180 ab9a70b2 2017-11-06 stsp const struct got_error *err;
181 ab9a70b2 2017-11-06 stsp struct got_zstream_buf zb;
182 a3e2cbea 2017-12-01 stsp char *buf;
183 a3e2cbea 2017-12-01 stsp const size_t zbsize = 64;
184 744d9326 2017-12-01 stsp size_t outlen, totlen;
185 25783624 2018-03-12 stsp int i;
186 ab9a70b2 2017-11-06 stsp
187 744d9326 2017-12-01 stsp buf = calloc(zbsize, sizeof(char));
188 a3e2cbea 2017-12-01 stsp if (buf == NULL)
189 0a585a0d 2018-03-17 stsp return got_error_from_errno();
190 a3e2cbea 2017-12-01 stsp
191 19d747f7 2018-03-16 stsp err = got_inflate_init(&zb, NULL, zbsize);
192 a1fd68d8 2018-01-12 stsp if (err)
193 ab9a70b2 2017-11-06 stsp return err;
194 ab9a70b2 2017-11-06 stsp
195 a3e2cbea 2017-12-01 stsp i = 0;
196 744d9326 2017-12-01 stsp totlen = 0;
197 a3e2cbea 2017-12-01 stsp do {
198 126ee060 2018-02-11 stsp err = got_inflate_read(&zb, f, &outlen);
199 a3e2cbea 2017-12-01 stsp if (err)
200 a3e2cbea 2017-12-01 stsp goto done;
201 e302c59e 2017-12-01 stsp if (strchr(zb.outbuf, '\0') == NULL) {
202 a3e2cbea 2017-12-01 stsp buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
203 e302c59e 2017-12-01 stsp if (buf == NULL) {
204 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
205 e302c59e 2017-12-01 stsp goto done;
206 e302c59e 2017-12-01 stsp }
207 e302c59e 2017-12-01 stsp }
208 c56976de 2017-12-01 stsp memcpy(buf + totlen, zb.outbuf, outlen);
209 744d9326 2017-12-01 stsp totlen += outlen;
210 a3e2cbea 2017-12-01 stsp i++;
211 a3e2cbea 2017-12-01 stsp } while (strchr(zb.outbuf, '\0') == NULL);
212 ab9a70b2 2017-11-06 stsp
213 744d9326 2017-12-01 stsp err = parse_object_header(obj, buf, totlen);
214 ab9a70b2 2017-11-06 stsp done:
215 4ca7b755 2018-01-26 stsp got_inflate_end(&zb);
216 2178c42e 2018-04-22 stsp return err;
217 2178c42e 2018-04-22 stsp }
218 2178c42e 2018-04-22 stsp
219 2178c42e 2018-04-22 stsp static const struct got_error *
220 2178c42e 2018-04-22 stsp read_object_header_privsep(struct got_object **obj, int fd)
221 2178c42e 2018-04-22 stsp {
222 2178c42e 2018-04-22 stsp struct imsgbuf parent_ibuf;
223 2178c42e 2018-04-22 stsp int imsg_fds[2];
224 2178c42e 2018-04-22 stsp const struct got_error *err = NULL;
225 2178c42e 2018-04-22 stsp pid_t pid;
226 2178c42e 2018-04-22 stsp int child_status;
227 2178c42e 2018-04-22 stsp
228 2178c42e 2018-04-22 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
229 2178c42e 2018-04-22 stsp return got_error_from_errno();
230 2178c42e 2018-04-22 stsp
231 2178c42e 2018-04-22 stsp pid = fork();
232 2178c42e 2018-04-22 stsp if (pid == -1)
233 2178c42e 2018-04-22 stsp return got_error_from_errno();
234 2178c42e 2018-04-22 stsp else if (pid == 0) {
235 2178c42e 2018-04-22 stsp struct got_object *child_obj = NULL;
236 2178c42e 2018-04-22 stsp struct imsgbuf child_ibuf;
237 2178c42e 2018-04-22 stsp FILE *f = NULL;
238 2178c42e 2018-04-22 stsp int status = 0;
239 2178c42e 2018-04-22 stsp
240 2178c42e 2018-04-22 stsp setproctitle("got: read object header");
241 2178c42e 2018-04-22 stsp close(imsg_fds[0]);
242 2178c42e 2018-04-22 stsp imsg_init(&child_ibuf, imsg_fds[1]);
243 2178c42e 2018-04-22 stsp if (err)
244 2178c42e 2018-04-22 stsp goto done;
245 2178c42e 2018-04-22 stsp
246 2178c42e 2018-04-22 stsp /* revoke access to most system calls */
247 2178c42e 2018-04-22 stsp if (pledge("stdio", NULL) == -1) {
248 2178c42e 2018-04-22 stsp err = got_error_from_errno();
249 2178c42e 2018-04-22 stsp goto done;
250 2178c42e 2018-04-22 stsp }
251 2178c42e 2018-04-22 stsp
252 2178c42e 2018-04-22 stsp f = fdopen(fd, "rb");
253 2178c42e 2018-04-22 stsp if (f == NULL) {
254 2178c42e 2018-04-22 stsp err = got_error_from_errno();
255 2178c42e 2018-04-22 stsp goto done;
256 2178c42e 2018-04-22 stsp }
257 2178c42e 2018-04-22 stsp
258 2178c42e 2018-04-22 stsp err = read_object_header(&child_obj, f);
259 2178c42e 2018-04-22 stsp if (err)
260 2178c42e 2018-04-22 stsp goto done;
261 2178c42e 2018-04-22 stsp
262 2178c42e 2018-04-22 stsp err = got_privsep_send_obj(&child_ibuf, child_obj, 0);
263 2178c42e 2018-04-22 stsp done:
264 2178c42e 2018-04-22 stsp if (child_obj)
265 2178c42e 2018-04-22 stsp got_object_close(child_obj);
266 2178c42e 2018-04-22 stsp if (err) {
267 2178c42e 2018-04-22 stsp got_privsep_send_error(&child_ibuf, err);
268 2178c42e 2018-04-22 stsp status = 1;
269 2178c42e 2018-04-22 stsp }
270 2178c42e 2018-04-22 stsp if (f)
271 2178c42e 2018-04-22 stsp fclose(f);
272 2178c42e 2018-04-22 stsp imsg_clear(&child_ibuf);
273 2178c42e 2018-04-22 stsp _exit(status);
274 2178c42e 2018-04-22 stsp }
275 2178c42e 2018-04-22 stsp
276 2178c42e 2018-04-22 stsp close(imsg_fds[1]);
277 2178c42e 2018-04-22 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
278 2178c42e 2018-04-22 stsp err = got_privsep_recv_obj(obj, &parent_ibuf);
279 2178c42e 2018-04-22 stsp imsg_clear(&parent_ibuf);
280 2178c42e 2018-04-22 stsp waitpid(pid, &child_status, 0);
281 2178c42e 2018-04-22 stsp close(imsg_fds[0]);
282 ab9a70b2 2017-11-06 stsp return err;
283 ab9a70b2 2017-11-06 stsp }
284 ab9a70b2 2017-11-06 stsp
285 d1cda826 2017-11-06 stsp static const struct got_error *
286 4558fcd4 2018-01-14 stsp object_path(char **path, struct got_object_id *id, struct got_repository *repo)
287 ab9a70b2 2017-11-06 stsp {
288 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
289 ef0981d5 2018-02-12 stsp char *hex;
290 d1cda826 2017-11-06 stsp char *path_objects = got_repo_get_path_objects(repo);
291 e6b1056e 2018-04-22 stsp
292 e6b1056e 2018-04-22 stsp *path = NULL;
293 ab9a70b2 2017-11-06 stsp
294 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
295 0a585a0d 2018-03-17 stsp return got_error_from_errno();
296 ab9a70b2 2017-11-06 stsp
297 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
298 ef0981d5 2018-02-12 stsp if (err)
299 ef0981d5 2018-02-12 stsp return err;
300 ab9a70b2 2017-11-06 stsp
301 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
302 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
303 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
304 ab9a70b2 2017-11-06 stsp
305 ef0981d5 2018-02-12 stsp free(hex);
306 d1cda826 2017-11-06 stsp free(path_objects);
307 d1cda826 2017-11-06 stsp return err;
308 d1cda826 2017-11-06 stsp }
309 d1cda826 2017-11-06 stsp
310 4ee4114f 2018-01-23 stsp static const struct got_error *
311 eb651edf 2018-02-11 stsp open_loose_object(FILE **f, struct got_object *obj, struct got_repository *repo)
312 d1cda826 2017-11-06 stsp {
313 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
314 a1fd68d8 2018-01-12 stsp char *path;
315 6c00b545 2018-01-17 stsp
316 6c00b545 2018-01-17 stsp err = object_path(&path, &obj->id, repo);
317 d1cda826 2017-11-06 stsp if (err)
318 d1cda826 2017-11-06 stsp return err;
319 4558fcd4 2018-01-14 stsp *f = fopen(path, "rb");
320 4558fcd4 2018-01-14 stsp if (*f == NULL) {
321 6c00b545 2018-01-17 stsp err = got_error_from_errno();
322 6c00b545 2018-01-17 stsp goto done;
323 a1fd68d8 2018-01-12 stsp }
324 4558fcd4 2018-01-14 stsp done:
325 4558fcd4 2018-01-14 stsp free(path);
326 4558fcd4 2018-01-14 stsp return err;
327 4558fcd4 2018-01-14 stsp }
328 a1fd68d8 2018-01-12 stsp
329 4558fcd4 2018-01-14 stsp const struct got_error *
330 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
331 4558fcd4 2018-01-14 stsp struct got_object_id *id)
332 4558fcd4 2018-01-14 stsp {
333 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
334 6c00b545 2018-01-17 stsp char *path;
335 2178c42e 2018-04-22 stsp int fd;
336 4558fcd4 2018-01-14 stsp
337 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
338 4558fcd4 2018-01-14 stsp if (err)
339 4558fcd4 2018-01-14 stsp return err;
340 4558fcd4 2018-01-14 stsp
341 2178c42e 2018-04-22 stsp fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
342 2178c42e 2018-04-22 stsp if (fd == -1) {
343 6c00b545 2018-01-17 stsp if (errno != ENOENT) {
344 6c00b545 2018-01-17 stsp err = got_error_from_errno();
345 6c00b545 2018-01-17 stsp goto done;
346 6c00b545 2018-01-17 stsp }
347 6c00b545 2018-01-17 stsp err = got_packfile_open_object(obj, id, repo);
348 6c00b545 2018-01-17 stsp if (err)
349 6c00b545 2018-01-17 stsp goto done;
350 6c00b545 2018-01-17 stsp if (*obj == NULL)
351 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NO_OBJ);
352 6c00b545 2018-01-17 stsp } else {
353 2178c42e 2018-04-22 stsp err = read_object_header_privsep(obj, fd);
354 6c00b545 2018-01-17 stsp if (err)
355 6c00b545 2018-01-17 stsp goto done;
356 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
357 6c00b545 2018-01-17 stsp }
358 6c00b545 2018-01-17 stsp done:
359 6c00b545 2018-01-17 stsp free(path);
360 2178c42e 2018-04-22 stsp if (fd != -1)
361 2178c42e 2018-04-22 stsp close(fd);
362 ab9a70b2 2017-11-06 stsp return err;
363 6c00b545 2018-01-17 stsp
364 ab9a70b2 2017-11-06 stsp }
365 ab9a70b2 2017-11-06 stsp
366 6dfa2fd3 2018-02-12 stsp const struct got_error *
367 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
368 6dfa2fd3 2018-02-12 stsp const char *id_str)
369 6dfa2fd3 2018-02-12 stsp {
370 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
371 6dfa2fd3 2018-02-12 stsp
372 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
373 6dfa2fd3 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
374 6dfa2fd3 2018-02-12 stsp
375 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
376 6dfa2fd3 2018-02-12 stsp }
377 6dfa2fd3 2018-02-12 stsp
378 ab9a70b2 2017-11-06 stsp void
379 ab9a70b2 2017-11-06 stsp got_object_close(struct got_object *obj)
380 ab9a70b2 2017-11-06 stsp {
381 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
382 c3703302 2018-01-23 stsp struct got_delta *delta;
383 96f5e8b3 2018-01-23 stsp while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
384 c3703302 2018-01-23 stsp delta = SIMPLEQ_FIRST(&obj->deltas.entries);
385 96f5e8b3 2018-01-23 stsp SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
386 c3703302 2018-01-23 stsp got_delta_close(delta);
387 96f5e8b3 2018-01-23 stsp }
388 96f5e8b3 2018-01-23 stsp }
389 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
390 96f5e8b3 2018-01-23 stsp free(obj->path_packfile);
391 ab9a70b2 2017-11-06 stsp free(obj);
392 d1cda826 2017-11-06 stsp }
393 d1cda826 2017-11-06 stsp
394 d1cda826 2017-11-06 stsp static const struct got_error *
395 d1cda826 2017-11-06 stsp parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
396 d1cda826 2017-11-06 stsp {
397 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
398 d1cda826 2017-11-06 stsp char *s = buf;
399 d1cda826 2017-11-06 stsp size_t tlen;
400 d1cda826 2017-11-06 stsp ssize_t remain = (ssize_t)len;
401 d1cda826 2017-11-06 stsp
402 d1cda826 2017-11-06 stsp *commit = calloc(1, sizeof(**commit));
403 d1cda826 2017-11-06 stsp if (*commit == NULL)
404 0a585a0d 2018-03-17 stsp return got_error_from_errno();
405 59ece79d 2018-02-12 stsp (*commit)->tree_id = calloc(1, sizeof(*(*commit)->tree_id));
406 59ece79d 2018-02-12 stsp if ((*commit)->tree_id == NULL) {
407 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
408 59ece79d 2018-02-12 stsp free(*commit);
409 59ece79d 2018-02-12 stsp *commit = NULL;
410 0a585a0d 2018-03-17 stsp return err;
411 59ece79d 2018-02-12 stsp }
412 d1cda826 2017-11-06 stsp
413 d1cda826 2017-11-06 stsp SIMPLEQ_INIT(&(*commit)->parent_ids);
414 d1cda826 2017-11-06 stsp
415 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
416 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
417 d1cda826 2017-11-06 stsp remain -= tlen;
418 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
419 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
420 d1cda826 2017-11-06 stsp goto done;
421 d1cda826 2017-11-06 stsp }
422 d1cda826 2017-11-06 stsp s += tlen;
423 59ece79d 2018-02-12 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
424 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
425 d1cda826 2017-11-06 stsp goto done;
426 d1cda826 2017-11-06 stsp }
427 d1cda826 2017-11-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
428 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
429 d1cda826 2017-11-06 stsp } else {
430 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
431 d1cda826 2017-11-06 stsp goto done;
432 d1cda826 2017-11-06 stsp }
433 d1cda826 2017-11-06 stsp
434 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
435 d1cda826 2017-11-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
436 d1cda826 2017-11-06 stsp struct got_parent_id *pid;
437 d1cda826 2017-11-06 stsp
438 d1cda826 2017-11-06 stsp remain -= tlen;
439 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
440 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
441 d1cda826 2017-11-06 stsp goto done;
442 ef0981d5 2018-02-12 stsp }
443 d1cda826 2017-11-06 stsp
444 d1cda826 2017-11-06 stsp pid = calloc(1, sizeof(*pid));
445 d1cda826 2017-11-06 stsp if (pid == NULL) {
446 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
447 d1cda826 2017-11-06 stsp goto done;
448 d1cda826 2017-11-06 stsp }
449 59ece79d 2018-02-12 stsp pid->id = calloc(1, sizeof(*pid->id));
450 59ece79d 2018-02-12 stsp if (pid->id == NULL) {
451 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
452 59ece79d 2018-02-12 stsp free(pid);
453 59ece79d 2018-02-12 stsp goto done;
454 59ece79d 2018-02-12 stsp }
455 59ece79d 2018-02-12 stsp s += tlen;
456 59ece79d 2018-02-12 stsp if (!got_parse_sha1_digest(pid->id->sha1, s)) {
457 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
458 59ece79d 2018-02-12 stsp free(pid->id);
459 59ece79d 2018-02-12 stsp free(pid);
460 d1cda826 2017-11-06 stsp goto done;
461 d1cda826 2017-11-06 stsp }
462 d1cda826 2017-11-06 stsp SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
463 d1cda826 2017-11-06 stsp (*commit)->nparents++;
464 d1cda826 2017-11-06 stsp
465 eb651edf 2018-02-11 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
466 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
467 d1cda826 2017-11-06 stsp }
468 d1cda826 2017-11-06 stsp
469 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
470 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
471 d1cda826 2017-11-06 stsp char *p;
472 d1cda826 2017-11-06 stsp
473 d1cda826 2017-11-06 stsp remain -= tlen;
474 d1cda826 2017-11-06 stsp if (remain <= 0) {
475 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
476 d1cda826 2017-11-06 stsp goto done;
477 d1cda826 2017-11-06 stsp }
478 d1cda826 2017-11-06 stsp s += tlen;
479 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
480 d1cda826 2017-11-06 stsp if (p == NULL) {
481 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
482 d1cda826 2017-11-06 stsp goto done;
483 d1cda826 2017-11-06 stsp }
484 d1cda826 2017-11-06 stsp *p = '\0';
485 d1cda826 2017-11-06 stsp (*commit)->author = strdup(s);
486 d1cda826 2017-11-06 stsp if ((*commit)->author == NULL) {
487 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
488 d1cda826 2017-11-06 stsp goto done;
489 d1cda826 2017-11-06 stsp }
490 d1cda826 2017-11-06 stsp s += strlen((*commit)->author) + 1;
491 eb651edf 2018-02-11 stsp remain -= strlen((*commit)->author) + 1;
492 d1cda826 2017-11-06 stsp }
493 d1cda826 2017-11-06 stsp
494 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
495 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
496 d1cda826 2017-11-06 stsp char *p;
497 d1cda826 2017-11-06 stsp
498 d1cda826 2017-11-06 stsp remain -= tlen;
499 d1cda826 2017-11-06 stsp if (remain <= 0) {
500 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
501 d1cda826 2017-11-06 stsp goto done;
502 d1cda826 2017-11-06 stsp }
503 d1cda826 2017-11-06 stsp s += tlen;
504 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
505 d1cda826 2017-11-06 stsp if (p == NULL) {
506 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
507 d1cda826 2017-11-06 stsp goto done;
508 d1cda826 2017-11-06 stsp }
509 d1cda826 2017-11-06 stsp *p = '\0';
510 d1cda826 2017-11-06 stsp (*commit)->committer = strdup(s);
511 d1cda826 2017-11-06 stsp if ((*commit)->committer == NULL) {
512 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
513 d1cda826 2017-11-06 stsp goto done;
514 d1cda826 2017-11-06 stsp }
515 d1cda826 2017-11-06 stsp s += strlen((*commit)->committer) + 1;
516 eb651edf 2018-02-11 stsp remain -= strlen((*commit)->committer) + 1;
517 d1cda826 2017-11-06 stsp }
518 d1cda826 2017-11-06 stsp
519 eb651edf 2018-02-11 stsp (*commit)->logmsg = strndup(s, remain);
520 eb651edf 2018-02-11 stsp if ((*commit)->logmsg == NULL) {
521 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
522 eb651edf 2018-02-11 stsp goto done;
523 eb651edf 2018-02-11 stsp }
524 d1cda826 2017-11-06 stsp done:
525 59ece79d 2018-02-12 stsp if (err) {
526 d1cda826 2017-11-06 stsp got_object_commit_close(*commit);
527 59ece79d 2018-02-12 stsp *commit = NULL;
528 59ece79d 2018-02-12 stsp }
529 0ffeb3c2 2017-11-26 stsp return err;
530 0ffeb3c2 2017-11-26 stsp }
531 0ffeb3c2 2017-11-26 stsp
532 0ffeb3c2 2017-11-26 stsp static void
533 0ffeb3c2 2017-11-26 stsp tree_entry_close(struct got_tree_entry *te)
534 0ffeb3c2 2017-11-26 stsp {
535 59ece79d 2018-02-12 stsp free(te->id);
536 0ffeb3c2 2017-11-26 stsp free(te->name);
537 0ffeb3c2 2017-11-26 stsp free(te);
538 0ffeb3c2 2017-11-26 stsp }
539 0ffeb3c2 2017-11-26 stsp
540 0ffeb3c2 2017-11-26 stsp static const struct got_error *
541 0ffeb3c2 2017-11-26 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
542 0ffeb3c2 2017-11-26 stsp size_t maxlen)
543 0ffeb3c2 2017-11-26 stsp {
544 0ffeb3c2 2017-11-26 stsp char *p = buf, *space;
545 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
546 0ffeb3c2 2017-11-26 stsp
547 0ffeb3c2 2017-11-26 stsp *te = calloc(1, sizeof(**te));
548 0ffeb3c2 2017-11-26 stsp if (*te == NULL)
549 0a585a0d 2018-03-17 stsp return got_error_from_errno();
550 59ece79d 2018-02-12 stsp
551 59ece79d 2018-02-12 stsp (*te)->id = calloc(1, sizeof(*(*te)->id));
552 59ece79d 2018-02-12 stsp if ((*te)->id == NULL) {
553 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
554 59ece79d 2018-02-12 stsp free(*te);
555 59ece79d 2018-02-12 stsp *te = NULL;
556 0a585a0d 2018-03-17 stsp return err;
557 59ece79d 2018-02-12 stsp }
558 0ffeb3c2 2017-11-26 stsp
559 0ffeb3c2 2017-11-26 stsp *elen = strlen(buf) + 1;
560 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen) {
561 0ffeb3c2 2017-11-26 stsp free(*te);
562 59ece79d 2018-02-12 stsp *te = NULL;
563 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
564 0ffeb3c2 2017-11-26 stsp }
565 0ffeb3c2 2017-11-26 stsp
566 0ffeb3c2 2017-11-26 stsp space = strchr(buf, ' ');
567 0ffeb3c2 2017-11-26 stsp if (space == NULL) {
568 0a585a0d 2018-03-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
569 0ffeb3c2 2017-11-26 stsp free(*te);
570 59ece79d 2018-02-12 stsp *te = NULL;
571 0a585a0d 2018-03-17 stsp return err;
572 0ffeb3c2 2017-11-26 stsp }
573 0ffeb3c2 2017-11-26 stsp while (*p != ' ') {
574 0ffeb3c2 2017-11-26 stsp if (*p < '0' && *p > '7') {
575 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
576 0ffeb3c2 2017-11-26 stsp goto done;
577 0ffeb3c2 2017-11-26 stsp }
578 0ffeb3c2 2017-11-26 stsp (*te)->mode <<= 3;
579 0ffeb3c2 2017-11-26 stsp (*te)->mode |= *p - '0';
580 0ffeb3c2 2017-11-26 stsp p++;
581 0ffeb3c2 2017-11-26 stsp }
582 0ffeb3c2 2017-11-26 stsp
583 0ffeb3c2 2017-11-26 stsp (*te)->name = strdup(space + 1);
584 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
585 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
586 0ffeb3c2 2017-11-26 stsp goto done;
587 0ffeb3c2 2017-11-26 stsp }
588 0ffeb3c2 2017-11-26 stsp buf += strlen(buf) + 1;
589 59ece79d 2018-02-12 stsp memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
590 0ffeb3c2 2017-11-26 stsp *elen += SHA1_DIGEST_LENGTH;
591 0ffeb3c2 2017-11-26 stsp done:
592 59ece79d 2018-02-12 stsp if (err) {
593 0ffeb3c2 2017-11-26 stsp tree_entry_close(*te);
594 59ece79d 2018-02-12 stsp *te = NULL;
595 59ece79d 2018-02-12 stsp }
596 d1cda826 2017-11-06 stsp return err;
597 d1cda826 2017-11-06 stsp }
598 d1cda826 2017-11-06 stsp
599 d1cda826 2017-11-06 stsp static const struct got_error *
600 0ffeb3c2 2017-11-26 stsp parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
601 e0ab43e7 2018-03-16 stsp uint8_t *buf, size_t len)
602 0ffeb3c2 2017-11-26 stsp {
603 90356acc 2018-01-27 stsp const struct got_error *err;
604 0ffeb3c2 2017-11-26 stsp size_t remain = len;
605 0ffeb3c2 2017-11-26 stsp
606 0ffeb3c2 2017-11-26 stsp *tree = calloc(1, sizeof(**tree));
607 0ffeb3c2 2017-11-26 stsp if (*tree == NULL)
608 0a585a0d 2018-03-17 stsp return got_error_from_errno();
609 0ffeb3c2 2017-11-26 stsp
610 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INIT(&(*tree)->entries);
611 0ffeb3c2 2017-11-26 stsp
612 0ffeb3c2 2017-11-26 stsp while (remain > 0) {
613 0ffeb3c2 2017-11-26 stsp struct got_tree_entry *te;
614 0ffeb3c2 2017-11-26 stsp size_t elen;
615 0ffeb3c2 2017-11-26 stsp
616 90356acc 2018-01-27 stsp err = parse_tree_entry(&te, &elen, buf, remain);
617 90356acc 2018-01-27 stsp if (err)
618 90356acc 2018-01-27 stsp return err;
619 0ffeb3c2 2017-11-26 stsp (*tree)->nentries++;
620 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
621 0ffeb3c2 2017-11-26 stsp buf += elen;
622 0ffeb3c2 2017-11-26 stsp remain -= elen;
623 0ffeb3c2 2017-11-26 stsp }
624 0ffeb3c2 2017-11-26 stsp
625 0ffeb3c2 2017-11-26 stsp if (remain != 0) {
626 0ffeb3c2 2017-11-26 stsp got_object_tree_close(*tree);
627 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
628 0ffeb3c2 2017-11-26 stsp }
629 0ffeb3c2 2017-11-26 stsp
630 0ffeb3c2 2017-11-26 stsp return NULL;
631 0ffeb3c2 2017-11-26 stsp }
632 0ffeb3c2 2017-11-26 stsp
633 0ffeb3c2 2017-11-26 stsp static const struct got_error *
634 eb651edf 2018-02-11 stsp read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
635 eb651edf 2018-02-11 stsp {
636 eb651edf 2018-02-11 stsp const struct got_error *err = NULL;
637 eb651edf 2018-02-11 stsp static const size_t blocksize = 512;
638 eb651edf 2018-02-11 stsp size_t n, total, remain;
639 eb651edf 2018-02-11 stsp uint8_t *buf;
640 eb651edf 2018-02-11 stsp
641 eb651edf 2018-02-11 stsp *outbuf = NULL;
642 eb651edf 2018-02-11 stsp *outlen = 0;
643 eb651edf 2018-02-11 stsp
644 eb651edf 2018-02-11 stsp buf = calloc(1, blocksize);
645 eb651edf 2018-02-11 stsp if (buf == NULL)
646 0a585a0d 2018-03-17 stsp return got_error_from_errno();
647 eb651edf 2018-02-11 stsp
648 eb651edf 2018-02-11 stsp remain = blocksize;
649 eb651edf 2018-02-11 stsp total = 0;
650 eb651edf 2018-02-11 stsp while (1) {
651 eb651edf 2018-02-11 stsp if (remain == 0) {
652 eb651edf 2018-02-11 stsp uint8_t *newbuf;
653 eb651edf 2018-02-11 stsp newbuf = reallocarray(buf, 1, total + blocksize);
654 eb651edf 2018-02-11 stsp if (newbuf == NULL) {
655 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
656 eb651edf 2018-02-11 stsp goto done;
657 eb651edf 2018-02-11 stsp }
658 eb651edf 2018-02-11 stsp buf = newbuf;
659 eb651edf 2018-02-11 stsp remain += blocksize;
660 eb651edf 2018-02-11 stsp }
661 be89e2b1 2018-03-03 stsp n = fread(buf + total, 1, remain, f);
662 eb651edf 2018-02-11 stsp if (n == 0) {
663 eb651edf 2018-02-11 stsp if (ferror(f)) {
664 eb651edf 2018-02-11 stsp err = got_ferror(f, GOT_ERR_IO);
665 eb651edf 2018-02-11 stsp goto done;
666 eb651edf 2018-02-11 stsp }
667 eb651edf 2018-02-11 stsp break; /* EOF */
668 eb651edf 2018-02-11 stsp }
669 eb651edf 2018-02-11 stsp remain -= n;
670 eb651edf 2018-02-11 stsp total += n;
671 eb651edf 2018-02-11 stsp };
672 eb651edf 2018-02-11 stsp
673 eb651edf 2018-02-11 stsp done:
674 eb651edf 2018-02-11 stsp if (err == NULL) {
675 eb651edf 2018-02-11 stsp *outbuf = buf;
676 eb651edf 2018-02-11 stsp *outlen = total;
677 eb651edf 2018-02-11 stsp } else
678 eb651edf 2018-02-11 stsp free(buf);
679 eb651edf 2018-02-11 stsp return err;
680 eb651edf 2018-02-11 stsp }
681 eb651edf 2018-02-11 stsp
682 eb651edf 2018-02-11 stsp static const struct got_error *
683 d1cda826 2017-11-06 stsp read_commit_object(struct got_commit_object **commit,
684 4558fcd4 2018-01-14 stsp struct got_repository *repo, struct got_object *obj, FILE *f)
685 d1cda826 2017-11-06 stsp {
686 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
687 d1cda826 2017-11-06 stsp size_t len;
688 eb651edf 2018-02-11 stsp uint8_t *p;
689 d1cda826 2017-11-06 stsp
690 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
691 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
692 eb651edf 2018-02-11 stsp else
693 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
694 4558fcd4 2018-01-14 stsp if (err)
695 d1cda826 2017-11-06 stsp return err;
696 d1cda826 2017-11-06 stsp
697 d1cda826 2017-11-06 stsp if (len < obj->hdrlen + obj->size) {
698 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
699 d1cda826 2017-11-06 stsp goto done;
700 d1cda826 2017-11-06 stsp }
701 d1cda826 2017-11-06 stsp
702 d1cda826 2017-11-06 stsp /* Skip object header. */
703 d1cda826 2017-11-06 stsp len -= obj->hdrlen;
704 eb651edf 2018-02-11 stsp err = parse_commit_object(commit, p + obj->hdrlen, len);
705 eb651edf 2018-02-11 stsp free(p);
706 d1cda826 2017-11-06 stsp done:
707 d1cda826 2017-11-06 stsp return err;
708 d1cda826 2017-11-06 stsp }
709 d1cda826 2017-11-06 stsp
710 d1cda826 2017-11-06 stsp const struct got_error *
711 d1cda826 2017-11-06 stsp got_object_commit_open(struct got_commit_object **commit,
712 d1cda826 2017-11-06 stsp struct got_repository *repo, struct got_object *obj)
713 d1cda826 2017-11-06 stsp {
714 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
715 d1cda826 2017-11-06 stsp
716 d1cda826 2017-11-06 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
717 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_OBJ_TYPE);
718 d1cda826 2017-11-06 stsp
719 ea35256b 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
720 ea35256b 2018-03-16 stsp uint8_t *buf;
721 ea35256b 2018-03-16 stsp size_t len;
722 ea35256b 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
723 ea35256b 2018-03-16 stsp if (err)
724 ea35256b 2018-03-16 stsp return err;
725 b29656e2 2018-03-16 stsp obj->size = len;
726 b29656e2 2018-03-16 stsp err = parse_commit_object(commit, buf, len);
727 ea35256b 2018-03-16 stsp free(buf);
728 ea35256b 2018-03-16 stsp } else {
729 ea35256b 2018-03-16 stsp FILE *f;
730 eb651edf 2018-02-11 stsp err = open_loose_object(&f, obj, repo);
731 ea35256b 2018-03-16 stsp if (err)
732 ea35256b 2018-03-16 stsp return err;
733 ea35256b 2018-03-16 stsp err = read_commit_object(commit, repo, obj, f);
734 ea35256b 2018-03-16 stsp fclose(f);
735 ea35256b 2018-03-16 stsp }
736 d1cda826 2017-11-06 stsp return err;
737 d1cda826 2017-11-06 stsp }
738 d1cda826 2017-11-06 stsp
739 d1cda826 2017-11-06 stsp void
740 d1cda826 2017-11-06 stsp got_object_commit_close(struct got_commit_object *commit)
741 d1cda826 2017-11-06 stsp {
742 d1cda826 2017-11-06 stsp struct got_parent_id *pid;
743 d1cda826 2017-11-06 stsp
744 d1cda826 2017-11-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
745 d1cda826 2017-11-06 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
746 d1cda826 2017-11-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
747 59ece79d 2018-02-12 stsp free(pid->id);
748 d1cda826 2017-11-06 stsp free(pid);
749 d1cda826 2017-11-06 stsp }
750 d1cda826 2017-11-06 stsp
751 59ece79d 2018-02-12 stsp free(commit->tree_id);
752 d1cda826 2017-11-06 stsp free(commit->author);
753 d1cda826 2017-11-06 stsp free(commit->committer);
754 d1cda826 2017-11-06 stsp free(commit->logmsg);
755 d1cda826 2017-11-06 stsp free(commit);
756 d1cda826 2017-11-06 stsp }
757 0ffeb3c2 2017-11-26 stsp
758 0ffeb3c2 2017-11-26 stsp static const struct got_error *
759 0ffeb3c2 2017-11-26 stsp read_tree_object(struct got_tree_object **tree,
760 4558fcd4 2018-01-14 stsp struct got_repository *repo, struct got_object *obj, FILE *f)
761 0ffeb3c2 2017-11-26 stsp {
762 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
763 0ffeb3c2 2017-11-26 stsp size_t len;
764 eb651edf 2018-02-11 stsp uint8_t *p;
765 0ffeb3c2 2017-11-26 stsp
766 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
767 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
768 eb651edf 2018-02-11 stsp else
769 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
770 4558fcd4 2018-01-14 stsp if (err)
771 0ffeb3c2 2017-11-26 stsp return err;
772 0ffeb3c2 2017-11-26 stsp
773 0ffeb3c2 2017-11-26 stsp if (len < obj->hdrlen + obj->size) {
774 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
775 0ffeb3c2 2017-11-26 stsp goto done;
776 0ffeb3c2 2017-11-26 stsp }
777 0ffeb3c2 2017-11-26 stsp
778 0ffeb3c2 2017-11-26 stsp /* Skip object header. */
779 0ffeb3c2 2017-11-26 stsp len -= obj->hdrlen;
780 eb651edf 2018-02-11 stsp err = parse_tree_object(tree, repo, p + obj->hdrlen, len);
781 eb651edf 2018-02-11 stsp free(p);
782 0ffeb3c2 2017-11-26 stsp done:
783 0ffeb3c2 2017-11-26 stsp return err;
784 0ffeb3c2 2017-11-26 stsp }
785 0ffeb3c2 2017-11-26 stsp
786 0ffeb3c2 2017-11-26 stsp const struct got_error *
787 0ffeb3c2 2017-11-26 stsp got_object_tree_open(struct got_tree_object **tree,
788 0ffeb3c2 2017-11-26 stsp struct got_repository *repo, struct got_object *obj)
789 0ffeb3c2 2017-11-26 stsp {
790 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
791 0ffeb3c2 2017-11-26 stsp
792 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
793 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
794 0ffeb3c2 2017-11-26 stsp
795 e0ab43e7 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
796 e0ab43e7 2018-03-16 stsp uint8_t *buf;
797 e0ab43e7 2018-03-16 stsp size_t len;
798 e0ab43e7 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
799 e0ab43e7 2018-03-16 stsp if (err)
800 e0ab43e7 2018-03-16 stsp return err;
801 b29656e2 2018-03-16 stsp obj->size = len;
802 b29656e2 2018-03-16 stsp err = parse_tree_object(tree, repo, buf, len);
803 e0ab43e7 2018-03-16 stsp free(buf);
804 e0ab43e7 2018-03-16 stsp } else {
805 e0ab43e7 2018-03-16 stsp FILE *f;
806 eb651edf 2018-02-11 stsp err = open_loose_object(&f, obj, repo);
807 e0ab43e7 2018-03-16 stsp if (err)
808 e0ab43e7 2018-03-16 stsp return err;
809 e0ab43e7 2018-03-16 stsp err = read_tree_object(tree, repo, obj, f);
810 e0ab43e7 2018-03-16 stsp fclose(f);
811 e0ab43e7 2018-03-16 stsp }
812 0ffeb3c2 2017-11-26 stsp return err;
813 0ffeb3c2 2017-11-26 stsp }
814 0ffeb3c2 2017-11-26 stsp
815 0ffeb3c2 2017-11-26 stsp void
816 0ffeb3c2 2017-11-26 stsp got_object_tree_close(struct got_tree_object *tree)
817 0ffeb3c2 2017-11-26 stsp {
818 f715ca7f 2017-11-27 stsp struct got_tree_entry *te;
819 f715ca7f 2017-11-27 stsp
820 f715ca7f 2017-11-27 stsp while (!SIMPLEQ_EMPTY(&tree->entries)) {
821 f715ca7f 2017-11-27 stsp te = SIMPLEQ_FIRST(&tree->entries);
822 f715ca7f 2017-11-27 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
823 f715ca7f 2017-11-27 stsp tree_entry_close(te);
824 f715ca7f 2017-11-27 stsp }
825 f715ca7f 2017-11-27 stsp
826 f715ca7f 2017-11-27 stsp free(tree);
827 68482ea3 2017-11-27 stsp }
828 68482ea3 2017-11-27 stsp
829 68482ea3 2017-11-27 stsp const struct got_error *
830 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
831 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
832 68482ea3 2017-11-27 stsp {
833 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
834 68482ea3 2017-11-27 stsp
835 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
836 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
837 68482ea3 2017-11-27 stsp
838 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
839 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
840 7d283eee 2017-11-29 stsp
841 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
842 4558fcd4 2018-01-14 stsp if (*blob == NULL)
843 0a585a0d 2018-03-17 stsp return got_error_from_errno();
844 68482ea3 2017-11-27 stsp
845 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
846 eb651edf 2018-02-11 stsp (*blob)->read_buf = calloc(1, blocksize);
847 56866f4a 2018-03-17 stsp if ((*blob)->read_buf == NULL) {
848 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
849 56866f4a 2018-03-17 stsp free(*blob);
850 56866f4a 2018-03-17 stsp *blob = NULL;
851 0a585a0d 2018-03-17 stsp return err;
852 56866f4a 2018-03-17 stsp }
853 eb651edf 2018-02-11 stsp err = got_packfile_extract_object(&((*blob)->f), obj, repo);
854 56866f4a 2018-03-17 stsp if (err) {
855 56866f4a 2018-03-17 stsp free((*blob)->read_buf);
856 56866f4a 2018-03-17 stsp free(*blob);
857 56866f4a 2018-03-17 stsp *blob = NULL;
858 eb651edf 2018-02-11 stsp return err;
859 56866f4a 2018-03-17 stsp }
860 eb651edf 2018-02-11 stsp } else {
861 eb651edf 2018-02-11 stsp err = open_loose_object(&((*blob)->f), obj, repo);
862 eb651edf 2018-02-11 stsp if (err) {
863 eb651edf 2018-02-11 stsp free(*blob);
864 d0f3be7c 2018-03-17 stsp *blob = NULL;
865 eb651edf 2018-02-11 stsp return err;
866 eb651edf 2018-02-11 stsp }
867 68482ea3 2017-11-27 stsp
868 19d747f7 2018-03-16 stsp err = got_inflate_init(&(*blob)->zb, NULL, blocksize);
869 eb651edf 2018-02-11 stsp if (err != NULL) {
870 eb651edf 2018-02-11 stsp fclose((*blob)->f);
871 eb651edf 2018-02-11 stsp free(*blob);
872 56866f4a 2018-03-17 stsp *blob = NULL;
873 eb651edf 2018-02-11 stsp return err;
874 eb651edf 2018-02-11 stsp }
875 eb651edf 2018-02-11 stsp
876 eb651edf 2018-02-11 stsp (*blob)->read_buf = (*blob)->zb.outbuf;
877 eb651edf 2018-02-11 stsp (*blob)->flags |= GOT_BLOB_F_COMPRESSED;
878 68482ea3 2017-11-27 stsp }
879 68482ea3 2017-11-27 stsp
880 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
881 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
882 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
883 7d283eee 2017-11-29 stsp
884 68482ea3 2017-11-27 stsp return err;
885 0ffeb3c2 2017-11-26 stsp }
886 68482ea3 2017-11-27 stsp
887 68482ea3 2017-11-27 stsp void
888 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
889 68482ea3 2017-11-27 stsp {
890 eb651edf 2018-02-11 stsp if (blob->flags & GOT_BLOB_F_COMPRESSED)
891 eb651edf 2018-02-11 stsp got_inflate_end(&blob->zb);
892 eb651edf 2018-02-11 stsp else
893 eb651edf 2018-02-11 stsp free(blob->read_buf);
894 68482ea3 2017-11-27 stsp fclose(blob->f);
895 68482ea3 2017-11-27 stsp free(blob);
896 f934cf2c 2018-02-12 stsp }
897 f934cf2c 2018-02-12 stsp
898 f934cf2c 2018-02-12 stsp char *
899 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
900 f934cf2c 2018-02-12 stsp {
901 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
902 f934cf2c 2018-02-12 stsp }
903 f934cf2c 2018-02-12 stsp
904 f934cf2c 2018-02-12 stsp size_t
905 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
906 f934cf2c 2018-02-12 stsp {
907 f934cf2c 2018-02-12 stsp return blob->hdrlen;
908 68482ea3 2017-11-27 stsp }
909 68482ea3 2017-11-27 stsp
910 f934cf2c 2018-02-12 stsp const uint8_t *
911 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
912 f934cf2c 2018-02-12 stsp {
913 f934cf2c 2018-02-12 stsp return blob->read_buf;
914 f934cf2c 2018-02-12 stsp }
915 f934cf2c 2018-02-12 stsp
916 68482ea3 2017-11-27 stsp const struct got_error *
917 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
918 68482ea3 2017-11-27 stsp {
919 eb651edf 2018-02-11 stsp size_t n;
920 eb651edf 2018-02-11 stsp
921 eb651edf 2018-02-11 stsp if (blob->flags & GOT_BLOB_F_COMPRESSED)
922 eb651edf 2018-02-11 stsp return got_inflate_read(&blob->zb, blob->f, outlenp);
923 eb651edf 2018-02-11 stsp
924 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
925 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
926 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
927 eb651edf 2018-02-11 stsp *outlenp = n;
928 eb651edf 2018-02-11 stsp return NULL;
929 68482ea3 2017-11-27 stsp }