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