Blame


1 d71d75ad 2017-11-05 stsp /*
2 a1fd68d8 2018-01-12 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 d71d75ad 2017-11-05 stsp *
4 d71d75ad 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 d71d75ad 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 d71d75ad 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
7 d71d75ad 2017-11-05 stsp *
8 d71d75ad 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 d71d75ad 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 d71d75ad 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 d71d75ad 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 d71d75ad 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 d71d75ad 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 d71d75ad 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 d71d75ad 2017-11-05 stsp */
16 d71d75ad 2017-11-05 stsp
17 2178c42e 2018-04-22 stsp #include <sys/types.h>
18 0ffeb3c2 2017-11-26 stsp #include <sys/stat.h>
19 d1cda826 2017-11-06 stsp #include <sys/queue.h>
20 2178c42e 2018-04-22 stsp #include <sys/uio.h>
21 2178c42e 2018-04-22 stsp #include <sys/socket.h>
22 2178c42e 2018-04-22 stsp #include <sys/wait.h>
23 d1cda826 2017-11-06 stsp
24 a1fd68d8 2018-01-12 stsp #include <errno.h>
25 2178c42e 2018-04-22 stsp #include <fcntl.h>
26 d71d75ad 2017-11-05 stsp #include <stdio.h>
27 ab9a70b2 2017-11-06 stsp #include <stdlib.h>
28 ab9a70b2 2017-11-06 stsp #include <string.h>
29 2178c42e 2018-04-22 stsp #include <stdint.h>
30 d71d75ad 2017-11-05 stsp #include <sha1.h>
31 ab9a70b2 2017-11-06 stsp #include <zlib.h>
32 ab9a70b2 2017-11-06 stsp #include <ctype.h>
33 ab9a70b2 2017-11-06 stsp #include <limits.h>
34 2178c42e 2018-04-22 stsp #include <imsg.h>
35 788c352e 2018-06-16 stsp #include <time.h>
36 d71d75ad 2017-11-05 stsp
37 ab9a70b2 2017-11-06 stsp #include "got_error.h"
38 d71d75ad 2017-11-05 stsp #include "got_object.h"
39 ab9a70b2 2017-11-06 stsp #include "got_repository.h"
40 511a516b 2018-05-19 stsp #include "got_opentemp.h"
41 d71d75ad 2017-11-05 stsp
42 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
43 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
44 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
45 2178c42e 2018-04-22 stsp #include "got_lib_path.h"
46 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
48 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
49 ad242220 2018-09-08 stsp #include "got_lib_object_parse.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 2090a03d 2018-09-09 stsp return err;
165 2090a03d 2018-09-09 stsp }
166 2090a03d 2018-09-09 stsp
167 2090a03d 2018-09-09 stsp static const struct got_error *
168 2090a03d 2018-09-09 stsp get_packfile_path(char **path_packfile, struct got_packidx *packidx)
169 2090a03d 2018-09-09 stsp {
170 2090a03d 2018-09-09 stsp size_t size;
171 2090a03d 2018-09-09 stsp
172 2090a03d 2018-09-09 stsp /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
173 2090a03d 2018-09-09 stsp size = strlen(packidx->path_packidx) + 2;
174 2090a03d 2018-09-09 stsp if (size < GOT_PACKFILE_NAMELEN + 1)
175 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_BAD_PATH);
176 2090a03d 2018-09-09 stsp
177 2090a03d 2018-09-09 stsp *path_packfile = calloc(size, sizeof(**path_packfile));
178 2090a03d 2018-09-09 stsp if (*path_packfile == NULL)
179 2090a03d 2018-09-09 stsp return got_error_from_errno();
180 2090a03d 2018-09-09 stsp
181 2090a03d 2018-09-09 stsp /* Copy up to and excluding ".idx". */
182 2090a03d 2018-09-09 stsp if (strlcpy(*path_packfile, packidx->path_packidx,
183 2090a03d 2018-09-09 stsp size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
184 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
185 2090a03d 2018-09-09 stsp
186 2090a03d 2018-09-09 stsp if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
187 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
188 2090a03d 2018-09-09 stsp
189 2090a03d 2018-09-09 stsp return NULL;
190 2090a03d 2018-09-09 stsp }
191 2090a03d 2018-09-09 stsp
192 2090a03d 2018-09-09 stsp static const struct got_error *
193 2090a03d 2018-09-09 stsp open_packed_object(struct got_object **obj, struct got_object_id *id,
194 2090a03d 2018-09-09 stsp struct got_repository *repo)
195 2090a03d 2018-09-09 stsp {
196 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
197 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
198 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
199 2090a03d 2018-09-09 stsp int idx;
200 2090a03d 2018-09-09 stsp char *path_packfile;
201 2090a03d 2018-09-09 stsp
202 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
203 2090a03d 2018-09-09 stsp if (err)
204 2090a03d 2018-09-09 stsp return err;
205 2090a03d 2018-09-09 stsp
206 2090a03d 2018-09-09 stsp err = get_packfile_path(&path_packfile, packidx);
207 2090a03d 2018-09-09 stsp if (err)
208 2090a03d 2018-09-09 stsp return err;
209 2090a03d 2018-09-09 stsp
210 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
211 2090a03d 2018-09-09 stsp if (pack == NULL) {
212 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
213 2090a03d 2018-09-09 stsp if (err)
214 2090a03d 2018-09-09 stsp goto done;
215 2090a03d 2018-09-09 stsp }
216 2090a03d 2018-09-09 stsp
217 2090a03d 2018-09-09 stsp err = got_packfile_open_object(obj, pack, packidx, idx, id);
218 2090a03d 2018-09-09 stsp if (err)
219 2090a03d 2018-09-09 stsp goto done;
220 2090a03d 2018-09-09 stsp
221 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
222 2090a03d 2018-09-09 stsp done:
223 2090a03d 2018-09-09 stsp free(path_packfile);
224 4558fcd4 2018-01-14 stsp return err;
225 4558fcd4 2018-01-14 stsp }
226 a1fd68d8 2018-01-12 stsp
227 4558fcd4 2018-01-14 stsp const struct got_error *
228 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
229 4558fcd4 2018-01-14 stsp struct got_object_id *id)
230 4558fcd4 2018-01-14 stsp {
231 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
232 6c00b545 2018-01-17 stsp char *path;
233 2178c42e 2018-04-22 stsp int fd;
234 7bb0daa1 2018-06-21 stsp
235 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
236 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
237 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
238 7bb0daa1 2018-06-21 stsp return NULL;
239 7bb0daa1 2018-06-21 stsp }
240 4558fcd4 2018-01-14 stsp
241 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
242 4558fcd4 2018-01-14 stsp if (err)
243 4558fcd4 2018-01-14 stsp return err;
244 4558fcd4 2018-01-14 stsp
245 2178c42e 2018-04-22 stsp fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
246 2178c42e 2018-04-22 stsp if (fd == -1) {
247 6c00b545 2018-01-17 stsp if (errno != ENOENT) {
248 6c00b545 2018-01-17 stsp err = got_error_from_errno();
249 6c00b545 2018-01-17 stsp goto done;
250 6c00b545 2018-01-17 stsp }
251 2090a03d 2018-09-09 stsp err = open_packed_object(obj, id, repo);
252 6c00b545 2018-01-17 stsp if (err)
253 6c00b545 2018-01-17 stsp goto done;
254 6c00b545 2018-01-17 stsp if (*obj == NULL)
255 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NO_OBJ);
256 6c00b545 2018-01-17 stsp } else {
257 ad242220 2018-09-08 stsp err = got_object_read_header_privsep(obj, repo, fd);
258 6c00b545 2018-01-17 stsp if (err)
259 6c00b545 2018-01-17 stsp goto done;
260 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
261 6c00b545 2018-01-17 stsp }
262 7bb0daa1 2018-06-21 stsp
263 7bb0daa1 2018-06-21 stsp if (err == NULL) {
264 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
265 7bb0daa1 2018-06-21 stsp err = got_repo_cache_object(repo, id, *obj);
266 7bb0daa1 2018-06-21 stsp }
267 6c00b545 2018-01-17 stsp done:
268 6c00b545 2018-01-17 stsp free(path);
269 2178c42e 2018-04-22 stsp if (fd != -1)
270 2178c42e 2018-04-22 stsp close(fd);
271 ab9a70b2 2017-11-06 stsp return err;
272 6c00b545 2018-01-17 stsp
273 ab9a70b2 2017-11-06 stsp }
274 ab9a70b2 2017-11-06 stsp
275 6dfa2fd3 2018-02-12 stsp const struct got_error *
276 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
277 6dfa2fd3 2018-02-12 stsp const char *id_str)
278 6dfa2fd3 2018-02-12 stsp {
279 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
280 6dfa2fd3 2018-02-12 stsp
281 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
282 6dfa2fd3 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
283 6dfa2fd3 2018-02-12 stsp
284 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
285 bff6ca00 2018-04-23 stsp }
286 bff6ca00 2018-04-23 stsp
287 bff6ca00 2018-04-23 stsp const struct got_error *
288 be6a1b5a 2018-06-11 stsp got_object_open_as_commit(struct got_commit_object **commit,
289 be6a1b5a 2018-06-11 stsp struct got_repository *repo, struct got_object_id *id)
290 be6a1b5a 2018-06-11 stsp {
291 be6a1b5a 2018-06-11 stsp const struct got_error *err;
292 be6a1b5a 2018-06-11 stsp struct got_object *obj;
293 835e0dbd 2018-06-21 stsp
294 835e0dbd 2018-06-21 stsp *commit = NULL;
295 be6a1b5a 2018-06-11 stsp
296 be6a1b5a 2018-06-11 stsp err = got_object_open(&obj, repo, id);
297 be6a1b5a 2018-06-11 stsp if (err)
298 be6a1b5a 2018-06-11 stsp return err;
299 be6a1b5a 2018-06-11 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
300 be6a1b5a 2018-06-11 stsp err = got_error(GOT_ERR_OBJ_TYPE);
301 be6a1b5a 2018-06-11 stsp goto done;
302 be6a1b5a 2018-06-11 stsp }
303 be6a1b5a 2018-06-11 stsp
304 be6a1b5a 2018-06-11 stsp err = got_object_commit_open(commit, repo, obj);
305 be6a1b5a 2018-06-11 stsp done:
306 be6a1b5a 2018-06-11 stsp got_object_close(obj);
307 be6a1b5a 2018-06-11 stsp return err;
308 be6a1b5a 2018-06-11 stsp }
309 be6a1b5a 2018-06-11 stsp
310 be6a1b5a 2018-06-11 stsp const struct got_error *
311 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
312 dbc6a6b6 2018-07-12 stsp {
313 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
314 dbc6a6b6 2018-07-12 stsp
315 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
316 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
317 dbc6a6b6 2018-07-12 stsp return got_error_from_errno();
318 dbc6a6b6 2018-07-12 stsp
319 dbc6a6b6 2018-07-12 stsp (*qid)->id = got_object_id_dup(id);
320 dbc6a6b6 2018-07-12 stsp if ((*qid)->id == NULL) {
321 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
322 fa2f6902 2018-07-23 stsp got_object_qid_free(*qid);
323 dbc6a6b6 2018-07-12 stsp *qid = NULL;
324 dbc6a6b6 2018-07-12 stsp return err;
325 dbc6a6b6 2018-07-12 stsp }
326 dbc6a6b6 2018-07-12 stsp
327 dbc6a6b6 2018-07-12 stsp return NULL;
328 dbc6a6b6 2018-07-12 stsp }
329 dbc6a6b6 2018-07-12 stsp
330 d1cda826 2017-11-06 stsp const struct got_error *
331 d1cda826 2017-11-06 stsp got_object_commit_open(struct got_commit_object **commit,
332 d1cda826 2017-11-06 stsp struct got_repository *repo, struct got_object *obj)
333 d1cda826 2017-11-06 stsp {
334 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
335 1943de01 2018-06-22 stsp
336 1943de01 2018-06-22 stsp *commit = got_repo_get_cached_commit(repo, &obj->id);
337 1943de01 2018-06-22 stsp if (*commit != NULL) {
338 1943de01 2018-06-22 stsp (*commit)->refcnt++;
339 1943de01 2018-06-22 stsp return NULL;
340 1943de01 2018-06-22 stsp }
341 d1cda826 2017-11-06 stsp
342 d1cda826 2017-11-06 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
343 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_OBJ_TYPE);
344 d1cda826 2017-11-06 stsp
345 ea35256b 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
346 ea35256b 2018-03-16 stsp uint8_t *buf;
347 ea35256b 2018-03-16 stsp size_t len;
348 ea35256b 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
349 ea35256b 2018-03-16 stsp if (err)
350 ea35256b 2018-03-16 stsp return err;
351 b29656e2 2018-03-16 stsp obj->size = len;
352 a440fac0 2018-09-06 stsp err = got_object_parse_commit(commit, buf, len);
353 ea35256b 2018-03-16 stsp free(buf);
354 ea35256b 2018-03-16 stsp } else {
355 d5003b79 2018-04-22 stsp int fd;
356 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
357 ea35256b 2018-03-16 stsp if (err)
358 ea35256b 2018-03-16 stsp return err;
359 ad242220 2018-09-08 stsp err = got_object_read_commit_privsep(commit, obj, fd, repo);
360 bff6ca00 2018-04-23 stsp close(fd);
361 ea35256b 2018-03-16 stsp }
362 1943de01 2018-06-22 stsp
363 1943de01 2018-06-22 stsp if (err == NULL) {
364 1943de01 2018-06-22 stsp (*commit)->refcnt++;
365 1943de01 2018-06-22 stsp err = got_repo_cache_commit(repo, &obj->id, *commit);
366 1943de01 2018-06-22 stsp }
367 1943de01 2018-06-22 stsp
368 d1cda826 2017-11-06 stsp return err;
369 d1cda826 2017-11-06 stsp }
370 d1cda826 2017-11-06 stsp
371 0ffeb3c2 2017-11-26 stsp const struct got_error *
372 0ffeb3c2 2017-11-26 stsp got_object_tree_open(struct got_tree_object **tree,
373 0ffeb3c2 2017-11-26 stsp struct got_repository *repo, struct got_object *obj)
374 0ffeb3c2 2017-11-26 stsp {
375 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
376 f6be5c30 2018-06-22 stsp
377 f6be5c30 2018-06-22 stsp *tree = got_repo_get_cached_tree(repo, &obj->id);
378 f6be5c30 2018-06-22 stsp if (*tree != NULL) {
379 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
380 f6be5c30 2018-06-22 stsp return NULL;
381 f6be5c30 2018-06-22 stsp }
382 0ffeb3c2 2017-11-26 stsp
383 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
384 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
385 0ffeb3c2 2017-11-26 stsp
386 e0ab43e7 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
387 e0ab43e7 2018-03-16 stsp uint8_t *buf;
388 e0ab43e7 2018-03-16 stsp size_t len;
389 e0ab43e7 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
390 e0ab43e7 2018-03-16 stsp if (err)
391 e0ab43e7 2018-03-16 stsp return err;
392 b29656e2 2018-03-16 stsp obj->size = len;
393 a440fac0 2018-09-06 stsp err = got_object_parse_tree(tree, buf, len);
394 e0ab43e7 2018-03-16 stsp free(buf);
395 e0ab43e7 2018-03-16 stsp } else {
396 d5003b79 2018-04-22 stsp int fd;
397 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
398 e0ab43e7 2018-03-16 stsp if (err)
399 e0ab43e7 2018-03-16 stsp return err;
400 ad242220 2018-09-08 stsp err = got_object_read_tree_privsep(tree, obj, fd, repo);
401 d5003b79 2018-04-22 stsp close(fd);
402 776d4d29 2018-06-17 stsp }
403 f6be5c30 2018-06-22 stsp
404 f6be5c30 2018-06-22 stsp if (err == NULL) {
405 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
406 f6be5c30 2018-06-22 stsp err = got_repo_cache_tree(repo, &obj->id, *tree);
407 f6be5c30 2018-06-22 stsp }
408 f6be5c30 2018-06-22 stsp
409 776d4d29 2018-06-17 stsp return err;
410 776d4d29 2018-06-17 stsp }
411 776d4d29 2018-06-17 stsp
412 776d4d29 2018-06-17 stsp const struct got_error *
413 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
414 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
415 776d4d29 2018-06-17 stsp {
416 776d4d29 2018-06-17 stsp const struct got_error *err;
417 776d4d29 2018-06-17 stsp struct got_object *obj;
418 835e0dbd 2018-06-21 stsp
419 835e0dbd 2018-06-21 stsp *tree = NULL;
420 776d4d29 2018-06-17 stsp
421 776d4d29 2018-06-17 stsp err = got_object_open(&obj, repo, id);
422 776d4d29 2018-06-17 stsp if (err)
423 776d4d29 2018-06-17 stsp return err;
424 1cbc02b6 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
425 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_OBJ_TYPE);
426 776d4d29 2018-06-17 stsp goto done;
427 e0ab43e7 2018-03-16 stsp }
428 776d4d29 2018-06-17 stsp
429 776d4d29 2018-06-17 stsp err = got_object_tree_open(tree, repo, obj);
430 776d4d29 2018-06-17 stsp done:
431 776d4d29 2018-06-17 stsp got_object_close(obj);
432 0ffeb3c2 2017-11-26 stsp return err;
433 57efb1af 2018-04-24 stsp }
434 ff6b18f8 2018-04-24 stsp
435 883f0469 2018-06-23 stsp const struct got_tree_entries *
436 883f0469 2018-06-23 stsp got_object_tree_get_entries(struct got_tree_object *tree)
437 883f0469 2018-06-23 stsp {
438 883f0469 2018-06-23 stsp return &tree->entries;
439 883f0469 2018-06-23 stsp }
440 883f0469 2018-06-23 stsp
441 68482ea3 2017-11-27 stsp const struct got_error *
442 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
443 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
444 68482ea3 2017-11-27 stsp {
445 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
446 68482ea3 2017-11-27 stsp
447 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
448 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
449 68482ea3 2017-11-27 stsp
450 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
451 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
452 7d283eee 2017-11-29 stsp
453 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
454 4558fcd4 2018-01-14 stsp if (*blob == NULL)
455 0a585a0d 2018-03-17 stsp return got_error_from_errno();
456 68482ea3 2017-11-27 stsp
457 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
458 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
459 15c8b0e6 2018-04-24 stsp err = got_error_from_errno();
460 c7254d79 2018-04-24 stsp goto done;
461 15c8b0e6 2018-04-24 stsp }
462 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
463 eb651edf 2018-02-11 stsp err = got_packfile_extract_object(&((*blob)->f), obj, repo);
464 c7254d79 2018-04-24 stsp if (err)
465 c7254d79 2018-04-24 stsp goto done;
466 eb651edf 2018-02-11 stsp } else {
467 3aca5731 2018-04-24 stsp int infd, outfd;
468 2967a784 2018-04-24 stsp size_t size;
469 2967a784 2018-04-24 stsp struct stat sb;
470 c7254d79 2018-04-24 stsp
471 3aca5731 2018-04-24 stsp err = open_loose_object(&infd, obj, repo);
472 c7254d79 2018-04-24 stsp if (err)
473 c7254d79 2018-04-24 stsp goto done;
474 c7254d79 2018-04-24 stsp
475 3aca5731 2018-04-24 stsp
476 3aca5731 2018-04-24 stsp outfd = got_opentempfd();
477 3aca5731 2018-04-24 stsp if (outfd == -1) {
478 3aca5731 2018-04-24 stsp err = got_error_from_errno();
479 3aca5731 2018-04-24 stsp close(infd);
480 3aca5731 2018-04-24 stsp goto done;
481 3aca5731 2018-04-24 stsp }
482 3aca5731 2018-04-24 stsp
483 ad242220 2018-09-08 stsp err = got_object_read_blob_privsep(&size, outfd, infd, repo);
484 3aca5731 2018-04-24 stsp close(infd);
485 57efb1af 2018-04-24 stsp if (err)
486 c7254d79 2018-04-24 stsp goto done;
487 3aca5731 2018-04-24 stsp
488 2967a784 2018-04-24 stsp if (size != obj->hdrlen + obj->size) {
489 1a6b3ab7 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
490 2967a784 2018-04-24 stsp close(outfd);
491 2967a784 2018-04-24 stsp goto done;
492 2967a784 2018-04-24 stsp }
493 2967a784 2018-04-24 stsp
494 2967a784 2018-04-24 stsp if (fstat(outfd, &sb) == -1) {
495 2967a784 2018-04-24 stsp err = got_error_from_errno();
496 2967a784 2018-04-24 stsp close(outfd);
497 2967a784 2018-04-24 stsp goto done;
498 2967a784 2018-04-24 stsp }
499 2967a784 2018-04-24 stsp
500 2967a784 2018-04-24 stsp if (sb.st_size != size) {
501 2967a784 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
502 2967a784 2018-04-24 stsp close(outfd);
503 2967a784 2018-04-24 stsp goto done;
504 2967a784 2018-04-24 stsp }
505 2967a784 2018-04-24 stsp
506 3aca5731 2018-04-24 stsp (*blob)->f = fdopen(outfd, "rb");
507 3aca5731 2018-04-24 stsp if ((*blob)->f == NULL) {
508 3aca5731 2018-04-24 stsp err = got_error_from_errno();
509 3aca5731 2018-04-24 stsp close(outfd);
510 3aca5731 2018-04-24 stsp goto done;
511 3aca5731 2018-04-24 stsp }
512 68482ea3 2017-11-27 stsp }
513 68482ea3 2017-11-27 stsp
514 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
515 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
516 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
517 7d283eee 2017-11-29 stsp
518 c7254d79 2018-04-24 stsp done:
519 c7254d79 2018-04-24 stsp if (err && *blob) {
520 c7254d79 2018-04-24 stsp if ((*blob)->f)
521 c7254d79 2018-04-24 stsp fclose((*blob)->f);
522 c7254d79 2018-04-24 stsp free((*blob)->read_buf);
523 c7254d79 2018-04-24 stsp free(*blob);
524 c7254d79 2018-04-24 stsp *blob = NULL;
525 a19581a2 2018-06-21 stsp }
526 a19581a2 2018-06-21 stsp return err;
527 a19581a2 2018-06-21 stsp }
528 a19581a2 2018-06-21 stsp
529 a19581a2 2018-06-21 stsp const struct got_error *
530 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
531 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
532 a19581a2 2018-06-21 stsp size_t blocksize)
533 a19581a2 2018-06-21 stsp {
534 a19581a2 2018-06-21 stsp const struct got_error *err;
535 a19581a2 2018-06-21 stsp struct got_object *obj;
536 835e0dbd 2018-06-21 stsp
537 835e0dbd 2018-06-21 stsp *blob = NULL;
538 a19581a2 2018-06-21 stsp
539 a19581a2 2018-06-21 stsp err = got_object_open(&obj, repo, id);
540 a19581a2 2018-06-21 stsp if (err)
541 a19581a2 2018-06-21 stsp return err;
542 a19581a2 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
543 a19581a2 2018-06-21 stsp err = got_error(GOT_ERR_OBJ_TYPE);
544 a19581a2 2018-06-21 stsp goto done;
545 c7254d79 2018-04-24 stsp }
546 a19581a2 2018-06-21 stsp
547 a19581a2 2018-06-21 stsp err = got_object_blob_open(blob, repo, obj, blocksize);
548 a19581a2 2018-06-21 stsp done:
549 a19581a2 2018-06-21 stsp got_object_close(obj);
550 68482ea3 2017-11-27 stsp return err;
551 0ffeb3c2 2017-11-26 stsp }
552 68482ea3 2017-11-27 stsp
553 68482ea3 2017-11-27 stsp void
554 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
555 68482ea3 2017-11-27 stsp {
556 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
557 68482ea3 2017-11-27 stsp fclose(blob->f);
558 68482ea3 2017-11-27 stsp free(blob);
559 f934cf2c 2018-02-12 stsp }
560 f934cf2c 2018-02-12 stsp
561 f934cf2c 2018-02-12 stsp char *
562 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
563 f934cf2c 2018-02-12 stsp {
564 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
565 f934cf2c 2018-02-12 stsp }
566 f934cf2c 2018-02-12 stsp
567 f934cf2c 2018-02-12 stsp size_t
568 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
569 f934cf2c 2018-02-12 stsp {
570 f934cf2c 2018-02-12 stsp return blob->hdrlen;
571 68482ea3 2017-11-27 stsp }
572 68482ea3 2017-11-27 stsp
573 f934cf2c 2018-02-12 stsp const uint8_t *
574 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
575 f934cf2c 2018-02-12 stsp {
576 f934cf2c 2018-02-12 stsp return blob->read_buf;
577 f934cf2c 2018-02-12 stsp }
578 f934cf2c 2018-02-12 stsp
579 68482ea3 2017-11-27 stsp const struct got_error *
580 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
581 68482ea3 2017-11-27 stsp {
582 eb651edf 2018-02-11 stsp size_t n;
583 eb651edf 2018-02-11 stsp
584 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
585 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
586 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
587 eb651edf 2018-02-11 stsp *outlenp = n;
588 35e9ba5d 2018-06-21 stsp return NULL;
589 35e9ba5d 2018-06-21 stsp }
590 35e9ba5d 2018-06-21 stsp
591 35e9ba5d 2018-06-21 stsp const struct got_error *
592 84451b3e 2018-07-10 stsp got_object_blob_dump_to_file(size_t *total_len, size_t *nlines,
593 84451b3e 2018-07-10 stsp FILE *outfile, struct got_blob_object *blob)
594 35e9ba5d 2018-06-21 stsp {
595 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
596 35e9ba5d 2018-06-21 stsp size_t len, hdrlen;
597 84451b3e 2018-07-10 stsp const uint8_t *buf;
598 84451b3e 2018-07-10 stsp int i;
599 84451b3e 2018-07-10 stsp
600 84451b3e 2018-07-10 stsp if (total_len)
601 84451b3e 2018-07-10 stsp *total_len = 0;
602 84451b3e 2018-07-10 stsp if (nlines)
603 84451b3e 2018-07-10 stsp *nlines = 0;
604 35e9ba5d 2018-06-21 stsp
605 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
606 35e9ba5d 2018-06-21 stsp do {
607 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
608 35e9ba5d 2018-06-21 stsp if (err)
609 35e9ba5d 2018-06-21 stsp return err;
610 35e9ba5d 2018-06-21 stsp if (len == 0)
611 35e9ba5d 2018-06-21 stsp break;
612 84451b3e 2018-07-10 stsp if (total_len)
613 84451b3e 2018-07-10 stsp *total_len += len;
614 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
615 84451b3e 2018-07-10 stsp if (nlines) {
616 84451b3e 2018-07-10 stsp for (i = 0; i < len; i++) {
617 84451b3e 2018-07-10 stsp if (buf[i] == '\n')
618 84451b3e 2018-07-10 stsp (*nlines)++;
619 84451b3e 2018-07-10 stsp }
620 84451b3e 2018-07-10 stsp }
621 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
622 84451b3e 2018-07-10 stsp fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
623 35e9ba5d 2018-06-21 stsp hdrlen = 0;
624 35e9ba5d 2018-06-21 stsp } while (len != 0);
625 35e9ba5d 2018-06-21 stsp
626 35e9ba5d 2018-06-21 stsp fflush(outfile);
627 35e9ba5d 2018-06-21 stsp rewind(outfile);
628 35e9ba5d 2018-06-21 stsp
629 776d4d29 2018-06-17 stsp return NULL;
630 776d4d29 2018-06-17 stsp }
631 776d4d29 2018-06-17 stsp
632 776d4d29 2018-06-17 stsp static struct got_tree_entry *
633 776d4d29 2018-06-17 stsp find_entry_by_name(struct got_tree_object *tree, const char *name)
634 776d4d29 2018-06-17 stsp {
635 776d4d29 2018-06-17 stsp struct got_tree_entry *te;
636 776d4d29 2018-06-17 stsp
637 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
638 776d4d29 2018-06-17 stsp if (strcmp(te->name, name) == 0)
639 776d4d29 2018-06-17 stsp return te;
640 776d4d29 2018-06-17 stsp }
641 eb651edf 2018-02-11 stsp return NULL;
642 776d4d29 2018-06-17 stsp }
643 776d4d29 2018-06-17 stsp
644 776d4d29 2018-06-17 stsp const struct got_error *
645 776d4d29 2018-06-17 stsp got_object_open_by_path(struct got_object **obj, struct got_repository *repo,
646 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
647 776d4d29 2018-06-17 stsp {
648 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
649 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
650 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
651 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
652 197aa481 2018-06-21 stsp char *seg, *s, *s0 = NULL;
653 67606321 2018-06-21 stsp size_t len = strlen(path);
654 776d4d29 2018-06-17 stsp
655 776d4d29 2018-06-17 stsp *obj = NULL;
656 776d4d29 2018-06-17 stsp
657 776d4d29 2018-06-17 stsp /* We are expecting an absolute in-repository path. */
658 776d4d29 2018-06-17 stsp if (path[0] != '/')
659 776d4d29 2018-06-17 stsp return got_error(GOT_ERR_NOT_ABSPATH);
660 776d4d29 2018-06-17 stsp
661 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
662 776d4d29 2018-06-17 stsp if (err)
663 776d4d29 2018-06-17 stsp goto done;
664 776d4d29 2018-06-17 stsp
665 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
666 776d4d29 2018-06-17 stsp if (path[1] == '\0') {
667 776d4d29 2018-06-17 stsp err = got_object_open(obj, repo, commit->tree_id);
668 ca008b32 2018-07-22 stsp goto done;
669 776d4d29 2018-06-17 stsp }
670 776d4d29 2018-06-17 stsp
671 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
672 776d4d29 2018-06-17 stsp if (err)
673 776d4d29 2018-06-17 stsp goto done;
674 776d4d29 2018-06-17 stsp
675 197aa481 2018-06-21 stsp s0 = strdup(path);
676 197aa481 2018-06-21 stsp if (s0 == NULL) {
677 776d4d29 2018-06-17 stsp err = got_error_from_errno();
678 776d4d29 2018-06-17 stsp goto done;
679 776d4d29 2018-06-17 stsp }
680 67606321 2018-06-21 stsp err = got_canonpath(path, s0, len + 1);
681 776d4d29 2018-06-17 stsp if (err)
682 776d4d29 2018-06-17 stsp goto done;
683 776d4d29 2018-06-17 stsp
684 197aa481 2018-06-21 stsp s = s0;
685 776d4d29 2018-06-17 stsp s++; /* skip leading '/' */
686 67606321 2018-06-21 stsp len--;
687 776d4d29 2018-06-17 stsp seg = s;
688 67606321 2018-06-21 stsp while (len > 0) {
689 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
690 776d4d29 2018-06-17 stsp
691 776d4d29 2018-06-17 stsp if (*s != '/') {
692 776d4d29 2018-06-17 stsp s++;
693 67606321 2018-06-21 stsp len--;
694 00530cfb 2018-06-21 stsp if (*s)
695 00530cfb 2018-06-21 stsp continue;
696 776d4d29 2018-06-17 stsp }
697 776d4d29 2018-06-17 stsp
698 776d4d29 2018-06-17 stsp /* end of path segment */
699 776d4d29 2018-06-17 stsp *s = '\0';
700 776d4d29 2018-06-17 stsp
701 db37e2c0 2018-06-21 stsp te = find_entry_by_name(tree, seg);
702 db37e2c0 2018-06-21 stsp if (te == NULL) {
703 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_NO_OBJ);
704 776d4d29 2018-06-17 stsp goto done;
705 776d4d29 2018-06-17 stsp }
706 776d4d29 2018-06-17 stsp
707 67606321 2018-06-21 stsp if (len == 0)
708 67606321 2018-06-21 stsp break;
709 67606321 2018-06-21 stsp
710 776d4d29 2018-06-17 stsp seg = s + 1;
711 776d4d29 2018-06-17 stsp s++;
712 67606321 2018-06-21 stsp len--;
713 776d4d29 2018-06-17 stsp if (*s) {
714 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
715 db37e2c0 2018-06-21 stsp te->id);
716 db37e2c0 2018-06-21 stsp te = NULL;
717 776d4d29 2018-06-17 stsp if (err)
718 776d4d29 2018-06-17 stsp goto done;
719 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
720 776d4d29 2018-06-17 stsp tree = next_tree;
721 776d4d29 2018-06-17 stsp }
722 776d4d29 2018-06-17 stsp }
723 776d4d29 2018-06-17 stsp
724 ca008b32 2018-07-22 stsp if (te)
725 db37e2c0 2018-06-21 stsp err = got_object_open(obj, repo, te->id);
726 ca008b32 2018-07-22 stsp else
727 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_NO_OBJ);
728 776d4d29 2018-06-17 stsp done:
729 197aa481 2018-06-21 stsp free(s0);
730 776d4d29 2018-06-17 stsp if (commit)
731 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
732 776d4d29 2018-06-17 stsp if (tree)
733 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
734 776d4d29 2018-06-17 stsp return err;
735 68482ea3 2017-11-27 stsp }