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 7e212e3d 2018-09-09 stsp }
330 7e212e3d 2018-09-09 stsp
331 d1cda826 2017-11-06 stsp const struct got_error *
332 d1cda826 2017-11-06 stsp got_object_commit_open(struct got_commit_object **commit,
333 d1cda826 2017-11-06 stsp struct got_repository *repo, struct got_object *obj)
334 d1cda826 2017-11-06 stsp {
335 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
336 1943de01 2018-06-22 stsp
337 1943de01 2018-06-22 stsp *commit = got_repo_get_cached_commit(repo, &obj->id);
338 1943de01 2018-06-22 stsp if (*commit != NULL) {
339 1943de01 2018-06-22 stsp (*commit)->refcnt++;
340 1943de01 2018-06-22 stsp return NULL;
341 1943de01 2018-06-22 stsp }
342 d1cda826 2017-11-06 stsp
343 d1cda826 2017-11-06 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
344 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_OBJ_TYPE);
345 d1cda826 2017-11-06 stsp
346 ea35256b 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
347 cfd633c2 2018-09-10 stsp struct got_pack *pack;
348 cfd633c2 2018-09-10 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
349 cfd633c2 2018-09-10 stsp if (pack == NULL) {
350 cfd633c2 2018-09-10 stsp err = got_repo_cache_pack(&pack, repo,
351 cfd633c2 2018-09-10 stsp obj->path_packfile, NULL);
352 cfd633c2 2018-09-10 stsp if (err)
353 cfd633c2 2018-09-10 stsp return err;
354 cfd633c2 2018-09-10 stsp }
355 cfd633c2 2018-09-10 stsp err = got_object_read_packed_commit_privsep(commit, obj, pack);
356 ea35256b 2018-03-16 stsp } else {
357 d5003b79 2018-04-22 stsp int fd;
358 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
359 ea35256b 2018-03-16 stsp if (err)
360 ea35256b 2018-03-16 stsp return err;
361 ad242220 2018-09-08 stsp err = got_object_read_commit_privsep(commit, obj, fd, repo);
362 bff6ca00 2018-04-23 stsp close(fd);
363 ea35256b 2018-03-16 stsp }
364 1943de01 2018-06-22 stsp
365 1943de01 2018-06-22 stsp if (err == NULL) {
366 1943de01 2018-06-22 stsp (*commit)->refcnt++;
367 1943de01 2018-06-22 stsp err = got_repo_cache_commit(repo, &obj->id, *commit);
368 1943de01 2018-06-22 stsp }
369 1943de01 2018-06-22 stsp
370 d1cda826 2017-11-06 stsp return err;
371 d1cda826 2017-11-06 stsp }
372 d1cda826 2017-11-06 stsp
373 0ffeb3c2 2017-11-26 stsp const struct got_error *
374 0ffeb3c2 2017-11-26 stsp got_object_tree_open(struct got_tree_object **tree,
375 0ffeb3c2 2017-11-26 stsp struct got_repository *repo, struct got_object *obj)
376 0ffeb3c2 2017-11-26 stsp {
377 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
378 f6be5c30 2018-06-22 stsp
379 f6be5c30 2018-06-22 stsp *tree = got_repo_get_cached_tree(repo, &obj->id);
380 f6be5c30 2018-06-22 stsp if (*tree != NULL) {
381 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
382 f6be5c30 2018-06-22 stsp return NULL;
383 f6be5c30 2018-06-22 stsp }
384 0ffeb3c2 2017-11-26 stsp
385 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
386 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
387 0ffeb3c2 2017-11-26 stsp
388 e0ab43e7 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
389 e7885405 2018-09-10 stsp struct got_pack *pack;
390 e7885405 2018-09-10 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
391 e7885405 2018-09-10 stsp if (pack == NULL) {
392 e7885405 2018-09-10 stsp err = got_repo_cache_pack(&pack, repo,
393 e7885405 2018-09-10 stsp obj->path_packfile, NULL);
394 e7885405 2018-09-10 stsp if (err)
395 e7885405 2018-09-10 stsp return err;
396 e7885405 2018-09-10 stsp }
397 e7885405 2018-09-10 stsp err = got_object_read_packed_tree_privsep(tree, obj, pack);
398 e0ab43e7 2018-03-16 stsp } else {
399 d5003b79 2018-04-22 stsp int fd;
400 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
401 e0ab43e7 2018-03-16 stsp if (err)
402 e0ab43e7 2018-03-16 stsp return err;
403 ad242220 2018-09-08 stsp err = got_object_read_tree_privsep(tree, obj, fd, repo);
404 d5003b79 2018-04-22 stsp close(fd);
405 776d4d29 2018-06-17 stsp }
406 f6be5c30 2018-06-22 stsp
407 f6be5c30 2018-06-22 stsp if (err == NULL) {
408 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
409 f6be5c30 2018-06-22 stsp err = got_repo_cache_tree(repo, &obj->id, *tree);
410 f6be5c30 2018-06-22 stsp }
411 f6be5c30 2018-06-22 stsp
412 776d4d29 2018-06-17 stsp return err;
413 776d4d29 2018-06-17 stsp }
414 776d4d29 2018-06-17 stsp
415 776d4d29 2018-06-17 stsp const struct got_error *
416 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
417 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
418 776d4d29 2018-06-17 stsp {
419 776d4d29 2018-06-17 stsp const struct got_error *err;
420 776d4d29 2018-06-17 stsp struct got_object *obj;
421 835e0dbd 2018-06-21 stsp
422 835e0dbd 2018-06-21 stsp *tree = NULL;
423 776d4d29 2018-06-17 stsp
424 776d4d29 2018-06-17 stsp err = got_object_open(&obj, repo, id);
425 776d4d29 2018-06-17 stsp if (err)
426 776d4d29 2018-06-17 stsp return err;
427 1cbc02b6 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
428 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_OBJ_TYPE);
429 776d4d29 2018-06-17 stsp goto done;
430 e0ab43e7 2018-03-16 stsp }
431 776d4d29 2018-06-17 stsp
432 776d4d29 2018-06-17 stsp err = got_object_tree_open(tree, repo, obj);
433 776d4d29 2018-06-17 stsp done:
434 776d4d29 2018-06-17 stsp got_object_close(obj);
435 0ffeb3c2 2017-11-26 stsp return err;
436 57efb1af 2018-04-24 stsp }
437 ff6b18f8 2018-04-24 stsp
438 883f0469 2018-06-23 stsp const struct got_tree_entries *
439 883f0469 2018-06-23 stsp got_object_tree_get_entries(struct got_tree_object *tree)
440 883f0469 2018-06-23 stsp {
441 883f0469 2018-06-23 stsp return &tree->entries;
442 883f0469 2018-06-23 stsp }
443 24140570 2018-09-09 stsp
444 68482ea3 2017-11-27 stsp const struct got_error *
445 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
446 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
447 68482ea3 2017-11-27 stsp {
448 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
449 55da3778 2018-09-10 stsp int outfd;
450 55da3778 2018-09-10 stsp size_t size;
451 55da3778 2018-09-10 stsp struct stat sb;
452 68482ea3 2017-11-27 stsp
453 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
454 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
455 68482ea3 2017-11-27 stsp
456 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
457 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
458 7d283eee 2017-11-29 stsp
459 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
460 4558fcd4 2018-01-14 stsp if (*blob == NULL)
461 0a585a0d 2018-03-17 stsp return got_error_from_errno();
462 68482ea3 2017-11-27 stsp
463 55da3778 2018-09-10 stsp outfd = got_opentempfd();
464 55da3778 2018-09-10 stsp if (outfd == -1)
465 55da3778 2018-09-10 stsp return got_error_from_errno();
466 55da3778 2018-09-10 stsp
467 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
468 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
469 15c8b0e6 2018-04-24 stsp err = got_error_from_errno();
470 c7254d79 2018-04-24 stsp goto done;
471 15c8b0e6 2018-04-24 stsp }
472 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
473 55da3778 2018-09-10 stsp struct got_pack *pack;
474 55da3778 2018-09-10 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
475 55da3778 2018-09-10 stsp if (pack == NULL) {
476 55da3778 2018-09-10 stsp err = got_repo_cache_pack(&pack, repo,
477 55da3778 2018-09-10 stsp obj->path_packfile, NULL);
478 55da3778 2018-09-10 stsp if (err)
479 55da3778 2018-09-10 stsp goto done;
480 55da3778 2018-09-10 stsp }
481 55da3778 2018-09-10 stsp err = got_object_read_packed_blob_privsep(&size, outfd,
482 55da3778 2018-09-10 stsp obj, pack);
483 c7254d79 2018-04-24 stsp if (err)
484 c7254d79 2018-04-24 stsp goto done;
485 55da3778 2018-09-10 stsp obj->size = size;
486 eb651edf 2018-02-11 stsp } else {
487 55da3778 2018-09-10 stsp int infd;
488 c7254d79 2018-04-24 stsp
489 3aca5731 2018-04-24 stsp err = open_loose_object(&infd, obj, repo);
490 c7254d79 2018-04-24 stsp if (err)
491 c7254d79 2018-04-24 stsp goto done;
492 c7254d79 2018-04-24 stsp
493 ad242220 2018-09-08 stsp err = got_object_read_blob_privsep(&size, outfd, infd, repo);
494 3aca5731 2018-04-24 stsp close(infd);
495 57efb1af 2018-04-24 stsp if (err)
496 c7254d79 2018-04-24 stsp goto done;
497 3aca5731 2018-04-24 stsp
498 2967a784 2018-04-24 stsp if (size != obj->hdrlen + obj->size) {
499 1a6b3ab7 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
500 2967a784 2018-04-24 stsp goto done;
501 2967a784 2018-04-24 stsp }
502 55da3778 2018-09-10 stsp }
503 2967a784 2018-04-24 stsp
504 55da3778 2018-09-10 stsp if (fstat(outfd, &sb) == -1) {
505 55da3778 2018-09-10 stsp err = got_error_from_errno();
506 55da3778 2018-09-10 stsp goto done;
507 55da3778 2018-09-10 stsp }
508 2967a784 2018-04-24 stsp
509 55da3778 2018-09-10 stsp if (sb.st_size != obj->hdrlen + obj->size) {
510 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
511 55da3778 2018-09-10 stsp goto done;
512 55da3778 2018-09-10 stsp }
513 2967a784 2018-04-24 stsp
514 55da3778 2018-09-10 stsp (*blob)->f = fdopen(outfd, "rb");
515 55da3778 2018-09-10 stsp if ((*blob)->f == NULL) {
516 55da3778 2018-09-10 stsp err = got_error_from_errno();
517 55da3778 2018-09-10 stsp close(outfd);
518 55da3778 2018-09-10 stsp goto done;
519 68482ea3 2017-11-27 stsp }
520 68482ea3 2017-11-27 stsp
521 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
522 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
523 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
524 7d283eee 2017-11-29 stsp
525 c7254d79 2018-04-24 stsp done:
526 55da3778 2018-09-10 stsp if (err) {
527 55da3778 2018-09-10 stsp if (*blob) {
528 55da3778 2018-09-10 stsp if ((*blob)->f)
529 55da3778 2018-09-10 stsp fclose((*blob)->f);
530 55da3778 2018-09-10 stsp free((*blob)->read_buf);
531 55da3778 2018-09-10 stsp free(*blob);
532 55da3778 2018-09-10 stsp *blob = NULL;
533 55da3778 2018-09-10 stsp } else if (outfd != -1)
534 55da3778 2018-09-10 stsp close(outfd);
535 a19581a2 2018-06-21 stsp }
536 a19581a2 2018-06-21 stsp return err;
537 a19581a2 2018-06-21 stsp }
538 a19581a2 2018-06-21 stsp
539 a19581a2 2018-06-21 stsp const struct got_error *
540 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
541 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
542 a19581a2 2018-06-21 stsp size_t blocksize)
543 a19581a2 2018-06-21 stsp {
544 a19581a2 2018-06-21 stsp const struct got_error *err;
545 a19581a2 2018-06-21 stsp struct got_object *obj;
546 835e0dbd 2018-06-21 stsp
547 835e0dbd 2018-06-21 stsp *blob = NULL;
548 a19581a2 2018-06-21 stsp
549 a19581a2 2018-06-21 stsp err = got_object_open(&obj, repo, id);
550 a19581a2 2018-06-21 stsp if (err)
551 a19581a2 2018-06-21 stsp return err;
552 a19581a2 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
553 a19581a2 2018-06-21 stsp err = got_error(GOT_ERR_OBJ_TYPE);
554 a19581a2 2018-06-21 stsp goto done;
555 c7254d79 2018-04-24 stsp }
556 a19581a2 2018-06-21 stsp
557 a19581a2 2018-06-21 stsp err = got_object_blob_open(blob, repo, obj, blocksize);
558 a19581a2 2018-06-21 stsp done:
559 a19581a2 2018-06-21 stsp got_object_close(obj);
560 68482ea3 2017-11-27 stsp return err;
561 0ffeb3c2 2017-11-26 stsp }
562 68482ea3 2017-11-27 stsp
563 68482ea3 2017-11-27 stsp void
564 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
565 68482ea3 2017-11-27 stsp {
566 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
567 68482ea3 2017-11-27 stsp fclose(blob->f);
568 68482ea3 2017-11-27 stsp free(blob);
569 f934cf2c 2018-02-12 stsp }
570 f934cf2c 2018-02-12 stsp
571 f934cf2c 2018-02-12 stsp char *
572 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
573 f934cf2c 2018-02-12 stsp {
574 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
575 f934cf2c 2018-02-12 stsp }
576 f934cf2c 2018-02-12 stsp
577 f934cf2c 2018-02-12 stsp size_t
578 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
579 f934cf2c 2018-02-12 stsp {
580 f934cf2c 2018-02-12 stsp return blob->hdrlen;
581 68482ea3 2017-11-27 stsp }
582 68482ea3 2017-11-27 stsp
583 f934cf2c 2018-02-12 stsp const uint8_t *
584 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
585 f934cf2c 2018-02-12 stsp {
586 f934cf2c 2018-02-12 stsp return blob->read_buf;
587 f934cf2c 2018-02-12 stsp }
588 f934cf2c 2018-02-12 stsp
589 68482ea3 2017-11-27 stsp const struct got_error *
590 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
591 68482ea3 2017-11-27 stsp {
592 eb651edf 2018-02-11 stsp size_t n;
593 eb651edf 2018-02-11 stsp
594 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
595 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
596 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
597 eb651edf 2018-02-11 stsp *outlenp = n;
598 35e9ba5d 2018-06-21 stsp return NULL;
599 35e9ba5d 2018-06-21 stsp }
600 35e9ba5d 2018-06-21 stsp
601 35e9ba5d 2018-06-21 stsp const struct got_error *
602 84451b3e 2018-07-10 stsp got_object_blob_dump_to_file(size_t *total_len, size_t *nlines,
603 84451b3e 2018-07-10 stsp FILE *outfile, struct got_blob_object *blob)
604 35e9ba5d 2018-06-21 stsp {
605 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
606 35e9ba5d 2018-06-21 stsp size_t len, hdrlen;
607 84451b3e 2018-07-10 stsp const uint8_t *buf;
608 84451b3e 2018-07-10 stsp int i;
609 84451b3e 2018-07-10 stsp
610 84451b3e 2018-07-10 stsp if (total_len)
611 84451b3e 2018-07-10 stsp *total_len = 0;
612 84451b3e 2018-07-10 stsp if (nlines)
613 84451b3e 2018-07-10 stsp *nlines = 0;
614 35e9ba5d 2018-06-21 stsp
615 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
616 35e9ba5d 2018-06-21 stsp do {
617 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
618 35e9ba5d 2018-06-21 stsp if (err)
619 35e9ba5d 2018-06-21 stsp return err;
620 35e9ba5d 2018-06-21 stsp if (len == 0)
621 35e9ba5d 2018-06-21 stsp break;
622 84451b3e 2018-07-10 stsp if (total_len)
623 84451b3e 2018-07-10 stsp *total_len += len;
624 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
625 84451b3e 2018-07-10 stsp if (nlines) {
626 84451b3e 2018-07-10 stsp for (i = 0; i < len; i++) {
627 84451b3e 2018-07-10 stsp if (buf[i] == '\n')
628 84451b3e 2018-07-10 stsp (*nlines)++;
629 84451b3e 2018-07-10 stsp }
630 84451b3e 2018-07-10 stsp }
631 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
632 84451b3e 2018-07-10 stsp fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
633 35e9ba5d 2018-06-21 stsp hdrlen = 0;
634 35e9ba5d 2018-06-21 stsp } while (len != 0);
635 35e9ba5d 2018-06-21 stsp
636 35e9ba5d 2018-06-21 stsp fflush(outfile);
637 35e9ba5d 2018-06-21 stsp rewind(outfile);
638 35e9ba5d 2018-06-21 stsp
639 776d4d29 2018-06-17 stsp return NULL;
640 776d4d29 2018-06-17 stsp }
641 776d4d29 2018-06-17 stsp
642 776d4d29 2018-06-17 stsp static struct got_tree_entry *
643 776d4d29 2018-06-17 stsp find_entry_by_name(struct got_tree_object *tree, const char *name)
644 776d4d29 2018-06-17 stsp {
645 776d4d29 2018-06-17 stsp struct got_tree_entry *te;
646 776d4d29 2018-06-17 stsp
647 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
648 776d4d29 2018-06-17 stsp if (strcmp(te->name, name) == 0)
649 776d4d29 2018-06-17 stsp return te;
650 776d4d29 2018-06-17 stsp }
651 eb651edf 2018-02-11 stsp return NULL;
652 776d4d29 2018-06-17 stsp }
653 776d4d29 2018-06-17 stsp
654 776d4d29 2018-06-17 stsp const struct got_error *
655 776d4d29 2018-06-17 stsp got_object_open_by_path(struct got_object **obj, struct got_repository *repo,
656 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
657 776d4d29 2018-06-17 stsp {
658 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
659 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
660 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
661 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
662 197aa481 2018-06-21 stsp char *seg, *s, *s0 = NULL;
663 67606321 2018-06-21 stsp size_t len = strlen(path);
664 776d4d29 2018-06-17 stsp
665 776d4d29 2018-06-17 stsp *obj = NULL;
666 776d4d29 2018-06-17 stsp
667 776d4d29 2018-06-17 stsp /* We are expecting an absolute in-repository path. */
668 776d4d29 2018-06-17 stsp if (path[0] != '/')
669 776d4d29 2018-06-17 stsp return got_error(GOT_ERR_NOT_ABSPATH);
670 776d4d29 2018-06-17 stsp
671 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
672 776d4d29 2018-06-17 stsp if (err)
673 776d4d29 2018-06-17 stsp goto done;
674 776d4d29 2018-06-17 stsp
675 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
676 776d4d29 2018-06-17 stsp if (path[1] == '\0') {
677 776d4d29 2018-06-17 stsp err = got_object_open(obj, repo, commit->tree_id);
678 ca008b32 2018-07-22 stsp goto done;
679 776d4d29 2018-06-17 stsp }
680 776d4d29 2018-06-17 stsp
681 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
682 776d4d29 2018-06-17 stsp if (err)
683 776d4d29 2018-06-17 stsp goto done;
684 776d4d29 2018-06-17 stsp
685 197aa481 2018-06-21 stsp s0 = strdup(path);
686 197aa481 2018-06-21 stsp if (s0 == NULL) {
687 776d4d29 2018-06-17 stsp err = got_error_from_errno();
688 776d4d29 2018-06-17 stsp goto done;
689 776d4d29 2018-06-17 stsp }
690 67606321 2018-06-21 stsp err = got_canonpath(path, s0, len + 1);
691 776d4d29 2018-06-17 stsp if (err)
692 776d4d29 2018-06-17 stsp goto done;
693 776d4d29 2018-06-17 stsp
694 197aa481 2018-06-21 stsp s = s0;
695 776d4d29 2018-06-17 stsp s++; /* skip leading '/' */
696 67606321 2018-06-21 stsp len--;
697 776d4d29 2018-06-17 stsp seg = s;
698 67606321 2018-06-21 stsp while (len > 0) {
699 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
700 776d4d29 2018-06-17 stsp
701 776d4d29 2018-06-17 stsp if (*s != '/') {
702 776d4d29 2018-06-17 stsp s++;
703 67606321 2018-06-21 stsp len--;
704 00530cfb 2018-06-21 stsp if (*s)
705 00530cfb 2018-06-21 stsp continue;
706 776d4d29 2018-06-17 stsp }
707 776d4d29 2018-06-17 stsp
708 776d4d29 2018-06-17 stsp /* end of path segment */
709 776d4d29 2018-06-17 stsp *s = '\0';
710 776d4d29 2018-06-17 stsp
711 db37e2c0 2018-06-21 stsp te = find_entry_by_name(tree, seg);
712 db37e2c0 2018-06-21 stsp if (te == NULL) {
713 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_NO_OBJ);
714 776d4d29 2018-06-17 stsp goto done;
715 776d4d29 2018-06-17 stsp }
716 776d4d29 2018-06-17 stsp
717 67606321 2018-06-21 stsp if (len == 0)
718 67606321 2018-06-21 stsp break;
719 67606321 2018-06-21 stsp
720 776d4d29 2018-06-17 stsp seg = s + 1;
721 776d4d29 2018-06-17 stsp s++;
722 67606321 2018-06-21 stsp len--;
723 776d4d29 2018-06-17 stsp if (*s) {
724 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
725 db37e2c0 2018-06-21 stsp te->id);
726 db37e2c0 2018-06-21 stsp te = NULL;
727 776d4d29 2018-06-17 stsp if (err)
728 776d4d29 2018-06-17 stsp goto done;
729 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
730 776d4d29 2018-06-17 stsp tree = next_tree;
731 776d4d29 2018-06-17 stsp }
732 776d4d29 2018-06-17 stsp }
733 776d4d29 2018-06-17 stsp
734 ca008b32 2018-07-22 stsp if (te)
735 db37e2c0 2018-06-21 stsp err = got_object_open(obj, repo, te->id);
736 ca008b32 2018-07-22 stsp else
737 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_NO_OBJ);
738 776d4d29 2018-06-17 stsp done:
739 197aa481 2018-06-21 stsp free(s0);
740 776d4d29 2018-06-17 stsp if (commit)
741 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
742 776d4d29 2018-06-17 stsp if (tree)
743 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
744 776d4d29 2018-06-17 stsp return err;
745 68482ea3 2017-11-27 stsp }