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 69c6accf 2023-02-04 op #include <sha2.h>
31 81a12da5 2020-09-09 naddy #include <unistd.h>
32 ab9a70b2 2017-11-06 stsp #include <zlib.h>
33 e40622f4 2020-07-23 stsp #include <libgen.h>
34 ab9a70b2 2017-11-06 stsp #include <limits.h>
35 2178c42e 2018-04-22 stsp #include <imsg.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 324d37e7 2019-05-11 stsp #include "got_path.h"
42 d71d75ad 2017-11-05 stsp
43 1362b0e3 2023-02-04 op #include "got_lib_hash.h"
44 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
45 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_object.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 3093e2d7 2023-02-04 op id->hash[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 328dced4 2023-02-04 op struct got_object_id id = { .algo = repo->algo };
153 6dfa2fd3 2018-02-12 stsp
154 328dced4 2023-02-04 op if (repo->algo == GOT_HASH_SHA256) {
155 328dced4 2023-02-04 op if (!got_parse_sha256_digest(id.hash, id_str))
156 328dced4 2023-02-04 op return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
157 328dced4 2023-02-04 op } else if (repo->algo == GOT_HASH_SHA1) {
158 328dced4 2023-02-04 op if (!got_parse_sha1_digest(id.hash, id_str))
159 328dced4 2023-02-04 op return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
160 328dced4 2023-02-04 op } else
161 6dd1ece6 2019-11-10 stsp return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
162 6dfa2fd3 2018-02-12 stsp
163 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
164 15a94983 2018-12-23 stsp }
165 15a94983 2018-12-23 stsp
166 15a94983 2018-12-23 stsp const struct got_error *
167 15a94983 2018-12-23 stsp got_object_resolve_id_str(struct got_object_id **id,
168 15a94983 2018-12-23 stsp struct got_repository *repo, const char *id_str)
169 15a94983 2018-12-23 stsp {
170 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
171 15a94983 2018-12-23 stsp struct got_object *obj;
172 15a94983 2018-12-23 stsp
173 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
174 15a94983 2018-12-23 stsp if (err)
175 15a94983 2018-12-23 stsp return err;
176 15a94983 2018-12-23 stsp
177 15a94983 2018-12-23 stsp *id = got_object_id_dup(got_object_get_id(obj));
178 15a94983 2018-12-23 stsp got_object_close(obj);
179 15a94983 2018-12-23 stsp if (*id == NULL)
180 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
181 15a94983 2018-12-23 stsp
182 15a94983 2018-12-23 stsp return NULL;
183 434025f3 2018-09-16 stsp }
184 434025f3 2018-09-16 stsp
185 e32baab7 2018-11-05 stsp const struct got_error *
186 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
187 dbc6a6b6 2018-07-12 stsp {
188 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
189 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
190 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
191 dbc6a6b6 2018-07-12 stsp
192 d7b5a0e8 2022-04-20 stsp memcpy(&(*qid)->id, id, sizeof((*qid)->id));
193 9ca9aafb 2021-06-18 stsp return NULL;
194 9ca9aafb 2021-06-18 stsp }
195 9ca9aafb 2021-06-18 stsp
196 9ca9aafb 2021-06-18 stsp const struct got_error *
197 9ca9aafb 2021-06-18 stsp got_object_id_queue_copy(const struct got_object_id_queue *src,
198 9ca9aafb 2021-06-18 stsp struct got_object_id_queue *dest)
199 9ca9aafb 2021-06-18 stsp {
200 9ca9aafb 2021-06-18 stsp const struct got_error *err;
201 9ca9aafb 2021-06-18 stsp struct got_object_qid *qid;
202 9ca9aafb 2021-06-18 stsp
203 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, src, entry) {
204 9ca9aafb 2021-06-18 stsp struct got_object_qid *new;
205 9ca9aafb 2021-06-18 stsp /*
206 9ca9aafb 2021-06-18 stsp * Deep-copy the object ID only. Let the caller deal
207 9ca9aafb 2021-06-18 stsp * with setting up the new->data pointer if needed.
208 9ca9aafb 2021-06-18 stsp */
209 5e91dae4 2022-08-30 stsp err = got_object_qid_alloc(&new, &qid->id);
210 9ca9aafb 2021-06-18 stsp if (err) {
211 9ca9aafb 2021-06-18 stsp got_object_id_queue_free(dest);
212 9ca9aafb 2021-06-18 stsp return err;
213 9ca9aafb 2021-06-18 stsp }
214 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(dest, new, entry);
215 dbc6a6b6 2018-07-12 stsp }
216 dbc6a6b6 2018-07-12 stsp
217 dbc6a6b6 2018-07-12 stsp return NULL;
218 9f2369b0 2018-12-24 stsp }
219 9f2369b0 2018-12-24 stsp
220 56e0773d 2019-11-28 stsp int
221 56e0773d 2019-11-28 stsp got_object_tree_get_nentries(struct got_tree_object *tree)
222 883f0469 2018-06-23 stsp {
223 56e0773d 2019-11-28 stsp return tree->nentries;
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_first_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, 0);
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_last_entry(struct got_tree_object *tree)
234 56e0773d 2019-11-28 stsp {
235 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, tree->nentries - 1);
236 56e0773d 2019-11-28 stsp }
237 56e0773d 2019-11-28 stsp
238 56e0773d 2019-11-28 stsp struct got_tree_entry *
239 56e0773d 2019-11-28 stsp got_object_tree_get_entry(struct got_tree_object *tree, int i)
240 56e0773d 2019-11-28 stsp {
241 56e0773d 2019-11-28 stsp if (i < 0 || i >= tree->nentries)
242 56e0773d 2019-11-28 stsp return NULL;
243 56e0773d 2019-11-28 stsp return &tree->entries[i];
244 883f0469 2018-06-23 stsp }
245 3840f4c9 2018-09-12 stsp
246 56e0773d 2019-11-28 stsp mode_t
247 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(struct got_tree_entry *te)
248 56e0773d 2019-11-28 stsp {
249 56e0773d 2019-11-28 stsp return te->mode;
250 56e0773d 2019-11-28 stsp }
251 56e0773d 2019-11-28 stsp
252 56e0773d 2019-11-28 stsp const char *
253 56e0773d 2019-11-28 stsp got_tree_entry_get_name(struct got_tree_entry *te)
254 56e0773d 2019-11-28 stsp {
255 56e0773d 2019-11-28 stsp return &te->name[0];
256 56e0773d 2019-11-28 stsp }
257 56e0773d 2019-11-28 stsp
258 56e0773d 2019-11-28 stsp struct got_object_id *
259 56e0773d 2019-11-28 stsp got_tree_entry_get_id(struct got_tree_entry *te)
260 56e0773d 2019-11-28 stsp {
261 56e0773d 2019-11-28 stsp return &te->id;
262 0d6c6ee3 2020-05-20 stsp }
263 0d6c6ee3 2020-05-20 stsp
264 0d6c6ee3 2020-05-20 stsp const struct got_error *
265 af57b12a 2020-07-23 stsp got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
266 0d6c6ee3 2020-05-20 stsp {
267 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
268 32596e16 2020-07-23 stsp size_t len, totlen, hdrlen, offset;
269 aa092692 2020-07-23 stsp
270 aa092692 2020-07-23 stsp *s = NULL;
271 0d6c6ee3 2020-05-20 stsp
272 659dc16e 2020-07-23 stsp hdrlen = got_object_blob_get_hdrlen(blob);
273 659dc16e 2020-07-23 stsp totlen = 0;
274 32596e16 2020-07-23 stsp offset = 0;
275 659dc16e 2020-07-23 stsp do {
276 659dc16e 2020-07-23 stsp char *p;
277 0d6c6ee3 2020-05-20 stsp
278 659dc16e 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
279 659dc16e 2020-07-23 stsp if (err)
280 af57b12a 2020-07-23 stsp return err;
281 659dc16e 2020-07-23 stsp
282 659dc16e 2020-07-23 stsp if (len == 0)
283 659dc16e 2020-07-23 stsp break;
284 659dc16e 2020-07-23 stsp
285 659dc16e 2020-07-23 stsp totlen += len - hdrlen;
286 af57b12a 2020-07-23 stsp p = realloc(*s, totlen + 1);
287 659dc16e 2020-07-23 stsp if (p == NULL) {
288 659dc16e 2020-07-23 stsp err = got_error_from_errno("realloc");
289 af57b12a 2020-07-23 stsp free(*s);
290 af57b12a 2020-07-23 stsp *s = NULL;
291 af57b12a 2020-07-23 stsp return err;
292 659dc16e 2020-07-23 stsp }
293 af57b12a 2020-07-23 stsp *s = p;
294 659dc16e 2020-07-23 stsp /* Skip blob object header first time around. */
295 af57b12a 2020-07-23 stsp memcpy(*s + offset,
296 f8f7c882 2020-07-23 stsp got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
297 659dc16e 2020-07-23 stsp hdrlen = 0;
298 32596e16 2020-07-23 stsp offset = totlen;
299 659dc16e 2020-07-23 stsp } while (len > 0);
300 af57b12a 2020-07-23 stsp
301 af57b12a 2020-07-23 stsp (*s)[totlen] = '\0';
302 af57b12a 2020-07-23 stsp return NULL;
303 af57b12a 2020-07-23 stsp }
304 af57b12a 2020-07-23 stsp
305 af57b12a 2020-07-23 stsp const struct got_error *
306 af57b12a 2020-07-23 stsp got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
307 af57b12a 2020-07-23 stsp struct got_repository *repo)
308 af57b12a 2020-07-23 stsp {
309 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
310 af57b12a 2020-07-23 stsp struct got_blob_object *blob = NULL;
311 eb81bc23 2022-06-28 tracey int fd = -1;
312 af57b12a 2020-07-23 stsp
313 af57b12a 2020-07-23 stsp *link_target = NULL;
314 af57b12a 2020-07-23 stsp
315 af57b12a 2020-07-23 stsp if (!got_object_tree_entry_is_symlink(te))
316 af57b12a 2020-07-23 stsp return got_error(GOT_ERR_TREE_ENTRY_TYPE);
317 af57b12a 2020-07-23 stsp
318 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
319 eb81bc23 2022-06-28 tracey if (fd == -1) {
320 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
321 eb81bc23 2022-06-28 tracey goto done;
322 eb81bc23 2022-06-28 tracey }
323 eb81bc23 2022-06-28 tracey
324 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo,
325 eb81bc23 2022-06-28 tracey got_tree_entry_get_id(te), PATH_MAX, fd);
326 af57b12a 2020-07-23 stsp if (err)
327 eb81bc23 2022-06-28 tracey goto done;
328 af57b12a 2020-07-23 stsp
329 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(link_target, blob);
330 eb81bc23 2022-06-28 tracey done:
331 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
332 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
333 eb81bc23 2022-06-28 tracey if (blob)
334 eb81bc23 2022-06-28 tracey got_object_blob_close(blob);
335 659dc16e 2020-07-23 stsp if (err) {
336 659dc16e 2020-07-23 stsp free(*link_target);
337 659dc16e 2020-07-23 stsp *link_target = NULL;
338 659dc16e 2020-07-23 stsp }
339 0d6c6ee3 2020-05-20 stsp return err;
340 56e0773d 2019-11-28 stsp }
341 56e0773d 2019-11-28 stsp
342 56e0773d 2019-11-28 stsp int
343 56e0773d 2019-11-28 stsp got_tree_entry_get_index(struct got_tree_entry *te)
344 56e0773d 2019-11-28 stsp {
345 56e0773d 2019-11-28 stsp return te->idx;
346 56e0773d 2019-11-28 stsp }
347 56e0773d 2019-11-28 stsp
348 56e0773d 2019-11-28 stsp struct got_tree_entry *
349 56e0773d 2019-11-28 stsp got_tree_entry_get_next(struct got_tree_object *tree,
350 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
351 56e0773d 2019-11-28 stsp {
352 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx + 1);
353 56e0773d 2019-11-28 stsp }
354 56e0773d 2019-11-28 stsp
355 56e0773d 2019-11-28 stsp struct got_tree_entry *
356 56e0773d 2019-11-28 stsp got_tree_entry_get_prev(struct got_tree_object *tree,
357 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
358 56e0773d 2019-11-28 stsp {
359 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx - 1);
360 56e0773d 2019-11-28 stsp }
361 56e0773d 2019-11-28 stsp
362 a19581a2 2018-06-21 stsp const struct got_error *
363 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
364 68482ea3 2017-11-27 stsp {
365 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
366 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
367 56b63ca4 2021-01-22 stsp if (blob->f && fclose(blob->f) == EOF)
368 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
369 ac544f8c 2019-01-13 stsp free(blob->data);
370 68482ea3 2017-11-27 stsp free(blob);
371 fb43ecf1 2019-02-11 stsp return err;
372 f934cf2c 2018-02-12 stsp }
373 f934cf2c 2018-02-12 stsp
374 8ba819a3 2020-07-23 stsp void
375 8ba819a3 2020-07-23 stsp got_object_blob_rewind(struct got_blob_object *blob)
376 8ba819a3 2020-07-23 stsp {
377 8ba819a3 2020-07-23 stsp if (blob->f)
378 8ba819a3 2020-07-23 stsp rewind(blob->f);
379 8ba819a3 2020-07-23 stsp }
380 8ba819a3 2020-07-23 stsp
381 f934cf2c 2018-02-12 stsp char *
382 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
383 f934cf2c 2018-02-12 stsp {
384 bbc740ac 2023-02-04 op return got_object_id_hex(&blob->id, buf, size);
385 f934cf2c 2018-02-12 stsp }
386 f934cf2c 2018-02-12 stsp
387 f934cf2c 2018-02-12 stsp size_t
388 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
389 f934cf2c 2018-02-12 stsp {
390 f934cf2c 2018-02-12 stsp return blob->hdrlen;
391 68482ea3 2017-11-27 stsp }
392 68482ea3 2017-11-27 stsp
393 f934cf2c 2018-02-12 stsp const uint8_t *
394 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
395 f934cf2c 2018-02-12 stsp {
396 f934cf2c 2018-02-12 stsp return blob->read_buf;
397 f934cf2c 2018-02-12 stsp }
398 f934cf2c 2018-02-12 stsp
399 68482ea3 2017-11-27 stsp const struct got_error *
400 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
401 68482ea3 2017-11-27 stsp {
402 eb651edf 2018-02-11 stsp size_t n;
403 eb651edf 2018-02-11 stsp
404 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
405 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
406 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
407 eb651edf 2018-02-11 stsp *outlenp = n;
408 35e9ba5d 2018-06-21 stsp return NULL;
409 35e9ba5d 2018-06-21 stsp }
410 35e9ba5d 2018-06-21 stsp
411 35e9ba5d 2018-06-21 stsp const struct got_error *
412 0d569390 2023-01-04 op got_object_blob_is_binary(int *binary, struct got_blob_object *blob)
413 0d569390 2023-01-04 op {
414 0d569390 2023-01-04 op const struct got_error *err;
415 0d569390 2023-01-04 op size_t hdrlen, len;
416 0d569390 2023-01-04 op
417 0d569390 2023-01-04 op *binary = 0;
418 0d569390 2023-01-04 op hdrlen = got_object_blob_get_hdrlen(blob);
419 0d569390 2023-01-04 op
420 0d569390 2023-01-04 op if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
421 0d569390 2023-01-04 op return got_error_from_errno("fseeko");
422 0d569390 2023-01-04 op
423 0d569390 2023-01-04 op err = got_object_blob_read_block(&len, blob);
424 0d569390 2023-01-04 op if (err)
425 0d569390 2023-01-04 op return err;
426 0d569390 2023-01-04 op
427 0d569390 2023-01-04 op *binary = memchr(blob->read_buf, '\0', len) != NULL;
428 0d569390 2023-01-04 op
429 0d569390 2023-01-04 op if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
430 0d569390 2023-01-04 op return got_error_from_errno("fseeko");
431 389a68d8 2023-01-05 op return NULL;
432 389a68d8 2023-01-05 op }
433 389a68d8 2023-01-05 op
434 389a68d8 2023-01-05 op const struct got_error *
435 389a68d8 2023-01-05 op got_object_blob_getline(char **line, ssize_t *linelen, size_t *linesize,
436 389a68d8 2023-01-05 op struct got_blob_object *blob)
437 389a68d8 2023-01-05 op {
438 389a68d8 2023-01-05 op *linelen = getline(line, linesize, blob->f);
439 389a68d8 2023-01-05 op if (*linelen == -1 && !feof(blob->f))
440 389a68d8 2023-01-05 op return got_error_from_errno("getline");
441 0d569390 2023-01-04 op return NULL;
442 0d569390 2023-01-04 op }
443 0d569390 2023-01-04 op
444 0d569390 2023-01-04 op const struct got_error *
445 be659d10 2020-11-18 stsp got_object_blob_dump_to_file(off_t *filesize, int *nlines,
446 6c4c42e0 2019-06-24 stsp off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
447 35e9ba5d 2018-06-21 stsp {
448 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
449 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
450 84451b3e 2018-07-10 stsp const uint8_t *buf;
451 84451b3e 2018-07-10 stsp int i;
452 c33ebc60 2020-11-18 stsp const int alloc_chunksz = 512;
453 c33ebc60 2020-11-18 stsp size_t nalloc = 0;
454 f595d9bd 2019-08-14 stsp off_t off = 0, total_len = 0;
455 84451b3e 2018-07-10 stsp
456 6c4c42e0 2019-06-24 stsp if (line_offsets)
457 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
458 f595d9bd 2019-08-14 stsp if (filesize)
459 f595d9bd 2019-08-14 stsp *filesize = 0;
460 84451b3e 2018-07-10 stsp if (nlines)
461 84451b3e 2018-07-10 stsp *nlines = 0;
462 35e9ba5d 2018-06-21 stsp
463 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
464 35e9ba5d 2018-06-21 stsp do {
465 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
466 35e9ba5d 2018-06-21 stsp if (err)
467 35e9ba5d 2018-06-21 stsp return err;
468 35e9ba5d 2018-06-21 stsp if (len == 0)
469 35e9ba5d 2018-06-21 stsp break;
470 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
471 b02560ec 2019-08-19 stsp i = hdrlen;
472 f1cbc3bc 2020-11-18 stsp if (nlines) {
473 f1cbc3bc 2020-11-18 stsp if (line_offsets && *line_offsets == NULL) {
474 78695fb7 2019-08-12 stsp /* Have some data but perhaps no '\n'. */
475 78695fb7 2019-08-12 stsp *nlines = 1;
476 c33ebc60 2020-11-18 stsp nalloc = alloc_chunksz;
477 c33ebc60 2020-11-18 stsp *line_offsets = calloc(nalloc,
478 c33ebc60 2020-11-18 stsp sizeof(**line_offsets));
479 78695fb7 2019-08-12 stsp if (*line_offsets == NULL)
480 845785d4 2020-02-02 tracey return got_error_from_errno("calloc");
481 b02560ec 2019-08-19 stsp
482 b02560ec 2019-08-19 stsp /* Skip forward over end of first line. */
483 b02560ec 2019-08-19 stsp while (i < len) {
484 b02560ec 2019-08-19 stsp if (buf[i] == '\n')
485 b02560ec 2019-08-19 stsp break;
486 b02560ec 2019-08-19 stsp i++;
487 b02560ec 2019-08-19 stsp }
488 b02560ec 2019-08-19 stsp }
489 b02560ec 2019-08-19 stsp /* Scan '\n' offsets in remaining chunk of data. */
490 b02560ec 2019-08-19 stsp while (i < len) {
491 b02560ec 2019-08-19 stsp if (buf[i] != '\n') {
492 b02560ec 2019-08-19 stsp i++;
493 f595d9bd 2019-08-14 stsp continue;
494 b02560ec 2019-08-19 stsp }
495 f595d9bd 2019-08-14 stsp (*nlines)++;
496 c33ebc60 2020-11-18 stsp if (line_offsets && nalloc < *nlines) {
497 c33ebc60 2020-11-18 stsp size_t n = *nlines + alloc_chunksz;
498 78695fb7 2019-08-12 stsp off_t *o = recallocarray(*line_offsets,
499 c33ebc60 2020-11-18 stsp nalloc, n, sizeof(**line_offsets));
500 78695fb7 2019-08-12 stsp if (o == NULL) {
501 78695fb7 2019-08-12 stsp free(*line_offsets);
502 78695fb7 2019-08-12 stsp *line_offsets = NULL;
503 78695fb7 2019-08-12 stsp return got_error_from_errno(
504 78695fb7 2019-08-12 stsp "recallocarray");
505 78695fb7 2019-08-12 stsp }
506 78695fb7 2019-08-12 stsp *line_offsets = o;
507 c33ebc60 2020-11-18 stsp nalloc = n;
508 78695fb7 2019-08-12 stsp }
509 f1cbc3bc 2020-11-18 stsp if (line_offsets) {
510 f1cbc3bc 2020-11-18 stsp off = total_len + i - hdrlen + 1;
511 f1cbc3bc 2020-11-18 stsp (*line_offsets)[*nlines - 1] = off;
512 f1cbc3bc 2020-11-18 stsp }
513 b02560ec 2019-08-19 stsp i++;
514 6c4c42e0 2019-06-24 stsp }
515 84451b3e 2018-07-10 stsp }
516 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
517 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
518 b6752625 2018-12-24 stsp if (n != len - hdrlen)
519 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
520 f595d9bd 2019-08-14 stsp total_len += len - hdrlen;
521 35e9ba5d 2018-06-21 stsp hdrlen = 0;
522 35e9ba5d 2018-06-21 stsp } while (len != 0);
523 35e9ba5d 2018-06-21 stsp
524 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
525 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
526 35e9ba5d 2018-06-21 stsp rewind(outfile);
527 35e9ba5d 2018-06-21 stsp
528 f595d9bd 2019-08-14 stsp if (filesize)
529 f595d9bd 2019-08-14 stsp *filesize = total_len;
530 f595d9bd 2019-08-14 stsp
531 776d4d29 2018-06-17 stsp return NULL;
532 f4a881ce 2018-11-17 stsp }
533 f4a881ce 2018-11-17 stsp
534 d24820bf 2019-08-11 stsp const char *
535 d24820bf 2019-08-11 stsp got_object_tag_get_name(struct got_tag_object *tag)
536 d24820bf 2019-08-11 stsp {
537 d24820bf 2019-08-11 stsp return tag->tag;
538 0bd18d37 2019-02-01 stsp }
539 0bd18d37 2019-02-01 stsp
540 0bd18d37 2019-02-01 stsp int
541 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
542 0bd18d37 2019-02-01 stsp {
543 0bd18d37 2019-02-01 stsp return tag->obj_type;
544 0bd18d37 2019-02-01 stsp }
545 0bd18d37 2019-02-01 stsp
546 0bd18d37 2019-02-01 stsp struct got_object_id *
547 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
548 0bd18d37 2019-02-01 stsp {
549 0bd18d37 2019-02-01 stsp return &tag->id;
550 01073a5d 2019-08-22 stsp }
551 01073a5d 2019-08-22 stsp
552 01073a5d 2019-08-22 stsp time_t
553 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_time(struct got_tag_object *tag)
554 01073a5d 2019-08-22 stsp {
555 01073a5d 2019-08-22 stsp return tag->tagger_time;
556 01073a5d 2019-08-22 stsp }
557 01073a5d 2019-08-22 stsp
558 01073a5d 2019-08-22 stsp time_t
559 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
560 01073a5d 2019-08-22 stsp {
561 01073a5d 2019-08-22 stsp return tag->tagger_gmtoff;
562 01073a5d 2019-08-22 stsp }
563 01073a5d 2019-08-22 stsp
564 01073a5d 2019-08-22 stsp const char *
565 01073a5d 2019-08-22 stsp got_object_tag_get_tagger(struct got_tag_object *tag)
566 01073a5d 2019-08-22 stsp {
567 01073a5d 2019-08-22 stsp return tag->tagger;
568 776d4d29 2018-06-17 stsp }
569 776d4d29 2018-06-17 stsp
570 01073a5d 2019-08-22 stsp const char *
571 01073a5d 2019-08-22 stsp got_object_tag_get_message(struct got_tag_object *tag)
572 01073a5d 2019-08-22 stsp {
573 01073a5d 2019-08-22 stsp return tag->tagmsg;
574 01073a5d 2019-08-22 stsp }
575 01073a5d 2019-08-22 stsp
576 776d4d29 2018-06-17 stsp static struct got_tree_entry *
577 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
578 776d4d29 2018-06-17 stsp {
579 56e0773d 2019-11-28 stsp int i;
580 776d4d29 2018-06-17 stsp
581 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
582 56e0773d 2019-11-28 stsp for (i = 0; i < tree->nentries; i++) {
583 56e0773d 2019-11-28 stsp struct got_tree_entry *te = &tree->entries[i];
584 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
585 63da309a 2018-11-07 stsp if (cmp < 0)
586 63da309a 2018-11-07 stsp continue;
587 63da309a 2018-11-07 stsp if (cmp > 0)
588 63da309a 2018-11-07 stsp break;
589 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
590 776d4d29 2018-06-17 stsp return te;
591 776d4d29 2018-06-17 stsp }
592 eb651edf 2018-02-11 stsp return NULL;
593 a129376b 2019-03-28 stsp }
594 a129376b 2019-03-28 stsp
595 56e0773d 2019-11-28 stsp struct got_tree_entry *
596 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
597 a129376b 2019-03-28 stsp {
598 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
599 776d4d29 2018-06-17 stsp }
600 776d4d29 2018-06-17 stsp
601 776d4d29 2018-06-17 stsp const struct got_error *
602 67b631c9 2021-10-10 stsp got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
603 67b631c9 2021-10-10 stsp struct got_repository *repo, struct got_tree_object *tree,
604 67b631c9 2021-10-10 stsp const char *path)
605 776d4d29 2018-06-17 stsp {
606 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
607 67b631c9 2021-10-10 stsp struct got_tree_object *subtree = NULL;
608 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
609 65a9bbe9 2018-09-15 stsp const char *seg, *s;
610 b7cd37e5 2018-11-18 stsp size_t seglen;
611 776d4d29 2018-06-17 stsp
612 27d434c2 2018-09-15 stsp *id = NULL;
613 776d4d29 2018-06-17 stsp
614 65a9bbe9 2018-09-15 stsp s = path;
615 5e54fb30 2019-05-31 stsp while (s[0] == '/')
616 5e54fb30 2019-05-31 stsp s++;
617 776d4d29 2018-06-17 stsp seg = s;
618 65a9bbe9 2018-09-15 stsp seglen = 0;
619 67b631c9 2021-10-10 stsp subtree = tree;
620 b7cd37e5 2018-11-18 stsp while (*s) {
621 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
622 776d4d29 2018-06-17 stsp
623 776d4d29 2018-06-17 stsp if (*s != '/') {
624 776d4d29 2018-06-17 stsp s++;
625 65a9bbe9 2018-09-15 stsp seglen++;
626 00530cfb 2018-06-21 stsp if (*s)
627 00530cfb 2018-06-21 stsp continue;
628 776d4d29 2018-06-17 stsp }
629 776d4d29 2018-06-17 stsp
630 67b631c9 2021-10-10 stsp te = find_entry_by_name(subtree, seg, seglen);
631 db37e2c0 2018-06-21 stsp if (te == NULL) {
632 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
633 776d4d29 2018-06-17 stsp goto done;
634 776d4d29 2018-06-17 stsp }
635 776d4d29 2018-06-17 stsp
636 b7cd37e5 2018-11-18 stsp if (*s == '\0')
637 67606321 2018-06-21 stsp break;
638 67606321 2018-06-21 stsp
639 776d4d29 2018-06-17 stsp seg = s + 1;
640 65a9bbe9 2018-09-15 stsp seglen = 0;
641 776d4d29 2018-06-17 stsp s++;
642 776d4d29 2018-06-17 stsp if (*s) {
643 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
644 56e0773d 2019-11-28 stsp &te->id);
645 db37e2c0 2018-06-21 stsp te = NULL;
646 776d4d29 2018-06-17 stsp if (err)
647 776d4d29 2018-06-17 stsp goto done;
648 67b631c9 2021-10-10 stsp if (subtree != tree)
649 67b631c9 2021-10-10 stsp got_object_tree_close(subtree);
650 67b631c9 2021-10-10 stsp subtree = next_tree;
651 776d4d29 2018-06-17 stsp }
652 776d4d29 2018-06-17 stsp }
653 776d4d29 2018-06-17 stsp
654 27d434c2 2018-09-15 stsp if (te) {
655 56e0773d 2019-11-28 stsp *id = got_object_id_dup(&te->id);
656 27d434c2 2018-09-15 stsp if (*id == NULL)
657 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
658 67b631c9 2021-10-10 stsp if (mode)
659 67b631c9 2021-10-10 stsp *mode = te->mode;
660 27d434c2 2018-09-15 stsp } else
661 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
662 67b631c9 2021-10-10 stsp done:
663 67b631c9 2021-10-10 stsp if (subtree && subtree != tree)
664 67b631c9 2021-10-10 stsp got_object_tree_close(subtree);
665 67b631c9 2021-10-10 stsp return err;
666 67b631c9 2021-10-10 stsp }
667 8e359fa0 2022-10-13 stsp
668 67b631c9 2021-10-10 stsp const struct got_error *
669 67b631c9 2021-10-10 stsp got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
670 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
671 67b631c9 2021-10-10 stsp {
672 67b631c9 2021-10-10 stsp const struct got_error *err = NULL;
673 67b631c9 2021-10-10 stsp struct got_tree_object *tree = NULL;
674 67b631c9 2021-10-10 stsp
675 67b631c9 2021-10-10 stsp *id = NULL;
676 67b631c9 2021-10-10 stsp
677 67b631c9 2021-10-10 stsp /* Handle opening of root of commit's tree. */
678 67b631c9 2021-10-10 stsp if (got_path_is_root_dir(path)) {
679 67b631c9 2021-10-10 stsp *id = got_object_id_dup(commit->tree_id);
680 67b631c9 2021-10-10 stsp if (*id == NULL)
681 67b631c9 2021-10-10 stsp err = got_error_from_errno("got_object_id_dup");
682 67b631c9 2021-10-10 stsp } else {
683 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
684 67b631c9 2021-10-10 stsp if (err)
685 67b631c9 2021-10-10 stsp goto done;
686 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(id, NULL, repo, tree, path);
687 67b631c9 2021-10-10 stsp }
688 776d4d29 2018-06-17 stsp done:
689 776d4d29 2018-06-17 stsp if (tree)
690 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
691 776d4d29 2018-06-17 stsp return err;
692 ac5f2b26 2020-05-05 stsp }
693 ac5f2b26 2020-05-05 stsp
694 ac5f2b26 2020-05-05 stsp /*
695 ac5f2b26 2020-05-05 stsp * Normalize file mode bits to avoid false positive tree entry differences
696 ac5f2b26 2020-05-05 stsp * in case tree entries have unexpected mode bits set.
697 ac5f2b26 2020-05-05 stsp */
698 ac5f2b26 2020-05-05 stsp static mode_t
699 ac5f2b26 2020-05-05 stsp normalize_mode_for_comparison(mode_t mode)
700 ac5f2b26 2020-05-05 stsp {
701 ac5f2b26 2020-05-05 stsp /*
702 ac5f2b26 2020-05-05 stsp * For directories, the only relevant bit is the IFDIR bit.
703 ac5f2b26 2020-05-05 stsp * This allows us to detect paths changing from a directory
704 ac5f2b26 2020-05-05 stsp * to a file and vice versa.
705 ac5f2b26 2020-05-05 stsp */
706 ac5f2b26 2020-05-05 stsp if (S_ISDIR(mode))
707 ac5f2b26 2020-05-05 stsp return mode & S_IFDIR;
708 40dde666 2020-07-23 stsp
709 40dde666 2020-07-23 stsp /*
710 40dde666 2020-07-23 stsp * For symlinks, the only relevant bit is the IFLNK bit.
711 40dde666 2020-07-23 stsp * This allows us to detect paths changing from a symlinks
712 40dde666 2020-07-23 stsp * to a file or directory and vice versa.
713 40dde666 2020-07-23 stsp */
714 40dde666 2020-07-23 stsp if (S_ISLNK(mode))
715 40dde666 2020-07-23 stsp return mode & S_IFLNK;
716 ac5f2b26 2020-05-05 stsp
717 ac5f2b26 2020-05-05 stsp /* For files, the only change we care about is the executable bit. */
718 ac5f2b26 2020-05-05 stsp return mode & S_IXUSR;
719 68482ea3 2017-11-27 stsp }
720 07862c20 2018-09-15 stsp
721 07862c20 2018-09-15 stsp const struct got_error *
722 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
723 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
724 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
725 07862c20 2018-09-15 stsp {
726 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
727 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
728 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
729 65a9bbe9 2018-09-15 stsp const char *seg, *s;
730 3b7f9878 2018-11-18 stsp size_t seglen;
731 07862c20 2018-09-15 stsp
732 07862c20 2018-09-15 stsp *changed = 0;
733 07862c20 2018-09-15 stsp
734 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
735 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
736 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
737 07862c20 2018-09-15 stsp
738 07862c20 2018-09-15 stsp tree1 = tree01;
739 07862c20 2018-09-15 stsp tree2 = tree02;
740 65a9bbe9 2018-09-15 stsp s = path;
741 61a7d79f 2020-02-29 stsp while (*s == '/')
742 61a7d79f 2020-02-29 stsp s++;
743 07862c20 2018-09-15 stsp seg = s;
744 65a9bbe9 2018-09-15 stsp seglen = 0;
745 3b7f9878 2018-11-18 stsp while (*s) {
746 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
747 ac5f2b26 2020-05-05 stsp mode_t mode1, mode2;
748 07862c20 2018-09-15 stsp
749 07862c20 2018-09-15 stsp if (*s != '/') {
750 07862c20 2018-09-15 stsp s++;
751 65a9bbe9 2018-09-15 stsp seglen++;
752 07862c20 2018-09-15 stsp if (*s)
753 07862c20 2018-09-15 stsp continue;
754 07862c20 2018-09-15 stsp }
755 07862c20 2018-09-15 stsp
756 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
757 07862c20 2018-09-15 stsp if (te1 == NULL) {
758 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
759 07862c20 2018-09-15 stsp goto done;
760 07862c20 2018-09-15 stsp }
761 07862c20 2018-09-15 stsp
762 e8bfb8f3 2020-12-18 stsp if (tree2)
763 e8bfb8f3 2020-12-18 stsp te2 = find_entry_by_name(tree2, seg, seglen);
764 07862c20 2018-09-15 stsp
765 e8bfb8f3 2020-12-18 stsp if (te2) {
766 e8bfb8f3 2020-12-18 stsp mode1 = normalize_mode_for_comparison(te1->mode);
767 e8bfb8f3 2020-12-18 stsp mode2 = normalize_mode_for_comparison(te2->mode);
768 e8bfb8f3 2020-12-18 stsp if (mode1 != mode2) {
769 e8bfb8f3 2020-12-18 stsp *changed = 1;
770 e8bfb8f3 2020-12-18 stsp goto done;
771 e8bfb8f3 2020-12-18 stsp }
772 e8bfb8f3 2020-12-18 stsp
773 e8bfb8f3 2020-12-18 stsp if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
774 e8bfb8f3 2020-12-18 stsp *changed = 0;
775 e8bfb8f3 2020-12-18 stsp goto done;
776 e8bfb8f3 2020-12-18 stsp }
777 07862c20 2018-09-15 stsp }
778 07862c20 2018-09-15 stsp
779 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
780 07862c20 2018-09-15 stsp *changed = 1;
781 07862c20 2018-09-15 stsp goto done;
782 07862c20 2018-09-15 stsp }
783 07862c20 2018-09-15 stsp
784 07862c20 2018-09-15 stsp seg = s + 1;
785 07862c20 2018-09-15 stsp s++;
786 65a9bbe9 2018-09-15 stsp seglen = 0;
787 07862c20 2018-09-15 stsp if (*s) {
788 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
789 56e0773d 2019-11-28 stsp &te1->id);
790 07862c20 2018-09-15 stsp te1 = NULL;
791 07862c20 2018-09-15 stsp if (err)
792 07862c20 2018-09-15 stsp goto done;
793 a31cea73 2018-09-15 stsp if (tree1 != tree01)
794 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
795 07862c20 2018-09-15 stsp tree1 = next_tree1;
796 07862c20 2018-09-15 stsp
797 e8bfb8f3 2020-12-18 stsp if (te2) {
798 e8bfb8f3 2020-12-18 stsp err = got_object_open_as_tree(&next_tree2, repo,
799 e8bfb8f3 2020-12-18 stsp &te2->id);
800 e8bfb8f3 2020-12-18 stsp te2 = NULL;
801 e8bfb8f3 2020-12-18 stsp if (err)
802 e8bfb8f3 2020-12-18 stsp goto done;
803 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
804 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
805 e8bfb8f3 2020-12-18 stsp tree2 = next_tree2;
806 e8bfb8f3 2020-12-18 stsp } else if (tree2) {
807 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
808 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
809 e8bfb8f3 2020-12-18 stsp tree2 = NULL;
810 e8bfb8f3 2020-12-18 stsp }
811 07862c20 2018-09-15 stsp }
812 07862c20 2018-09-15 stsp }
813 07862c20 2018-09-15 stsp done:
814 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
815 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
816 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
817 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
818 77880158 2018-11-04 stsp return err;
819 77880158 2018-11-04 stsp }
820 ed175427 2019-05-09 stsp
821 ed175427 2019-05-09 stsp const struct got_error *
822 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
823 ed175427 2019-05-09 stsp struct got_tree_entry *te)
824 ed175427 2019-05-09 stsp {
825 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
826 ed175427 2019-05-09 stsp
827 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
828 ed175427 2019-05-09 stsp if (*new_te == NULL)
829 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
830 ed175427 2019-05-09 stsp
831 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
832 56e0773d 2019-11-28 stsp memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
833 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
834 8c4eabf2 2019-05-10 stsp return err;
835 63c5ca5d 2019-08-24 stsp }
836 63c5ca5d 2019-08-24 stsp
837 63c5ca5d 2019-08-24 stsp int
838 56e0773d 2019-11-28 stsp got_object_tree_entry_is_submodule(struct got_tree_entry *te)
839 63c5ca5d 2019-08-24 stsp {
840 63c5ca5d 2019-08-24 stsp return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
841 e40622f4 2020-07-23 stsp }
842 e40622f4 2020-07-23 stsp
843 e40622f4 2020-07-23 stsp int
844 e40622f4 2020-07-23 stsp got_object_tree_entry_is_symlink(struct got_tree_entry *te)
845 e40622f4 2020-07-23 stsp {
846 e40622f4 2020-07-23 stsp /* S_IFDIR check avoids confusing symlinks with submodules. */
847 e40622f4 2020-07-23 stsp return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
848 e40622f4 2020-07-23 stsp }
849 e40622f4 2020-07-23 stsp
850 e40622f4 2020-07-23 stsp static const struct got_error *
851 e40622f4 2020-07-23 stsp resolve_symlink(char **link_target, const char *path,
852 a44927cc 2022-04-07 stsp struct got_commit_object *commit, struct got_repository *repo)
853 e40622f4 2020-07-23 stsp {
854 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
855 dbdd6209 2020-10-19 stsp char buf[PATH_MAX];
856 e40622f4 2020-07-23 stsp char *name, *parent_path = NULL;
857 e40622f4 2020-07-23 stsp struct got_object_id *tree_obj_id = NULL;
858 e40622f4 2020-07-23 stsp struct got_tree_object *tree = NULL;
859 e40622f4 2020-07-23 stsp struct got_tree_entry *te = NULL;
860 e40622f4 2020-07-23 stsp
861 e40622f4 2020-07-23 stsp *link_target = NULL;
862 559d127c 2020-07-23 stsp
863 dbdd6209 2020-10-19 stsp if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
864 dbdd6209 2020-10-19 stsp return got_error(GOT_ERR_NO_SPACE);
865 dbdd6209 2020-10-19 stsp
866 dbdd6209 2020-10-19 stsp name = basename(buf);
867 e40622f4 2020-07-23 stsp if (name == NULL)
868 e40622f4 2020-07-23 stsp return got_error_from_errno2("basename", path);
869 e40622f4 2020-07-23 stsp
870 e40622f4 2020-07-23 stsp err = got_path_dirname(&parent_path, path);
871 e40622f4 2020-07-23 stsp if (err)
872 e40622f4 2020-07-23 stsp return err;
873 e40622f4 2020-07-23 stsp
874 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_obj_id, repo, commit,
875 e40622f4 2020-07-23 stsp parent_path);
876 e40622f4 2020-07-23 stsp if (err) {
877 e40622f4 2020-07-23 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY) {
878 e40622f4 2020-07-23 stsp /* Display the complete path in error message. */
879 e40622f4 2020-07-23 stsp err = got_error_path(path, err->code);
880 e40622f4 2020-07-23 stsp }
881 e40622f4 2020-07-23 stsp goto done;
882 e40622f4 2020-07-23 stsp }
883 e40622f4 2020-07-23 stsp
884 e40622f4 2020-07-23 stsp err = got_object_open_as_tree(&tree, repo, tree_obj_id);
885 e40622f4 2020-07-23 stsp if (err)
886 e40622f4 2020-07-23 stsp goto done;
887 e40622f4 2020-07-23 stsp
888 e40622f4 2020-07-23 stsp te = got_object_tree_find_entry(tree, name);
889 e40622f4 2020-07-23 stsp if (te == NULL) {
890 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
891 e40622f4 2020-07-23 stsp goto done;
892 e40622f4 2020-07-23 stsp }
893 e40622f4 2020-07-23 stsp
894 e40622f4 2020-07-23 stsp if (got_object_tree_entry_is_symlink(te)) {
895 e40622f4 2020-07-23 stsp err = got_tree_entry_get_symlink_target(link_target, te, repo);
896 e40622f4 2020-07-23 stsp if (err)
897 e40622f4 2020-07-23 stsp goto done;
898 e40622f4 2020-07-23 stsp if (!got_path_is_absolute(*link_target)) {
899 e40622f4 2020-07-23 stsp char *abspath;
900 e40622f4 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent_path,
901 e40622f4 2020-07-23 stsp *link_target) == -1) {
902 e40622f4 2020-07-23 stsp err = got_error_from_errno("asprintf");
903 e40622f4 2020-07-23 stsp goto done;
904 e40622f4 2020-07-23 stsp }
905 e40622f4 2020-07-23 stsp free(*link_target);
906 e40622f4 2020-07-23 stsp *link_target = malloc(PATH_MAX);
907 e40622f4 2020-07-23 stsp if (*link_target == NULL) {
908 e40622f4 2020-07-23 stsp err = got_error_from_errno("malloc");
909 e40622f4 2020-07-23 stsp goto done;
910 e40622f4 2020-07-23 stsp }
911 e40622f4 2020-07-23 stsp err = got_canonpath(abspath, *link_target, PATH_MAX);
912 e40622f4 2020-07-23 stsp free(abspath);
913 e40622f4 2020-07-23 stsp if (err)
914 e40622f4 2020-07-23 stsp goto done;
915 e40622f4 2020-07-23 stsp }
916 e40622f4 2020-07-23 stsp }
917 e40622f4 2020-07-23 stsp done:
918 b68bd9d2 2022-09-03 op free(parent_path);
919 e40622f4 2020-07-23 stsp free(tree_obj_id);
920 e40622f4 2020-07-23 stsp if (tree)
921 e40622f4 2020-07-23 stsp got_object_tree_close(tree);
922 e40622f4 2020-07-23 stsp if (err) {
923 e40622f4 2020-07-23 stsp free(*link_target);
924 e40622f4 2020-07-23 stsp *link_target = NULL;
925 e40622f4 2020-07-23 stsp }
926 e40622f4 2020-07-23 stsp return err;
927 ca6e02ac 2020-01-07 stsp }
928 ca6e02ac 2020-01-07 stsp
929 ca6e02ac 2020-01-07 stsp const struct got_error *
930 e40622f4 2020-07-23 stsp got_object_resolve_symlinks(char **link_target, const char *path,
931 a44927cc 2022-04-07 stsp struct got_commit_object *commit, struct got_repository *repo)
932 e40622f4 2020-07-23 stsp {
933 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
934 e40622f4 2020-07-23 stsp char *next_target = NULL;
935 e40622f4 2020-07-23 stsp int max_recursion = 40; /* matches Git */
936 e40622f4 2020-07-23 stsp
937 e40622f4 2020-07-23 stsp *link_target = NULL;
938 e40622f4 2020-07-23 stsp
939 e40622f4 2020-07-23 stsp do {
940 e40622f4 2020-07-23 stsp err = resolve_symlink(&next_target,
941 a44927cc 2022-04-07 stsp *link_target ? *link_target : path, commit, repo);
942 e40622f4 2020-07-23 stsp if (err)
943 e40622f4 2020-07-23 stsp break;
944 e40622f4 2020-07-23 stsp if (next_target) {
945 e40622f4 2020-07-23 stsp free(*link_target);
946 e40622f4 2020-07-23 stsp if (--max_recursion == 0) {
947 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_RECURSION);
948 e40622f4 2020-07-23 stsp *link_target = NULL;
949 e40622f4 2020-07-23 stsp break;
950 e40622f4 2020-07-23 stsp }
951 e40622f4 2020-07-23 stsp *link_target = next_target;
952 e40622f4 2020-07-23 stsp }
953 e40622f4 2020-07-23 stsp } while (next_target);
954 e40622f4 2020-07-23 stsp
955 e40622f4 2020-07-23 stsp return err;
956 e40622f4 2020-07-23 stsp }
957 ca6e02ac 2020-01-07 stsp
958 34a842a4 2022-09-18 mark void
959 34a842a4 2022-09-18 mark got_object_commit_retain(struct got_commit_object *commit)
960 568eae95 2022-09-11 mark {
961 568eae95 2022-09-11 mark commit->refcnt++;
962 568eae95 2022-09-11 mark }
963 13b2bc37 2022-10-23 stsp
964 13b2bc37 2022-10-23 stsp const struct got_error *
965 13b2bc37 2022-10-23 stsp got_object_raw_alloc(struct got_raw_object **obj, uint8_t *outbuf, int *outfd,
966 60c140ae 2023-01-09 stsp size_t max_in_mem_size, size_t hdrlen, off_t size)
967 13b2bc37 2022-10-23 stsp {
968 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
969 1c28a361 2022-10-25 op off_t tot;
970 13b2bc37 2022-10-23 stsp
971 1c28a361 2022-10-25 op tot = hdrlen + size;
972 1c28a361 2022-10-25 op
973 13b2bc37 2022-10-23 stsp *obj = calloc(1, sizeof(**obj));
974 13b2bc37 2022-10-23 stsp if (*obj == NULL) {
975 13b2bc37 2022-10-23 stsp err = got_error_from_errno("calloc");
976 13b2bc37 2022-10-23 stsp goto done;
977 13b2bc37 2022-10-23 stsp }
978 13b2bc37 2022-10-23 stsp (*obj)->fd = -1;
979 13b2bc37 2022-10-23 stsp (*obj)->tempfile_idx = -1;
980 13b2bc37 2022-10-23 stsp
981 13b2bc37 2022-10-23 stsp if (outbuf) {
982 13b2bc37 2022-10-23 stsp (*obj)->data = outbuf;
983 13b2bc37 2022-10-23 stsp } else {
984 13b2bc37 2022-10-23 stsp struct stat sb;
985 13b2bc37 2022-10-23 stsp if (fstat(*outfd, &sb) == -1) {
986 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fstat");
987 13b2bc37 2022-10-23 stsp goto done;
988 13b2bc37 2022-10-23 stsp }
989 13b2bc37 2022-10-23 stsp
990 1c28a361 2022-10-25 op if (sb.st_size != tot) {
991 e98a81e6 2023-01-09 stsp err = got_error_msg(GOT_ERR_BAD_OBJ_HDR,
992 e98a81e6 2023-01-09 stsp "raw object has unexpected size");
993 13b2bc37 2022-10-23 stsp goto done;
994 13b2bc37 2022-10-23 stsp }
995 13b2bc37 2022-10-23 stsp #ifndef GOT_PACK_NO_MMAP
996 60c140ae 2023-01-09 stsp if (tot > 0 && tot <= max_in_mem_size) {
997 1c28a361 2022-10-25 op (*obj)->data = mmap(NULL, tot, PROT_READ,
998 13b2bc37 2022-10-23 stsp MAP_PRIVATE, *outfd, 0);
999 13b2bc37 2022-10-23 stsp if ((*obj)->data == MAP_FAILED) {
1000 13b2bc37 2022-10-23 stsp if (errno != ENOMEM) {
1001 13b2bc37 2022-10-23 stsp err = got_error_from_errno("mmap");
1002 13b2bc37 2022-10-23 stsp goto done;
1003 13b2bc37 2022-10-23 stsp }
1004 13b2bc37 2022-10-23 stsp (*obj)->data = NULL;
1005 13b2bc37 2022-10-23 stsp } else {
1006 13b2bc37 2022-10-23 stsp (*obj)->fd = *outfd;
1007 13b2bc37 2022-10-23 stsp *outfd = -1;
1008 13b2bc37 2022-10-23 stsp }
1009 13b2bc37 2022-10-23 stsp }
1010 13b2bc37 2022-10-23 stsp #endif
1011 13b2bc37 2022-10-23 stsp if (*outfd != -1) {
1012 13b2bc37 2022-10-23 stsp (*obj)->f = fdopen(*outfd, "r");
1013 13b2bc37 2022-10-23 stsp if ((*obj)->f == NULL) {
1014 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fdopen");
1015 13b2bc37 2022-10-23 stsp goto done;
1016 13b2bc37 2022-10-23 stsp }
1017 13b2bc37 2022-10-23 stsp *outfd = -1;
1018 13b2bc37 2022-10-23 stsp }
1019 13b2bc37 2022-10-23 stsp }
1020 13b2bc37 2022-10-23 stsp (*obj)->hdrlen = hdrlen;
1021 13b2bc37 2022-10-23 stsp (*obj)->size = size;
1022 13b2bc37 2022-10-23 stsp done:
1023 13b2bc37 2022-10-23 stsp if (err) {
1024 13b2bc37 2022-10-23 stsp if (*obj) {
1025 13b2bc37 2022-10-23 stsp got_object_raw_close(*obj);
1026 13b2bc37 2022-10-23 stsp *obj = NULL;
1027 13b2bc37 2022-10-23 stsp }
1028 13b2bc37 2022-10-23 stsp } else
1029 13b2bc37 2022-10-23 stsp (*obj)->refcnt++;
1030 13b2bc37 2022-10-23 stsp return err;
1031 13b2bc37 2022-10-23 stsp }