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>
33 #include "got_error.h"
34 #include "got_object.h"
37 #include "got_lib_fileindex.h"
38 #include "got_lib_worktree.h"
40 /* got_fileindex_entry flags */
41 #define GOT_FILEIDX_F_PATH_LEN 0x00000fff
42 #define GOT_FILEIDX_F_STAGE 0x0000f000
43 #define GOT_FILEIDX_F_STAGE_SHIFT 12
44 #define GOT_FILEIDX_F_NOT_FLUSHED 0x00010000
45 #define GOT_FILEIDX_F_NO_BLOB 0x00020000
46 #define GOT_FILEIDX_F_NO_COMMIT 0x00040000
47 #define GOT_FILEIDX_F_NO_FILE_ON_DISK 0x00080000
48 #define GOT_FILEIDX_F_REMOVE_ON_FLUSH 0x00100000
50 struct got_fileindex {
51 struct got_fileindex_tree entries;
52 int nentries; /* Does not include entries marked for removal. */
53 #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
57 got_fileindex_perms_from_st(struct stat *sb)
59 uint16_t perms = (sb->st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
60 return (perms << GOT_FILEIDX_MODE_PERMS_SHIFT);
64 got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
66 mode_t perms = (ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT);
67 return (S_IFREG | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
70 const struct got_error *
71 got_fileindex_entry_update(struct got_fileindex_entry *ie,
72 const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
73 int update_timestamps)
77 if (lstat(ondisk_path, &sb) != 0) {
78 if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
80 return got_error_from_errno2("lstat", ondisk_path);
82 if (sb.st_mode & S_IFDIR)
83 return got_error_set_errno(EISDIR, ondisk_path);
84 ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
88 if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
89 if (update_timestamps) {
90 ie->ctime_sec = sb.st_ctime;
91 ie->ctime_nsec = sb.st_ctimensec;
92 ie->mtime_sec = sb.st_mtime;
93 ie->mtime_nsec = sb.st_mtimensec;
97 ie->size = (sb.st_size & 0xffffffff);
98 if (S_ISLNK(sb.st_mode))
99 ie->mode = GOT_FILEIDX_MODE_SYMLINK;
101 ie->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
102 ie->mode |= got_fileindex_perms_from_st(&sb);
106 memcpy(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
107 ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
109 ie->flags |= GOT_FILEIDX_F_NO_BLOB;
112 memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
113 ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
115 ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
121 got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
123 ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
126 const struct got_error *
127 got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
132 *ie = calloc(1, sizeof(**ie));
134 return got_error_from_errno("calloc");
136 (*ie)->path = strdup(relpath);
137 if ((*ie)->path == NULL) {
138 const struct got_error *err = got_error_from_errno("strdup");
144 len = strlen(relpath);
145 if (len > GOT_FILEIDX_F_PATH_LEN)
146 len = GOT_FILEIDX_F_PATH_LEN;
153 got_fileindex_entry_free(struct got_fileindex_entry *ie)
160 got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
162 return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
166 got_fileindex_entry_stage_get(const struct got_fileindex_entry *ie)
168 return ((ie->flags & GOT_FILEIDX_F_STAGE) >> GOT_FILEIDX_F_STAGE_SHIFT);
172 got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage)
174 ie->flags &= ~GOT_FILEIDX_F_STAGE;
175 ie->flags |= ((stage << GOT_FILEIDX_F_STAGE_SHIFT) &
176 GOT_FILEIDX_F_STAGE);
180 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
182 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
186 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
188 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
192 got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
194 return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
197 static const struct got_error *
198 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
200 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
201 return got_error(GOT_ERR_NO_SPACE);
203 RB_INSERT(got_fileindex_tree, &fileindex->entries, ie);
204 fileindex->nentries++;
208 const struct got_error *
209 got_fileindex_entry_add(struct got_fileindex *fileindex,
210 struct got_fileindex_entry *ie)
212 /* Flag this entry until it gets written out to disk. */
213 ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
215 return add_entry(fileindex, ie);
219 got_fileindex_entry_remove(struct got_fileindex *fileindex,
220 struct got_fileindex_entry *ie)
223 * Removing an entry from the RB tree immediately breaks
224 * in-progress iterations over file index entries.
225 * So flag this entry for removal and remove it once the index
226 * is written out to disk. Meanwhile, pretend this entry no longer
227 * exists if we get queried for it again before then.
229 ie->flags |= GOT_FILEIDX_F_REMOVE_ON_FLUSH;
230 fileindex->nentries--;
233 struct got_fileindex_entry *
234 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
237 struct got_fileindex_entry *ie;
238 struct got_fileindex_entry key;
239 memset(&key, 0, sizeof(key));
240 key.path = (char *)path;
241 key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
242 ie = RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
243 if (ie && (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH))
248 const struct got_error *
249 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
250 got_fileindex_cb cb, void *cb_arg)
252 const struct got_error *err;
253 struct got_fileindex_entry *ie, *tmp;
255 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
256 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
258 err = (*cb)(cb_arg, ie);
265 struct got_fileindex *
266 got_fileindex_alloc(void)
268 struct got_fileindex *fileindex;
270 fileindex = calloc(1, sizeof(*fileindex));
271 if (fileindex == NULL)
274 RB_INIT(&fileindex->entries);
279 got_fileindex_free(struct got_fileindex *fileindex)
281 struct got_fileindex_entry *ie;
283 while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
284 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
285 got_fileindex_entry_free(ie);
290 static const struct got_error *
291 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
296 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
297 n = fwrite(&val, 1, sizeof(val), outfile);
298 if (n != sizeof(val))
299 return got_ferror(outfile, GOT_ERR_IO);
303 static const struct got_error *
304 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
309 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
310 n = fwrite(&val, 1, sizeof(val), outfile);
311 if (n != sizeof(val))
312 return got_ferror(outfile, GOT_ERR_IO);
316 static const struct got_error *
317 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
322 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
323 n = fwrite(&val, 1, sizeof(val), outfile);
324 if (n != sizeof(val))
325 return got_ferror(outfile, GOT_ERR_IO);
329 static const struct got_error *
330 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
332 size_t n, len, pad = 0;
333 static const uint8_t zero[8] = { 0 };
336 while ((len + pad) % 8 != 0)
339 pad = 8; /* NUL-terminate */
341 SHA1Update(ctx, path, len);
342 n = fwrite(path, 1, len, outfile);
344 return got_ferror(outfile, GOT_ERR_IO);
345 SHA1Update(ctx, zero, pad);
346 n = fwrite(zero, 1, pad, outfile);
348 return got_ferror(outfile, GOT_ERR_IO);
352 static const struct got_error *
353 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
356 const struct got_error *err;
360 err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
363 err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
366 err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
369 err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
373 err = write_fileindex_val32(ctx, ie->uid, outfile);
376 err = write_fileindex_val32(ctx, ie->gid, outfile);
379 err = write_fileindex_val32(ctx, ie->size, outfile);
383 err = write_fileindex_val16(ctx, ie->mode, outfile);
387 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
388 n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
389 if (n != SHA1_DIGEST_LENGTH)
390 return got_ferror(outfile, GOT_ERR_IO);
392 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
393 n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
394 if (n != SHA1_DIGEST_LENGTH)
395 return got_ferror(outfile, GOT_ERR_IO);
397 err = write_fileindex_val32(ctx, ie->flags, outfile);
401 err = write_fileindex_path(ctx, ie->path, outfile);
405 stage = got_fileindex_entry_stage_get(ie);
406 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
407 stage == GOT_FILEIDX_STAGE_ADD) {
408 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
409 n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
411 if (n != SHA1_DIGEST_LENGTH)
412 return got_ferror(outfile, GOT_ERR_IO);
418 const struct got_error *
419 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
421 const struct got_error *err = NULL;
422 struct got_fileindex_hdr hdr;
424 uint8_t sha1[SHA1_DIGEST_LENGTH];
426 struct got_fileindex_entry *ie, *tmp;
430 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
431 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
432 hdr.nentries = htobe32(fileindex->nentries);
434 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
435 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
436 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
437 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
438 if (n != sizeof(hdr.signature))
439 return got_ferror(outfile, GOT_ERR_IO);
440 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
441 if (n != sizeof(hdr.version))
442 return got_ferror(outfile, GOT_ERR_IO);
443 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
444 if (n != sizeof(hdr.nentries))
445 return got_ferror(outfile, GOT_ERR_IO);
447 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
448 ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
449 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH) {
450 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
451 got_fileindex_entry_free(ie);
454 err = write_fileindex_entry(&ctx, ie, outfile);
459 SHA1Final(sha1, &ctx);
460 n = fwrite(sha1, 1, sizeof(sha1), outfile);
461 if (n != sizeof(sha1))
462 return got_ferror(outfile, GOT_ERR_IO);
464 if (fflush(outfile) != 0)
465 return got_error_from_errno("fflush");
470 static const struct got_error *
471 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
475 n = fread(val, 1, sizeof(*val), infile);
476 if (n != sizeof(*val))
477 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
478 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
479 *val = be64toh(*val);
483 static const struct got_error *
484 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
488 n = fread(val, 1, sizeof(*val), infile);
489 if (n != sizeof(*val))
490 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
491 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
492 *val = be32toh(*val);
496 static const struct got_error *
497 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
501 n = fread(val, 1, sizeof(*val), infile);
502 if (n != sizeof(*val))
503 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
504 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
505 *val = be16toh(*val);
509 static const struct got_error *
510 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
512 const struct got_error *err = NULL;
513 const size_t chunk_size = 8;
514 size_t n, len = 0, totlen = chunk_size;
516 *path = malloc(totlen);
518 return got_error_from_errno("malloc");
521 if (len + chunk_size > totlen) {
522 char *p = reallocarray(*path, totlen + chunk_size, 1);
524 err = got_error_from_errno("reallocarray");
527 totlen += chunk_size;
530 n = fread(*path + len, 1, chunk_size, infile);
531 if (n != chunk_size) {
532 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
535 SHA1Update(ctx, *path + len, chunk_size);
537 } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
546 static const struct got_error *
547 read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
548 FILE *infile, uint32_t version)
550 const struct got_error *err;
551 struct got_fileindex_entry *ie;
556 ie = calloc(1, sizeof(*ie));
558 return got_error_from_errno("calloc");
560 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
563 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
566 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
569 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
573 err = read_fileindex_val32(&ie->uid, ctx, infile);
576 err = read_fileindex_val32(&ie->gid, ctx, infile);
579 err = read_fileindex_val32(&ie->size, ctx, infile);
583 err = read_fileindex_val16(&ie->mode, ctx, infile);
587 n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
588 if (n != SHA1_DIGEST_LENGTH) {
589 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
592 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
594 n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
595 if (n != SHA1_DIGEST_LENGTH) {
596 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
599 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
601 err = read_fileindex_val32(&ie->flags, ctx, infile);
605 err = read_fileindex_path(&ie->path, ctx, infile);
610 uint32_t stage = got_fileindex_entry_stage_get(ie);
611 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
612 stage == GOT_FILEIDX_STAGE_ADD) {
613 n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
615 if (n != SHA1_DIGEST_LENGTH) {
616 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
619 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
622 /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
623 ie->flags &= ~GOT_FILEIDX_F_STAGE;
628 got_fileindex_entry_free(ie);
634 const struct got_error *
635 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
637 const struct got_error *err = NULL;
638 struct got_fileindex_hdr hdr;
640 struct got_fileindex_entry *ie;
641 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
642 uint8_t sha1[SHA1_DIGEST_LENGTH];
648 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
649 if (n != sizeof(hdr.signature)) {
650 if (n == 0) /* EOF */
652 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
654 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
655 if (n != sizeof(hdr.version)) {
656 if (n == 0) /* EOF */
658 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
660 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
661 if (n != sizeof(hdr.nentries)) {
662 if (n == 0) /* EOF */
664 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
667 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
668 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
669 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
671 hdr.signature = be32toh(hdr.signature);
672 hdr.version = be32toh(hdr.version);
673 hdr.nentries = be32toh(hdr.nentries);
675 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
676 return got_error(GOT_ERR_FILEIDX_SIG);
677 if (hdr.version > GOT_FILE_INDEX_VERSION)
678 return got_error(GOT_ERR_FILEIDX_VER);
680 for (i = 0; i < hdr.nentries; i++) {
681 err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
684 err = add_entry(fileindex, ie);
689 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
690 if (n != sizeof(sha1_expected))
691 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
692 SHA1Final(sha1, &ctx);
693 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
694 return got_error(GOT_ERR_FILEIDX_CSUM);
699 static struct got_fileindex_entry *
700 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
702 struct got_fileindex_entry *next;
704 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
706 /* Skip entries which were added or removed by diff callbacks. */
707 while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
708 GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
709 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
714 static const struct got_error *
715 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
716 struct got_tree_object *tree, const char *, const char *,
717 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
719 static const struct got_error *
720 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
721 struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
722 const char *path, const char *entry_name, struct got_repository *repo,
723 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
725 const struct got_error *err = NULL;
726 struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
728 if (!got_object_tree_entry_is_submodule(te) &&
729 S_ISDIR(got_tree_entry_get_mode(te))) {
731 struct got_tree_object *subtree;
733 if (asprintf(&subpath, "%s%s%s", path,
734 path[0] == '\0' ? "" : "/",
735 got_tree_entry_get_name(te)) == -1)
736 return got_error_from_errno("asprintf");
738 err = got_object_open_as_tree(&subtree, repo,
739 got_tree_entry_get_id(te));
745 err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
746 entry_name, repo, cb, cb_arg);
748 got_object_tree_close(subtree);
754 *next = got_object_tree_get_entry(tree, *tidx);
758 static const struct got_error *
759 diff_fileindex_tree(struct got_fileindex *fileindex,
760 struct got_fileindex_entry **ie, struct got_tree_object *tree,
761 const char *path, const char *entry_name, struct got_repository *repo,
762 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
764 const struct got_error *err = NULL;
765 struct got_tree_entry *te = NULL;
766 size_t path_len = strlen(path);
767 struct got_fileindex_entry *next;
770 te = got_object_tree_get_entry(tree, tidx);
771 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
774 const char *te_name = got_tree_entry_get_name(te);
776 if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
777 err = got_error_from_errno("asprintf");
780 cmp = got_path_cmp((*ie)->path, te_path,
781 got_fileindex_entry_path_len(*ie), strlen(te_path));
784 if (got_path_is_child((*ie)->path, path,
786 !got_object_tree_entry_is_submodule(te) &&
787 (entry_name == NULL ||
788 strcmp(te_name, entry_name) == 0)) {
789 err = cb->diff_old_new(cb_arg, *ie, te,
791 if (err || entry_name)
794 *ie = walk_fileindex(fileindex, *ie);
795 err = walk_tree(&te, fileindex, ie, tree, &tidx,
796 path, entry_name, repo, cb, cb_arg);
797 } else if (cmp < 0) {
798 next = walk_fileindex(fileindex, *ie);
799 if (got_path_is_child((*ie)->path, path,
800 path_len) && (entry_name == NULL ||
801 strcmp(te_name, entry_name) == 0)) {
802 err = cb->diff_old(cb_arg, *ie, path);
803 if (err || entry_name)
808 if ((entry_name == NULL ||
809 strcmp(te_name, entry_name) == 0)) {
810 err = cb->diff_new(cb_arg, te, path);
811 if (err || entry_name)
814 err = walk_tree(&te, fileindex, ie, tree, &tidx,
815 path, entry_name, repo, cb, cb_arg);
820 next = walk_fileindex(fileindex, *ie);
821 if (got_path_is_child((*ie)->path, path, path_len) &&
822 (entry_name == NULL ||
823 (te && strcmp(got_tree_entry_get_name(te),
824 entry_name) == 0))) {
825 err = cb->diff_old(cb_arg, *ie, path);
826 if (err || entry_name)
831 if (!got_object_tree_entry_is_submodule(te) &&
832 (entry_name == NULL ||
833 strcmp(got_tree_entry_get_name(te), entry_name)
835 err = cb->diff_new(cb_arg, te, path);
836 if (err || entry_name)
839 err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
840 entry_name, repo, cb, cb_arg);
849 const struct got_error *
850 got_fileindex_diff_tree(struct got_fileindex *fileindex,
851 struct got_tree_object *tree, const char *path, const char *entry_name,
852 struct got_repository *repo,
853 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
855 struct got_fileindex_entry *ie;
856 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
857 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
858 ie = walk_fileindex(fileindex, ie);
859 return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
863 static const struct got_error *
864 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
865 struct got_pathlist_head *, int, const char *, const char *,
866 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
868 static const struct got_error *
869 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
871 const struct got_error *err = NULL;
872 struct got_pathlist_entry *new = NULL;
873 struct dirent *dep = NULL;
874 struct dirent *de = NULL;
877 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
879 err = got_error_from_errno("malloc");
883 if (readdir_r(dir, de, &dep) != 0) {
884 err = got_error_from_errno("readdir_r");
893 if (strcmp(de->d_name, ".") == 0 ||
894 strcmp(de->d_name, "..") == 0 ||
896 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
901 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
907 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
917 free_dirlist(struct got_pathlist_head *dirlist)
919 struct got_pathlist_entry *dle;
921 TAILQ_FOREACH(dle, dirlist, entry)
923 got_pathlist_free(dirlist);
926 static const struct got_error *
927 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
928 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
929 const char *path, const char *rootpath, struct got_repository *repo,
930 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
932 const struct got_error *err = NULL;
933 struct dirent *de = dle->data;
940 if (de->d_type == DT_UNKNOWN) {
941 /* Occurs on NFS mounts without "readdir plus" RPC. */
943 if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
944 return got_error_from_errno("asprintf");
945 err = got_path_dirent_type(&type, dir_path, de);
952 if (type == DT_DIR) {
955 struct got_pathlist_head subdirlist;
957 TAILQ_INIT(&subdirlist);
959 if (asprintf(&subpath, "%s%s%s", path,
960 path[0] == '\0' ? "" : "/", de->d_name) == -1)
961 return got_error_from_errno("asprintf");
963 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
965 return got_error_from_errno("asprintf");
968 subdirfd = openat(fd, de->d_name,
969 O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
970 if (subdirfd == -1) {
971 if (errno == EACCES) {
972 *next = TAILQ_NEXT(dle, entry);
975 err = got_error_from_errno2("openat", subdirpath);
981 subdir = fdopendir(subdirfd);
983 return got_error_from_errno2("fdopendir", path);
985 err = read_dirlist(&subdirlist, subdir, subdirpath);
992 err = diff_fileindex_dir(fileindex, ie, &subdirlist,
993 dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
994 if (subdir && closedir(subdir) == -1 && err == NULL)
995 err = got_error_from_errno2("closedir", subdirpath);
998 free_dirlist(&subdirlist);
1003 *next = TAILQ_NEXT(dle, entry);
1007 static const struct got_error *
1008 diff_fileindex_dir(struct got_fileindex *fileindex,
1009 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1010 int dirfd, const char *rootpath, const char *path,
1011 struct got_repository *repo,
1012 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1014 const struct got_error *err = NULL;
1015 struct dirent *de = NULL;
1016 size_t path_len = strlen(path);
1017 struct got_pathlist_entry *dle;
1019 if (cb->diff_traverse) {
1020 err = cb->diff_traverse(cb_arg, path, dirfd);
1025 dle = TAILQ_FIRST(dirlist);
1026 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1031 if (asprintf(&de_path, "%s/%s", path,
1032 de->d_name) == -1) {
1033 err = got_error_from_errno("asprintf");
1036 cmp = got_path_cmp((*ie)->path, de_path,
1037 got_fileindex_entry_path_len(*ie),
1038 strlen(path) + 1 + de->d_namlen);
1041 err = cb->diff_old_new(cb_arg, *ie, de, path,
1045 *ie = walk_fileindex(fileindex, *ie);
1046 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1047 path, rootpath, repo, cb, cb_arg);
1048 } else if (cmp < 0 ) {
1049 err = cb->diff_old(cb_arg, *ie, path);
1052 *ie = walk_fileindex(fileindex, *ie);
1054 err = cb->diff_new(cb_arg, de, path, dirfd);
1057 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1058 path, rootpath, repo, cb, cb_arg);
1063 err = cb->diff_old(cb_arg, *ie, path);
1066 *ie = walk_fileindex(fileindex, *ie);
1069 err = cb->diff_new(cb_arg, de, path, dirfd);
1072 err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1073 rootpath, repo, cb, cb_arg);
1082 const struct got_error *
1083 got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1084 const char *rootpath, const char *path, struct got_repository *repo,
1085 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1087 const struct got_error *err;
1088 struct got_fileindex_entry *ie;
1089 struct got_pathlist_head dirlist;
1093 TAILQ_INIT(&dirlist);
1096 * Duplicate the file descriptor so we can call closedir() below
1097 * without closing the file descriptor passed in by our caller.
1101 return got_error_from_errno2("dup", path);
1102 if (lseek(fd2, 0, SEEK_SET) == -1) {
1103 err = got_error_from_errno2("lseek", path);
1107 dir = fdopendir(fd2);
1109 err = got_error_from_errno2("fdopendir", path);
1113 err = read_dirlist(&dirlist, dir, path);
1119 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1120 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1121 ie = walk_fileindex(fileindex, ie);
1122 err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1123 rootpath, path, repo, cb, cb_arg);
1125 if (closedir(dir) == -1 && err == NULL)
1126 err = got_error_from_errno2("closedir", path);
1127 free_dirlist(&dirlist);
1131 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);