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