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