Blame


1 d71d75ad 2017-11-05 stsp /*
2 a1fd68d8 2018-01-12 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 d71d75ad 2017-11-05 stsp *
4 d71d75ad 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 d71d75ad 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 d71d75ad 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
7 d71d75ad 2017-11-05 stsp *
8 d71d75ad 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 d71d75ad 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 d71d75ad 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 d71d75ad 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 d71d75ad 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 d71d75ad 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 d71d75ad 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 d71d75ad 2017-11-05 stsp */
16 d71d75ad 2017-11-05 stsp
17 2178c42e 2018-04-22 stsp #include <sys/types.h>
18 0ffeb3c2 2017-11-26 stsp #include <sys/stat.h>
19 d1cda826 2017-11-06 stsp #include <sys/queue.h>
20 2178c42e 2018-04-22 stsp #include <sys/uio.h>
21 2178c42e 2018-04-22 stsp #include <sys/socket.h>
22 2178c42e 2018-04-22 stsp #include <sys/wait.h>
23 d1cda826 2017-11-06 stsp
24 a1fd68d8 2018-01-12 stsp #include <errno.h>
25 2178c42e 2018-04-22 stsp #include <fcntl.h>
26 d71d75ad 2017-11-05 stsp #include <stdio.h>
27 ab9a70b2 2017-11-06 stsp #include <stdlib.h>
28 ab9a70b2 2017-11-06 stsp #include <string.h>
29 2178c42e 2018-04-22 stsp #include <stdint.h>
30 d71d75ad 2017-11-05 stsp #include <sha1.h>
31 ab9a70b2 2017-11-06 stsp #include <zlib.h>
32 ab9a70b2 2017-11-06 stsp #include <ctype.h>
33 ab9a70b2 2017-11-06 stsp #include <limits.h>
34 2178c42e 2018-04-22 stsp #include <imsg.h>
35 788c352e 2018-06-16 stsp #include <time.h>
36 d71d75ad 2017-11-05 stsp
37 ab9a70b2 2017-11-06 stsp #include "got_error.h"
38 d71d75ad 2017-11-05 stsp #include "got_object.h"
39 ab9a70b2 2017-11-06 stsp #include "got_repository.h"
40 511a516b 2018-05-19 stsp #include "got_opentemp.h"
41 d71d75ad 2017-11-05 stsp
42 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
43 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
44 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
45 2178c42e 2018-04-22 stsp #include "got_lib_path.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_zbuf.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
48 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
49 7bb0daa1 2018-06-21 stsp #include "got_lib_repository.h"
50 1411938b 2018-02-12 stsp
51 ab9a70b2 2017-11-06 stsp #ifndef MIN
52 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 ab9a70b2 2017-11-06 stsp #endif
54 ab9a70b2 2017-11-06 stsp
55 ab9a70b2 2017-11-06 stsp #ifndef nitems
56 ab9a70b2 2017-11-06 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
57 ab9a70b2 2017-11-06 stsp #endif
58 ab9a70b2 2017-11-06 stsp
59 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_COMMIT "commit"
60 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_TREE "tree"
61 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_BLOB "blob"
62 ab9a70b2 2017-11-06 stsp
63 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_TREE "tree "
64 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_PARENT "parent "
65 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_AUTHOR "author "
66 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_COMMITTER "committer "
67 d1cda826 2017-11-06 stsp
68 ef0981d5 2018-02-12 stsp const struct got_error *
69 ef0981d5 2018-02-12 stsp got_object_id_str(char **outbuf, struct got_object_id *id)
70 d71d75ad 2017-11-05 stsp {
71 ef0981d5 2018-02-12 stsp static const size_t len = SHA1_DIGEST_STRING_LENGTH;
72 ef0981d5 2018-02-12 stsp
73 ef0981d5 2018-02-12 stsp *outbuf = calloc(1, len);
74 ef0981d5 2018-02-12 stsp if (*outbuf == NULL)
75 0a585a0d 2018-03-17 stsp return got_error_from_errno();
76 ef0981d5 2018-02-12 stsp
77 ef0981d5 2018-02-12 stsp if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
78 ef0981d5 2018-02-12 stsp free(*outbuf);
79 ef0981d5 2018-02-12 stsp *outbuf = NULL;
80 ef0981d5 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
81 ef0981d5 2018-02-12 stsp }
82 ef0981d5 2018-02-12 stsp
83 ef0981d5 2018-02-12 stsp return NULL;
84 59ece79d 2018-02-12 stsp }
85 59ece79d 2018-02-12 stsp
86 a1fd68d8 2018-01-12 stsp int
87 a1fd68d8 2018-01-12 stsp got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
88 a1fd68d8 2018-01-12 stsp {
89 a1fd68d8 2018-01-12 stsp return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
90 8bf5b3c9 2018-03-17 stsp }
91 8bf5b3c9 2018-03-17 stsp
92 8bf5b3c9 2018-03-17 stsp struct got_object_id *
93 8bf5b3c9 2018-03-17 stsp got_object_id_dup(struct got_object_id *id1)
94 8bf5b3c9 2018-03-17 stsp {
95 8bf5b3c9 2018-03-17 stsp struct got_object_id *id2;
96 8bf5b3c9 2018-03-17 stsp
97 8bf5b3c9 2018-03-17 stsp id2 = malloc(sizeof(*id2));
98 8bf5b3c9 2018-03-17 stsp if (id2 == NULL)
99 8bf5b3c9 2018-03-17 stsp return NULL;
100 8bf5b3c9 2018-03-17 stsp memcpy(id2, id1, sizeof(*id2));
101 8bf5b3c9 2018-03-17 stsp return id2;
102 3235492e 2018-04-01 stsp }
103 3235492e 2018-04-01 stsp
104 3235492e 2018-04-01 stsp struct got_object_id *
105 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
106 3235492e 2018-04-01 stsp {
107 3235492e 2018-04-01 stsp return got_object_id_dup(&obj->id);
108 bacc9935 2018-05-20 stsp }
109 bacc9935 2018-05-20 stsp
110 bacc9935 2018-05-20 stsp const struct got_error *
111 bacc9935 2018-05-20 stsp got_object_get_id_str(char **outbuf, struct got_object *obj)
112 bacc9935 2018-05-20 stsp {
113 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
114 a1fd68d8 2018-01-12 stsp }
115 d71d75ad 2017-11-05 stsp
116 b107e67f 2018-01-19 stsp int
117 b107e67f 2018-01-19 stsp got_object_get_type(struct got_object *obj)
118 a1fd68d8 2018-01-12 stsp {
119 b107e67f 2018-01-19 stsp switch (obj->type) {
120 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
121 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
122 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
123 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
124 b107e67f 2018-01-19 stsp return obj->type;
125 96f5e8b3 2018-01-23 stsp default:
126 96f5e8b3 2018-01-23 stsp abort();
127 96f5e8b3 2018-01-23 stsp break;
128 d71d75ad 2017-11-05 stsp }
129 d71d75ad 2017-11-05 stsp
130 96f5e8b3 2018-01-23 stsp /* not reached */
131 96f5e8b3 2018-01-23 stsp return 0;
132 d71d75ad 2017-11-05 stsp }
133 ab9a70b2 2017-11-06 stsp
134 ab9a70b2 2017-11-06 stsp static const struct got_error *
135 d1cda826 2017-11-06 stsp parse_object_header(struct got_object **obj, char *buf, size_t len)
136 ab9a70b2 2017-11-06 stsp {
137 ab9a70b2 2017-11-06 stsp const char *obj_tags[] = {
138 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_COMMIT,
139 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_TREE,
140 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_BLOB
141 ab9a70b2 2017-11-06 stsp };
142 ab9a70b2 2017-11-06 stsp const int obj_types[] = {
143 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_COMMIT,
144 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_TREE,
145 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_BLOB,
146 ab9a70b2 2017-11-06 stsp };
147 ab9a70b2 2017-11-06 stsp int type = 0;
148 d1cda826 2017-11-06 stsp size_t size = 0, hdrlen = 0;
149 ab9a70b2 2017-11-06 stsp int i;
150 ab9a70b2 2017-11-06 stsp char *p = strchr(buf, '\0');
151 ab9a70b2 2017-11-06 stsp
152 ab9a70b2 2017-11-06 stsp if (p == NULL)
153 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
154 ab9a70b2 2017-11-06 stsp
155 d1cda826 2017-11-06 stsp hdrlen = strlen(buf) + 1 /* '\0' */;
156 d1cda826 2017-11-06 stsp
157 ab9a70b2 2017-11-06 stsp for (i = 0; i < nitems(obj_tags); i++) {
158 ab9a70b2 2017-11-06 stsp const char *tag = obj_tags[i];
159 63323519 2017-11-06 stsp size_t tlen = strlen(tag);
160 ab9a70b2 2017-11-06 stsp const char *errstr;
161 ab9a70b2 2017-11-06 stsp
162 63323519 2017-11-06 stsp if (strncmp(buf, tag, tlen) != 0)
163 ab9a70b2 2017-11-06 stsp continue;
164 ab9a70b2 2017-11-06 stsp
165 ab9a70b2 2017-11-06 stsp type = obj_types[i];
166 63323519 2017-11-06 stsp if (len <= tlen)
167 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
168 63323519 2017-11-06 stsp size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
169 ab9a70b2 2017-11-06 stsp if (errstr != NULL)
170 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
171 ab9a70b2 2017-11-06 stsp break;
172 ab9a70b2 2017-11-06 stsp }
173 ab9a70b2 2017-11-06 stsp
174 ab9a70b2 2017-11-06 stsp if (type == 0)
175 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
176 ab9a70b2 2017-11-06 stsp
177 ab9a70b2 2017-11-06 stsp *obj = calloc(1, sizeof(**obj));
178 1db76ab5 2018-01-26 mpi if (*obj == NULL)
179 0a585a0d 2018-03-17 stsp return got_error_from_errno();
180 ab9a70b2 2017-11-06 stsp (*obj)->type = type;
181 d1cda826 2017-11-06 stsp (*obj)->hdrlen = hdrlen;
182 ab9a70b2 2017-11-06 stsp (*obj)->size = size;
183 ab9a70b2 2017-11-06 stsp return NULL;
184 ab9a70b2 2017-11-06 stsp }
185 ab9a70b2 2017-11-06 stsp
186 ab9a70b2 2017-11-06 stsp static const struct got_error *
187 2178c42e 2018-04-22 stsp read_object_header(struct got_object **obj, FILE *f)
188 ab9a70b2 2017-11-06 stsp {
189 ab9a70b2 2017-11-06 stsp const struct got_error *err;
190 ab9a70b2 2017-11-06 stsp struct got_zstream_buf zb;
191 a3e2cbea 2017-12-01 stsp char *buf;
192 a3e2cbea 2017-12-01 stsp const size_t zbsize = 64;
193 744d9326 2017-12-01 stsp size_t outlen, totlen;
194 25783624 2018-03-12 stsp int i;
195 ab9a70b2 2017-11-06 stsp
196 744d9326 2017-12-01 stsp buf = calloc(zbsize, sizeof(char));
197 a3e2cbea 2017-12-01 stsp if (buf == NULL)
198 0a585a0d 2018-03-17 stsp return got_error_from_errno();
199 a3e2cbea 2017-12-01 stsp
200 19d747f7 2018-03-16 stsp err = got_inflate_init(&zb, NULL, zbsize);
201 a1fd68d8 2018-01-12 stsp if (err)
202 ab9a70b2 2017-11-06 stsp return err;
203 ab9a70b2 2017-11-06 stsp
204 a3e2cbea 2017-12-01 stsp i = 0;
205 744d9326 2017-12-01 stsp totlen = 0;
206 a3e2cbea 2017-12-01 stsp do {
207 126ee060 2018-02-11 stsp err = got_inflate_read(&zb, f, &outlen);
208 a3e2cbea 2017-12-01 stsp if (err)
209 a3e2cbea 2017-12-01 stsp goto done;
210 e302c59e 2017-12-01 stsp if (strchr(zb.outbuf, '\0') == NULL) {
211 a3e2cbea 2017-12-01 stsp buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
212 e302c59e 2017-12-01 stsp if (buf == NULL) {
213 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
214 e302c59e 2017-12-01 stsp goto done;
215 e302c59e 2017-12-01 stsp }
216 e302c59e 2017-12-01 stsp }
217 c56976de 2017-12-01 stsp memcpy(buf + totlen, zb.outbuf, outlen);
218 744d9326 2017-12-01 stsp totlen += outlen;
219 a3e2cbea 2017-12-01 stsp i++;
220 a3e2cbea 2017-12-01 stsp } while (strchr(zb.outbuf, '\0') == NULL);
221 ab9a70b2 2017-11-06 stsp
222 744d9326 2017-12-01 stsp err = parse_object_header(obj, buf, totlen);
223 ab9a70b2 2017-11-06 stsp done:
224 4ca7b755 2018-01-26 stsp got_inflate_end(&zb);
225 2178c42e 2018-04-22 stsp return err;
226 302b7dd6 2018-04-23 stsp }
227 302b7dd6 2018-04-23 stsp
228 302b7dd6 2018-04-23 stsp static void
229 302b7dd6 2018-04-23 stsp read_object_header_privsep_child(int obj_fd, int imsg_fds[2])
230 302b7dd6 2018-04-23 stsp {
231 302b7dd6 2018-04-23 stsp const struct got_error *err = NULL;
232 e3306bd9 2018-04-23 stsp struct got_object *obj = NULL;
233 e3306bd9 2018-04-23 stsp struct imsgbuf ibuf;
234 302b7dd6 2018-04-23 stsp FILE *f = NULL;
235 302b7dd6 2018-04-23 stsp int status = 0;
236 302b7dd6 2018-04-23 stsp
237 be37c2e6 2018-04-24 stsp setproctitle("read object header");
238 302b7dd6 2018-04-23 stsp close(imsg_fds[0]);
239 e3306bd9 2018-04-23 stsp imsg_init(&ibuf, imsg_fds[1]);
240 302b7dd6 2018-04-23 stsp
241 302b7dd6 2018-04-23 stsp /* revoke access to most system calls */
242 302b7dd6 2018-04-23 stsp if (pledge("stdio", NULL) == -1) {
243 302b7dd6 2018-04-23 stsp err = got_error_from_errno();
244 302b7dd6 2018-04-23 stsp goto done;
245 302b7dd6 2018-04-23 stsp }
246 302b7dd6 2018-04-23 stsp
247 302b7dd6 2018-04-23 stsp f = fdopen(obj_fd, "rb");
248 302b7dd6 2018-04-23 stsp if (f == NULL) {
249 302b7dd6 2018-04-23 stsp err = got_error_from_errno();
250 302b7dd6 2018-04-23 stsp close(obj_fd);
251 302b7dd6 2018-04-23 stsp goto done;
252 302b7dd6 2018-04-23 stsp }
253 302b7dd6 2018-04-23 stsp
254 e3306bd9 2018-04-23 stsp err = read_object_header(&obj, f);
255 302b7dd6 2018-04-23 stsp if (err)
256 302b7dd6 2018-04-23 stsp goto done;
257 302b7dd6 2018-04-23 stsp
258 e3306bd9 2018-04-23 stsp err = got_privsep_send_obj(&ibuf, obj, 0);
259 302b7dd6 2018-04-23 stsp done:
260 e3306bd9 2018-04-23 stsp if (obj)
261 e3306bd9 2018-04-23 stsp got_object_close(obj);
262 302b7dd6 2018-04-23 stsp if (err) {
263 e3306bd9 2018-04-23 stsp got_privsep_send_error(&ibuf, err);
264 302b7dd6 2018-04-23 stsp status = 1;
265 302b7dd6 2018-04-23 stsp }
266 302b7dd6 2018-04-23 stsp if (f)
267 302b7dd6 2018-04-23 stsp fclose(f);
268 e3306bd9 2018-04-23 stsp imsg_clear(&ibuf);
269 302b7dd6 2018-04-23 stsp close(imsg_fds[1]);
270 302b7dd6 2018-04-23 stsp _exit(status);
271 b419fc47 2018-04-26 stsp }
272 b419fc47 2018-04-26 stsp
273 b419fc47 2018-04-26 stsp static const struct got_error *
274 b419fc47 2018-04-26 stsp wait_for_child(pid_t pid)
275 b419fc47 2018-04-26 stsp {
276 b419fc47 2018-04-26 stsp int child_status;
277 b419fc47 2018-04-26 stsp
278 b419fc47 2018-04-26 stsp waitpid(pid, &child_status, 0);
279 b419fc47 2018-04-26 stsp
280 b419fc47 2018-04-26 stsp if (!WIFEXITED(child_status))
281 b419fc47 2018-04-26 stsp return got_error(GOT_ERR_PRIVSEP_DIED);
282 b419fc47 2018-04-26 stsp
283 b419fc47 2018-04-26 stsp if (WEXITSTATUS(child_status) != 0)
284 b419fc47 2018-04-26 stsp return got_error(GOT_ERR_PRIVSEP_EXIT);
285 b419fc47 2018-04-26 stsp
286 b419fc47 2018-04-26 stsp return NULL;
287 2178c42e 2018-04-22 stsp }
288 2178c42e 2018-04-22 stsp
289 2178c42e 2018-04-22 stsp static const struct got_error *
290 2178c42e 2018-04-22 stsp read_object_header_privsep(struct got_object **obj, int fd)
291 2178c42e 2018-04-22 stsp {
292 2178c42e 2018-04-22 stsp struct imsgbuf parent_ibuf;
293 2178c42e 2018-04-22 stsp int imsg_fds[2];
294 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
295 2178c42e 2018-04-22 stsp pid_t pid;
296 2178c42e 2018-04-22 stsp
297 2178c42e 2018-04-22 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
298 2178c42e 2018-04-22 stsp return got_error_from_errno();
299 2178c42e 2018-04-22 stsp
300 2178c42e 2018-04-22 stsp pid = fork();
301 2178c42e 2018-04-22 stsp if (pid == -1)
302 2178c42e 2018-04-22 stsp return got_error_from_errno();
303 2178c42e 2018-04-22 stsp else if (pid == 0) {
304 302b7dd6 2018-04-23 stsp read_object_header_privsep_child(fd, imsg_fds);
305 302b7dd6 2018-04-23 stsp /* not reached */
306 2178c42e 2018-04-22 stsp }
307 2178c42e 2018-04-22 stsp
308 2178c42e 2018-04-22 stsp close(imsg_fds[1]);
309 2178c42e 2018-04-22 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
310 2178c42e 2018-04-22 stsp err = got_privsep_recv_obj(obj, &parent_ibuf);
311 2178c42e 2018-04-22 stsp imsg_clear(&parent_ibuf);
312 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
313 2178c42e 2018-04-22 stsp close(imsg_fds[0]);
314 b419fc47 2018-04-26 stsp return err ? err : err_child;
315 ab9a70b2 2017-11-06 stsp }
316 ab9a70b2 2017-11-06 stsp
317 d1cda826 2017-11-06 stsp static const struct got_error *
318 4558fcd4 2018-01-14 stsp object_path(char **path, struct got_object_id *id, struct got_repository *repo)
319 ab9a70b2 2017-11-06 stsp {
320 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
321 ef0981d5 2018-02-12 stsp char *hex;
322 d1cda826 2017-11-06 stsp char *path_objects = got_repo_get_path_objects(repo);
323 e6b1056e 2018-04-22 stsp
324 e6b1056e 2018-04-22 stsp *path = NULL;
325 ab9a70b2 2017-11-06 stsp
326 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
327 0a585a0d 2018-03-17 stsp return got_error_from_errno();
328 ab9a70b2 2017-11-06 stsp
329 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
330 ef0981d5 2018-02-12 stsp if (err)
331 ef0981d5 2018-02-12 stsp return err;
332 ab9a70b2 2017-11-06 stsp
333 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
334 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
335 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
336 ab9a70b2 2017-11-06 stsp
337 ef0981d5 2018-02-12 stsp free(hex);
338 d1cda826 2017-11-06 stsp free(path_objects);
339 d1cda826 2017-11-06 stsp return err;
340 d1cda826 2017-11-06 stsp }
341 d1cda826 2017-11-06 stsp
342 4ee4114f 2018-01-23 stsp static const struct got_error *
343 d5003b79 2018-04-22 stsp open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
344 d1cda826 2017-11-06 stsp {
345 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
346 a1fd68d8 2018-01-12 stsp char *path;
347 6c00b545 2018-01-17 stsp
348 6c00b545 2018-01-17 stsp err = object_path(&path, &obj->id, repo);
349 d1cda826 2017-11-06 stsp if (err)
350 d1cda826 2017-11-06 stsp return err;
351 d5003b79 2018-04-22 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
352 d5003b79 2018-04-22 stsp if (*fd == -1) {
353 6c00b545 2018-01-17 stsp err = got_error_from_errno();
354 6c00b545 2018-01-17 stsp goto done;
355 a1fd68d8 2018-01-12 stsp }
356 4558fcd4 2018-01-14 stsp done:
357 4558fcd4 2018-01-14 stsp free(path);
358 4558fcd4 2018-01-14 stsp return err;
359 4558fcd4 2018-01-14 stsp }
360 a1fd68d8 2018-01-12 stsp
361 4558fcd4 2018-01-14 stsp const struct got_error *
362 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
363 4558fcd4 2018-01-14 stsp struct got_object_id *id)
364 4558fcd4 2018-01-14 stsp {
365 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
366 6c00b545 2018-01-17 stsp char *path;
367 2178c42e 2018-04-22 stsp int fd;
368 7bb0daa1 2018-06-21 stsp
369 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
370 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
371 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
372 7bb0daa1 2018-06-21 stsp return NULL;
373 7bb0daa1 2018-06-21 stsp }
374 4558fcd4 2018-01-14 stsp
375 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
376 4558fcd4 2018-01-14 stsp if (err)
377 4558fcd4 2018-01-14 stsp return err;
378 4558fcd4 2018-01-14 stsp
379 2178c42e 2018-04-22 stsp fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
380 2178c42e 2018-04-22 stsp if (fd == -1) {
381 6c00b545 2018-01-17 stsp if (errno != ENOENT) {
382 6c00b545 2018-01-17 stsp err = got_error_from_errno();
383 6c00b545 2018-01-17 stsp goto done;
384 6c00b545 2018-01-17 stsp }
385 6c00b545 2018-01-17 stsp err = got_packfile_open_object(obj, id, repo);
386 6c00b545 2018-01-17 stsp if (err)
387 6c00b545 2018-01-17 stsp goto done;
388 6c00b545 2018-01-17 stsp if (*obj == NULL)
389 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NO_OBJ);
390 6c00b545 2018-01-17 stsp } else {
391 2178c42e 2018-04-22 stsp err = read_object_header_privsep(obj, fd);
392 6c00b545 2018-01-17 stsp if (err)
393 6c00b545 2018-01-17 stsp goto done;
394 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
395 6c00b545 2018-01-17 stsp }
396 7bb0daa1 2018-06-21 stsp
397 7bb0daa1 2018-06-21 stsp if (err == NULL) {
398 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
399 7bb0daa1 2018-06-21 stsp err = got_repo_cache_object(repo, id, *obj);
400 7bb0daa1 2018-06-21 stsp }
401 6c00b545 2018-01-17 stsp done:
402 6c00b545 2018-01-17 stsp free(path);
403 2178c42e 2018-04-22 stsp if (fd != -1)
404 2178c42e 2018-04-22 stsp close(fd);
405 ab9a70b2 2017-11-06 stsp return err;
406 6c00b545 2018-01-17 stsp
407 ab9a70b2 2017-11-06 stsp }
408 ab9a70b2 2017-11-06 stsp
409 6dfa2fd3 2018-02-12 stsp const struct got_error *
410 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
411 6dfa2fd3 2018-02-12 stsp const char *id_str)
412 6dfa2fd3 2018-02-12 stsp {
413 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
414 6dfa2fd3 2018-02-12 stsp
415 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
416 6dfa2fd3 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
417 6dfa2fd3 2018-02-12 stsp
418 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
419 6dfa2fd3 2018-02-12 stsp }
420 6dfa2fd3 2018-02-12 stsp
421 ab9a70b2 2017-11-06 stsp void
422 ab9a70b2 2017-11-06 stsp got_object_close(struct got_object *obj)
423 ab9a70b2 2017-11-06 stsp {
424 7bb0daa1 2018-06-21 stsp if (obj->refcnt > 0) {
425 7bb0daa1 2018-06-21 stsp obj->refcnt--;
426 cc538cdd 2018-06-22 stsp if (obj->refcnt > 0)
427 cc538cdd 2018-06-22 stsp return;
428 7bb0daa1 2018-06-21 stsp }
429 7bb0daa1 2018-06-21 stsp
430 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
431 c3703302 2018-01-23 stsp struct got_delta *delta;
432 96f5e8b3 2018-01-23 stsp while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
433 c3703302 2018-01-23 stsp delta = SIMPLEQ_FIRST(&obj->deltas.entries);
434 96f5e8b3 2018-01-23 stsp SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
435 c3703302 2018-01-23 stsp got_delta_close(delta);
436 96f5e8b3 2018-01-23 stsp }
437 96f5e8b3 2018-01-23 stsp }
438 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
439 96f5e8b3 2018-01-23 stsp free(obj->path_packfile);
440 ab9a70b2 2017-11-06 stsp free(obj);
441 bff6ca00 2018-04-23 stsp }
442 bff6ca00 2018-04-23 stsp
443 bff6ca00 2018-04-23 stsp struct got_commit_object *
444 bff6ca00 2018-04-23 stsp got_object_commit_alloc_partial(void)
445 bff6ca00 2018-04-23 stsp {
446 bff6ca00 2018-04-23 stsp struct got_commit_object *commit;
447 bff6ca00 2018-04-23 stsp
448 bff6ca00 2018-04-23 stsp commit = calloc(1, sizeof(*commit));
449 bff6ca00 2018-04-23 stsp if (commit == NULL)
450 bff6ca00 2018-04-23 stsp return NULL;
451 bff6ca00 2018-04-23 stsp commit->tree_id = calloc(1, sizeof(*commit->tree_id));
452 bff6ca00 2018-04-23 stsp if (commit->tree_id == NULL) {
453 bff6ca00 2018-04-23 stsp free(commit);
454 bff6ca00 2018-04-23 stsp return NULL;
455 bff6ca00 2018-04-23 stsp }
456 bff6ca00 2018-04-23 stsp
457 bff6ca00 2018-04-23 stsp SIMPLEQ_INIT(&commit->parent_ids);
458 bff6ca00 2018-04-23 stsp
459 bff6ca00 2018-04-23 stsp return commit;
460 bff6ca00 2018-04-23 stsp }
461 bff6ca00 2018-04-23 stsp
462 bff6ca00 2018-04-23 stsp const struct got_error *
463 be6a1b5a 2018-06-11 stsp got_object_open_as_commit(struct got_commit_object **commit,
464 be6a1b5a 2018-06-11 stsp struct got_repository *repo, struct got_object_id *id)
465 be6a1b5a 2018-06-11 stsp {
466 be6a1b5a 2018-06-11 stsp const struct got_error *err;
467 be6a1b5a 2018-06-11 stsp struct got_object *obj;
468 835e0dbd 2018-06-21 stsp
469 835e0dbd 2018-06-21 stsp *commit = NULL;
470 be6a1b5a 2018-06-11 stsp
471 be6a1b5a 2018-06-11 stsp err = got_object_open(&obj, repo, id);
472 be6a1b5a 2018-06-11 stsp if (err)
473 be6a1b5a 2018-06-11 stsp return err;
474 be6a1b5a 2018-06-11 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
475 be6a1b5a 2018-06-11 stsp err = got_error(GOT_ERR_OBJ_TYPE);
476 be6a1b5a 2018-06-11 stsp goto done;
477 be6a1b5a 2018-06-11 stsp }
478 be6a1b5a 2018-06-11 stsp
479 be6a1b5a 2018-06-11 stsp err = got_object_commit_open(commit, repo, obj);
480 be6a1b5a 2018-06-11 stsp done:
481 be6a1b5a 2018-06-11 stsp got_object_close(obj);
482 be6a1b5a 2018-06-11 stsp return err;
483 be6a1b5a 2018-06-11 stsp }
484 be6a1b5a 2018-06-11 stsp
485 be6a1b5a 2018-06-11 stsp const struct got_error *
486 bff6ca00 2018-04-23 stsp got_object_commit_add_parent(struct got_commit_object *commit,
487 bff6ca00 2018-04-23 stsp const char *id_str)
488 bff6ca00 2018-04-23 stsp {
489 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
490 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
491 bff6ca00 2018-04-23 stsp
492 79f35eb3 2018-06-11 stsp qid = calloc(1, sizeof(*qid));
493 79f35eb3 2018-06-11 stsp if (qid == NULL)
494 bff6ca00 2018-04-23 stsp return got_error_from_errno();
495 bff6ca00 2018-04-23 stsp
496 79f35eb3 2018-06-11 stsp qid->id = calloc(1, sizeof(*qid->id));
497 79f35eb3 2018-06-11 stsp if (qid->id == NULL) {
498 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
499 79f35eb3 2018-06-11 stsp free(qid);
500 bff6ca00 2018-04-23 stsp return err;
501 bff6ca00 2018-04-23 stsp }
502 bff6ca00 2018-04-23 stsp
503 79f35eb3 2018-06-11 stsp if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
504 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
505 79f35eb3 2018-06-11 stsp free(qid->id);
506 79f35eb3 2018-06-11 stsp free(qid);
507 bff6ca00 2018-04-23 stsp return err;
508 bff6ca00 2018-04-23 stsp }
509 bff6ca00 2018-04-23 stsp
510 79f35eb3 2018-06-11 stsp SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
511 bff6ca00 2018-04-23 stsp commit->nparents++;
512 4626e416 2018-06-10 stsp
513 4626e416 2018-06-10 stsp return NULL;
514 4626e416 2018-06-10 stsp }
515 4626e416 2018-06-10 stsp
516 4626e416 2018-06-10 stsp static const struct got_error *
517 788c352e 2018-06-16 stsp parse_gmtoff(time_t *gmtoff, const char *tzstr)
518 4626e416 2018-06-10 stsp {
519 788c352e 2018-06-16 stsp int sign = 1;
520 788c352e 2018-06-16 stsp const char *p = tzstr;
521 788c352e 2018-06-16 stsp time_t h, m;
522 4626e416 2018-06-10 stsp
523 788c352e 2018-06-16 stsp *gmtoff = 0;
524 4626e416 2018-06-10 stsp
525 788c352e 2018-06-16 stsp if (*p == '-')
526 788c352e 2018-06-16 stsp sign = -1;
527 788c352e 2018-06-16 stsp else if (*p != '+')
528 788c352e 2018-06-16 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
529 788c352e 2018-06-16 stsp p++;
530 788c352e 2018-06-16 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
531 788c352e 2018-06-16 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
532 788c352e 2018-06-16 stsp h = (((*p - '0') * 10) + (*(p + 1) - '0'));
533 788c352e 2018-06-16 stsp
534 788c352e 2018-06-16 stsp p += 2;
535 788c352e 2018-06-16 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
536 788c352e 2018-06-16 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
537 788c352e 2018-06-16 stsp m = ((*p - '0') * 10) + (*(p + 1) - '0');
538 788c352e 2018-06-16 stsp
539 788c352e 2018-06-16 stsp *gmtoff = (h * 60 * 60 + m * 60) * sign;
540 788c352e 2018-06-16 stsp return NULL;
541 788c352e 2018-06-16 stsp }
542 788c352e 2018-06-16 stsp
543 788c352e 2018-06-16 stsp static const struct got_error *
544 788c352e 2018-06-16 stsp parse_commit_time(struct tm *tm, char *committer)
545 788c352e 2018-06-16 stsp {
546 788c352e 2018-06-16 stsp const struct got_error *err = NULL;
547 788c352e 2018-06-16 stsp const char *errstr;
548 788c352e 2018-06-16 stsp char *space, *tzstr;
549 788c352e 2018-06-16 stsp time_t gmtoff;
550 788c352e 2018-06-16 stsp time_t time;
551 788c352e 2018-06-16 stsp
552 788c352e 2018-06-16 stsp /* Parse and strip off trailing timezone indicator string. */
553 4626e416 2018-06-10 stsp space = strrchr(committer, ' ');
554 4626e416 2018-06-10 stsp if (space == NULL)
555 4626e416 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
556 788c352e 2018-06-16 stsp tzstr = strdup(space + 1);
557 788c352e 2018-06-16 stsp if (tzstr == NULL)
558 6c281f94 2018-06-11 stsp return got_error_from_errno();
559 788c352e 2018-06-16 stsp err = parse_gmtoff(&gmtoff, tzstr);
560 788c352e 2018-06-16 stsp free(tzstr);
561 788c352e 2018-06-16 stsp if (err)
562 788c352e 2018-06-16 stsp return err;
563 4626e416 2018-06-10 stsp *space = '\0';
564 4626e416 2018-06-10 stsp
565 4626e416 2018-06-10 stsp /* Timestamp is separated from committer name + email by space. */
566 4626e416 2018-06-10 stsp space = strrchr(committer, ' ');
567 4626e416 2018-06-10 stsp if (space == NULL)
568 4626e416 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
569 4626e416 2018-06-10 stsp
570 788c352e 2018-06-16 stsp /* Timestamp parsed here is expressed in comitter's local time. */
571 788c352e 2018-06-16 stsp time = strtonum(space + 1, 0, INT64_MAX, &errstr);
572 4626e416 2018-06-10 stsp if (errstr)
573 4626e416 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
574 bff6ca00 2018-04-23 stsp
575 788c352e 2018-06-16 stsp /* Express the time stamp in UTC. */
576 788c352e 2018-06-16 stsp memset(tm, 0, sizeof(*tm));
577 788c352e 2018-06-16 stsp time -= gmtoff;
578 788c352e 2018-06-16 stsp if (localtime_r(&time, tm) == NULL)
579 788c352e 2018-06-16 stsp return got_error_from_errno();
580 788c352e 2018-06-16 stsp tm->tm_gmtoff = gmtoff;
581 788c352e 2018-06-16 stsp
582 4626e416 2018-06-10 stsp /* Strip off parsed time information, leaving just author and email. */
583 4626e416 2018-06-10 stsp *space = '\0';
584 788c352e 2018-06-16 stsp
585 bff6ca00 2018-04-23 stsp return NULL;
586 d1cda826 2017-11-06 stsp }
587 d1cda826 2017-11-06 stsp
588 d1cda826 2017-11-06 stsp static const struct got_error *
589 d1cda826 2017-11-06 stsp parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
590 d1cda826 2017-11-06 stsp {
591 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
592 d1cda826 2017-11-06 stsp char *s = buf;
593 d1cda826 2017-11-06 stsp size_t tlen;
594 d1cda826 2017-11-06 stsp ssize_t remain = (ssize_t)len;
595 d1cda826 2017-11-06 stsp
596 bff6ca00 2018-04-23 stsp *commit = got_object_commit_alloc_partial();
597 d1cda826 2017-11-06 stsp if (*commit == NULL)
598 0a585a0d 2018-03-17 stsp return got_error_from_errno();
599 d1cda826 2017-11-06 stsp
600 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
601 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
602 d1cda826 2017-11-06 stsp remain -= tlen;
603 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
604 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
605 d1cda826 2017-11-06 stsp goto done;
606 d1cda826 2017-11-06 stsp }
607 d1cda826 2017-11-06 stsp s += tlen;
608 59ece79d 2018-02-12 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
609 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
610 d1cda826 2017-11-06 stsp goto done;
611 d1cda826 2017-11-06 stsp }
612 d1cda826 2017-11-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
613 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
614 d1cda826 2017-11-06 stsp } else {
615 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
616 d1cda826 2017-11-06 stsp goto done;
617 d1cda826 2017-11-06 stsp }
618 d1cda826 2017-11-06 stsp
619 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
620 d1cda826 2017-11-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
621 d1cda826 2017-11-06 stsp remain -= tlen;
622 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
623 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
624 d1cda826 2017-11-06 stsp goto done;
625 ef0981d5 2018-02-12 stsp }
626 59ece79d 2018-02-12 stsp s += tlen;
627 bff6ca00 2018-04-23 stsp err = got_object_commit_add_parent(*commit, s);
628 bff6ca00 2018-04-23 stsp if (err)
629 d1cda826 2017-11-06 stsp goto done;
630 d1cda826 2017-11-06 stsp
631 eb651edf 2018-02-11 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
632 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
633 d1cda826 2017-11-06 stsp }
634 d1cda826 2017-11-06 stsp
635 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
636 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
637 d1cda826 2017-11-06 stsp char *p;
638 4626e416 2018-06-10 stsp size_t slen;
639 d1cda826 2017-11-06 stsp
640 d1cda826 2017-11-06 stsp remain -= tlen;
641 d1cda826 2017-11-06 stsp if (remain <= 0) {
642 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
643 d1cda826 2017-11-06 stsp goto done;
644 d1cda826 2017-11-06 stsp }
645 d1cda826 2017-11-06 stsp s += tlen;
646 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
647 d1cda826 2017-11-06 stsp if (p == NULL) {
648 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
649 d1cda826 2017-11-06 stsp goto done;
650 d1cda826 2017-11-06 stsp }
651 d1cda826 2017-11-06 stsp *p = '\0';
652 4626e416 2018-06-10 stsp slen = strlen(s);
653 788c352e 2018-06-16 stsp err = parse_commit_time(&(*commit)->tm_author, s);
654 4626e416 2018-06-10 stsp if (err)
655 4626e416 2018-06-10 stsp goto done;
656 d1cda826 2017-11-06 stsp (*commit)->author = strdup(s);
657 d1cda826 2017-11-06 stsp if ((*commit)->author == NULL) {
658 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
659 d1cda826 2017-11-06 stsp goto done;
660 d1cda826 2017-11-06 stsp }
661 4626e416 2018-06-10 stsp s += slen + 1;
662 4626e416 2018-06-10 stsp remain -= slen + 1;
663 d1cda826 2017-11-06 stsp }
664 d1cda826 2017-11-06 stsp
665 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
666 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
667 d1cda826 2017-11-06 stsp char *p;
668 4626e416 2018-06-10 stsp size_t slen;
669 d1cda826 2017-11-06 stsp
670 d1cda826 2017-11-06 stsp remain -= tlen;
671 d1cda826 2017-11-06 stsp if (remain <= 0) {
672 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
673 d1cda826 2017-11-06 stsp goto done;
674 d1cda826 2017-11-06 stsp }
675 d1cda826 2017-11-06 stsp s += tlen;
676 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
677 d1cda826 2017-11-06 stsp if (p == NULL) {
678 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
679 d1cda826 2017-11-06 stsp goto done;
680 d1cda826 2017-11-06 stsp }
681 d1cda826 2017-11-06 stsp *p = '\0';
682 4626e416 2018-06-10 stsp slen = strlen(s);
683 788c352e 2018-06-16 stsp err = parse_commit_time(&(*commit)->tm_committer, s);
684 4626e416 2018-06-10 stsp if (err)
685 4626e416 2018-06-10 stsp goto done;
686 d1cda826 2017-11-06 stsp (*commit)->committer = strdup(s);
687 d1cda826 2017-11-06 stsp if ((*commit)->committer == NULL) {
688 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
689 d1cda826 2017-11-06 stsp goto done;
690 d1cda826 2017-11-06 stsp }
691 4626e416 2018-06-10 stsp s += slen + 1;
692 4626e416 2018-06-10 stsp remain -= slen + 1;
693 d1cda826 2017-11-06 stsp }
694 d1cda826 2017-11-06 stsp
695 eb651edf 2018-02-11 stsp (*commit)->logmsg = strndup(s, remain);
696 eb651edf 2018-02-11 stsp if ((*commit)->logmsg == NULL) {
697 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
698 eb651edf 2018-02-11 stsp goto done;
699 eb651edf 2018-02-11 stsp }
700 d1cda826 2017-11-06 stsp done:
701 59ece79d 2018-02-12 stsp if (err) {
702 d1cda826 2017-11-06 stsp got_object_commit_close(*commit);
703 59ece79d 2018-02-12 stsp *commit = NULL;
704 59ece79d 2018-02-12 stsp }
705 0ffeb3c2 2017-11-26 stsp return err;
706 0ffeb3c2 2017-11-26 stsp }
707 6e790f45 2018-06-10 stsp
708 0ffeb3c2 2017-11-26 stsp static void
709 0ffeb3c2 2017-11-26 stsp tree_entry_close(struct got_tree_entry *te)
710 0ffeb3c2 2017-11-26 stsp {
711 59ece79d 2018-02-12 stsp free(te->id);
712 0ffeb3c2 2017-11-26 stsp free(te->name);
713 0ffeb3c2 2017-11-26 stsp free(te);
714 0ffeb3c2 2017-11-26 stsp }
715 0ffeb3c2 2017-11-26 stsp
716 e033d803 2018-04-23 stsp struct got_tree_entry *
717 e033d803 2018-04-23 stsp got_alloc_tree_entry_partial(void)
718 e033d803 2018-04-23 stsp {
719 e033d803 2018-04-23 stsp struct got_tree_entry *te;
720 e033d803 2018-04-23 stsp
721 e033d803 2018-04-23 stsp te = calloc(1, sizeof(*te));
722 e033d803 2018-04-23 stsp if (te == NULL)
723 e033d803 2018-04-23 stsp return NULL;
724 e033d803 2018-04-23 stsp
725 e033d803 2018-04-23 stsp te->id = calloc(1, sizeof(*te->id));
726 e033d803 2018-04-23 stsp if (te->id == NULL) {
727 e033d803 2018-04-23 stsp free(te);
728 e033d803 2018-04-23 stsp te = NULL;
729 e033d803 2018-04-23 stsp }
730 e033d803 2018-04-23 stsp return te;
731 e033d803 2018-04-23 stsp }
732 e033d803 2018-04-23 stsp
733 0ffeb3c2 2017-11-26 stsp static const struct got_error *
734 0ffeb3c2 2017-11-26 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
735 0ffeb3c2 2017-11-26 stsp size_t maxlen)
736 0ffeb3c2 2017-11-26 stsp {
737 0ffeb3c2 2017-11-26 stsp char *p = buf, *space;
738 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
739 0ffeb3c2 2017-11-26 stsp
740 e033d803 2018-04-23 stsp *te = got_alloc_tree_entry_partial();
741 0ffeb3c2 2017-11-26 stsp if (*te == NULL)
742 0a585a0d 2018-03-17 stsp return got_error_from_errno();
743 59ece79d 2018-02-12 stsp
744 0ffeb3c2 2017-11-26 stsp *elen = strlen(buf) + 1;
745 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen) {
746 0ffeb3c2 2017-11-26 stsp free(*te);
747 59ece79d 2018-02-12 stsp *te = NULL;
748 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
749 0ffeb3c2 2017-11-26 stsp }
750 0ffeb3c2 2017-11-26 stsp
751 0ffeb3c2 2017-11-26 stsp space = strchr(buf, ' ');
752 0ffeb3c2 2017-11-26 stsp if (space == NULL) {
753 0a585a0d 2018-03-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
754 0ffeb3c2 2017-11-26 stsp free(*te);
755 59ece79d 2018-02-12 stsp *te = NULL;
756 0a585a0d 2018-03-17 stsp return err;
757 0ffeb3c2 2017-11-26 stsp }
758 0ffeb3c2 2017-11-26 stsp while (*p != ' ') {
759 0ffeb3c2 2017-11-26 stsp if (*p < '0' && *p > '7') {
760 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
761 0ffeb3c2 2017-11-26 stsp goto done;
762 0ffeb3c2 2017-11-26 stsp }
763 0ffeb3c2 2017-11-26 stsp (*te)->mode <<= 3;
764 0ffeb3c2 2017-11-26 stsp (*te)->mode |= *p - '0';
765 0ffeb3c2 2017-11-26 stsp p++;
766 0ffeb3c2 2017-11-26 stsp }
767 0ffeb3c2 2017-11-26 stsp
768 0ffeb3c2 2017-11-26 stsp (*te)->name = strdup(space + 1);
769 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
770 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
771 0ffeb3c2 2017-11-26 stsp goto done;
772 0ffeb3c2 2017-11-26 stsp }
773 0ffeb3c2 2017-11-26 stsp buf += strlen(buf) + 1;
774 59ece79d 2018-02-12 stsp memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
775 0ffeb3c2 2017-11-26 stsp *elen += SHA1_DIGEST_LENGTH;
776 0ffeb3c2 2017-11-26 stsp done:
777 59ece79d 2018-02-12 stsp if (err) {
778 0ffeb3c2 2017-11-26 stsp tree_entry_close(*te);
779 59ece79d 2018-02-12 stsp *te = NULL;
780 59ece79d 2018-02-12 stsp }
781 d1cda826 2017-11-06 stsp return err;
782 d1cda826 2017-11-06 stsp }
783 d1cda826 2017-11-06 stsp
784 d1cda826 2017-11-06 stsp static const struct got_error *
785 e033d803 2018-04-23 stsp parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
786 0ffeb3c2 2017-11-26 stsp {
787 90356acc 2018-01-27 stsp const struct got_error *err;
788 0ffeb3c2 2017-11-26 stsp size_t remain = len;
789 0ffeb3c2 2017-11-26 stsp
790 0ffeb3c2 2017-11-26 stsp *tree = calloc(1, sizeof(**tree));
791 0ffeb3c2 2017-11-26 stsp if (*tree == NULL)
792 0a585a0d 2018-03-17 stsp return got_error_from_errno();
793 0ffeb3c2 2017-11-26 stsp
794 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INIT(&(*tree)->entries);
795 0ffeb3c2 2017-11-26 stsp
796 0ffeb3c2 2017-11-26 stsp while (remain > 0) {
797 0ffeb3c2 2017-11-26 stsp struct got_tree_entry *te;
798 0ffeb3c2 2017-11-26 stsp size_t elen;
799 0ffeb3c2 2017-11-26 stsp
800 90356acc 2018-01-27 stsp err = parse_tree_entry(&te, &elen, buf, remain);
801 90356acc 2018-01-27 stsp if (err)
802 90356acc 2018-01-27 stsp return err;
803 0ffeb3c2 2017-11-26 stsp (*tree)->nentries++;
804 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
805 0ffeb3c2 2017-11-26 stsp buf += elen;
806 0ffeb3c2 2017-11-26 stsp remain -= elen;
807 0ffeb3c2 2017-11-26 stsp }
808 0ffeb3c2 2017-11-26 stsp
809 0ffeb3c2 2017-11-26 stsp if (remain != 0) {
810 0ffeb3c2 2017-11-26 stsp got_object_tree_close(*tree);
811 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
812 0ffeb3c2 2017-11-26 stsp }
813 0ffeb3c2 2017-11-26 stsp
814 0ffeb3c2 2017-11-26 stsp return NULL;
815 0ffeb3c2 2017-11-26 stsp }
816 0ffeb3c2 2017-11-26 stsp
817 0ffeb3c2 2017-11-26 stsp static const struct got_error *
818 eb651edf 2018-02-11 stsp read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
819 eb651edf 2018-02-11 stsp {
820 eb651edf 2018-02-11 stsp const struct got_error *err = NULL;
821 eb651edf 2018-02-11 stsp static const size_t blocksize = 512;
822 eb651edf 2018-02-11 stsp size_t n, total, remain;
823 eb651edf 2018-02-11 stsp uint8_t *buf;
824 eb651edf 2018-02-11 stsp
825 eb651edf 2018-02-11 stsp *outbuf = NULL;
826 eb651edf 2018-02-11 stsp *outlen = 0;
827 eb651edf 2018-02-11 stsp
828 eb651edf 2018-02-11 stsp buf = calloc(1, blocksize);
829 eb651edf 2018-02-11 stsp if (buf == NULL)
830 0a585a0d 2018-03-17 stsp return got_error_from_errno();
831 eb651edf 2018-02-11 stsp
832 eb651edf 2018-02-11 stsp remain = blocksize;
833 eb651edf 2018-02-11 stsp total = 0;
834 eb651edf 2018-02-11 stsp while (1) {
835 eb651edf 2018-02-11 stsp if (remain == 0) {
836 eb651edf 2018-02-11 stsp uint8_t *newbuf;
837 eb651edf 2018-02-11 stsp newbuf = reallocarray(buf, 1, total + blocksize);
838 eb651edf 2018-02-11 stsp if (newbuf == NULL) {
839 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
840 eb651edf 2018-02-11 stsp goto done;
841 eb651edf 2018-02-11 stsp }
842 eb651edf 2018-02-11 stsp buf = newbuf;
843 eb651edf 2018-02-11 stsp remain += blocksize;
844 eb651edf 2018-02-11 stsp }
845 be89e2b1 2018-03-03 stsp n = fread(buf + total, 1, remain, f);
846 eb651edf 2018-02-11 stsp if (n == 0) {
847 eb651edf 2018-02-11 stsp if (ferror(f)) {
848 eb651edf 2018-02-11 stsp err = got_ferror(f, GOT_ERR_IO);
849 eb651edf 2018-02-11 stsp goto done;
850 eb651edf 2018-02-11 stsp }
851 eb651edf 2018-02-11 stsp break; /* EOF */
852 eb651edf 2018-02-11 stsp }
853 eb651edf 2018-02-11 stsp remain -= n;
854 eb651edf 2018-02-11 stsp total += n;
855 eb651edf 2018-02-11 stsp };
856 eb651edf 2018-02-11 stsp
857 eb651edf 2018-02-11 stsp done:
858 eb651edf 2018-02-11 stsp if (err == NULL) {
859 eb651edf 2018-02-11 stsp *outbuf = buf;
860 eb651edf 2018-02-11 stsp *outlen = total;
861 eb651edf 2018-02-11 stsp } else
862 eb651edf 2018-02-11 stsp free(buf);
863 eb651edf 2018-02-11 stsp return err;
864 eb651edf 2018-02-11 stsp }
865 eb651edf 2018-02-11 stsp
866 eb651edf 2018-02-11 stsp static const struct got_error *
867 bff6ca00 2018-04-23 stsp read_commit_object(struct got_commit_object **commit, struct got_object *obj,
868 bff6ca00 2018-04-23 stsp FILE *f)
869 d1cda826 2017-11-06 stsp {
870 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
871 d1cda826 2017-11-06 stsp size_t len;
872 eb651edf 2018-02-11 stsp uint8_t *p;
873 d1cda826 2017-11-06 stsp
874 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
875 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
876 eb651edf 2018-02-11 stsp else
877 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
878 4558fcd4 2018-01-14 stsp if (err)
879 d1cda826 2017-11-06 stsp return err;
880 d1cda826 2017-11-06 stsp
881 d1cda826 2017-11-06 stsp if (len < obj->hdrlen + obj->size) {
882 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
883 d1cda826 2017-11-06 stsp goto done;
884 d1cda826 2017-11-06 stsp }
885 d1cda826 2017-11-06 stsp
886 d1cda826 2017-11-06 stsp /* Skip object header. */
887 d1cda826 2017-11-06 stsp len -= obj->hdrlen;
888 eb651edf 2018-02-11 stsp err = parse_commit_object(commit, p + obj->hdrlen, len);
889 eb651edf 2018-02-11 stsp free(p);
890 d1cda826 2017-11-06 stsp done:
891 d1cda826 2017-11-06 stsp return err;
892 d1cda826 2017-11-06 stsp }
893 d1cda826 2017-11-06 stsp
894 bff6ca00 2018-04-23 stsp static void
895 bff6ca00 2018-04-23 stsp read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
896 bff6ca00 2018-04-23 stsp int imsg_fds[2])
897 bff6ca00 2018-04-23 stsp {
898 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
899 bff6ca00 2018-04-23 stsp struct got_commit_object *commit = NULL;
900 bff6ca00 2018-04-23 stsp struct imsgbuf ibuf;
901 bff6ca00 2018-04-23 stsp FILE *f = NULL;
902 bff6ca00 2018-04-23 stsp int status = 0;
903 bff6ca00 2018-04-23 stsp
904 be37c2e6 2018-04-24 stsp setproctitle("read commit object");
905 bff6ca00 2018-04-23 stsp close(imsg_fds[0]);
906 bff6ca00 2018-04-23 stsp imsg_init(&ibuf, imsg_fds[1]);
907 bff6ca00 2018-04-23 stsp
908 bff6ca00 2018-04-23 stsp /* revoke access to most system calls */
909 bff6ca00 2018-04-23 stsp if (pledge("stdio", NULL) == -1) {
910 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
911 bff6ca00 2018-04-23 stsp goto done;
912 bff6ca00 2018-04-23 stsp }
913 bff6ca00 2018-04-23 stsp
914 bff6ca00 2018-04-23 stsp f = fdopen(obj_fd, "rb");
915 bff6ca00 2018-04-23 stsp if (f == NULL) {
916 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
917 bff6ca00 2018-04-23 stsp close(obj_fd);
918 bff6ca00 2018-04-23 stsp goto done;
919 bff6ca00 2018-04-23 stsp }
920 bff6ca00 2018-04-23 stsp
921 bff6ca00 2018-04-23 stsp err = read_commit_object(&commit, obj, f);
922 bff6ca00 2018-04-23 stsp if (err)
923 bff6ca00 2018-04-23 stsp goto done;
924 bff6ca00 2018-04-23 stsp
925 068fd2bf 2018-04-24 stsp err = got_privsep_send_commit(&ibuf, commit);
926 bff6ca00 2018-04-23 stsp done:
927 bff6ca00 2018-04-23 stsp if (commit)
928 bff6ca00 2018-04-23 stsp got_object_commit_close(commit);
929 bff6ca00 2018-04-23 stsp if (err) {
930 bff6ca00 2018-04-23 stsp got_privsep_send_error(&ibuf, err);
931 bff6ca00 2018-04-23 stsp status = 1;
932 bff6ca00 2018-04-23 stsp }
933 bff6ca00 2018-04-23 stsp if (f)
934 bff6ca00 2018-04-23 stsp fclose(f);
935 bff6ca00 2018-04-23 stsp imsg_clear(&ibuf);
936 bff6ca00 2018-04-23 stsp close(imsg_fds[1]);
937 bff6ca00 2018-04-23 stsp _exit(status);
938 bff6ca00 2018-04-23 stsp }
939 bff6ca00 2018-04-23 stsp
940 bff6ca00 2018-04-23 stsp static const struct got_error *
941 bff6ca00 2018-04-23 stsp read_commit_object_privsep(struct got_commit_object **commit,
942 bff6ca00 2018-04-23 stsp struct got_repository *repo, struct got_object *obj, int fd)
943 bff6ca00 2018-04-23 stsp {
944 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
945 bff6ca00 2018-04-23 stsp struct imsgbuf parent_ibuf;
946 bff6ca00 2018-04-23 stsp int imsg_fds[2];
947 bff6ca00 2018-04-23 stsp pid_t pid;
948 bff6ca00 2018-04-23 stsp
949 bff6ca00 2018-04-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
950 bff6ca00 2018-04-23 stsp return got_error_from_errno();
951 bff6ca00 2018-04-23 stsp
952 bff6ca00 2018-04-23 stsp pid = fork();
953 bff6ca00 2018-04-23 stsp if (pid == -1)
954 bff6ca00 2018-04-23 stsp return got_error_from_errno();
955 bff6ca00 2018-04-23 stsp else if (pid == 0) {
956 bff6ca00 2018-04-23 stsp read_commit_object_privsep_child(obj, fd, imsg_fds);
957 e506bf32 2018-04-23 stsp /* not reached */
958 bff6ca00 2018-04-23 stsp }
959 bff6ca00 2018-04-23 stsp
960 bff6ca00 2018-04-23 stsp close(imsg_fds[1]);
961 bff6ca00 2018-04-23 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
962 068fd2bf 2018-04-24 stsp err = got_privsep_recv_commit(commit, &parent_ibuf);
963 bff6ca00 2018-04-23 stsp imsg_clear(&parent_ibuf);
964 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
965 bff6ca00 2018-04-23 stsp close(imsg_fds[0]);
966 b419fc47 2018-04-26 stsp return err ? err : err_child;
967 bff6ca00 2018-04-23 stsp }
968 bff6ca00 2018-04-23 stsp
969 d1cda826 2017-11-06 stsp const struct got_error *
970 d1cda826 2017-11-06 stsp got_object_commit_open(struct got_commit_object **commit,
971 d1cda826 2017-11-06 stsp struct got_repository *repo, struct got_object *obj)
972 d1cda826 2017-11-06 stsp {
973 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
974 1943de01 2018-06-22 stsp
975 1943de01 2018-06-22 stsp *commit = got_repo_get_cached_commit(repo, &obj->id);
976 1943de01 2018-06-22 stsp if (*commit != NULL) {
977 1943de01 2018-06-22 stsp (*commit)->refcnt++;
978 1943de01 2018-06-22 stsp return NULL;
979 1943de01 2018-06-22 stsp }
980 d1cda826 2017-11-06 stsp
981 d1cda826 2017-11-06 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
982 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_OBJ_TYPE);
983 d1cda826 2017-11-06 stsp
984 ea35256b 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
985 ea35256b 2018-03-16 stsp uint8_t *buf;
986 ea35256b 2018-03-16 stsp size_t len;
987 ea35256b 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
988 ea35256b 2018-03-16 stsp if (err)
989 ea35256b 2018-03-16 stsp return err;
990 b29656e2 2018-03-16 stsp obj->size = len;
991 b29656e2 2018-03-16 stsp err = parse_commit_object(commit, buf, len);
992 ea35256b 2018-03-16 stsp free(buf);
993 ea35256b 2018-03-16 stsp } else {
994 d5003b79 2018-04-22 stsp int fd;
995 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
996 ea35256b 2018-03-16 stsp if (err)
997 ea35256b 2018-03-16 stsp return err;
998 bff6ca00 2018-04-23 stsp err = read_commit_object_privsep(commit, repo, obj, fd);
999 bff6ca00 2018-04-23 stsp close(fd);
1000 ea35256b 2018-03-16 stsp }
1001 1943de01 2018-06-22 stsp
1002 1943de01 2018-06-22 stsp if (err == NULL) {
1003 1943de01 2018-06-22 stsp (*commit)->refcnt++;
1004 1943de01 2018-06-22 stsp err = got_repo_cache_commit(repo, &obj->id, *commit);
1005 1943de01 2018-06-22 stsp }
1006 1943de01 2018-06-22 stsp
1007 d1cda826 2017-11-06 stsp return err;
1008 d1cda826 2017-11-06 stsp }
1009 d1cda826 2017-11-06 stsp
1010 d1cda826 2017-11-06 stsp void
1011 d1cda826 2017-11-06 stsp got_object_commit_close(struct got_commit_object *commit)
1012 d1cda826 2017-11-06 stsp {
1013 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1014 1943de01 2018-06-22 stsp
1015 1943de01 2018-06-22 stsp if (commit->refcnt > 0) {
1016 1943de01 2018-06-22 stsp commit->refcnt--;
1017 cc538cdd 2018-06-22 stsp if (commit->refcnt > 0)
1018 cc538cdd 2018-06-22 stsp return;
1019 1943de01 2018-06-22 stsp }
1020 d1cda826 2017-11-06 stsp
1021 d1cda826 2017-11-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
1022 79f35eb3 2018-06-11 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
1023 d1cda826 2017-11-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
1024 79f35eb3 2018-06-11 stsp free(qid->id);
1025 79f35eb3 2018-06-11 stsp free(qid);
1026 d1cda826 2017-11-06 stsp }
1027 d1cda826 2017-11-06 stsp
1028 59ece79d 2018-02-12 stsp free(commit->tree_id);
1029 d1cda826 2017-11-06 stsp free(commit->author);
1030 d1cda826 2017-11-06 stsp free(commit->committer);
1031 d1cda826 2017-11-06 stsp free(commit->logmsg);
1032 d1cda826 2017-11-06 stsp free(commit);
1033 d1cda826 2017-11-06 stsp }
1034 0ffeb3c2 2017-11-26 stsp
1035 0ffeb3c2 2017-11-26 stsp static const struct got_error *
1036 e033d803 2018-04-23 stsp read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
1037 0ffeb3c2 2017-11-26 stsp {
1038 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
1039 0ffeb3c2 2017-11-26 stsp size_t len;
1040 eb651edf 2018-02-11 stsp uint8_t *p;
1041 0ffeb3c2 2017-11-26 stsp
1042 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
1043 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
1044 eb651edf 2018-02-11 stsp else
1045 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
1046 4558fcd4 2018-01-14 stsp if (err)
1047 0ffeb3c2 2017-11-26 stsp return err;
1048 0ffeb3c2 2017-11-26 stsp
1049 0ffeb3c2 2017-11-26 stsp if (len < obj->hdrlen + obj->size) {
1050 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1051 0ffeb3c2 2017-11-26 stsp goto done;
1052 0ffeb3c2 2017-11-26 stsp }
1053 0ffeb3c2 2017-11-26 stsp
1054 0ffeb3c2 2017-11-26 stsp /* Skip object header. */
1055 0ffeb3c2 2017-11-26 stsp len -= obj->hdrlen;
1056 e033d803 2018-04-23 stsp err = parse_tree_object(tree, p + obj->hdrlen, len);
1057 eb651edf 2018-02-11 stsp free(p);
1058 e033d803 2018-04-23 stsp done:
1059 e033d803 2018-04-23 stsp return err;
1060 e033d803 2018-04-23 stsp }
1061 e033d803 2018-04-23 stsp
1062 e033d803 2018-04-23 stsp static void
1063 e033d803 2018-04-23 stsp read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
1064 e033d803 2018-04-23 stsp int imsg_fds[2])
1065 e033d803 2018-04-23 stsp {
1066 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
1067 e033d803 2018-04-23 stsp struct got_tree_object *tree = NULL;
1068 e033d803 2018-04-23 stsp struct imsgbuf ibuf;
1069 e033d803 2018-04-23 stsp FILE *f = NULL;
1070 e033d803 2018-04-23 stsp int status = 0;
1071 e033d803 2018-04-23 stsp
1072 be37c2e6 2018-04-24 stsp setproctitle("read tree object");
1073 e033d803 2018-04-23 stsp close(imsg_fds[0]);
1074 e033d803 2018-04-23 stsp imsg_init(&ibuf, imsg_fds[1]);
1075 e033d803 2018-04-23 stsp
1076 e033d803 2018-04-23 stsp /* revoke access to most system calls */
1077 e033d803 2018-04-23 stsp if (pledge("stdio", NULL) == -1) {
1078 e033d803 2018-04-23 stsp err = got_error_from_errno();
1079 e033d803 2018-04-23 stsp goto done;
1080 e033d803 2018-04-23 stsp }
1081 e033d803 2018-04-23 stsp
1082 e033d803 2018-04-23 stsp f = fdopen(obj_fd, "rb");
1083 e033d803 2018-04-23 stsp if (f == NULL) {
1084 e033d803 2018-04-23 stsp err = got_error_from_errno();
1085 e033d803 2018-04-23 stsp close(obj_fd);
1086 e033d803 2018-04-23 stsp goto done;
1087 e033d803 2018-04-23 stsp }
1088 e033d803 2018-04-23 stsp
1089 e033d803 2018-04-23 stsp err = read_tree_object(&tree, obj, f);
1090 e033d803 2018-04-23 stsp if (err)
1091 e033d803 2018-04-23 stsp goto done;
1092 e033d803 2018-04-23 stsp
1093 068fd2bf 2018-04-24 stsp err = got_privsep_send_tree(&ibuf, tree);
1094 0ffeb3c2 2017-11-26 stsp done:
1095 e033d803 2018-04-23 stsp if (tree)
1096 e033d803 2018-04-23 stsp got_object_tree_close(tree);
1097 e033d803 2018-04-23 stsp if (err) {
1098 e033d803 2018-04-23 stsp got_privsep_send_error(&ibuf, err);
1099 e033d803 2018-04-23 stsp status = 1;
1100 e033d803 2018-04-23 stsp }
1101 e033d803 2018-04-23 stsp if (f)
1102 e033d803 2018-04-23 stsp fclose(f);
1103 e033d803 2018-04-23 stsp imsg_clear(&ibuf);
1104 e033d803 2018-04-23 stsp close(imsg_fds[1]);
1105 e033d803 2018-04-23 stsp _exit(status);
1106 e033d803 2018-04-23 stsp }
1107 e033d803 2018-04-23 stsp
1108 e033d803 2018-04-23 stsp static const struct got_error *
1109 e033d803 2018-04-23 stsp read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
1110 e033d803 2018-04-23 stsp int fd)
1111 e033d803 2018-04-23 stsp {
1112 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
1113 e033d803 2018-04-23 stsp struct imsgbuf parent_ibuf;
1114 e033d803 2018-04-23 stsp int imsg_fds[2];
1115 e033d803 2018-04-23 stsp pid_t pid;
1116 e033d803 2018-04-23 stsp
1117 e033d803 2018-04-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1118 e033d803 2018-04-23 stsp return got_error_from_errno();
1119 e033d803 2018-04-23 stsp
1120 e033d803 2018-04-23 stsp pid = fork();
1121 e033d803 2018-04-23 stsp if (pid == -1)
1122 e033d803 2018-04-23 stsp return got_error_from_errno();
1123 e033d803 2018-04-23 stsp else if (pid == 0) {
1124 e033d803 2018-04-23 stsp read_tree_object_privsep_child(obj, fd, imsg_fds);
1125 e033d803 2018-04-23 stsp /* not reached */
1126 e033d803 2018-04-23 stsp }
1127 e033d803 2018-04-23 stsp
1128 e033d803 2018-04-23 stsp close(imsg_fds[1]);
1129 e033d803 2018-04-23 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
1130 068fd2bf 2018-04-24 stsp err = got_privsep_recv_tree(tree, &parent_ibuf);
1131 e033d803 2018-04-23 stsp imsg_clear(&parent_ibuf);
1132 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
1133 e033d803 2018-04-23 stsp close(imsg_fds[0]);
1134 b419fc47 2018-04-26 stsp return err ? err : err_child;
1135 0ffeb3c2 2017-11-26 stsp }
1136 0ffeb3c2 2017-11-26 stsp
1137 0ffeb3c2 2017-11-26 stsp const struct got_error *
1138 0ffeb3c2 2017-11-26 stsp got_object_tree_open(struct got_tree_object **tree,
1139 0ffeb3c2 2017-11-26 stsp struct got_repository *repo, struct got_object *obj)
1140 0ffeb3c2 2017-11-26 stsp {
1141 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
1142 f6be5c30 2018-06-22 stsp
1143 f6be5c30 2018-06-22 stsp *tree = got_repo_get_cached_tree(repo, &obj->id);
1144 f6be5c30 2018-06-22 stsp if (*tree != NULL) {
1145 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
1146 f6be5c30 2018-06-22 stsp return NULL;
1147 f6be5c30 2018-06-22 stsp }
1148 0ffeb3c2 2017-11-26 stsp
1149 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
1150 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
1151 0ffeb3c2 2017-11-26 stsp
1152 e0ab43e7 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1153 e0ab43e7 2018-03-16 stsp uint8_t *buf;
1154 e0ab43e7 2018-03-16 stsp size_t len;
1155 e0ab43e7 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1156 e0ab43e7 2018-03-16 stsp if (err)
1157 e0ab43e7 2018-03-16 stsp return err;
1158 b29656e2 2018-03-16 stsp obj->size = len;
1159 e033d803 2018-04-23 stsp err = parse_tree_object(tree, buf, len);
1160 e0ab43e7 2018-03-16 stsp free(buf);
1161 e0ab43e7 2018-03-16 stsp } else {
1162 d5003b79 2018-04-22 stsp int fd;
1163 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
1164 e0ab43e7 2018-03-16 stsp if (err)
1165 e0ab43e7 2018-03-16 stsp return err;
1166 e033d803 2018-04-23 stsp err = read_tree_object_privsep(tree, obj, fd);
1167 d5003b79 2018-04-22 stsp close(fd);
1168 776d4d29 2018-06-17 stsp }
1169 f6be5c30 2018-06-22 stsp
1170 f6be5c30 2018-06-22 stsp if (err == NULL) {
1171 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
1172 f6be5c30 2018-06-22 stsp err = got_repo_cache_tree(repo, &obj->id, *tree);
1173 f6be5c30 2018-06-22 stsp }
1174 f6be5c30 2018-06-22 stsp
1175 776d4d29 2018-06-17 stsp return err;
1176 776d4d29 2018-06-17 stsp }
1177 776d4d29 2018-06-17 stsp
1178 776d4d29 2018-06-17 stsp const struct got_error *
1179 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
1180 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
1181 776d4d29 2018-06-17 stsp {
1182 776d4d29 2018-06-17 stsp const struct got_error *err;
1183 776d4d29 2018-06-17 stsp struct got_object *obj;
1184 835e0dbd 2018-06-21 stsp
1185 835e0dbd 2018-06-21 stsp *tree = NULL;
1186 776d4d29 2018-06-17 stsp
1187 776d4d29 2018-06-17 stsp err = got_object_open(&obj, repo, id);
1188 776d4d29 2018-06-17 stsp if (err)
1189 776d4d29 2018-06-17 stsp return err;
1190 1cbc02b6 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
1191 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1192 776d4d29 2018-06-17 stsp goto done;
1193 e0ab43e7 2018-03-16 stsp }
1194 776d4d29 2018-06-17 stsp
1195 776d4d29 2018-06-17 stsp err = got_object_tree_open(tree, repo, obj);
1196 776d4d29 2018-06-17 stsp done:
1197 776d4d29 2018-06-17 stsp got_object_close(obj);
1198 0ffeb3c2 2017-11-26 stsp return err;
1199 0ffeb3c2 2017-11-26 stsp }
1200 0ffeb3c2 2017-11-26 stsp
1201 0ffeb3c2 2017-11-26 stsp void
1202 0ffeb3c2 2017-11-26 stsp got_object_tree_close(struct got_tree_object *tree)
1203 0ffeb3c2 2017-11-26 stsp {
1204 f715ca7f 2017-11-27 stsp struct got_tree_entry *te;
1205 f6be5c30 2018-06-22 stsp
1206 f6be5c30 2018-06-22 stsp if (tree->refcnt > 0) {
1207 f6be5c30 2018-06-22 stsp tree->refcnt--;
1208 cc538cdd 2018-06-22 stsp if (tree->refcnt > 0)
1209 cc538cdd 2018-06-22 stsp return;
1210 f6be5c30 2018-06-22 stsp }
1211 f715ca7f 2017-11-27 stsp
1212 f715ca7f 2017-11-27 stsp while (!SIMPLEQ_EMPTY(&tree->entries)) {
1213 f715ca7f 2017-11-27 stsp te = SIMPLEQ_FIRST(&tree->entries);
1214 f715ca7f 2017-11-27 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
1215 f715ca7f 2017-11-27 stsp tree_entry_close(te);
1216 f715ca7f 2017-11-27 stsp }
1217 f715ca7f 2017-11-27 stsp
1218 f715ca7f 2017-11-27 stsp free(tree);
1219 57efb1af 2018-04-24 stsp }
1220 ff6b18f8 2018-04-24 stsp
1221 ff6b18f8 2018-04-24 stsp static const struct got_error *
1222 ff6b18f8 2018-04-24 stsp read_blob_object_privsep_child(int outfd, int infd, int imsg_fds[2])
1223 ff6b18f8 2018-04-24 stsp {
1224 ff6b18f8 2018-04-24 stsp const struct got_error *err = NULL;
1225 ff6b18f8 2018-04-24 stsp struct imsgbuf ibuf;
1226 ff6b18f8 2018-04-24 stsp int status = 0;
1227 2967a784 2018-04-24 stsp size_t size;
1228 8b2180d4 2018-04-26 stsp FILE *infile = NULL;
1229 ff6b18f8 2018-04-24 stsp
1230 be37c2e6 2018-04-24 stsp setproctitle("read blob object");
1231 ff6b18f8 2018-04-24 stsp close(imsg_fds[0]);
1232 ff6b18f8 2018-04-24 stsp imsg_init(&ibuf, imsg_fds[1]);
1233 57efb1af 2018-04-24 stsp
1234 ff6b18f8 2018-04-24 stsp /* revoke access to most system calls */
1235 ff6b18f8 2018-04-24 stsp if (pledge("stdio", NULL) == -1) {
1236 ff6b18f8 2018-04-24 stsp err = got_error_from_errno();
1237 ff6b18f8 2018-04-24 stsp goto done;
1238 ff6b18f8 2018-04-24 stsp }
1239 ff6b18f8 2018-04-24 stsp
1240 8b2180d4 2018-04-26 stsp infile = fdopen(infd, "rb");
1241 8b2180d4 2018-04-26 stsp if (infile == NULL) {
1242 8b2180d4 2018-04-26 stsp err = got_error_from_errno();
1243 8b2180d4 2018-04-26 stsp close(infd);
1244 8b2180d4 2018-04-26 stsp goto done;
1245 8b2180d4 2018-04-26 stsp }
1246 8b2180d4 2018-04-26 stsp err = got_inflate_to_fd(&size, infile, outfd);
1247 8b2180d4 2018-04-26 stsp fclose(infile);
1248 ff6b18f8 2018-04-24 stsp if (err)
1249 ff6b18f8 2018-04-24 stsp goto done;
1250 ff6b18f8 2018-04-24 stsp
1251 2967a784 2018-04-24 stsp err = got_privsep_send_blob(&ibuf, size);
1252 ff6b18f8 2018-04-24 stsp done:
1253 ff6b18f8 2018-04-24 stsp if (err) {
1254 ff6b18f8 2018-04-24 stsp got_privsep_send_error(&ibuf, err);
1255 ff6b18f8 2018-04-24 stsp status = 1;
1256 ff6b18f8 2018-04-24 stsp }
1257 ff6b18f8 2018-04-24 stsp close(outfd);
1258 ff6b18f8 2018-04-24 stsp imsg_clear(&ibuf);
1259 ff6b18f8 2018-04-24 stsp close(imsg_fds[1]);
1260 ff6b18f8 2018-04-24 stsp _exit(status);
1261 ff6b18f8 2018-04-24 stsp }
1262 ff6b18f8 2018-04-24 stsp
1263 ff6b18f8 2018-04-24 stsp static const struct got_error *
1264 2967a784 2018-04-24 stsp read_blob_object_privsep(size_t *size, int outfd, int infd)
1265 ff6b18f8 2018-04-24 stsp {
1266 ff6b18f8 2018-04-24 stsp struct imsgbuf parent_ibuf;
1267 ff6b18f8 2018-04-24 stsp int imsg_fds[2];
1268 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
1269 ff6b18f8 2018-04-24 stsp pid_t pid;
1270 ff6b18f8 2018-04-24 stsp
1271 ff6b18f8 2018-04-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1272 ff6b18f8 2018-04-24 stsp return got_error_from_errno();
1273 ff6b18f8 2018-04-24 stsp
1274 ff6b18f8 2018-04-24 stsp pid = fork();
1275 ff6b18f8 2018-04-24 stsp if (pid == -1)
1276 ff6b18f8 2018-04-24 stsp return got_error_from_errno();
1277 ff6b18f8 2018-04-24 stsp else if (pid == 0) {
1278 ff6b18f8 2018-04-24 stsp read_blob_object_privsep_child(outfd, infd, imsg_fds);
1279 ff6b18f8 2018-04-24 stsp /* not reached */
1280 ff6b18f8 2018-04-24 stsp }
1281 ff6b18f8 2018-04-24 stsp
1282 ff6b18f8 2018-04-24 stsp close(imsg_fds[1]);
1283 ff6b18f8 2018-04-24 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
1284 2967a784 2018-04-24 stsp err = got_privsep_recv_blob(size, &parent_ibuf);
1285 ff6b18f8 2018-04-24 stsp imsg_clear(&parent_ibuf);
1286 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
1287 ff6b18f8 2018-04-24 stsp close(imsg_fds[0]);
1288 ff6b18f8 2018-04-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1289 ff6b18f8 2018-04-24 stsp err = got_error_from_errno();
1290 b419fc47 2018-04-26 stsp return err ? err : err_child;
1291 ff6b18f8 2018-04-24 stsp }
1292 ff6b18f8 2018-04-24 stsp
1293 68482ea3 2017-11-27 stsp const struct got_error *
1294 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
1295 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1296 68482ea3 2017-11-27 stsp {
1297 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
1298 68482ea3 2017-11-27 stsp
1299 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
1300 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
1301 68482ea3 2017-11-27 stsp
1302 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
1303 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
1304 7d283eee 2017-11-29 stsp
1305 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1306 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1307 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1308 68482ea3 2017-11-27 stsp
1309 15c8b0e6 2018-04-24 stsp (*blob)->read_buf = calloc(1, blocksize);
1310 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1311 15c8b0e6 2018-04-24 stsp err = got_error_from_errno();
1312 c7254d79 2018-04-24 stsp goto done;
1313 15c8b0e6 2018-04-24 stsp }
1314 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1315 eb651edf 2018-02-11 stsp err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1316 c7254d79 2018-04-24 stsp if (err)
1317 c7254d79 2018-04-24 stsp goto done;
1318 eb651edf 2018-02-11 stsp } else {
1319 3aca5731 2018-04-24 stsp int infd, outfd;
1320 2967a784 2018-04-24 stsp size_t size;
1321 2967a784 2018-04-24 stsp struct stat sb;
1322 c7254d79 2018-04-24 stsp
1323 3aca5731 2018-04-24 stsp err = open_loose_object(&infd, obj, repo);
1324 c7254d79 2018-04-24 stsp if (err)
1325 c7254d79 2018-04-24 stsp goto done;
1326 c7254d79 2018-04-24 stsp
1327 3aca5731 2018-04-24 stsp
1328 3aca5731 2018-04-24 stsp outfd = got_opentempfd();
1329 3aca5731 2018-04-24 stsp if (outfd == -1) {
1330 3aca5731 2018-04-24 stsp err = got_error_from_errno();
1331 3aca5731 2018-04-24 stsp close(infd);
1332 3aca5731 2018-04-24 stsp goto done;
1333 3aca5731 2018-04-24 stsp }
1334 3aca5731 2018-04-24 stsp
1335 2967a784 2018-04-24 stsp err = read_blob_object_privsep(&size, outfd, infd);
1336 3aca5731 2018-04-24 stsp close(infd);
1337 57efb1af 2018-04-24 stsp if (err)
1338 c7254d79 2018-04-24 stsp goto done;
1339 3aca5731 2018-04-24 stsp
1340 2967a784 2018-04-24 stsp if (size != obj->hdrlen + obj->size) {
1341 1a6b3ab7 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1342 2967a784 2018-04-24 stsp close(outfd);
1343 2967a784 2018-04-24 stsp goto done;
1344 2967a784 2018-04-24 stsp }
1345 2967a784 2018-04-24 stsp
1346 2967a784 2018-04-24 stsp if (fstat(outfd, &sb) == -1) {
1347 2967a784 2018-04-24 stsp err = got_error_from_errno();
1348 2967a784 2018-04-24 stsp close(outfd);
1349 2967a784 2018-04-24 stsp goto done;
1350 2967a784 2018-04-24 stsp }
1351 2967a784 2018-04-24 stsp
1352 2967a784 2018-04-24 stsp if (sb.st_size != size) {
1353 2967a784 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1354 2967a784 2018-04-24 stsp close(outfd);
1355 2967a784 2018-04-24 stsp goto done;
1356 2967a784 2018-04-24 stsp }
1357 2967a784 2018-04-24 stsp
1358 3aca5731 2018-04-24 stsp (*blob)->f = fdopen(outfd, "rb");
1359 3aca5731 2018-04-24 stsp if ((*blob)->f == NULL) {
1360 3aca5731 2018-04-24 stsp err = got_error_from_errno();
1361 3aca5731 2018-04-24 stsp close(outfd);
1362 3aca5731 2018-04-24 stsp goto done;
1363 3aca5731 2018-04-24 stsp }
1364 68482ea3 2017-11-27 stsp }
1365 68482ea3 2017-11-27 stsp
1366 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
1367 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1368 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1369 7d283eee 2017-11-29 stsp
1370 c7254d79 2018-04-24 stsp done:
1371 c7254d79 2018-04-24 stsp if (err && *blob) {
1372 c7254d79 2018-04-24 stsp if ((*blob)->f)
1373 c7254d79 2018-04-24 stsp fclose((*blob)->f);
1374 c7254d79 2018-04-24 stsp free((*blob)->read_buf);
1375 c7254d79 2018-04-24 stsp free(*blob);
1376 c7254d79 2018-04-24 stsp *blob = NULL;
1377 a19581a2 2018-06-21 stsp }
1378 a19581a2 2018-06-21 stsp return err;
1379 a19581a2 2018-06-21 stsp }
1380 a19581a2 2018-06-21 stsp
1381 a19581a2 2018-06-21 stsp const struct got_error *
1382 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1383 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
1384 a19581a2 2018-06-21 stsp size_t blocksize)
1385 a19581a2 2018-06-21 stsp {
1386 a19581a2 2018-06-21 stsp const struct got_error *err;
1387 a19581a2 2018-06-21 stsp struct got_object *obj;
1388 835e0dbd 2018-06-21 stsp
1389 835e0dbd 2018-06-21 stsp *blob = NULL;
1390 a19581a2 2018-06-21 stsp
1391 a19581a2 2018-06-21 stsp err = got_object_open(&obj, repo, id);
1392 a19581a2 2018-06-21 stsp if (err)
1393 a19581a2 2018-06-21 stsp return err;
1394 a19581a2 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1395 a19581a2 2018-06-21 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1396 a19581a2 2018-06-21 stsp goto done;
1397 c7254d79 2018-04-24 stsp }
1398 a19581a2 2018-06-21 stsp
1399 a19581a2 2018-06-21 stsp err = got_object_blob_open(blob, repo, obj, blocksize);
1400 a19581a2 2018-06-21 stsp done:
1401 a19581a2 2018-06-21 stsp got_object_close(obj);
1402 68482ea3 2017-11-27 stsp return err;
1403 0ffeb3c2 2017-11-26 stsp }
1404 68482ea3 2017-11-27 stsp
1405 68482ea3 2017-11-27 stsp void
1406 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1407 68482ea3 2017-11-27 stsp {
1408 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1409 68482ea3 2017-11-27 stsp fclose(blob->f);
1410 68482ea3 2017-11-27 stsp free(blob);
1411 f934cf2c 2018-02-12 stsp }
1412 f934cf2c 2018-02-12 stsp
1413 f934cf2c 2018-02-12 stsp char *
1414 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1415 f934cf2c 2018-02-12 stsp {
1416 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1417 f934cf2c 2018-02-12 stsp }
1418 f934cf2c 2018-02-12 stsp
1419 f934cf2c 2018-02-12 stsp size_t
1420 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1421 f934cf2c 2018-02-12 stsp {
1422 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1423 68482ea3 2017-11-27 stsp }
1424 68482ea3 2017-11-27 stsp
1425 f934cf2c 2018-02-12 stsp const uint8_t *
1426 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1427 f934cf2c 2018-02-12 stsp {
1428 f934cf2c 2018-02-12 stsp return blob->read_buf;
1429 f934cf2c 2018-02-12 stsp }
1430 f934cf2c 2018-02-12 stsp
1431 68482ea3 2017-11-27 stsp const struct got_error *
1432 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1433 68482ea3 2017-11-27 stsp {
1434 eb651edf 2018-02-11 stsp size_t n;
1435 eb651edf 2018-02-11 stsp
1436 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1437 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1438 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1439 eb651edf 2018-02-11 stsp *outlenp = n;
1440 35e9ba5d 2018-06-21 stsp return NULL;
1441 35e9ba5d 2018-06-21 stsp }
1442 35e9ba5d 2018-06-21 stsp
1443 35e9ba5d 2018-06-21 stsp const struct got_error *
1444 35e9ba5d 2018-06-21 stsp got_object_blob_dump_to_file(size_t *total_len, FILE *outfile,
1445 35e9ba5d 2018-06-21 stsp struct got_blob_object *blob)
1446 35e9ba5d 2018-06-21 stsp {
1447 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1448 35e9ba5d 2018-06-21 stsp size_t len, hdrlen;
1449 35e9ba5d 2018-06-21 stsp
1450 35e9ba5d 2018-06-21 stsp *total_len = 0;
1451 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1452 35e9ba5d 2018-06-21 stsp do {
1453 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1454 35e9ba5d 2018-06-21 stsp if (err)
1455 35e9ba5d 2018-06-21 stsp return err;
1456 35e9ba5d 2018-06-21 stsp if (len == 0)
1457 35e9ba5d 2018-06-21 stsp break;
1458 35e9ba5d 2018-06-21 stsp *total_len += len;
1459 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1460 35e9ba5d 2018-06-21 stsp fwrite(got_object_blob_get_read_buf(blob) + hdrlen,
1461 35e9ba5d 2018-06-21 stsp len - hdrlen, 1, outfile);
1462 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1463 35e9ba5d 2018-06-21 stsp } while (len != 0);
1464 35e9ba5d 2018-06-21 stsp
1465 35e9ba5d 2018-06-21 stsp fflush(outfile);
1466 35e9ba5d 2018-06-21 stsp rewind(outfile);
1467 35e9ba5d 2018-06-21 stsp
1468 776d4d29 2018-06-17 stsp return NULL;
1469 776d4d29 2018-06-17 stsp }
1470 776d4d29 2018-06-17 stsp
1471 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1472 776d4d29 2018-06-17 stsp find_entry_by_name(struct got_tree_object *tree, const char *name)
1473 776d4d29 2018-06-17 stsp {
1474 776d4d29 2018-06-17 stsp struct got_tree_entry *te;
1475 776d4d29 2018-06-17 stsp
1476 776d4d29 2018-06-17 stsp SIMPLEQ_FOREACH(te, &tree->entries, entry) {
1477 776d4d29 2018-06-17 stsp if (strcmp(te->name, name) == 0)
1478 776d4d29 2018-06-17 stsp return te;
1479 776d4d29 2018-06-17 stsp }
1480 eb651edf 2018-02-11 stsp return NULL;
1481 776d4d29 2018-06-17 stsp }
1482 776d4d29 2018-06-17 stsp
1483 776d4d29 2018-06-17 stsp const struct got_error *
1484 776d4d29 2018-06-17 stsp got_object_open_by_path(struct got_object **obj, struct got_repository *repo,
1485 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
1486 776d4d29 2018-06-17 stsp {
1487 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1488 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
1489 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
1490 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1491 197aa481 2018-06-21 stsp char *seg, *s, *s0 = NULL;
1492 67606321 2018-06-21 stsp size_t len = strlen(path);
1493 776d4d29 2018-06-17 stsp
1494 776d4d29 2018-06-17 stsp *obj = NULL;
1495 776d4d29 2018-06-17 stsp
1496 776d4d29 2018-06-17 stsp /* We are expecting an absolute in-repository path. */
1497 776d4d29 2018-06-17 stsp if (path[0] != '/')
1498 776d4d29 2018-06-17 stsp return got_error(GOT_ERR_NOT_ABSPATH);
1499 776d4d29 2018-06-17 stsp
1500 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
1501 776d4d29 2018-06-17 stsp if (err)
1502 776d4d29 2018-06-17 stsp goto done;
1503 776d4d29 2018-06-17 stsp
1504 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
1505 776d4d29 2018-06-17 stsp if (path[1] == '\0') {
1506 776d4d29 2018-06-17 stsp err = got_object_open(obj, repo, commit->tree_id);
1507 776d4d29 2018-06-17 stsp if (err)
1508 776d4d29 2018-06-17 stsp goto done;
1509 776d4d29 2018-06-17 stsp return NULL;
1510 776d4d29 2018-06-17 stsp }
1511 776d4d29 2018-06-17 stsp
1512 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1513 776d4d29 2018-06-17 stsp if (err)
1514 776d4d29 2018-06-17 stsp goto done;
1515 776d4d29 2018-06-17 stsp
1516 197aa481 2018-06-21 stsp s0 = strdup(path);
1517 197aa481 2018-06-21 stsp if (s0 == NULL) {
1518 776d4d29 2018-06-17 stsp err = got_error_from_errno();
1519 776d4d29 2018-06-17 stsp goto done;
1520 776d4d29 2018-06-17 stsp }
1521 67606321 2018-06-21 stsp err = got_canonpath(path, s0, len + 1);
1522 776d4d29 2018-06-17 stsp if (err)
1523 776d4d29 2018-06-17 stsp goto done;
1524 776d4d29 2018-06-17 stsp
1525 197aa481 2018-06-21 stsp s = s0;
1526 776d4d29 2018-06-17 stsp s++; /* skip leading '/' */
1527 67606321 2018-06-21 stsp len--;
1528 776d4d29 2018-06-17 stsp seg = s;
1529 67606321 2018-06-21 stsp while (len > 0) {
1530 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
1531 776d4d29 2018-06-17 stsp
1532 776d4d29 2018-06-17 stsp if (*s != '/') {
1533 776d4d29 2018-06-17 stsp s++;
1534 67606321 2018-06-21 stsp len--;
1535 00530cfb 2018-06-21 stsp if (*s)
1536 00530cfb 2018-06-21 stsp continue;
1537 776d4d29 2018-06-17 stsp }
1538 776d4d29 2018-06-17 stsp
1539 776d4d29 2018-06-17 stsp /* end of path segment */
1540 776d4d29 2018-06-17 stsp *s = '\0';
1541 776d4d29 2018-06-17 stsp
1542 db37e2c0 2018-06-21 stsp te = find_entry_by_name(tree, seg);
1543 db37e2c0 2018-06-21 stsp if (te == NULL) {
1544 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_NO_OBJ);
1545 776d4d29 2018-06-17 stsp goto done;
1546 776d4d29 2018-06-17 stsp }
1547 776d4d29 2018-06-17 stsp
1548 67606321 2018-06-21 stsp if (len == 0)
1549 67606321 2018-06-21 stsp break;
1550 67606321 2018-06-21 stsp
1551 776d4d29 2018-06-17 stsp seg = s + 1;
1552 776d4d29 2018-06-17 stsp s++;
1553 67606321 2018-06-21 stsp len--;
1554 776d4d29 2018-06-17 stsp if (*s) {
1555 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
1556 db37e2c0 2018-06-21 stsp te->id);
1557 db37e2c0 2018-06-21 stsp te = NULL;
1558 776d4d29 2018-06-17 stsp if (err)
1559 776d4d29 2018-06-17 stsp goto done;
1560 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1561 776d4d29 2018-06-17 stsp tree = next_tree;
1562 776d4d29 2018-06-17 stsp }
1563 776d4d29 2018-06-17 stsp }
1564 776d4d29 2018-06-17 stsp
1565 7bb0daa1 2018-06-21 stsp if (te) {
1566 db37e2c0 2018-06-21 stsp err = got_object_open(obj, repo, te->id);
1567 7bb0daa1 2018-06-21 stsp if (err)
1568 7bb0daa1 2018-06-21 stsp goto done;
1569 7bb0daa1 2018-06-21 stsp } else
1570 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_NO_OBJ);
1571 776d4d29 2018-06-17 stsp done:
1572 197aa481 2018-06-21 stsp free(s0);
1573 776d4d29 2018-06-17 stsp if (commit)
1574 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
1575 776d4d29 2018-06-17 stsp if (tree)
1576 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1577 776d4d29 2018-06-17 stsp return err;
1578 68482ea3 2017-11-27 stsp }