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 be6a1b5a 2018-06-11 stsp got_object_open_as_commit(struct got_commit_object **commit,
445 be6a1b5a 2018-06-11 stsp struct got_repository *repo, struct got_object_id *id)
446 be6a1b5a 2018-06-11 stsp {
447 be6a1b5a 2018-06-11 stsp const struct got_error *err;
448 be6a1b5a 2018-06-11 stsp struct got_object *obj;
449 be6a1b5a 2018-06-11 stsp
450 be6a1b5a 2018-06-11 stsp err = got_object_open(&obj, repo, id);
451 be6a1b5a 2018-06-11 stsp if (err)
452 be6a1b5a 2018-06-11 stsp return err;
453 be6a1b5a 2018-06-11 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
454 be6a1b5a 2018-06-11 stsp err = got_error(GOT_ERR_OBJ_TYPE);
455 be6a1b5a 2018-06-11 stsp goto done;
456 be6a1b5a 2018-06-11 stsp }
457 be6a1b5a 2018-06-11 stsp
458 be6a1b5a 2018-06-11 stsp err = got_object_commit_open(commit, repo, obj);
459 be6a1b5a 2018-06-11 stsp done:
460 be6a1b5a 2018-06-11 stsp got_object_close(obj);
461 be6a1b5a 2018-06-11 stsp return err;
462 be6a1b5a 2018-06-11 stsp }
463 be6a1b5a 2018-06-11 stsp
464 be6a1b5a 2018-06-11 stsp const struct got_error *
465 bff6ca00 2018-04-23 stsp got_object_commit_add_parent(struct got_commit_object *commit,
466 bff6ca00 2018-04-23 stsp const char *id_str)
467 bff6ca00 2018-04-23 stsp {
468 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
469 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
470 bff6ca00 2018-04-23 stsp
471 79f35eb3 2018-06-11 stsp qid = calloc(1, sizeof(*qid));
472 79f35eb3 2018-06-11 stsp if (qid == NULL)
473 bff6ca00 2018-04-23 stsp return got_error_from_errno();
474 bff6ca00 2018-04-23 stsp
475 79f35eb3 2018-06-11 stsp qid->id = calloc(1, sizeof(*qid->id));
476 79f35eb3 2018-06-11 stsp if (qid->id == NULL) {
477 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
478 79f35eb3 2018-06-11 stsp free(qid);
479 bff6ca00 2018-04-23 stsp return err;
480 bff6ca00 2018-04-23 stsp }
481 bff6ca00 2018-04-23 stsp
482 79f35eb3 2018-06-11 stsp if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
483 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
484 79f35eb3 2018-06-11 stsp free(qid->id);
485 79f35eb3 2018-06-11 stsp free(qid);
486 bff6ca00 2018-04-23 stsp return err;
487 bff6ca00 2018-04-23 stsp }
488 bff6ca00 2018-04-23 stsp
489 79f35eb3 2018-06-11 stsp SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
490 bff6ca00 2018-04-23 stsp commit->nparents++;
491 4626e416 2018-06-10 stsp
492 4626e416 2018-06-10 stsp return NULL;
493 4626e416 2018-06-10 stsp }
494 4626e416 2018-06-10 stsp
495 4626e416 2018-06-10 stsp static const struct got_error *
496 6c281f94 2018-06-11 stsp parse_commit_time(time_t *time, char **tzoff, char *committer)
497 4626e416 2018-06-10 stsp {
498 4626e416 2018-06-10 stsp const char *errstr;
499 4626e416 2018-06-10 stsp char *space;
500 4626e416 2018-06-10 stsp
501 4626e416 2018-06-10 stsp *time = 0;
502 4626e416 2018-06-10 stsp
503 6c281f94 2018-06-11 stsp /* Parse and then strip trailing timezone indicator. */
504 4626e416 2018-06-10 stsp space = strrchr(committer, ' ');
505 4626e416 2018-06-10 stsp if (space == NULL)
506 4626e416 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
507 6c281f94 2018-06-11 stsp *tzoff = strdup(space + 1);
508 6c281f94 2018-06-11 stsp if (*tzoff == NULL)
509 6c281f94 2018-06-11 stsp return got_error_from_errno();
510 4626e416 2018-06-10 stsp *space = '\0';
511 4626e416 2018-06-10 stsp
512 4626e416 2018-06-10 stsp /* Timestamp is separated from committer name + email by space. */
513 4626e416 2018-06-10 stsp space = strrchr(committer, ' ');
514 4626e416 2018-06-10 stsp if (space == NULL)
515 4626e416 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
516 4626e416 2018-06-10 stsp
517 4626e416 2018-06-10 stsp *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
518 4626e416 2018-06-10 stsp if (errstr)
519 4626e416 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
520 bff6ca00 2018-04-23 stsp
521 4626e416 2018-06-10 stsp /* Strip off parsed time information, leaving just author and email. */
522 4626e416 2018-06-10 stsp *space = '\0';
523 bff6ca00 2018-04-23 stsp return NULL;
524 d1cda826 2017-11-06 stsp }
525 d1cda826 2017-11-06 stsp
526 d1cda826 2017-11-06 stsp static const struct got_error *
527 d1cda826 2017-11-06 stsp parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
528 d1cda826 2017-11-06 stsp {
529 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
530 d1cda826 2017-11-06 stsp char *s = buf;
531 d1cda826 2017-11-06 stsp size_t tlen;
532 d1cda826 2017-11-06 stsp ssize_t remain = (ssize_t)len;
533 d1cda826 2017-11-06 stsp
534 bff6ca00 2018-04-23 stsp *commit = got_object_commit_alloc_partial();
535 d1cda826 2017-11-06 stsp if (*commit == NULL)
536 0a585a0d 2018-03-17 stsp return got_error_from_errno();
537 d1cda826 2017-11-06 stsp
538 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
539 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
540 d1cda826 2017-11-06 stsp remain -= tlen;
541 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
542 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
543 d1cda826 2017-11-06 stsp goto done;
544 d1cda826 2017-11-06 stsp }
545 d1cda826 2017-11-06 stsp s += tlen;
546 59ece79d 2018-02-12 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
547 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
548 d1cda826 2017-11-06 stsp goto done;
549 d1cda826 2017-11-06 stsp }
550 d1cda826 2017-11-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
551 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
552 d1cda826 2017-11-06 stsp } else {
553 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
554 d1cda826 2017-11-06 stsp goto done;
555 d1cda826 2017-11-06 stsp }
556 d1cda826 2017-11-06 stsp
557 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
558 d1cda826 2017-11-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
559 d1cda826 2017-11-06 stsp remain -= tlen;
560 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
561 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
562 d1cda826 2017-11-06 stsp goto done;
563 ef0981d5 2018-02-12 stsp }
564 59ece79d 2018-02-12 stsp s += tlen;
565 bff6ca00 2018-04-23 stsp err = got_object_commit_add_parent(*commit, s);
566 bff6ca00 2018-04-23 stsp if (err)
567 d1cda826 2017-11-06 stsp goto done;
568 d1cda826 2017-11-06 stsp
569 eb651edf 2018-02-11 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
570 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
571 d1cda826 2017-11-06 stsp }
572 d1cda826 2017-11-06 stsp
573 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
574 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
575 d1cda826 2017-11-06 stsp char *p;
576 4626e416 2018-06-10 stsp size_t slen;
577 d1cda826 2017-11-06 stsp
578 d1cda826 2017-11-06 stsp remain -= tlen;
579 d1cda826 2017-11-06 stsp if (remain <= 0) {
580 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
581 d1cda826 2017-11-06 stsp goto done;
582 d1cda826 2017-11-06 stsp }
583 d1cda826 2017-11-06 stsp s += tlen;
584 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
585 d1cda826 2017-11-06 stsp if (p == NULL) {
586 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
587 d1cda826 2017-11-06 stsp goto done;
588 d1cda826 2017-11-06 stsp }
589 d1cda826 2017-11-06 stsp *p = '\0';
590 4626e416 2018-06-10 stsp slen = strlen(s);
591 6c281f94 2018-06-11 stsp err = parse_commit_time(&(*commit)->author_time,
592 6c281f94 2018-06-11 stsp &(*commit)->author_tzoff, s);
593 4626e416 2018-06-10 stsp if (err)
594 4626e416 2018-06-10 stsp goto done;
595 d1cda826 2017-11-06 stsp (*commit)->author = strdup(s);
596 d1cda826 2017-11-06 stsp if ((*commit)->author == NULL) {
597 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
598 d1cda826 2017-11-06 stsp goto done;
599 d1cda826 2017-11-06 stsp }
600 4626e416 2018-06-10 stsp s += slen + 1;
601 4626e416 2018-06-10 stsp remain -= slen + 1;
602 d1cda826 2017-11-06 stsp }
603 d1cda826 2017-11-06 stsp
604 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
605 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
606 d1cda826 2017-11-06 stsp char *p;
607 4626e416 2018-06-10 stsp size_t slen;
608 d1cda826 2017-11-06 stsp
609 d1cda826 2017-11-06 stsp remain -= tlen;
610 d1cda826 2017-11-06 stsp if (remain <= 0) {
611 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
612 d1cda826 2017-11-06 stsp goto done;
613 d1cda826 2017-11-06 stsp }
614 d1cda826 2017-11-06 stsp s += tlen;
615 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
616 d1cda826 2017-11-06 stsp if (p == NULL) {
617 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
618 d1cda826 2017-11-06 stsp goto done;
619 d1cda826 2017-11-06 stsp }
620 d1cda826 2017-11-06 stsp *p = '\0';
621 4626e416 2018-06-10 stsp slen = strlen(s);
622 6c281f94 2018-06-11 stsp err = parse_commit_time(&(*commit)->committer_time,
623 6c281f94 2018-06-11 stsp &(*commit)->committer_tzoff, s);
624 4626e416 2018-06-10 stsp if (err)
625 4626e416 2018-06-10 stsp goto done;
626 d1cda826 2017-11-06 stsp (*commit)->committer = strdup(s);
627 d1cda826 2017-11-06 stsp if ((*commit)->committer == NULL) {
628 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
629 d1cda826 2017-11-06 stsp goto done;
630 d1cda826 2017-11-06 stsp }
631 4626e416 2018-06-10 stsp s += slen + 1;
632 4626e416 2018-06-10 stsp remain -= slen + 1;
633 d1cda826 2017-11-06 stsp }
634 d1cda826 2017-11-06 stsp
635 eb651edf 2018-02-11 stsp (*commit)->logmsg = strndup(s, remain);
636 eb651edf 2018-02-11 stsp if ((*commit)->logmsg == NULL) {
637 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
638 eb651edf 2018-02-11 stsp goto done;
639 eb651edf 2018-02-11 stsp }
640 d1cda826 2017-11-06 stsp done:
641 59ece79d 2018-02-12 stsp if (err) {
642 d1cda826 2017-11-06 stsp got_object_commit_close(*commit);
643 59ece79d 2018-02-12 stsp *commit = NULL;
644 59ece79d 2018-02-12 stsp }
645 0ffeb3c2 2017-11-26 stsp return err;
646 0ffeb3c2 2017-11-26 stsp }
647 6e790f45 2018-06-10 stsp
648 0ffeb3c2 2017-11-26 stsp static void
649 0ffeb3c2 2017-11-26 stsp tree_entry_close(struct got_tree_entry *te)
650 0ffeb3c2 2017-11-26 stsp {
651 59ece79d 2018-02-12 stsp free(te->id);
652 0ffeb3c2 2017-11-26 stsp free(te->name);
653 0ffeb3c2 2017-11-26 stsp free(te);
654 0ffeb3c2 2017-11-26 stsp }
655 0ffeb3c2 2017-11-26 stsp
656 e033d803 2018-04-23 stsp struct got_tree_entry *
657 e033d803 2018-04-23 stsp got_alloc_tree_entry_partial(void)
658 e033d803 2018-04-23 stsp {
659 e033d803 2018-04-23 stsp struct got_tree_entry *te;
660 e033d803 2018-04-23 stsp
661 e033d803 2018-04-23 stsp te = calloc(1, sizeof(*te));
662 e033d803 2018-04-23 stsp if (te == NULL)
663 e033d803 2018-04-23 stsp return NULL;
664 e033d803 2018-04-23 stsp
665 e033d803 2018-04-23 stsp te->id = calloc(1, sizeof(*te->id));
666 e033d803 2018-04-23 stsp if (te->id == NULL) {
667 e033d803 2018-04-23 stsp free(te);
668 e033d803 2018-04-23 stsp te = NULL;
669 e033d803 2018-04-23 stsp }
670 e033d803 2018-04-23 stsp return te;
671 e033d803 2018-04-23 stsp }
672 e033d803 2018-04-23 stsp
673 0ffeb3c2 2017-11-26 stsp static const struct got_error *
674 0ffeb3c2 2017-11-26 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
675 0ffeb3c2 2017-11-26 stsp size_t maxlen)
676 0ffeb3c2 2017-11-26 stsp {
677 0ffeb3c2 2017-11-26 stsp char *p = buf, *space;
678 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
679 0ffeb3c2 2017-11-26 stsp
680 e033d803 2018-04-23 stsp *te = got_alloc_tree_entry_partial();
681 0ffeb3c2 2017-11-26 stsp if (*te == NULL)
682 0a585a0d 2018-03-17 stsp return got_error_from_errno();
683 59ece79d 2018-02-12 stsp
684 0ffeb3c2 2017-11-26 stsp *elen = strlen(buf) + 1;
685 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen) {
686 0ffeb3c2 2017-11-26 stsp free(*te);
687 59ece79d 2018-02-12 stsp *te = NULL;
688 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
689 0ffeb3c2 2017-11-26 stsp }
690 0ffeb3c2 2017-11-26 stsp
691 0ffeb3c2 2017-11-26 stsp space = strchr(buf, ' ');
692 0ffeb3c2 2017-11-26 stsp if (space == NULL) {
693 0a585a0d 2018-03-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
694 0ffeb3c2 2017-11-26 stsp free(*te);
695 59ece79d 2018-02-12 stsp *te = NULL;
696 0a585a0d 2018-03-17 stsp return err;
697 0ffeb3c2 2017-11-26 stsp }
698 0ffeb3c2 2017-11-26 stsp while (*p != ' ') {
699 0ffeb3c2 2017-11-26 stsp if (*p < '0' && *p > '7') {
700 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
701 0ffeb3c2 2017-11-26 stsp goto done;
702 0ffeb3c2 2017-11-26 stsp }
703 0ffeb3c2 2017-11-26 stsp (*te)->mode <<= 3;
704 0ffeb3c2 2017-11-26 stsp (*te)->mode |= *p - '0';
705 0ffeb3c2 2017-11-26 stsp p++;
706 0ffeb3c2 2017-11-26 stsp }
707 0ffeb3c2 2017-11-26 stsp
708 0ffeb3c2 2017-11-26 stsp (*te)->name = strdup(space + 1);
709 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
710 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
711 0ffeb3c2 2017-11-26 stsp goto done;
712 0ffeb3c2 2017-11-26 stsp }
713 0ffeb3c2 2017-11-26 stsp buf += strlen(buf) + 1;
714 59ece79d 2018-02-12 stsp memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
715 0ffeb3c2 2017-11-26 stsp *elen += SHA1_DIGEST_LENGTH;
716 0ffeb3c2 2017-11-26 stsp done:
717 59ece79d 2018-02-12 stsp if (err) {
718 0ffeb3c2 2017-11-26 stsp tree_entry_close(*te);
719 59ece79d 2018-02-12 stsp *te = NULL;
720 59ece79d 2018-02-12 stsp }
721 d1cda826 2017-11-06 stsp return err;
722 d1cda826 2017-11-06 stsp }
723 d1cda826 2017-11-06 stsp
724 d1cda826 2017-11-06 stsp static const struct got_error *
725 e033d803 2018-04-23 stsp parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
726 0ffeb3c2 2017-11-26 stsp {
727 90356acc 2018-01-27 stsp const struct got_error *err;
728 0ffeb3c2 2017-11-26 stsp size_t remain = len;
729 0ffeb3c2 2017-11-26 stsp
730 0ffeb3c2 2017-11-26 stsp *tree = calloc(1, sizeof(**tree));
731 0ffeb3c2 2017-11-26 stsp if (*tree == NULL)
732 0a585a0d 2018-03-17 stsp return got_error_from_errno();
733 0ffeb3c2 2017-11-26 stsp
734 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INIT(&(*tree)->entries);
735 0ffeb3c2 2017-11-26 stsp
736 0ffeb3c2 2017-11-26 stsp while (remain > 0) {
737 0ffeb3c2 2017-11-26 stsp struct got_tree_entry *te;
738 0ffeb3c2 2017-11-26 stsp size_t elen;
739 0ffeb3c2 2017-11-26 stsp
740 90356acc 2018-01-27 stsp err = parse_tree_entry(&te, &elen, buf, remain);
741 90356acc 2018-01-27 stsp if (err)
742 90356acc 2018-01-27 stsp return err;
743 0ffeb3c2 2017-11-26 stsp (*tree)->nentries++;
744 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
745 0ffeb3c2 2017-11-26 stsp buf += elen;
746 0ffeb3c2 2017-11-26 stsp remain -= elen;
747 0ffeb3c2 2017-11-26 stsp }
748 0ffeb3c2 2017-11-26 stsp
749 0ffeb3c2 2017-11-26 stsp if (remain != 0) {
750 0ffeb3c2 2017-11-26 stsp got_object_tree_close(*tree);
751 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
752 0ffeb3c2 2017-11-26 stsp }
753 0ffeb3c2 2017-11-26 stsp
754 0ffeb3c2 2017-11-26 stsp return NULL;
755 0ffeb3c2 2017-11-26 stsp }
756 0ffeb3c2 2017-11-26 stsp
757 0ffeb3c2 2017-11-26 stsp static const struct got_error *
758 eb651edf 2018-02-11 stsp read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
759 eb651edf 2018-02-11 stsp {
760 eb651edf 2018-02-11 stsp const struct got_error *err = NULL;
761 eb651edf 2018-02-11 stsp static const size_t blocksize = 512;
762 eb651edf 2018-02-11 stsp size_t n, total, remain;
763 eb651edf 2018-02-11 stsp uint8_t *buf;
764 eb651edf 2018-02-11 stsp
765 eb651edf 2018-02-11 stsp *outbuf = NULL;
766 eb651edf 2018-02-11 stsp *outlen = 0;
767 eb651edf 2018-02-11 stsp
768 eb651edf 2018-02-11 stsp buf = calloc(1, blocksize);
769 eb651edf 2018-02-11 stsp if (buf == NULL)
770 0a585a0d 2018-03-17 stsp return got_error_from_errno();
771 eb651edf 2018-02-11 stsp
772 eb651edf 2018-02-11 stsp remain = blocksize;
773 eb651edf 2018-02-11 stsp total = 0;
774 eb651edf 2018-02-11 stsp while (1) {
775 eb651edf 2018-02-11 stsp if (remain == 0) {
776 eb651edf 2018-02-11 stsp uint8_t *newbuf;
777 eb651edf 2018-02-11 stsp newbuf = reallocarray(buf, 1, total + blocksize);
778 eb651edf 2018-02-11 stsp if (newbuf == NULL) {
779 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
780 eb651edf 2018-02-11 stsp goto done;
781 eb651edf 2018-02-11 stsp }
782 eb651edf 2018-02-11 stsp buf = newbuf;
783 eb651edf 2018-02-11 stsp remain += blocksize;
784 eb651edf 2018-02-11 stsp }
785 be89e2b1 2018-03-03 stsp n = fread(buf + total, 1, remain, f);
786 eb651edf 2018-02-11 stsp if (n == 0) {
787 eb651edf 2018-02-11 stsp if (ferror(f)) {
788 eb651edf 2018-02-11 stsp err = got_ferror(f, GOT_ERR_IO);
789 eb651edf 2018-02-11 stsp goto done;
790 eb651edf 2018-02-11 stsp }
791 eb651edf 2018-02-11 stsp break; /* EOF */
792 eb651edf 2018-02-11 stsp }
793 eb651edf 2018-02-11 stsp remain -= n;
794 eb651edf 2018-02-11 stsp total += n;
795 eb651edf 2018-02-11 stsp };
796 eb651edf 2018-02-11 stsp
797 eb651edf 2018-02-11 stsp done:
798 eb651edf 2018-02-11 stsp if (err == NULL) {
799 eb651edf 2018-02-11 stsp *outbuf = buf;
800 eb651edf 2018-02-11 stsp *outlen = total;
801 eb651edf 2018-02-11 stsp } else
802 eb651edf 2018-02-11 stsp free(buf);
803 eb651edf 2018-02-11 stsp return err;
804 eb651edf 2018-02-11 stsp }
805 eb651edf 2018-02-11 stsp
806 eb651edf 2018-02-11 stsp static const struct got_error *
807 bff6ca00 2018-04-23 stsp read_commit_object(struct got_commit_object **commit, struct got_object *obj,
808 bff6ca00 2018-04-23 stsp FILE *f)
809 d1cda826 2017-11-06 stsp {
810 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
811 d1cda826 2017-11-06 stsp size_t len;
812 eb651edf 2018-02-11 stsp uint8_t *p;
813 d1cda826 2017-11-06 stsp
814 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
815 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
816 eb651edf 2018-02-11 stsp else
817 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
818 4558fcd4 2018-01-14 stsp if (err)
819 d1cda826 2017-11-06 stsp return err;
820 d1cda826 2017-11-06 stsp
821 d1cda826 2017-11-06 stsp if (len < obj->hdrlen + obj->size) {
822 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
823 d1cda826 2017-11-06 stsp goto done;
824 d1cda826 2017-11-06 stsp }
825 d1cda826 2017-11-06 stsp
826 d1cda826 2017-11-06 stsp /* Skip object header. */
827 d1cda826 2017-11-06 stsp len -= obj->hdrlen;
828 eb651edf 2018-02-11 stsp err = parse_commit_object(commit, p + obj->hdrlen, len);
829 eb651edf 2018-02-11 stsp free(p);
830 d1cda826 2017-11-06 stsp done:
831 d1cda826 2017-11-06 stsp return err;
832 d1cda826 2017-11-06 stsp }
833 d1cda826 2017-11-06 stsp
834 bff6ca00 2018-04-23 stsp static void
835 bff6ca00 2018-04-23 stsp read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
836 bff6ca00 2018-04-23 stsp int imsg_fds[2])
837 bff6ca00 2018-04-23 stsp {
838 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
839 bff6ca00 2018-04-23 stsp struct got_commit_object *commit = NULL;
840 bff6ca00 2018-04-23 stsp struct imsgbuf ibuf;
841 bff6ca00 2018-04-23 stsp FILE *f = NULL;
842 bff6ca00 2018-04-23 stsp int status = 0;
843 bff6ca00 2018-04-23 stsp
844 be37c2e6 2018-04-24 stsp setproctitle("read commit object");
845 bff6ca00 2018-04-23 stsp close(imsg_fds[0]);
846 bff6ca00 2018-04-23 stsp imsg_init(&ibuf, imsg_fds[1]);
847 bff6ca00 2018-04-23 stsp
848 bff6ca00 2018-04-23 stsp /* revoke access to most system calls */
849 bff6ca00 2018-04-23 stsp if (pledge("stdio", NULL) == -1) {
850 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
851 bff6ca00 2018-04-23 stsp goto done;
852 bff6ca00 2018-04-23 stsp }
853 bff6ca00 2018-04-23 stsp
854 bff6ca00 2018-04-23 stsp f = fdopen(obj_fd, "rb");
855 bff6ca00 2018-04-23 stsp if (f == NULL) {
856 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
857 bff6ca00 2018-04-23 stsp close(obj_fd);
858 bff6ca00 2018-04-23 stsp goto done;
859 bff6ca00 2018-04-23 stsp }
860 bff6ca00 2018-04-23 stsp
861 bff6ca00 2018-04-23 stsp err = read_commit_object(&commit, obj, f);
862 bff6ca00 2018-04-23 stsp if (err)
863 bff6ca00 2018-04-23 stsp goto done;
864 bff6ca00 2018-04-23 stsp
865 068fd2bf 2018-04-24 stsp err = got_privsep_send_commit(&ibuf, commit);
866 bff6ca00 2018-04-23 stsp done:
867 bff6ca00 2018-04-23 stsp if (commit)
868 bff6ca00 2018-04-23 stsp got_object_commit_close(commit);
869 bff6ca00 2018-04-23 stsp if (err) {
870 bff6ca00 2018-04-23 stsp got_privsep_send_error(&ibuf, err);
871 bff6ca00 2018-04-23 stsp status = 1;
872 bff6ca00 2018-04-23 stsp }
873 bff6ca00 2018-04-23 stsp if (f)
874 bff6ca00 2018-04-23 stsp fclose(f);
875 bff6ca00 2018-04-23 stsp imsg_clear(&ibuf);
876 bff6ca00 2018-04-23 stsp close(imsg_fds[1]);
877 bff6ca00 2018-04-23 stsp _exit(status);
878 bff6ca00 2018-04-23 stsp }
879 bff6ca00 2018-04-23 stsp
880 bff6ca00 2018-04-23 stsp static const struct got_error *
881 bff6ca00 2018-04-23 stsp read_commit_object_privsep(struct got_commit_object **commit,
882 bff6ca00 2018-04-23 stsp struct got_repository *repo, struct got_object *obj, int fd)
883 bff6ca00 2018-04-23 stsp {
884 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
885 bff6ca00 2018-04-23 stsp struct imsgbuf parent_ibuf;
886 bff6ca00 2018-04-23 stsp int imsg_fds[2];
887 bff6ca00 2018-04-23 stsp pid_t pid;
888 bff6ca00 2018-04-23 stsp
889 bff6ca00 2018-04-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
890 bff6ca00 2018-04-23 stsp return got_error_from_errno();
891 bff6ca00 2018-04-23 stsp
892 bff6ca00 2018-04-23 stsp pid = fork();
893 bff6ca00 2018-04-23 stsp if (pid == -1)
894 bff6ca00 2018-04-23 stsp return got_error_from_errno();
895 bff6ca00 2018-04-23 stsp else if (pid == 0) {
896 bff6ca00 2018-04-23 stsp read_commit_object_privsep_child(obj, fd, imsg_fds);
897 e506bf32 2018-04-23 stsp /* not reached */
898 bff6ca00 2018-04-23 stsp }
899 bff6ca00 2018-04-23 stsp
900 bff6ca00 2018-04-23 stsp close(imsg_fds[1]);
901 bff6ca00 2018-04-23 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
902 068fd2bf 2018-04-24 stsp err = got_privsep_recv_commit(commit, &parent_ibuf);
903 bff6ca00 2018-04-23 stsp imsg_clear(&parent_ibuf);
904 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
905 bff6ca00 2018-04-23 stsp close(imsg_fds[0]);
906 b419fc47 2018-04-26 stsp return err ? err : err_child;
907 bff6ca00 2018-04-23 stsp }
908 bff6ca00 2018-04-23 stsp
909 d1cda826 2017-11-06 stsp const struct got_error *
910 d1cda826 2017-11-06 stsp got_object_commit_open(struct got_commit_object **commit,
911 d1cda826 2017-11-06 stsp struct got_repository *repo, struct got_object *obj)
912 d1cda826 2017-11-06 stsp {
913 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
914 d1cda826 2017-11-06 stsp
915 d1cda826 2017-11-06 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
916 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_OBJ_TYPE);
917 d1cda826 2017-11-06 stsp
918 ea35256b 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
919 ea35256b 2018-03-16 stsp uint8_t *buf;
920 ea35256b 2018-03-16 stsp size_t len;
921 ea35256b 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
922 ea35256b 2018-03-16 stsp if (err)
923 ea35256b 2018-03-16 stsp return err;
924 b29656e2 2018-03-16 stsp obj->size = len;
925 b29656e2 2018-03-16 stsp err = parse_commit_object(commit, buf, len);
926 ea35256b 2018-03-16 stsp free(buf);
927 ea35256b 2018-03-16 stsp } else {
928 d5003b79 2018-04-22 stsp int fd;
929 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
930 ea35256b 2018-03-16 stsp if (err)
931 ea35256b 2018-03-16 stsp return err;
932 bff6ca00 2018-04-23 stsp err = read_commit_object_privsep(commit, repo, obj, fd);
933 bff6ca00 2018-04-23 stsp close(fd);
934 ea35256b 2018-03-16 stsp }
935 d1cda826 2017-11-06 stsp return err;
936 d1cda826 2017-11-06 stsp }
937 d1cda826 2017-11-06 stsp
938 d1cda826 2017-11-06 stsp void
939 d1cda826 2017-11-06 stsp got_object_commit_close(struct got_commit_object *commit)
940 d1cda826 2017-11-06 stsp {
941 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
942 d1cda826 2017-11-06 stsp
943 d1cda826 2017-11-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
944 79f35eb3 2018-06-11 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
945 d1cda826 2017-11-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
946 79f35eb3 2018-06-11 stsp free(qid->id);
947 79f35eb3 2018-06-11 stsp free(qid);
948 d1cda826 2017-11-06 stsp }
949 d1cda826 2017-11-06 stsp
950 59ece79d 2018-02-12 stsp free(commit->tree_id);
951 d1cda826 2017-11-06 stsp free(commit->author);
952 7cd42a1a 2018-06-11 stsp free(commit->author_tzoff);
953 d1cda826 2017-11-06 stsp free(commit->committer);
954 7cd42a1a 2018-06-11 stsp free(commit->committer_tzoff);
955 d1cda826 2017-11-06 stsp free(commit->logmsg);
956 d1cda826 2017-11-06 stsp free(commit);
957 d1cda826 2017-11-06 stsp }
958 0ffeb3c2 2017-11-26 stsp
959 0ffeb3c2 2017-11-26 stsp static const struct got_error *
960 e033d803 2018-04-23 stsp read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
961 0ffeb3c2 2017-11-26 stsp {
962 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
963 0ffeb3c2 2017-11-26 stsp size_t len;
964 eb651edf 2018-02-11 stsp uint8_t *p;
965 0ffeb3c2 2017-11-26 stsp
966 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
967 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
968 eb651edf 2018-02-11 stsp else
969 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
970 4558fcd4 2018-01-14 stsp if (err)
971 0ffeb3c2 2017-11-26 stsp return err;
972 0ffeb3c2 2017-11-26 stsp
973 0ffeb3c2 2017-11-26 stsp if (len < obj->hdrlen + obj->size) {
974 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
975 0ffeb3c2 2017-11-26 stsp goto done;
976 0ffeb3c2 2017-11-26 stsp }
977 0ffeb3c2 2017-11-26 stsp
978 0ffeb3c2 2017-11-26 stsp /* Skip object header. */
979 0ffeb3c2 2017-11-26 stsp len -= obj->hdrlen;
980 e033d803 2018-04-23 stsp err = parse_tree_object(tree, p + obj->hdrlen, len);
981 eb651edf 2018-02-11 stsp free(p);
982 e033d803 2018-04-23 stsp done:
983 e033d803 2018-04-23 stsp return err;
984 e033d803 2018-04-23 stsp }
985 e033d803 2018-04-23 stsp
986 e033d803 2018-04-23 stsp static void
987 e033d803 2018-04-23 stsp read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
988 e033d803 2018-04-23 stsp int imsg_fds[2])
989 e033d803 2018-04-23 stsp {
990 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
991 e033d803 2018-04-23 stsp struct got_tree_object *tree = NULL;
992 e033d803 2018-04-23 stsp struct imsgbuf ibuf;
993 e033d803 2018-04-23 stsp FILE *f = NULL;
994 e033d803 2018-04-23 stsp int status = 0;
995 e033d803 2018-04-23 stsp
996 be37c2e6 2018-04-24 stsp setproctitle("read tree object");
997 e033d803 2018-04-23 stsp close(imsg_fds[0]);
998 e033d803 2018-04-23 stsp imsg_init(&ibuf, imsg_fds[1]);
999 e033d803 2018-04-23 stsp
1000 e033d803 2018-04-23 stsp /* revoke access to most system calls */
1001 e033d803 2018-04-23 stsp if (pledge("stdio", NULL) == -1) {
1002 e033d803 2018-04-23 stsp err = got_error_from_errno();
1003 e033d803 2018-04-23 stsp goto done;
1004 e033d803 2018-04-23 stsp }
1005 e033d803 2018-04-23 stsp
1006 e033d803 2018-04-23 stsp f = fdopen(obj_fd, "rb");
1007 e033d803 2018-04-23 stsp if (f == NULL) {
1008 e033d803 2018-04-23 stsp err = got_error_from_errno();
1009 e033d803 2018-04-23 stsp close(obj_fd);
1010 e033d803 2018-04-23 stsp goto done;
1011 e033d803 2018-04-23 stsp }
1012 e033d803 2018-04-23 stsp
1013 e033d803 2018-04-23 stsp err = read_tree_object(&tree, obj, f);
1014 e033d803 2018-04-23 stsp if (err)
1015 e033d803 2018-04-23 stsp goto done;
1016 e033d803 2018-04-23 stsp
1017 068fd2bf 2018-04-24 stsp err = got_privsep_send_tree(&ibuf, tree);
1018 0ffeb3c2 2017-11-26 stsp done:
1019 e033d803 2018-04-23 stsp if (tree)
1020 e033d803 2018-04-23 stsp got_object_tree_close(tree);
1021 e033d803 2018-04-23 stsp if (err) {
1022 e033d803 2018-04-23 stsp got_privsep_send_error(&ibuf, err);
1023 e033d803 2018-04-23 stsp status = 1;
1024 e033d803 2018-04-23 stsp }
1025 e033d803 2018-04-23 stsp if (f)
1026 e033d803 2018-04-23 stsp fclose(f);
1027 e033d803 2018-04-23 stsp imsg_clear(&ibuf);
1028 e033d803 2018-04-23 stsp close(imsg_fds[1]);
1029 e033d803 2018-04-23 stsp _exit(status);
1030 e033d803 2018-04-23 stsp }
1031 e033d803 2018-04-23 stsp
1032 e033d803 2018-04-23 stsp static const struct got_error *
1033 e033d803 2018-04-23 stsp read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
1034 e033d803 2018-04-23 stsp int fd)
1035 e033d803 2018-04-23 stsp {
1036 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
1037 e033d803 2018-04-23 stsp struct imsgbuf parent_ibuf;
1038 e033d803 2018-04-23 stsp int imsg_fds[2];
1039 e033d803 2018-04-23 stsp pid_t pid;
1040 e033d803 2018-04-23 stsp
1041 e033d803 2018-04-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1042 e033d803 2018-04-23 stsp return got_error_from_errno();
1043 e033d803 2018-04-23 stsp
1044 e033d803 2018-04-23 stsp pid = fork();
1045 e033d803 2018-04-23 stsp if (pid == -1)
1046 e033d803 2018-04-23 stsp return got_error_from_errno();
1047 e033d803 2018-04-23 stsp else if (pid == 0) {
1048 e033d803 2018-04-23 stsp read_tree_object_privsep_child(obj, fd, imsg_fds);
1049 e033d803 2018-04-23 stsp /* not reached */
1050 e033d803 2018-04-23 stsp }
1051 e033d803 2018-04-23 stsp
1052 e033d803 2018-04-23 stsp close(imsg_fds[1]);
1053 e033d803 2018-04-23 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
1054 068fd2bf 2018-04-24 stsp err = got_privsep_recv_tree(tree, &parent_ibuf);
1055 e033d803 2018-04-23 stsp imsg_clear(&parent_ibuf);
1056 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
1057 e033d803 2018-04-23 stsp close(imsg_fds[0]);
1058 b419fc47 2018-04-26 stsp return err ? err : err_child;
1059 0ffeb3c2 2017-11-26 stsp }
1060 0ffeb3c2 2017-11-26 stsp
1061 0ffeb3c2 2017-11-26 stsp const struct got_error *
1062 0ffeb3c2 2017-11-26 stsp got_object_tree_open(struct got_tree_object **tree,
1063 0ffeb3c2 2017-11-26 stsp struct got_repository *repo, struct got_object *obj)
1064 0ffeb3c2 2017-11-26 stsp {
1065 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
1066 0ffeb3c2 2017-11-26 stsp
1067 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
1068 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
1069 0ffeb3c2 2017-11-26 stsp
1070 e0ab43e7 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1071 e0ab43e7 2018-03-16 stsp uint8_t *buf;
1072 e0ab43e7 2018-03-16 stsp size_t len;
1073 e0ab43e7 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1074 e0ab43e7 2018-03-16 stsp if (err)
1075 e0ab43e7 2018-03-16 stsp return err;
1076 b29656e2 2018-03-16 stsp obj->size = len;
1077 e033d803 2018-04-23 stsp err = parse_tree_object(tree, buf, len);
1078 e0ab43e7 2018-03-16 stsp free(buf);
1079 e0ab43e7 2018-03-16 stsp } else {
1080 d5003b79 2018-04-22 stsp int fd;
1081 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
1082 e0ab43e7 2018-03-16 stsp if (err)
1083 e0ab43e7 2018-03-16 stsp return err;
1084 e033d803 2018-04-23 stsp err = read_tree_object_privsep(tree, obj, fd);
1085 d5003b79 2018-04-22 stsp close(fd);
1086 e0ab43e7 2018-03-16 stsp }
1087 0ffeb3c2 2017-11-26 stsp return err;
1088 0ffeb3c2 2017-11-26 stsp }
1089 0ffeb3c2 2017-11-26 stsp
1090 0ffeb3c2 2017-11-26 stsp void
1091 0ffeb3c2 2017-11-26 stsp got_object_tree_close(struct got_tree_object *tree)
1092 0ffeb3c2 2017-11-26 stsp {
1093 f715ca7f 2017-11-27 stsp struct got_tree_entry *te;
1094 f715ca7f 2017-11-27 stsp
1095 f715ca7f 2017-11-27 stsp while (!SIMPLEQ_EMPTY(&tree->entries)) {
1096 f715ca7f 2017-11-27 stsp te = SIMPLEQ_FIRST(&tree->entries);
1097 f715ca7f 2017-11-27 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
1098 f715ca7f 2017-11-27 stsp tree_entry_close(te);
1099 f715ca7f 2017-11-27 stsp }
1100 f715ca7f 2017-11-27 stsp
1101 f715ca7f 2017-11-27 stsp free(tree);
1102 57efb1af 2018-04-24 stsp }
1103 ff6b18f8 2018-04-24 stsp
1104 ff6b18f8 2018-04-24 stsp static const struct got_error *
1105 ff6b18f8 2018-04-24 stsp read_blob_object_privsep_child(int outfd, int infd, int imsg_fds[2])
1106 ff6b18f8 2018-04-24 stsp {
1107 ff6b18f8 2018-04-24 stsp const struct got_error *err = NULL;
1108 ff6b18f8 2018-04-24 stsp struct imsgbuf ibuf;
1109 ff6b18f8 2018-04-24 stsp int status = 0;
1110 2967a784 2018-04-24 stsp size_t size;
1111 8b2180d4 2018-04-26 stsp FILE *infile = NULL;
1112 ff6b18f8 2018-04-24 stsp
1113 be37c2e6 2018-04-24 stsp setproctitle("read blob object");
1114 ff6b18f8 2018-04-24 stsp close(imsg_fds[0]);
1115 ff6b18f8 2018-04-24 stsp imsg_init(&ibuf, imsg_fds[1]);
1116 57efb1af 2018-04-24 stsp
1117 ff6b18f8 2018-04-24 stsp /* revoke access to most system calls */
1118 ff6b18f8 2018-04-24 stsp if (pledge("stdio", NULL) == -1) {
1119 ff6b18f8 2018-04-24 stsp err = got_error_from_errno();
1120 ff6b18f8 2018-04-24 stsp goto done;
1121 ff6b18f8 2018-04-24 stsp }
1122 ff6b18f8 2018-04-24 stsp
1123 8b2180d4 2018-04-26 stsp infile = fdopen(infd, "rb");
1124 8b2180d4 2018-04-26 stsp if (infile == NULL) {
1125 8b2180d4 2018-04-26 stsp err = got_error_from_errno();
1126 8b2180d4 2018-04-26 stsp close(infd);
1127 8b2180d4 2018-04-26 stsp goto done;
1128 8b2180d4 2018-04-26 stsp }
1129 8b2180d4 2018-04-26 stsp err = got_inflate_to_fd(&size, infile, outfd);
1130 8b2180d4 2018-04-26 stsp fclose(infile);
1131 ff6b18f8 2018-04-24 stsp if (err)
1132 ff6b18f8 2018-04-24 stsp goto done;
1133 ff6b18f8 2018-04-24 stsp
1134 2967a784 2018-04-24 stsp err = got_privsep_send_blob(&ibuf, size);
1135 ff6b18f8 2018-04-24 stsp done:
1136 ff6b18f8 2018-04-24 stsp if (err) {
1137 ff6b18f8 2018-04-24 stsp got_privsep_send_error(&ibuf, err);
1138 ff6b18f8 2018-04-24 stsp status = 1;
1139 ff6b18f8 2018-04-24 stsp }
1140 ff6b18f8 2018-04-24 stsp close(outfd);
1141 ff6b18f8 2018-04-24 stsp imsg_clear(&ibuf);
1142 ff6b18f8 2018-04-24 stsp close(imsg_fds[1]);
1143 ff6b18f8 2018-04-24 stsp _exit(status);
1144 ff6b18f8 2018-04-24 stsp }
1145 ff6b18f8 2018-04-24 stsp
1146 ff6b18f8 2018-04-24 stsp static const struct got_error *
1147 2967a784 2018-04-24 stsp read_blob_object_privsep(size_t *size, int outfd, int infd)
1148 ff6b18f8 2018-04-24 stsp {
1149 ff6b18f8 2018-04-24 stsp struct imsgbuf parent_ibuf;
1150 ff6b18f8 2018-04-24 stsp int imsg_fds[2];
1151 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
1152 ff6b18f8 2018-04-24 stsp pid_t pid;
1153 ff6b18f8 2018-04-24 stsp
1154 ff6b18f8 2018-04-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1155 ff6b18f8 2018-04-24 stsp return got_error_from_errno();
1156 ff6b18f8 2018-04-24 stsp
1157 ff6b18f8 2018-04-24 stsp pid = fork();
1158 ff6b18f8 2018-04-24 stsp if (pid == -1)
1159 ff6b18f8 2018-04-24 stsp return got_error_from_errno();
1160 ff6b18f8 2018-04-24 stsp else if (pid == 0) {
1161 ff6b18f8 2018-04-24 stsp read_blob_object_privsep_child(outfd, infd, imsg_fds);
1162 ff6b18f8 2018-04-24 stsp /* not reached */
1163 ff6b18f8 2018-04-24 stsp }
1164 ff6b18f8 2018-04-24 stsp
1165 ff6b18f8 2018-04-24 stsp close(imsg_fds[1]);
1166 ff6b18f8 2018-04-24 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
1167 2967a784 2018-04-24 stsp err = got_privsep_recv_blob(size, &parent_ibuf);
1168 ff6b18f8 2018-04-24 stsp imsg_clear(&parent_ibuf);
1169 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
1170 ff6b18f8 2018-04-24 stsp close(imsg_fds[0]);
1171 ff6b18f8 2018-04-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1172 ff6b18f8 2018-04-24 stsp err = got_error_from_errno();
1173 b419fc47 2018-04-26 stsp return err ? err : err_child;
1174 ff6b18f8 2018-04-24 stsp }
1175 ff6b18f8 2018-04-24 stsp
1176 68482ea3 2017-11-27 stsp const struct got_error *
1177 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
1178 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1179 68482ea3 2017-11-27 stsp {
1180 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
1181 68482ea3 2017-11-27 stsp
1182 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
1183 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
1184 68482ea3 2017-11-27 stsp
1185 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
1186 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
1187 7d283eee 2017-11-29 stsp
1188 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1189 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1190 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1191 68482ea3 2017-11-27 stsp
1192 15c8b0e6 2018-04-24 stsp (*blob)->read_buf = calloc(1, blocksize);
1193 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1194 15c8b0e6 2018-04-24 stsp err = got_error_from_errno();
1195 c7254d79 2018-04-24 stsp goto done;
1196 15c8b0e6 2018-04-24 stsp }
1197 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1198 eb651edf 2018-02-11 stsp err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1199 c7254d79 2018-04-24 stsp if (err)
1200 c7254d79 2018-04-24 stsp goto done;
1201 eb651edf 2018-02-11 stsp } else {
1202 3aca5731 2018-04-24 stsp int infd, outfd;
1203 2967a784 2018-04-24 stsp size_t size;
1204 2967a784 2018-04-24 stsp struct stat sb;
1205 c7254d79 2018-04-24 stsp
1206 3aca5731 2018-04-24 stsp err = open_loose_object(&infd, obj, repo);
1207 c7254d79 2018-04-24 stsp if (err)
1208 c7254d79 2018-04-24 stsp goto done;
1209 c7254d79 2018-04-24 stsp
1210 3aca5731 2018-04-24 stsp
1211 3aca5731 2018-04-24 stsp outfd = got_opentempfd();
1212 3aca5731 2018-04-24 stsp if (outfd == -1) {
1213 3aca5731 2018-04-24 stsp err = got_error_from_errno();
1214 3aca5731 2018-04-24 stsp close(infd);
1215 3aca5731 2018-04-24 stsp goto done;
1216 3aca5731 2018-04-24 stsp }
1217 3aca5731 2018-04-24 stsp
1218 2967a784 2018-04-24 stsp err = read_blob_object_privsep(&size, outfd, infd);
1219 3aca5731 2018-04-24 stsp close(infd);
1220 57efb1af 2018-04-24 stsp if (err)
1221 c7254d79 2018-04-24 stsp goto done;
1222 3aca5731 2018-04-24 stsp
1223 2967a784 2018-04-24 stsp if (size != obj->hdrlen + obj->size) {
1224 1a6b3ab7 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1225 2967a784 2018-04-24 stsp close(outfd);
1226 2967a784 2018-04-24 stsp goto done;
1227 2967a784 2018-04-24 stsp }
1228 2967a784 2018-04-24 stsp
1229 2967a784 2018-04-24 stsp if (fstat(outfd, &sb) == -1) {
1230 2967a784 2018-04-24 stsp err = got_error_from_errno();
1231 2967a784 2018-04-24 stsp close(outfd);
1232 2967a784 2018-04-24 stsp goto done;
1233 2967a784 2018-04-24 stsp }
1234 2967a784 2018-04-24 stsp
1235 2967a784 2018-04-24 stsp if (sb.st_size != size) {
1236 2967a784 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1237 2967a784 2018-04-24 stsp close(outfd);
1238 2967a784 2018-04-24 stsp goto done;
1239 2967a784 2018-04-24 stsp }
1240 2967a784 2018-04-24 stsp
1241 3aca5731 2018-04-24 stsp (*blob)->f = fdopen(outfd, "rb");
1242 3aca5731 2018-04-24 stsp if ((*blob)->f == NULL) {
1243 3aca5731 2018-04-24 stsp err = got_error_from_errno();
1244 3aca5731 2018-04-24 stsp close(outfd);
1245 3aca5731 2018-04-24 stsp goto done;
1246 3aca5731 2018-04-24 stsp }
1247 68482ea3 2017-11-27 stsp }
1248 68482ea3 2017-11-27 stsp
1249 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
1250 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1251 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1252 7d283eee 2017-11-29 stsp
1253 c7254d79 2018-04-24 stsp done:
1254 c7254d79 2018-04-24 stsp if (err && *blob) {
1255 c7254d79 2018-04-24 stsp if ((*blob)->f)
1256 c7254d79 2018-04-24 stsp fclose((*blob)->f);
1257 c7254d79 2018-04-24 stsp free((*blob)->read_buf);
1258 c7254d79 2018-04-24 stsp free(*blob);
1259 c7254d79 2018-04-24 stsp *blob = NULL;
1260 c7254d79 2018-04-24 stsp }
1261 68482ea3 2017-11-27 stsp return err;
1262 0ffeb3c2 2017-11-26 stsp }
1263 68482ea3 2017-11-27 stsp
1264 68482ea3 2017-11-27 stsp void
1265 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1266 68482ea3 2017-11-27 stsp {
1267 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1268 68482ea3 2017-11-27 stsp fclose(blob->f);
1269 68482ea3 2017-11-27 stsp free(blob);
1270 f934cf2c 2018-02-12 stsp }
1271 f934cf2c 2018-02-12 stsp
1272 f934cf2c 2018-02-12 stsp char *
1273 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1274 f934cf2c 2018-02-12 stsp {
1275 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1276 f934cf2c 2018-02-12 stsp }
1277 f934cf2c 2018-02-12 stsp
1278 f934cf2c 2018-02-12 stsp size_t
1279 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1280 f934cf2c 2018-02-12 stsp {
1281 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1282 68482ea3 2017-11-27 stsp }
1283 68482ea3 2017-11-27 stsp
1284 f934cf2c 2018-02-12 stsp const uint8_t *
1285 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1286 f934cf2c 2018-02-12 stsp {
1287 f934cf2c 2018-02-12 stsp return blob->read_buf;
1288 f934cf2c 2018-02-12 stsp }
1289 f934cf2c 2018-02-12 stsp
1290 68482ea3 2017-11-27 stsp const struct got_error *
1291 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1292 68482ea3 2017-11-27 stsp {
1293 eb651edf 2018-02-11 stsp size_t n;
1294 eb651edf 2018-02-11 stsp
1295 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1296 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1297 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1298 eb651edf 2018-02-11 stsp *outlenp = n;
1299 eb651edf 2018-02-11 stsp return NULL;
1300 68482ea3 2017-11-27 stsp }