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);
107 memcpy(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
108 ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
110 ie->flags |= GOT_FILEIDX_F_NO_BLOB;
113 memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
114 ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
116 ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
122 got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
124 ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
127 const struct got_error *
128 got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
133 *ie = calloc(1, sizeof(**ie));
135 return got_error_from_errno("calloc");
137 (*ie)->path = strdup(relpath);
138 if ((*ie)->path == NULL) {
139 const struct got_error *err = got_error_from_errno("strdup");
145 len = strlen(relpath);
146 if (len > GOT_FILEIDX_F_PATH_LEN)
147 len = GOT_FILEIDX_F_PATH_LEN;
154 got_fileindex_entry_free(struct got_fileindex_entry *ie)
161 got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
163 return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
167 got_fileindex_entry_stage_get(const struct got_fileindex_entry *ie)
169 return ((ie->flags & GOT_FILEIDX_F_STAGE) >> GOT_FILEIDX_F_STAGE_SHIFT);
173 got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage)
175 ie->flags &= ~GOT_FILEIDX_F_STAGE;
176 ie->flags |= ((stage << GOT_FILEIDX_F_STAGE_SHIFT) &
177 GOT_FILEIDX_F_STAGE);
181 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
183 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
187 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
189 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
193 got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
195 return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
198 static const struct got_error *
199 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
201 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
202 return got_error(GOT_ERR_NO_SPACE);
204 RB_INSERT(got_fileindex_tree, &fileindex->entries, ie);
205 fileindex->nentries++;
209 const struct got_error *
210 got_fileindex_entry_add(struct got_fileindex *fileindex,
211 struct got_fileindex_entry *ie)
213 /* Flag this entry until it gets written out to disk. */
214 ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
216 return add_entry(fileindex, ie);
220 got_fileindex_entry_remove(struct got_fileindex *fileindex,
221 struct got_fileindex_entry *ie)
224 * Removing an entry from the RB tree immediately breaks
225 * in-progress iterations over file index entries.
226 * So flag this entry for removal and remove it once the index
227 * is written out to disk. Meanwhile, pretend this entry no longer
228 * exists if we get queried for it again before then.
230 ie->flags |= GOT_FILEIDX_F_REMOVE_ON_FLUSH;
231 fileindex->nentries--;
234 struct got_fileindex_entry *
235 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
238 struct got_fileindex_entry *ie;
239 struct got_fileindex_entry key;
240 memset(&key, 0, sizeof(key));
241 key.path = (char *)path;
242 key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
243 ie = RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
244 if (ie && (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH))
249 const struct got_error *
250 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
251 got_fileindex_cb cb, void *cb_arg)
253 const struct got_error *err;
254 struct got_fileindex_entry *ie, *tmp;
256 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
257 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
259 err = (*cb)(cb_arg, ie);
266 struct got_fileindex *
267 got_fileindex_alloc(void)
269 struct got_fileindex *fileindex;
271 fileindex = calloc(1, sizeof(*fileindex));
272 if (fileindex == NULL)
275 RB_INIT(&fileindex->entries);
280 got_fileindex_free(struct got_fileindex *fileindex)
282 struct got_fileindex_entry *ie;
284 while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
285 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
286 got_fileindex_entry_free(ie);
291 static const struct got_error *
292 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
297 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
298 n = fwrite(&val, 1, sizeof(val), outfile);
299 if (n != sizeof(val))
300 return got_ferror(outfile, GOT_ERR_IO);
304 static const struct got_error *
305 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
310 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
311 n = fwrite(&val, 1, sizeof(val), outfile);
312 if (n != sizeof(val))
313 return got_ferror(outfile, GOT_ERR_IO);
317 static const struct got_error *
318 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
323 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
324 n = fwrite(&val, 1, sizeof(val), outfile);
325 if (n != sizeof(val))
326 return got_ferror(outfile, GOT_ERR_IO);
330 static const struct got_error *
331 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
333 size_t n, len, pad = 0;
334 static const uint8_t zero[8] = { 0 };
337 while ((len + pad) % 8 != 0)
340 pad = 8; /* NUL-terminate */
342 SHA1Update(ctx, path, len);
343 n = fwrite(path, 1, len, outfile);
345 return got_ferror(outfile, GOT_ERR_IO);
346 SHA1Update(ctx, zero, pad);
347 n = fwrite(zero, 1, pad, outfile);
349 return got_ferror(outfile, GOT_ERR_IO);
353 static const struct got_error *
354 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
357 const struct got_error *err;
361 err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
364 err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
367 err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
370 err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
374 err = write_fileindex_val32(ctx, ie->uid, outfile);
377 err = write_fileindex_val32(ctx, ie->gid, outfile);
380 err = write_fileindex_val32(ctx, ie->size, outfile);
384 err = write_fileindex_val16(ctx, ie->mode, outfile);
388 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
389 n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
390 if (n != SHA1_DIGEST_LENGTH)
391 return got_ferror(outfile, GOT_ERR_IO);
393 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
394 n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
395 if (n != SHA1_DIGEST_LENGTH)
396 return got_ferror(outfile, GOT_ERR_IO);
398 err = write_fileindex_val32(ctx, ie->flags, outfile);
402 err = write_fileindex_path(ctx, ie->path, outfile);
406 stage = got_fileindex_entry_stage_get(ie);
407 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
408 stage == GOT_FILEIDX_STAGE_ADD) {
409 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
410 n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
412 if (n != SHA1_DIGEST_LENGTH)
413 return got_ferror(outfile, GOT_ERR_IO);
419 const struct got_error *
420 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
422 const struct got_error *err = NULL;
423 struct got_fileindex_hdr hdr;
425 uint8_t sha1[SHA1_DIGEST_LENGTH];
427 struct got_fileindex_entry *ie, *tmp;
431 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
432 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
433 hdr.nentries = htobe32(fileindex->nentries);
435 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
436 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
437 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
438 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
439 if (n != sizeof(hdr.signature))
440 return got_ferror(outfile, GOT_ERR_IO);
441 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
442 if (n != sizeof(hdr.version))
443 return got_ferror(outfile, GOT_ERR_IO);
444 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
445 if (n != sizeof(hdr.nentries))
446 return got_ferror(outfile, GOT_ERR_IO);
448 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
449 ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
450 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH) {
451 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
452 got_fileindex_entry_free(ie);
455 err = write_fileindex_entry(&ctx, ie, outfile);
460 SHA1Final(sha1, &ctx);
461 n = fwrite(sha1, 1, sizeof(sha1), outfile);
462 if (n != sizeof(sha1))
463 return got_ferror(outfile, GOT_ERR_IO);
465 if (fflush(outfile) != 0)
466 return got_error_from_errno("fflush");
471 static const struct got_error *
472 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
476 n = fread(val, 1, sizeof(*val), infile);
477 if (n != sizeof(*val))
478 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
479 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
480 *val = be64toh(*val);
484 static const struct got_error *
485 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
489 n = fread(val, 1, sizeof(*val), infile);
490 if (n != sizeof(*val))
491 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
492 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
493 *val = be32toh(*val);
497 static const struct got_error *
498 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
502 n = fread(val, 1, sizeof(*val), infile);
503 if (n != sizeof(*val))
504 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
505 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
506 *val = be16toh(*val);
510 static const struct got_error *
511 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
513 const struct got_error *err = NULL;
514 const size_t chunk_size = 8;
515 size_t n, len = 0, totlen = chunk_size;
517 *path = malloc(totlen);
519 return got_error_from_errno("malloc");
522 if (len + chunk_size > totlen) {
523 char *p = reallocarray(*path, totlen + chunk_size, 1);
525 err = got_error_from_errno("reallocarray");
528 totlen += chunk_size;
531 n = fread(*path + len, 1, chunk_size, infile);
532 if (n != chunk_size) {
533 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
536 SHA1Update(ctx, *path + len, chunk_size);
538 } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
547 static const struct got_error *
548 read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
549 FILE *infile, uint32_t version)
551 const struct got_error *err;
552 struct got_fileindex_entry *ie;
557 ie = calloc(1, sizeof(*ie));
559 return got_error_from_errno("calloc");
561 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
564 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
567 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
570 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
574 err = read_fileindex_val32(&ie->uid, ctx, infile);
577 err = read_fileindex_val32(&ie->gid, ctx, infile);
580 err = read_fileindex_val32(&ie->size, ctx, infile);
584 err = read_fileindex_val16(&ie->mode, ctx, infile);
588 n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
589 if (n != SHA1_DIGEST_LENGTH) {
590 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
593 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
595 n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
596 if (n != SHA1_DIGEST_LENGTH) {
597 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
600 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
602 err = read_fileindex_val32(&ie->flags, ctx, infile);
606 err = read_fileindex_path(&ie->path, ctx, infile);
611 uint32_t stage = got_fileindex_entry_stage_get(ie);
612 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
613 stage == GOT_FILEIDX_STAGE_ADD) {
614 n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
616 if (n != SHA1_DIGEST_LENGTH) {
617 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
620 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
623 /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
624 ie->flags &= ~GOT_FILEIDX_F_STAGE;
629 got_fileindex_entry_free(ie);
635 const struct got_error *
636 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
638 const struct got_error *err = NULL;
639 struct got_fileindex_hdr hdr;
641 struct got_fileindex_entry *ie;
642 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
643 uint8_t sha1[SHA1_DIGEST_LENGTH];
649 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
650 if (n != sizeof(hdr.signature)) {
651 if (n == 0) /* EOF */
653 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
655 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
656 if (n != sizeof(hdr.version)) {
657 if (n == 0) /* EOF */
659 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
661 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
662 if (n != sizeof(hdr.nentries)) {
663 if (n == 0) /* EOF */
665 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
668 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
669 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
670 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
672 hdr.signature = be32toh(hdr.signature);
673 hdr.version = be32toh(hdr.version);
674 hdr.nentries = be32toh(hdr.nentries);
676 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
677 return got_error(GOT_ERR_FILEIDX_SIG);
678 if (hdr.version > GOT_FILE_INDEX_VERSION)
679 return got_error(GOT_ERR_FILEIDX_VER);
681 for (i = 0; i < hdr.nentries; i++) {
682 err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
685 err = add_entry(fileindex, ie);
690 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
691 if (n != sizeof(sha1_expected))
692 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
693 SHA1Final(sha1, &ctx);
694 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
695 return got_error(GOT_ERR_FILEIDX_CSUM);
700 static struct got_fileindex_entry *
701 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
703 struct got_fileindex_entry *next;
705 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
707 /* Skip entries which were added or removed by diff callbacks. */
708 while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
709 GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
710 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
715 static const struct got_error *
716 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
717 struct got_tree_object *tree, const char *, const char *,
718 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
720 static const struct got_error *
721 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
722 struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
723 const char *path, const char *entry_name, struct got_repository *repo,
724 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
726 const struct got_error *err = NULL;
727 struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
729 if (!got_object_tree_entry_is_submodule(te) &&
730 S_ISDIR(got_tree_entry_get_mode(te))) {
732 struct got_tree_object *subtree;
734 if (asprintf(&subpath, "%s%s%s", path,
735 path[0] == '\0' ? "" : "/",
736 got_tree_entry_get_name(te)) == -1)
737 return got_error_from_errno("asprintf");
739 err = got_object_open_as_tree(&subtree, repo,
740 got_tree_entry_get_id(te));
746 err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
747 entry_name, repo, cb, cb_arg);
749 got_object_tree_close(subtree);
755 *next = got_object_tree_get_entry(tree, *tidx);
759 static const struct got_error *
760 diff_fileindex_tree(struct got_fileindex *fileindex,
761 struct got_fileindex_entry **ie, struct got_tree_object *tree,
762 const char *path, const char *entry_name, struct got_repository *repo,
763 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
765 const struct got_error *err = NULL;
766 struct got_tree_entry *te = NULL;
767 size_t path_len = strlen(path);
768 struct got_fileindex_entry *next;
771 te = got_object_tree_get_entry(tree, tidx);
772 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
775 const char *te_name = got_tree_entry_get_name(te);
777 if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
778 err = got_error_from_errno("asprintf");
781 cmp = got_path_cmp((*ie)->path, te_path,
782 got_fileindex_entry_path_len(*ie), strlen(te_path));
785 if (got_path_is_child((*ie)->path, path,
787 !got_object_tree_entry_is_submodule(te) &&
788 (entry_name == NULL ||
789 strcmp(te_name, entry_name) == 0)) {
790 err = cb->diff_old_new(cb_arg, *ie, te,
792 if (err || entry_name)
795 *ie = walk_fileindex(fileindex, *ie);
796 err = walk_tree(&te, fileindex, ie, tree, &tidx,
797 path, entry_name, repo, cb, cb_arg);
798 } else if (cmp < 0) {
799 next = walk_fileindex(fileindex, *ie);
800 if (got_path_is_child((*ie)->path, path,
801 path_len) && (entry_name == NULL ||
802 strcmp(te_name, entry_name) == 0)) {
803 err = cb->diff_old(cb_arg, *ie, path);
804 if (err || entry_name)
809 if ((entry_name == NULL ||
810 strcmp(te_name, entry_name) == 0)) {
811 err = cb->diff_new(cb_arg, te, path);
812 if (err || entry_name)
815 err = walk_tree(&te, fileindex, ie, tree, &tidx,
816 path, entry_name, repo, cb, cb_arg);
821 next = walk_fileindex(fileindex, *ie);
822 if (got_path_is_child((*ie)->path, path, path_len) &&
823 (entry_name == NULL ||
824 (te && strcmp(got_tree_entry_get_name(te),
825 entry_name) == 0))) {
826 err = cb->diff_old(cb_arg, *ie, path);
827 if (err || entry_name)
832 if (!got_object_tree_entry_is_submodule(te) &&
833 (entry_name == NULL ||
834 strcmp(got_tree_entry_get_name(te), entry_name)
836 err = cb->diff_new(cb_arg, te, path);
837 if (err || entry_name)
840 err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
841 entry_name, repo, cb, cb_arg);
850 const struct got_error *
851 got_fileindex_diff_tree(struct got_fileindex *fileindex,
852 struct got_tree_object *tree, const char *path, const char *entry_name,
853 struct got_repository *repo,
854 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
856 struct got_fileindex_entry *ie;
857 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
858 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
859 ie = walk_fileindex(fileindex, ie);
860 return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
864 static const struct got_error *
865 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
866 struct got_pathlist_head *, int, const char *, const char *,
867 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
869 static const struct got_error *
870 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
872 const struct got_error *err = NULL;
873 struct got_pathlist_entry *new = NULL;
874 struct dirent *dep = NULL;
875 struct dirent *de = NULL;
878 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
880 err = got_error_from_errno("malloc");
884 if (readdir_r(dir, de, &dep) != 0) {
885 err = got_error_from_errno("readdir_r");
894 if (strcmp(de->d_name, ".") == 0 ||
895 strcmp(de->d_name, "..") == 0 ||
897 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
902 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
908 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
918 free_dirlist(struct got_pathlist_head *dirlist)
920 struct got_pathlist_entry *dle;
922 TAILQ_FOREACH(dle, dirlist, entry)
924 got_pathlist_free(dirlist);
927 static const struct got_error *
928 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
929 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
930 const char *path, const char *rootpath, struct got_repository *repo,
931 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
933 const struct got_error *err = NULL;
934 struct dirent *de = dle->data;
941 if (de->d_type == DT_UNKNOWN) {
942 /* Occurs on NFS mounts without "readdir plus" RPC. */
944 if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
945 return got_error_from_errno("asprintf");
946 err = got_path_dirent_type(&type, dir_path, de);
953 if (type == DT_DIR) {
956 struct got_pathlist_head subdirlist;
958 TAILQ_INIT(&subdirlist);
960 if (asprintf(&subpath, "%s%s%s", path,
961 path[0] == '\0' ? "" : "/", de->d_name) == -1)
962 return got_error_from_errno("asprintf");
964 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
966 return got_error_from_errno("asprintf");
969 subdirfd = openat(fd, de->d_name,
970 O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
971 if (subdirfd == -1) {
972 if (errno == EACCES) {
973 *next = TAILQ_NEXT(dle, entry);
976 err = got_error_from_errno2("openat", subdirpath);
982 subdir = fdopendir(subdirfd);
984 return got_error_from_errno2("fdopendir", path);
986 err = read_dirlist(&subdirlist, subdir, subdirpath);
993 err = diff_fileindex_dir(fileindex, ie, &subdirlist,
994 dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
995 if (subdir && closedir(subdir) == -1 && err == NULL)
996 err = got_error_from_errno2("closedir", subdirpath);
999 free_dirlist(&subdirlist);
1004 *next = TAILQ_NEXT(dle, entry);
1008 static const struct got_error *
1009 diff_fileindex_dir(struct got_fileindex *fileindex,
1010 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1011 int dirfd, const char *rootpath, const char *path,
1012 struct got_repository *repo,
1013 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1015 const struct got_error *err = NULL;
1016 struct dirent *de = NULL;
1017 size_t path_len = strlen(path);
1018 struct got_pathlist_entry *dle;
1020 if (cb->diff_traverse) {
1021 err = cb->diff_traverse(cb_arg, path, dirfd);
1026 dle = TAILQ_FIRST(dirlist);
1027 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1032 if (asprintf(&de_path, "%s/%s", path,
1033 de->d_name) == -1) {
1034 err = got_error_from_errno("asprintf");
1037 cmp = got_path_cmp((*ie)->path, de_path,
1038 got_fileindex_entry_path_len(*ie),
1039 strlen(path) + 1 + de->d_namlen);
1042 err = cb->diff_old_new(cb_arg, *ie, de, path,
1046 *ie = walk_fileindex(fileindex, *ie);
1047 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1048 path, rootpath, repo, cb, cb_arg);
1049 } else if (cmp < 0 ) {
1050 err = cb->diff_old(cb_arg, *ie, path);
1053 *ie = walk_fileindex(fileindex, *ie);
1055 err = cb->diff_new(cb_arg, de, path, dirfd);
1058 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1059 path, rootpath, repo, cb, cb_arg);
1064 err = cb->diff_old(cb_arg, *ie, path);
1067 *ie = walk_fileindex(fileindex, *ie);
1070 err = cb->diff_new(cb_arg, de, path, dirfd);
1073 err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1074 rootpath, repo, cb, cb_arg);
1083 const struct got_error *
1084 got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1085 const char *rootpath, const char *path, struct got_repository *repo,
1086 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1088 const struct got_error *err;
1089 struct got_fileindex_entry *ie;
1090 struct got_pathlist_head dirlist;
1094 TAILQ_INIT(&dirlist);
1097 * Duplicate the file descriptor so we can call closedir() below
1098 * without closing the file descriptor passed in by our caller.
1102 return got_error_from_errno2("dup", path);
1103 if (lseek(fd2, 0, SEEK_SET) == -1) {
1104 err = got_error_from_errno2("lseek", path);
1108 dir = fdopendir(fd2);
1110 err = got_error_from_errno2("fdopendir", path);
1114 err = read_dirlist(&dirlist, dir, path);
1120 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1121 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1122 ie = walk_fileindex(fileindex, ie);
1123 err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1124 rootpath, path, repo, cb, cb_arg);
1126 if (closedir(dir) == -1 && err == NULL)
1127 err = got_error_from_errno2("closedir", path);
1128 free_dirlist(&dirlist);
1132 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);