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