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