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