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 876c234b 2018-09-10 stsp #include <sys/syslimits.h>
24 d1cda826 2017-11-06 stsp
25 a1fd68d8 2018-01-12 stsp #include <errno.h>
26 2178c42e 2018-04-22 stsp #include <fcntl.h>
27 d71d75ad 2017-11-05 stsp #include <stdio.h>
28 ab9a70b2 2017-11-06 stsp #include <stdlib.h>
29 ab9a70b2 2017-11-06 stsp #include <string.h>
30 2178c42e 2018-04-22 stsp #include <stdint.h>
31 d71d75ad 2017-11-05 stsp #include <sha1.h>
32 ab9a70b2 2017-11-06 stsp #include <zlib.h>
33 ab9a70b2 2017-11-06 stsp #include <ctype.h>
34 ab9a70b2 2017-11-06 stsp #include <limits.h>
35 2178c42e 2018-04-22 stsp #include <imsg.h>
36 788c352e 2018-06-16 stsp #include <time.h>
37 d71d75ad 2017-11-05 stsp
38 ab9a70b2 2017-11-06 stsp #include "got_error.h"
39 d71d75ad 2017-11-05 stsp #include "got_object.h"
40 ab9a70b2 2017-11-06 stsp #include "got_repository.h"
41 511a516b 2018-05-19 stsp #include "got_opentemp.h"
42 d71d75ad 2017-11-05 stsp
43 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
44 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
45 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
46 2178c42e 2018-04-22 stsp #include "got_lib_path.h"
47 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
49 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
50 ad242220 2018-09-08 stsp #include "got_lib_object_parse.h"
51 7bb0daa1 2018-06-21 stsp #include "got_lib_repository.h"
52 1411938b 2018-02-12 stsp
53 ab9a70b2 2017-11-06 stsp #ifndef MIN
54 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
55 ab9a70b2 2017-11-06 stsp #endif
56 ab9a70b2 2017-11-06 stsp
57 ef0981d5 2018-02-12 stsp const struct got_error *
58 ef0981d5 2018-02-12 stsp got_object_id_str(char **outbuf, struct got_object_id *id)
59 d71d75ad 2017-11-05 stsp {
60 ef0981d5 2018-02-12 stsp static const size_t len = SHA1_DIGEST_STRING_LENGTH;
61 ef0981d5 2018-02-12 stsp
62 062ebb78 2018-07-12 stsp *outbuf = malloc(len);
63 ef0981d5 2018-02-12 stsp if (*outbuf == NULL)
64 0a585a0d 2018-03-17 stsp return got_error_from_errno();
65 ef0981d5 2018-02-12 stsp
66 ef0981d5 2018-02-12 stsp if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
67 ef0981d5 2018-02-12 stsp free(*outbuf);
68 ef0981d5 2018-02-12 stsp *outbuf = NULL;
69 ef0981d5 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
70 ef0981d5 2018-02-12 stsp }
71 ef0981d5 2018-02-12 stsp
72 ef0981d5 2018-02-12 stsp return NULL;
73 59ece79d 2018-02-12 stsp }
74 59ece79d 2018-02-12 stsp
75 a1fd68d8 2018-01-12 stsp int
76 a1fd68d8 2018-01-12 stsp got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
77 a1fd68d8 2018-01-12 stsp {
78 a1fd68d8 2018-01-12 stsp return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
79 8bf5b3c9 2018-03-17 stsp }
80 8bf5b3c9 2018-03-17 stsp
81 8bf5b3c9 2018-03-17 stsp struct got_object_id *
82 8bf5b3c9 2018-03-17 stsp got_object_id_dup(struct got_object_id *id1)
83 8bf5b3c9 2018-03-17 stsp {
84 8bf5b3c9 2018-03-17 stsp struct got_object_id *id2;
85 8bf5b3c9 2018-03-17 stsp
86 8bf5b3c9 2018-03-17 stsp id2 = malloc(sizeof(*id2));
87 8bf5b3c9 2018-03-17 stsp if (id2 == NULL)
88 8bf5b3c9 2018-03-17 stsp return NULL;
89 8bf5b3c9 2018-03-17 stsp memcpy(id2, id1, sizeof(*id2));
90 8bf5b3c9 2018-03-17 stsp return id2;
91 3235492e 2018-04-01 stsp }
92 3235492e 2018-04-01 stsp
93 3235492e 2018-04-01 stsp struct got_object_id *
94 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
95 3235492e 2018-04-01 stsp {
96 3235492e 2018-04-01 stsp return got_object_id_dup(&obj->id);
97 bacc9935 2018-05-20 stsp }
98 bacc9935 2018-05-20 stsp
99 bacc9935 2018-05-20 stsp const struct got_error *
100 bacc9935 2018-05-20 stsp got_object_get_id_str(char **outbuf, struct got_object *obj)
101 bacc9935 2018-05-20 stsp {
102 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
103 a1fd68d8 2018-01-12 stsp }
104 d71d75ad 2017-11-05 stsp
105 b107e67f 2018-01-19 stsp int
106 b107e67f 2018-01-19 stsp got_object_get_type(struct got_object *obj)
107 a1fd68d8 2018-01-12 stsp {
108 b107e67f 2018-01-19 stsp switch (obj->type) {
109 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
110 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
111 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
112 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
113 b107e67f 2018-01-19 stsp return obj->type;
114 96f5e8b3 2018-01-23 stsp default:
115 96f5e8b3 2018-01-23 stsp abort();
116 96f5e8b3 2018-01-23 stsp break;
117 d71d75ad 2017-11-05 stsp }
118 d71d75ad 2017-11-05 stsp
119 96f5e8b3 2018-01-23 stsp /* not reached */
120 96f5e8b3 2018-01-23 stsp return 0;
121 d71d75ad 2017-11-05 stsp }
122 ab9a70b2 2017-11-06 stsp
123 ab9a70b2 2017-11-06 stsp static const struct got_error *
124 4558fcd4 2018-01-14 stsp object_path(char **path, struct got_object_id *id, struct got_repository *repo)
125 ab9a70b2 2017-11-06 stsp {
126 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
127 7a132809 2018-07-23 stsp char *hex = NULL;
128 d1cda826 2017-11-06 stsp char *path_objects = got_repo_get_path_objects(repo);
129 e6b1056e 2018-04-22 stsp
130 e6b1056e 2018-04-22 stsp *path = NULL;
131 ab9a70b2 2017-11-06 stsp
132 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
133 0a585a0d 2018-03-17 stsp return got_error_from_errno();
134 ab9a70b2 2017-11-06 stsp
135 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
136 ef0981d5 2018-02-12 stsp if (err)
137 7a132809 2018-07-23 stsp goto done;
138 ab9a70b2 2017-11-06 stsp
139 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
140 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
141 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
142 ab9a70b2 2017-11-06 stsp
143 7a132809 2018-07-23 stsp done:
144 ef0981d5 2018-02-12 stsp free(hex);
145 d1cda826 2017-11-06 stsp free(path_objects);
146 d1cda826 2017-11-06 stsp return err;
147 d1cda826 2017-11-06 stsp }
148 d1cda826 2017-11-06 stsp
149 4ee4114f 2018-01-23 stsp static const struct got_error *
150 d5003b79 2018-04-22 stsp open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
151 d1cda826 2017-11-06 stsp {
152 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
153 a1fd68d8 2018-01-12 stsp char *path;
154 6c00b545 2018-01-17 stsp
155 6c00b545 2018-01-17 stsp err = object_path(&path, &obj->id, repo);
156 d1cda826 2017-11-06 stsp if (err)
157 d1cda826 2017-11-06 stsp return err;
158 d5003b79 2018-04-22 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
159 d5003b79 2018-04-22 stsp if (*fd == -1) {
160 6c00b545 2018-01-17 stsp err = got_error_from_errno();
161 6c00b545 2018-01-17 stsp goto done;
162 a1fd68d8 2018-01-12 stsp }
163 4558fcd4 2018-01-14 stsp done:
164 4558fcd4 2018-01-14 stsp free(path);
165 2090a03d 2018-09-09 stsp return err;
166 2090a03d 2018-09-09 stsp }
167 2090a03d 2018-09-09 stsp
168 2090a03d 2018-09-09 stsp static const struct got_error *
169 2090a03d 2018-09-09 stsp get_packfile_path(char **path_packfile, struct got_packidx *packidx)
170 2090a03d 2018-09-09 stsp {
171 2090a03d 2018-09-09 stsp size_t size;
172 2090a03d 2018-09-09 stsp
173 2090a03d 2018-09-09 stsp /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
174 2090a03d 2018-09-09 stsp size = strlen(packidx->path_packidx) + 2;
175 2090a03d 2018-09-09 stsp if (size < GOT_PACKFILE_NAMELEN + 1)
176 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_BAD_PATH);
177 2090a03d 2018-09-09 stsp
178 2090a03d 2018-09-09 stsp *path_packfile = calloc(size, sizeof(**path_packfile));
179 2090a03d 2018-09-09 stsp if (*path_packfile == NULL)
180 2090a03d 2018-09-09 stsp return got_error_from_errno();
181 2090a03d 2018-09-09 stsp
182 2090a03d 2018-09-09 stsp /* Copy up to and excluding ".idx". */
183 2090a03d 2018-09-09 stsp if (strlcpy(*path_packfile, packidx->path_packidx,
184 2090a03d 2018-09-09 stsp size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
185 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
186 2090a03d 2018-09-09 stsp
187 2090a03d 2018-09-09 stsp if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
188 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
189 2090a03d 2018-09-09 stsp
190 2090a03d 2018-09-09 stsp return NULL;
191 2090a03d 2018-09-09 stsp }
192 2090a03d 2018-09-09 stsp
193 2090a03d 2018-09-09 stsp static const struct got_error *
194 2090a03d 2018-09-09 stsp open_packed_object(struct got_object **obj, struct got_object_id *id,
195 2090a03d 2018-09-09 stsp struct got_repository *repo)
196 2090a03d 2018-09-09 stsp {
197 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
198 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
199 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
200 2090a03d 2018-09-09 stsp int idx;
201 2090a03d 2018-09-09 stsp char *path_packfile;
202 2090a03d 2018-09-09 stsp
203 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
204 2090a03d 2018-09-09 stsp if (err)
205 2090a03d 2018-09-09 stsp return err;
206 2090a03d 2018-09-09 stsp
207 2090a03d 2018-09-09 stsp err = get_packfile_path(&path_packfile, packidx);
208 2090a03d 2018-09-09 stsp if (err)
209 2090a03d 2018-09-09 stsp return err;
210 2090a03d 2018-09-09 stsp
211 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
212 2090a03d 2018-09-09 stsp if (pack == NULL) {
213 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
214 2090a03d 2018-09-09 stsp if (err)
215 2090a03d 2018-09-09 stsp goto done;
216 2090a03d 2018-09-09 stsp }
217 2090a03d 2018-09-09 stsp
218 876c234b 2018-09-10 stsp err = got_object_packed_read_privsep(obj, repo, pack, packidx, idx, id);
219 2090a03d 2018-09-09 stsp if (err)
220 2090a03d 2018-09-09 stsp goto done;
221 2090a03d 2018-09-09 stsp
222 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
223 2090a03d 2018-09-09 stsp done:
224 2090a03d 2018-09-09 stsp free(path_packfile);
225 4558fcd4 2018-01-14 stsp return err;
226 4558fcd4 2018-01-14 stsp }
227 a1fd68d8 2018-01-12 stsp
228 4558fcd4 2018-01-14 stsp const struct got_error *
229 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
230 4558fcd4 2018-01-14 stsp struct got_object_id *id)
231 4558fcd4 2018-01-14 stsp {
232 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
233 6c00b545 2018-01-17 stsp char *path;
234 2178c42e 2018-04-22 stsp int fd;
235 7bb0daa1 2018-06-21 stsp
236 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
237 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
238 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
239 7bb0daa1 2018-06-21 stsp return NULL;
240 7bb0daa1 2018-06-21 stsp }
241 4558fcd4 2018-01-14 stsp
242 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
243 4558fcd4 2018-01-14 stsp if (err)
244 4558fcd4 2018-01-14 stsp return err;
245 4558fcd4 2018-01-14 stsp
246 2178c42e 2018-04-22 stsp fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
247 2178c42e 2018-04-22 stsp if (fd == -1) {
248 6c00b545 2018-01-17 stsp if (errno != ENOENT) {
249 6c00b545 2018-01-17 stsp err = got_error_from_errno();
250 6c00b545 2018-01-17 stsp goto done;
251 6c00b545 2018-01-17 stsp }
252 2090a03d 2018-09-09 stsp err = open_packed_object(obj, id, repo);
253 6c00b545 2018-01-17 stsp if (err)
254 6c00b545 2018-01-17 stsp goto done;
255 6c00b545 2018-01-17 stsp if (*obj == NULL)
256 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NO_OBJ);
257 6c00b545 2018-01-17 stsp } else {
258 ad242220 2018-09-08 stsp err = got_object_read_header_privsep(obj, repo, fd);
259 6c00b545 2018-01-17 stsp if (err)
260 6c00b545 2018-01-17 stsp goto done;
261 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
262 6c00b545 2018-01-17 stsp }
263 7bb0daa1 2018-06-21 stsp
264 7bb0daa1 2018-06-21 stsp if (err == NULL) {
265 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
266 7bb0daa1 2018-06-21 stsp err = got_repo_cache_object(repo, id, *obj);
267 7bb0daa1 2018-06-21 stsp }
268 6c00b545 2018-01-17 stsp done:
269 6c00b545 2018-01-17 stsp free(path);
270 2178c42e 2018-04-22 stsp if (fd != -1)
271 2178c42e 2018-04-22 stsp close(fd);
272 ab9a70b2 2017-11-06 stsp return err;
273 6c00b545 2018-01-17 stsp
274 ab9a70b2 2017-11-06 stsp }
275 ab9a70b2 2017-11-06 stsp
276 6dfa2fd3 2018-02-12 stsp const struct got_error *
277 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
278 6dfa2fd3 2018-02-12 stsp const char *id_str)
279 6dfa2fd3 2018-02-12 stsp {
280 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
281 6dfa2fd3 2018-02-12 stsp
282 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
283 6dfa2fd3 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
284 6dfa2fd3 2018-02-12 stsp
285 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
286 bff6ca00 2018-04-23 stsp }
287 bff6ca00 2018-04-23 stsp
288 bff6ca00 2018-04-23 stsp const struct got_error *
289 be6a1b5a 2018-06-11 stsp got_object_open_as_commit(struct got_commit_object **commit,
290 be6a1b5a 2018-06-11 stsp struct got_repository *repo, struct got_object_id *id)
291 be6a1b5a 2018-06-11 stsp {
292 be6a1b5a 2018-06-11 stsp const struct got_error *err;
293 be6a1b5a 2018-06-11 stsp struct got_object *obj;
294 835e0dbd 2018-06-21 stsp
295 835e0dbd 2018-06-21 stsp *commit = NULL;
296 be6a1b5a 2018-06-11 stsp
297 be6a1b5a 2018-06-11 stsp err = got_object_open(&obj, repo, id);
298 be6a1b5a 2018-06-11 stsp if (err)
299 be6a1b5a 2018-06-11 stsp return err;
300 be6a1b5a 2018-06-11 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
301 be6a1b5a 2018-06-11 stsp err = got_error(GOT_ERR_OBJ_TYPE);
302 be6a1b5a 2018-06-11 stsp goto done;
303 be6a1b5a 2018-06-11 stsp }
304 be6a1b5a 2018-06-11 stsp
305 be6a1b5a 2018-06-11 stsp err = got_object_commit_open(commit, repo, obj);
306 be6a1b5a 2018-06-11 stsp done:
307 be6a1b5a 2018-06-11 stsp got_object_close(obj);
308 be6a1b5a 2018-06-11 stsp return err;
309 be6a1b5a 2018-06-11 stsp }
310 be6a1b5a 2018-06-11 stsp
311 be6a1b5a 2018-06-11 stsp const struct got_error *
312 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
313 dbc6a6b6 2018-07-12 stsp {
314 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
315 dbc6a6b6 2018-07-12 stsp
316 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
317 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
318 dbc6a6b6 2018-07-12 stsp return got_error_from_errno();
319 dbc6a6b6 2018-07-12 stsp
320 dbc6a6b6 2018-07-12 stsp (*qid)->id = got_object_id_dup(id);
321 dbc6a6b6 2018-07-12 stsp if ((*qid)->id == NULL) {
322 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
323 fa2f6902 2018-07-23 stsp got_object_qid_free(*qid);
324 dbc6a6b6 2018-07-12 stsp *qid = NULL;
325 dbc6a6b6 2018-07-12 stsp return err;
326 dbc6a6b6 2018-07-12 stsp }
327 dbc6a6b6 2018-07-12 stsp
328 dbc6a6b6 2018-07-12 stsp return NULL;
329 dbc6a6b6 2018-07-12 stsp }
330 7e212e3d 2018-09-09 stsp
331 7e212e3d 2018-09-09 stsp static const struct got_error *
332 7e212e3d 2018-09-09 stsp extract_packed_object_to_mem(uint8_t **buf, size_t *len,
333 7e212e3d 2018-09-09 stsp struct got_object *obj, struct got_repository *repo)
334 7e212e3d 2018-09-09 stsp {
335 7e212e3d 2018-09-09 stsp const struct got_error *err = NULL;
336 7e212e3d 2018-09-09 stsp struct got_pack *pack;
337 dbc6a6b6 2018-07-12 stsp
338 7e212e3d 2018-09-09 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
339 7e212e3d 2018-09-09 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
340 7e212e3d 2018-09-09 stsp
341 7e212e3d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
342 7e212e3d 2018-09-09 stsp if (pack == NULL) {
343 7e212e3d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo,
344 7e212e3d 2018-09-09 stsp obj->path_packfile, NULL);
345 7e212e3d 2018-09-09 stsp if (err)
346 7e212e3d 2018-09-09 stsp return err;
347 7e212e3d 2018-09-09 stsp }
348 7e212e3d 2018-09-09 stsp
349 7e212e3d 2018-09-09 stsp return got_packfile_extract_object_to_mem(buf, len, obj, pack);
350 7e212e3d 2018-09-09 stsp }
351 7e212e3d 2018-09-09 stsp
352 d1cda826 2017-11-06 stsp const struct got_error *
353 d1cda826 2017-11-06 stsp got_object_commit_open(struct got_commit_object **commit,
354 d1cda826 2017-11-06 stsp struct got_repository *repo, struct got_object *obj)
355 d1cda826 2017-11-06 stsp {
356 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
357 1943de01 2018-06-22 stsp
358 1943de01 2018-06-22 stsp *commit = got_repo_get_cached_commit(repo, &obj->id);
359 1943de01 2018-06-22 stsp if (*commit != NULL) {
360 1943de01 2018-06-22 stsp (*commit)->refcnt++;
361 1943de01 2018-06-22 stsp return NULL;
362 1943de01 2018-06-22 stsp }
363 d1cda826 2017-11-06 stsp
364 d1cda826 2017-11-06 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
365 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_OBJ_TYPE);
366 d1cda826 2017-11-06 stsp
367 ea35256b 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
368 ea35256b 2018-03-16 stsp uint8_t *buf;
369 ea35256b 2018-03-16 stsp size_t len;
370 7e212e3d 2018-09-09 stsp err = extract_packed_object_to_mem(&buf, &len, obj, repo);
371 ea35256b 2018-03-16 stsp if (err)
372 ea35256b 2018-03-16 stsp return err;
373 b29656e2 2018-03-16 stsp obj->size = len;
374 a440fac0 2018-09-06 stsp err = got_object_parse_commit(commit, buf, len);
375 ea35256b 2018-03-16 stsp free(buf);
376 ea35256b 2018-03-16 stsp } else {
377 d5003b79 2018-04-22 stsp int fd;
378 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
379 ea35256b 2018-03-16 stsp if (err)
380 ea35256b 2018-03-16 stsp return err;
381 ad242220 2018-09-08 stsp err = got_object_read_commit_privsep(commit, obj, fd, repo);
382 bff6ca00 2018-04-23 stsp close(fd);
383 ea35256b 2018-03-16 stsp }
384 1943de01 2018-06-22 stsp
385 1943de01 2018-06-22 stsp if (err == NULL) {
386 1943de01 2018-06-22 stsp (*commit)->refcnt++;
387 1943de01 2018-06-22 stsp err = got_repo_cache_commit(repo, &obj->id, *commit);
388 1943de01 2018-06-22 stsp }
389 1943de01 2018-06-22 stsp
390 d1cda826 2017-11-06 stsp return err;
391 d1cda826 2017-11-06 stsp }
392 d1cda826 2017-11-06 stsp
393 0ffeb3c2 2017-11-26 stsp const struct got_error *
394 0ffeb3c2 2017-11-26 stsp got_object_tree_open(struct got_tree_object **tree,
395 0ffeb3c2 2017-11-26 stsp struct got_repository *repo, struct got_object *obj)
396 0ffeb3c2 2017-11-26 stsp {
397 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
398 f6be5c30 2018-06-22 stsp
399 f6be5c30 2018-06-22 stsp *tree = got_repo_get_cached_tree(repo, &obj->id);
400 f6be5c30 2018-06-22 stsp if (*tree != NULL) {
401 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
402 f6be5c30 2018-06-22 stsp return NULL;
403 f6be5c30 2018-06-22 stsp }
404 0ffeb3c2 2017-11-26 stsp
405 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
406 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
407 0ffeb3c2 2017-11-26 stsp
408 e0ab43e7 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
409 e0ab43e7 2018-03-16 stsp uint8_t *buf;
410 e0ab43e7 2018-03-16 stsp size_t len;
411 7e212e3d 2018-09-09 stsp err = extract_packed_object_to_mem(&buf, &len, obj, repo);
412 e0ab43e7 2018-03-16 stsp if (err)
413 e0ab43e7 2018-03-16 stsp return err;
414 b29656e2 2018-03-16 stsp obj->size = len;
415 a440fac0 2018-09-06 stsp err = got_object_parse_tree(tree, buf, len);
416 e0ab43e7 2018-03-16 stsp free(buf);
417 e0ab43e7 2018-03-16 stsp } else {
418 d5003b79 2018-04-22 stsp int fd;
419 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
420 e0ab43e7 2018-03-16 stsp if (err)
421 e0ab43e7 2018-03-16 stsp return err;
422 ad242220 2018-09-08 stsp err = got_object_read_tree_privsep(tree, obj, fd, repo);
423 d5003b79 2018-04-22 stsp close(fd);
424 776d4d29 2018-06-17 stsp }
425 f6be5c30 2018-06-22 stsp
426 f6be5c30 2018-06-22 stsp if (err == NULL) {
427 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
428 f6be5c30 2018-06-22 stsp err = got_repo_cache_tree(repo, &obj->id, *tree);
429 f6be5c30 2018-06-22 stsp }
430 f6be5c30 2018-06-22 stsp
431 776d4d29 2018-06-17 stsp return err;
432 776d4d29 2018-06-17 stsp }
433 776d4d29 2018-06-17 stsp
434 776d4d29 2018-06-17 stsp const struct got_error *
435 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
436 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
437 776d4d29 2018-06-17 stsp {
438 776d4d29 2018-06-17 stsp const struct got_error *err;
439 776d4d29 2018-06-17 stsp struct got_object *obj;
440 835e0dbd 2018-06-21 stsp
441 835e0dbd 2018-06-21 stsp *tree = NULL;
442 776d4d29 2018-06-17 stsp
443 776d4d29 2018-06-17 stsp err = got_object_open(&obj, repo, id);
444 776d4d29 2018-06-17 stsp if (err)
445 776d4d29 2018-06-17 stsp return err;
446 1cbc02b6 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
447 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_OBJ_TYPE);
448 776d4d29 2018-06-17 stsp goto done;
449 e0ab43e7 2018-03-16 stsp }
450 776d4d29 2018-06-17 stsp
451 776d4d29 2018-06-17 stsp err = got_object_tree_open(tree, repo, obj);
452 776d4d29 2018-06-17 stsp done:
453 776d4d29 2018-06-17 stsp got_object_close(obj);
454 0ffeb3c2 2017-11-26 stsp return err;
455 57efb1af 2018-04-24 stsp }
456 ff6b18f8 2018-04-24 stsp
457 883f0469 2018-06-23 stsp const struct got_tree_entries *
458 883f0469 2018-06-23 stsp got_object_tree_get_entries(struct got_tree_object *tree)
459 883f0469 2018-06-23 stsp {
460 883f0469 2018-06-23 stsp return &tree->entries;
461 883f0469 2018-06-23 stsp }
462 24140570 2018-09-09 stsp
463 24140570 2018-09-09 stsp static const struct got_error *
464 24140570 2018-09-09 stsp extract_packed_object(FILE **f, struct got_object *obj,
465 24140570 2018-09-09 stsp struct got_repository *repo)
466 24140570 2018-09-09 stsp {
467 24140570 2018-09-09 stsp const struct got_error *err = NULL;
468 24140570 2018-09-09 stsp struct got_pack *pack;
469 24140570 2018-09-09 stsp int fd;
470 883f0469 2018-06-23 stsp
471 24140570 2018-09-09 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
472 24140570 2018-09-09 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
473 24140570 2018-09-09 stsp
474 24140570 2018-09-09 stsp fd = got_opentempfd();
475 24140570 2018-09-09 stsp if (fd == -1)
476 24140570 2018-09-09 stsp return got_error_from_errno();
477 24140570 2018-09-09 stsp
478 24140570 2018-09-09 stsp *f = fdopen(fd, "w+");
479 24140570 2018-09-09 stsp if (*f == NULL) {
480 24140570 2018-09-09 stsp err = got_error_from_errno();
481 24140570 2018-09-09 stsp goto done;
482 24140570 2018-09-09 stsp }
483 24140570 2018-09-09 stsp
484 24140570 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
485 24140570 2018-09-09 stsp if (pack == NULL) {
486 24140570 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo,
487 24140570 2018-09-09 stsp obj->path_packfile, NULL);
488 24140570 2018-09-09 stsp if (err)
489 24140570 2018-09-09 stsp goto done;
490 24140570 2018-09-09 stsp }
491 24140570 2018-09-09 stsp
492 24140570 2018-09-09 stsp err = got_packfile_extract_object(pack, obj, *f);
493 24140570 2018-09-09 stsp done:
494 24140570 2018-09-09 stsp if (err) {
495 24140570 2018-09-09 stsp if (*f == NULL)
496 24140570 2018-09-09 stsp close(fd);
497 24140570 2018-09-09 stsp else
498 24140570 2018-09-09 stsp fclose(*f);
499 24140570 2018-09-09 stsp *f = NULL;
500 24140570 2018-09-09 stsp }
501 24140570 2018-09-09 stsp return err;
502 24140570 2018-09-09 stsp }
503 24140570 2018-09-09 stsp
504 68482ea3 2017-11-27 stsp const struct got_error *
505 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
506 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
507 68482ea3 2017-11-27 stsp {
508 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
509 68482ea3 2017-11-27 stsp
510 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
511 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
512 68482ea3 2017-11-27 stsp
513 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
514 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
515 7d283eee 2017-11-29 stsp
516 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
517 4558fcd4 2018-01-14 stsp if (*blob == NULL)
518 0a585a0d 2018-03-17 stsp return got_error_from_errno();
519 68482ea3 2017-11-27 stsp
520 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
521 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
522 15c8b0e6 2018-04-24 stsp err = got_error_from_errno();
523 c7254d79 2018-04-24 stsp goto done;
524 15c8b0e6 2018-04-24 stsp }
525 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
526 24140570 2018-09-09 stsp err = extract_packed_object(&((*blob)->f), obj, repo);
527 c7254d79 2018-04-24 stsp if (err)
528 c7254d79 2018-04-24 stsp goto done;
529 eb651edf 2018-02-11 stsp } else {
530 3aca5731 2018-04-24 stsp int infd, outfd;
531 2967a784 2018-04-24 stsp size_t size;
532 2967a784 2018-04-24 stsp struct stat sb;
533 c7254d79 2018-04-24 stsp
534 3aca5731 2018-04-24 stsp err = open_loose_object(&infd, obj, repo);
535 c7254d79 2018-04-24 stsp if (err)
536 c7254d79 2018-04-24 stsp goto done;
537 c7254d79 2018-04-24 stsp
538 3aca5731 2018-04-24 stsp
539 3aca5731 2018-04-24 stsp outfd = got_opentempfd();
540 3aca5731 2018-04-24 stsp if (outfd == -1) {
541 3aca5731 2018-04-24 stsp err = got_error_from_errno();
542 3aca5731 2018-04-24 stsp close(infd);
543 3aca5731 2018-04-24 stsp goto done;
544 3aca5731 2018-04-24 stsp }
545 3aca5731 2018-04-24 stsp
546 ad242220 2018-09-08 stsp err = got_object_read_blob_privsep(&size, outfd, infd, repo);
547 3aca5731 2018-04-24 stsp close(infd);
548 57efb1af 2018-04-24 stsp if (err)
549 c7254d79 2018-04-24 stsp goto done;
550 3aca5731 2018-04-24 stsp
551 2967a784 2018-04-24 stsp if (size != obj->hdrlen + obj->size) {
552 1a6b3ab7 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
553 2967a784 2018-04-24 stsp close(outfd);
554 2967a784 2018-04-24 stsp goto done;
555 2967a784 2018-04-24 stsp }
556 2967a784 2018-04-24 stsp
557 2967a784 2018-04-24 stsp if (fstat(outfd, &sb) == -1) {
558 2967a784 2018-04-24 stsp err = got_error_from_errno();
559 2967a784 2018-04-24 stsp close(outfd);
560 2967a784 2018-04-24 stsp goto done;
561 2967a784 2018-04-24 stsp }
562 2967a784 2018-04-24 stsp
563 2967a784 2018-04-24 stsp if (sb.st_size != size) {
564 2967a784 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
565 2967a784 2018-04-24 stsp close(outfd);
566 2967a784 2018-04-24 stsp goto done;
567 2967a784 2018-04-24 stsp }
568 2967a784 2018-04-24 stsp
569 3aca5731 2018-04-24 stsp (*blob)->f = fdopen(outfd, "rb");
570 3aca5731 2018-04-24 stsp if ((*blob)->f == NULL) {
571 3aca5731 2018-04-24 stsp err = got_error_from_errno();
572 3aca5731 2018-04-24 stsp close(outfd);
573 3aca5731 2018-04-24 stsp goto done;
574 3aca5731 2018-04-24 stsp }
575 68482ea3 2017-11-27 stsp }
576 68482ea3 2017-11-27 stsp
577 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
578 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
579 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
580 7d283eee 2017-11-29 stsp
581 c7254d79 2018-04-24 stsp done:
582 c7254d79 2018-04-24 stsp if (err && *blob) {
583 c7254d79 2018-04-24 stsp if ((*blob)->f)
584 c7254d79 2018-04-24 stsp fclose((*blob)->f);
585 c7254d79 2018-04-24 stsp free((*blob)->read_buf);
586 c7254d79 2018-04-24 stsp free(*blob);
587 c7254d79 2018-04-24 stsp *blob = NULL;
588 a19581a2 2018-06-21 stsp }
589 a19581a2 2018-06-21 stsp return err;
590 a19581a2 2018-06-21 stsp }
591 a19581a2 2018-06-21 stsp
592 a19581a2 2018-06-21 stsp const struct got_error *
593 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
594 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
595 a19581a2 2018-06-21 stsp size_t blocksize)
596 a19581a2 2018-06-21 stsp {
597 a19581a2 2018-06-21 stsp const struct got_error *err;
598 a19581a2 2018-06-21 stsp struct got_object *obj;
599 835e0dbd 2018-06-21 stsp
600 835e0dbd 2018-06-21 stsp *blob = NULL;
601 a19581a2 2018-06-21 stsp
602 a19581a2 2018-06-21 stsp err = got_object_open(&obj, repo, id);
603 a19581a2 2018-06-21 stsp if (err)
604 a19581a2 2018-06-21 stsp return err;
605 a19581a2 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
606 a19581a2 2018-06-21 stsp err = got_error(GOT_ERR_OBJ_TYPE);
607 a19581a2 2018-06-21 stsp goto done;
608 c7254d79 2018-04-24 stsp }
609 a19581a2 2018-06-21 stsp
610 a19581a2 2018-06-21 stsp err = got_object_blob_open(blob, repo, obj, blocksize);
611 a19581a2 2018-06-21 stsp done:
612 a19581a2 2018-06-21 stsp got_object_close(obj);
613 68482ea3 2017-11-27 stsp return err;
614 0ffeb3c2 2017-11-26 stsp }
615 68482ea3 2017-11-27 stsp
616 68482ea3 2017-11-27 stsp void
617 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
618 68482ea3 2017-11-27 stsp {
619 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
620 68482ea3 2017-11-27 stsp fclose(blob->f);
621 68482ea3 2017-11-27 stsp free(blob);
622 f934cf2c 2018-02-12 stsp }
623 f934cf2c 2018-02-12 stsp
624 f934cf2c 2018-02-12 stsp char *
625 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
626 f934cf2c 2018-02-12 stsp {
627 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
628 f934cf2c 2018-02-12 stsp }
629 f934cf2c 2018-02-12 stsp
630 f934cf2c 2018-02-12 stsp size_t
631 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
632 f934cf2c 2018-02-12 stsp {
633 f934cf2c 2018-02-12 stsp return blob->hdrlen;
634 68482ea3 2017-11-27 stsp }
635 68482ea3 2017-11-27 stsp
636 f934cf2c 2018-02-12 stsp const uint8_t *
637 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
638 f934cf2c 2018-02-12 stsp {
639 f934cf2c 2018-02-12 stsp return blob->read_buf;
640 f934cf2c 2018-02-12 stsp }
641 f934cf2c 2018-02-12 stsp
642 68482ea3 2017-11-27 stsp const struct got_error *
643 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
644 68482ea3 2017-11-27 stsp {
645 eb651edf 2018-02-11 stsp size_t n;
646 eb651edf 2018-02-11 stsp
647 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
648 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
649 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
650 eb651edf 2018-02-11 stsp *outlenp = n;
651 35e9ba5d 2018-06-21 stsp return NULL;
652 35e9ba5d 2018-06-21 stsp }
653 35e9ba5d 2018-06-21 stsp
654 35e9ba5d 2018-06-21 stsp const struct got_error *
655 84451b3e 2018-07-10 stsp got_object_blob_dump_to_file(size_t *total_len, size_t *nlines,
656 84451b3e 2018-07-10 stsp FILE *outfile, struct got_blob_object *blob)
657 35e9ba5d 2018-06-21 stsp {
658 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
659 35e9ba5d 2018-06-21 stsp size_t len, hdrlen;
660 84451b3e 2018-07-10 stsp const uint8_t *buf;
661 84451b3e 2018-07-10 stsp int i;
662 84451b3e 2018-07-10 stsp
663 84451b3e 2018-07-10 stsp if (total_len)
664 84451b3e 2018-07-10 stsp *total_len = 0;
665 84451b3e 2018-07-10 stsp if (nlines)
666 84451b3e 2018-07-10 stsp *nlines = 0;
667 35e9ba5d 2018-06-21 stsp
668 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
669 35e9ba5d 2018-06-21 stsp do {
670 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
671 35e9ba5d 2018-06-21 stsp if (err)
672 35e9ba5d 2018-06-21 stsp return err;
673 35e9ba5d 2018-06-21 stsp if (len == 0)
674 35e9ba5d 2018-06-21 stsp break;
675 84451b3e 2018-07-10 stsp if (total_len)
676 84451b3e 2018-07-10 stsp *total_len += len;
677 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
678 84451b3e 2018-07-10 stsp if (nlines) {
679 84451b3e 2018-07-10 stsp for (i = 0; i < len; i++) {
680 84451b3e 2018-07-10 stsp if (buf[i] == '\n')
681 84451b3e 2018-07-10 stsp (*nlines)++;
682 84451b3e 2018-07-10 stsp }
683 84451b3e 2018-07-10 stsp }
684 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
685 84451b3e 2018-07-10 stsp fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
686 35e9ba5d 2018-06-21 stsp hdrlen = 0;
687 35e9ba5d 2018-06-21 stsp } while (len != 0);
688 35e9ba5d 2018-06-21 stsp
689 35e9ba5d 2018-06-21 stsp fflush(outfile);
690 35e9ba5d 2018-06-21 stsp rewind(outfile);
691 35e9ba5d 2018-06-21 stsp
692 776d4d29 2018-06-17 stsp return NULL;
693 776d4d29 2018-06-17 stsp }
694 776d4d29 2018-06-17 stsp
695 776d4d29 2018-06-17 stsp static struct got_tree_entry *
696 776d4d29 2018-06-17 stsp find_entry_by_name(struct got_tree_object *tree, const char *name)
697 776d4d29 2018-06-17 stsp {
698 776d4d29 2018-06-17 stsp struct got_tree_entry *te;
699 776d4d29 2018-06-17 stsp
700 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
701 776d4d29 2018-06-17 stsp if (strcmp(te->name, name) == 0)
702 776d4d29 2018-06-17 stsp return te;
703 776d4d29 2018-06-17 stsp }
704 eb651edf 2018-02-11 stsp return NULL;
705 776d4d29 2018-06-17 stsp }
706 776d4d29 2018-06-17 stsp
707 776d4d29 2018-06-17 stsp const struct got_error *
708 776d4d29 2018-06-17 stsp got_object_open_by_path(struct got_object **obj, struct got_repository *repo,
709 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
710 776d4d29 2018-06-17 stsp {
711 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
712 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
713 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
714 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
715 197aa481 2018-06-21 stsp char *seg, *s, *s0 = NULL;
716 67606321 2018-06-21 stsp size_t len = strlen(path);
717 776d4d29 2018-06-17 stsp
718 776d4d29 2018-06-17 stsp *obj = NULL;
719 776d4d29 2018-06-17 stsp
720 776d4d29 2018-06-17 stsp /* We are expecting an absolute in-repository path. */
721 776d4d29 2018-06-17 stsp if (path[0] != '/')
722 776d4d29 2018-06-17 stsp return got_error(GOT_ERR_NOT_ABSPATH);
723 776d4d29 2018-06-17 stsp
724 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
725 776d4d29 2018-06-17 stsp if (err)
726 776d4d29 2018-06-17 stsp goto done;
727 776d4d29 2018-06-17 stsp
728 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
729 776d4d29 2018-06-17 stsp if (path[1] == '\0') {
730 776d4d29 2018-06-17 stsp err = got_object_open(obj, repo, commit->tree_id);
731 ca008b32 2018-07-22 stsp goto done;
732 776d4d29 2018-06-17 stsp }
733 776d4d29 2018-06-17 stsp
734 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
735 776d4d29 2018-06-17 stsp if (err)
736 776d4d29 2018-06-17 stsp goto done;
737 776d4d29 2018-06-17 stsp
738 197aa481 2018-06-21 stsp s0 = strdup(path);
739 197aa481 2018-06-21 stsp if (s0 == NULL) {
740 776d4d29 2018-06-17 stsp err = got_error_from_errno();
741 776d4d29 2018-06-17 stsp goto done;
742 776d4d29 2018-06-17 stsp }
743 67606321 2018-06-21 stsp err = got_canonpath(path, s0, len + 1);
744 776d4d29 2018-06-17 stsp if (err)
745 776d4d29 2018-06-17 stsp goto done;
746 776d4d29 2018-06-17 stsp
747 197aa481 2018-06-21 stsp s = s0;
748 776d4d29 2018-06-17 stsp s++; /* skip leading '/' */
749 67606321 2018-06-21 stsp len--;
750 776d4d29 2018-06-17 stsp seg = s;
751 67606321 2018-06-21 stsp while (len > 0) {
752 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
753 776d4d29 2018-06-17 stsp
754 776d4d29 2018-06-17 stsp if (*s != '/') {
755 776d4d29 2018-06-17 stsp s++;
756 67606321 2018-06-21 stsp len--;
757 00530cfb 2018-06-21 stsp if (*s)
758 00530cfb 2018-06-21 stsp continue;
759 776d4d29 2018-06-17 stsp }
760 776d4d29 2018-06-17 stsp
761 776d4d29 2018-06-17 stsp /* end of path segment */
762 776d4d29 2018-06-17 stsp *s = '\0';
763 776d4d29 2018-06-17 stsp
764 db37e2c0 2018-06-21 stsp te = find_entry_by_name(tree, seg);
765 db37e2c0 2018-06-21 stsp if (te == NULL) {
766 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_NO_OBJ);
767 776d4d29 2018-06-17 stsp goto done;
768 776d4d29 2018-06-17 stsp }
769 776d4d29 2018-06-17 stsp
770 67606321 2018-06-21 stsp if (len == 0)
771 67606321 2018-06-21 stsp break;
772 67606321 2018-06-21 stsp
773 776d4d29 2018-06-17 stsp seg = s + 1;
774 776d4d29 2018-06-17 stsp s++;
775 67606321 2018-06-21 stsp len--;
776 776d4d29 2018-06-17 stsp if (*s) {
777 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
778 db37e2c0 2018-06-21 stsp te->id);
779 db37e2c0 2018-06-21 stsp te = NULL;
780 776d4d29 2018-06-17 stsp if (err)
781 776d4d29 2018-06-17 stsp goto done;
782 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
783 776d4d29 2018-06-17 stsp tree = next_tree;
784 776d4d29 2018-06-17 stsp }
785 776d4d29 2018-06-17 stsp }
786 776d4d29 2018-06-17 stsp
787 ca008b32 2018-07-22 stsp if (te)
788 db37e2c0 2018-06-21 stsp err = got_object_open(obj, repo, te->id);
789 ca008b32 2018-07-22 stsp else
790 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_NO_OBJ);
791 776d4d29 2018-06-17 stsp done:
792 197aa481 2018-06-21 stsp free(s0);
793 776d4d29 2018-06-17 stsp if (commit)
794 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
795 776d4d29 2018-06-17 stsp if (tree)
796 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
797 776d4d29 2018-06-17 stsp return err;
798 68482ea3 2017-11-27 stsp }