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