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