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