Blame


1 a440fac0 2018-09-06 stsp /*
2 a440fac0 2018-09-06 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 a440fac0 2018-09-06 stsp *
4 a440fac0 2018-09-06 stsp * Permission to use, copy, modify, and distribute this software for any
5 a440fac0 2018-09-06 stsp * purpose with or without fee is hereby granted, provided that the above
6 a440fac0 2018-09-06 stsp * copyright notice and this permission notice appear in all copies.
7 a440fac0 2018-09-06 stsp *
8 a440fac0 2018-09-06 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 a440fac0 2018-09-06 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 a440fac0 2018-09-06 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 a440fac0 2018-09-06 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 a440fac0 2018-09-06 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 a440fac0 2018-09-06 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 a440fac0 2018-09-06 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 a440fac0 2018-09-06 stsp */
16 a440fac0 2018-09-06 stsp
17 a440fac0 2018-09-06 stsp #include <sys/types.h>
18 a440fac0 2018-09-06 stsp #include <sys/stat.h>
19 a440fac0 2018-09-06 stsp #include <sys/queue.h>
20 a440fac0 2018-09-06 stsp #include <sys/uio.h>
21 a440fac0 2018-09-06 stsp #include <sys/socket.h>
22 876c234b 2018-09-10 stsp #include <sys/syslimits.h>
23 a440fac0 2018-09-06 stsp #include <sys/wait.h>
24 a440fac0 2018-09-06 stsp
25 a440fac0 2018-09-06 stsp #include <errno.h>
26 a440fac0 2018-09-06 stsp #include <stdio.h>
27 a440fac0 2018-09-06 stsp #include <stdlib.h>
28 a440fac0 2018-09-06 stsp #include <string.h>
29 a440fac0 2018-09-06 stsp #include <stdint.h>
30 a440fac0 2018-09-06 stsp #include <sha1.h>
31 a440fac0 2018-09-06 stsp #include <zlib.h>
32 a440fac0 2018-09-06 stsp #include <ctype.h>
33 a440fac0 2018-09-06 stsp #include <limits.h>
34 a440fac0 2018-09-06 stsp #include <imsg.h>
35 a440fac0 2018-09-06 stsp #include <time.h>
36 ad242220 2018-09-08 stsp #include <unistd.h>
37 a440fac0 2018-09-06 stsp
38 a440fac0 2018-09-06 stsp #include "got_error.h"
39 a440fac0 2018-09-06 stsp #include "got_object.h"
40 a440fac0 2018-09-06 stsp #include "got_repository.h"
41 a440fac0 2018-09-06 stsp #include "got_opentemp.h"
42 a440fac0 2018-09-06 stsp
43 a440fac0 2018-09-06 stsp #include "got_lib_sha1.h"
44 a440fac0 2018-09-06 stsp #include "got_lib_delta.h"
45 ad242220 2018-09-08 stsp #include "got_lib_privsep.h"
46 a440fac0 2018-09-06 stsp #include "got_lib_pack.h"
47 a440fac0 2018-09-06 stsp #include "got_lib_inflate.h"
48 a440fac0 2018-09-06 stsp #include "got_lib_object.h"
49 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
50 ad242220 2018-09-08 stsp #include "got_lib_repository.h"
51 a440fac0 2018-09-06 stsp
52 a440fac0 2018-09-06 stsp #ifndef nitems
53 a440fac0 2018-09-06 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
54 a440fac0 2018-09-06 stsp #endif
55 a440fac0 2018-09-06 stsp
56 a440fac0 2018-09-06 stsp #define GOT_OBJ_TAG_COMMIT "commit"
57 a440fac0 2018-09-06 stsp #define GOT_OBJ_TAG_TREE "tree"
58 a440fac0 2018-09-06 stsp #define GOT_OBJ_TAG_BLOB "blob"
59 a440fac0 2018-09-06 stsp
60 a440fac0 2018-09-06 stsp #define GOT_COMMIT_TAG_TREE "tree "
61 a440fac0 2018-09-06 stsp #define GOT_COMMIT_TAG_PARENT "parent "
62 a440fac0 2018-09-06 stsp #define GOT_COMMIT_TAG_AUTHOR "author "
63 a440fac0 2018-09-06 stsp #define GOT_COMMIT_TAG_COMMITTER "committer "
64 a440fac0 2018-09-06 stsp
65 03fa71c8 2018-09-06 stsp void
66 03fa71c8 2018-09-06 stsp got_object_close(struct got_object *obj)
67 03fa71c8 2018-09-06 stsp {
68 03fa71c8 2018-09-06 stsp if (obj->refcnt > 0) {
69 03fa71c8 2018-09-06 stsp obj->refcnt--;
70 03fa71c8 2018-09-06 stsp if (obj->refcnt > 0)
71 03fa71c8 2018-09-06 stsp return;
72 03fa71c8 2018-09-06 stsp }
73 03fa71c8 2018-09-06 stsp
74 03fa71c8 2018-09-06 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
75 03fa71c8 2018-09-06 stsp struct got_delta *delta;
76 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
77 03fa71c8 2018-09-06 stsp delta = SIMPLEQ_FIRST(&obj->deltas.entries);
78 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
79 03fa71c8 2018-09-06 stsp got_delta_close(delta);
80 03fa71c8 2018-09-06 stsp }
81 03fa71c8 2018-09-06 stsp }
82 03fa71c8 2018-09-06 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
83 03fa71c8 2018-09-06 stsp free(obj->path_packfile);
84 03fa71c8 2018-09-06 stsp free(obj);
85 03fa71c8 2018-09-06 stsp }
86 03fa71c8 2018-09-06 stsp
87 03fa71c8 2018-09-06 stsp void
88 03fa71c8 2018-09-06 stsp got_object_qid_free(struct got_object_qid *qid)
89 03fa71c8 2018-09-06 stsp {
90 03fa71c8 2018-09-06 stsp free(qid->id);
91 03fa71c8 2018-09-06 stsp free(qid);
92 03fa71c8 2018-09-06 stsp }
93 03fa71c8 2018-09-06 stsp
94 a440fac0 2018-09-06 stsp static const struct got_error *
95 ad242220 2018-09-08 stsp request_object(struct got_object **obj, struct got_repository *repo, int fd)
96 a440fac0 2018-09-06 stsp {
97 ad242220 2018-09-08 stsp const struct got_error *err = NULL;
98 3516b818 2018-09-08 stsp struct imsgbuf *ibuf;
99 a440fac0 2018-09-06 stsp
100 3516b818 2018-09-08 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
101 a440fac0 2018-09-06 stsp
102 3516b818 2018-09-08 stsp err = got_privsep_send_obj_req(ibuf, fd, NULL);
103 a440fac0 2018-09-06 stsp if (err)
104 3516b818 2018-09-08 stsp return err;
105 3516b818 2018-09-08 stsp
106 3516b818 2018-09-08 stsp return got_privsep_recv_obj(obj, ibuf);
107 a440fac0 2018-09-06 stsp }
108 a440fac0 2018-09-06 stsp
109 a440fac0 2018-09-06 stsp static void
110 3cab8b4d 2018-09-08 stsp exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
111 a440fac0 2018-09-06 stsp {
112 a440fac0 2018-09-06 stsp close(imsg_fds[0]);
113 a440fac0 2018-09-06 stsp
114 ad242220 2018-09-08 stsp if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
115 ad242220 2018-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(),
116 ad242220 2018-09-08 stsp strerror(errno));
117 ad242220 2018-09-08 stsp _exit(1);
118 a440fac0 2018-09-06 stsp }
119 ad242220 2018-09-08 stsp if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
120 ad242220 2018-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(),
121 ad242220 2018-09-08 stsp strerror(errno));
122 ad242220 2018-09-08 stsp _exit(1);
123 ad242220 2018-09-08 stsp }
124 a440fac0 2018-09-06 stsp
125 3cab8b4d 2018-09-08 stsp if (execl(path, path, repo_path, (char *)NULL) == -1) {
126 ad242220 2018-09-08 stsp fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
127 ad242220 2018-09-08 stsp strerror(errno));
128 ad242220 2018-09-08 stsp _exit(1);
129 a440fac0 2018-09-06 stsp }
130 a440fac0 2018-09-06 stsp }
131 a440fac0 2018-09-06 stsp
132 a440fac0 2018-09-06 stsp const struct got_error *
133 ad242220 2018-09-08 stsp got_object_read_header_privsep(struct got_object **obj,
134 ad242220 2018-09-08 stsp struct got_repository *repo, int obj_fd)
135 a440fac0 2018-09-06 stsp {
136 a440fac0 2018-09-06 stsp int imsg_fds[2];
137 a440fac0 2018-09-06 stsp pid_t pid;
138 3516b818 2018-09-08 stsp struct imsgbuf *ibuf;
139 a440fac0 2018-09-06 stsp
140 ad242220 2018-09-08 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
141 ad242220 2018-09-08 stsp return request_object(obj, repo, obj_fd);
142 ad242220 2018-09-08 stsp
143 3516b818 2018-09-08 stsp ibuf = calloc(1, sizeof(*ibuf));
144 3516b818 2018-09-08 stsp if (ibuf == NULL)
145 3516b818 2018-09-08 stsp return got_error_from_errno();
146 3516b818 2018-09-08 stsp
147 a440fac0 2018-09-06 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
148 a440fac0 2018-09-06 stsp return got_error_from_errno();
149 a440fac0 2018-09-06 stsp
150 a440fac0 2018-09-06 stsp pid = fork();
151 a440fac0 2018-09-06 stsp if (pid == -1)
152 a440fac0 2018-09-06 stsp return got_error_from_errno();
153 a440fac0 2018-09-06 stsp else if (pid == 0) {
154 3cab8b4d 2018-09-08 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
155 3cab8b4d 2018-09-08 stsp repo->path);
156 a440fac0 2018-09-06 stsp /* not reached */
157 a440fac0 2018-09-06 stsp }
158 a440fac0 2018-09-06 stsp
159 a440fac0 2018-09-06 stsp close(imsg_fds[1]);
160 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
161 ad242220 2018-09-08 stsp imsg_fds[0];
162 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
163 3516b818 2018-09-08 stsp imsg_init(ibuf, imsg_fds[0]);
164 3516b818 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
165 ad242220 2018-09-08 stsp
166 ad242220 2018-09-08 stsp return request_object(obj, repo, obj_fd);
167 a440fac0 2018-09-06 stsp }
168 a440fac0 2018-09-06 stsp
169 876c234b 2018-09-10 stsp static const struct got_error *
170 876c234b 2018-09-10 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
171 876c234b 2018-09-10 stsp struct got_object_id *id)
172 876c234b 2018-09-10 stsp {
173 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
174 876c234b 2018-09-10 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
175 876c234b 2018-09-10 stsp
176 876c234b 2018-09-10 stsp err = got_privsep_send_packed_obj_req(ibuf, idx);
177 876c234b 2018-09-10 stsp if (err)
178 876c234b 2018-09-10 stsp return err;
179 876c234b 2018-09-10 stsp
180 876c234b 2018-09-10 stsp err = got_privsep_recv_obj(obj, ibuf);
181 876c234b 2018-09-10 stsp if (err)
182 876c234b 2018-09-10 stsp return err;
183 876c234b 2018-09-10 stsp
184 876c234b 2018-09-10 stsp (*obj)->path_packfile = strdup(pack->path_packfile);
185 876c234b 2018-09-10 stsp if ((*obj)->path_packfile == NULL) {
186 876c234b 2018-09-10 stsp err = got_error_from_errno();
187 876c234b 2018-09-10 stsp return err;
188 876c234b 2018-09-10 stsp }
189 876c234b 2018-09-10 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
190 876c234b 2018-09-10 stsp
191 876c234b 2018-09-10 stsp return NULL;
192 876c234b 2018-09-10 stsp }
193 876c234b 2018-09-10 stsp
194 876c234b 2018-09-10 stsp const struct got_error *
195 876c234b 2018-09-10 stsp got_object_packed_read_privsep(struct got_object **obj,
196 876c234b 2018-09-10 stsp struct got_repository *repo, struct got_pack *pack,
197 876c234b 2018-09-10 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
198 876c234b 2018-09-10 stsp {
199 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
200 876c234b 2018-09-10 stsp int imsg_fds[2];
201 876c234b 2018-09-10 stsp pid_t pid;
202 876c234b 2018-09-10 stsp struct imsgbuf *ibuf;
203 876c234b 2018-09-10 stsp
204 876c234b 2018-09-10 stsp if (pack->privsep_child)
205 876c234b 2018-09-10 stsp return request_packed_object(obj, pack, idx, id);
206 876c234b 2018-09-10 stsp
207 876c234b 2018-09-10 stsp ibuf = calloc(1, sizeof(*ibuf));
208 876c234b 2018-09-10 stsp if (ibuf == NULL)
209 876c234b 2018-09-10 stsp return got_error_from_errno();
210 876c234b 2018-09-10 stsp
211 876c234b 2018-09-10 stsp pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
212 876c234b 2018-09-10 stsp if (pack->privsep_child == NULL) {
213 876c234b 2018-09-10 stsp err = got_error_from_errno();
214 876c234b 2018-09-10 stsp free(ibuf);
215 876c234b 2018-09-10 stsp return err;
216 876c234b 2018-09-10 stsp }
217 876c234b 2018-09-10 stsp
218 876c234b 2018-09-10 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
219 876c234b 2018-09-10 stsp err = got_error_from_errno();
220 876c234b 2018-09-10 stsp goto done;
221 876c234b 2018-09-10 stsp }
222 876c234b 2018-09-10 stsp
223 876c234b 2018-09-10 stsp pid = fork();
224 876c234b 2018-09-10 stsp if (pid == -1) {
225 876c234b 2018-09-10 stsp err = got_error_from_errno();
226 876c234b 2018-09-10 stsp goto done;
227 876c234b 2018-09-10 stsp } else if (pid == 0) {
228 876c234b 2018-09-10 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
229 876c234b 2018-09-10 stsp pack->path_packfile);
230 876c234b 2018-09-10 stsp /* not reached */
231 876c234b 2018-09-10 stsp }
232 876c234b 2018-09-10 stsp
233 876c234b 2018-09-10 stsp close(imsg_fds[1]);
234 876c234b 2018-09-10 stsp pack->privsep_child->imsg_fd = imsg_fds[0];
235 876c234b 2018-09-10 stsp pack->privsep_child->pid = pid;
236 876c234b 2018-09-10 stsp imsg_init(ibuf, imsg_fds[0]);
237 876c234b 2018-09-10 stsp pack->privsep_child->ibuf = ibuf;
238 876c234b 2018-09-10 stsp
239 876c234b 2018-09-10 stsp err = got_privsep_init_pack_child(ibuf, pack, packidx);
240 876c234b 2018-09-10 stsp if (err) {
241 876c234b 2018-09-10 stsp const struct got_error *child_err;
242 876c234b 2018-09-10 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
243 876c234b 2018-09-10 stsp child_err = got_privsep_wait_for_child(
244 876c234b 2018-09-10 stsp pack->privsep_child->pid);
245 876c234b 2018-09-10 stsp if (child_err && err == NULL)
246 876c234b 2018-09-10 stsp err = child_err;
247 876c234b 2018-09-10 stsp free(ibuf);
248 876c234b 2018-09-10 stsp free(pack->privsep_child);
249 876c234b 2018-09-10 stsp pack->privsep_child = NULL;
250 876c234b 2018-09-10 stsp return err;
251 876c234b 2018-09-10 stsp }
252 876c234b 2018-09-10 stsp
253 876c234b 2018-09-10 stsp done:
254 876c234b 2018-09-10 stsp if (err) {
255 876c234b 2018-09-10 stsp free(ibuf);
256 876c234b 2018-09-10 stsp free(pack->privsep_child);
257 876c234b 2018-09-10 stsp pack->privsep_child = NULL;
258 876c234b 2018-09-10 stsp } else
259 876c234b 2018-09-10 stsp err = request_packed_object(obj, pack, idx, id);
260 876c234b 2018-09-10 stsp return err;
261 876c234b 2018-09-10 stsp }
262 876c234b 2018-09-10 stsp
263 a440fac0 2018-09-06 stsp struct got_commit_object *
264 a440fac0 2018-09-06 stsp got_object_commit_alloc_partial(void)
265 a440fac0 2018-09-06 stsp {
266 a440fac0 2018-09-06 stsp struct got_commit_object *commit;
267 a440fac0 2018-09-06 stsp
268 a440fac0 2018-09-06 stsp commit = calloc(1, sizeof(*commit));
269 a440fac0 2018-09-06 stsp if (commit == NULL)
270 a440fac0 2018-09-06 stsp return NULL;
271 a440fac0 2018-09-06 stsp commit->tree_id = calloc(1, sizeof(*commit->tree_id));
272 a440fac0 2018-09-06 stsp if (commit->tree_id == NULL) {
273 a440fac0 2018-09-06 stsp free(commit);
274 a440fac0 2018-09-06 stsp return NULL;
275 a440fac0 2018-09-06 stsp }
276 a440fac0 2018-09-06 stsp
277 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&commit->parent_ids);
278 a440fac0 2018-09-06 stsp
279 a440fac0 2018-09-06 stsp return commit;
280 a440fac0 2018-09-06 stsp }
281 a440fac0 2018-09-06 stsp
282 a440fac0 2018-09-06 stsp const struct got_error *
283 a440fac0 2018-09-06 stsp got_object_commit_add_parent(struct got_commit_object *commit,
284 a440fac0 2018-09-06 stsp const char *id_str)
285 a440fac0 2018-09-06 stsp {
286 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
287 a440fac0 2018-09-06 stsp struct got_object_qid *qid;
288 a440fac0 2018-09-06 stsp
289 a440fac0 2018-09-06 stsp qid = malloc(sizeof(*qid));
290 a440fac0 2018-09-06 stsp if (qid == NULL)
291 a440fac0 2018-09-06 stsp return got_error_from_errno();
292 a440fac0 2018-09-06 stsp
293 a440fac0 2018-09-06 stsp qid->id = malloc(sizeof(*qid->id));
294 a440fac0 2018-09-06 stsp if (qid->id == NULL) {
295 a440fac0 2018-09-06 stsp err = got_error_from_errno();
296 a440fac0 2018-09-06 stsp got_object_qid_free(qid);
297 a440fac0 2018-09-06 stsp return err;
298 a440fac0 2018-09-06 stsp }
299 a440fac0 2018-09-06 stsp
300 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
301 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
302 a440fac0 2018-09-06 stsp free(qid->id);
303 a440fac0 2018-09-06 stsp free(qid);
304 a440fac0 2018-09-06 stsp return err;
305 a440fac0 2018-09-06 stsp }
306 a440fac0 2018-09-06 stsp
307 a440fac0 2018-09-06 stsp SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
308 a440fac0 2018-09-06 stsp commit->nparents++;
309 a440fac0 2018-09-06 stsp
310 a440fac0 2018-09-06 stsp return NULL;
311 a440fac0 2018-09-06 stsp }
312 a440fac0 2018-09-06 stsp
313 a440fac0 2018-09-06 stsp static const struct got_error *
314 a440fac0 2018-09-06 stsp parse_gmtoff(time_t *gmtoff, const char *tzstr)
315 a440fac0 2018-09-06 stsp {
316 a440fac0 2018-09-06 stsp int sign = 1;
317 a440fac0 2018-09-06 stsp const char *p = tzstr;
318 a440fac0 2018-09-06 stsp time_t h, m;
319 a440fac0 2018-09-06 stsp
320 a440fac0 2018-09-06 stsp *gmtoff = 0;
321 a440fac0 2018-09-06 stsp
322 a440fac0 2018-09-06 stsp if (*p == '-')
323 a440fac0 2018-09-06 stsp sign = -1;
324 a440fac0 2018-09-06 stsp else if (*p != '+')
325 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
326 a440fac0 2018-09-06 stsp p++;
327 a440fac0 2018-09-06 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
328 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
329 a440fac0 2018-09-06 stsp h = (((*p - '0') * 10) + (*(p + 1) - '0'));
330 a440fac0 2018-09-06 stsp
331 a440fac0 2018-09-06 stsp p += 2;
332 a440fac0 2018-09-06 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
333 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
334 a440fac0 2018-09-06 stsp m = ((*p - '0') * 10) + (*(p + 1) - '0');
335 a440fac0 2018-09-06 stsp
336 a440fac0 2018-09-06 stsp *gmtoff = (h * 60 * 60 + m * 60) * sign;
337 a440fac0 2018-09-06 stsp return NULL;
338 a440fac0 2018-09-06 stsp }
339 a440fac0 2018-09-06 stsp
340 a440fac0 2018-09-06 stsp static const struct got_error *
341 a440fac0 2018-09-06 stsp parse_commit_time(struct tm *tm, char *committer)
342 a440fac0 2018-09-06 stsp {
343 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
344 a440fac0 2018-09-06 stsp const char *errstr;
345 a440fac0 2018-09-06 stsp char *space, *tzstr;
346 a440fac0 2018-09-06 stsp time_t gmtoff;
347 a440fac0 2018-09-06 stsp time_t time;
348 a440fac0 2018-09-06 stsp
349 a440fac0 2018-09-06 stsp /* Parse and strip off trailing timezone indicator string. */
350 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
351 a440fac0 2018-09-06 stsp if (space == NULL)
352 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
353 a440fac0 2018-09-06 stsp tzstr = strdup(space + 1);
354 a440fac0 2018-09-06 stsp if (tzstr == NULL)
355 a440fac0 2018-09-06 stsp return got_error_from_errno();
356 a440fac0 2018-09-06 stsp err = parse_gmtoff(&gmtoff, tzstr);
357 a440fac0 2018-09-06 stsp free(tzstr);
358 a440fac0 2018-09-06 stsp if (err)
359 a440fac0 2018-09-06 stsp return err;
360 a440fac0 2018-09-06 stsp *space = '\0';
361 a440fac0 2018-09-06 stsp
362 a440fac0 2018-09-06 stsp /* Timestamp is separated from committer name + email by space. */
363 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
364 a440fac0 2018-09-06 stsp if (space == NULL)
365 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
366 a440fac0 2018-09-06 stsp
367 a440fac0 2018-09-06 stsp /* Timestamp parsed here is expressed in comitter's local time. */
368 a440fac0 2018-09-06 stsp time = strtonum(space + 1, 0, INT64_MAX, &errstr);
369 a440fac0 2018-09-06 stsp if (errstr)
370 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
371 a440fac0 2018-09-06 stsp
372 a440fac0 2018-09-06 stsp /* Express the time stamp in UTC. */
373 a440fac0 2018-09-06 stsp memset(tm, 0, sizeof(*tm));
374 a440fac0 2018-09-06 stsp time -= gmtoff;
375 a440fac0 2018-09-06 stsp if (localtime_r(&time, tm) == NULL)
376 a440fac0 2018-09-06 stsp return got_error_from_errno();
377 a440fac0 2018-09-06 stsp tm->tm_gmtoff = gmtoff;
378 a440fac0 2018-09-06 stsp
379 a440fac0 2018-09-06 stsp /* Strip off parsed time information, leaving just author and email. */
380 a440fac0 2018-09-06 stsp *space = '\0';
381 a440fac0 2018-09-06 stsp
382 a440fac0 2018-09-06 stsp return NULL;
383 a440fac0 2018-09-06 stsp }
384 a440fac0 2018-09-06 stsp
385 03fa71c8 2018-09-06 stsp void
386 03fa71c8 2018-09-06 stsp got_object_commit_close(struct got_commit_object *commit)
387 03fa71c8 2018-09-06 stsp {
388 03fa71c8 2018-09-06 stsp struct got_object_qid *qid;
389 03fa71c8 2018-09-06 stsp
390 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0) {
391 03fa71c8 2018-09-06 stsp commit->refcnt--;
392 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0)
393 03fa71c8 2018-09-06 stsp return;
394 03fa71c8 2018-09-06 stsp }
395 03fa71c8 2018-09-06 stsp
396 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
397 03fa71c8 2018-09-06 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
398 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
399 03fa71c8 2018-09-06 stsp got_object_qid_free(qid);
400 03fa71c8 2018-09-06 stsp }
401 03fa71c8 2018-09-06 stsp
402 03fa71c8 2018-09-06 stsp free(commit->tree_id);
403 03fa71c8 2018-09-06 stsp free(commit->author);
404 03fa71c8 2018-09-06 stsp free(commit->committer);
405 03fa71c8 2018-09-06 stsp free(commit->logmsg);
406 03fa71c8 2018-09-06 stsp free(commit);
407 03fa71c8 2018-09-06 stsp }
408 03fa71c8 2018-09-06 stsp
409 a440fac0 2018-09-06 stsp const struct got_error *
410 a440fac0 2018-09-06 stsp got_object_parse_commit(struct got_commit_object **commit, char *buf, size_t len)
411 a440fac0 2018-09-06 stsp {
412 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
413 a440fac0 2018-09-06 stsp char *s = buf;
414 a440fac0 2018-09-06 stsp size_t tlen;
415 a440fac0 2018-09-06 stsp ssize_t remain = (ssize_t)len;
416 a440fac0 2018-09-06 stsp
417 a440fac0 2018-09-06 stsp *commit = got_object_commit_alloc_partial();
418 a440fac0 2018-09-06 stsp if (*commit == NULL)
419 a440fac0 2018-09-06 stsp return got_error_from_errno();
420 a440fac0 2018-09-06 stsp
421 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
422 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
423 a440fac0 2018-09-06 stsp remain -= tlen;
424 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
425 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
426 a440fac0 2018-09-06 stsp goto done;
427 a440fac0 2018-09-06 stsp }
428 a440fac0 2018-09-06 stsp s += tlen;
429 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
430 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
431 a440fac0 2018-09-06 stsp goto done;
432 a440fac0 2018-09-06 stsp }
433 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
434 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
435 a440fac0 2018-09-06 stsp } else {
436 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
437 a440fac0 2018-09-06 stsp goto done;
438 a440fac0 2018-09-06 stsp }
439 a440fac0 2018-09-06 stsp
440 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
441 a440fac0 2018-09-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
442 a440fac0 2018-09-06 stsp remain -= tlen;
443 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
444 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
445 a440fac0 2018-09-06 stsp goto done;
446 a440fac0 2018-09-06 stsp }
447 a440fac0 2018-09-06 stsp s += tlen;
448 a440fac0 2018-09-06 stsp err = got_object_commit_add_parent(*commit, s);
449 a440fac0 2018-09-06 stsp if (err)
450 a440fac0 2018-09-06 stsp goto done;
451 a440fac0 2018-09-06 stsp
452 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
453 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
454 a440fac0 2018-09-06 stsp }
455 a440fac0 2018-09-06 stsp
456 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
457 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
458 a440fac0 2018-09-06 stsp char *p;
459 a440fac0 2018-09-06 stsp size_t slen;
460 a440fac0 2018-09-06 stsp
461 a440fac0 2018-09-06 stsp remain -= tlen;
462 a440fac0 2018-09-06 stsp if (remain <= 0) {
463 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
464 a440fac0 2018-09-06 stsp goto done;
465 a440fac0 2018-09-06 stsp }
466 a440fac0 2018-09-06 stsp s += tlen;
467 a440fac0 2018-09-06 stsp p = strchr(s, '\n');
468 a440fac0 2018-09-06 stsp if (p == NULL) {
469 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
470 a440fac0 2018-09-06 stsp goto done;
471 a440fac0 2018-09-06 stsp }
472 a440fac0 2018-09-06 stsp *p = '\0';
473 a440fac0 2018-09-06 stsp slen = strlen(s);
474 a440fac0 2018-09-06 stsp err = parse_commit_time(&(*commit)->tm_author, s);
475 a440fac0 2018-09-06 stsp if (err)
476 a440fac0 2018-09-06 stsp goto done;
477 a440fac0 2018-09-06 stsp (*commit)->author = strdup(s);
478 a440fac0 2018-09-06 stsp if ((*commit)->author == NULL) {
479 a440fac0 2018-09-06 stsp err = got_error_from_errno();
480 a440fac0 2018-09-06 stsp goto done;
481 a440fac0 2018-09-06 stsp }
482 a440fac0 2018-09-06 stsp s += slen + 1;
483 a440fac0 2018-09-06 stsp remain -= slen + 1;
484 a440fac0 2018-09-06 stsp }
485 a440fac0 2018-09-06 stsp
486 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
487 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
488 a440fac0 2018-09-06 stsp char *p;
489 a440fac0 2018-09-06 stsp size_t slen;
490 a440fac0 2018-09-06 stsp
491 a440fac0 2018-09-06 stsp remain -= tlen;
492 a440fac0 2018-09-06 stsp if (remain <= 0) {
493 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
494 a440fac0 2018-09-06 stsp goto done;
495 a440fac0 2018-09-06 stsp }
496 a440fac0 2018-09-06 stsp s += tlen;
497 a440fac0 2018-09-06 stsp p = strchr(s, '\n');
498 a440fac0 2018-09-06 stsp if (p == NULL) {
499 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
500 a440fac0 2018-09-06 stsp goto done;
501 a440fac0 2018-09-06 stsp }
502 a440fac0 2018-09-06 stsp *p = '\0';
503 a440fac0 2018-09-06 stsp slen = strlen(s);
504 a440fac0 2018-09-06 stsp err = parse_commit_time(&(*commit)->tm_committer, s);
505 a440fac0 2018-09-06 stsp if (err)
506 a440fac0 2018-09-06 stsp goto done;
507 a440fac0 2018-09-06 stsp (*commit)->committer = strdup(s);
508 a440fac0 2018-09-06 stsp if ((*commit)->committer == NULL) {
509 a440fac0 2018-09-06 stsp err = got_error_from_errno();
510 a440fac0 2018-09-06 stsp goto done;
511 a440fac0 2018-09-06 stsp }
512 a440fac0 2018-09-06 stsp s += slen + 1;
513 a440fac0 2018-09-06 stsp remain -= slen + 1;
514 a440fac0 2018-09-06 stsp }
515 a440fac0 2018-09-06 stsp
516 a440fac0 2018-09-06 stsp (*commit)->logmsg = strndup(s, remain);
517 a440fac0 2018-09-06 stsp if ((*commit)->logmsg == NULL) {
518 a440fac0 2018-09-06 stsp err = got_error_from_errno();
519 a440fac0 2018-09-06 stsp goto done;
520 a440fac0 2018-09-06 stsp }
521 a440fac0 2018-09-06 stsp done:
522 a440fac0 2018-09-06 stsp if (err) {
523 a440fac0 2018-09-06 stsp got_object_commit_close(*commit);
524 a440fac0 2018-09-06 stsp *commit = NULL;
525 a440fac0 2018-09-06 stsp }
526 a440fac0 2018-09-06 stsp return err;
527 a440fac0 2018-09-06 stsp }
528 a440fac0 2018-09-06 stsp
529 ad242220 2018-09-08 stsp void
530 ad242220 2018-09-08 stsp got_object_tree_entry_close(struct got_tree_entry *te)
531 a440fac0 2018-09-06 stsp {
532 a440fac0 2018-09-06 stsp free(te->id);
533 a440fac0 2018-09-06 stsp free(te->name);
534 a440fac0 2018-09-06 stsp free(te);
535 a440fac0 2018-09-06 stsp }
536 a440fac0 2018-09-06 stsp
537 03fa71c8 2018-09-06 stsp void
538 03fa71c8 2018-09-06 stsp got_object_tree_close(struct got_tree_object *tree)
539 03fa71c8 2018-09-06 stsp {
540 03fa71c8 2018-09-06 stsp struct got_tree_entry *te;
541 03fa71c8 2018-09-06 stsp
542 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0) {
543 03fa71c8 2018-09-06 stsp tree->refcnt--;
544 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0)
545 03fa71c8 2018-09-06 stsp return;
546 03fa71c8 2018-09-06 stsp }
547 03fa71c8 2018-09-06 stsp
548 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
549 03fa71c8 2018-09-06 stsp te = SIMPLEQ_FIRST(&tree->entries.head);
550 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
551 ad242220 2018-09-08 stsp got_object_tree_entry_close(te);
552 03fa71c8 2018-09-06 stsp }
553 03fa71c8 2018-09-06 stsp
554 03fa71c8 2018-09-06 stsp free(tree);
555 03fa71c8 2018-09-06 stsp }
556 03fa71c8 2018-09-06 stsp
557 a440fac0 2018-09-06 stsp struct got_tree_entry *
558 a440fac0 2018-09-06 stsp got_alloc_tree_entry_partial(void)
559 a440fac0 2018-09-06 stsp {
560 a440fac0 2018-09-06 stsp struct got_tree_entry *te;
561 a440fac0 2018-09-06 stsp
562 a440fac0 2018-09-06 stsp te = calloc(1, sizeof(*te));
563 a440fac0 2018-09-06 stsp if (te == NULL)
564 a440fac0 2018-09-06 stsp return NULL;
565 a440fac0 2018-09-06 stsp
566 a440fac0 2018-09-06 stsp te->id = calloc(1, sizeof(*te->id));
567 a440fac0 2018-09-06 stsp if (te->id == NULL) {
568 a440fac0 2018-09-06 stsp free(te);
569 a440fac0 2018-09-06 stsp te = NULL;
570 a440fac0 2018-09-06 stsp }
571 a440fac0 2018-09-06 stsp return te;
572 a440fac0 2018-09-06 stsp }
573 a440fac0 2018-09-06 stsp
574 a440fac0 2018-09-06 stsp static const struct got_error *
575 a440fac0 2018-09-06 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
576 a440fac0 2018-09-06 stsp size_t maxlen)
577 a440fac0 2018-09-06 stsp {
578 a440fac0 2018-09-06 stsp char *p = buf, *space;
579 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
580 a440fac0 2018-09-06 stsp
581 a440fac0 2018-09-06 stsp *te = got_alloc_tree_entry_partial();
582 a440fac0 2018-09-06 stsp if (*te == NULL)
583 a440fac0 2018-09-06 stsp return got_error_from_errno();
584 a440fac0 2018-09-06 stsp
585 a440fac0 2018-09-06 stsp *elen = strlen(buf) + 1;
586 a440fac0 2018-09-06 stsp if (*elen > maxlen) {
587 a440fac0 2018-09-06 stsp free(*te);
588 a440fac0 2018-09-06 stsp *te = NULL;
589 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
590 a440fac0 2018-09-06 stsp }
591 a440fac0 2018-09-06 stsp
592 a440fac0 2018-09-06 stsp space = strchr(buf, ' ');
593 a440fac0 2018-09-06 stsp if (space == NULL) {
594 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
595 a440fac0 2018-09-06 stsp free(*te);
596 a440fac0 2018-09-06 stsp *te = NULL;
597 a440fac0 2018-09-06 stsp return err;
598 a440fac0 2018-09-06 stsp }
599 a440fac0 2018-09-06 stsp while (*p != ' ') {
600 a440fac0 2018-09-06 stsp if (*p < '0' && *p > '7') {
601 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
602 a440fac0 2018-09-06 stsp goto done;
603 a440fac0 2018-09-06 stsp }
604 a440fac0 2018-09-06 stsp (*te)->mode <<= 3;
605 a440fac0 2018-09-06 stsp (*te)->mode |= *p - '0';
606 a440fac0 2018-09-06 stsp p++;
607 a440fac0 2018-09-06 stsp }
608 a440fac0 2018-09-06 stsp
609 a440fac0 2018-09-06 stsp (*te)->name = strdup(space + 1);
610 a440fac0 2018-09-06 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
611 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
612 a440fac0 2018-09-06 stsp goto done;
613 a440fac0 2018-09-06 stsp }
614 a440fac0 2018-09-06 stsp buf += strlen(buf) + 1;
615 a440fac0 2018-09-06 stsp memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
616 a440fac0 2018-09-06 stsp *elen += SHA1_DIGEST_LENGTH;
617 a440fac0 2018-09-06 stsp done:
618 a440fac0 2018-09-06 stsp if (err) {
619 ad242220 2018-09-08 stsp got_object_tree_entry_close(*te);
620 a440fac0 2018-09-06 stsp *te = NULL;
621 a440fac0 2018-09-06 stsp }
622 a440fac0 2018-09-06 stsp return err;
623 a440fac0 2018-09-06 stsp }
624 a440fac0 2018-09-06 stsp
625 a440fac0 2018-09-06 stsp const struct got_error *
626 a440fac0 2018-09-06 stsp got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
627 a440fac0 2018-09-06 stsp {
628 a440fac0 2018-09-06 stsp const struct got_error *err;
629 a440fac0 2018-09-06 stsp size_t remain = len;
630 a440fac0 2018-09-06 stsp
631 a440fac0 2018-09-06 stsp *tree = calloc(1, sizeof(**tree));
632 a440fac0 2018-09-06 stsp if (*tree == NULL)
633 a440fac0 2018-09-06 stsp return got_error_from_errno();
634 a440fac0 2018-09-06 stsp
635 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&(*tree)->entries.head);
636 a440fac0 2018-09-06 stsp
637 a440fac0 2018-09-06 stsp while (remain > 0) {
638 a440fac0 2018-09-06 stsp struct got_tree_entry *te;
639 a440fac0 2018-09-06 stsp size_t elen;
640 a440fac0 2018-09-06 stsp
641 a440fac0 2018-09-06 stsp err = parse_tree_entry(&te, &elen, buf, remain);
642 a440fac0 2018-09-06 stsp if (err)
643 a440fac0 2018-09-06 stsp return err;
644 a440fac0 2018-09-06 stsp (*tree)->entries.nentries++;
645 a440fac0 2018-09-06 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
646 a440fac0 2018-09-06 stsp buf += elen;
647 a440fac0 2018-09-06 stsp remain -= elen;
648 a440fac0 2018-09-06 stsp }
649 a440fac0 2018-09-06 stsp
650 a440fac0 2018-09-06 stsp if (remain != 0) {
651 a440fac0 2018-09-06 stsp got_object_tree_close(*tree);
652 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
653 a440fac0 2018-09-06 stsp }
654 a440fac0 2018-09-06 stsp
655 a440fac0 2018-09-06 stsp return NULL;
656 a440fac0 2018-09-06 stsp }
657 a440fac0 2018-09-06 stsp
658 ad242220 2018-09-08 stsp const struct got_error *
659 ad242220 2018-09-08 stsp got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
660 a440fac0 2018-09-06 stsp {
661 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
662 a440fac0 2018-09-06 stsp static const size_t blocksize = 512;
663 a440fac0 2018-09-06 stsp size_t n, total, remain;
664 a440fac0 2018-09-06 stsp uint8_t *buf;
665 a440fac0 2018-09-06 stsp
666 a440fac0 2018-09-06 stsp *outbuf = NULL;
667 a440fac0 2018-09-06 stsp *outlen = 0;
668 a440fac0 2018-09-06 stsp
669 a440fac0 2018-09-06 stsp buf = malloc(blocksize);
670 a440fac0 2018-09-06 stsp if (buf == NULL)
671 a440fac0 2018-09-06 stsp return got_error_from_errno();
672 a440fac0 2018-09-06 stsp
673 a440fac0 2018-09-06 stsp remain = blocksize;
674 a440fac0 2018-09-06 stsp total = 0;
675 a440fac0 2018-09-06 stsp while (1) {
676 a440fac0 2018-09-06 stsp if (remain == 0) {
677 a440fac0 2018-09-06 stsp uint8_t *newbuf;
678 a440fac0 2018-09-06 stsp newbuf = reallocarray(buf, 1, total + blocksize);
679 a440fac0 2018-09-06 stsp if (newbuf == NULL) {
680 a440fac0 2018-09-06 stsp err = got_error_from_errno();
681 a440fac0 2018-09-06 stsp goto done;
682 a440fac0 2018-09-06 stsp }
683 a440fac0 2018-09-06 stsp buf = newbuf;
684 a440fac0 2018-09-06 stsp remain += blocksize;
685 a440fac0 2018-09-06 stsp }
686 a440fac0 2018-09-06 stsp n = fread(buf + total, 1, remain, f);
687 a440fac0 2018-09-06 stsp if (n == 0) {
688 a440fac0 2018-09-06 stsp if (ferror(f)) {
689 a440fac0 2018-09-06 stsp err = got_ferror(f, GOT_ERR_IO);
690 a440fac0 2018-09-06 stsp goto done;
691 a440fac0 2018-09-06 stsp }
692 a440fac0 2018-09-06 stsp break; /* EOF */
693 a440fac0 2018-09-06 stsp }
694 a440fac0 2018-09-06 stsp remain -= n;
695 a440fac0 2018-09-06 stsp total += n;
696 a440fac0 2018-09-06 stsp };
697 a440fac0 2018-09-06 stsp
698 a440fac0 2018-09-06 stsp done:
699 a440fac0 2018-09-06 stsp if (err == NULL) {
700 a440fac0 2018-09-06 stsp *outbuf = buf;
701 a440fac0 2018-09-06 stsp *outlen = total;
702 a440fac0 2018-09-06 stsp } else
703 a440fac0 2018-09-06 stsp free(buf);
704 a440fac0 2018-09-06 stsp return err;
705 a440fac0 2018-09-06 stsp }
706 a440fac0 2018-09-06 stsp
707 a440fac0 2018-09-06 stsp static const struct got_error *
708 ad242220 2018-09-08 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
709 ad242220 2018-09-08 stsp struct got_object *obj, int fd)
710 a440fac0 2018-09-06 stsp {
711 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
712 3516b818 2018-09-08 stsp struct imsgbuf *ibuf;
713 a440fac0 2018-09-06 stsp
714 3516b818 2018-09-08 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
715 a440fac0 2018-09-06 stsp
716 cfd633c2 2018-09-10 stsp err = got_privsep_send_obj_req(ibuf, fd, obj);
717 3516b818 2018-09-08 stsp if (err)
718 3516b818 2018-09-08 stsp return err;
719 a440fac0 2018-09-06 stsp
720 3516b818 2018-09-08 stsp return got_privsep_recv_commit(commit, ibuf);
721 a440fac0 2018-09-06 stsp }
722 a440fac0 2018-09-06 stsp
723 a440fac0 2018-09-06 stsp const struct got_error *
724 cfd633c2 2018-09-10 stsp got_object_read_packed_commit_privsep(struct got_commit_object **commit,
725 cfd633c2 2018-09-10 stsp struct got_object *obj, struct got_pack *pack)
726 cfd633c2 2018-09-10 stsp {
727 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
728 cfd633c2 2018-09-10 stsp
729 cfd633c2 2018-09-10 stsp err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
730 cfd633c2 2018-09-10 stsp if (err)
731 cfd633c2 2018-09-10 stsp return err;
732 cfd633c2 2018-09-10 stsp
733 cfd633c2 2018-09-10 stsp return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
734 cfd633c2 2018-09-10 stsp }
735 cfd633c2 2018-09-10 stsp
736 cfd633c2 2018-09-10 stsp const struct got_error *
737 a440fac0 2018-09-06 stsp got_object_read_commit_privsep(struct got_commit_object **commit,
738 ad242220 2018-09-08 stsp struct got_object *obj, int obj_fd, struct got_repository *repo)
739 a440fac0 2018-09-06 stsp {
740 a440fac0 2018-09-06 stsp int imsg_fds[2];
741 a440fac0 2018-09-06 stsp pid_t pid;
742 3516b818 2018-09-08 stsp struct imsgbuf *ibuf;
743 a440fac0 2018-09-06 stsp
744 ad242220 2018-09-08 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
745 ad242220 2018-09-08 stsp return request_commit(commit, repo, obj, obj_fd);
746 ad242220 2018-09-08 stsp
747 3516b818 2018-09-08 stsp ibuf = calloc(1, sizeof(*ibuf));
748 3516b818 2018-09-08 stsp if (ibuf == NULL)
749 3516b818 2018-09-08 stsp return got_error_from_errno();
750 3516b818 2018-09-08 stsp
751 a440fac0 2018-09-06 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
752 a440fac0 2018-09-06 stsp return got_error_from_errno();
753 a440fac0 2018-09-06 stsp
754 a440fac0 2018-09-06 stsp pid = fork();
755 a440fac0 2018-09-06 stsp if (pid == -1)
756 a440fac0 2018-09-06 stsp return got_error_from_errno();
757 a440fac0 2018-09-06 stsp else if (pid == 0) {
758 3cab8b4d 2018-09-08 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
759 3cab8b4d 2018-09-08 stsp repo->path);
760 a440fac0 2018-09-06 stsp /* not reached */
761 a440fac0 2018-09-06 stsp }
762 a440fac0 2018-09-06 stsp
763 a440fac0 2018-09-06 stsp close(imsg_fds[1]);
764 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
765 ad242220 2018-09-08 stsp imsg_fds[0];
766 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
767 3516b818 2018-09-08 stsp imsg_init(ibuf, imsg_fds[0]);
768 3516b818 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
769 ad242220 2018-09-08 stsp
770 ad242220 2018-09-08 stsp return request_commit(commit, repo, obj, obj_fd);
771 a440fac0 2018-09-06 stsp }
772 a440fac0 2018-09-06 stsp
773 a440fac0 2018-09-06 stsp static const struct got_error *
774 ad242220 2018-09-08 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
775 ad242220 2018-09-08 stsp struct got_object *obj, int fd)
776 a440fac0 2018-09-06 stsp {
777 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
778 3516b818 2018-09-08 stsp struct imsgbuf *ibuf;
779 a440fac0 2018-09-06 stsp
780 3516b818 2018-09-08 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
781 a440fac0 2018-09-06 stsp
782 cfd633c2 2018-09-10 stsp err = got_privsep_send_obj_req(ibuf, fd, obj);
783 a440fac0 2018-09-06 stsp if (err)
784 3516b818 2018-09-08 stsp return err;
785 a440fac0 2018-09-06 stsp
786 3516b818 2018-09-08 stsp return got_privsep_recv_tree(tree, ibuf);
787 a440fac0 2018-09-06 stsp }
788 a440fac0 2018-09-06 stsp
789 a440fac0 2018-09-06 stsp const struct got_error *
790 a440fac0 2018-09-06 stsp got_object_read_tree_privsep(struct got_tree_object **tree,
791 ad242220 2018-09-08 stsp struct got_object *obj, int obj_fd, struct got_repository *repo)
792 a440fac0 2018-09-06 stsp {
793 a440fac0 2018-09-06 stsp int imsg_fds[2];
794 a440fac0 2018-09-06 stsp pid_t pid;
795 3516b818 2018-09-08 stsp struct imsgbuf *ibuf;
796 a440fac0 2018-09-06 stsp
797 ad242220 2018-09-08 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
798 ad242220 2018-09-08 stsp return request_tree(tree, repo, obj, obj_fd);
799 ad242220 2018-09-08 stsp
800 3516b818 2018-09-08 stsp ibuf = calloc(1, sizeof(*ibuf));
801 3516b818 2018-09-08 stsp if (ibuf == NULL)
802 3516b818 2018-09-08 stsp return got_error_from_errno();
803 3516b818 2018-09-08 stsp
804 a440fac0 2018-09-06 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
805 a440fac0 2018-09-06 stsp return got_error_from_errno();
806 a440fac0 2018-09-06 stsp
807 a440fac0 2018-09-06 stsp pid = fork();
808 a440fac0 2018-09-06 stsp if (pid == -1)
809 a440fac0 2018-09-06 stsp return got_error_from_errno();
810 a440fac0 2018-09-06 stsp else if (pid == 0) {
811 3cab8b4d 2018-09-08 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
812 3cab8b4d 2018-09-08 stsp repo->path);
813 a440fac0 2018-09-06 stsp /* not reached */
814 a440fac0 2018-09-06 stsp }
815 a440fac0 2018-09-06 stsp
816 a440fac0 2018-09-06 stsp close(imsg_fds[1]);
817 3516b818 2018-09-08 stsp
818 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
819 ad242220 2018-09-08 stsp imsg_fds[0];
820 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
821 3516b818 2018-09-08 stsp imsg_init(ibuf, imsg_fds[0]);
822 3516b818 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
823 ad242220 2018-09-08 stsp
824 3516b818 2018-09-08 stsp
825 ad242220 2018-09-08 stsp return request_tree(tree, repo, obj, obj_fd);
826 a440fac0 2018-09-06 stsp }
827 e7885405 2018-09-10 stsp
828 e7885405 2018-09-10 stsp const struct got_error *
829 e7885405 2018-09-10 stsp got_object_read_packed_tree_privsep(struct got_tree_object **tree,
830 e7885405 2018-09-10 stsp struct got_object *obj, struct got_pack *pack)
831 e7885405 2018-09-10 stsp {
832 e7885405 2018-09-10 stsp const struct got_error *err = NULL;
833 a440fac0 2018-09-06 stsp
834 e7885405 2018-09-10 stsp err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
835 e7885405 2018-09-10 stsp if (err)
836 e7885405 2018-09-10 stsp return err;
837 e7885405 2018-09-10 stsp
838 e7885405 2018-09-10 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
839 55da3778 2018-09-10 stsp }
840 55da3778 2018-09-10 stsp
841 55da3778 2018-09-10 stsp static const struct got_error *
842 55da3778 2018-09-10 stsp request_blob(size_t *size, int outfd, int infd, struct imsgbuf *ibuf)
843 55da3778 2018-09-10 stsp {
844 55da3778 2018-09-10 stsp const struct got_error *err = NULL;
845 55da3778 2018-09-10 stsp int outfd_child;
846 55da3778 2018-09-10 stsp
847 55da3778 2018-09-10 stsp outfd_child = dup(outfd);
848 55da3778 2018-09-10 stsp if (outfd_child == -1)
849 55da3778 2018-09-10 stsp return got_error_from_errno();
850 55da3778 2018-09-10 stsp
851 55da3778 2018-09-10 stsp err = got_privsep_send_blob_req(ibuf, infd);
852 55da3778 2018-09-10 stsp if (err)
853 55da3778 2018-09-10 stsp return err;
854 55da3778 2018-09-10 stsp
855 55da3778 2018-09-10 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
856 55da3778 2018-09-10 stsp if (err) {
857 55da3778 2018-09-10 stsp close(outfd_child);
858 55da3778 2018-09-10 stsp return err;
859 55da3778 2018-09-10 stsp }
860 55da3778 2018-09-10 stsp
861 3516b818 2018-09-08 stsp err = got_privsep_recv_blob(size, ibuf);
862 a440fac0 2018-09-06 stsp if (err)
863 3516b818 2018-09-08 stsp return err;
864 a440fac0 2018-09-06 stsp
865 ad242220 2018-09-08 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
866 3516b818 2018-09-08 stsp return got_error_from_errno();
867 3516b818 2018-09-08 stsp
868 ad242220 2018-09-08 stsp return err;
869 a440fac0 2018-09-06 stsp }
870 a440fac0 2018-09-06 stsp
871 a440fac0 2018-09-06 stsp const struct got_error *
872 ad242220 2018-09-08 stsp got_object_read_blob_privsep(size_t *size, int outfd, int infd,
873 ad242220 2018-09-08 stsp struct got_repository *repo)
874 a440fac0 2018-09-06 stsp {
875 a440fac0 2018-09-06 stsp int imsg_fds[2];
876 a440fac0 2018-09-06 stsp pid_t pid;
877 3516b818 2018-09-08 stsp struct imsgbuf *ibuf;
878 a440fac0 2018-09-06 stsp
879 55da3778 2018-09-10 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
880 55da3778 2018-09-10 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
881 55da3778 2018-09-10 stsp return request_blob(size, outfd, infd, ibuf);
882 55da3778 2018-09-10 stsp }
883 ad242220 2018-09-08 stsp
884 3516b818 2018-09-08 stsp ibuf = calloc(1, sizeof(*ibuf));
885 3516b818 2018-09-08 stsp if (ibuf == NULL)
886 3516b818 2018-09-08 stsp return got_error_from_errno();
887 3516b818 2018-09-08 stsp
888 a440fac0 2018-09-06 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
889 a440fac0 2018-09-06 stsp return got_error_from_errno();
890 a440fac0 2018-09-06 stsp
891 a440fac0 2018-09-06 stsp pid = fork();
892 a440fac0 2018-09-06 stsp if (pid == -1)
893 a440fac0 2018-09-06 stsp return got_error_from_errno();
894 a440fac0 2018-09-06 stsp else if (pid == 0) {
895 3cab8b4d 2018-09-08 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
896 3cab8b4d 2018-09-08 stsp repo->path);
897 a440fac0 2018-09-06 stsp /* not reached */
898 a440fac0 2018-09-06 stsp }
899 a440fac0 2018-09-06 stsp
900 a440fac0 2018-09-06 stsp close(imsg_fds[1]);
901 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
902 ad242220 2018-09-08 stsp imsg_fds[0];
903 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
904 3516b818 2018-09-08 stsp imsg_init(ibuf, imsg_fds[0]);
905 3516b818 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
906 ad242220 2018-09-08 stsp
907 55da3778 2018-09-10 stsp return request_blob(size, outfd, infd, ibuf);
908 a440fac0 2018-09-06 stsp }