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 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>
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>
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"
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"
54 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
57 0ab4c957 2022-06-13 stsp #ifndef nitems
58 0ab4c957 2022-06-13 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
61 3235492e 2018-04-01 stsp struct got_object_id *
62 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
64 6402fb3c 2018-09-15 stsp return &obj->id;
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)
70 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
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)
77 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
78 15a94983 2018-12-23 stsp struct got_object *obj;
80 15a94983 2018-12-23 stsp err = got_object_open(&obj, repo, id);
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;
92 15a94983 2018-12-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
96 15a94983 2018-12-23 stsp got_object_close(obj);
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)
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;
108 e6b1056e 2018-04-22 stsp *path = NULL;
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");
114 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
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");
124 d1cda826 2017-11-06 stsp free(path_objects);
125 d1cda826 2017-11-06 stsp return err;
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)
132 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
133 a1fd68d8 2018-01-12 stsp char *path;
135 90bdb554 2019-04-11 stsp err = got_object_get_path(&path, id, repo);
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);
144 4558fcd4 2018-01-14 stsp free(path);
145 59d1e4a0 2021-03-10 stsp return err;
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)
152 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
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);
157 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
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)
164 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
165 15a94983 2018-12-23 stsp struct got_object *obj;
167 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
169 15a94983 2018-12-23 stsp return err;
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");
176 15a94983 2018-12-23 stsp return NULL;
179 e32baab7 2018-11-05 stsp const struct got_error *
180 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
182 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
183 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
184 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
186 d7b5a0e8 2022-04-20 stsp memcpy(&(*qid)->id, id, sizeof((*qid)->id));
187 9ca9aafb 2021-06-18 stsp return NULL;
190 9ca9aafb 2021-06-18 stsp const struct got_error *
191 9ca9aafb 2021-06-18 stsp got_object_id_queue_copy(const struct got_object_id_queue *src,
192 9ca9aafb 2021-06-18 stsp struct got_object_id_queue *dest)
194 9ca9aafb 2021-06-18 stsp const struct got_error *err;
195 9ca9aafb 2021-06-18 stsp struct got_object_qid *qid;
197 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, src, entry) {
198 9ca9aafb 2021-06-18 stsp struct got_object_qid *new;
200 9ca9aafb 2021-06-18 stsp * Deep-copy the object ID only. Let the caller deal
201 9ca9aafb 2021-06-18 stsp * with setting up the new->data pointer if needed.
203 5e91dae4 2022-08-30 stsp err = got_object_qid_alloc(&new, &qid->id);
205 9ca9aafb 2021-06-18 stsp got_object_id_queue_free(dest);
206 9ca9aafb 2021-06-18 stsp return err;
208 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(dest, new, entry);
211 dbc6a6b6 2018-07-12 stsp return NULL;
215 56e0773d 2019-11-28 stsp got_object_tree_get_nentries(struct got_tree_object *tree)
217 56e0773d 2019-11-28 stsp return tree->nentries;
220 56e0773d 2019-11-28 stsp struct got_tree_entry *
221 56e0773d 2019-11-28 stsp got_object_tree_get_first_entry(struct got_tree_object *tree)
223 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, 0);
226 56e0773d 2019-11-28 stsp struct got_tree_entry *
227 56e0773d 2019-11-28 stsp got_object_tree_get_last_entry(struct got_tree_object *tree)
229 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, tree->nentries - 1);
232 56e0773d 2019-11-28 stsp struct got_tree_entry *
233 56e0773d 2019-11-28 stsp got_object_tree_get_entry(struct got_tree_object *tree, int i)
235 56e0773d 2019-11-28 stsp if (i < 0 || i >= tree->nentries)
236 56e0773d 2019-11-28 stsp return NULL;
237 56e0773d 2019-11-28 stsp return &tree->entries[i];
241 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(struct got_tree_entry *te)
243 56e0773d 2019-11-28 stsp return te->mode;
246 56e0773d 2019-11-28 stsp const char *
247 56e0773d 2019-11-28 stsp got_tree_entry_get_name(struct got_tree_entry *te)
249 56e0773d 2019-11-28 stsp return &te->name[0];
252 56e0773d 2019-11-28 stsp struct got_object_id *
253 56e0773d 2019-11-28 stsp got_tree_entry_get_id(struct got_tree_entry *te)
255 56e0773d 2019-11-28 stsp return &te->id;
258 0d6c6ee3 2020-05-20 stsp const struct got_error *
259 af57b12a 2020-07-23 stsp got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
261 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
262 32596e16 2020-07-23 stsp size_t len, totlen, hdrlen, offset;
266 659dc16e 2020-07-23 stsp hdrlen = got_object_blob_get_hdrlen(blob);
267 659dc16e 2020-07-23 stsp totlen = 0;
268 32596e16 2020-07-23 stsp offset = 0;
272 659dc16e 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
274 af57b12a 2020-07-23 stsp return err;
276 659dc16e 2020-07-23 stsp if (len == 0)
279 659dc16e 2020-07-23 stsp totlen += len - hdrlen;
280 af57b12a 2020-07-23 stsp p = realloc(*s, totlen + 1);
281 659dc16e 2020-07-23 stsp if (p == NULL) {
282 659dc16e 2020-07-23 stsp err = got_error_from_errno("realloc");
285 af57b12a 2020-07-23 stsp return err;
288 659dc16e 2020-07-23 stsp /* Skip blob object header first time around. */
289 af57b12a 2020-07-23 stsp memcpy(*s + offset,
290 f8f7c882 2020-07-23 stsp got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
291 659dc16e 2020-07-23 stsp hdrlen = 0;
292 32596e16 2020-07-23 stsp offset = totlen;
293 659dc16e 2020-07-23 stsp } while (len > 0);
295 af57b12a 2020-07-23 stsp (*s)[totlen] = '\0';
296 af57b12a 2020-07-23 stsp return NULL;
299 af57b12a 2020-07-23 stsp const struct got_error *
300 af57b12a 2020-07-23 stsp got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
301 af57b12a 2020-07-23 stsp struct got_repository *repo)
303 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
304 af57b12a 2020-07-23 stsp struct got_blob_object *blob = NULL;
305 eb81bc23 2022-06-28 tracey int fd = -1;
307 af57b12a 2020-07-23 stsp *link_target = NULL;
309 af57b12a 2020-07-23 stsp if (!got_object_tree_entry_is_symlink(te))
310 af57b12a 2020-07-23 stsp return got_error(GOT_ERR_TREE_ENTRY_TYPE);
312 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
313 eb81bc23 2022-06-28 tracey if (fd == -1) {
314 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
315 eb81bc23 2022-06-28 tracey goto done;
318 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo,
319 eb81bc23 2022-06-28 tracey got_tree_entry_get_id(te), PATH_MAX, fd);
321 eb81bc23 2022-06-28 tracey goto done;
323 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(link_target, blob);
325 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
326 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
327 eb81bc23 2022-06-28 tracey if (blob)
328 eb81bc23 2022-06-28 tracey got_object_blob_close(blob);
330 659dc16e 2020-07-23 stsp free(*link_target);
331 659dc16e 2020-07-23 stsp *link_target = NULL;
333 0d6c6ee3 2020-05-20 stsp return err;
337 56e0773d 2019-11-28 stsp got_tree_entry_get_index(struct got_tree_entry *te)
339 56e0773d 2019-11-28 stsp return te->idx;
342 56e0773d 2019-11-28 stsp struct got_tree_entry *
343 56e0773d 2019-11-28 stsp got_tree_entry_get_next(struct got_tree_object *tree,
344 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
346 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx + 1);
349 56e0773d 2019-11-28 stsp struct got_tree_entry *
350 56e0773d 2019-11-28 stsp got_tree_entry_get_prev(struct got_tree_object *tree,
351 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
353 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx - 1);
356 a19581a2 2018-06-21 stsp const struct got_error *
357 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
359 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
360 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
361 56b63ca4 2021-01-22 stsp if (blob->f && fclose(blob->f) == EOF)
362 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
363 ac544f8c 2019-01-13 stsp free(blob->data);
364 68482ea3 2017-11-27 stsp free(blob);
365 fb43ecf1 2019-02-11 stsp return err;
369 8ba819a3 2020-07-23 stsp got_object_blob_rewind(struct got_blob_object *blob)
371 8ba819a3 2020-07-23 stsp if (blob->f)
372 8ba819a3 2020-07-23 stsp rewind(blob->f);
376 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
378 bbc740ac 2023-02-04 op return got_object_id_hex(&blob->id, buf, size);
382 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
384 f934cf2c 2018-02-12 stsp return blob->hdrlen;
387 f934cf2c 2018-02-12 stsp const uint8_t *
388 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
390 f934cf2c 2018-02-12 stsp return blob->read_buf;
393 68482ea3 2017-11-27 stsp const struct got_error *
394 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
398 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
399 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
400 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
401 eb651edf 2018-02-11 stsp *outlenp = n;
402 35e9ba5d 2018-06-21 stsp return NULL;
405 35e9ba5d 2018-06-21 stsp const struct got_error *
406 0d569390 2023-01-04 op got_object_blob_is_binary(int *binary, struct got_blob_object *blob)
408 0d569390 2023-01-04 op const struct got_error *err;
409 0d569390 2023-01-04 op size_t hdrlen, len;
412 0d569390 2023-01-04 op hdrlen = got_object_blob_get_hdrlen(blob);
414 0d569390 2023-01-04 op if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
415 0d569390 2023-01-04 op return got_error_from_errno("fseeko");
417 0d569390 2023-01-04 op err = got_object_blob_read_block(&len, blob);
421 0d569390 2023-01-04 op *binary = memchr(blob->read_buf, '\0', len) != NULL;
423 0d569390 2023-01-04 op if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
424 0d569390 2023-01-04 op return got_error_from_errno("fseeko");
428 389a68d8 2023-01-05 op const struct got_error *
429 389a68d8 2023-01-05 op got_object_blob_getline(char **line, ssize_t *linelen, size_t *linesize,
430 389a68d8 2023-01-05 op struct got_blob_object *blob)
432 389a68d8 2023-01-05 op *linelen = getline(line, linesize, blob->f);
433 389a68d8 2023-01-05 op if (*linelen == -1 && !feof(blob->f))
434 389a68d8 2023-01-05 op return got_error_from_errno("getline");
438 0d569390 2023-01-04 op const struct got_error *
439 be659d10 2020-11-18 stsp got_object_blob_dump_to_file(off_t *filesize, int *nlines,
440 6c4c42e0 2019-06-24 stsp off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
442 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
443 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
444 84451b3e 2018-07-10 stsp const uint8_t *buf;
446 c33ebc60 2020-11-18 stsp const int alloc_chunksz = 512;
447 c33ebc60 2020-11-18 stsp size_t nalloc = 0;
448 f595d9bd 2019-08-14 stsp off_t off = 0, total_len = 0;
450 6c4c42e0 2019-06-24 stsp if (line_offsets)
451 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
452 f595d9bd 2019-08-14 stsp if (filesize)
453 f595d9bd 2019-08-14 stsp *filesize = 0;
454 84451b3e 2018-07-10 stsp if (nlines)
455 84451b3e 2018-07-10 stsp *nlines = 0;
457 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
459 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
461 35e9ba5d 2018-06-21 stsp return err;
462 35e9ba5d 2018-06-21 stsp if (len == 0)
464 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
465 b02560ec 2019-08-19 stsp i = hdrlen;
466 f1cbc3bc 2020-11-18 stsp if (nlines) {
467 f1cbc3bc 2020-11-18 stsp if (line_offsets && *line_offsets == NULL) {
468 78695fb7 2019-08-12 stsp /* Have some data but perhaps no '\n'. */
469 78695fb7 2019-08-12 stsp *nlines = 1;
470 c33ebc60 2020-11-18 stsp nalloc = alloc_chunksz;
471 c33ebc60 2020-11-18 stsp *line_offsets = calloc(nalloc,
472 c33ebc60 2020-11-18 stsp sizeof(**line_offsets));
473 78695fb7 2019-08-12 stsp if (*line_offsets == NULL)
474 845785d4 2020-02-02 tracey return got_error_from_errno("calloc");
476 b02560ec 2019-08-19 stsp /* Skip forward over end of first line. */
477 b02560ec 2019-08-19 stsp while (i < len) {
478 b02560ec 2019-08-19 stsp if (buf[i] == '\n')
483 b02560ec 2019-08-19 stsp /* Scan '\n' offsets in remaining chunk of data. */
484 b02560ec 2019-08-19 stsp while (i < len) {
485 b02560ec 2019-08-19 stsp if (buf[i] != '\n') {
489 f595d9bd 2019-08-14 stsp (*nlines)++;
490 c33ebc60 2020-11-18 stsp if (line_offsets && nalloc < *nlines) {
491 c33ebc60 2020-11-18 stsp size_t n = *nlines + alloc_chunksz;
492 78695fb7 2019-08-12 stsp off_t *o = recallocarray(*line_offsets,
493 c33ebc60 2020-11-18 stsp nalloc, n, sizeof(**line_offsets));
494 78695fb7 2019-08-12 stsp if (o == NULL) {
495 78695fb7 2019-08-12 stsp free(*line_offsets);
496 78695fb7 2019-08-12 stsp *line_offsets = NULL;
497 78695fb7 2019-08-12 stsp return got_error_from_errno(
498 78695fb7 2019-08-12 stsp "recallocarray");
500 78695fb7 2019-08-12 stsp *line_offsets = o;
501 c33ebc60 2020-11-18 stsp nalloc = n;
503 f1cbc3bc 2020-11-18 stsp if (line_offsets) {
504 f1cbc3bc 2020-11-18 stsp off = total_len + i - hdrlen + 1;
505 f1cbc3bc 2020-11-18 stsp (*line_offsets)[*nlines - 1] = off;
510 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
511 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
512 b6752625 2018-12-24 stsp if (n != len - hdrlen)
513 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
514 f595d9bd 2019-08-14 stsp total_len += len - hdrlen;
515 35e9ba5d 2018-06-21 stsp hdrlen = 0;
516 35e9ba5d 2018-06-21 stsp } while (len != 0);
518 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
519 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
520 35e9ba5d 2018-06-21 stsp rewind(outfile);
522 f595d9bd 2019-08-14 stsp if (filesize)
523 f595d9bd 2019-08-14 stsp *filesize = total_len;
525 776d4d29 2018-06-17 stsp return NULL;
528 d24820bf 2019-08-11 stsp const char *
529 d24820bf 2019-08-11 stsp got_object_tag_get_name(struct got_tag_object *tag)
531 d24820bf 2019-08-11 stsp return tag->tag;
535 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
537 0bd18d37 2019-02-01 stsp return tag->obj_type;
540 0bd18d37 2019-02-01 stsp struct got_object_id *
541 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
543 0bd18d37 2019-02-01 stsp return &tag->id;
547 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_time(struct got_tag_object *tag)
549 01073a5d 2019-08-22 stsp return tag->tagger_time;
553 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
555 01073a5d 2019-08-22 stsp return tag->tagger_gmtoff;
558 01073a5d 2019-08-22 stsp const char *
559 01073a5d 2019-08-22 stsp got_object_tag_get_tagger(struct got_tag_object *tag)
561 01073a5d 2019-08-22 stsp return tag->tagger;
564 01073a5d 2019-08-22 stsp const char *
565 01073a5d 2019-08-22 stsp got_object_tag_get_message(struct got_tag_object *tag)
567 01073a5d 2019-08-22 stsp return tag->tagmsg;
570 776d4d29 2018-06-17 stsp static struct got_tree_entry *
571 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
575 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
576 56e0773d 2019-11-28 stsp for (i = 0; i < tree->nentries; i++) {
577 56e0773d 2019-11-28 stsp struct got_tree_entry *te = &tree->entries[i];
578 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
579 63da309a 2018-11-07 stsp if (cmp < 0)
581 63da309a 2018-11-07 stsp if (cmp > 0)
583 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
586 eb651edf 2018-02-11 stsp return NULL;
589 56e0773d 2019-11-28 stsp struct got_tree_entry *
590 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
592 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
595 776d4d29 2018-06-17 stsp const struct got_error *
596 67b631c9 2021-10-10 stsp got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
597 67b631c9 2021-10-10 stsp struct got_repository *repo, struct got_tree_object *tree,
598 67b631c9 2021-10-10 stsp const char *path)
600 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
601 67b631c9 2021-10-10 stsp struct got_tree_object *subtree = NULL;
602 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
603 65a9bbe9 2018-09-15 stsp const char *seg, *s;
604 b7cd37e5 2018-11-18 stsp size_t seglen;
606 27d434c2 2018-09-15 stsp *id = NULL;
609 5e54fb30 2019-05-31 stsp while (s[0] == '/')
612 65a9bbe9 2018-09-15 stsp seglen = 0;
613 67b631c9 2021-10-10 stsp subtree = tree;
614 b7cd37e5 2018-11-18 stsp while (*s) {
615 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
617 776d4d29 2018-06-17 stsp if (*s != '/') {
624 67b631c9 2021-10-10 stsp te = find_entry_by_name(subtree, seg, seglen);
625 db37e2c0 2018-06-21 stsp if (te == NULL) {
626 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
630 b7cd37e5 2018-11-18 stsp if (*s == '\0')
633 776d4d29 2018-06-17 stsp seg = s + 1;
634 65a9bbe9 2018-09-15 stsp seglen = 0;
637 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
642 67b631c9 2021-10-10 stsp if (subtree != tree)
643 67b631c9 2021-10-10 stsp got_object_tree_close(subtree);
644 67b631c9 2021-10-10 stsp subtree = next_tree;
649 56e0773d 2019-11-28 stsp *id = got_object_id_dup(&te->id);
650 27d434c2 2018-09-15 stsp if (*id == NULL)
651 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
653 67b631c9 2021-10-10 stsp *mode = te->mode;
655 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
657 67b631c9 2021-10-10 stsp if (subtree && subtree != tree)
658 67b631c9 2021-10-10 stsp got_object_tree_close(subtree);
659 67b631c9 2021-10-10 stsp return err;
662 67b631c9 2021-10-10 stsp const struct got_error *
663 67b631c9 2021-10-10 stsp got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
664 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
666 67b631c9 2021-10-10 stsp const struct got_error *err = NULL;
667 67b631c9 2021-10-10 stsp struct got_tree_object *tree = NULL;
669 67b631c9 2021-10-10 stsp *id = NULL;
671 67b631c9 2021-10-10 stsp /* Handle opening of root of commit's tree. */
672 67b631c9 2021-10-10 stsp if (got_path_is_root_dir(path)) {
673 67b631c9 2021-10-10 stsp *id = got_object_id_dup(commit->tree_id);
674 67b631c9 2021-10-10 stsp if (*id == NULL)
675 67b631c9 2021-10-10 stsp err = got_error_from_errno("got_object_id_dup");
677 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
680 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(id, NULL, repo, tree, path);
684 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
685 776d4d29 2018-06-17 stsp return err;
689 ac5f2b26 2020-05-05 stsp * Normalize file mode bits to avoid false positive tree entry differences
690 ac5f2b26 2020-05-05 stsp * in case tree entries have unexpected mode bits set.
692 ac5f2b26 2020-05-05 stsp static mode_t
693 ac5f2b26 2020-05-05 stsp normalize_mode_for_comparison(mode_t mode)
696 ac5f2b26 2020-05-05 stsp * For directories, the only relevant bit is the IFDIR bit.
697 ac5f2b26 2020-05-05 stsp * This allows us to detect paths changing from a directory
698 ac5f2b26 2020-05-05 stsp * to a file and vice versa.
700 ac5f2b26 2020-05-05 stsp if (S_ISDIR(mode))
701 ac5f2b26 2020-05-05 stsp return mode & S_IFDIR;
704 40dde666 2020-07-23 stsp * For symlinks, the only relevant bit is the IFLNK bit.
705 40dde666 2020-07-23 stsp * This allows us to detect paths changing from a symlinks
706 40dde666 2020-07-23 stsp * to a file or directory and vice versa.
708 40dde666 2020-07-23 stsp if (S_ISLNK(mode))
709 40dde666 2020-07-23 stsp return mode & S_IFLNK;
711 ac5f2b26 2020-05-05 stsp /* For files, the only change we care about is the executable bit. */
712 ac5f2b26 2020-05-05 stsp return mode & S_IXUSR;
715 07862c20 2018-09-15 stsp const struct got_error *
716 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
717 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
718 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
720 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
721 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
722 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
723 65a9bbe9 2018-09-15 stsp const char *seg, *s;
724 3b7f9878 2018-11-18 stsp size_t seglen;
726 07862c20 2018-09-15 stsp *changed = 0;
728 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
729 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
730 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
732 07862c20 2018-09-15 stsp tree1 = tree01;
733 07862c20 2018-09-15 stsp tree2 = tree02;
735 61a7d79f 2020-02-29 stsp while (*s == '/')
738 65a9bbe9 2018-09-15 stsp seglen = 0;
739 3b7f9878 2018-11-18 stsp while (*s) {
740 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
741 ac5f2b26 2020-05-05 stsp mode_t mode1, mode2;
743 07862c20 2018-09-15 stsp if (*s != '/') {
750 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
751 07862c20 2018-09-15 stsp if (te1 == NULL) {
752 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
757 e8bfb8f3 2020-12-18 stsp te2 = find_entry_by_name(tree2, seg, seglen);
760 e8bfb8f3 2020-12-18 stsp mode1 = normalize_mode_for_comparison(te1->mode);
761 e8bfb8f3 2020-12-18 stsp mode2 = normalize_mode_for_comparison(te2->mode);
762 e8bfb8f3 2020-12-18 stsp if (mode1 != mode2) {
763 e8bfb8f3 2020-12-18 stsp *changed = 1;
767 e8bfb8f3 2020-12-18 stsp if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
768 e8bfb8f3 2020-12-18 stsp *changed = 0;
773 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
774 07862c20 2018-09-15 stsp *changed = 1;
778 07862c20 2018-09-15 stsp seg = s + 1;
780 65a9bbe9 2018-09-15 stsp seglen = 0;
782 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
784 07862c20 2018-09-15 stsp te1 = NULL;
787 a31cea73 2018-09-15 stsp if (tree1 != tree01)
788 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
789 07862c20 2018-09-15 stsp tree1 = next_tree1;
792 e8bfb8f3 2020-12-18 stsp err = got_object_open_as_tree(&next_tree2, repo,
794 e8bfb8f3 2020-12-18 stsp te2 = NULL;
797 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
798 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
799 e8bfb8f3 2020-12-18 stsp tree2 = next_tree2;
800 e8bfb8f3 2020-12-18 stsp } else if (tree2) {
801 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
802 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
803 e8bfb8f3 2020-12-18 stsp tree2 = NULL;
808 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
809 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
810 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
811 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
812 77880158 2018-11-04 stsp return err;
815 ed175427 2019-05-09 stsp const struct got_error *
816 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
817 ed175427 2019-05-09 stsp struct got_tree_entry *te)
819 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
821 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
822 ed175427 2019-05-09 stsp if (*new_te == NULL)
823 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
825 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
826 56e0773d 2019-11-28 stsp memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
827 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
828 8c4eabf2 2019-05-10 stsp return err;
832 56e0773d 2019-11-28 stsp got_object_tree_entry_is_submodule(struct got_tree_entry *te)
834 63c5ca5d 2019-08-24 stsp return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
838 e40622f4 2020-07-23 stsp got_object_tree_entry_is_symlink(struct got_tree_entry *te)
840 e40622f4 2020-07-23 stsp /* S_IFDIR check avoids confusing symlinks with submodules. */
841 e40622f4 2020-07-23 stsp return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
844 e40622f4 2020-07-23 stsp static const struct got_error *
845 e40622f4 2020-07-23 stsp resolve_symlink(char **link_target, const char *path,
846 a44927cc 2022-04-07 stsp struct got_commit_object *commit, struct got_repository *repo)
848 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
849 dbdd6209 2020-10-19 stsp char buf[PATH_MAX];
850 e40622f4 2020-07-23 stsp char *name, *parent_path = NULL;
851 e40622f4 2020-07-23 stsp struct got_object_id *tree_obj_id = NULL;
852 e40622f4 2020-07-23 stsp struct got_tree_object *tree = NULL;
853 e40622f4 2020-07-23 stsp struct got_tree_entry *te = NULL;
855 e40622f4 2020-07-23 stsp *link_target = NULL;
857 dbdd6209 2020-10-19 stsp if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
858 dbdd6209 2020-10-19 stsp return got_error(GOT_ERR_NO_SPACE);
860 dbdd6209 2020-10-19 stsp name = basename(buf);
861 e40622f4 2020-07-23 stsp if (name == NULL)
862 e40622f4 2020-07-23 stsp return got_error_from_errno2("basename", path);
864 e40622f4 2020-07-23 stsp err = got_path_dirname(&parent_path, path);
866 e40622f4 2020-07-23 stsp return err;
868 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_obj_id, repo, commit,
869 e40622f4 2020-07-23 stsp parent_path);
871 e40622f4 2020-07-23 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY) {
872 e40622f4 2020-07-23 stsp /* Display the complete path in error message. */
873 e40622f4 2020-07-23 stsp err = got_error_path(path, err->code);
878 e40622f4 2020-07-23 stsp err = got_object_open_as_tree(&tree, repo, tree_obj_id);
882 e40622f4 2020-07-23 stsp te = got_object_tree_find_entry(tree, name);
883 e40622f4 2020-07-23 stsp if (te == NULL) {
884 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
888 e40622f4 2020-07-23 stsp if (got_object_tree_entry_is_symlink(te)) {
889 e40622f4 2020-07-23 stsp err = got_tree_entry_get_symlink_target(link_target, te, repo);
892 e40622f4 2020-07-23 stsp if (!got_path_is_absolute(*link_target)) {
893 e40622f4 2020-07-23 stsp char *abspath;
894 e40622f4 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent_path,
895 e40622f4 2020-07-23 stsp *link_target) == -1) {
896 e40622f4 2020-07-23 stsp err = got_error_from_errno("asprintf");
899 e40622f4 2020-07-23 stsp free(*link_target);
900 e40622f4 2020-07-23 stsp *link_target = malloc(PATH_MAX);
901 e40622f4 2020-07-23 stsp if (*link_target == NULL) {
902 e40622f4 2020-07-23 stsp err = got_error_from_errno("malloc");
905 e40622f4 2020-07-23 stsp err = got_canonpath(abspath, *link_target, PATH_MAX);
906 e40622f4 2020-07-23 stsp free(abspath);
912 b68bd9d2 2022-09-03 op free(parent_path);
913 e40622f4 2020-07-23 stsp free(tree_obj_id);
915 e40622f4 2020-07-23 stsp got_object_tree_close(tree);
917 e40622f4 2020-07-23 stsp free(*link_target);
918 e40622f4 2020-07-23 stsp *link_target = NULL;
920 e40622f4 2020-07-23 stsp return err;
923 ca6e02ac 2020-01-07 stsp const struct got_error *
924 e40622f4 2020-07-23 stsp got_object_resolve_symlinks(char **link_target, const char *path,
925 a44927cc 2022-04-07 stsp struct got_commit_object *commit, struct got_repository *repo)
927 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
928 e40622f4 2020-07-23 stsp char *next_target = NULL;
929 e40622f4 2020-07-23 stsp int max_recursion = 40; /* matches Git */
931 e40622f4 2020-07-23 stsp *link_target = NULL;
934 e40622f4 2020-07-23 stsp err = resolve_symlink(&next_target,
935 a44927cc 2022-04-07 stsp *link_target ? *link_target : path, commit, repo);
938 e40622f4 2020-07-23 stsp if (next_target) {
939 e40622f4 2020-07-23 stsp free(*link_target);
940 e40622f4 2020-07-23 stsp if (--max_recursion == 0) {
941 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_RECURSION);
942 e40622f4 2020-07-23 stsp *link_target = NULL;
945 e40622f4 2020-07-23 stsp *link_target = next_target;
947 e40622f4 2020-07-23 stsp } while (next_target);
949 e40622f4 2020-07-23 stsp return err;
953 34a842a4 2022-09-18 mark got_object_commit_retain(struct got_commit_object *commit)
955 568eae95 2022-09-11 mark commit->refcnt++;
958 13b2bc37 2022-10-23 stsp const struct got_error *
959 13b2bc37 2022-10-23 stsp got_object_raw_alloc(struct got_raw_object **obj, uint8_t *outbuf, int *outfd,
960 60c140ae 2023-01-09 stsp size_t max_in_mem_size, size_t hdrlen, off_t size)
962 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
965 1c28a361 2022-10-25 op tot = hdrlen + size;
967 13b2bc37 2022-10-23 stsp *obj = calloc(1, sizeof(**obj));
968 13b2bc37 2022-10-23 stsp if (*obj == NULL) {
969 13b2bc37 2022-10-23 stsp err = got_error_from_errno("calloc");
972 13b2bc37 2022-10-23 stsp (*obj)->fd = -1;
973 13b2bc37 2022-10-23 stsp (*obj)->tempfile_idx = -1;
975 13b2bc37 2022-10-23 stsp if (outbuf) {
976 13b2bc37 2022-10-23 stsp (*obj)->data = outbuf;
978 13b2bc37 2022-10-23 stsp struct stat sb;
979 13b2bc37 2022-10-23 stsp if (fstat(*outfd, &sb) == -1) {
980 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fstat");
984 1c28a361 2022-10-25 op if (sb.st_size != tot) {
985 e98a81e6 2023-01-09 stsp err = got_error_msg(GOT_ERR_BAD_OBJ_HDR,
986 e98a81e6 2023-01-09 stsp "raw object has unexpected size");
989 13b2bc37 2022-10-23 stsp #ifndef GOT_PACK_NO_MMAP
990 60c140ae 2023-01-09 stsp if (tot > 0 && tot <= max_in_mem_size) {
991 1c28a361 2022-10-25 op (*obj)->data = mmap(NULL, tot, PROT_READ,
992 13b2bc37 2022-10-23 stsp MAP_PRIVATE, *outfd, 0);
993 13b2bc37 2022-10-23 stsp if ((*obj)->data == MAP_FAILED) {
994 13b2bc37 2022-10-23 stsp if (errno != ENOMEM) {
995 13b2bc37 2022-10-23 stsp err = got_error_from_errno("mmap");
998 13b2bc37 2022-10-23 stsp (*obj)->data = NULL;
1000 13b2bc37 2022-10-23 stsp (*obj)->fd = *outfd;
1001 13b2bc37 2022-10-23 stsp *outfd = -1;
1005 13b2bc37 2022-10-23 stsp if (*outfd != -1) {
1006 13b2bc37 2022-10-23 stsp (*obj)->f = fdopen(*outfd, "r");
1007 13b2bc37 2022-10-23 stsp if ((*obj)->f == NULL) {
1008 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fdopen");
1009 13b2bc37 2022-10-23 stsp goto done;
1011 13b2bc37 2022-10-23 stsp *outfd = -1;
1014 13b2bc37 2022-10-23 stsp (*obj)->hdrlen = hdrlen;
1015 13b2bc37 2022-10-23 stsp (*obj)->size = size;
1017 13b2bc37 2022-10-23 stsp if (err) {
1018 13b2bc37 2022-10-23 stsp if (*obj) {
1019 13b2bc37 2022-10-23 stsp got_object_raw_close(*obj);
1020 13b2bc37 2022-10-23 stsp *obj = NULL;
1023 13b2bc37 2022-10-23 stsp (*obj)->refcnt++;
1024 13b2bc37 2022-10-23 stsp return err;