2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 2178c42e 2018-04-22 stsp #include <sys/types.h>
18 0ffeb3c2 2017-11-26 stsp #include <sys/stat.h>
19 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
20 2178c42e 2018-04-22 stsp #include <sys/uio.h>
21 2b0ae357 2022-01-10 thomas #include <sys/mman.h>
23 a1fd68d8 2018-01-12 stsp #include <errno.h>
24 2178c42e 2018-04-22 stsp #include <fcntl.h>
25 d71d75ad 2017-11-05 stsp #include <stdio.h>
26 ab9a70b2 2017-11-06 stsp #include <stdlib.h>
27 ab9a70b2 2017-11-06 stsp #include <string.h>
28 81a12da5 2020-09-09 naddy #include <unistd.h>
29 ab9a70b2 2017-11-06 stsp #include <zlib.h>
30 e40622f4 2020-07-23 stsp #include <libgen.h>
31 ab9a70b2 2017-11-06 stsp #include <limits.h>
32 b61ceafc 2022-10-13 thomas #include <imsg.h>
34 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
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"
42 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
43 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
44 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
45 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
46 6bef87be 2018-09-11 stsp #include "got_lib_object_idcache.h"
47 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
48 ad242220 2018-09-08 stsp #include "got_lib_object_parse.h"
49 15a94983 2018-12-23 stsp #include "got_lib_pack.h"
50 7bb0daa1 2018-06-21 stsp #include "got_lib_repository.h"
53 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
56 63915ee5 2022-06-23 thomas #ifndef nitems
57 63915ee5 2022-06-23 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
60 3235492e 2018-04-01 stsp struct got_object_id *
61 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
63 6402fb3c 2018-09-15 stsp return &obj->id;
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)
69 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
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)
76 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
77 15a94983 2018-12-23 stsp struct got_object *obj;
79 15a94983 2018-12-23 stsp err = got_object_open(&obj, repo, id);
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;
91 15a94983 2018-12-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
95 15a94983 2018-12-23 stsp got_object_close(obj);
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)
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;
107 e6b1056e 2018-04-22 stsp *path = NULL;
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");
113 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
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");
123 d1cda826 2017-11-06 stsp free(path_objects);
124 d1cda826 2017-11-06 stsp return err;
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)
131 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
132 a1fd68d8 2018-01-12 stsp char *path;
134 90bdb554 2019-04-11 stsp err = got_object_get_path(&path, id, repo);
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);
143 4558fcd4 2018-01-14 stsp free(path);
144 2090a03d 2018-09-09 stsp return err;
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)
151 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
153 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
154 6dd1ece6 2019-11-10 stsp return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
156 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
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)
163 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
164 15a94983 2018-12-23 stsp struct got_object *obj;
166 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
168 15a94983 2018-12-23 stsp return err;
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");
175 15a94983 2018-12-23 stsp return NULL;
178 e32baab7 2018-11-05 stsp const struct got_error *
179 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
181 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
182 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
183 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
185 ec242592 2022-04-22 thomas memcpy(&(*qid)->id, id, sizeof((*qid)->id));
186 9ca9aafb 2021-06-18 stsp return NULL;
189 9ca9aafb 2021-06-18 stsp const struct got_error *
190 9ca9aafb 2021-06-18 stsp got_object_id_queue_copy(const struct got_object_id_queue *src,
191 9ca9aafb 2021-06-18 stsp struct got_object_id_queue *dest)
193 9ca9aafb 2021-06-18 stsp const struct got_error *err;
194 9ca9aafb 2021-06-18 stsp struct got_object_qid *qid;
196 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, src, entry) {
197 9ca9aafb 2021-06-18 stsp struct got_object_qid *new;
199 9ca9aafb 2021-06-18 stsp * Deep-copy the object ID only. Let the caller deal
200 9ca9aafb 2021-06-18 stsp * with setting up the new->data pointer if needed.
202 b6b86fd1 2022-08-30 thomas err = got_object_qid_alloc(&new, &qid->id);
204 9ca9aafb 2021-06-18 stsp got_object_id_queue_free(dest);
205 9ca9aafb 2021-06-18 stsp return err;
207 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(dest, new, entry);
210 dbc6a6b6 2018-07-12 stsp return NULL;
214 56e0773d 2019-11-28 stsp got_object_tree_get_nentries(struct got_tree_object *tree)
216 56e0773d 2019-11-28 stsp return tree->nentries;
219 56e0773d 2019-11-28 stsp struct got_tree_entry *
220 56e0773d 2019-11-28 stsp got_object_tree_get_first_entry(struct got_tree_object *tree)
222 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, 0);
225 56e0773d 2019-11-28 stsp struct got_tree_entry *
226 56e0773d 2019-11-28 stsp got_object_tree_get_last_entry(struct got_tree_object *tree)
228 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, tree->nentries - 1);
231 56e0773d 2019-11-28 stsp struct got_tree_entry *
232 56e0773d 2019-11-28 stsp got_object_tree_get_entry(struct got_tree_object *tree, int i)
234 56e0773d 2019-11-28 stsp if (i < 0 || i >= tree->nentries)
235 56e0773d 2019-11-28 stsp return NULL;
236 56e0773d 2019-11-28 stsp return &tree->entries[i];
240 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(struct got_tree_entry *te)
242 56e0773d 2019-11-28 stsp return te->mode;
245 56e0773d 2019-11-28 stsp const char *
246 56e0773d 2019-11-28 stsp got_tree_entry_get_name(struct got_tree_entry *te)
248 56e0773d 2019-11-28 stsp return &te->name[0];
251 56e0773d 2019-11-28 stsp struct got_object_id *
252 56e0773d 2019-11-28 stsp got_tree_entry_get_id(struct got_tree_entry *te)
254 56e0773d 2019-11-28 stsp return &te->id;
257 0d6c6ee3 2020-05-20 stsp const struct got_error *
258 af57b12a 2020-07-23 stsp got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
260 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
261 32596e16 2020-07-23 stsp size_t len, totlen, hdrlen, offset;
265 659dc16e 2020-07-23 stsp hdrlen = got_object_blob_get_hdrlen(blob);
266 659dc16e 2020-07-23 stsp totlen = 0;
267 32596e16 2020-07-23 stsp offset = 0;
271 659dc16e 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
273 af57b12a 2020-07-23 stsp return err;
275 659dc16e 2020-07-23 stsp if (len == 0)
278 659dc16e 2020-07-23 stsp totlen += len - hdrlen;
279 af57b12a 2020-07-23 stsp p = realloc(*s, totlen + 1);
280 659dc16e 2020-07-23 stsp if (p == NULL) {
281 659dc16e 2020-07-23 stsp err = got_error_from_errno("realloc");
284 af57b12a 2020-07-23 stsp return err;
287 659dc16e 2020-07-23 stsp /* Skip blob object header first time around. */
288 af57b12a 2020-07-23 stsp memcpy(*s + offset,
289 f8f7c882 2020-07-23 stsp got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
290 659dc16e 2020-07-23 stsp hdrlen = 0;
291 32596e16 2020-07-23 stsp offset = totlen;
292 659dc16e 2020-07-23 stsp } while (len > 0);
294 af57b12a 2020-07-23 stsp (*s)[totlen] = '\0';
295 af57b12a 2020-07-23 stsp return NULL;
298 af57b12a 2020-07-23 stsp const struct got_error *
299 af57b12a 2020-07-23 stsp got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
300 af57b12a 2020-07-23 stsp struct got_repository *repo)
302 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
303 af57b12a 2020-07-23 stsp struct got_blob_object *blob = NULL;
304 f4ae6ddb 2022-07-01 thomas int fd = -1;
306 af57b12a 2020-07-23 stsp *link_target = NULL;
308 af57b12a 2020-07-23 stsp if (!got_object_tree_entry_is_symlink(te))
309 af57b12a 2020-07-23 stsp return got_error(GOT_ERR_TREE_ENTRY_TYPE);
311 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
312 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
313 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
314 f4ae6ddb 2022-07-01 thomas goto done;
317 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo,
318 f4ae6ddb 2022-07-01 thomas got_tree_entry_get_id(te), PATH_MAX, fd);
320 f4ae6ddb 2022-07-01 thomas goto done;
322 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(link_target, blob);
324 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
325 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
326 f4ae6ddb 2022-07-01 thomas if (blob)
327 f4ae6ddb 2022-07-01 thomas got_object_blob_close(blob);
329 659dc16e 2020-07-23 stsp free(*link_target);
330 659dc16e 2020-07-23 stsp *link_target = NULL;
332 0d6c6ee3 2020-05-20 stsp return err;
336 56e0773d 2019-11-28 stsp got_tree_entry_get_index(struct got_tree_entry *te)
338 56e0773d 2019-11-28 stsp return te->idx;
341 56e0773d 2019-11-28 stsp struct got_tree_entry *
342 56e0773d 2019-11-28 stsp got_tree_entry_get_next(struct got_tree_object *tree,
343 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
345 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx + 1);
348 56e0773d 2019-11-28 stsp struct got_tree_entry *
349 56e0773d 2019-11-28 stsp got_tree_entry_get_prev(struct got_tree_object *tree,
350 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
352 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx - 1);
355 a19581a2 2018-06-21 stsp const struct got_error *
356 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
358 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
359 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
360 56b63ca4 2021-01-22 stsp if (blob->f && fclose(blob->f) == EOF)
361 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
362 ac544f8c 2019-01-13 stsp free(blob->data);
363 68482ea3 2017-11-27 stsp free(blob);
364 fb43ecf1 2019-02-11 stsp return err;
368 8ba819a3 2020-07-23 stsp got_object_blob_rewind(struct got_blob_object *blob)
370 8ba819a3 2020-07-23 stsp if (blob->f)
371 8ba819a3 2020-07-23 stsp rewind(blob->f);
375 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
377 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
381 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
383 f934cf2c 2018-02-12 stsp return blob->hdrlen;
386 f934cf2c 2018-02-12 stsp const uint8_t *
387 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
389 f934cf2c 2018-02-12 stsp return blob->read_buf;
392 68482ea3 2017-11-27 stsp const struct got_error *
393 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
397 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
398 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
399 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
400 eb651edf 2018-02-11 stsp *outlenp = n;
401 35e9ba5d 2018-06-21 stsp return NULL;
404 35e9ba5d 2018-06-21 stsp const struct got_error *
405 a5f70c45 2023-01-06 thomas got_object_blob_is_binary(int *binary, struct got_blob_object *blob)
407 a5f70c45 2023-01-06 thomas const struct got_error *err;
408 a5f70c45 2023-01-06 thomas size_t hdrlen, len;
410 a5f70c45 2023-01-06 thomas *binary = 0;
411 a5f70c45 2023-01-06 thomas hdrlen = got_object_blob_get_hdrlen(blob);
413 a5f70c45 2023-01-06 thomas if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
414 a5f70c45 2023-01-06 thomas return got_error_from_errno("fseeko");
416 a5f70c45 2023-01-06 thomas err = got_object_blob_read_block(&len, blob);
418 a5f70c45 2023-01-06 thomas return err;
420 a5f70c45 2023-01-06 thomas *binary = memchr(blob->read_buf, '\0', len) != NULL;
422 a5f70c45 2023-01-06 thomas if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
423 a5f70c45 2023-01-06 thomas return got_error_from_errno("fseeko");
424 a5f70c45 2023-01-06 thomas return NULL;
427 a5f70c45 2023-01-06 thomas const struct got_error *
428 be659d10 2020-11-18 stsp got_object_blob_dump_to_file(off_t *filesize, int *nlines,
429 6c4c42e0 2019-06-24 stsp off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
431 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
432 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
433 84451b3e 2018-07-10 stsp const uint8_t *buf;
435 c33ebc60 2020-11-18 stsp const int alloc_chunksz = 512;
436 c33ebc60 2020-11-18 stsp size_t nalloc = 0;
437 f595d9bd 2019-08-14 stsp off_t off = 0, total_len = 0;
439 6c4c42e0 2019-06-24 stsp if (line_offsets)
440 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
441 f595d9bd 2019-08-14 stsp if (filesize)
442 f595d9bd 2019-08-14 stsp *filesize = 0;
443 84451b3e 2018-07-10 stsp if (nlines)
444 84451b3e 2018-07-10 stsp *nlines = 0;
446 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
448 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
450 35e9ba5d 2018-06-21 stsp return err;
451 35e9ba5d 2018-06-21 stsp if (len == 0)
453 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
454 b02560ec 2019-08-19 stsp i = hdrlen;
455 f1cbc3bc 2020-11-18 stsp if (nlines) {
456 f1cbc3bc 2020-11-18 stsp if (line_offsets && *line_offsets == NULL) {
457 78695fb7 2019-08-12 stsp /* Have some data but perhaps no '\n'. */
458 78695fb7 2019-08-12 stsp *nlines = 1;
459 c33ebc60 2020-11-18 stsp nalloc = alloc_chunksz;
460 c33ebc60 2020-11-18 stsp *line_offsets = calloc(nalloc,
461 c33ebc60 2020-11-18 stsp sizeof(**line_offsets));
462 78695fb7 2019-08-12 stsp if (*line_offsets == NULL)
463 845785d4 2020-02-02 tracey return got_error_from_errno("calloc");
465 b02560ec 2019-08-19 stsp /* Skip forward over end of first line. */
466 b02560ec 2019-08-19 stsp while (i < len) {
467 b02560ec 2019-08-19 stsp if (buf[i] == '\n')
472 b02560ec 2019-08-19 stsp /* Scan '\n' offsets in remaining chunk of data. */
473 b02560ec 2019-08-19 stsp while (i < len) {
474 b02560ec 2019-08-19 stsp if (buf[i] != '\n') {
478 f595d9bd 2019-08-14 stsp (*nlines)++;
479 c33ebc60 2020-11-18 stsp if (line_offsets && nalloc < *nlines) {
480 c33ebc60 2020-11-18 stsp size_t n = *nlines + alloc_chunksz;
481 78695fb7 2019-08-12 stsp off_t *o = recallocarray(*line_offsets,
482 c33ebc60 2020-11-18 stsp nalloc, n, sizeof(**line_offsets));
483 78695fb7 2019-08-12 stsp if (o == NULL) {
484 78695fb7 2019-08-12 stsp free(*line_offsets);
485 78695fb7 2019-08-12 stsp *line_offsets = NULL;
486 78695fb7 2019-08-12 stsp return got_error_from_errno(
487 78695fb7 2019-08-12 stsp "recallocarray");
489 78695fb7 2019-08-12 stsp *line_offsets = o;
490 c33ebc60 2020-11-18 stsp nalloc = n;
492 f1cbc3bc 2020-11-18 stsp if (line_offsets) {
493 f1cbc3bc 2020-11-18 stsp off = total_len + i - hdrlen + 1;
494 f1cbc3bc 2020-11-18 stsp (*line_offsets)[*nlines - 1] = off;
499 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
500 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
501 b6752625 2018-12-24 stsp if (n != len - hdrlen)
502 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
503 f595d9bd 2019-08-14 stsp total_len += len - hdrlen;
504 35e9ba5d 2018-06-21 stsp hdrlen = 0;
505 35e9ba5d 2018-06-21 stsp } while (len != 0);
507 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
508 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
509 35e9ba5d 2018-06-21 stsp rewind(outfile);
511 f595d9bd 2019-08-14 stsp if (filesize)
512 f595d9bd 2019-08-14 stsp *filesize = total_len;
514 776d4d29 2018-06-17 stsp return NULL;
517 d24820bf 2019-08-11 stsp const char *
518 d24820bf 2019-08-11 stsp got_object_tag_get_name(struct got_tag_object *tag)
520 d24820bf 2019-08-11 stsp return tag->tag;
524 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
526 0bd18d37 2019-02-01 stsp return tag->obj_type;
529 0bd18d37 2019-02-01 stsp struct got_object_id *
530 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
532 0bd18d37 2019-02-01 stsp return &tag->id;
536 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_time(struct got_tag_object *tag)
538 01073a5d 2019-08-22 stsp return tag->tagger_time;
542 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
544 01073a5d 2019-08-22 stsp return tag->tagger_gmtoff;
547 01073a5d 2019-08-22 stsp const char *
548 01073a5d 2019-08-22 stsp got_object_tag_get_tagger(struct got_tag_object *tag)
550 01073a5d 2019-08-22 stsp return tag->tagger;
553 01073a5d 2019-08-22 stsp const char *
554 01073a5d 2019-08-22 stsp got_object_tag_get_message(struct got_tag_object *tag)
556 01073a5d 2019-08-22 stsp return tag->tagmsg;
559 776d4d29 2018-06-17 stsp static struct got_tree_entry *
560 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
564 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
565 56e0773d 2019-11-28 stsp for (i = 0; i < tree->nentries; i++) {
566 56e0773d 2019-11-28 stsp struct got_tree_entry *te = &tree->entries[i];
567 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
568 63da309a 2018-11-07 stsp if (cmp < 0)
570 63da309a 2018-11-07 stsp if (cmp > 0)
572 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
575 eb651edf 2018-02-11 stsp return NULL;
578 56e0773d 2019-11-28 stsp struct got_tree_entry *
579 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
581 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
584 776d4d29 2018-06-17 stsp const struct got_error *
585 cc8021af 2021-10-12 thomas got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
586 cc8021af 2021-10-12 thomas struct got_repository *repo, struct got_tree_object *tree,
587 cc8021af 2021-10-12 thomas const char *path)
589 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
590 cc8021af 2021-10-12 thomas struct got_tree_object *subtree = NULL;
591 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
592 65a9bbe9 2018-09-15 stsp const char *seg, *s;
593 b7cd37e5 2018-11-18 stsp size_t seglen;
595 27d434c2 2018-09-15 stsp *id = NULL;
598 5e54fb30 2019-05-31 stsp while (s[0] == '/')
601 65a9bbe9 2018-09-15 stsp seglen = 0;
602 cc8021af 2021-10-12 thomas subtree = tree;
603 b7cd37e5 2018-11-18 stsp while (*s) {
604 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
606 776d4d29 2018-06-17 stsp if (*s != '/') {
613 cc8021af 2021-10-12 thomas te = find_entry_by_name(subtree, seg, seglen);
614 db37e2c0 2018-06-21 stsp if (te == NULL) {
615 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
619 b7cd37e5 2018-11-18 stsp if (*s == '\0')
622 776d4d29 2018-06-17 stsp seg = s + 1;
623 65a9bbe9 2018-09-15 stsp seglen = 0;
626 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
631 cc8021af 2021-10-12 thomas if (subtree != tree)
632 cc8021af 2021-10-12 thomas got_object_tree_close(subtree);
633 cc8021af 2021-10-12 thomas subtree = next_tree;
638 56e0773d 2019-11-28 stsp *id = got_object_id_dup(&te->id);
639 27d434c2 2018-09-15 stsp if (*id == NULL)
640 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
641 cc8021af 2021-10-12 thomas if (mode)
642 cc8021af 2021-10-12 thomas *mode = te->mode;
644 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
646 cc8021af 2021-10-12 thomas if (subtree && subtree != tree)
647 cc8021af 2021-10-12 thomas got_object_tree_close(subtree);
648 cc8021af 2021-10-12 thomas return err;
651 cc8021af 2021-10-12 thomas const struct got_error *
652 cc8021af 2021-10-12 thomas got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
653 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
655 cc8021af 2021-10-12 thomas const struct got_error *err = NULL;
656 cc8021af 2021-10-12 thomas struct got_tree_object *tree = NULL;
658 cc8021af 2021-10-12 thomas *id = NULL;
660 cc8021af 2021-10-12 thomas /* Handle opening of root of commit's tree. */
661 cc8021af 2021-10-12 thomas if (got_path_is_root_dir(path)) {
662 cc8021af 2021-10-12 thomas *id = got_object_id_dup(commit->tree_id);
663 cc8021af 2021-10-12 thomas if (*id == NULL)
664 cc8021af 2021-10-12 thomas err = got_error_from_errno("got_object_id_dup");
666 cc8021af 2021-10-12 thomas err = got_object_open_as_tree(&tree, repo, commit->tree_id);
668 cc8021af 2021-10-12 thomas goto done;
669 cc8021af 2021-10-12 thomas err = got_object_tree_find_path(id, NULL, repo, tree, path);
673 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
674 776d4d29 2018-06-17 stsp return err;
678 ac5f2b26 2020-05-05 stsp * Normalize file mode bits to avoid false positive tree entry differences
679 ac5f2b26 2020-05-05 stsp * in case tree entries have unexpected mode bits set.
681 ac5f2b26 2020-05-05 stsp static mode_t
682 ac5f2b26 2020-05-05 stsp normalize_mode_for_comparison(mode_t mode)
685 ac5f2b26 2020-05-05 stsp * For directories, the only relevant bit is the IFDIR bit.
686 ac5f2b26 2020-05-05 stsp * This allows us to detect paths changing from a directory
687 ac5f2b26 2020-05-05 stsp * to a file and vice versa.
689 ac5f2b26 2020-05-05 stsp if (S_ISDIR(mode))
690 ac5f2b26 2020-05-05 stsp return mode & S_IFDIR;
693 40dde666 2020-07-23 stsp * For symlinks, the only relevant bit is the IFLNK bit.
694 40dde666 2020-07-23 stsp * This allows us to detect paths changing from a symlinks
695 40dde666 2020-07-23 stsp * to a file or directory and vice versa.
697 40dde666 2020-07-23 stsp if (S_ISLNK(mode))
698 40dde666 2020-07-23 stsp return mode & S_IFLNK;
700 ac5f2b26 2020-05-05 stsp /* For files, the only change we care about is the executable bit. */
701 ac5f2b26 2020-05-05 stsp return mode & S_IXUSR;
704 07862c20 2018-09-15 stsp const struct got_error *
705 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
706 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
707 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
709 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
710 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
711 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
712 65a9bbe9 2018-09-15 stsp const char *seg, *s;
713 3b7f9878 2018-11-18 stsp size_t seglen;
715 07862c20 2018-09-15 stsp *changed = 0;
717 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
718 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
719 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
721 07862c20 2018-09-15 stsp tree1 = tree01;
722 07862c20 2018-09-15 stsp tree2 = tree02;
724 61a7d79f 2020-02-29 stsp while (*s == '/')
727 65a9bbe9 2018-09-15 stsp seglen = 0;
728 3b7f9878 2018-11-18 stsp while (*s) {
729 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
730 ac5f2b26 2020-05-05 stsp mode_t mode1, mode2;
732 07862c20 2018-09-15 stsp if (*s != '/') {
739 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
740 07862c20 2018-09-15 stsp if (te1 == NULL) {
741 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
746 e8bfb8f3 2020-12-18 stsp te2 = find_entry_by_name(tree2, seg, seglen);
749 e8bfb8f3 2020-12-18 stsp mode1 = normalize_mode_for_comparison(te1->mode);
750 e8bfb8f3 2020-12-18 stsp mode2 = normalize_mode_for_comparison(te2->mode);
751 e8bfb8f3 2020-12-18 stsp if (mode1 != mode2) {
752 e8bfb8f3 2020-12-18 stsp *changed = 1;
756 e8bfb8f3 2020-12-18 stsp if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
757 e8bfb8f3 2020-12-18 stsp *changed = 0;
762 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
763 07862c20 2018-09-15 stsp *changed = 1;
767 07862c20 2018-09-15 stsp seg = s + 1;
769 65a9bbe9 2018-09-15 stsp seglen = 0;
771 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
773 07862c20 2018-09-15 stsp te1 = NULL;
776 a31cea73 2018-09-15 stsp if (tree1 != tree01)
777 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
778 07862c20 2018-09-15 stsp tree1 = next_tree1;
781 e8bfb8f3 2020-12-18 stsp err = got_object_open_as_tree(&next_tree2, repo,
783 e8bfb8f3 2020-12-18 stsp te2 = NULL;
786 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
787 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
788 e8bfb8f3 2020-12-18 stsp tree2 = next_tree2;
789 e8bfb8f3 2020-12-18 stsp } else if (tree2) {
790 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
791 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
792 e8bfb8f3 2020-12-18 stsp tree2 = NULL;
797 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
798 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
799 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
800 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
801 77880158 2018-11-04 stsp return err;
804 ed175427 2019-05-09 stsp const struct got_error *
805 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
806 ed175427 2019-05-09 stsp struct got_tree_entry *te)
808 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
810 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
811 ed175427 2019-05-09 stsp if (*new_te == NULL)
812 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
814 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
815 56e0773d 2019-11-28 stsp memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
816 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
817 8c4eabf2 2019-05-10 stsp return err;
821 56e0773d 2019-11-28 stsp got_object_tree_entry_is_submodule(struct got_tree_entry *te)
823 63c5ca5d 2019-08-24 stsp return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
827 e40622f4 2020-07-23 stsp got_object_tree_entry_is_symlink(struct got_tree_entry *te)
829 e40622f4 2020-07-23 stsp /* S_IFDIR check avoids confusing symlinks with submodules. */
830 e40622f4 2020-07-23 stsp return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
833 e40622f4 2020-07-23 stsp static const struct got_error *
834 e40622f4 2020-07-23 stsp resolve_symlink(char **link_target, const char *path,
835 945f9229 2022-04-16 thomas struct got_commit_object *commit, struct got_repository *repo)
837 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
838 dbdd6209 2020-10-19 stsp char buf[PATH_MAX];
839 e40622f4 2020-07-23 stsp char *name, *parent_path = NULL;
840 e40622f4 2020-07-23 stsp struct got_object_id *tree_obj_id = NULL;
841 e40622f4 2020-07-23 stsp struct got_tree_object *tree = NULL;
842 e40622f4 2020-07-23 stsp struct got_tree_entry *te = NULL;
844 e40622f4 2020-07-23 stsp *link_target = NULL;
846 dbdd6209 2020-10-19 stsp if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
847 dbdd6209 2020-10-19 stsp return got_error(GOT_ERR_NO_SPACE);
849 dbdd6209 2020-10-19 stsp name = basename(buf);
850 e40622f4 2020-07-23 stsp if (name == NULL)
851 e40622f4 2020-07-23 stsp return got_error_from_errno2("basename", path);
853 e40622f4 2020-07-23 stsp err = got_path_dirname(&parent_path, path);
855 e40622f4 2020-07-23 stsp return err;
857 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_obj_id, repo, commit,
858 e40622f4 2020-07-23 stsp parent_path);
860 e40622f4 2020-07-23 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY) {
861 e40622f4 2020-07-23 stsp /* Display the complete path in error message. */
862 e40622f4 2020-07-23 stsp err = got_error_path(path, err->code);
867 e40622f4 2020-07-23 stsp err = got_object_open_as_tree(&tree, repo, tree_obj_id);
871 e40622f4 2020-07-23 stsp te = got_object_tree_find_entry(tree, name);
872 e40622f4 2020-07-23 stsp if (te == NULL) {
873 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
877 e40622f4 2020-07-23 stsp if (got_object_tree_entry_is_symlink(te)) {
878 e40622f4 2020-07-23 stsp err = got_tree_entry_get_symlink_target(link_target, te, repo);
881 e40622f4 2020-07-23 stsp if (!got_path_is_absolute(*link_target)) {
882 e40622f4 2020-07-23 stsp char *abspath;
883 e40622f4 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent_path,
884 e40622f4 2020-07-23 stsp *link_target) == -1) {
885 e40622f4 2020-07-23 stsp err = got_error_from_errno("asprintf");
888 e40622f4 2020-07-23 stsp free(*link_target);
889 e40622f4 2020-07-23 stsp *link_target = malloc(PATH_MAX);
890 e40622f4 2020-07-23 stsp if (*link_target == NULL) {
891 e40622f4 2020-07-23 stsp err = got_error_from_errno("malloc");
894 e40622f4 2020-07-23 stsp err = got_canonpath(abspath, *link_target, PATH_MAX);
895 e40622f4 2020-07-23 stsp free(abspath);
901 35eabca9 2022-09-03 thomas free(parent_path);
902 e40622f4 2020-07-23 stsp free(tree_obj_id);
904 e40622f4 2020-07-23 stsp got_object_tree_close(tree);
906 e40622f4 2020-07-23 stsp free(*link_target);
907 e40622f4 2020-07-23 stsp *link_target = NULL;
909 e40622f4 2020-07-23 stsp return err;
912 ca6e02ac 2020-01-07 stsp const struct got_error *
913 e40622f4 2020-07-23 stsp got_object_resolve_symlinks(char **link_target, const char *path,
914 945f9229 2022-04-16 thomas struct got_commit_object *commit, struct got_repository *repo)
916 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
917 e40622f4 2020-07-23 stsp char *next_target = NULL;
918 e40622f4 2020-07-23 stsp int max_recursion = 40; /* matches Git */
920 e40622f4 2020-07-23 stsp *link_target = NULL;
923 e40622f4 2020-07-23 stsp err = resolve_symlink(&next_target,
924 945f9229 2022-04-16 thomas *link_target ? *link_target : path, commit, repo);
927 e40622f4 2020-07-23 stsp if (next_target) {
928 e40622f4 2020-07-23 stsp free(*link_target);
929 e40622f4 2020-07-23 stsp if (--max_recursion == 0) {
930 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_RECURSION);
931 e40622f4 2020-07-23 stsp *link_target = NULL;
934 e40622f4 2020-07-23 stsp *link_target = next_target;
936 e40622f4 2020-07-23 stsp } while (next_target);
938 e40622f4 2020-07-23 stsp return err;
942 6f6c25d6 2022-09-18 thomas got_object_commit_retain(struct got_commit_object *commit)
944 7e8004ba 2022-09-11 thomas commit->refcnt++;
947 3efd8e31 2022-10-23 thomas const struct got_error *
948 3efd8e31 2022-10-23 thomas got_object_raw_alloc(struct got_raw_object **obj, uint8_t *outbuf, int *outfd,
949 3efd8e31 2022-10-23 thomas size_t hdrlen, off_t size)
951 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
952 aa75acde 2022-10-25 thomas off_t tot;
954 aa75acde 2022-10-25 thomas tot = hdrlen + size;
956 3efd8e31 2022-10-23 thomas *obj = calloc(1, sizeof(**obj));
957 3efd8e31 2022-10-23 thomas if (*obj == NULL) {
958 3efd8e31 2022-10-23 thomas err = got_error_from_errno("calloc");
959 3efd8e31 2022-10-23 thomas goto done;
961 3efd8e31 2022-10-23 thomas (*obj)->fd = -1;
962 3efd8e31 2022-10-23 thomas (*obj)->tempfile_idx = -1;
964 3efd8e31 2022-10-23 thomas if (outbuf) {
965 3efd8e31 2022-10-23 thomas (*obj)->data = outbuf;
967 3efd8e31 2022-10-23 thomas struct stat sb;
968 3efd8e31 2022-10-23 thomas if (fstat(*outfd, &sb) == -1) {
969 3efd8e31 2022-10-23 thomas err = got_error_from_errno("fstat");
970 3efd8e31 2022-10-23 thomas goto done;
973 aa75acde 2022-10-25 thomas if (sb.st_size != tot) {
974 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_LEN);
975 3efd8e31 2022-10-23 thomas goto done;
977 3efd8e31 2022-10-23 thomas #ifndef GOT_PACK_NO_MMAP
978 aa75acde 2022-10-25 thomas if (tot > 0 && tot <= SIZE_MAX) {
979 aa75acde 2022-10-25 thomas (*obj)->data = mmap(NULL, tot, PROT_READ,
980 3efd8e31 2022-10-23 thomas MAP_PRIVATE, *outfd, 0);
981 3efd8e31 2022-10-23 thomas if ((*obj)->data == MAP_FAILED) {
982 3efd8e31 2022-10-23 thomas if (errno != ENOMEM) {
983 3efd8e31 2022-10-23 thomas err = got_error_from_errno("mmap");
984 3efd8e31 2022-10-23 thomas goto done;
986 3efd8e31 2022-10-23 thomas (*obj)->data = NULL;
988 3efd8e31 2022-10-23 thomas (*obj)->fd = *outfd;
989 3efd8e31 2022-10-23 thomas *outfd = -1;
993 3efd8e31 2022-10-23 thomas if (*outfd != -1) {
994 3efd8e31 2022-10-23 thomas (*obj)->f = fdopen(*outfd, "r");
995 3efd8e31 2022-10-23 thomas if ((*obj)->f == NULL) {
996 3efd8e31 2022-10-23 thomas err = got_error_from_errno("fdopen");
997 3efd8e31 2022-10-23 thomas goto done;
999 3efd8e31 2022-10-23 thomas *outfd = -1;
1002 3efd8e31 2022-10-23 thomas (*obj)->hdrlen = hdrlen;
1003 3efd8e31 2022-10-23 thomas (*obj)->size = size;
1005 3efd8e31 2022-10-23 thomas if (err) {
1006 3efd8e31 2022-10-23 thomas if (*obj) {
1007 3efd8e31 2022-10-23 thomas got_object_raw_close(*obj);
1008 3efd8e31 2022-10-23 thomas *obj = NULL;
1011 3efd8e31 2022-10-23 thomas (*obj)->refcnt++;
1012 3efd8e31 2022-10-23 thomas return err;