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