Blame


1 d71d75ad 2017-11-05 stsp /*
2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 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 f8b19efd 2021-10-13 stsp #include <sys/tree.h>
21 2178c42e 2018-04-22 stsp #include <sys/uio.h>
22 64a8571e 2022-01-07 stsp #include <sys/mman.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 d71d75ad 2017-11-05 stsp #include <sha1.h>
30 81a12da5 2020-09-09 naddy #include <unistd.h>
31 ab9a70b2 2017-11-06 stsp #include <zlib.h>
32 e40622f4 2020-07-23 stsp #include <libgen.h>
33 ab9a70b2 2017-11-06 stsp #include <limits.h>
34 2178c42e 2018-04-22 stsp #include <imsg.h>
35 d71d75ad 2017-11-05 stsp
36 ab9a70b2 2017-11-06 stsp #include "got_error.h"
37 d71d75ad 2017-11-05 stsp #include "got_object.h"
38 ab9a70b2 2017-11-06 stsp #include "got_repository.h"
39 511a516b 2018-05-19 stsp #include "got_opentemp.h"
40 324d37e7 2019-05-11 stsp #include "got_path.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 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
45 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
46 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
47 6bef87be 2018-09-11 stsp #include "got_lib_object_idcache.h"
48 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
49 ad242220 2018-09-08 stsp #include "got_lib_object_parse.h"
50 15a94983 2018-12-23 stsp #include "got_lib_pack.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 3235492e 2018-04-01 stsp
57 0ab4c957 2022-06-13 stsp #ifndef nitems
58 0ab4c957 2022-06-13 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 0ab4c957 2022-06-13 stsp #endif
60 0ab4c957 2022-06-13 stsp
61 3235492e 2018-04-01 stsp struct got_object_id *
62 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
63 3235492e 2018-04-01 stsp {
64 6402fb3c 2018-09-15 stsp return &obj->id;
65 bacc9935 2018-05-20 stsp }
66 bacc9935 2018-05-20 stsp
67 bacc9935 2018-05-20 stsp const struct got_error *
68 bacc9935 2018-05-20 stsp got_object_get_id_str(char **outbuf, struct got_object *obj)
69 bacc9935 2018-05-20 stsp {
70 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
71 a1fd68d8 2018-01-12 stsp }
72 d71d75ad 2017-11-05 stsp
73 15a94983 2018-12-23 stsp const struct got_error *
74 15a94983 2018-12-23 stsp got_object_get_type(int *type, struct got_repository *repo,
75 15a94983 2018-12-23 stsp struct got_object_id *id)
76 a1fd68d8 2018-01-12 stsp {
77 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
78 15a94983 2018-12-23 stsp struct got_object *obj;
79 15a94983 2018-12-23 stsp
80 15a94983 2018-12-23 stsp err = got_object_open(&obj, repo, id);
81 15a94983 2018-12-23 stsp if (err)
82 15a94983 2018-12-23 stsp return err;
83 15a94983 2018-12-23 stsp
84 b107e67f 2018-01-19 stsp switch (obj->type) {
85 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
86 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
87 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
88 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
89 15a94983 2018-12-23 stsp *type = obj->type;
90 15a94983 2018-12-23 stsp break;
91 96f5e8b3 2018-01-23 stsp default:
92 15a94983 2018-12-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
93 96f5e8b3 2018-01-23 stsp break;
94 d71d75ad 2017-11-05 stsp }
95 d71d75ad 2017-11-05 stsp
96 15a94983 2018-12-23 stsp got_object_close(obj);
97 15a94983 2018-12-23 stsp return err;
98 d71d75ad 2017-11-05 stsp }
99 ab9a70b2 2017-11-06 stsp
100 90bdb554 2019-04-11 stsp const struct got_error *
101 90bdb554 2019-04-11 stsp got_object_get_path(char **path, struct got_object_id *id,
102 90bdb554 2019-04-11 stsp struct got_repository *repo)
103 ab9a70b2 2017-11-06 stsp {
104 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
105 7a132809 2018-07-23 stsp char *hex = NULL;
106 41d2888b 2019-08-11 stsp char *path_objects;
107 e6b1056e 2018-04-22 stsp
108 e6b1056e 2018-04-22 stsp *path = NULL;
109 ab9a70b2 2017-11-06 stsp
110 41d2888b 2019-08-11 stsp path_objects = got_repo_get_path_objects(repo);
111 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
112 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_objects");
113 ab9a70b2 2017-11-06 stsp
114 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
115 ef0981d5 2018-02-12 stsp if (err)
116 7a132809 2018-07-23 stsp goto done;
117 ab9a70b2 2017-11-06 stsp
118 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
119 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
120 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
121 ab9a70b2 2017-11-06 stsp
122 7a132809 2018-07-23 stsp done:
123 ef0981d5 2018-02-12 stsp free(hex);
124 d1cda826 2017-11-06 stsp free(path_objects);
125 d1cda826 2017-11-06 stsp return err;
126 d1cda826 2017-11-06 stsp }
127 d1cda826 2017-11-06 stsp
128 762d73f4 2021-04-10 stsp const struct got_error *
129 762d73f4 2021-04-10 stsp got_object_open_loose_fd(int *fd, struct got_object_id *id,
130 4796fb13 2018-12-23 stsp struct got_repository *repo)
131 d1cda826 2017-11-06 stsp {
132 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
133 a1fd68d8 2018-01-12 stsp char *path;
134 6c00b545 2018-01-17 stsp
135 90bdb554 2019-04-11 stsp err = got_object_get_path(&path, id, repo);
136 d1cda826 2017-11-06 stsp if (err)
137 d1cda826 2017-11-06 stsp return err;
138 8bd0cdad 2021-12-31 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
139 d5003b79 2018-04-22 stsp if (*fd == -1) {
140 e82b1d81 2019-07-27 stsp err = got_error_from_errno2("open", path);
141 6c00b545 2018-01-17 stsp goto done;
142 a1fd68d8 2018-01-12 stsp }
143 4558fcd4 2018-01-14 stsp done:
144 4558fcd4 2018-01-14 stsp free(path);
145 59d1e4a0 2021-03-10 stsp return err;
146 ab9a70b2 2017-11-06 stsp }
147 ab9a70b2 2017-11-06 stsp
148 59d1e4a0 2021-03-10 stsp const struct got_error *
149 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
150 6dfa2fd3 2018-02-12 stsp const char *id_str)
151 6dfa2fd3 2018-02-12 stsp {
152 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
153 6dfa2fd3 2018-02-12 stsp
154 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
155 6dd1ece6 2019-11-10 stsp return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
156 6dfa2fd3 2018-02-12 stsp
157 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
158 15a94983 2018-12-23 stsp }
159 15a94983 2018-12-23 stsp
160 15a94983 2018-12-23 stsp const struct got_error *
161 15a94983 2018-12-23 stsp got_object_resolve_id_str(struct got_object_id **id,
162 15a94983 2018-12-23 stsp struct got_repository *repo, const char *id_str)
163 15a94983 2018-12-23 stsp {
164 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
165 15a94983 2018-12-23 stsp struct got_object *obj;
166 15a94983 2018-12-23 stsp
167 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
168 15a94983 2018-12-23 stsp if (err)
169 15a94983 2018-12-23 stsp return err;
170 15a94983 2018-12-23 stsp
171 15a94983 2018-12-23 stsp *id = got_object_id_dup(got_object_get_id(obj));
172 15a94983 2018-12-23 stsp got_object_close(obj);
173 15a94983 2018-12-23 stsp if (*id == NULL)
174 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
175 15a94983 2018-12-23 stsp
176 15a94983 2018-12-23 stsp return NULL;
177 434025f3 2018-09-16 stsp }
178 434025f3 2018-09-16 stsp
179 e32baab7 2018-11-05 stsp const struct got_error *
180 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
181 dbc6a6b6 2018-07-12 stsp {
182 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
183 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
184 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
185 dbc6a6b6 2018-07-12 stsp
186 d7b5a0e8 2022-04-20 stsp memcpy(&(*qid)->id, id, sizeof((*qid)->id));
187 9ca9aafb 2021-06-18 stsp return NULL;
188 9ca9aafb 2021-06-18 stsp }
189 9ca9aafb 2021-06-18 stsp
190 9ca9aafb 2021-06-18 stsp const struct got_error *
191 9ca9aafb 2021-06-18 stsp got_object_id_queue_copy(const struct got_object_id_queue *src,
192 9ca9aafb 2021-06-18 stsp struct got_object_id_queue *dest)
193 9ca9aafb 2021-06-18 stsp {
194 9ca9aafb 2021-06-18 stsp const struct got_error *err;
195 9ca9aafb 2021-06-18 stsp struct got_object_qid *qid;
196 9ca9aafb 2021-06-18 stsp
197 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, src, entry) {
198 9ca9aafb 2021-06-18 stsp struct got_object_qid *new;
199 9ca9aafb 2021-06-18 stsp /*
200 9ca9aafb 2021-06-18 stsp * Deep-copy the object ID only. Let the caller deal
201 9ca9aafb 2021-06-18 stsp * with setting up the new->data pointer if needed.
202 9ca9aafb 2021-06-18 stsp */
203 5e91dae4 2022-08-30 stsp err = got_object_qid_alloc(&new, &qid->id);
204 9ca9aafb 2021-06-18 stsp if (err) {
205 9ca9aafb 2021-06-18 stsp got_object_id_queue_free(dest);
206 9ca9aafb 2021-06-18 stsp return err;
207 9ca9aafb 2021-06-18 stsp }
208 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(dest, new, entry);
209 dbc6a6b6 2018-07-12 stsp }
210 dbc6a6b6 2018-07-12 stsp
211 dbc6a6b6 2018-07-12 stsp return NULL;
212 9f2369b0 2018-12-24 stsp }
213 9f2369b0 2018-12-24 stsp
214 56e0773d 2019-11-28 stsp int
215 56e0773d 2019-11-28 stsp got_object_tree_get_nentries(struct got_tree_object *tree)
216 883f0469 2018-06-23 stsp {
217 56e0773d 2019-11-28 stsp return tree->nentries;
218 56e0773d 2019-11-28 stsp }
219 56e0773d 2019-11-28 stsp
220 56e0773d 2019-11-28 stsp struct got_tree_entry *
221 56e0773d 2019-11-28 stsp got_object_tree_get_first_entry(struct got_tree_object *tree)
222 56e0773d 2019-11-28 stsp {
223 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, 0);
224 56e0773d 2019-11-28 stsp }
225 56e0773d 2019-11-28 stsp
226 56e0773d 2019-11-28 stsp struct got_tree_entry *
227 56e0773d 2019-11-28 stsp got_object_tree_get_last_entry(struct got_tree_object *tree)
228 56e0773d 2019-11-28 stsp {
229 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, tree->nentries - 1);
230 56e0773d 2019-11-28 stsp }
231 56e0773d 2019-11-28 stsp
232 56e0773d 2019-11-28 stsp struct got_tree_entry *
233 56e0773d 2019-11-28 stsp got_object_tree_get_entry(struct got_tree_object *tree, int i)
234 56e0773d 2019-11-28 stsp {
235 56e0773d 2019-11-28 stsp if (i < 0 || i >= tree->nentries)
236 56e0773d 2019-11-28 stsp return NULL;
237 56e0773d 2019-11-28 stsp return &tree->entries[i];
238 883f0469 2018-06-23 stsp }
239 3840f4c9 2018-09-12 stsp
240 56e0773d 2019-11-28 stsp mode_t
241 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(struct got_tree_entry *te)
242 56e0773d 2019-11-28 stsp {
243 56e0773d 2019-11-28 stsp return te->mode;
244 56e0773d 2019-11-28 stsp }
245 56e0773d 2019-11-28 stsp
246 56e0773d 2019-11-28 stsp const char *
247 56e0773d 2019-11-28 stsp got_tree_entry_get_name(struct got_tree_entry *te)
248 56e0773d 2019-11-28 stsp {
249 56e0773d 2019-11-28 stsp return &te->name[0];
250 56e0773d 2019-11-28 stsp }
251 56e0773d 2019-11-28 stsp
252 56e0773d 2019-11-28 stsp struct got_object_id *
253 56e0773d 2019-11-28 stsp got_tree_entry_get_id(struct got_tree_entry *te)
254 56e0773d 2019-11-28 stsp {
255 56e0773d 2019-11-28 stsp return &te->id;
256 0d6c6ee3 2020-05-20 stsp }
257 0d6c6ee3 2020-05-20 stsp
258 0d6c6ee3 2020-05-20 stsp const struct got_error *
259 af57b12a 2020-07-23 stsp got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
260 0d6c6ee3 2020-05-20 stsp {
261 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
262 32596e16 2020-07-23 stsp size_t len, totlen, hdrlen, offset;
263 aa092692 2020-07-23 stsp
264 aa092692 2020-07-23 stsp *s = NULL;
265 0d6c6ee3 2020-05-20 stsp
266 659dc16e 2020-07-23 stsp hdrlen = got_object_blob_get_hdrlen(blob);
267 659dc16e 2020-07-23 stsp totlen = 0;
268 32596e16 2020-07-23 stsp offset = 0;
269 659dc16e 2020-07-23 stsp do {
270 659dc16e 2020-07-23 stsp char *p;
271 0d6c6ee3 2020-05-20 stsp
272 659dc16e 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
273 659dc16e 2020-07-23 stsp if (err)
274 af57b12a 2020-07-23 stsp return err;
275 659dc16e 2020-07-23 stsp
276 659dc16e 2020-07-23 stsp if (len == 0)
277 659dc16e 2020-07-23 stsp break;
278 659dc16e 2020-07-23 stsp
279 659dc16e 2020-07-23 stsp totlen += len - hdrlen;
280 af57b12a 2020-07-23 stsp p = realloc(*s, totlen + 1);
281 659dc16e 2020-07-23 stsp if (p == NULL) {
282 659dc16e 2020-07-23 stsp err = got_error_from_errno("realloc");
283 af57b12a 2020-07-23 stsp free(*s);
284 af57b12a 2020-07-23 stsp *s = NULL;
285 af57b12a 2020-07-23 stsp return err;
286 659dc16e 2020-07-23 stsp }
287 af57b12a 2020-07-23 stsp *s = p;
288 659dc16e 2020-07-23 stsp /* Skip blob object header first time around. */
289 af57b12a 2020-07-23 stsp memcpy(*s + offset,
290 f8f7c882 2020-07-23 stsp got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
291 659dc16e 2020-07-23 stsp hdrlen = 0;
292 32596e16 2020-07-23 stsp offset = totlen;
293 659dc16e 2020-07-23 stsp } while (len > 0);
294 af57b12a 2020-07-23 stsp
295 af57b12a 2020-07-23 stsp (*s)[totlen] = '\0';
296 af57b12a 2020-07-23 stsp return NULL;
297 af57b12a 2020-07-23 stsp }
298 af57b12a 2020-07-23 stsp
299 af57b12a 2020-07-23 stsp const struct got_error *
300 af57b12a 2020-07-23 stsp got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
301 af57b12a 2020-07-23 stsp struct got_repository *repo)
302 af57b12a 2020-07-23 stsp {
303 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
304 af57b12a 2020-07-23 stsp struct got_blob_object *blob = NULL;
305 eb81bc23 2022-06-28 tracey int fd = -1;
306 af57b12a 2020-07-23 stsp
307 af57b12a 2020-07-23 stsp *link_target = NULL;
308 af57b12a 2020-07-23 stsp
309 af57b12a 2020-07-23 stsp if (!got_object_tree_entry_is_symlink(te))
310 af57b12a 2020-07-23 stsp return got_error(GOT_ERR_TREE_ENTRY_TYPE);
311 af57b12a 2020-07-23 stsp
312 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
313 eb81bc23 2022-06-28 tracey if (fd == -1) {
314 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
315 eb81bc23 2022-06-28 tracey goto done;
316 eb81bc23 2022-06-28 tracey }
317 eb81bc23 2022-06-28 tracey
318 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo,
319 eb81bc23 2022-06-28 tracey got_tree_entry_get_id(te), PATH_MAX, fd);
320 af57b12a 2020-07-23 stsp if (err)
321 eb81bc23 2022-06-28 tracey goto done;
322 af57b12a 2020-07-23 stsp
323 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(link_target, blob);
324 eb81bc23 2022-06-28 tracey done:
325 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
326 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
327 eb81bc23 2022-06-28 tracey if (blob)
328 eb81bc23 2022-06-28 tracey got_object_blob_close(blob);
329 659dc16e 2020-07-23 stsp if (err) {
330 659dc16e 2020-07-23 stsp free(*link_target);
331 659dc16e 2020-07-23 stsp *link_target = NULL;
332 659dc16e 2020-07-23 stsp }
333 0d6c6ee3 2020-05-20 stsp return err;
334 56e0773d 2019-11-28 stsp }
335 56e0773d 2019-11-28 stsp
336 56e0773d 2019-11-28 stsp int
337 56e0773d 2019-11-28 stsp got_tree_entry_get_index(struct got_tree_entry *te)
338 56e0773d 2019-11-28 stsp {
339 56e0773d 2019-11-28 stsp return te->idx;
340 56e0773d 2019-11-28 stsp }
341 56e0773d 2019-11-28 stsp
342 56e0773d 2019-11-28 stsp struct got_tree_entry *
343 56e0773d 2019-11-28 stsp got_tree_entry_get_next(struct got_tree_object *tree,
344 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
345 56e0773d 2019-11-28 stsp {
346 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx + 1);
347 56e0773d 2019-11-28 stsp }
348 56e0773d 2019-11-28 stsp
349 56e0773d 2019-11-28 stsp struct got_tree_entry *
350 56e0773d 2019-11-28 stsp got_tree_entry_get_prev(struct got_tree_object *tree,
351 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
352 56e0773d 2019-11-28 stsp {
353 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx - 1);
354 56e0773d 2019-11-28 stsp }
355 56e0773d 2019-11-28 stsp
356 a19581a2 2018-06-21 stsp const struct got_error *
357 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
358 68482ea3 2017-11-27 stsp {
359 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
360 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
361 56b63ca4 2021-01-22 stsp if (blob->f && fclose(blob->f) == EOF)
362 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
363 ac544f8c 2019-01-13 stsp free(blob->data);
364 68482ea3 2017-11-27 stsp free(blob);
365 fb43ecf1 2019-02-11 stsp return err;
366 f934cf2c 2018-02-12 stsp }
367 f934cf2c 2018-02-12 stsp
368 8ba819a3 2020-07-23 stsp void
369 8ba819a3 2020-07-23 stsp got_object_blob_rewind(struct got_blob_object *blob)
370 8ba819a3 2020-07-23 stsp {
371 8ba819a3 2020-07-23 stsp if (blob->f)
372 8ba819a3 2020-07-23 stsp rewind(blob->f);
373 8ba819a3 2020-07-23 stsp }
374 8ba819a3 2020-07-23 stsp
375 f934cf2c 2018-02-12 stsp char *
376 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
377 f934cf2c 2018-02-12 stsp {
378 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
379 f934cf2c 2018-02-12 stsp }
380 f934cf2c 2018-02-12 stsp
381 f934cf2c 2018-02-12 stsp size_t
382 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
383 f934cf2c 2018-02-12 stsp {
384 f934cf2c 2018-02-12 stsp return blob->hdrlen;
385 68482ea3 2017-11-27 stsp }
386 68482ea3 2017-11-27 stsp
387 f934cf2c 2018-02-12 stsp const uint8_t *
388 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
389 f934cf2c 2018-02-12 stsp {
390 f934cf2c 2018-02-12 stsp return blob->read_buf;
391 f934cf2c 2018-02-12 stsp }
392 f934cf2c 2018-02-12 stsp
393 68482ea3 2017-11-27 stsp const struct got_error *
394 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
395 68482ea3 2017-11-27 stsp {
396 eb651edf 2018-02-11 stsp size_t n;
397 eb651edf 2018-02-11 stsp
398 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
399 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
400 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
401 eb651edf 2018-02-11 stsp *outlenp = n;
402 35e9ba5d 2018-06-21 stsp return NULL;
403 35e9ba5d 2018-06-21 stsp }
404 35e9ba5d 2018-06-21 stsp
405 35e9ba5d 2018-06-21 stsp const struct got_error *
406 be659d10 2020-11-18 stsp got_object_blob_dump_to_file(off_t *filesize, int *nlines,
407 6c4c42e0 2019-06-24 stsp off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
408 35e9ba5d 2018-06-21 stsp {
409 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
410 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
411 84451b3e 2018-07-10 stsp const uint8_t *buf;
412 84451b3e 2018-07-10 stsp int i;
413 c33ebc60 2020-11-18 stsp const int alloc_chunksz = 512;
414 c33ebc60 2020-11-18 stsp size_t nalloc = 0;
415 f595d9bd 2019-08-14 stsp off_t off = 0, total_len = 0;
416 84451b3e 2018-07-10 stsp
417 6c4c42e0 2019-06-24 stsp if (line_offsets)
418 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
419 f595d9bd 2019-08-14 stsp if (filesize)
420 f595d9bd 2019-08-14 stsp *filesize = 0;
421 84451b3e 2018-07-10 stsp if (nlines)
422 84451b3e 2018-07-10 stsp *nlines = 0;
423 35e9ba5d 2018-06-21 stsp
424 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
425 35e9ba5d 2018-06-21 stsp do {
426 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
427 35e9ba5d 2018-06-21 stsp if (err)
428 35e9ba5d 2018-06-21 stsp return err;
429 35e9ba5d 2018-06-21 stsp if (len == 0)
430 35e9ba5d 2018-06-21 stsp break;
431 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
432 b02560ec 2019-08-19 stsp i = hdrlen;
433 f1cbc3bc 2020-11-18 stsp if (nlines) {
434 f1cbc3bc 2020-11-18 stsp if (line_offsets && *line_offsets == NULL) {
435 78695fb7 2019-08-12 stsp /* Have some data but perhaps no '\n'. */
436 78695fb7 2019-08-12 stsp *nlines = 1;
437 c33ebc60 2020-11-18 stsp nalloc = alloc_chunksz;
438 c33ebc60 2020-11-18 stsp *line_offsets = calloc(nalloc,
439 c33ebc60 2020-11-18 stsp sizeof(**line_offsets));
440 78695fb7 2019-08-12 stsp if (*line_offsets == NULL)
441 845785d4 2020-02-02 tracey return got_error_from_errno("calloc");
442 b02560ec 2019-08-19 stsp
443 b02560ec 2019-08-19 stsp /* Skip forward over end of first line. */
444 b02560ec 2019-08-19 stsp while (i < len) {
445 b02560ec 2019-08-19 stsp if (buf[i] == '\n')
446 b02560ec 2019-08-19 stsp break;
447 b02560ec 2019-08-19 stsp i++;
448 b02560ec 2019-08-19 stsp }
449 b02560ec 2019-08-19 stsp }
450 b02560ec 2019-08-19 stsp /* Scan '\n' offsets in remaining chunk of data. */
451 b02560ec 2019-08-19 stsp while (i < len) {
452 b02560ec 2019-08-19 stsp if (buf[i] != '\n') {
453 b02560ec 2019-08-19 stsp i++;
454 f595d9bd 2019-08-14 stsp continue;
455 b02560ec 2019-08-19 stsp }
456 f595d9bd 2019-08-14 stsp (*nlines)++;
457 c33ebc60 2020-11-18 stsp if (line_offsets && nalloc < *nlines) {
458 c33ebc60 2020-11-18 stsp size_t n = *nlines + alloc_chunksz;
459 78695fb7 2019-08-12 stsp off_t *o = recallocarray(*line_offsets,
460 c33ebc60 2020-11-18 stsp nalloc, n, sizeof(**line_offsets));
461 78695fb7 2019-08-12 stsp if (o == NULL) {
462 78695fb7 2019-08-12 stsp free(*line_offsets);
463 78695fb7 2019-08-12 stsp *line_offsets = NULL;
464 78695fb7 2019-08-12 stsp return got_error_from_errno(
465 78695fb7 2019-08-12 stsp "recallocarray");
466 78695fb7 2019-08-12 stsp }
467 78695fb7 2019-08-12 stsp *line_offsets = o;
468 c33ebc60 2020-11-18 stsp nalloc = n;
469 78695fb7 2019-08-12 stsp }
470 f1cbc3bc 2020-11-18 stsp if (line_offsets) {
471 f1cbc3bc 2020-11-18 stsp off = total_len + i - hdrlen + 1;
472 f1cbc3bc 2020-11-18 stsp (*line_offsets)[*nlines - 1] = off;
473 f1cbc3bc 2020-11-18 stsp }
474 b02560ec 2019-08-19 stsp i++;
475 6c4c42e0 2019-06-24 stsp }
476 84451b3e 2018-07-10 stsp }
477 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
478 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
479 b6752625 2018-12-24 stsp if (n != len - hdrlen)
480 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
481 f595d9bd 2019-08-14 stsp total_len += len - hdrlen;
482 35e9ba5d 2018-06-21 stsp hdrlen = 0;
483 35e9ba5d 2018-06-21 stsp } while (len != 0);
484 35e9ba5d 2018-06-21 stsp
485 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
486 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
487 35e9ba5d 2018-06-21 stsp rewind(outfile);
488 35e9ba5d 2018-06-21 stsp
489 f595d9bd 2019-08-14 stsp if (filesize)
490 f595d9bd 2019-08-14 stsp *filesize = total_len;
491 f595d9bd 2019-08-14 stsp
492 776d4d29 2018-06-17 stsp return NULL;
493 f4a881ce 2018-11-17 stsp }
494 f4a881ce 2018-11-17 stsp
495 d24820bf 2019-08-11 stsp const char *
496 d24820bf 2019-08-11 stsp got_object_tag_get_name(struct got_tag_object *tag)
497 d24820bf 2019-08-11 stsp {
498 d24820bf 2019-08-11 stsp return tag->tag;
499 0bd18d37 2019-02-01 stsp }
500 0bd18d37 2019-02-01 stsp
501 0bd18d37 2019-02-01 stsp int
502 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
503 0bd18d37 2019-02-01 stsp {
504 0bd18d37 2019-02-01 stsp return tag->obj_type;
505 0bd18d37 2019-02-01 stsp }
506 0bd18d37 2019-02-01 stsp
507 0bd18d37 2019-02-01 stsp struct got_object_id *
508 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
509 0bd18d37 2019-02-01 stsp {
510 0bd18d37 2019-02-01 stsp return &tag->id;
511 01073a5d 2019-08-22 stsp }
512 01073a5d 2019-08-22 stsp
513 01073a5d 2019-08-22 stsp time_t
514 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_time(struct got_tag_object *tag)
515 01073a5d 2019-08-22 stsp {
516 01073a5d 2019-08-22 stsp return tag->tagger_time;
517 01073a5d 2019-08-22 stsp }
518 01073a5d 2019-08-22 stsp
519 01073a5d 2019-08-22 stsp time_t
520 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
521 01073a5d 2019-08-22 stsp {
522 01073a5d 2019-08-22 stsp return tag->tagger_gmtoff;
523 01073a5d 2019-08-22 stsp }
524 01073a5d 2019-08-22 stsp
525 01073a5d 2019-08-22 stsp const char *
526 01073a5d 2019-08-22 stsp got_object_tag_get_tagger(struct got_tag_object *tag)
527 01073a5d 2019-08-22 stsp {
528 01073a5d 2019-08-22 stsp return tag->tagger;
529 776d4d29 2018-06-17 stsp }
530 776d4d29 2018-06-17 stsp
531 01073a5d 2019-08-22 stsp const char *
532 01073a5d 2019-08-22 stsp got_object_tag_get_message(struct got_tag_object *tag)
533 01073a5d 2019-08-22 stsp {
534 01073a5d 2019-08-22 stsp return tag->tagmsg;
535 01073a5d 2019-08-22 stsp }
536 01073a5d 2019-08-22 stsp
537 776d4d29 2018-06-17 stsp static struct got_tree_entry *
538 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
539 776d4d29 2018-06-17 stsp {
540 56e0773d 2019-11-28 stsp int i;
541 776d4d29 2018-06-17 stsp
542 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
543 56e0773d 2019-11-28 stsp for (i = 0; i < tree->nentries; i++) {
544 56e0773d 2019-11-28 stsp struct got_tree_entry *te = &tree->entries[i];
545 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
546 63da309a 2018-11-07 stsp if (cmp < 0)
547 63da309a 2018-11-07 stsp continue;
548 63da309a 2018-11-07 stsp if (cmp > 0)
549 63da309a 2018-11-07 stsp break;
550 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
551 776d4d29 2018-06-17 stsp return te;
552 776d4d29 2018-06-17 stsp }
553 eb651edf 2018-02-11 stsp return NULL;
554 a129376b 2019-03-28 stsp }
555 a129376b 2019-03-28 stsp
556 56e0773d 2019-11-28 stsp struct got_tree_entry *
557 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
558 a129376b 2019-03-28 stsp {
559 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
560 776d4d29 2018-06-17 stsp }
561 776d4d29 2018-06-17 stsp
562 776d4d29 2018-06-17 stsp const struct got_error *
563 67b631c9 2021-10-10 stsp got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
564 67b631c9 2021-10-10 stsp struct got_repository *repo, struct got_tree_object *tree,
565 67b631c9 2021-10-10 stsp const char *path)
566 776d4d29 2018-06-17 stsp {
567 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
568 67b631c9 2021-10-10 stsp struct got_tree_object *subtree = NULL;
569 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
570 65a9bbe9 2018-09-15 stsp const char *seg, *s;
571 b7cd37e5 2018-11-18 stsp size_t seglen;
572 776d4d29 2018-06-17 stsp
573 27d434c2 2018-09-15 stsp *id = NULL;
574 776d4d29 2018-06-17 stsp
575 65a9bbe9 2018-09-15 stsp s = path;
576 5e54fb30 2019-05-31 stsp while (s[0] == '/')
577 5e54fb30 2019-05-31 stsp s++;
578 776d4d29 2018-06-17 stsp seg = s;
579 65a9bbe9 2018-09-15 stsp seglen = 0;
580 67b631c9 2021-10-10 stsp subtree = tree;
581 b7cd37e5 2018-11-18 stsp while (*s) {
582 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
583 776d4d29 2018-06-17 stsp
584 776d4d29 2018-06-17 stsp if (*s != '/') {
585 776d4d29 2018-06-17 stsp s++;
586 65a9bbe9 2018-09-15 stsp seglen++;
587 00530cfb 2018-06-21 stsp if (*s)
588 00530cfb 2018-06-21 stsp continue;
589 776d4d29 2018-06-17 stsp }
590 776d4d29 2018-06-17 stsp
591 67b631c9 2021-10-10 stsp te = find_entry_by_name(subtree, seg, seglen);
592 db37e2c0 2018-06-21 stsp if (te == NULL) {
593 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
594 776d4d29 2018-06-17 stsp goto done;
595 776d4d29 2018-06-17 stsp }
596 776d4d29 2018-06-17 stsp
597 b7cd37e5 2018-11-18 stsp if (*s == '\0')
598 67606321 2018-06-21 stsp break;
599 67606321 2018-06-21 stsp
600 776d4d29 2018-06-17 stsp seg = s + 1;
601 65a9bbe9 2018-09-15 stsp seglen = 0;
602 776d4d29 2018-06-17 stsp s++;
603 776d4d29 2018-06-17 stsp if (*s) {
604 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
605 56e0773d 2019-11-28 stsp &te->id);
606 db37e2c0 2018-06-21 stsp te = NULL;
607 776d4d29 2018-06-17 stsp if (err)
608 776d4d29 2018-06-17 stsp goto done;
609 67b631c9 2021-10-10 stsp if (subtree != tree)
610 67b631c9 2021-10-10 stsp got_object_tree_close(subtree);
611 67b631c9 2021-10-10 stsp subtree = next_tree;
612 776d4d29 2018-06-17 stsp }
613 776d4d29 2018-06-17 stsp }
614 776d4d29 2018-06-17 stsp
615 27d434c2 2018-09-15 stsp if (te) {
616 56e0773d 2019-11-28 stsp *id = got_object_id_dup(&te->id);
617 27d434c2 2018-09-15 stsp if (*id == NULL)
618 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
619 67b631c9 2021-10-10 stsp if (mode)
620 67b631c9 2021-10-10 stsp *mode = te->mode;
621 27d434c2 2018-09-15 stsp } else
622 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
623 67b631c9 2021-10-10 stsp done:
624 67b631c9 2021-10-10 stsp if (subtree && subtree != tree)
625 67b631c9 2021-10-10 stsp got_object_tree_close(subtree);
626 67b631c9 2021-10-10 stsp return err;
627 67b631c9 2021-10-10 stsp }
628 8e359fa0 2022-10-13 stsp
629 67b631c9 2021-10-10 stsp const struct got_error *
630 67b631c9 2021-10-10 stsp got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
631 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
632 67b631c9 2021-10-10 stsp {
633 67b631c9 2021-10-10 stsp const struct got_error *err = NULL;
634 67b631c9 2021-10-10 stsp struct got_tree_object *tree = NULL;
635 67b631c9 2021-10-10 stsp
636 67b631c9 2021-10-10 stsp *id = NULL;
637 67b631c9 2021-10-10 stsp
638 67b631c9 2021-10-10 stsp /* Handle opening of root of commit's tree. */
639 67b631c9 2021-10-10 stsp if (got_path_is_root_dir(path)) {
640 67b631c9 2021-10-10 stsp *id = got_object_id_dup(commit->tree_id);
641 67b631c9 2021-10-10 stsp if (*id == NULL)
642 67b631c9 2021-10-10 stsp err = got_error_from_errno("got_object_id_dup");
643 67b631c9 2021-10-10 stsp } else {
644 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
645 67b631c9 2021-10-10 stsp if (err)
646 67b631c9 2021-10-10 stsp goto done;
647 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(id, NULL, repo, tree, path);
648 67b631c9 2021-10-10 stsp }
649 776d4d29 2018-06-17 stsp done:
650 776d4d29 2018-06-17 stsp if (tree)
651 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
652 776d4d29 2018-06-17 stsp return err;
653 ac5f2b26 2020-05-05 stsp }
654 ac5f2b26 2020-05-05 stsp
655 ac5f2b26 2020-05-05 stsp /*
656 ac5f2b26 2020-05-05 stsp * Normalize file mode bits to avoid false positive tree entry differences
657 ac5f2b26 2020-05-05 stsp * in case tree entries have unexpected mode bits set.
658 ac5f2b26 2020-05-05 stsp */
659 ac5f2b26 2020-05-05 stsp static mode_t
660 ac5f2b26 2020-05-05 stsp normalize_mode_for_comparison(mode_t mode)
661 ac5f2b26 2020-05-05 stsp {
662 ac5f2b26 2020-05-05 stsp /*
663 ac5f2b26 2020-05-05 stsp * For directories, the only relevant bit is the IFDIR bit.
664 ac5f2b26 2020-05-05 stsp * This allows us to detect paths changing from a directory
665 ac5f2b26 2020-05-05 stsp * to a file and vice versa.
666 ac5f2b26 2020-05-05 stsp */
667 ac5f2b26 2020-05-05 stsp if (S_ISDIR(mode))
668 ac5f2b26 2020-05-05 stsp return mode & S_IFDIR;
669 40dde666 2020-07-23 stsp
670 40dde666 2020-07-23 stsp /*
671 40dde666 2020-07-23 stsp * For symlinks, the only relevant bit is the IFLNK bit.
672 40dde666 2020-07-23 stsp * This allows us to detect paths changing from a symlinks
673 40dde666 2020-07-23 stsp * to a file or directory and vice versa.
674 40dde666 2020-07-23 stsp */
675 40dde666 2020-07-23 stsp if (S_ISLNK(mode))
676 40dde666 2020-07-23 stsp return mode & S_IFLNK;
677 ac5f2b26 2020-05-05 stsp
678 ac5f2b26 2020-05-05 stsp /* For files, the only change we care about is the executable bit. */
679 ac5f2b26 2020-05-05 stsp return mode & S_IXUSR;
680 68482ea3 2017-11-27 stsp }
681 07862c20 2018-09-15 stsp
682 07862c20 2018-09-15 stsp const struct got_error *
683 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
684 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
685 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
686 07862c20 2018-09-15 stsp {
687 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
688 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
689 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
690 65a9bbe9 2018-09-15 stsp const char *seg, *s;
691 3b7f9878 2018-11-18 stsp size_t seglen;
692 07862c20 2018-09-15 stsp
693 07862c20 2018-09-15 stsp *changed = 0;
694 07862c20 2018-09-15 stsp
695 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
696 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
697 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
698 07862c20 2018-09-15 stsp
699 07862c20 2018-09-15 stsp tree1 = tree01;
700 07862c20 2018-09-15 stsp tree2 = tree02;
701 65a9bbe9 2018-09-15 stsp s = path;
702 61a7d79f 2020-02-29 stsp while (*s == '/')
703 61a7d79f 2020-02-29 stsp s++;
704 07862c20 2018-09-15 stsp seg = s;
705 65a9bbe9 2018-09-15 stsp seglen = 0;
706 3b7f9878 2018-11-18 stsp while (*s) {
707 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
708 ac5f2b26 2020-05-05 stsp mode_t mode1, mode2;
709 07862c20 2018-09-15 stsp
710 07862c20 2018-09-15 stsp if (*s != '/') {
711 07862c20 2018-09-15 stsp s++;
712 65a9bbe9 2018-09-15 stsp seglen++;
713 07862c20 2018-09-15 stsp if (*s)
714 07862c20 2018-09-15 stsp continue;
715 07862c20 2018-09-15 stsp }
716 07862c20 2018-09-15 stsp
717 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
718 07862c20 2018-09-15 stsp if (te1 == NULL) {
719 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
720 07862c20 2018-09-15 stsp goto done;
721 07862c20 2018-09-15 stsp }
722 07862c20 2018-09-15 stsp
723 e8bfb8f3 2020-12-18 stsp if (tree2)
724 e8bfb8f3 2020-12-18 stsp te2 = find_entry_by_name(tree2, seg, seglen);
725 07862c20 2018-09-15 stsp
726 e8bfb8f3 2020-12-18 stsp if (te2) {
727 e8bfb8f3 2020-12-18 stsp mode1 = normalize_mode_for_comparison(te1->mode);
728 e8bfb8f3 2020-12-18 stsp mode2 = normalize_mode_for_comparison(te2->mode);
729 e8bfb8f3 2020-12-18 stsp if (mode1 != mode2) {
730 e8bfb8f3 2020-12-18 stsp *changed = 1;
731 e8bfb8f3 2020-12-18 stsp goto done;
732 e8bfb8f3 2020-12-18 stsp }
733 e8bfb8f3 2020-12-18 stsp
734 e8bfb8f3 2020-12-18 stsp if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
735 e8bfb8f3 2020-12-18 stsp *changed = 0;
736 e8bfb8f3 2020-12-18 stsp goto done;
737 e8bfb8f3 2020-12-18 stsp }
738 07862c20 2018-09-15 stsp }
739 07862c20 2018-09-15 stsp
740 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
741 07862c20 2018-09-15 stsp *changed = 1;
742 07862c20 2018-09-15 stsp goto done;
743 07862c20 2018-09-15 stsp }
744 07862c20 2018-09-15 stsp
745 07862c20 2018-09-15 stsp seg = s + 1;
746 07862c20 2018-09-15 stsp s++;
747 65a9bbe9 2018-09-15 stsp seglen = 0;
748 07862c20 2018-09-15 stsp if (*s) {
749 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
750 56e0773d 2019-11-28 stsp &te1->id);
751 07862c20 2018-09-15 stsp te1 = NULL;
752 07862c20 2018-09-15 stsp if (err)
753 07862c20 2018-09-15 stsp goto done;
754 a31cea73 2018-09-15 stsp if (tree1 != tree01)
755 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
756 07862c20 2018-09-15 stsp tree1 = next_tree1;
757 07862c20 2018-09-15 stsp
758 e8bfb8f3 2020-12-18 stsp if (te2) {
759 e8bfb8f3 2020-12-18 stsp err = got_object_open_as_tree(&next_tree2, repo,
760 e8bfb8f3 2020-12-18 stsp &te2->id);
761 e8bfb8f3 2020-12-18 stsp te2 = NULL;
762 e8bfb8f3 2020-12-18 stsp if (err)
763 e8bfb8f3 2020-12-18 stsp goto done;
764 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
765 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
766 e8bfb8f3 2020-12-18 stsp tree2 = next_tree2;
767 e8bfb8f3 2020-12-18 stsp } else if (tree2) {
768 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
769 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
770 e8bfb8f3 2020-12-18 stsp tree2 = NULL;
771 e8bfb8f3 2020-12-18 stsp }
772 07862c20 2018-09-15 stsp }
773 07862c20 2018-09-15 stsp }
774 07862c20 2018-09-15 stsp done:
775 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
776 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
777 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
778 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
779 77880158 2018-11-04 stsp return err;
780 77880158 2018-11-04 stsp }
781 ed175427 2019-05-09 stsp
782 ed175427 2019-05-09 stsp const struct got_error *
783 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
784 ed175427 2019-05-09 stsp struct got_tree_entry *te)
785 ed175427 2019-05-09 stsp {
786 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
787 ed175427 2019-05-09 stsp
788 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
789 ed175427 2019-05-09 stsp if (*new_te == NULL)
790 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
791 ed175427 2019-05-09 stsp
792 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
793 56e0773d 2019-11-28 stsp memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
794 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
795 8c4eabf2 2019-05-10 stsp return err;
796 63c5ca5d 2019-08-24 stsp }
797 63c5ca5d 2019-08-24 stsp
798 63c5ca5d 2019-08-24 stsp int
799 56e0773d 2019-11-28 stsp got_object_tree_entry_is_submodule(struct got_tree_entry *te)
800 63c5ca5d 2019-08-24 stsp {
801 63c5ca5d 2019-08-24 stsp return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
802 e40622f4 2020-07-23 stsp }
803 e40622f4 2020-07-23 stsp
804 e40622f4 2020-07-23 stsp int
805 e40622f4 2020-07-23 stsp got_object_tree_entry_is_symlink(struct got_tree_entry *te)
806 e40622f4 2020-07-23 stsp {
807 e40622f4 2020-07-23 stsp /* S_IFDIR check avoids confusing symlinks with submodules. */
808 e40622f4 2020-07-23 stsp return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
809 e40622f4 2020-07-23 stsp }
810 e40622f4 2020-07-23 stsp
811 e40622f4 2020-07-23 stsp static const struct got_error *
812 e40622f4 2020-07-23 stsp resolve_symlink(char **link_target, const char *path,
813 a44927cc 2022-04-07 stsp struct got_commit_object *commit, struct got_repository *repo)
814 e40622f4 2020-07-23 stsp {
815 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
816 dbdd6209 2020-10-19 stsp char buf[PATH_MAX];
817 e40622f4 2020-07-23 stsp char *name, *parent_path = NULL;
818 e40622f4 2020-07-23 stsp struct got_object_id *tree_obj_id = NULL;
819 e40622f4 2020-07-23 stsp struct got_tree_object *tree = NULL;
820 e40622f4 2020-07-23 stsp struct got_tree_entry *te = NULL;
821 e40622f4 2020-07-23 stsp
822 e40622f4 2020-07-23 stsp *link_target = NULL;
823 559d127c 2020-07-23 stsp
824 dbdd6209 2020-10-19 stsp if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
825 dbdd6209 2020-10-19 stsp return got_error(GOT_ERR_NO_SPACE);
826 dbdd6209 2020-10-19 stsp
827 dbdd6209 2020-10-19 stsp name = basename(buf);
828 e40622f4 2020-07-23 stsp if (name == NULL)
829 e40622f4 2020-07-23 stsp return got_error_from_errno2("basename", path);
830 e40622f4 2020-07-23 stsp
831 e40622f4 2020-07-23 stsp err = got_path_dirname(&parent_path, path);
832 e40622f4 2020-07-23 stsp if (err)
833 e40622f4 2020-07-23 stsp return err;
834 e40622f4 2020-07-23 stsp
835 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_obj_id, repo, commit,
836 e40622f4 2020-07-23 stsp parent_path);
837 e40622f4 2020-07-23 stsp if (err) {
838 e40622f4 2020-07-23 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY) {
839 e40622f4 2020-07-23 stsp /* Display the complete path in error message. */
840 e40622f4 2020-07-23 stsp err = got_error_path(path, err->code);
841 e40622f4 2020-07-23 stsp }
842 e40622f4 2020-07-23 stsp goto done;
843 e40622f4 2020-07-23 stsp }
844 e40622f4 2020-07-23 stsp
845 e40622f4 2020-07-23 stsp err = got_object_open_as_tree(&tree, repo, tree_obj_id);
846 e40622f4 2020-07-23 stsp if (err)
847 e40622f4 2020-07-23 stsp goto done;
848 e40622f4 2020-07-23 stsp
849 e40622f4 2020-07-23 stsp te = got_object_tree_find_entry(tree, name);
850 e40622f4 2020-07-23 stsp if (te == NULL) {
851 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
852 e40622f4 2020-07-23 stsp goto done;
853 e40622f4 2020-07-23 stsp }
854 e40622f4 2020-07-23 stsp
855 e40622f4 2020-07-23 stsp if (got_object_tree_entry_is_symlink(te)) {
856 e40622f4 2020-07-23 stsp err = got_tree_entry_get_symlink_target(link_target, te, repo);
857 e40622f4 2020-07-23 stsp if (err)
858 e40622f4 2020-07-23 stsp goto done;
859 e40622f4 2020-07-23 stsp if (!got_path_is_absolute(*link_target)) {
860 e40622f4 2020-07-23 stsp char *abspath;
861 e40622f4 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent_path,
862 e40622f4 2020-07-23 stsp *link_target) == -1) {
863 e40622f4 2020-07-23 stsp err = got_error_from_errno("asprintf");
864 e40622f4 2020-07-23 stsp goto done;
865 e40622f4 2020-07-23 stsp }
866 e40622f4 2020-07-23 stsp free(*link_target);
867 e40622f4 2020-07-23 stsp *link_target = malloc(PATH_MAX);
868 e40622f4 2020-07-23 stsp if (*link_target == NULL) {
869 e40622f4 2020-07-23 stsp err = got_error_from_errno("malloc");
870 e40622f4 2020-07-23 stsp goto done;
871 e40622f4 2020-07-23 stsp }
872 e40622f4 2020-07-23 stsp err = got_canonpath(abspath, *link_target, PATH_MAX);
873 e40622f4 2020-07-23 stsp free(abspath);
874 e40622f4 2020-07-23 stsp if (err)
875 e40622f4 2020-07-23 stsp goto done;
876 e40622f4 2020-07-23 stsp }
877 e40622f4 2020-07-23 stsp }
878 e40622f4 2020-07-23 stsp done:
879 b68bd9d2 2022-09-03 op free(parent_path);
880 e40622f4 2020-07-23 stsp free(tree_obj_id);
881 e40622f4 2020-07-23 stsp if (tree)
882 e40622f4 2020-07-23 stsp got_object_tree_close(tree);
883 e40622f4 2020-07-23 stsp if (err) {
884 e40622f4 2020-07-23 stsp free(*link_target);
885 e40622f4 2020-07-23 stsp *link_target = NULL;
886 e40622f4 2020-07-23 stsp }
887 e40622f4 2020-07-23 stsp return err;
888 ca6e02ac 2020-01-07 stsp }
889 ca6e02ac 2020-01-07 stsp
890 ca6e02ac 2020-01-07 stsp const struct got_error *
891 e40622f4 2020-07-23 stsp got_object_resolve_symlinks(char **link_target, const char *path,
892 a44927cc 2022-04-07 stsp struct got_commit_object *commit, struct got_repository *repo)
893 e40622f4 2020-07-23 stsp {
894 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
895 e40622f4 2020-07-23 stsp char *next_target = NULL;
896 e40622f4 2020-07-23 stsp int max_recursion = 40; /* matches Git */
897 e40622f4 2020-07-23 stsp
898 e40622f4 2020-07-23 stsp *link_target = NULL;
899 e40622f4 2020-07-23 stsp
900 e40622f4 2020-07-23 stsp do {
901 e40622f4 2020-07-23 stsp err = resolve_symlink(&next_target,
902 a44927cc 2022-04-07 stsp *link_target ? *link_target : path, commit, repo);
903 e40622f4 2020-07-23 stsp if (err)
904 e40622f4 2020-07-23 stsp break;
905 e40622f4 2020-07-23 stsp if (next_target) {
906 e40622f4 2020-07-23 stsp free(*link_target);
907 e40622f4 2020-07-23 stsp if (--max_recursion == 0) {
908 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_RECURSION);
909 e40622f4 2020-07-23 stsp *link_target = NULL;
910 e40622f4 2020-07-23 stsp break;
911 e40622f4 2020-07-23 stsp }
912 e40622f4 2020-07-23 stsp *link_target = next_target;
913 e40622f4 2020-07-23 stsp }
914 e40622f4 2020-07-23 stsp } while (next_target);
915 e40622f4 2020-07-23 stsp
916 e40622f4 2020-07-23 stsp return err;
917 e40622f4 2020-07-23 stsp }
918 ca6e02ac 2020-01-07 stsp
919 34a842a4 2022-09-18 mark void
920 34a842a4 2022-09-18 mark got_object_commit_retain(struct got_commit_object *commit)
921 568eae95 2022-09-11 mark {
922 568eae95 2022-09-11 mark commit->refcnt++;
923 568eae95 2022-09-11 mark }