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 511a516b 2018-05-19 stsp #include "got_opentemp.h"
40 d71d75ad 2017-11-05 stsp
41 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
42 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
43 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
44 2178c42e 2018-04-22 stsp #include "got_lib_path.h"
45 718b3ab0 2018-03-17 stsp #include "got_lib_zbuf.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
47 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
48 1411938b 2018-02-12 stsp
49 ab9a70b2 2017-11-06 stsp #ifndef MIN
50 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
51 ab9a70b2 2017-11-06 stsp #endif
52 ab9a70b2 2017-11-06 stsp
53 ab9a70b2 2017-11-06 stsp #ifndef nitems
54 ab9a70b2 2017-11-06 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
55 ab9a70b2 2017-11-06 stsp #endif
56 ab9a70b2 2017-11-06 stsp
57 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_COMMIT "commit"
58 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_TREE "tree"
59 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_BLOB "blob"
60 ab9a70b2 2017-11-06 stsp
61 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_TREE "tree "
62 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_PARENT "parent "
63 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_AUTHOR "author "
64 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_COMMITTER "committer "
65 d1cda826 2017-11-06 stsp
66 ef0981d5 2018-02-12 stsp const struct got_error *
67 ef0981d5 2018-02-12 stsp got_object_id_str(char **outbuf, struct got_object_id *id)
68 d71d75ad 2017-11-05 stsp {
69 ef0981d5 2018-02-12 stsp static const size_t len = SHA1_DIGEST_STRING_LENGTH;
70 ef0981d5 2018-02-12 stsp
71 ef0981d5 2018-02-12 stsp *outbuf = calloc(1, len);
72 ef0981d5 2018-02-12 stsp if (*outbuf == NULL)
73 0a585a0d 2018-03-17 stsp return got_error_from_errno();
74 ef0981d5 2018-02-12 stsp
75 ef0981d5 2018-02-12 stsp if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
76 ef0981d5 2018-02-12 stsp free(*outbuf);
77 ef0981d5 2018-02-12 stsp *outbuf = NULL;
78 ef0981d5 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
79 ef0981d5 2018-02-12 stsp }
80 ef0981d5 2018-02-12 stsp
81 ef0981d5 2018-02-12 stsp return NULL;
82 59ece79d 2018-02-12 stsp }
83 59ece79d 2018-02-12 stsp
84 a1fd68d8 2018-01-12 stsp int
85 a1fd68d8 2018-01-12 stsp got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
86 a1fd68d8 2018-01-12 stsp {
87 a1fd68d8 2018-01-12 stsp return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
88 8bf5b3c9 2018-03-17 stsp }
89 8bf5b3c9 2018-03-17 stsp
90 8bf5b3c9 2018-03-17 stsp struct got_object_id *
91 8bf5b3c9 2018-03-17 stsp got_object_id_dup(struct got_object_id *id1)
92 8bf5b3c9 2018-03-17 stsp {
93 8bf5b3c9 2018-03-17 stsp struct got_object_id *id2;
94 8bf5b3c9 2018-03-17 stsp
95 8bf5b3c9 2018-03-17 stsp id2 = malloc(sizeof(*id2));
96 8bf5b3c9 2018-03-17 stsp if (id2 == NULL)
97 8bf5b3c9 2018-03-17 stsp return NULL;
98 8bf5b3c9 2018-03-17 stsp memcpy(id2, id1, sizeof(*id2));
99 8bf5b3c9 2018-03-17 stsp return id2;
100 3235492e 2018-04-01 stsp }
101 3235492e 2018-04-01 stsp
102 3235492e 2018-04-01 stsp struct got_object_id *
103 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
104 3235492e 2018-04-01 stsp {
105 3235492e 2018-04-01 stsp return got_object_id_dup(&obj->id);
106 bacc9935 2018-05-20 stsp }
107 bacc9935 2018-05-20 stsp
108 bacc9935 2018-05-20 stsp const struct got_error *
109 bacc9935 2018-05-20 stsp got_object_get_id_str(char **outbuf, struct got_object *obj)
110 bacc9935 2018-05-20 stsp {
111 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
112 a1fd68d8 2018-01-12 stsp }
113 d71d75ad 2017-11-05 stsp
114 b107e67f 2018-01-19 stsp int
115 b107e67f 2018-01-19 stsp got_object_get_type(struct got_object *obj)
116 a1fd68d8 2018-01-12 stsp {
117 b107e67f 2018-01-19 stsp switch (obj->type) {
118 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
119 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
120 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
121 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
122 b107e67f 2018-01-19 stsp return obj->type;
123 96f5e8b3 2018-01-23 stsp default:
124 96f5e8b3 2018-01-23 stsp abort();
125 96f5e8b3 2018-01-23 stsp break;
126 d71d75ad 2017-11-05 stsp }
127 d71d75ad 2017-11-05 stsp
128 96f5e8b3 2018-01-23 stsp /* not reached */
129 96f5e8b3 2018-01-23 stsp return 0;
130 d71d75ad 2017-11-05 stsp }
131 ab9a70b2 2017-11-06 stsp
132 ab9a70b2 2017-11-06 stsp static const struct got_error *
133 d1cda826 2017-11-06 stsp parse_object_header(struct got_object **obj, char *buf, size_t len)
134 ab9a70b2 2017-11-06 stsp {
135 ab9a70b2 2017-11-06 stsp const char *obj_tags[] = {
136 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_COMMIT,
137 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_TREE,
138 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_BLOB
139 ab9a70b2 2017-11-06 stsp };
140 ab9a70b2 2017-11-06 stsp const int obj_types[] = {
141 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_COMMIT,
142 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_TREE,
143 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_BLOB,
144 ab9a70b2 2017-11-06 stsp };
145 ab9a70b2 2017-11-06 stsp int type = 0;
146 d1cda826 2017-11-06 stsp size_t size = 0, hdrlen = 0;
147 ab9a70b2 2017-11-06 stsp int i;
148 ab9a70b2 2017-11-06 stsp char *p = strchr(buf, '\0');
149 ab9a70b2 2017-11-06 stsp
150 ab9a70b2 2017-11-06 stsp if (p == NULL)
151 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
152 ab9a70b2 2017-11-06 stsp
153 d1cda826 2017-11-06 stsp hdrlen = strlen(buf) + 1 /* '\0' */;
154 d1cda826 2017-11-06 stsp
155 ab9a70b2 2017-11-06 stsp for (i = 0; i < nitems(obj_tags); i++) {
156 ab9a70b2 2017-11-06 stsp const char *tag = obj_tags[i];
157 63323519 2017-11-06 stsp size_t tlen = strlen(tag);
158 ab9a70b2 2017-11-06 stsp const char *errstr;
159 ab9a70b2 2017-11-06 stsp
160 63323519 2017-11-06 stsp if (strncmp(buf, tag, tlen) != 0)
161 ab9a70b2 2017-11-06 stsp continue;
162 ab9a70b2 2017-11-06 stsp
163 ab9a70b2 2017-11-06 stsp type = obj_types[i];
164 63323519 2017-11-06 stsp if (len <= tlen)
165 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
166 63323519 2017-11-06 stsp size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
167 ab9a70b2 2017-11-06 stsp if (errstr != NULL)
168 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
169 ab9a70b2 2017-11-06 stsp break;
170 ab9a70b2 2017-11-06 stsp }
171 ab9a70b2 2017-11-06 stsp
172 ab9a70b2 2017-11-06 stsp if (type == 0)
173 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
174 ab9a70b2 2017-11-06 stsp
175 ab9a70b2 2017-11-06 stsp *obj = calloc(1, sizeof(**obj));
176 1db76ab5 2018-01-26 mpi if (*obj == NULL)
177 0a585a0d 2018-03-17 stsp return got_error_from_errno();
178 ab9a70b2 2017-11-06 stsp (*obj)->type = type;
179 d1cda826 2017-11-06 stsp (*obj)->hdrlen = hdrlen;
180 ab9a70b2 2017-11-06 stsp (*obj)->size = size;
181 ab9a70b2 2017-11-06 stsp return NULL;
182 ab9a70b2 2017-11-06 stsp }
183 ab9a70b2 2017-11-06 stsp
184 ab9a70b2 2017-11-06 stsp static const struct got_error *
185 2178c42e 2018-04-22 stsp read_object_header(struct got_object **obj, FILE *f)
186 ab9a70b2 2017-11-06 stsp {
187 ab9a70b2 2017-11-06 stsp const struct got_error *err;
188 ab9a70b2 2017-11-06 stsp struct got_zstream_buf zb;
189 a3e2cbea 2017-12-01 stsp char *buf;
190 a3e2cbea 2017-12-01 stsp const size_t zbsize = 64;
191 744d9326 2017-12-01 stsp size_t outlen, totlen;
192 25783624 2018-03-12 stsp int i;
193 ab9a70b2 2017-11-06 stsp
194 744d9326 2017-12-01 stsp buf = calloc(zbsize, sizeof(char));
195 a3e2cbea 2017-12-01 stsp if (buf == NULL)
196 0a585a0d 2018-03-17 stsp return got_error_from_errno();
197 a3e2cbea 2017-12-01 stsp
198 19d747f7 2018-03-16 stsp err = got_inflate_init(&zb, NULL, zbsize);
199 a1fd68d8 2018-01-12 stsp if (err)
200 ab9a70b2 2017-11-06 stsp return err;
201 ab9a70b2 2017-11-06 stsp
202 a3e2cbea 2017-12-01 stsp i = 0;
203 744d9326 2017-12-01 stsp totlen = 0;
204 a3e2cbea 2017-12-01 stsp do {
205 126ee060 2018-02-11 stsp err = got_inflate_read(&zb, f, &outlen);
206 a3e2cbea 2017-12-01 stsp if (err)
207 a3e2cbea 2017-12-01 stsp goto done;
208 e302c59e 2017-12-01 stsp if (strchr(zb.outbuf, '\0') == NULL) {
209 a3e2cbea 2017-12-01 stsp buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
210 e302c59e 2017-12-01 stsp if (buf == NULL) {
211 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
212 e302c59e 2017-12-01 stsp goto done;
213 e302c59e 2017-12-01 stsp }
214 e302c59e 2017-12-01 stsp }
215 c56976de 2017-12-01 stsp memcpy(buf + totlen, zb.outbuf, outlen);
216 744d9326 2017-12-01 stsp totlen += outlen;
217 a3e2cbea 2017-12-01 stsp i++;
218 a3e2cbea 2017-12-01 stsp } while (strchr(zb.outbuf, '\0') == NULL);
219 ab9a70b2 2017-11-06 stsp
220 744d9326 2017-12-01 stsp err = parse_object_header(obj, buf, totlen);
221 ab9a70b2 2017-11-06 stsp done:
222 4ca7b755 2018-01-26 stsp got_inflate_end(&zb);
223 2178c42e 2018-04-22 stsp return err;
224 302b7dd6 2018-04-23 stsp }
225 302b7dd6 2018-04-23 stsp
226 302b7dd6 2018-04-23 stsp static void
227 302b7dd6 2018-04-23 stsp read_object_header_privsep_child(int obj_fd, int imsg_fds[2])
228 302b7dd6 2018-04-23 stsp {
229 302b7dd6 2018-04-23 stsp const struct got_error *err = NULL;
230 e3306bd9 2018-04-23 stsp struct got_object *obj = NULL;
231 e3306bd9 2018-04-23 stsp struct imsgbuf ibuf;
232 302b7dd6 2018-04-23 stsp FILE *f = NULL;
233 302b7dd6 2018-04-23 stsp int status = 0;
234 302b7dd6 2018-04-23 stsp
235 be37c2e6 2018-04-24 stsp setproctitle("read object header");
236 302b7dd6 2018-04-23 stsp close(imsg_fds[0]);
237 e3306bd9 2018-04-23 stsp imsg_init(&ibuf, imsg_fds[1]);
238 302b7dd6 2018-04-23 stsp
239 302b7dd6 2018-04-23 stsp /* revoke access to most system calls */
240 302b7dd6 2018-04-23 stsp if (pledge("stdio", NULL) == -1) {
241 302b7dd6 2018-04-23 stsp err = got_error_from_errno();
242 302b7dd6 2018-04-23 stsp goto done;
243 302b7dd6 2018-04-23 stsp }
244 302b7dd6 2018-04-23 stsp
245 302b7dd6 2018-04-23 stsp f = fdopen(obj_fd, "rb");
246 302b7dd6 2018-04-23 stsp if (f == NULL) {
247 302b7dd6 2018-04-23 stsp err = got_error_from_errno();
248 302b7dd6 2018-04-23 stsp close(obj_fd);
249 302b7dd6 2018-04-23 stsp goto done;
250 302b7dd6 2018-04-23 stsp }
251 302b7dd6 2018-04-23 stsp
252 e3306bd9 2018-04-23 stsp err = read_object_header(&obj, f);
253 302b7dd6 2018-04-23 stsp if (err)
254 302b7dd6 2018-04-23 stsp goto done;
255 302b7dd6 2018-04-23 stsp
256 e3306bd9 2018-04-23 stsp err = got_privsep_send_obj(&ibuf, obj, 0);
257 302b7dd6 2018-04-23 stsp done:
258 e3306bd9 2018-04-23 stsp if (obj)
259 e3306bd9 2018-04-23 stsp got_object_close(obj);
260 302b7dd6 2018-04-23 stsp if (err) {
261 e3306bd9 2018-04-23 stsp got_privsep_send_error(&ibuf, err);
262 302b7dd6 2018-04-23 stsp status = 1;
263 302b7dd6 2018-04-23 stsp }
264 302b7dd6 2018-04-23 stsp if (f)
265 302b7dd6 2018-04-23 stsp fclose(f);
266 e3306bd9 2018-04-23 stsp imsg_clear(&ibuf);
267 302b7dd6 2018-04-23 stsp close(imsg_fds[1]);
268 302b7dd6 2018-04-23 stsp _exit(status);
269 b419fc47 2018-04-26 stsp }
270 b419fc47 2018-04-26 stsp
271 b419fc47 2018-04-26 stsp static const struct got_error *
272 b419fc47 2018-04-26 stsp wait_for_child(pid_t pid)
273 b419fc47 2018-04-26 stsp {
274 b419fc47 2018-04-26 stsp int child_status;
275 b419fc47 2018-04-26 stsp
276 b419fc47 2018-04-26 stsp waitpid(pid, &child_status, 0);
277 b419fc47 2018-04-26 stsp
278 b419fc47 2018-04-26 stsp if (!WIFEXITED(child_status))
279 b419fc47 2018-04-26 stsp return got_error(GOT_ERR_PRIVSEP_DIED);
280 b419fc47 2018-04-26 stsp
281 b419fc47 2018-04-26 stsp if (WEXITSTATUS(child_status) != 0)
282 b419fc47 2018-04-26 stsp return got_error(GOT_ERR_PRIVSEP_EXIT);
283 b419fc47 2018-04-26 stsp
284 b419fc47 2018-04-26 stsp return NULL;
285 2178c42e 2018-04-22 stsp }
286 2178c42e 2018-04-22 stsp
287 2178c42e 2018-04-22 stsp static const struct got_error *
288 2178c42e 2018-04-22 stsp read_object_header_privsep(struct got_object **obj, int fd)
289 2178c42e 2018-04-22 stsp {
290 2178c42e 2018-04-22 stsp struct imsgbuf parent_ibuf;
291 2178c42e 2018-04-22 stsp int imsg_fds[2];
292 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
293 2178c42e 2018-04-22 stsp pid_t pid;
294 2178c42e 2018-04-22 stsp
295 2178c42e 2018-04-22 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
296 2178c42e 2018-04-22 stsp return got_error_from_errno();
297 2178c42e 2018-04-22 stsp
298 2178c42e 2018-04-22 stsp pid = fork();
299 2178c42e 2018-04-22 stsp if (pid == -1)
300 2178c42e 2018-04-22 stsp return got_error_from_errno();
301 2178c42e 2018-04-22 stsp else if (pid == 0) {
302 302b7dd6 2018-04-23 stsp read_object_header_privsep_child(fd, imsg_fds);
303 302b7dd6 2018-04-23 stsp /* not reached */
304 2178c42e 2018-04-22 stsp }
305 2178c42e 2018-04-22 stsp
306 2178c42e 2018-04-22 stsp close(imsg_fds[1]);
307 2178c42e 2018-04-22 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
308 2178c42e 2018-04-22 stsp err = got_privsep_recv_obj(obj, &parent_ibuf);
309 2178c42e 2018-04-22 stsp imsg_clear(&parent_ibuf);
310 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
311 2178c42e 2018-04-22 stsp close(imsg_fds[0]);
312 b419fc47 2018-04-26 stsp return err ? err : err_child;
313 ab9a70b2 2017-11-06 stsp }
314 ab9a70b2 2017-11-06 stsp
315 d1cda826 2017-11-06 stsp static const struct got_error *
316 4558fcd4 2018-01-14 stsp object_path(char **path, struct got_object_id *id, struct got_repository *repo)
317 ab9a70b2 2017-11-06 stsp {
318 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
319 ef0981d5 2018-02-12 stsp char *hex;
320 d1cda826 2017-11-06 stsp char *path_objects = got_repo_get_path_objects(repo);
321 e6b1056e 2018-04-22 stsp
322 e6b1056e 2018-04-22 stsp *path = NULL;
323 ab9a70b2 2017-11-06 stsp
324 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
325 0a585a0d 2018-03-17 stsp return got_error_from_errno();
326 ab9a70b2 2017-11-06 stsp
327 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
328 ef0981d5 2018-02-12 stsp if (err)
329 ef0981d5 2018-02-12 stsp return err;
330 ab9a70b2 2017-11-06 stsp
331 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
332 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
333 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
334 ab9a70b2 2017-11-06 stsp
335 ef0981d5 2018-02-12 stsp free(hex);
336 d1cda826 2017-11-06 stsp free(path_objects);
337 d1cda826 2017-11-06 stsp return err;
338 d1cda826 2017-11-06 stsp }
339 d1cda826 2017-11-06 stsp
340 4ee4114f 2018-01-23 stsp static const struct got_error *
341 d5003b79 2018-04-22 stsp open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
342 d1cda826 2017-11-06 stsp {
343 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
344 a1fd68d8 2018-01-12 stsp char *path;
345 6c00b545 2018-01-17 stsp
346 6c00b545 2018-01-17 stsp err = object_path(&path, &obj->id, repo);
347 d1cda826 2017-11-06 stsp if (err)
348 d1cda826 2017-11-06 stsp return err;
349 d5003b79 2018-04-22 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
350 d5003b79 2018-04-22 stsp if (*fd == -1) {
351 6c00b545 2018-01-17 stsp err = got_error_from_errno();
352 6c00b545 2018-01-17 stsp goto done;
353 a1fd68d8 2018-01-12 stsp }
354 4558fcd4 2018-01-14 stsp done:
355 4558fcd4 2018-01-14 stsp free(path);
356 4558fcd4 2018-01-14 stsp return err;
357 4558fcd4 2018-01-14 stsp }
358 a1fd68d8 2018-01-12 stsp
359 4558fcd4 2018-01-14 stsp const struct got_error *
360 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
361 4558fcd4 2018-01-14 stsp struct got_object_id *id)
362 4558fcd4 2018-01-14 stsp {
363 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
364 6c00b545 2018-01-17 stsp char *path;
365 2178c42e 2018-04-22 stsp int fd;
366 4558fcd4 2018-01-14 stsp
367 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
368 4558fcd4 2018-01-14 stsp if (err)
369 4558fcd4 2018-01-14 stsp return err;
370 4558fcd4 2018-01-14 stsp
371 2178c42e 2018-04-22 stsp fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
372 2178c42e 2018-04-22 stsp if (fd == -1) {
373 6c00b545 2018-01-17 stsp if (errno != ENOENT) {
374 6c00b545 2018-01-17 stsp err = got_error_from_errno();
375 6c00b545 2018-01-17 stsp goto done;
376 6c00b545 2018-01-17 stsp }
377 6c00b545 2018-01-17 stsp err = got_packfile_open_object(obj, id, repo);
378 6c00b545 2018-01-17 stsp if (err)
379 6c00b545 2018-01-17 stsp goto done;
380 6c00b545 2018-01-17 stsp if (*obj == NULL)
381 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NO_OBJ);
382 6c00b545 2018-01-17 stsp } else {
383 2178c42e 2018-04-22 stsp err = read_object_header_privsep(obj, fd);
384 6c00b545 2018-01-17 stsp if (err)
385 6c00b545 2018-01-17 stsp goto done;
386 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
387 6c00b545 2018-01-17 stsp }
388 6c00b545 2018-01-17 stsp done:
389 6c00b545 2018-01-17 stsp free(path);
390 2178c42e 2018-04-22 stsp if (fd != -1)
391 2178c42e 2018-04-22 stsp close(fd);
392 ab9a70b2 2017-11-06 stsp return err;
393 6c00b545 2018-01-17 stsp
394 ab9a70b2 2017-11-06 stsp }
395 ab9a70b2 2017-11-06 stsp
396 6dfa2fd3 2018-02-12 stsp const struct got_error *
397 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
398 6dfa2fd3 2018-02-12 stsp const char *id_str)
399 6dfa2fd3 2018-02-12 stsp {
400 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
401 6dfa2fd3 2018-02-12 stsp
402 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
403 6dfa2fd3 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
404 6dfa2fd3 2018-02-12 stsp
405 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
406 6dfa2fd3 2018-02-12 stsp }
407 6dfa2fd3 2018-02-12 stsp
408 ab9a70b2 2017-11-06 stsp void
409 ab9a70b2 2017-11-06 stsp got_object_close(struct got_object *obj)
410 ab9a70b2 2017-11-06 stsp {
411 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
412 c3703302 2018-01-23 stsp struct got_delta *delta;
413 96f5e8b3 2018-01-23 stsp while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
414 c3703302 2018-01-23 stsp delta = SIMPLEQ_FIRST(&obj->deltas.entries);
415 96f5e8b3 2018-01-23 stsp SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
416 c3703302 2018-01-23 stsp got_delta_close(delta);
417 96f5e8b3 2018-01-23 stsp }
418 96f5e8b3 2018-01-23 stsp }
419 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
420 96f5e8b3 2018-01-23 stsp free(obj->path_packfile);
421 ab9a70b2 2017-11-06 stsp free(obj);
422 bff6ca00 2018-04-23 stsp }
423 bff6ca00 2018-04-23 stsp
424 bff6ca00 2018-04-23 stsp struct got_commit_object *
425 bff6ca00 2018-04-23 stsp got_object_commit_alloc_partial(void)
426 bff6ca00 2018-04-23 stsp {
427 bff6ca00 2018-04-23 stsp struct got_commit_object *commit;
428 bff6ca00 2018-04-23 stsp
429 bff6ca00 2018-04-23 stsp commit = calloc(1, sizeof(*commit));
430 bff6ca00 2018-04-23 stsp if (commit == NULL)
431 bff6ca00 2018-04-23 stsp return NULL;
432 bff6ca00 2018-04-23 stsp commit->tree_id = calloc(1, sizeof(*commit->tree_id));
433 bff6ca00 2018-04-23 stsp if (commit->tree_id == NULL) {
434 bff6ca00 2018-04-23 stsp free(commit);
435 bff6ca00 2018-04-23 stsp return NULL;
436 bff6ca00 2018-04-23 stsp }
437 bff6ca00 2018-04-23 stsp
438 bff6ca00 2018-04-23 stsp SIMPLEQ_INIT(&commit->parent_ids);
439 bff6ca00 2018-04-23 stsp
440 bff6ca00 2018-04-23 stsp return commit;
441 bff6ca00 2018-04-23 stsp }
442 bff6ca00 2018-04-23 stsp
443 bff6ca00 2018-04-23 stsp const struct got_error *
444 bff6ca00 2018-04-23 stsp got_object_commit_add_parent(struct got_commit_object *commit,
445 bff6ca00 2018-04-23 stsp const char *id_str)
446 bff6ca00 2018-04-23 stsp {
447 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
448 bff6ca00 2018-04-23 stsp struct got_parent_id *pid;
449 bff6ca00 2018-04-23 stsp
450 bff6ca00 2018-04-23 stsp pid = calloc(1, sizeof(*pid));
451 bff6ca00 2018-04-23 stsp if (pid == NULL)
452 bff6ca00 2018-04-23 stsp return got_error_from_errno();
453 bff6ca00 2018-04-23 stsp
454 bff6ca00 2018-04-23 stsp pid->id = calloc(1, sizeof(*pid->id));
455 bff6ca00 2018-04-23 stsp if (pid->id == NULL) {
456 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
457 bff6ca00 2018-04-23 stsp free(pid);
458 bff6ca00 2018-04-23 stsp return err;
459 bff6ca00 2018-04-23 stsp }
460 bff6ca00 2018-04-23 stsp
461 bff6ca00 2018-04-23 stsp if (!got_parse_sha1_digest(pid->id->sha1, id_str)) {
462 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
463 bff6ca00 2018-04-23 stsp free(pid->id);
464 bff6ca00 2018-04-23 stsp free(pid);
465 bff6ca00 2018-04-23 stsp return err;
466 bff6ca00 2018-04-23 stsp }
467 bff6ca00 2018-04-23 stsp
468 bff6ca00 2018-04-23 stsp SIMPLEQ_INSERT_TAIL(&commit->parent_ids, pid, entry);
469 bff6ca00 2018-04-23 stsp commit->nparents++;
470 bff6ca00 2018-04-23 stsp
471 bff6ca00 2018-04-23 stsp return NULL;
472 d1cda826 2017-11-06 stsp }
473 d1cda826 2017-11-06 stsp
474 d1cda826 2017-11-06 stsp static const struct got_error *
475 d1cda826 2017-11-06 stsp parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
476 d1cda826 2017-11-06 stsp {
477 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
478 d1cda826 2017-11-06 stsp char *s = buf;
479 d1cda826 2017-11-06 stsp size_t tlen;
480 d1cda826 2017-11-06 stsp ssize_t remain = (ssize_t)len;
481 d1cda826 2017-11-06 stsp
482 bff6ca00 2018-04-23 stsp *commit = got_object_commit_alloc_partial();
483 d1cda826 2017-11-06 stsp if (*commit == NULL)
484 0a585a0d 2018-03-17 stsp return got_error_from_errno();
485 d1cda826 2017-11-06 stsp
486 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
487 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
488 d1cda826 2017-11-06 stsp remain -= tlen;
489 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
490 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
491 d1cda826 2017-11-06 stsp goto done;
492 d1cda826 2017-11-06 stsp }
493 d1cda826 2017-11-06 stsp s += tlen;
494 59ece79d 2018-02-12 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
495 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
496 d1cda826 2017-11-06 stsp goto done;
497 d1cda826 2017-11-06 stsp }
498 d1cda826 2017-11-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
499 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
500 d1cda826 2017-11-06 stsp } else {
501 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
502 d1cda826 2017-11-06 stsp goto done;
503 d1cda826 2017-11-06 stsp }
504 d1cda826 2017-11-06 stsp
505 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
506 d1cda826 2017-11-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
507 d1cda826 2017-11-06 stsp remain -= tlen;
508 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
509 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
510 d1cda826 2017-11-06 stsp goto done;
511 ef0981d5 2018-02-12 stsp }
512 59ece79d 2018-02-12 stsp s += tlen;
513 bff6ca00 2018-04-23 stsp err = got_object_commit_add_parent(*commit, s);
514 bff6ca00 2018-04-23 stsp if (err)
515 d1cda826 2017-11-06 stsp goto done;
516 d1cda826 2017-11-06 stsp
517 eb651edf 2018-02-11 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
518 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
519 d1cda826 2017-11-06 stsp }
520 d1cda826 2017-11-06 stsp
521 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
522 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
523 d1cda826 2017-11-06 stsp char *p;
524 d1cda826 2017-11-06 stsp
525 d1cda826 2017-11-06 stsp remain -= tlen;
526 d1cda826 2017-11-06 stsp if (remain <= 0) {
527 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
528 d1cda826 2017-11-06 stsp goto done;
529 d1cda826 2017-11-06 stsp }
530 d1cda826 2017-11-06 stsp s += tlen;
531 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
532 d1cda826 2017-11-06 stsp if (p == NULL) {
533 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
534 d1cda826 2017-11-06 stsp goto done;
535 d1cda826 2017-11-06 stsp }
536 d1cda826 2017-11-06 stsp *p = '\0';
537 d1cda826 2017-11-06 stsp (*commit)->author = strdup(s);
538 d1cda826 2017-11-06 stsp if ((*commit)->author == NULL) {
539 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
540 d1cda826 2017-11-06 stsp goto done;
541 d1cda826 2017-11-06 stsp }
542 d1cda826 2017-11-06 stsp s += strlen((*commit)->author) + 1;
543 eb651edf 2018-02-11 stsp remain -= strlen((*commit)->author) + 1;
544 d1cda826 2017-11-06 stsp }
545 d1cda826 2017-11-06 stsp
546 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
547 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
548 d1cda826 2017-11-06 stsp char *p;
549 d1cda826 2017-11-06 stsp
550 d1cda826 2017-11-06 stsp remain -= tlen;
551 d1cda826 2017-11-06 stsp if (remain <= 0) {
552 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
553 d1cda826 2017-11-06 stsp goto done;
554 d1cda826 2017-11-06 stsp }
555 d1cda826 2017-11-06 stsp s += tlen;
556 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
557 d1cda826 2017-11-06 stsp if (p == NULL) {
558 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
559 d1cda826 2017-11-06 stsp goto done;
560 d1cda826 2017-11-06 stsp }
561 d1cda826 2017-11-06 stsp *p = '\0';
562 d1cda826 2017-11-06 stsp (*commit)->committer = strdup(s);
563 d1cda826 2017-11-06 stsp if ((*commit)->committer == NULL) {
564 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
565 d1cda826 2017-11-06 stsp goto done;
566 d1cda826 2017-11-06 stsp }
567 d1cda826 2017-11-06 stsp s += strlen((*commit)->committer) + 1;
568 eb651edf 2018-02-11 stsp remain -= strlen((*commit)->committer) + 1;
569 d1cda826 2017-11-06 stsp }
570 d1cda826 2017-11-06 stsp
571 eb651edf 2018-02-11 stsp (*commit)->logmsg = strndup(s, remain);
572 eb651edf 2018-02-11 stsp if ((*commit)->logmsg == NULL) {
573 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
574 eb651edf 2018-02-11 stsp goto done;
575 eb651edf 2018-02-11 stsp }
576 d1cda826 2017-11-06 stsp done:
577 59ece79d 2018-02-12 stsp if (err) {
578 d1cda826 2017-11-06 stsp got_object_commit_close(*commit);
579 59ece79d 2018-02-12 stsp *commit = NULL;
580 59ece79d 2018-02-12 stsp }
581 0ffeb3c2 2017-11-26 stsp return err;
582 0ffeb3c2 2017-11-26 stsp }
583 6e790f45 2018-06-10 stsp
584 6e790f45 2018-06-10 stsp static const struct got_error *
585 6e790f45 2018-06-10 stsp parse_commit_time(time_t *time, const char *author_str)
586 6e790f45 2018-06-10 stsp {
587 6e790f45 2018-06-10 stsp const struct got_error *err = NULL;
588 6e790f45 2018-06-10 stsp const char *errstr;
589 6e790f45 2018-06-10 stsp char *committer, *space;
590 0ffeb3c2 2017-11-26 stsp
591 6e790f45 2018-06-10 stsp *time = 0;
592 6e790f45 2018-06-10 stsp
593 6e790f45 2018-06-10 stsp committer = strdup(author_str);
594 6e790f45 2018-06-10 stsp if (committer == NULL)
595 6e790f45 2018-06-10 stsp return got_error_from_errno();
596 6e790f45 2018-06-10 stsp
597 6e790f45 2018-06-10 stsp /* Strip off trailing timezone indicator. */
598 6e790f45 2018-06-10 stsp space = strrchr(committer, ' ');
599 6e790f45 2018-06-10 stsp if (space == NULL) {
600 6e790f45 2018-06-10 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
601 6e790f45 2018-06-10 stsp goto done;
602 6e790f45 2018-06-10 stsp }
603 6e790f45 2018-06-10 stsp *space = '\0';
604 6e790f45 2018-06-10 stsp
605 6e790f45 2018-06-10 stsp /* Timestamp is separated from committer name + email by space. */
606 6e790f45 2018-06-10 stsp space = strrchr(committer, ' ');
607 6e790f45 2018-06-10 stsp if (space == NULL) {
608 6e790f45 2018-06-10 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
609 6e790f45 2018-06-10 stsp goto done;
610 6e790f45 2018-06-10 stsp }
611 6e790f45 2018-06-10 stsp
612 6e790f45 2018-06-10 stsp *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
613 6e790f45 2018-06-10 stsp if (errstr)
614 6e790f45 2018-06-10 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
615 6e790f45 2018-06-10 stsp
616 6e790f45 2018-06-10 stsp done:
617 6e790f45 2018-06-10 stsp free(committer);
618 6e790f45 2018-06-10 stsp return err;
619 6e790f45 2018-06-10 stsp }
620 6e790f45 2018-06-10 stsp
621 6e790f45 2018-06-10 stsp const struct got_error *
622 6e790f45 2018-06-10 stsp got_object_commit_get_committer_time(time_t *time,
623 6e790f45 2018-06-10 stsp struct got_commit_object *commit)
624 6e790f45 2018-06-10 stsp {
625 6e790f45 2018-06-10 stsp return parse_commit_time(time, commit->committer);
626 6e790f45 2018-06-10 stsp }
627 6e790f45 2018-06-10 stsp
628 6e790f45 2018-06-10 stsp const struct got_error *
629 6e790f45 2018-06-10 stsp got_object_commit_get_author_time(time_t *time,
630 6e790f45 2018-06-10 stsp struct got_commit_object *commit)
631 6e790f45 2018-06-10 stsp {
632 6e790f45 2018-06-10 stsp return parse_commit_time(time, commit->committer);
633 6e790f45 2018-06-10 stsp }
634 6e790f45 2018-06-10 stsp
635 0ffeb3c2 2017-11-26 stsp static void
636 0ffeb3c2 2017-11-26 stsp tree_entry_close(struct got_tree_entry *te)
637 0ffeb3c2 2017-11-26 stsp {
638 59ece79d 2018-02-12 stsp free(te->id);
639 0ffeb3c2 2017-11-26 stsp free(te->name);
640 0ffeb3c2 2017-11-26 stsp free(te);
641 0ffeb3c2 2017-11-26 stsp }
642 0ffeb3c2 2017-11-26 stsp
643 e033d803 2018-04-23 stsp struct got_tree_entry *
644 e033d803 2018-04-23 stsp got_alloc_tree_entry_partial(void)
645 e033d803 2018-04-23 stsp {
646 e033d803 2018-04-23 stsp struct got_tree_entry *te;
647 e033d803 2018-04-23 stsp
648 e033d803 2018-04-23 stsp te = calloc(1, sizeof(*te));
649 e033d803 2018-04-23 stsp if (te == NULL)
650 e033d803 2018-04-23 stsp return NULL;
651 e033d803 2018-04-23 stsp
652 e033d803 2018-04-23 stsp te->id = calloc(1, sizeof(*te->id));
653 e033d803 2018-04-23 stsp if (te->id == NULL) {
654 e033d803 2018-04-23 stsp free(te);
655 e033d803 2018-04-23 stsp te = NULL;
656 e033d803 2018-04-23 stsp }
657 e033d803 2018-04-23 stsp return te;
658 e033d803 2018-04-23 stsp }
659 e033d803 2018-04-23 stsp
660 0ffeb3c2 2017-11-26 stsp static const struct got_error *
661 0ffeb3c2 2017-11-26 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
662 0ffeb3c2 2017-11-26 stsp size_t maxlen)
663 0ffeb3c2 2017-11-26 stsp {
664 0ffeb3c2 2017-11-26 stsp char *p = buf, *space;
665 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
666 0ffeb3c2 2017-11-26 stsp
667 e033d803 2018-04-23 stsp *te = got_alloc_tree_entry_partial();
668 0ffeb3c2 2017-11-26 stsp if (*te == NULL)
669 0a585a0d 2018-03-17 stsp return got_error_from_errno();
670 59ece79d 2018-02-12 stsp
671 0ffeb3c2 2017-11-26 stsp *elen = strlen(buf) + 1;
672 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen) {
673 0ffeb3c2 2017-11-26 stsp free(*te);
674 59ece79d 2018-02-12 stsp *te = NULL;
675 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
676 0ffeb3c2 2017-11-26 stsp }
677 0ffeb3c2 2017-11-26 stsp
678 0ffeb3c2 2017-11-26 stsp space = strchr(buf, ' ');
679 0ffeb3c2 2017-11-26 stsp if (space == NULL) {
680 0a585a0d 2018-03-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
681 0ffeb3c2 2017-11-26 stsp free(*te);
682 59ece79d 2018-02-12 stsp *te = NULL;
683 0a585a0d 2018-03-17 stsp return err;
684 0ffeb3c2 2017-11-26 stsp }
685 0ffeb3c2 2017-11-26 stsp while (*p != ' ') {
686 0ffeb3c2 2017-11-26 stsp if (*p < '0' && *p > '7') {
687 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
688 0ffeb3c2 2017-11-26 stsp goto done;
689 0ffeb3c2 2017-11-26 stsp }
690 0ffeb3c2 2017-11-26 stsp (*te)->mode <<= 3;
691 0ffeb3c2 2017-11-26 stsp (*te)->mode |= *p - '0';
692 0ffeb3c2 2017-11-26 stsp p++;
693 0ffeb3c2 2017-11-26 stsp }
694 0ffeb3c2 2017-11-26 stsp
695 0ffeb3c2 2017-11-26 stsp (*te)->name = strdup(space + 1);
696 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
697 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
698 0ffeb3c2 2017-11-26 stsp goto done;
699 0ffeb3c2 2017-11-26 stsp }
700 0ffeb3c2 2017-11-26 stsp buf += strlen(buf) + 1;
701 59ece79d 2018-02-12 stsp memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
702 0ffeb3c2 2017-11-26 stsp *elen += SHA1_DIGEST_LENGTH;
703 0ffeb3c2 2017-11-26 stsp done:
704 59ece79d 2018-02-12 stsp if (err) {
705 0ffeb3c2 2017-11-26 stsp tree_entry_close(*te);
706 59ece79d 2018-02-12 stsp *te = NULL;
707 59ece79d 2018-02-12 stsp }
708 d1cda826 2017-11-06 stsp return err;
709 d1cda826 2017-11-06 stsp }
710 d1cda826 2017-11-06 stsp
711 d1cda826 2017-11-06 stsp static const struct got_error *
712 e033d803 2018-04-23 stsp parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
713 0ffeb3c2 2017-11-26 stsp {
714 90356acc 2018-01-27 stsp const struct got_error *err;
715 0ffeb3c2 2017-11-26 stsp size_t remain = len;
716 0ffeb3c2 2017-11-26 stsp
717 0ffeb3c2 2017-11-26 stsp *tree = calloc(1, sizeof(**tree));
718 0ffeb3c2 2017-11-26 stsp if (*tree == NULL)
719 0a585a0d 2018-03-17 stsp return got_error_from_errno();
720 0ffeb3c2 2017-11-26 stsp
721 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INIT(&(*tree)->entries);
722 0ffeb3c2 2017-11-26 stsp
723 0ffeb3c2 2017-11-26 stsp while (remain > 0) {
724 0ffeb3c2 2017-11-26 stsp struct got_tree_entry *te;
725 0ffeb3c2 2017-11-26 stsp size_t elen;
726 0ffeb3c2 2017-11-26 stsp
727 90356acc 2018-01-27 stsp err = parse_tree_entry(&te, &elen, buf, remain);
728 90356acc 2018-01-27 stsp if (err)
729 90356acc 2018-01-27 stsp return err;
730 0ffeb3c2 2017-11-26 stsp (*tree)->nentries++;
731 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
732 0ffeb3c2 2017-11-26 stsp buf += elen;
733 0ffeb3c2 2017-11-26 stsp remain -= elen;
734 0ffeb3c2 2017-11-26 stsp }
735 0ffeb3c2 2017-11-26 stsp
736 0ffeb3c2 2017-11-26 stsp if (remain != 0) {
737 0ffeb3c2 2017-11-26 stsp got_object_tree_close(*tree);
738 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
739 0ffeb3c2 2017-11-26 stsp }
740 0ffeb3c2 2017-11-26 stsp
741 0ffeb3c2 2017-11-26 stsp return NULL;
742 0ffeb3c2 2017-11-26 stsp }
743 0ffeb3c2 2017-11-26 stsp
744 0ffeb3c2 2017-11-26 stsp static const struct got_error *
745 eb651edf 2018-02-11 stsp read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
746 eb651edf 2018-02-11 stsp {
747 eb651edf 2018-02-11 stsp const struct got_error *err = NULL;
748 eb651edf 2018-02-11 stsp static const size_t blocksize = 512;
749 eb651edf 2018-02-11 stsp size_t n, total, remain;
750 eb651edf 2018-02-11 stsp uint8_t *buf;
751 eb651edf 2018-02-11 stsp
752 eb651edf 2018-02-11 stsp *outbuf = NULL;
753 eb651edf 2018-02-11 stsp *outlen = 0;
754 eb651edf 2018-02-11 stsp
755 eb651edf 2018-02-11 stsp buf = calloc(1, blocksize);
756 eb651edf 2018-02-11 stsp if (buf == NULL)
757 0a585a0d 2018-03-17 stsp return got_error_from_errno();
758 eb651edf 2018-02-11 stsp
759 eb651edf 2018-02-11 stsp remain = blocksize;
760 eb651edf 2018-02-11 stsp total = 0;
761 eb651edf 2018-02-11 stsp while (1) {
762 eb651edf 2018-02-11 stsp if (remain == 0) {
763 eb651edf 2018-02-11 stsp uint8_t *newbuf;
764 eb651edf 2018-02-11 stsp newbuf = reallocarray(buf, 1, total + blocksize);
765 eb651edf 2018-02-11 stsp if (newbuf == NULL) {
766 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
767 eb651edf 2018-02-11 stsp goto done;
768 eb651edf 2018-02-11 stsp }
769 eb651edf 2018-02-11 stsp buf = newbuf;
770 eb651edf 2018-02-11 stsp remain += blocksize;
771 eb651edf 2018-02-11 stsp }
772 be89e2b1 2018-03-03 stsp n = fread(buf + total, 1, remain, f);
773 eb651edf 2018-02-11 stsp if (n == 0) {
774 eb651edf 2018-02-11 stsp if (ferror(f)) {
775 eb651edf 2018-02-11 stsp err = got_ferror(f, GOT_ERR_IO);
776 eb651edf 2018-02-11 stsp goto done;
777 eb651edf 2018-02-11 stsp }
778 eb651edf 2018-02-11 stsp break; /* EOF */
779 eb651edf 2018-02-11 stsp }
780 eb651edf 2018-02-11 stsp remain -= n;
781 eb651edf 2018-02-11 stsp total += n;
782 eb651edf 2018-02-11 stsp };
783 eb651edf 2018-02-11 stsp
784 eb651edf 2018-02-11 stsp done:
785 eb651edf 2018-02-11 stsp if (err == NULL) {
786 eb651edf 2018-02-11 stsp *outbuf = buf;
787 eb651edf 2018-02-11 stsp *outlen = total;
788 eb651edf 2018-02-11 stsp } else
789 eb651edf 2018-02-11 stsp free(buf);
790 eb651edf 2018-02-11 stsp return err;
791 eb651edf 2018-02-11 stsp }
792 eb651edf 2018-02-11 stsp
793 eb651edf 2018-02-11 stsp static const struct got_error *
794 bff6ca00 2018-04-23 stsp read_commit_object(struct got_commit_object **commit, struct got_object *obj,
795 bff6ca00 2018-04-23 stsp FILE *f)
796 d1cda826 2017-11-06 stsp {
797 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
798 d1cda826 2017-11-06 stsp size_t len;
799 eb651edf 2018-02-11 stsp uint8_t *p;
800 d1cda826 2017-11-06 stsp
801 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
802 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
803 eb651edf 2018-02-11 stsp else
804 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
805 4558fcd4 2018-01-14 stsp if (err)
806 d1cda826 2017-11-06 stsp return err;
807 d1cda826 2017-11-06 stsp
808 d1cda826 2017-11-06 stsp if (len < obj->hdrlen + obj->size) {
809 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
810 d1cda826 2017-11-06 stsp goto done;
811 d1cda826 2017-11-06 stsp }
812 d1cda826 2017-11-06 stsp
813 d1cda826 2017-11-06 stsp /* Skip object header. */
814 d1cda826 2017-11-06 stsp len -= obj->hdrlen;
815 eb651edf 2018-02-11 stsp err = parse_commit_object(commit, p + obj->hdrlen, len);
816 eb651edf 2018-02-11 stsp free(p);
817 d1cda826 2017-11-06 stsp done:
818 d1cda826 2017-11-06 stsp return err;
819 d1cda826 2017-11-06 stsp }
820 d1cda826 2017-11-06 stsp
821 bff6ca00 2018-04-23 stsp static void
822 bff6ca00 2018-04-23 stsp read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
823 bff6ca00 2018-04-23 stsp int imsg_fds[2])
824 bff6ca00 2018-04-23 stsp {
825 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
826 bff6ca00 2018-04-23 stsp struct got_commit_object *commit = NULL;
827 bff6ca00 2018-04-23 stsp struct imsgbuf ibuf;
828 bff6ca00 2018-04-23 stsp FILE *f = NULL;
829 bff6ca00 2018-04-23 stsp int status = 0;
830 bff6ca00 2018-04-23 stsp
831 be37c2e6 2018-04-24 stsp setproctitle("read commit object");
832 bff6ca00 2018-04-23 stsp close(imsg_fds[0]);
833 bff6ca00 2018-04-23 stsp imsg_init(&ibuf, imsg_fds[1]);
834 bff6ca00 2018-04-23 stsp
835 bff6ca00 2018-04-23 stsp /* revoke access to most system calls */
836 bff6ca00 2018-04-23 stsp if (pledge("stdio", NULL) == -1) {
837 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
838 bff6ca00 2018-04-23 stsp goto done;
839 bff6ca00 2018-04-23 stsp }
840 bff6ca00 2018-04-23 stsp
841 bff6ca00 2018-04-23 stsp f = fdopen(obj_fd, "rb");
842 bff6ca00 2018-04-23 stsp if (f == NULL) {
843 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
844 bff6ca00 2018-04-23 stsp close(obj_fd);
845 bff6ca00 2018-04-23 stsp goto done;
846 bff6ca00 2018-04-23 stsp }
847 bff6ca00 2018-04-23 stsp
848 bff6ca00 2018-04-23 stsp err = read_commit_object(&commit, obj, f);
849 bff6ca00 2018-04-23 stsp if (err)
850 bff6ca00 2018-04-23 stsp goto done;
851 bff6ca00 2018-04-23 stsp
852 068fd2bf 2018-04-24 stsp err = got_privsep_send_commit(&ibuf, commit);
853 bff6ca00 2018-04-23 stsp done:
854 bff6ca00 2018-04-23 stsp if (commit)
855 bff6ca00 2018-04-23 stsp got_object_commit_close(commit);
856 bff6ca00 2018-04-23 stsp if (err) {
857 bff6ca00 2018-04-23 stsp got_privsep_send_error(&ibuf, err);
858 bff6ca00 2018-04-23 stsp status = 1;
859 bff6ca00 2018-04-23 stsp }
860 bff6ca00 2018-04-23 stsp if (f)
861 bff6ca00 2018-04-23 stsp fclose(f);
862 bff6ca00 2018-04-23 stsp imsg_clear(&ibuf);
863 bff6ca00 2018-04-23 stsp close(imsg_fds[1]);
864 bff6ca00 2018-04-23 stsp _exit(status);
865 bff6ca00 2018-04-23 stsp }
866 bff6ca00 2018-04-23 stsp
867 bff6ca00 2018-04-23 stsp static const struct got_error *
868 bff6ca00 2018-04-23 stsp read_commit_object_privsep(struct got_commit_object **commit,
869 bff6ca00 2018-04-23 stsp struct got_repository *repo, struct got_object *obj, int fd)
870 bff6ca00 2018-04-23 stsp {
871 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
872 bff6ca00 2018-04-23 stsp struct imsgbuf parent_ibuf;
873 bff6ca00 2018-04-23 stsp int imsg_fds[2];
874 bff6ca00 2018-04-23 stsp pid_t pid;
875 bff6ca00 2018-04-23 stsp
876 bff6ca00 2018-04-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
877 bff6ca00 2018-04-23 stsp return got_error_from_errno();
878 bff6ca00 2018-04-23 stsp
879 bff6ca00 2018-04-23 stsp pid = fork();
880 bff6ca00 2018-04-23 stsp if (pid == -1)
881 bff6ca00 2018-04-23 stsp return got_error_from_errno();
882 bff6ca00 2018-04-23 stsp else if (pid == 0) {
883 bff6ca00 2018-04-23 stsp read_commit_object_privsep_child(obj, fd, imsg_fds);
884 e506bf32 2018-04-23 stsp /* not reached */
885 bff6ca00 2018-04-23 stsp }
886 bff6ca00 2018-04-23 stsp
887 bff6ca00 2018-04-23 stsp close(imsg_fds[1]);
888 bff6ca00 2018-04-23 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
889 068fd2bf 2018-04-24 stsp err = got_privsep_recv_commit(commit, &parent_ibuf);
890 bff6ca00 2018-04-23 stsp imsg_clear(&parent_ibuf);
891 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
892 bff6ca00 2018-04-23 stsp close(imsg_fds[0]);
893 b419fc47 2018-04-26 stsp return err ? err : err_child;
894 bff6ca00 2018-04-23 stsp }
895 bff6ca00 2018-04-23 stsp
896 d1cda826 2017-11-06 stsp const struct got_error *
897 d1cda826 2017-11-06 stsp got_object_commit_open(struct got_commit_object **commit,
898 d1cda826 2017-11-06 stsp struct got_repository *repo, struct got_object *obj)
899 d1cda826 2017-11-06 stsp {
900 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
901 d1cda826 2017-11-06 stsp
902 d1cda826 2017-11-06 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
903 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_OBJ_TYPE);
904 d1cda826 2017-11-06 stsp
905 ea35256b 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
906 ea35256b 2018-03-16 stsp uint8_t *buf;
907 ea35256b 2018-03-16 stsp size_t len;
908 ea35256b 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
909 ea35256b 2018-03-16 stsp if (err)
910 ea35256b 2018-03-16 stsp return err;
911 b29656e2 2018-03-16 stsp obj->size = len;
912 b29656e2 2018-03-16 stsp err = parse_commit_object(commit, buf, len);
913 ea35256b 2018-03-16 stsp free(buf);
914 ea35256b 2018-03-16 stsp } else {
915 d5003b79 2018-04-22 stsp int fd;
916 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
917 ea35256b 2018-03-16 stsp if (err)
918 ea35256b 2018-03-16 stsp return err;
919 bff6ca00 2018-04-23 stsp err = read_commit_object_privsep(commit, repo, obj, fd);
920 bff6ca00 2018-04-23 stsp close(fd);
921 ea35256b 2018-03-16 stsp }
922 d1cda826 2017-11-06 stsp return err;
923 d1cda826 2017-11-06 stsp }
924 d1cda826 2017-11-06 stsp
925 d1cda826 2017-11-06 stsp void
926 d1cda826 2017-11-06 stsp got_object_commit_close(struct got_commit_object *commit)
927 d1cda826 2017-11-06 stsp {
928 d1cda826 2017-11-06 stsp struct got_parent_id *pid;
929 d1cda826 2017-11-06 stsp
930 d1cda826 2017-11-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
931 d1cda826 2017-11-06 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
932 d1cda826 2017-11-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
933 59ece79d 2018-02-12 stsp free(pid->id);
934 d1cda826 2017-11-06 stsp free(pid);
935 d1cda826 2017-11-06 stsp }
936 d1cda826 2017-11-06 stsp
937 59ece79d 2018-02-12 stsp free(commit->tree_id);
938 d1cda826 2017-11-06 stsp free(commit->author);
939 d1cda826 2017-11-06 stsp free(commit->committer);
940 d1cda826 2017-11-06 stsp free(commit->logmsg);
941 d1cda826 2017-11-06 stsp free(commit);
942 d1cda826 2017-11-06 stsp }
943 0ffeb3c2 2017-11-26 stsp
944 0ffeb3c2 2017-11-26 stsp static const struct got_error *
945 e033d803 2018-04-23 stsp read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
946 0ffeb3c2 2017-11-26 stsp {
947 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
948 0ffeb3c2 2017-11-26 stsp size_t len;
949 eb651edf 2018-02-11 stsp uint8_t *p;
950 0ffeb3c2 2017-11-26 stsp
951 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
952 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
953 eb651edf 2018-02-11 stsp else
954 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
955 4558fcd4 2018-01-14 stsp if (err)
956 0ffeb3c2 2017-11-26 stsp return err;
957 0ffeb3c2 2017-11-26 stsp
958 0ffeb3c2 2017-11-26 stsp if (len < obj->hdrlen + obj->size) {
959 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
960 0ffeb3c2 2017-11-26 stsp goto done;
961 0ffeb3c2 2017-11-26 stsp }
962 0ffeb3c2 2017-11-26 stsp
963 0ffeb3c2 2017-11-26 stsp /* Skip object header. */
964 0ffeb3c2 2017-11-26 stsp len -= obj->hdrlen;
965 e033d803 2018-04-23 stsp err = parse_tree_object(tree, p + obj->hdrlen, len);
966 eb651edf 2018-02-11 stsp free(p);
967 e033d803 2018-04-23 stsp done:
968 e033d803 2018-04-23 stsp return err;
969 e033d803 2018-04-23 stsp }
970 e033d803 2018-04-23 stsp
971 e033d803 2018-04-23 stsp static void
972 e033d803 2018-04-23 stsp read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
973 e033d803 2018-04-23 stsp int imsg_fds[2])
974 e033d803 2018-04-23 stsp {
975 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
976 e033d803 2018-04-23 stsp struct got_tree_object *tree = NULL;
977 e033d803 2018-04-23 stsp struct imsgbuf ibuf;
978 e033d803 2018-04-23 stsp FILE *f = NULL;
979 e033d803 2018-04-23 stsp int status = 0;
980 e033d803 2018-04-23 stsp
981 be37c2e6 2018-04-24 stsp setproctitle("read tree object");
982 e033d803 2018-04-23 stsp close(imsg_fds[0]);
983 e033d803 2018-04-23 stsp imsg_init(&ibuf, imsg_fds[1]);
984 e033d803 2018-04-23 stsp
985 e033d803 2018-04-23 stsp /* revoke access to most system calls */
986 e033d803 2018-04-23 stsp if (pledge("stdio", NULL) == -1) {
987 e033d803 2018-04-23 stsp err = got_error_from_errno();
988 e033d803 2018-04-23 stsp goto done;
989 e033d803 2018-04-23 stsp }
990 e033d803 2018-04-23 stsp
991 e033d803 2018-04-23 stsp f = fdopen(obj_fd, "rb");
992 e033d803 2018-04-23 stsp if (f == NULL) {
993 e033d803 2018-04-23 stsp err = got_error_from_errno();
994 e033d803 2018-04-23 stsp close(obj_fd);
995 e033d803 2018-04-23 stsp goto done;
996 e033d803 2018-04-23 stsp }
997 e033d803 2018-04-23 stsp
998 e033d803 2018-04-23 stsp err = read_tree_object(&tree, obj, f);
999 e033d803 2018-04-23 stsp if (err)
1000 e033d803 2018-04-23 stsp goto done;
1001 e033d803 2018-04-23 stsp
1002 068fd2bf 2018-04-24 stsp err = got_privsep_send_tree(&ibuf, tree);
1003 0ffeb3c2 2017-11-26 stsp done:
1004 e033d803 2018-04-23 stsp if (tree)
1005 e033d803 2018-04-23 stsp got_object_tree_close(tree);
1006 e033d803 2018-04-23 stsp if (err) {
1007 e033d803 2018-04-23 stsp got_privsep_send_error(&ibuf, err);
1008 e033d803 2018-04-23 stsp status = 1;
1009 e033d803 2018-04-23 stsp }
1010 e033d803 2018-04-23 stsp if (f)
1011 e033d803 2018-04-23 stsp fclose(f);
1012 e033d803 2018-04-23 stsp imsg_clear(&ibuf);
1013 e033d803 2018-04-23 stsp close(imsg_fds[1]);
1014 e033d803 2018-04-23 stsp _exit(status);
1015 e033d803 2018-04-23 stsp }
1016 e033d803 2018-04-23 stsp
1017 e033d803 2018-04-23 stsp static const struct got_error *
1018 e033d803 2018-04-23 stsp read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
1019 e033d803 2018-04-23 stsp int fd)
1020 e033d803 2018-04-23 stsp {
1021 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
1022 e033d803 2018-04-23 stsp struct imsgbuf parent_ibuf;
1023 e033d803 2018-04-23 stsp int imsg_fds[2];
1024 e033d803 2018-04-23 stsp pid_t pid;
1025 e033d803 2018-04-23 stsp
1026 e033d803 2018-04-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1027 e033d803 2018-04-23 stsp return got_error_from_errno();
1028 e033d803 2018-04-23 stsp
1029 e033d803 2018-04-23 stsp pid = fork();
1030 e033d803 2018-04-23 stsp if (pid == -1)
1031 e033d803 2018-04-23 stsp return got_error_from_errno();
1032 e033d803 2018-04-23 stsp else if (pid == 0) {
1033 e033d803 2018-04-23 stsp read_tree_object_privsep_child(obj, fd, imsg_fds);
1034 e033d803 2018-04-23 stsp /* not reached */
1035 e033d803 2018-04-23 stsp }
1036 e033d803 2018-04-23 stsp
1037 e033d803 2018-04-23 stsp close(imsg_fds[1]);
1038 e033d803 2018-04-23 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
1039 068fd2bf 2018-04-24 stsp err = got_privsep_recv_tree(tree, &parent_ibuf);
1040 e033d803 2018-04-23 stsp imsg_clear(&parent_ibuf);
1041 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
1042 e033d803 2018-04-23 stsp close(imsg_fds[0]);
1043 b419fc47 2018-04-26 stsp return err ? err : err_child;
1044 0ffeb3c2 2017-11-26 stsp }
1045 0ffeb3c2 2017-11-26 stsp
1046 0ffeb3c2 2017-11-26 stsp const struct got_error *
1047 0ffeb3c2 2017-11-26 stsp got_object_tree_open(struct got_tree_object **tree,
1048 0ffeb3c2 2017-11-26 stsp struct got_repository *repo, struct got_object *obj)
1049 0ffeb3c2 2017-11-26 stsp {
1050 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
1051 0ffeb3c2 2017-11-26 stsp
1052 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
1053 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
1054 0ffeb3c2 2017-11-26 stsp
1055 e0ab43e7 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1056 e0ab43e7 2018-03-16 stsp uint8_t *buf;
1057 e0ab43e7 2018-03-16 stsp size_t len;
1058 e0ab43e7 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1059 e0ab43e7 2018-03-16 stsp if (err)
1060 e0ab43e7 2018-03-16 stsp return err;
1061 b29656e2 2018-03-16 stsp obj->size = len;
1062 e033d803 2018-04-23 stsp err = parse_tree_object(tree, buf, len);
1063 e0ab43e7 2018-03-16 stsp free(buf);
1064 e0ab43e7 2018-03-16 stsp } else {
1065 d5003b79 2018-04-22 stsp int fd;
1066 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
1067 e0ab43e7 2018-03-16 stsp if (err)
1068 e0ab43e7 2018-03-16 stsp return err;
1069 e033d803 2018-04-23 stsp err = read_tree_object_privsep(tree, obj, fd);
1070 d5003b79 2018-04-22 stsp close(fd);
1071 e0ab43e7 2018-03-16 stsp }
1072 0ffeb3c2 2017-11-26 stsp return err;
1073 0ffeb3c2 2017-11-26 stsp }
1074 0ffeb3c2 2017-11-26 stsp
1075 0ffeb3c2 2017-11-26 stsp void
1076 0ffeb3c2 2017-11-26 stsp got_object_tree_close(struct got_tree_object *tree)
1077 0ffeb3c2 2017-11-26 stsp {
1078 f715ca7f 2017-11-27 stsp struct got_tree_entry *te;
1079 f715ca7f 2017-11-27 stsp
1080 f715ca7f 2017-11-27 stsp while (!SIMPLEQ_EMPTY(&tree->entries)) {
1081 f715ca7f 2017-11-27 stsp te = SIMPLEQ_FIRST(&tree->entries);
1082 f715ca7f 2017-11-27 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
1083 f715ca7f 2017-11-27 stsp tree_entry_close(te);
1084 f715ca7f 2017-11-27 stsp }
1085 f715ca7f 2017-11-27 stsp
1086 f715ca7f 2017-11-27 stsp free(tree);
1087 57efb1af 2018-04-24 stsp }
1088 ff6b18f8 2018-04-24 stsp
1089 ff6b18f8 2018-04-24 stsp static const struct got_error *
1090 ff6b18f8 2018-04-24 stsp read_blob_object_privsep_child(int outfd, int infd, int imsg_fds[2])
1091 ff6b18f8 2018-04-24 stsp {
1092 ff6b18f8 2018-04-24 stsp const struct got_error *err = NULL;
1093 ff6b18f8 2018-04-24 stsp struct imsgbuf ibuf;
1094 ff6b18f8 2018-04-24 stsp int status = 0;
1095 2967a784 2018-04-24 stsp size_t size;
1096 8b2180d4 2018-04-26 stsp FILE *infile = NULL;
1097 ff6b18f8 2018-04-24 stsp
1098 be37c2e6 2018-04-24 stsp setproctitle("read blob object");
1099 ff6b18f8 2018-04-24 stsp close(imsg_fds[0]);
1100 ff6b18f8 2018-04-24 stsp imsg_init(&ibuf, imsg_fds[1]);
1101 57efb1af 2018-04-24 stsp
1102 ff6b18f8 2018-04-24 stsp /* revoke access to most system calls */
1103 ff6b18f8 2018-04-24 stsp if (pledge("stdio", NULL) == -1) {
1104 ff6b18f8 2018-04-24 stsp err = got_error_from_errno();
1105 ff6b18f8 2018-04-24 stsp goto done;
1106 ff6b18f8 2018-04-24 stsp }
1107 ff6b18f8 2018-04-24 stsp
1108 8b2180d4 2018-04-26 stsp infile = fdopen(infd, "rb");
1109 8b2180d4 2018-04-26 stsp if (infile == NULL) {
1110 8b2180d4 2018-04-26 stsp err = got_error_from_errno();
1111 8b2180d4 2018-04-26 stsp close(infd);
1112 8b2180d4 2018-04-26 stsp goto done;
1113 8b2180d4 2018-04-26 stsp }
1114 8b2180d4 2018-04-26 stsp err = got_inflate_to_fd(&size, infile, outfd);
1115 8b2180d4 2018-04-26 stsp fclose(infile);
1116 ff6b18f8 2018-04-24 stsp if (err)
1117 ff6b18f8 2018-04-24 stsp goto done;
1118 ff6b18f8 2018-04-24 stsp
1119 2967a784 2018-04-24 stsp err = got_privsep_send_blob(&ibuf, size);
1120 ff6b18f8 2018-04-24 stsp done:
1121 ff6b18f8 2018-04-24 stsp if (err) {
1122 ff6b18f8 2018-04-24 stsp got_privsep_send_error(&ibuf, err);
1123 ff6b18f8 2018-04-24 stsp status = 1;
1124 ff6b18f8 2018-04-24 stsp }
1125 ff6b18f8 2018-04-24 stsp close(outfd);
1126 ff6b18f8 2018-04-24 stsp imsg_clear(&ibuf);
1127 ff6b18f8 2018-04-24 stsp close(imsg_fds[1]);
1128 ff6b18f8 2018-04-24 stsp _exit(status);
1129 ff6b18f8 2018-04-24 stsp }
1130 ff6b18f8 2018-04-24 stsp
1131 ff6b18f8 2018-04-24 stsp static const struct got_error *
1132 2967a784 2018-04-24 stsp read_blob_object_privsep(size_t *size, int outfd, int infd)
1133 ff6b18f8 2018-04-24 stsp {
1134 ff6b18f8 2018-04-24 stsp struct imsgbuf parent_ibuf;
1135 ff6b18f8 2018-04-24 stsp int imsg_fds[2];
1136 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
1137 ff6b18f8 2018-04-24 stsp pid_t pid;
1138 ff6b18f8 2018-04-24 stsp
1139 ff6b18f8 2018-04-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1140 ff6b18f8 2018-04-24 stsp return got_error_from_errno();
1141 ff6b18f8 2018-04-24 stsp
1142 ff6b18f8 2018-04-24 stsp pid = fork();
1143 ff6b18f8 2018-04-24 stsp if (pid == -1)
1144 ff6b18f8 2018-04-24 stsp return got_error_from_errno();
1145 ff6b18f8 2018-04-24 stsp else if (pid == 0) {
1146 ff6b18f8 2018-04-24 stsp read_blob_object_privsep_child(outfd, infd, imsg_fds);
1147 ff6b18f8 2018-04-24 stsp /* not reached */
1148 ff6b18f8 2018-04-24 stsp }
1149 ff6b18f8 2018-04-24 stsp
1150 ff6b18f8 2018-04-24 stsp close(imsg_fds[1]);
1151 ff6b18f8 2018-04-24 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
1152 2967a784 2018-04-24 stsp err = got_privsep_recv_blob(size, &parent_ibuf);
1153 ff6b18f8 2018-04-24 stsp imsg_clear(&parent_ibuf);
1154 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
1155 ff6b18f8 2018-04-24 stsp close(imsg_fds[0]);
1156 ff6b18f8 2018-04-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1157 ff6b18f8 2018-04-24 stsp err = got_error_from_errno();
1158 b419fc47 2018-04-26 stsp return err ? err : err_child;
1159 ff6b18f8 2018-04-24 stsp }
1160 ff6b18f8 2018-04-24 stsp
1161 68482ea3 2017-11-27 stsp const struct got_error *
1162 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
1163 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1164 68482ea3 2017-11-27 stsp {
1165 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
1166 68482ea3 2017-11-27 stsp
1167 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
1168 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
1169 68482ea3 2017-11-27 stsp
1170 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
1171 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
1172 7d283eee 2017-11-29 stsp
1173 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1174 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1175 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1176 68482ea3 2017-11-27 stsp
1177 15c8b0e6 2018-04-24 stsp (*blob)->read_buf = calloc(1, blocksize);
1178 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1179 15c8b0e6 2018-04-24 stsp err = got_error_from_errno();
1180 c7254d79 2018-04-24 stsp goto done;
1181 15c8b0e6 2018-04-24 stsp }
1182 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1183 eb651edf 2018-02-11 stsp err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1184 c7254d79 2018-04-24 stsp if (err)
1185 c7254d79 2018-04-24 stsp goto done;
1186 eb651edf 2018-02-11 stsp } else {
1187 3aca5731 2018-04-24 stsp int infd, outfd;
1188 2967a784 2018-04-24 stsp size_t size;
1189 2967a784 2018-04-24 stsp struct stat sb;
1190 c7254d79 2018-04-24 stsp
1191 3aca5731 2018-04-24 stsp err = open_loose_object(&infd, obj, repo);
1192 c7254d79 2018-04-24 stsp if (err)
1193 c7254d79 2018-04-24 stsp goto done;
1194 c7254d79 2018-04-24 stsp
1195 3aca5731 2018-04-24 stsp
1196 3aca5731 2018-04-24 stsp outfd = got_opentempfd();
1197 3aca5731 2018-04-24 stsp if (outfd == -1) {
1198 3aca5731 2018-04-24 stsp err = got_error_from_errno();
1199 3aca5731 2018-04-24 stsp close(infd);
1200 3aca5731 2018-04-24 stsp goto done;
1201 3aca5731 2018-04-24 stsp }
1202 3aca5731 2018-04-24 stsp
1203 2967a784 2018-04-24 stsp err = read_blob_object_privsep(&size, outfd, infd);
1204 3aca5731 2018-04-24 stsp close(infd);
1205 57efb1af 2018-04-24 stsp if (err)
1206 c7254d79 2018-04-24 stsp goto done;
1207 3aca5731 2018-04-24 stsp
1208 2967a784 2018-04-24 stsp if (size != obj->hdrlen + obj->size) {
1209 1a6b3ab7 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1210 2967a784 2018-04-24 stsp close(outfd);
1211 2967a784 2018-04-24 stsp goto done;
1212 2967a784 2018-04-24 stsp }
1213 2967a784 2018-04-24 stsp
1214 2967a784 2018-04-24 stsp if (fstat(outfd, &sb) == -1) {
1215 2967a784 2018-04-24 stsp err = got_error_from_errno();
1216 2967a784 2018-04-24 stsp close(outfd);
1217 2967a784 2018-04-24 stsp goto done;
1218 2967a784 2018-04-24 stsp }
1219 2967a784 2018-04-24 stsp
1220 2967a784 2018-04-24 stsp if (sb.st_size != size) {
1221 2967a784 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1222 2967a784 2018-04-24 stsp close(outfd);
1223 2967a784 2018-04-24 stsp goto done;
1224 2967a784 2018-04-24 stsp }
1225 2967a784 2018-04-24 stsp
1226 3aca5731 2018-04-24 stsp (*blob)->f = fdopen(outfd, "rb");
1227 3aca5731 2018-04-24 stsp if ((*blob)->f == NULL) {
1228 3aca5731 2018-04-24 stsp err = got_error_from_errno();
1229 3aca5731 2018-04-24 stsp close(outfd);
1230 3aca5731 2018-04-24 stsp goto done;
1231 3aca5731 2018-04-24 stsp }
1232 68482ea3 2017-11-27 stsp }
1233 68482ea3 2017-11-27 stsp
1234 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
1235 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1236 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1237 7d283eee 2017-11-29 stsp
1238 c7254d79 2018-04-24 stsp done:
1239 c7254d79 2018-04-24 stsp if (err && *blob) {
1240 c7254d79 2018-04-24 stsp if ((*blob)->f)
1241 c7254d79 2018-04-24 stsp fclose((*blob)->f);
1242 c7254d79 2018-04-24 stsp free((*blob)->read_buf);
1243 c7254d79 2018-04-24 stsp free(*blob);
1244 c7254d79 2018-04-24 stsp *blob = NULL;
1245 c7254d79 2018-04-24 stsp }
1246 68482ea3 2017-11-27 stsp return err;
1247 0ffeb3c2 2017-11-26 stsp }
1248 68482ea3 2017-11-27 stsp
1249 68482ea3 2017-11-27 stsp void
1250 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1251 68482ea3 2017-11-27 stsp {
1252 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1253 68482ea3 2017-11-27 stsp fclose(blob->f);
1254 68482ea3 2017-11-27 stsp free(blob);
1255 f934cf2c 2018-02-12 stsp }
1256 f934cf2c 2018-02-12 stsp
1257 f934cf2c 2018-02-12 stsp char *
1258 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1259 f934cf2c 2018-02-12 stsp {
1260 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1261 f934cf2c 2018-02-12 stsp }
1262 f934cf2c 2018-02-12 stsp
1263 f934cf2c 2018-02-12 stsp size_t
1264 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1265 f934cf2c 2018-02-12 stsp {
1266 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1267 68482ea3 2017-11-27 stsp }
1268 68482ea3 2017-11-27 stsp
1269 f934cf2c 2018-02-12 stsp const uint8_t *
1270 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1271 f934cf2c 2018-02-12 stsp {
1272 f934cf2c 2018-02-12 stsp return blob->read_buf;
1273 f934cf2c 2018-02-12 stsp }
1274 f934cf2c 2018-02-12 stsp
1275 68482ea3 2017-11-27 stsp const struct got_error *
1276 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1277 68482ea3 2017-11-27 stsp {
1278 eb651edf 2018-02-11 stsp size_t n;
1279 eb651edf 2018-02-11 stsp
1280 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1281 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1282 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1283 eb651edf 2018-02-11 stsp *outlenp = n;
1284 eb651edf 2018-02-11 stsp return NULL;
1285 68482ea3 2017-11-27 stsp }