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 302b7dd6 2018-04-23 stsp }
218 302b7dd6 2018-04-23 stsp
219 302b7dd6 2018-04-23 stsp static void
220 302b7dd6 2018-04-23 stsp read_object_header_privsep_child(int obj_fd, int imsg_fds[2])
221 302b7dd6 2018-04-23 stsp {
222 302b7dd6 2018-04-23 stsp const struct got_error *err = NULL;
223 e3306bd9 2018-04-23 stsp struct got_object *obj = NULL;
224 e3306bd9 2018-04-23 stsp struct imsgbuf ibuf;
225 302b7dd6 2018-04-23 stsp FILE *f = NULL;
226 302b7dd6 2018-04-23 stsp int status = 0;
227 302b7dd6 2018-04-23 stsp
228 be37c2e6 2018-04-24 stsp setproctitle("read object header");
229 302b7dd6 2018-04-23 stsp close(imsg_fds[0]);
230 e3306bd9 2018-04-23 stsp imsg_init(&ibuf, imsg_fds[1]);
231 302b7dd6 2018-04-23 stsp
232 302b7dd6 2018-04-23 stsp /* revoke access to most system calls */
233 302b7dd6 2018-04-23 stsp if (pledge("stdio", NULL) == -1) {
234 302b7dd6 2018-04-23 stsp err = got_error_from_errno();
235 302b7dd6 2018-04-23 stsp goto done;
236 302b7dd6 2018-04-23 stsp }
237 302b7dd6 2018-04-23 stsp
238 302b7dd6 2018-04-23 stsp f = fdopen(obj_fd, "rb");
239 302b7dd6 2018-04-23 stsp if (f == NULL) {
240 302b7dd6 2018-04-23 stsp err = got_error_from_errno();
241 302b7dd6 2018-04-23 stsp close(obj_fd);
242 302b7dd6 2018-04-23 stsp goto done;
243 302b7dd6 2018-04-23 stsp }
244 302b7dd6 2018-04-23 stsp
245 e3306bd9 2018-04-23 stsp err = read_object_header(&obj, f);
246 302b7dd6 2018-04-23 stsp if (err)
247 302b7dd6 2018-04-23 stsp goto done;
248 302b7dd6 2018-04-23 stsp
249 e3306bd9 2018-04-23 stsp err = got_privsep_send_obj(&ibuf, obj, 0);
250 302b7dd6 2018-04-23 stsp done:
251 e3306bd9 2018-04-23 stsp if (obj)
252 e3306bd9 2018-04-23 stsp got_object_close(obj);
253 302b7dd6 2018-04-23 stsp if (err) {
254 e3306bd9 2018-04-23 stsp got_privsep_send_error(&ibuf, err);
255 302b7dd6 2018-04-23 stsp status = 1;
256 302b7dd6 2018-04-23 stsp }
257 302b7dd6 2018-04-23 stsp if (f)
258 302b7dd6 2018-04-23 stsp fclose(f);
259 e3306bd9 2018-04-23 stsp imsg_clear(&ibuf);
260 302b7dd6 2018-04-23 stsp close(imsg_fds[1]);
261 302b7dd6 2018-04-23 stsp _exit(status);
262 2178c42e 2018-04-22 stsp }
263 2178c42e 2018-04-22 stsp
264 2178c42e 2018-04-22 stsp static const struct got_error *
265 2178c42e 2018-04-22 stsp read_object_header_privsep(struct got_object **obj, int fd)
266 2178c42e 2018-04-22 stsp {
267 2178c42e 2018-04-22 stsp struct imsgbuf parent_ibuf;
268 2178c42e 2018-04-22 stsp int imsg_fds[2];
269 2178c42e 2018-04-22 stsp const struct got_error *err = NULL;
270 2178c42e 2018-04-22 stsp pid_t pid;
271 2178c42e 2018-04-22 stsp int child_status;
272 2178c42e 2018-04-22 stsp
273 2178c42e 2018-04-22 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
274 2178c42e 2018-04-22 stsp return got_error_from_errno();
275 2178c42e 2018-04-22 stsp
276 2178c42e 2018-04-22 stsp pid = fork();
277 2178c42e 2018-04-22 stsp if (pid == -1)
278 2178c42e 2018-04-22 stsp return got_error_from_errno();
279 2178c42e 2018-04-22 stsp else if (pid == 0) {
280 302b7dd6 2018-04-23 stsp read_object_header_privsep_child(fd, imsg_fds);
281 302b7dd6 2018-04-23 stsp /* not reached */
282 2178c42e 2018-04-22 stsp }
283 2178c42e 2018-04-22 stsp
284 2178c42e 2018-04-22 stsp close(imsg_fds[1]);
285 2178c42e 2018-04-22 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
286 2178c42e 2018-04-22 stsp err = got_privsep_recv_obj(obj, &parent_ibuf);
287 2178c42e 2018-04-22 stsp imsg_clear(&parent_ibuf);
288 2178c42e 2018-04-22 stsp waitpid(pid, &child_status, 0);
289 2178c42e 2018-04-22 stsp close(imsg_fds[0]);
290 ab9a70b2 2017-11-06 stsp return err;
291 ab9a70b2 2017-11-06 stsp }
292 ab9a70b2 2017-11-06 stsp
293 d1cda826 2017-11-06 stsp static const struct got_error *
294 4558fcd4 2018-01-14 stsp object_path(char **path, struct got_object_id *id, struct got_repository *repo)
295 ab9a70b2 2017-11-06 stsp {
296 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
297 ef0981d5 2018-02-12 stsp char *hex;
298 d1cda826 2017-11-06 stsp char *path_objects = got_repo_get_path_objects(repo);
299 e6b1056e 2018-04-22 stsp
300 e6b1056e 2018-04-22 stsp *path = NULL;
301 ab9a70b2 2017-11-06 stsp
302 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
303 0a585a0d 2018-03-17 stsp return got_error_from_errno();
304 ab9a70b2 2017-11-06 stsp
305 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
306 ef0981d5 2018-02-12 stsp if (err)
307 ef0981d5 2018-02-12 stsp return err;
308 ab9a70b2 2017-11-06 stsp
309 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
310 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
311 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
312 ab9a70b2 2017-11-06 stsp
313 ef0981d5 2018-02-12 stsp free(hex);
314 d1cda826 2017-11-06 stsp free(path_objects);
315 d1cda826 2017-11-06 stsp return err;
316 d1cda826 2017-11-06 stsp }
317 d1cda826 2017-11-06 stsp
318 4ee4114f 2018-01-23 stsp static const struct got_error *
319 d5003b79 2018-04-22 stsp open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
320 d1cda826 2017-11-06 stsp {
321 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
322 a1fd68d8 2018-01-12 stsp char *path;
323 6c00b545 2018-01-17 stsp
324 6c00b545 2018-01-17 stsp err = object_path(&path, &obj->id, repo);
325 d1cda826 2017-11-06 stsp if (err)
326 d1cda826 2017-11-06 stsp return err;
327 d5003b79 2018-04-22 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
328 d5003b79 2018-04-22 stsp if (*fd == -1) {
329 6c00b545 2018-01-17 stsp err = got_error_from_errno();
330 6c00b545 2018-01-17 stsp goto done;
331 a1fd68d8 2018-01-12 stsp }
332 4558fcd4 2018-01-14 stsp done:
333 4558fcd4 2018-01-14 stsp free(path);
334 4558fcd4 2018-01-14 stsp return err;
335 4558fcd4 2018-01-14 stsp }
336 a1fd68d8 2018-01-12 stsp
337 4558fcd4 2018-01-14 stsp const struct got_error *
338 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
339 4558fcd4 2018-01-14 stsp struct got_object_id *id)
340 4558fcd4 2018-01-14 stsp {
341 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
342 6c00b545 2018-01-17 stsp char *path;
343 2178c42e 2018-04-22 stsp int fd;
344 4558fcd4 2018-01-14 stsp
345 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
346 4558fcd4 2018-01-14 stsp if (err)
347 4558fcd4 2018-01-14 stsp return err;
348 4558fcd4 2018-01-14 stsp
349 2178c42e 2018-04-22 stsp fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
350 2178c42e 2018-04-22 stsp if (fd == -1) {
351 6c00b545 2018-01-17 stsp if (errno != ENOENT) {
352 6c00b545 2018-01-17 stsp err = got_error_from_errno();
353 6c00b545 2018-01-17 stsp goto done;
354 6c00b545 2018-01-17 stsp }
355 6c00b545 2018-01-17 stsp err = got_packfile_open_object(obj, id, repo);
356 6c00b545 2018-01-17 stsp if (err)
357 6c00b545 2018-01-17 stsp goto done;
358 6c00b545 2018-01-17 stsp if (*obj == NULL)
359 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NO_OBJ);
360 6c00b545 2018-01-17 stsp } else {
361 2178c42e 2018-04-22 stsp err = read_object_header_privsep(obj, fd);
362 6c00b545 2018-01-17 stsp if (err)
363 6c00b545 2018-01-17 stsp goto done;
364 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
365 6c00b545 2018-01-17 stsp }
366 6c00b545 2018-01-17 stsp done:
367 6c00b545 2018-01-17 stsp free(path);
368 2178c42e 2018-04-22 stsp if (fd != -1)
369 2178c42e 2018-04-22 stsp close(fd);
370 ab9a70b2 2017-11-06 stsp return err;
371 6c00b545 2018-01-17 stsp
372 ab9a70b2 2017-11-06 stsp }
373 ab9a70b2 2017-11-06 stsp
374 6dfa2fd3 2018-02-12 stsp const struct got_error *
375 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
376 6dfa2fd3 2018-02-12 stsp const char *id_str)
377 6dfa2fd3 2018-02-12 stsp {
378 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
379 6dfa2fd3 2018-02-12 stsp
380 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
381 6dfa2fd3 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
382 6dfa2fd3 2018-02-12 stsp
383 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
384 6dfa2fd3 2018-02-12 stsp }
385 6dfa2fd3 2018-02-12 stsp
386 ab9a70b2 2017-11-06 stsp void
387 ab9a70b2 2017-11-06 stsp got_object_close(struct got_object *obj)
388 ab9a70b2 2017-11-06 stsp {
389 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
390 c3703302 2018-01-23 stsp struct got_delta *delta;
391 96f5e8b3 2018-01-23 stsp while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
392 c3703302 2018-01-23 stsp delta = SIMPLEQ_FIRST(&obj->deltas.entries);
393 96f5e8b3 2018-01-23 stsp SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
394 c3703302 2018-01-23 stsp got_delta_close(delta);
395 96f5e8b3 2018-01-23 stsp }
396 96f5e8b3 2018-01-23 stsp }
397 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
398 96f5e8b3 2018-01-23 stsp free(obj->path_packfile);
399 ab9a70b2 2017-11-06 stsp free(obj);
400 bff6ca00 2018-04-23 stsp }
401 bff6ca00 2018-04-23 stsp
402 bff6ca00 2018-04-23 stsp struct got_commit_object *
403 bff6ca00 2018-04-23 stsp got_object_commit_alloc_partial(void)
404 bff6ca00 2018-04-23 stsp {
405 bff6ca00 2018-04-23 stsp struct got_commit_object *commit;
406 bff6ca00 2018-04-23 stsp
407 bff6ca00 2018-04-23 stsp commit = calloc(1, sizeof(*commit));
408 bff6ca00 2018-04-23 stsp if (commit == NULL)
409 bff6ca00 2018-04-23 stsp return NULL;
410 bff6ca00 2018-04-23 stsp commit->tree_id = calloc(1, sizeof(*commit->tree_id));
411 bff6ca00 2018-04-23 stsp if (commit->tree_id == NULL) {
412 bff6ca00 2018-04-23 stsp free(commit);
413 bff6ca00 2018-04-23 stsp return NULL;
414 bff6ca00 2018-04-23 stsp }
415 bff6ca00 2018-04-23 stsp
416 bff6ca00 2018-04-23 stsp SIMPLEQ_INIT(&commit->parent_ids);
417 bff6ca00 2018-04-23 stsp
418 bff6ca00 2018-04-23 stsp return commit;
419 bff6ca00 2018-04-23 stsp }
420 bff6ca00 2018-04-23 stsp
421 bff6ca00 2018-04-23 stsp const struct got_error *
422 bff6ca00 2018-04-23 stsp got_object_commit_add_parent(struct got_commit_object *commit,
423 bff6ca00 2018-04-23 stsp const char *id_str)
424 bff6ca00 2018-04-23 stsp {
425 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
426 bff6ca00 2018-04-23 stsp struct got_parent_id *pid;
427 bff6ca00 2018-04-23 stsp
428 bff6ca00 2018-04-23 stsp pid = calloc(1, sizeof(*pid));
429 bff6ca00 2018-04-23 stsp if (pid == NULL)
430 bff6ca00 2018-04-23 stsp return got_error_from_errno();
431 bff6ca00 2018-04-23 stsp
432 bff6ca00 2018-04-23 stsp pid->id = calloc(1, sizeof(*pid->id));
433 bff6ca00 2018-04-23 stsp if (pid->id == NULL) {
434 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
435 bff6ca00 2018-04-23 stsp free(pid);
436 bff6ca00 2018-04-23 stsp return err;
437 bff6ca00 2018-04-23 stsp }
438 bff6ca00 2018-04-23 stsp
439 bff6ca00 2018-04-23 stsp if (!got_parse_sha1_digest(pid->id->sha1, id_str)) {
440 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
441 bff6ca00 2018-04-23 stsp free(pid->id);
442 bff6ca00 2018-04-23 stsp free(pid);
443 bff6ca00 2018-04-23 stsp return err;
444 bff6ca00 2018-04-23 stsp }
445 bff6ca00 2018-04-23 stsp
446 bff6ca00 2018-04-23 stsp SIMPLEQ_INSERT_TAIL(&commit->parent_ids, pid, entry);
447 bff6ca00 2018-04-23 stsp commit->nparents++;
448 bff6ca00 2018-04-23 stsp
449 bff6ca00 2018-04-23 stsp return NULL;
450 d1cda826 2017-11-06 stsp }
451 d1cda826 2017-11-06 stsp
452 d1cda826 2017-11-06 stsp static const struct got_error *
453 d1cda826 2017-11-06 stsp parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
454 d1cda826 2017-11-06 stsp {
455 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
456 d1cda826 2017-11-06 stsp char *s = buf;
457 d1cda826 2017-11-06 stsp size_t tlen;
458 d1cda826 2017-11-06 stsp ssize_t remain = (ssize_t)len;
459 d1cda826 2017-11-06 stsp
460 bff6ca00 2018-04-23 stsp *commit = got_object_commit_alloc_partial();
461 d1cda826 2017-11-06 stsp if (*commit == NULL)
462 0a585a0d 2018-03-17 stsp return got_error_from_errno();
463 d1cda826 2017-11-06 stsp
464 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
465 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
466 d1cda826 2017-11-06 stsp remain -= tlen;
467 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
468 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
469 d1cda826 2017-11-06 stsp goto done;
470 d1cda826 2017-11-06 stsp }
471 d1cda826 2017-11-06 stsp s += tlen;
472 59ece79d 2018-02-12 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
473 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
474 d1cda826 2017-11-06 stsp goto done;
475 d1cda826 2017-11-06 stsp }
476 d1cda826 2017-11-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
477 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
478 d1cda826 2017-11-06 stsp } else {
479 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
480 d1cda826 2017-11-06 stsp goto done;
481 d1cda826 2017-11-06 stsp }
482 d1cda826 2017-11-06 stsp
483 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
484 d1cda826 2017-11-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
485 d1cda826 2017-11-06 stsp remain -= tlen;
486 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
487 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
488 d1cda826 2017-11-06 stsp goto done;
489 ef0981d5 2018-02-12 stsp }
490 59ece79d 2018-02-12 stsp s += tlen;
491 bff6ca00 2018-04-23 stsp err = got_object_commit_add_parent(*commit, s);
492 bff6ca00 2018-04-23 stsp if (err)
493 d1cda826 2017-11-06 stsp goto done;
494 d1cda826 2017-11-06 stsp
495 eb651edf 2018-02-11 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
496 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
497 d1cda826 2017-11-06 stsp }
498 d1cda826 2017-11-06 stsp
499 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
500 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
501 d1cda826 2017-11-06 stsp char *p;
502 d1cda826 2017-11-06 stsp
503 d1cda826 2017-11-06 stsp remain -= tlen;
504 d1cda826 2017-11-06 stsp if (remain <= 0) {
505 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
506 d1cda826 2017-11-06 stsp goto done;
507 d1cda826 2017-11-06 stsp }
508 d1cda826 2017-11-06 stsp s += tlen;
509 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
510 d1cda826 2017-11-06 stsp if (p == NULL) {
511 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
512 d1cda826 2017-11-06 stsp goto done;
513 d1cda826 2017-11-06 stsp }
514 d1cda826 2017-11-06 stsp *p = '\0';
515 d1cda826 2017-11-06 stsp (*commit)->author = strdup(s);
516 d1cda826 2017-11-06 stsp if ((*commit)->author == NULL) {
517 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
518 d1cda826 2017-11-06 stsp goto done;
519 d1cda826 2017-11-06 stsp }
520 d1cda826 2017-11-06 stsp s += strlen((*commit)->author) + 1;
521 eb651edf 2018-02-11 stsp remain -= strlen((*commit)->author) + 1;
522 d1cda826 2017-11-06 stsp }
523 d1cda826 2017-11-06 stsp
524 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
525 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
526 d1cda826 2017-11-06 stsp char *p;
527 d1cda826 2017-11-06 stsp
528 d1cda826 2017-11-06 stsp remain -= tlen;
529 d1cda826 2017-11-06 stsp if (remain <= 0) {
530 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
531 d1cda826 2017-11-06 stsp goto done;
532 d1cda826 2017-11-06 stsp }
533 d1cda826 2017-11-06 stsp s += tlen;
534 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
535 d1cda826 2017-11-06 stsp if (p == NULL) {
536 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
537 d1cda826 2017-11-06 stsp goto done;
538 d1cda826 2017-11-06 stsp }
539 d1cda826 2017-11-06 stsp *p = '\0';
540 d1cda826 2017-11-06 stsp (*commit)->committer = strdup(s);
541 d1cda826 2017-11-06 stsp if ((*commit)->committer == NULL) {
542 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
543 d1cda826 2017-11-06 stsp goto done;
544 d1cda826 2017-11-06 stsp }
545 d1cda826 2017-11-06 stsp s += strlen((*commit)->committer) + 1;
546 eb651edf 2018-02-11 stsp remain -= strlen((*commit)->committer) + 1;
547 d1cda826 2017-11-06 stsp }
548 d1cda826 2017-11-06 stsp
549 eb651edf 2018-02-11 stsp (*commit)->logmsg = strndup(s, remain);
550 eb651edf 2018-02-11 stsp if ((*commit)->logmsg == NULL) {
551 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
552 eb651edf 2018-02-11 stsp goto done;
553 eb651edf 2018-02-11 stsp }
554 d1cda826 2017-11-06 stsp done:
555 59ece79d 2018-02-12 stsp if (err) {
556 d1cda826 2017-11-06 stsp got_object_commit_close(*commit);
557 59ece79d 2018-02-12 stsp *commit = NULL;
558 59ece79d 2018-02-12 stsp }
559 0ffeb3c2 2017-11-26 stsp return err;
560 0ffeb3c2 2017-11-26 stsp }
561 0ffeb3c2 2017-11-26 stsp
562 0ffeb3c2 2017-11-26 stsp static void
563 0ffeb3c2 2017-11-26 stsp tree_entry_close(struct got_tree_entry *te)
564 0ffeb3c2 2017-11-26 stsp {
565 59ece79d 2018-02-12 stsp free(te->id);
566 0ffeb3c2 2017-11-26 stsp free(te->name);
567 0ffeb3c2 2017-11-26 stsp free(te);
568 0ffeb3c2 2017-11-26 stsp }
569 0ffeb3c2 2017-11-26 stsp
570 e033d803 2018-04-23 stsp struct got_tree_entry *
571 e033d803 2018-04-23 stsp got_alloc_tree_entry_partial(void)
572 e033d803 2018-04-23 stsp {
573 e033d803 2018-04-23 stsp struct got_tree_entry *te;
574 e033d803 2018-04-23 stsp
575 e033d803 2018-04-23 stsp te = calloc(1, sizeof(*te));
576 e033d803 2018-04-23 stsp if (te == NULL)
577 e033d803 2018-04-23 stsp return NULL;
578 e033d803 2018-04-23 stsp
579 e033d803 2018-04-23 stsp te->id = calloc(1, sizeof(*te->id));
580 e033d803 2018-04-23 stsp if (te->id == NULL) {
581 e033d803 2018-04-23 stsp free(te);
582 e033d803 2018-04-23 stsp te = NULL;
583 e033d803 2018-04-23 stsp }
584 e033d803 2018-04-23 stsp return te;
585 e033d803 2018-04-23 stsp }
586 e033d803 2018-04-23 stsp
587 0ffeb3c2 2017-11-26 stsp static const struct got_error *
588 0ffeb3c2 2017-11-26 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
589 0ffeb3c2 2017-11-26 stsp size_t maxlen)
590 0ffeb3c2 2017-11-26 stsp {
591 0ffeb3c2 2017-11-26 stsp char *p = buf, *space;
592 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
593 0ffeb3c2 2017-11-26 stsp
594 e033d803 2018-04-23 stsp *te = got_alloc_tree_entry_partial();
595 0ffeb3c2 2017-11-26 stsp if (*te == NULL)
596 0a585a0d 2018-03-17 stsp return got_error_from_errno();
597 59ece79d 2018-02-12 stsp
598 0ffeb3c2 2017-11-26 stsp *elen = strlen(buf) + 1;
599 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen) {
600 0ffeb3c2 2017-11-26 stsp free(*te);
601 59ece79d 2018-02-12 stsp *te = NULL;
602 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
603 0ffeb3c2 2017-11-26 stsp }
604 0ffeb3c2 2017-11-26 stsp
605 0ffeb3c2 2017-11-26 stsp space = strchr(buf, ' ');
606 0ffeb3c2 2017-11-26 stsp if (space == NULL) {
607 0a585a0d 2018-03-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
608 0ffeb3c2 2017-11-26 stsp free(*te);
609 59ece79d 2018-02-12 stsp *te = NULL;
610 0a585a0d 2018-03-17 stsp return err;
611 0ffeb3c2 2017-11-26 stsp }
612 0ffeb3c2 2017-11-26 stsp while (*p != ' ') {
613 0ffeb3c2 2017-11-26 stsp if (*p < '0' && *p > '7') {
614 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
615 0ffeb3c2 2017-11-26 stsp goto done;
616 0ffeb3c2 2017-11-26 stsp }
617 0ffeb3c2 2017-11-26 stsp (*te)->mode <<= 3;
618 0ffeb3c2 2017-11-26 stsp (*te)->mode |= *p - '0';
619 0ffeb3c2 2017-11-26 stsp p++;
620 0ffeb3c2 2017-11-26 stsp }
621 0ffeb3c2 2017-11-26 stsp
622 0ffeb3c2 2017-11-26 stsp (*te)->name = strdup(space + 1);
623 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
624 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
625 0ffeb3c2 2017-11-26 stsp goto done;
626 0ffeb3c2 2017-11-26 stsp }
627 0ffeb3c2 2017-11-26 stsp buf += strlen(buf) + 1;
628 59ece79d 2018-02-12 stsp memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
629 0ffeb3c2 2017-11-26 stsp *elen += SHA1_DIGEST_LENGTH;
630 0ffeb3c2 2017-11-26 stsp done:
631 59ece79d 2018-02-12 stsp if (err) {
632 0ffeb3c2 2017-11-26 stsp tree_entry_close(*te);
633 59ece79d 2018-02-12 stsp *te = NULL;
634 59ece79d 2018-02-12 stsp }
635 d1cda826 2017-11-06 stsp return err;
636 d1cda826 2017-11-06 stsp }
637 d1cda826 2017-11-06 stsp
638 d1cda826 2017-11-06 stsp static const struct got_error *
639 e033d803 2018-04-23 stsp parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
640 0ffeb3c2 2017-11-26 stsp {
641 90356acc 2018-01-27 stsp const struct got_error *err;
642 0ffeb3c2 2017-11-26 stsp size_t remain = len;
643 0ffeb3c2 2017-11-26 stsp
644 0ffeb3c2 2017-11-26 stsp *tree = calloc(1, sizeof(**tree));
645 0ffeb3c2 2017-11-26 stsp if (*tree == NULL)
646 0a585a0d 2018-03-17 stsp return got_error_from_errno();
647 0ffeb3c2 2017-11-26 stsp
648 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INIT(&(*tree)->entries);
649 0ffeb3c2 2017-11-26 stsp
650 0ffeb3c2 2017-11-26 stsp while (remain > 0) {
651 0ffeb3c2 2017-11-26 stsp struct got_tree_entry *te;
652 0ffeb3c2 2017-11-26 stsp size_t elen;
653 0ffeb3c2 2017-11-26 stsp
654 90356acc 2018-01-27 stsp err = parse_tree_entry(&te, &elen, buf, remain);
655 90356acc 2018-01-27 stsp if (err)
656 90356acc 2018-01-27 stsp return err;
657 0ffeb3c2 2017-11-26 stsp (*tree)->nentries++;
658 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
659 0ffeb3c2 2017-11-26 stsp buf += elen;
660 0ffeb3c2 2017-11-26 stsp remain -= elen;
661 0ffeb3c2 2017-11-26 stsp }
662 0ffeb3c2 2017-11-26 stsp
663 0ffeb3c2 2017-11-26 stsp if (remain != 0) {
664 0ffeb3c2 2017-11-26 stsp got_object_tree_close(*tree);
665 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
666 0ffeb3c2 2017-11-26 stsp }
667 0ffeb3c2 2017-11-26 stsp
668 0ffeb3c2 2017-11-26 stsp return NULL;
669 0ffeb3c2 2017-11-26 stsp }
670 0ffeb3c2 2017-11-26 stsp
671 0ffeb3c2 2017-11-26 stsp static const struct got_error *
672 eb651edf 2018-02-11 stsp read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
673 eb651edf 2018-02-11 stsp {
674 eb651edf 2018-02-11 stsp const struct got_error *err = NULL;
675 eb651edf 2018-02-11 stsp static const size_t blocksize = 512;
676 eb651edf 2018-02-11 stsp size_t n, total, remain;
677 eb651edf 2018-02-11 stsp uint8_t *buf;
678 eb651edf 2018-02-11 stsp
679 eb651edf 2018-02-11 stsp *outbuf = NULL;
680 eb651edf 2018-02-11 stsp *outlen = 0;
681 eb651edf 2018-02-11 stsp
682 eb651edf 2018-02-11 stsp buf = calloc(1, blocksize);
683 eb651edf 2018-02-11 stsp if (buf == NULL)
684 0a585a0d 2018-03-17 stsp return got_error_from_errno();
685 eb651edf 2018-02-11 stsp
686 eb651edf 2018-02-11 stsp remain = blocksize;
687 eb651edf 2018-02-11 stsp total = 0;
688 eb651edf 2018-02-11 stsp while (1) {
689 eb651edf 2018-02-11 stsp if (remain == 0) {
690 eb651edf 2018-02-11 stsp uint8_t *newbuf;
691 eb651edf 2018-02-11 stsp newbuf = reallocarray(buf, 1, total + blocksize);
692 eb651edf 2018-02-11 stsp if (newbuf == NULL) {
693 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
694 eb651edf 2018-02-11 stsp goto done;
695 eb651edf 2018-02-11 stsp }
696 eb651edf 2018-02-11 stsp buf = newbuf;
697 eb651edf 2018-02-11 stsp remain += blocksize;
698 eb651edf 2018-02-11 stsp }
699 be89e2b1 2018-03-03 stsp n = fread(buf + total, 1, remain, f);
700 eb651edf 2018-02-11 stsp if (n == 0) {
701 eb651edf 2018-02-11 stsp if (ferror(f)) {
702 eb651edf 2018-02-11 stsp err = got_ferror(f, GOT_ERR_IO);
703 eb651edf 2018-02-11 stsp goto done;
704 eb651edf 2018-02-11 stsp }
705 eb651edf 2018-02-11 stsp break; /* EOF */
706 eb651edf 2018-02-11 stsp }
707 eb651edf 2018-02-11 stsp remain -= n;
708 eb651edf 2018-02-11 stsp total += n;
709 eb651edf 2018-02-11 stsp };
710 eb651edf 2018-02-11 stsp
711 eb651edf 2018-02-11 stsp done:
712 eb651edf 2018-02-11 stsp if (err == NULL) {
713 eb651edf 2018-02-11 stsp *outbuf = buf;
714 eb651edf 2018-02-11 stsp *outlen = total;
715 eb651edf 2018-02-11 stsp } else
716 eb651edf 2018-02-11 stsp free(buf);
717 eb651edf 2018-02-11 stsp return err;
718 eb651edf 2018-02-11 stsp }
719 eb651edf 2018-02-11 stsp
720 eb651edf 2018-02-11 stsp static const struct got_error *
721 bff6ca00 2018-04-23 stsp read_commit_object(struct got_commit_object **commit, struct got_object *obj,
722 bff6ca00 2018-04-23 stsp FILE *f)
723 d1cda826 2017-11-06 stsp {
724 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
725 d1cda826 2017-11-06 stsp size_t len;
726 eb651edf 2018-02-11 stsp uint8_t *p;
727 d1cda826 2017-11-06 stsp
728 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
729 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
730 eb651edf 2018-02-11 stsp else
731 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
732 4558fcd4 2018-01-14 stsp if (err)
733 d1cda826 2017-11-06 stsp return err;
734 d1cda826 2017-11-06 stsp
735 d1cda826 2017-11-06 stsp if (len < obj->hdrlen + obj->size) {
736 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
737 d1cda826 2017-11-06 stsp goto done;
738 d1cda826 2017-11-06 stsp }
739 d1cda826 2017-11-06 stsp
740 d1cda826 2017-11-06 stsp /* Skip object header. */
741 d1cda826 2017-11-06 stsp len -= obj->hdrlen;
742 eb651edf 2018-02-11 stsp err = parse_commit_object(commit, p + obj->hdrlen, len);
743 eb651edf 2018-02-11 stsp free(p);
744 d1cda826 2017-11-06 stsp done:
745 d1cda826 2017-11-06 stsp return err;
746 d1cda826 2017-11-06 stsp }
747 d1cda826 2017-11-06 stsp
748 bff6ca00 2018-04-23 stsp static void
749 bff6ca00 2018-04-23 stsp read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
750 bff6ca00 2018-04-23 stsp int imsg_fds[2])
751 bff6ca00 2018-04-23 stsp {
752 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
753 bff6ca00 2018-04-23 stsp struct got_commit_object *commit = NULL;
754 bff6ca00 2018-04-23 stsp struct imsgbuf ibuf;
755 bff6ca00 2018-04-23 stsp FILE *f = NULL;
756 bff6ca00 2018-04-23 stsp int status = 0;
757 bff6ca00 2018-04-23 stsp
758 be37c2e6 2018-04-24 stsp setproctitle("read commit object");
759 bff6ca00 2018-04-23 stsp close(imsg_fds[0]);
760 bff6ca00 2018-04-23 stsp imsg_init(&ibuf, imsg_fds[1]);
761 bff6ca00 2018-04-23 stsp
762 bff6ca00 2018-04-23 stsp /* revoke access to most system calls */
763 bff6ca00 2018-04-23 stsp if (pledge("stdio", NULL) == -1) {
764 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
765 bff6ca00 2018-04-23 stsp goto done;
766 bff6ca00 2018-04-23 stsp }
767 bff6ca00 2018-04-23 stsp
768 bff6ca00 2018-04-23 stsp f = fdopen(obj_fd, "rb");
769 bff6ca00 2018-04-23 stsp if (f == NULL) {
770 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
771 bff6ca00 2018-04-23 stsp close(obj_fd);
772 bff6ca00 2018-04-23 stsp goto done;
773 bff6ca00 2018-04-23 stsp }
774 bff6ca00 2018-04-23 stsp
775 bff6ca00 2018-04-23 stsp err = read_commit_object(&commit, obj, f);
776 bff6ca00 2018-04-23 stsp if (err)
777 bff6ca00 2018-04-23 stsp goto done;
778 bff6ca00 2018-04-23 stsp
779 068fd2bf 2018-04-24 stsp err = got_privsep_send_commit(&ibuf, commit);
780 bff6ca00 2018-04-23 stsp done:
781 bff6ca00 2018-04-23 stsp if (commit)
782 bff6ca00 2018-04-23 stsp got_object_commit_close(commit);
783 bff6ca00 2018-04-23 stsp if (err) {
784 bff6ca00 2018-04-23 stsp got_privsep_send_error(&ibuf, err);
785 bff6ca00 2018-04-23 stsp status = 1;
786 bff6ca00 2018-04-23 stsp }
787 bff6ca00 2018-04-23 stsp if (f)
788 bff6ca00 2018-04-23 stsp fclose(f);
789 bff6ca00 2018-04-23 stsp imsg_clear(&ibuf);
790 bff6ca00 2018-04-23 stsp close(imsg_fds[1]);
791 bff6ca00 2018-04-23 stsp _exit(status);
792 bff6ca00 2018-04-23 stsp }
793 bff6ca00 2018-04-23 stsp
794 bff6ca00 2018-04-23 stsp static const struct got_error *
795 bff6ca00 2018-04-23 stsp read_commit_object_privsep(struct got_commit_object **commit,
796 bff6ca00 2018-04-23 stsp struct got_repository *repo, struct got_object *obj, int fd)
797 bff6ca00 2018-04-23 stsp {
798 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
799 bff6ca00 2018-04-23 stsp struct imsgbuf parent_ibuf;
800 bff6ca00 2018-04-23 stsp int imsg_fds[2];
801 bff6ca00 2018-04-23 stsp pid_t pid;
802 bff6ca00 2018-04-23 stsp int child_status;
803 bff6ca00 2018-04-23 stsp
804 bff6ca00 2018-04-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
805 bff6ca00 2018-04-23 stsp return got_error_from_errno();
806 bff6ca00 2018-04-23 stsp
807 bff6ca00 2018-04-23 stsp pid = fork();
808 bff6ca00 2018-04-23 stsp if (pid == -1)
809 bff6ca00 2018-04-23 stsp return got_error_from_errno();
810 bff6ca00 2018-04-23 stsp else if (pid == 0) {
811 bff6ca00 2018-04-23 stsp read_commit_object_privsep_child(obj, fd, imsg_fds);
812 e506bf32 2018-04-23 stsp /* not reached */
813 bff6ca00 2018-04-23 stsp }
814 bff6ca00 2018-04-23 stsp
815 bff6ca00 2018-04-23 stsp close(imsg_fds[1]);
816 bff6ca00 2018-04-23 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
817 068fd2bf 2018-04-24 stsp err = got_privsep_recv_commit(commit, &parent_ibuf);
818 bff6ca00 2018-04-23 stsp imsg_clear(&parent_ibuf);
819 bff6ca00 2018-04-23 stsp waitpid(pid, &child_status, 0);
820 bff6ca00 2018-04-23 stsp close(imsg_fds[0]);
821 bff6ca00 2018-04-23 stsp return err;
822 bff6ca00 2018-04-23 stsp }
823 bff6ca00 2018-04-23 stsp
824 d1cda826 2017-11-06 stsp const struct got_error *
825 d1cda826 2017-11-06 stsp got_object_commit_open(struct got_commit_object **commit,
826 d1cda826 2017-11-06 stsp struct got_repository *repo, struct got_object *obj)
827 d1cda826 2017-11-06 stsp {
828 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
829 d1cda826 2017-11-06 stsp
830 d1cda826 2017-11-06 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
831 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_OBJ_TYPE);
832 d1cda826 2017-11-06 stsp
833 ea35256b 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
834 ea35256b 2018-03-16 stsp uint8_t *buf;
835 ea35256b 2018-03-16 stsp size_t len;
836 ea35256b 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
837 ea35256b 2018-03-16 stsp if (err)
838 ea35256b 2018-03-16 stsp return err;
839 b29656e2 2018-03-16 stsp obj->size = len;
840 b29656e2 2018-03-16 stsp err = parse_commit_object(commit, buf, len);
841 ea35256b 2018-03-16 stsp free(buf);
842 ea35256b 2018-03-16 stsp } else {
843 d5003b79 2018-04-22 stsp int fd;
844 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
845 ea35256b 2018-03-16 stsp if (err)
846 ea35256b 2018-03-16 stsp return err;
847 bff6ca00 2018-04-23 stsp err = read_commit_object_privsep(commit, repo, obj, fd);
848 bff6ca00 2018-04-23 stsp close(fd);
849 ea35256b 2018-03-16 stsp }
850 d1cda826 2017-11-06 stsp return err;
851 d1cda826 2017-11-06 stsp }
852 d1cda826 2017-11-06 stsp
853 d1cda826 2017-11-06 stsp void
854 d1cda826 2017-11-06 stsp got_object_commit_close(struct got_commit_object *commit)
855 d1cda826 2017-11-06 stsp {
856 d1cda826 2017-11-06 stsp struct got_parent_id *pid;
857 d1cda826 2017-11-06 stsp
858 d1cda826 2017-11-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
859 d1cda826 2017-11-06 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
860 d1cda826 2017-11-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
861 59ece79d 2018-02-12 stsp free(pid->id);
862 d1cda826 2017-11-06 stsp free(pid);
863 d1cda826 2017-11-06 stsp }
864 d1cda826 2017-11-06 stsp
865 59ece79d 2018-02-12 stsp free(commit->tree_id);
866 d1cda826 2017-11-06 stsp free(commit->author);
867 d1cda826 2017-11-06 stsp free(commit->committer);
868 d1cda826 2017-11-06 stsp free(commit->logmsg);
869 d1cda826 2017-11-06 stsp free(commit);
870 d1cda826 2017-11-06 stsp }
871 0ffeb3c2 2017-11-26 stsp
872 0ffeb3c2 2017-11-26 stsp static const struct got_error *
873 e033d803 2018-04-23 stsp read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
874 0ffeb3c2 2017-11-26 stsp {
875 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
876 0ffeb3c2 2017-11-26 stsp size_t len;
877 eb651edf 2018-02-11 stsp uint8_t *p;
878 0ffeb3c2 2017-11-26 stsp
879 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
880 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
881 eb651edf 2018-02-11 stsp else
882 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
883 4558fcd4 2018-01-14 stsp if (err)
884 0ffeb3c2 2017-11-26 stsp return err;
885 0ffeb3c2 2017-11-26 stsp
886 0ffeb3c2 2017-11-26 stsp if (len < obj->hdrlen + obj->size) {
887 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
888 0ffeb3c2 2017-11-26 stsp goto done;
889 0ffeb3c2 2017-11-26 stsp }
890 0ffeb3c2 2017-11-26 stsp
891 0ffeb3c2 2017-11-26 stsp /* Skip object header. */
892 0ffeb3c2 2017-11-26 stsp len -= obj->hdrlen;
893 e033d803 2018-04-23 stsp err = parse_tree_object(tree, p + obj->hdrlen, len);
894 eb651edf 2018-02-11 stsp free(p);
895 e033d803 2018-04-23 stsp done:
896 e033d803 2018-04-23 stsp return err;
897 e033d803 2018-04-23 stsp }
898 e033d803 2018-04-23 stsp
899 e033d803 2018-04-23 stsp static void
900 e033d803 2018-04-23 stsp read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
901 e033d803 2018-04-23 stsp int imsg_fds[2])
902 e033d803 2018-04-23 stsp {
903 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
904 e033d803 2018-04-23 stsp struct got_tree_object *tree = NULL;
905 e033d803 2018-04-23 stsp struct imsgbuf ibuf;
906 e033d803 2018-04-23 stsp FILE *f = NULL;
907 e033d803 2018-04-23 stsp int status = 0;
908 e033d803 2018-04-23 stsp
909 be37c2e6 2018-04-24 stsp setproctitle("read tree object");
910 e033d803 2018-04-23 stsp close(imsg_fds[0]);
911 e033d803 2018-04-23 stsp imsg_init(&ibuf, imsg_fds[1]);
912 e033d803 2018-04-23 stsp
913 e033d803 2018-04-23 stsp /* revoke access to most system calls */
914 e033d803 2018-04-23 stsp if (pledge("stdio", NULL) == -1) {
915 e033d803 2018-04-23 stsp err = got_error_from_errno();
916 e033d803 2018-04-23 stsp goto done;
917 e033d803 2018-04-23 stsp }
918 e033d803 2018-04-23 stsp
919 e033d803 2018-04-23 stsp f = fdopen(obj_fd, "rb");
920 e033d803 2018-04-23 stsp if (f == NULL) {
921 e033d803 2018-04-23 stsp err = got_error_from_errno();
922 e033d803 2018-04-23 stsp close(obj_fd);
923 e033d803 2018-04-23 stsp goto done;
924 e033d803 2018-04-23 stsp }
925 e033d803 2018-04-23 stsp
926 e033d803 2018-04-23 stsp err = read_tree_object(&tree, obj, f);
927 e033d803 2018-04-23 stsp if (err)
928 e033d803 2018-04-23 stsp goto done;
929 e033d803 2018-04-23 stsp
930 068fd2bf 2018-04-24 stsp err = got_privsep_send_tree(&ibuf, tree);
931 0ffeb3c2 2017-11-26 stsp done:
932 e033d803 2018-04-23 stsp if (tree)
933 e033d803 2018-04-23 stsp got_object_tree_close(tree);
934 e033d803 2018-04-23 stsp if (err) {
935 e033d803 2018-04-23 stsp got_privsep_send_error(&ibuf, err);
936 e033d803 2018-04-23 stsp status = 1;
937 e033d803 2018-04-23 stsp }
938 e033d803 2018-04-23 stsp if (f)
939 e033d803 2018-04-23 stsp fclose(f);
940 e033d803 2018-04-23 stsp imsg_clear(&ibuf);
941 e033d803 2018-04-23 stsp close(imsg_fds[1]);
942 e033d803 2018-04-23 stsp _exit(status);
943 e033d803 2018-04-23 stsp }
944 e033d803 2018-04-23 stsp
945 e033d803 2018-04-23 stsp static const struct got_error *
946 e033d803 2018-04-23 stsp read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
947 e033d803 2018-04-23 stsp int fd)
948 e033d803 2018-04-23 stsp {
949 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
950 e033d803 2018-04-23 stsp struct imsgbuf parent_ibuf;
951 e033d803 2018-04-23 stsp int imsg_fds[2];
952 e033d803 2018-04-23 stsp pid_t pid;
953 e033d803 2018-04-23 stsp int child_status;
954 e033d803 2018-04-23 stsp
955 e033d803 2018-04-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
956 e033d803 2018-04-23 stsp return got_error_from_errno();
957 e033d803 2018-04-23 stsp
958 e033d803 2018-04-23 stsp pid = fork();
959 e033d803 2018-04-23 stsp if (pid == -1)
960 e033d803 2018-04-23 stsp return got_error_from_errno();
961 e033d803 2018-04-23 stsp else if (pid == 0) {
962 e033d803 2018-04-23 stsp read_tree_object_privsep_child(obj, fd, imsg_fds);
963 e033d803 2018-04-23 stsp /* not reached */
964 e033d803 2018-04-23 stsp }
965 e033d803 2018-04-23 stsp
966 e033d803 2018-04-23 stsp close(imsg_fds[1]);
967 e033d803 2018-04-23 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
968 068fd2bf 2018-04-24 stsp err = got_privsep_recv_tree(tree, &parent_ibuf);
969 e033d803 2018-04-23 stsp imsg_clear(&parent_ibuf);
970 e033d803 2018-04-23 stsp waitpid(pid, &child_status, 0);
971 e033d803 2018-04-23 stsp close(imsg_fds[0]);
972 0ffeb3c2 2017-11-26 stsp return err;
973 0ffeb3c2 2017-11-26 stsp }
974 0ffeb3c2 2017-11-26 stsp
975 0ffeb3c2 2017-11-26 stsp const struct got_error *
976 0ffeb3c2 2017-11-26 stsp got_object_tree_open(struct got_tree_object **tree,
977 0ffeb3c2 2017-11-26 stsp struct got_repository *repo, struct got_object *obj)
978 0ffeb3c2 2017-11-26 stsp {
979 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
980 0ffeb3c2 2017-11-26 stsp
981 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
982 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
983 0ffeb3c2 2017-11-26 stsp
984 e0ab43e7 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
985 e0ab43e7 2018-03-16 stsp uint8_t *buf;
986 e0ab43e7 2018-03-16 stsp size_t len;
987 e0ab43e7 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
988 e0ab43e7 2018-03-16 stsp if (err)
989 e0ab43e7 2018-03-16 stsp return err;
990 b29656e2 2018-03-16 stsp obj->size = len;
991 e033d803 2018-04-23 stsp err = parse_tree_object(tree, buf, len);
992 e0ab43e7 2018-03-16 stsp free(buf);
993 e0ab43e7 2018-03-16 stsp } else {
994 d5003b79 2018-04-22 stsp int fd;
995 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
996 e0ab43e7 2018-03-16 stsp if (err)
997 e0ab43e7 2018-03-16 stsp return err;
998 e033d803 2018-04-23 stsp err = read_tree_object_privsep(tree, obj, fd);
999 d5003b79 2018-04-22 stsp close(fd);
1000 e0ab43e7 2018-03-16 stsp }
1001 0ffeb3c2 2017-11-26 stsp return err;
1002 0ffeb3c2 2017-11-26 stsp }
1003 0ffeb3c2 2017-11-26 stsp
1004 0ffeb3c2 2017-11-26 stsp void
1005 0ffeb3c2 2017-11-26 stsp got_object_tree_close(struct got_tree_object *tree)
1006 0ffeb3c2 2017-11-26 stsp {
1007 f715ca7f 2017-11-27 stsp struct got_tree_entry *te;
1008 f715ca7f 2017-11-27 stsp
1009 f715ca7f 2017-11-27 stsp while (!SIMPLEQ_EMPTY(&tree->entries)) {
1010 f715ca7f 2017-11-27 stsp te = SIMPLEQ_FIRST(&tree->entries);
1011 f715ca7f 2017-11-27 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
1012 f715ca7f 2017-11-27 stsp tree_entry_close(te);
1013 f715ca7f 2017-11-27 stsp }
1014 f715ca7f 2017-11-27 stsp
1015 f715ca7f 2017-11-27 stsp free(tree);
1016 57efb1af 2018-04-24 stsp }
1017 ff6b18f8 2018-04-24 stsp
1018 ff6b18f8 2018-04-24 stsp static const struct got_error *
1019 ff6b18f8 2018-04-24 stsp read_blob_object_privsep_child(int outfd, int infd, int imsg_fds[2])
1020 ff6b18f8 2018-04-24 stsp {
1021 ff6b18f8 2018-04-24 stsp const struct got_error *err = NULL;
1022 ff6b18f8 2018-04-24 stsp struct imsgbuf ibuf;
1023 ff6b18f8 2018-04-24 stsp int status = 0;
1024 2967a784 2018-04-24 stsp size_t size;
1025 8b2180d4 2018-04-26 stsp FILE *infile = NULL;
1026 ff6b18f8 2018-04-24 stsp
1027 be37c2e6 2018-04-24 stsp setproctitle("read blob object");
1028 ff6b18f8 2018-04-24 stsp close(imsg_fds[0]);
1029 ff6b18f8 2018-04-24 stsp imsg_init(&ibuf, imsg_fds[1]);
1030 57efb1af 2018-04-24 stsp
1031 ff6b18f8 2018-04-24 stsp /* revoke access to most system calls */
1032 ff6b18f8 2018-04-24 stsp if (pledge("stdio", NULL) == -1) {
1033 ff6b18f8 2018-04-24 stsp err = got_error_from_errno();
1034 ff6b18f8 2018-04-24 stsp goto done;
1035 ff6b18f8 2018-04-24 stsp }
1036 ff6b18f8 2018-04-24 stsp
1037 8b2180d4 2018-04-26 stsp infile = fdopen(infd, "rb");
1038 8b2180d4 2018-04-26 stsp if (infile == NULL) {
1039 8b2180d4 2018-04-26 stsp err = got_error_from_errno();
1040 8b2180d4 2018-04-26 stsp close(infd);
1041 8b2180d4 2018-04-26 stsp goto done;
1042 8b2180d4 2018-04-26 stsp }
1043 8b2180d4 2018-04-26 stsp err = got_inflate_to_fd(&size, infile, outfd);
1044 8b2180d4 2018-04-26 stsp fclose(infile);
1045 ff6b18f8 2018-04-24 stsp if (err)
1046 ff6b18f8 2018-04-24 stsp goto done;
1047 ff6b18f8 2018-04-24 stsp
1048 2967a784 2018-04-24 stsp err = got_privsep_send_blob(&ibuf, size);
1049 ff6b18f8 2018-04-24 stsp done:
1050 ff6b18f8 2018-04-24 stsp if (err) {
1051 ff6b18f8 2018-04-24 stsp got_privsep_send_error(&ibuf, err);
1052 ff6b18f8 2018-04-24 stsp status = 1;
1053 ff6b18f8 2018-04-24 stsp }
1054 ff6b18f8 2018-04-24 stsp close(outfd);
1055 ff6b18f8 2018-04-24 stsp imsg_clear(&ibuf);
1056 ff6b18f8 2018-04-24 stsp close(imsg_fds[1]);
1057 ff6b18f8 2018-04-24 stsp _exit(status);
1058 ff6b18f8 2018-04-24 stsp }
1059 ff6b18f8 2018-04-24 stsp
1060 ff6b18f8 2018-04-24 stsp static const struct got_error *
1061 2967a784 2018-04-24 stsp read_blob_object_privsep(size_t *size, int outfd, int infd)
1062 ff6b18f8 2018-04-24 stsp {
1063 ff6b18f8 2018-04-24 stsp struct imsgbuf parent_ibuf;
1064 ff6b18f8 2018-04-24 stsp int imsg_fds[2];
1065 ff6b18f8 2018-04-24 stsp const struct got_error *err = NULL;
1066 ff6b18f8 2018-04-24 stsp pid_t pid;
1067 ff6b18f8 2018-04-24 stsp int child_status;
1068 ff6b18f8 2018-04-24 stsp
1069 ff6b18f8 2018-04-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1070 ff6b18f8 2018-04-24 stsp return got_error_from_errno();
1071 ff6b18f8 2018-04-24 stsp
1072 ff6b18f8 2018-04-24 stsp pid = fork();
1073 ff6b18f8 2018-04-24 stsp if (pid == -1)
1074 ff6b18f8 2018-04-24 stsp return got_error_from_errno();
1075 ff6b18f8 2018-04-24 stsp else if (pid == 0) {
1076 ff6b18f8 2018-04-24 stsp read_blob_object_privsep_child(outfd, infd, imsg_fds);
1077 ff6b18f8 2018-04-24 stsp /* not reached */
1078 ff6b18f8 2018-04-24 stsp }
1079 ff6b18f8 2018-04-24 stsp
1080 ff6b18f8 2018-04-24 stsp close(imsg_fds[1]);
1081 ff6b18f8 2018-04-24 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
1082 2967a784 2018-04-24 stsp err = got_privsep_recv_blob(size, &parent_ibuf);
1083 ff6b18f8 2018-04-24 stsp imsg_clear(&parent_ibuf);
1084 ff6b18f8 2018-04-24 stsp waitpid(pid, &child_status, 0);
1085 ff6b18f8 2018-04-24 stsp close(imsg_fds[0]);
1086 ff6b18f8 2018-04-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1087 ff6b18f8 2018-04-24 stsp err = got_error_from_errno();
1088 ff6b18f8 2018-04-24 stsp return err;
1089 ff6b18f8 2018-04-24 stsp }
1090 ff6b18f8 2018-04-24 stsp
1091 68482ea3 2017-11-27 stsp const struct got_error *
1092 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
1093 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1094 68482ea3 2017-11-27 stsp {
1095 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
1096 68482ea3 2017-11-27 stsp
1097 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
1098 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
1099 68482ea3 2017-11-27 stsp
1100 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
1101 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
1102 7d283eee 2017-11-29 stsp
1103 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1104 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1105 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1106 68482ea3 2017-11-27 stsp
1107 15c8b0e6 2018-04-24 stsp (*blob)->read_buf = calloc(1, blocksize);
1108 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1109 15c8b0e6 2018-04-24 stsp err = got_error_from_errno();
1110 c7254d79 2018-04-24 stsp goto done;
1111 15c8b0e6 2018-04-24 stsp }
1112 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1113 eb651edf 2018-02-11 stsp err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1114 c7254d79 2018-04-24 stsp if (err)
1115 c7254d79 2018-04-24 stsp goto done;
1116 eb651edf 2018-02-11 stsp } else {
1117 3aca5731 2018-04-24 stsp int infd, outfd;
1118 2967a784 2018-04-24 stsp size_t size;
1119 2967a784 2018-04-24 stsp struct stat sb;
1120 c7254d79 2018-04-24 stsp
1121 3aca5731 2018-04-24 stsp err = open_loose_object(&infd, obj, repo);
1122 c7254d79 2018-04-24 stsp if (err)
1123 c7254d79 2018-04-24 stsp goto done;
1124 c7254d79 2018-04-24 stsp
1125 3aca5731 2018-04-24 stsp
1126 3aca5731 2018-04-24 stsp outfd = got_opentempfd();
1127 3aca5731 2018-04-24 stsp if (outfd == -1) {
1128 3aca5731 2018-04-24 stsp err = got_error_from_errno();
1129 3aca5731 2018-04-24 stsp close(infd);
1130 3aca5731 2018-04-24 stsp goto done;
1131 3aca5731 2018-04-24 stsp }
1132 3aca5731 2018-04-24 stsp
1133 2967a784 2018-04-24 stsp err = read_blob_object_privsep(&size, outfd, infd);
1134 3aca5731 2018-04-24 stsp close(infd);
1135 57efb1af 2018-04-24 stsp if (err)
1136 c7254d79 2018-04-24 stsp goto done;
1137 3aca5731 2018-04-24 stsp
1138 2967a784 2018-04-24 stsp if (size != obj->hdrlen + obj->size) {
1139 1a6b3ab7 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1140 2967a784 2018-04-24 stsp close(outfd);
1141 2967a784 2018-04-24 stsp goto done;
1142 2967a784 2018-04-24 stsp }
1143 2967a784 2018-04-24 stsp
1144 2967a784 2018-04-24 stsp if (fstat(outfd, &sb) == -1) {
1145 2967a784 2018-04-24 stsp err = got_error_from_errno();
1146 2967a784 2018-04-24 stsp close(outfd);
1147 2967a784 2018-04-24 stsp goto done;
1148 2967a784 2018-04-24 stsp }
1149 2967a784 2018-04-24 stsp
1150 2967a784 2018-04-24 stsp if (sb.st_size != size) {
1151 2967a784 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1152 2967a784 2018-04-24 stsp close(outfd);
1153 2967a784 2018-04-24 stsp goto done;
1154 2967a784 2018-04-24 stsp }
1155 2967a784 2018-04-24 stsp
1156 3aca5731 2018-04-24 stsp (*blob)->f = fdopen(outfd, "rb");
1157 3aca5731 2018-04-24 stsp if ((*blob)->f == NULL) {
1158 3aca5731 2018-04-24 stsp err = got_error_from_errno();
1159 3aca5731 2018-04-24 stsp close(outfd);
1160 3aca5731 2018-04-24 stsp goto done;
1161 3aca5731 2018-04-24 stsp }
1162 68482ea3 2017-11-27 stsp }
1163 68482ea3 2017-11-27 stsp
1164 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
1165 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1166 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1167 7d283eee 2017-11-29 stsp
1168 c7254d79 2018-04-24 stsp done:
1169 c7254d79 2018-04-24 stsp if (err && *blob) {
1170 c7254d79 2018-04-24 stsp if ((*blob)->f)
1171 c7254d79 2018-04-24 stsp fclose((*blob)->f);
1172 c7254d79 2018-04-24 stsp free((*blob)->read_buf);
1173 c7254d79 2018-04-24 stsp free(*blob);
1174 c7254d79 2018-04-24 stsp *blob = NULL;
1175 c7254d79 2018-04-24 stsp }
1176 68482ea3 2017-11-27 stsp return err;
1177 0ffeb3c2 2017-11-26 stsp }
1178 68482ea3 2017-11-27 stsp
1179 68482ea3 2017-11-27 stsp void
1180 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1181 68482ea3 2017-11-27 stsp {
1182 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1183 68482ea3 2017-11-27 stsp fclose(blob->f);
1184 68482ea3 2017-11-27 stsp free(blob);
1185 f934cf2c 2018-02-12 stsp }
1186 f934cf2c 2018-02-12 stsp
1187 f934cf2c 2018-02-12 stsp char *
1188 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1189 f934cf2c 2018-02-12 stsp {
1190 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1191 f934cf2c 2018-02-12 stsp }
1192 f934cf2c 2018-02-12 stsp
1193 f934cf2c 2018-02-12 stsp size_t
1194 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1195 f934cf2c 2018-02-12 stsp {
1196 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1197 68482ea3 2017-11-27 stsp }
1198 68482ea3 2017-11-27 stsp
1199 f934cf2c 2018-02-12 stsp const uint8_t *
1200 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1201 f934cf2c 2018-02-12 stsp {
1202 f934cf2c 2018-02-12 stsp return blob->read_buf;
1203 f934cf2c 2018-02-12 stsp }
1204 f934cf2c 2018-02-12 stsp
1205 68482ea3 2017-11-27 stsp const struct got_error *
1206 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1207 68482ea3 2017-11-27 stsp {
1208 eb651edf 2018-02-11 stsp size_t n;
1209 eb651edf 2018-02-11 stsp
1210 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1211 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1212 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1213 eb651edf 2018-02-11 stsp *outlenp = n;
1214 eb651edf 2018-02-11 stsp return NULL;
1215 68482ea3 2017-11-27 stsp }