2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
35 #include "got_error.h"
36 #include "got_object.h"
39 #include "got_lib_hash.h"
40 #include "got_lib_fileindex.h"
41 #include "got_lib_worktree.h"
43 /* got_fileindex_entry flags */
44 #define GOT_FILEIDX_F_PATH_LEN 0x00000fff
45 #define GOT_FILEIDX_F_STAGE 0x0000f000
46 #define GOT_FILEIDX_F_STAGE_SHIFT 12
47 #define GOT_FILEIDX_F_NOT_FLUSHED 0x00010000
48 #define GOT_FILEIDX_F_NO_BLOB 0x00020000
49 #define GOT_FILEIDX_F_NO_COMMIT 0x00040000
50 #define GOT_FILEIDX_F_NO_FILE_ON_DISK 0x00080000
51 #define GOT_FILEIDX_F_REMOVE_ON_FLUSH 0x00100000
52 #define GOT_FILEIDX_F_SKIPPED 0x00200000
54 struct got_fileindex {
55 struct got_fileindex_tree entries;
56 int nentries; /* Does not include entries marked for removal. */
57 #define GOT_FILEIDX_MAX_ENTRIES INT32_MAX
61 got_fileindex_entry_perms_get(struct got_fileindex_entry *ie)
63 return ((ie->mode & GOT_FILEIDX_MODE_PERMS) >>
64 GOT_FILEIDX_MODE_PERMS_SHIFT);
68 fileindex_entry_perms_set(struct got_fileindex_entry *ie, mode_t mode)
70 ie->mode &= ~GOT_FILEIDX_MODE_PERMS;
71 ie->mode |= ((mode << GOT_FILEIDX_MODE_PERMS_SHIFT) &
72 GOT_FILEIDX_MODE_PERMS);
76 got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
78 mode_t perms = got_fileindex_entry_perms_get(ie);
79 int type = got_fileindex_entry_filetype_get(ie);
82 if (type == GOT_FILEIDX_MODE_REGULAR_FILE ||
83 type == GOT_FILEIDX_MODE_BAD_SYMLINK)
88 return (ftype | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
91 const struct got_error *
92 got_fileindex_entry_update(struct got_fileindex_entry *ie,
93 int wt_fd, const char *ondisk_path, uint8_t *blob_sha1,
94 uint8_t *commit_sha1, int update_timestamps)
98 if (fstatat(wt_fd, ondisk_path, &sb, AT_SYMLINK_NOFOLLOW) != 0) {
99 if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
101 return got_error_from_errno2("fstatat", ondisk_path);
102 sb.st_mode = GOT_DEFAULT_FILE_MODE;
104 if (sb.st_mode & S_IFDIR)
105 return got_error_set_errno(EISDIR, ondisk_path);
106 ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
109 if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
110 if (update_timestamps) {
111 ie->ctime_sec = sb.st_ctim.tv_sec;
112 ie->ctime_nsec = sb.st_ctim.tv_nsec;
113 ie->mtime_sec = sb.st_mtim.tv_sec;
114 ie->mtime_nsec = sb.st_mtim.tv_nsec;
118 ie->size = (sb.st_size & 0xffffffff);
119 if (S_ISLNK(sb.st_mode)) {
120 got_fileindex_entry_filetype_set(ie,
121 GOT_FILEIDX_MODE_SYMLINK);
122 fileindex_entry_perms_set(ie, 0);
124 got_fileindex_entry_filetype_set(ie,
125 GOT_FILEIDX_MODE_REGULAR_FILE);
126 fileindex_entry_perms_set(ie,
127 sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
132 memmove(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
133 ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
135 ie->flags |= GOT_FILEIDX_F_NO_BLOB;
138 memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
139 ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
141 ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
147 got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
149 ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
153 got_fileindex_entry_mark_skipped(struct got_fileindex_entry *ie)
155 ie->flags |= GOT_FILEIDX_F_SKIPPED;
158 const struct got_error *
159 got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
164 *ie = calloc(1, sizeof(**ie));
166 return got_error_from_errno("calloc");
168 (*ie)->path = strdup(relpath);
169 if ((*ie)->path == NULL) {
170 const struct got_error *err = got_error_from_errno("strdup");
176 len = strlen(relpath);
177 if (len > GOT_FILEIDX_F_PATH_LEN)
178 len = GOT_FILEIDX_F_PATH_LEN;
185 got_fileindex_entry_free(struct got_fileindex_entry *ie)
192 got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
194 return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
198 got_fileindex_entry_stage_get(const struct got_fileindex_entry *ie)
200 return ((ie->flags & GOT_FILEIDX_F_STAGE) >> GOT_FILEIDX_F_STAGE_SHIFT);
204 got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage)
206 ie->flags &= ~GOT_FILEIDX_F_STAGE;
207 ie->flags |= ((stage << GOT_FILEIDX_F_STAGE_SHIFT) &
208 GOT_FILEIDX_F_STAGE);
212 got_fileindex_entry_filetype_get(struct got_fileindex_entry *ie)
214 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
218 got_fileindex_entry_filetype_set(struct got_fileindex_entry *ie, int type)
220 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_ONDISK;
221 ie->mode |= (type & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
225 got_fileindex_entry_staged_filetype_set(struct got_fileindex_entry *ie,
228 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_STAGED;
229 ie->mode |= ((type << GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT) &
230 GOT_FILEIDX_MODE_FILE_TYPE_STAGED);
234 got_fileindex_entry_staged_filetype_get(struct got_fileindex_entry *ie)
236 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_STAGED) >>
237 GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT;
241 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
243 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
247 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
249 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
253 got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
255 return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
259 got_fileindex_entry_was_skipped(struct got_fileindex_entry *ie)
261 return (ie->flags & GOT_FILEIDX_F_SKIPPED) != 0;
264 static const struct got_error *
265 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
267 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
268 return got_error(GOT_ERR_NO_SPACE);
270 if (RB_INSERT(got_fileindex_tree, &fileindex->entries, ie) != NULL)
271 return got_error_path(ie->path, GOT_ERR_FILEIDX_DUP_ENTRY);
273 fileindex->nentries++;
277 const struct got_error *
278 got_fileindex_entry_add(struct got_fileindex *fileindex,
279 struct got_fileindex_entry *ie)
281 /* Flag this entry until it gets written out to disk. */
282 ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
284 return add_entry(fileindex, ie);
288 got_fileindex_entry_remove(struct got_fileindex *fileindex,
289 struct got_fileindex_entry *ie)
292 * Removing an entry from the RB tree immediately breaks
293 * in-progress iterations over file index entries.
294 * So flag this entry for removal and remove it once the index
295 * is written out to disk. Meanwhile, pretend this entry no longer
296 * exists if we get queried for it again before then.
298 ie->flags |= GOT_FILEIDX_F_REMOVE_ON_FLUSH;
299 fileindex->nentries--;
302 struct got_fileindex_entry *
303 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
306 struct got_fileindex_entry *ie;
307 struct got_fileindex_entry key;
308 memset(&key, 0, sizeof(key));
309 key.path = (char *)path;
310 key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
311 ie = RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
312 if (ie && (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH))
317 const struct got_error *
318 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
319 got_fileindex_cb cb, void *cb_arg)
321 const struct got_error *err;
322 struct got_fileindex_entry *ie, *tmp;
324 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
325 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
327 err = (*cb)(cb_arg, ie);
334 struct got_fileindex *
335 got_fileindex_alloc(void)
337 struct got_fileindex *fileindex;
339 fileindex = calloc(1, sizeof(*fileindex));
340 if (fileindex == NULL)
343 RB_INIT(&fileindex->entries);
348 got_fileindex_free(struct got_fileindex *fileindex)
350 struct got_fileindex_entry *ie;
352 while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
353 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
354 got_fileindex_entry_free(ie);
359 static const struct got_error *
360 write_fileindex_val64(struct got_hash *ctx, uint64_t val, FILE *outfile)
365 got_hash_update(ctx, &val, sizeof(val));
366 n = fwrite(&val, 1, sizeof(val), outfile);
367 if (n != sizeof(val))
368 return got_ferror(outfile, GOT_ERR_IO);
372 static const struct got_error *
373 write_fileindex_val32(struct got_hash *ctx, uint32_t val, FILE *outfile)
378 got_hash_update(ctx, &val, sizeof(val));
379 n = fwrite(&val, 1, sizeof(val), outfile);
380 if (n != sizeof(val))
381 return got_ferror(outfile, GOT_ERR_IO);
385 static const struct got_error *
386 write_fileindex_val16(struct got_hash *ctx, uint16_t val, FILE *outfile)
391 got_hash_update(ctx, &val, sizeof(val));
392 n = fwrite(&val, 1, sizeof(val), outfile);
393 if (n != sizeof(val))
394 return got_ferror(outfile, GOT_ERR_IO);
398 static const struct got_error *
399 write_fileindex_path(struct got_hash *ctx, const char *path, FILE *outfile)
401 size_t n, len, pad = 0;
402 static const uint8_t zero[8] = { 0 };
405 while ((len + pad) % 8 != 0)
408 pad = 8; /* NUL-terminate */
410 got_hash_update(ctx, path, len);
411 n = fwrite(path, 1, len, outfile);
413 return got_ferror(outfile, GOT_ERR_IO);
414 got_hash_update(ctx, zero, pad);
415 n = fwrite(zero, 1, pad, outfile);
417 return got_ferror(outfile, GOT_ERR_IO);
421 static const struct got_error *
422 write_fileindex_entry(struct got_hash *ctx, struct got_fileindex_entry *ie,
425 const struct got_error *err;
429 err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
432 err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
435 err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
438 err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
442 err = write_fileindex_val32(ctx, ie->uid, outfile);
445 err = write_fileindex_val32(ctx, ie->gid, outfile);
448 err = write_fileindex_val32(ctx, ie->size, outfile);
452 err = write_fileindex_val16(ctx, ie->mode, outfile);
456 got_hash_update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
457 n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
458 if (n != SHA1_DIGEST_LENGTH)
459 return got_ferror(outfile, GOT_ERR_IO);
461 got_hash_update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
462 n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
463 if (n != SHA1_DIGEST_LENGTH)
464 return got_ferror(outfile, GOT_ERR_IO);
466 err = write_fileindex_val32(ctx, ie->flags, outfile);
470 err = write_fileindex_path(ctx, ie->path, outfile);
474 stage = got_fileindex_entry_stage_get(ie);
475 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
476 stage == GOT_FILEIDX_STAGE_ADD) {
477 got_hash_update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
478 n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
480 if (n != SHA1_DIGEST_LENGTH)
481 return got_ferror(outfile, GOT_ERR_IO);
487 const struct got_error *
488 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
490 const struct got_error *err = NULL;
491 struct got_fileindex_hdr hdr;
493 uint8_t hash[GOT_HASH_DIGEST_MAXLEN];
495 struct got_fileindex_entry *ie, *tmp;
497 got_hash_init(&ctx, GOT_HASH_SHA1);
499 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
500 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
501 hdr.nentries = htobe32(fileindex->nentries);
503 got_hash_update(&ctx, &hdr.signature, sizeof(hdr.signature));
504 got_hash_update(&ctx, &hdr.version, sizeof(hdr.version));
505 got_hash_update(&ctx, &hdr.nentries, sizeof(hdr.nentries));
506 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
507 if (n != sizeof(hdr.signature))
508 return got_ferror(outfile, GOT_ERR_IO);
509 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
510 if (n != sizeof(hdr.version))
511 return got_ferror(outfile, GOT_ERR_IO);
512 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
513 if (n != sizeof(hdr.nentries))
514 return got_ferror(outfile, GOT_ERR_IO);
516 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
517 ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
518 ie->flags &= ~GOT_FILEIDX_F_SKIPPED;
519 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH) {
520 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
521 got_fileindex_entry_free(ie);
524 err = write_fileindex_entry(&ctx, ie, outfile);
529 got_hash_final(&ctx, hash);
530 n = fwrite(hash, 1, SHA1_DIGEST_LENGTH, outfile);
531 if (n != SHA1_DIGEST_LENGTH)
532 return got_ferror(outfile, GOT_ERR_IO);
534 if (fflush(outfile) != 0)
535 return got_error_from_errno("fflush");
540 static const struct got_error *
541 read_fileindex_val64(uint64_t *val, struct got_hash *ctx, FILE *infile)
545 n = fread(val, 1, sizeof(*val), infile);
546 if (n != sizeof(*val))
547 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
548 got_hash_update(ctx, val, sizeof(*val));
549 *val = be64toh(*val);
553 static const struct got_error *
554 read_fileindex_val32(uint32_t *val, struct got_hash *ctx, FILE *infile)
558 n = fread(val, 1, sizeof(*val), infile);
559 if (n != sizeof(*val))
560 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
561 got_hash_update(ctx, val, sizeof(*val));
562 *val = be32toh(*val);
566 static const struct got_error *
567 read_fileindex_val16(uint16_t *val, struct got_hash *ctx, FILE *infile)
571 n = fread(val, 1, sizeof(*val), infile);
572 if (n != sizeof(*val))
573 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
574 got_hash_update(ctx, val, sizeof(*val));
575 *val = be16toh(*val);
579 static const struct got_error *
580 read_fileindex_path(char **path, struct got_hash *ctx, FILE *infile)
582 const size_t chunk_size = 8;
587 if (len + chunk_size > sizeof(p))
588 return got_error(GOT_ERR_FILEIDX_BAD);
590 n = fread(&p[len], 1, chunk_size, infile);
592 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
594 got_hash_update(ctx, &p[len], chunk_size);
596 } while (memchr(&p[len - chunk_size], '\0', chunk_size) == NULL);
600 return got_error_from_errno("strdup");
604 static const struct got_error *
605 read_fileindex_entry(struct got_fileindex_entry **iep, struct got_hash *ctx,
606 FILE *infile, uint32_t version)
608 const struct got_error *err;
609 struct got_fileindex_entry *ie;
614 ie = calloc(1, sizeof(*ie));
616 return got_error_from_errno("calloc");
618 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
621 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
624 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
627 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
631 err = read_fileindex_val32(&ie->uid, ctx, infile);
634 err = read_fileindex_val32(&ie->gid, ctx, infile);
637 err = read_fileindex_val32(&ie->size, ctx, infile);
641 err = read_fileindex_val16(&ie->mode, ctx, infile);
645 n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
646 if (n != SHA1_DIGEST_LENGTH) {
647 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
650 got_hash_update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
652 n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
653 if (n != SHA1_DIGEST_LENGTH) {
654 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
657 got_hash_update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
659 err = read_fileindex_val32(&ie->flags, ctx, infile);
663 err = read_fileindex_path(&ie->path, ctx, infile);
668 uint32_t stage = got_fileindex_entry_stage_get(ie);
669 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
670 stage == GOT_FILEIDX_STAGE_ADD) {
671 n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
673 if (n != SHA1_DIGEST_LENGTH) {
674 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
677 got_hash_update(ctx, ie->staged_blob_sha1,
681 /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
682 ie->flags &= ~GOT_FILEIDX_F_STAGE;
687 got_fileindex_entry_free(ie);
693 const struct got_error *
694 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
696 const struct got_error *err = NULL;
697 struct got_fileindex_hdr hdr;
699 struct got_fileindex_entry *ie;
700 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
701 uint8_t sha1[SHA1_DIGEST_LENGTH];
705 got_hash_init(&ctx, GOT_HASH_SHA1);
707 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
708 if (n != sizeof(hdr.signature)) {
709 if (n == 0) /* EOF */
711 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
713 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
714 if (n != sizeof(hdr.version)) {
715 if (n == 0) /* EOF */
717 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
719 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
720 if (n != sizeof(hdr.nentries)) {
721 if (n == 0) /* EOF */
723 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
726 got_hash_update(&ctx, &hdr.signature, sizeof(hdr.signature));
727 got_hash_update(&ctx, &hdr.version, sizeof(hdr.version));
728 got_hash_update(&ctx, &hdr.nentries, sizeof(hdr.nentries));
730 hdr.signature = be32toh(hdr.signature);
731 hdr.version = be32toh(hdr.version);
732 hdr.nentries = be32toh(hdr.nentries);
734 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
735 return got_error(GOT_ERR_FILEIDX_SIG);
736 if (hdr.version > GOT_FILE_INDEX_VERSION)
737 return got_error(GOT_ERR_FILEIDX_VER);
739 for (i = 0; i < hdr.nentries; i++) {
740 err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
743 err = add_entry(fileindex, ie);
745 got_fileindex_entry_free(ie);
750 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
751 if (n != sizeof(sha1_expected))
752 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
753 got_hash_final(&ctx, sha1);
754 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
755 return got_error(GOT_ERR_FILEIDX_CSUM);
760 static struct got_fileindex_entry *
761 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
763 struct got_fileindex_entry *next;
765 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
767 /* Skip entries which were added or removed by diff callbacks. */
768 while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
769 GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
770 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
775 static const struct got_error *
776 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
777 struct got_tree_object *tree, const char *, const char *,
778 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
780 static const struct got_error *
781 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
782 struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
783 const char *path, const char *entry_name, struct got_repository *repo,
784 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
786 const struct got_error *err = NULL;
787 struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
789 if (!got_object_tree_entry_is_submodule(te) &&
790 S_ISDIR(got_tree_entry_get_mode(te))) {
792 struct got_tree_object *subtree;
794 if (asprintf(&subpath, "%s%s%s", path,
795 path[0] == '\0' ? "" : "/",
796 got_tree_entry_get_name(te)) == -1)
797 return got_error_from_errno("asprintf");
799 err = got_object_open_as_tree(&subtree, repo,
800 got_tree_entry_get_id(te));
806 err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
807 entry_name, repo, cb, cb_arg);
809 got_object_tree_close(subtree);
815 *next = got_object_tree_get_entry(tree, *tidx);
819 static const struct got_error *
820 diff_fileindex_tree(struct got_fileindex *fileindex,
821 struct got_fileindex_entry **ie, struct got_tree_object *tree,
822 const char *path, const char *entry_name, struct got_repository *repo,
823 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
825 const struct got_error *err = NULL;
826 struct got_tree_entry *te = NULL;
827 size_t path_len = strlen(path);
828 struct got_fileindex_entry *next;
831 te = got_object_tree_get_entry(tree, tidx);
832 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
835 const char *te_name = got_tree_entry_get_name(te);
837 if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
838 err = got_error_from_errno("asprintf");
841 cmp = got_path_cmp((*ie)->path, te_path,
842 got_fileindex_entry_path_len(*ie), strlen(te_path));
845 if (got_path_is_child((*ie)->path, path,
847 !got_object_tree_entry_is_submodule(te) &&
848 (entry_name == NULL ||
849 strcmp(te_name, entry_name) == 0)) {
850 err = cb->diff_old_new(cb_arg, *ie, te,
852 if (err || entry_name)
855 *ie = walk_fileindex(fileindex, *ie);
856 err = walk_tree(&te, fileindex, ie, tree, &tidx,
857 path, entry_name, repo, cb, cb_arg);
858 } else if (cmp < 0) {
859 next = walk_fileindex(fileindex, *ie);
860 if (got_path_is_child((*ie)->path, path,
861 path_len) && entry_name == NULL) {
862 err = cb->diff_old(cb_arg, *ie, path);
863 if (err || entry_name)
868 if ((entry_name == NULL ||
869 strcmp(te_name, entry_name) == 0)) {
870 err = cb->diff_new(cb_arg, te, path);
871 if (err || entry_name)
874 err = walk_tree(&te, fileindex, ie, tree, &tidx,
875 path, entry_name, repo, cb, cb_arg);
880 next = walk_fileindex(fileindex, *ie);
881 if (got_path_is_child((*ie)->path, path, path_len) &&
882 (entry_name == NULL ||
883 (te && strcmp(got_tree_entry_get_name(te),
884 entry_name) == 0))) {
885 err = cb->diff_old(cb_arg, *ie, path);
886 if (err || entry_name)
891 if (!got_object_tree_entry_is_submodule(te) &&
892 (entry_name == NULL ||
893 strcmp(got_tree_entry_get_name(te), entry_name)
895 err = cb->diff_new(cb_arg, te, path);
896 if (err || entry_name)
899 err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
900 entry_name, repo, cb, cb_arg);
909 const struct got_error *
910 got_fileindex_diff_tree(struct got_fileindex *fileindex,
911 struct got_tree_object *tree, const char *path, const char *entry_name,
912 struct got_repository *repo,
913 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
915 struct got_fileindex_entry *ie;
916 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
917 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
918 ie = walk_fileindex(fileindex, ie);
919 return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
923 static const struct got_error *
924 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
925 struct got_pathlist_head *, int, const char *, const char *,
926 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
928 static const struct got_error *
929 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
931 const struct got_error *err = NULL;
932 struct got_pathlist_entry *new = NULL;
933 struct dirent *dep = NULL;
934 struct dirent *de = NULL;
937 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
939 err = got_error_from_errno("malloc");
943 if (readdir_r(dir, de, &dep) != 0) {
944 err = got_error_from_errno("readdir_r");
953 if (strcmp(de->d_name, ".") == 0 ||
954 strcmp(de->d_name, "..") == 0 ||
956 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0) ||
958 strcmp(de->d_name, GOT_WORKTREE_CVG_DIR) == 0)) {
963 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
969 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
979 have_tracked_file_in_dir(struct got_fileindex *fileindex, const char *path)
981 struct got_fileindex_entry *ie;
982 size_t path_len = strlen(path);
985 ie = RB_ROOT(&fileindex->entries);
987 if (got_path_is_child(ie->path, path, path_len))
989 cmp = got_path_cmp(path, ie->path, path_len,
990 got_fileindex_entry_path_len(ie));
992 ie = RB_LEFT(ie, entry);
994 ie = RB_RIGHT(ie, entry);
1002 static const struct got_error *
1003 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
1004 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
1005 const char *path, const char *rootpath, struct got_repository *repo,
1006 int ignore, struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1008 const struct got_error *err = NULL;
1009 struct dirent *de = dle->data;
1015 /* Must traverse ignored directories if they contain tracked files. */
1016 if (de->d_type == DT_DIR && ignore &&
1017 have_tracked_file_in_dir(fileindex, path))
1020 if (de->d_type == DT_DIR && !ignore) {
1023 struct got_pathlist_head subdirlist;
1025 TAILQ_INIT(&subdirlist);
1027 if (asprintf(&subpath, "%s%s%s", path,
1028 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1029 return got_error_from_errno("asprintf");
1031 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
1033 return got_error_from_errno("asprintf");
1036 subdirfd = openat(fd, de->d_name,
1037 O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
1038 if (subdirfd == -1) {
1039 if (errno == EACCES) {
1040 *next = TAILQ_NEXT(dle, entry);
1043 err = got_error_from_errno2("openat", subdirpath);
1049 subdir = fdopendir(subdirfd);
1051 return got_error_from_errno2("fdopendir", path);
1053 err = read_dirlist(&subdirlist, subdir, subdirpath);
1060 err = diff_fileindex_dir(fileindex, ie, &subdirlist,
1061 dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
1062 if (subdir && closedir(subdir) == -1 && err == NULL)
1063 err = got_error_from_errno2("closedir", subdirpath);
1066 got_pathlist_free(&subdirlist, GOT_PATHLIST_FREE_DATA);
1071 *next = TAILQ_NEXT(dle, entry);
1075 static const struct got_error *
1076 dirent_type_fixup(struct dirent *de, const char *rootpath, const char *path)
1078 const struct got_error *err;
1082 if (de->d_type != DT_UNKNOWN)
1085 /* DT_UNKNOWN occurs on NFS mounts without "readdir plus" RPC. */
1086 if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
1087 return got_error_from_errno("asprintf");
1088 err = got_path_dirent_type(&type, dir_path, de);
1097 static const struct got_error *
1098 diff_fileindex_dir(struct got_fileindex *fileindex,
1099 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1100 int dirfd, const char *rootpath, const char *path,
1101 struct got_repository *repo,
1102 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1104 const struct got_error *err = NULL;
1105 struct dirent *de = NULL;
1106 size_t path_len = strlen(path);
1107 struct got_pathlist_entry *dle;
1110 if (cb->diff_traverse) {
1111 err = cb->diff_traverse(cb_arg, path, dirfd);
1116 dle = TAILQ_FIRST(dirlist);
1117 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1122 err = dirent_type_fixup(de, rootpath, path);
1125 if (asprintf(&de_path, "%s/%s", path,
1126 de->d_name) == -1) {
1127 err = got_error_from_errno("asprintf");
1130 cmp = got_path_cmp((*ie)->path, de_path,
1131 got_fileindex_entry_path_len(*ie),
1132 strlen(path) + 1 + de->d_namlen);
1135 err = cb->diff_old_new(cb_arg, *ie, de, path,
1139 *ie = walk_fileindex(fileindex, *ie);
1140 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1141 path, rootpath, repo, 0, cb, cb_arg);
1142 } else if (cmp < 0 ) {
1143 err = cb->diff_old(cb_arg, *ie, path);
1146 *ie = walk_fileindex(fileindex, *ie);
1148 err = cb->diff_new(&ignore, cb_arg, de, path,
1152 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1153 path, rootpath, repo, ignore, cb, cb_arg);
1158 err = cb->diff_old(cb_arg, *ie, path);
1161 *ie = walk_fileindex(fileindex, *ie);
1164 err = dirent_type_fixup(de, rootpath, path);
1167 err = cb->diff_new(&ignore, cb_arg, de, path, dirfd);
1170 err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1171 rootpath, repo, ignore, cb, cb_arg);
1180 const struct got_error *
1181 got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1182 const char *rootpath, const char *path, struct got_repository *repo,
1183 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1185 const struct got_error *err;
1186 struct got_fileindex_entry *ie;
1187 struct got_pathlist_head dirlist;
1191 TAILQ_INIT(&dirlist);
1194 * Duplicate the file descriptor so we can call closedir() below
1195 * without closing the file descriptor passed in by our caller.
1199 return got_error_from_errno2("dup", path);
1200 if (lseek(fd2, 0, SEEK_SET) == -1) {
1201 err = got_error_from_errno2("lseek", path);
1205 dir = fdopendir(fd2);
1207 err = got_error_from_errno2("fdopendir", path);
1211 err = read_dirlist(&dirlist, dir, path);
1217 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1218 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1219 ie = walk_fileindex(fileindex, ie);
1220 err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1221 rootpath, path, repo, cb, cb_arg);
1223 if (closedir(dir) == -1 && err == NULL)
1224 err = got_error_from_errno2("closedir", path);
1225 got_pathlist_free(&dirlist, GOT_PATHLIST_FREE_DATA);
1229 struct got_object_id *
1230 got_fileindex_entry_get_staged_blob_id(struct got_object_id *id,
1231 struct got_fileindex_entry *ie)
1233 memset(id, 0, sizeof(*id));
1234 memcpy(id->sha1, ie->staged_blob_sha1, sizeof(ie->staged_blob_sha1));
1238 struct got_object_id *
1239 got_fileindex_entry_get_blob_id(struct got_object_id *id,
1240 struct got_fileindex_entry *ie)
1242 memset(id, 0, sizeof(*id));
1243 memcpy(id->sha1, ie->blob_sha1, sizeof(ie->blob_sha1));
1247 struct got_object_id *
1248 got_fileindex_entry_get_commit_id(struct got_object_id *id,
1249 struct got_fileindex_entry *ie)
1251 memset(id, 0, sizeof(*id));
1252 memcpy(id->sha1, ie->commit_sha1, sizeof(ie->commit_sha1));
1256 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);