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