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 (sb.st_mode & S_IFLNK)
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 skip it once the index
226 * is written out to disk, and 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;
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(ie, got_fileindex_tree, &fileindex->entries) {
448 ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
449 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
451 err = write_fileindex_entry(&ctx, ie, outfile);
456 SHA1Final(sha1, &ctx);
457 n = fwrite(sha1, 1, sizeof(sha1), outfile);
458 if (n != sizeof(sha1))
459 return got_ferror(outfile, GOT_ERR_IO);
461 if (fflush(outfile) != 0)
462 return got_error_from_errno("fflush");
467 static const struct got_error *
468 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
472 n = fread(val, 1, sizeof(*val), infile);
473 if (n != sizeof(*val))
474 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
475 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
476 *val = be64toh(*val);
480 static const struct got_error *
481 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
485 n = fread(val, 1, sizeof(*val), infile);
486 if (n != sizeof(*val))
487 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
488 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
489 *val = be32toh(*val);
493 static const struct got_error *
494 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
498 n = fread(val, 1, sizeof(*val), infile);
499 if (n != sizeof(*val))
500 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
501 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
502 *val = be16toh(*val);
506 static const struct got_error *
507 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
509 const struct got_error *err = NULL;
510 const size_t chunk_size = 8;
511 size_t n, len = 0, totlen = chunk_size;
513 *path = malloc(totlen);
515 return got_error_from_errno("malloc");
518 if (len + chunk_size > totlen) {
519 char *p = reallocarray(*path, totlen + chunk_size, 1);
521 err = got_error_from_errno("reallocarray");
524 totlen += chunk_size;
527 n = fread(*path + len, 1, chunk_size, infile);
528 if (n != chunk_size) {
529 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
532 SHA1Update(ctx, *path + len, chunk_size);
534 } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
543 static const struct got_error *
544 read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
545 FILE *infile, uint32_t version)
547 const struct got_error *err;
548 struct got_fileindex_entry *ie;
553 ie = calloc(1, sizeof(*ie));
555 return got_error_from_errno("calloc");
557 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
560 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
563 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
566 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
570 err = read_fileindex_val32(&ie->uid, ctx, infile);
573 err = read_fileindex_val32(&ie->gid, ctx, infile);
576 err = read_fileindex_val32(&ie->size, ctx, infile);
580 err = read_fileindex_val16(&ie->mode, ctx, infile);
584 n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
585 if (n != SHA1_DIGEST_LENGTH) {
586 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
589 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
591 n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
592 if (n != SHA1_DIGEST_LENGTH) {
593 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
596 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
598 err = read_fileindex_val32(&ie->flags, ctx, infile);
602 err = read_fileindex_path(&ie->path, ctx, infile);
607 uint32_t stage = got_fileindex_entry_stage_get(ie);
608 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
609 stage == GOT_FILEIDX_STAGE_ADD) {
610 n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
612 if (n != SHA1_DIGEST_LENGTH) {
613 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
616 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
619 /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
620 ie->flags &= ~GOT_FILEIDX_F_STAGE;
625 got_fileindex_entry_free(ie);
631 const struct got_error *
632 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
634 const struct got_error *err = NULL;
635 struct got_fileindex_hdr hdr;
637 struct got_fileindex_entry *ie;
638 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
639 uint8_t sha1[SHA1_DIGEST_LENGTH];
645 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
646 if (n != sizeof(hdr.signature)) {
647 if (n == 0) /* EOF */
649 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
651 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
652 if (n != sizeof(hdr.version)) {
653 if (n == 0) /* EOF */
655 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
657 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
658 if (n != sizeof(hdr.nentries)) {
659 if (n == 0) /* EOF */
661 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
664 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
665 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
666 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
668 hdr.signature = be32toh(hdr.signature);
669 hdr.version = be32toh(hdr.version);
670 hdr.nentries = be32toh(hdr.nentries);
672 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
673 return got_error(GOT_ERR_FILEIDX_SIG);
674 if (hdr.version > GOT_FILE_INDEX_VERSION)
675 return got_error(GOT_ERR_FILEIDX_VER);
677 for (i = 0; i < hdr.nentries; i++) {
678 err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
681 err = add_entry(fileindex, ie);
686 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
687 if (n != sizeof(sha1_expected))
688 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
689 SHA1Final(sha1, &ctx);
690 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
691 return got_error(GOT_ERR_FILEIDX_CSUM);
696 static struct got_fileindex_entry *
697 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
699 struct got_fileindex_entry *next;
701 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
703 /* Skip entries which were added or removed by diff callbacks. */
704 while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
705 GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
706 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
711 static const struct got_error *
712 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
713 struct got_tree_object *tree, const char *, const char *,
714 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
716 static const struct got_error *
717 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
718 struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
719 const char *path, const char *entry_name, struct got_repository *repo,
720 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
722 const struct got_error *err = NULL;
723 struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
725 if (!got_object_tree_entry_is_submodule(te) &&
726 S_ISDIR(got_tree_entry_get_mode(te))) {
728 struct got_tree_object *subtree;
730 if (asprintf(&subpath, "%s%s%s", path,
731 path[0] == '\0' ? "" : "/",
732 got_tree_entry_get_name(te)) == -1)
733 return got_error_from_errno("asprintf");
735 err = got_object_open_as_tree(&subtree, repo,
736 got_tree_entry_get_id(te));
742 err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
743 entry_name, repo, cb, cb_arg);
745 got_object_tree_close(subtree);
751 *next = got_object_tree_get_entry(tree, *tidx);
755 static const struct got_error *
756 diff_fileindex_tree(struct got_fileindex *fileindex,
757 struct got_fileindex_entry **ie, struct got_tree_object *tree,
758 const char *path, const char *entry_name, struct got_repository *repo,
759 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
761 const struct got_error *err = NULL;
762 struct got_tree_entry *te = NULL;
763 size_t path_len = strlen(path);
764 struct got_fileindex_entry *next;
767 te = got_object_tree_get_entry(tree, tidx);
768 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
771 const char *te_name = got_tree_entry_get_name(te);
773 if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
774 err = got_error_from_errno("asprintf");
777 cmp = got_path_cmp((*ie)->path, te_path,
778 got_fileindex_entry_path_len(*ie), strlen(te_path));
781 if (got_path_is_child((*ie)->path, path,
783 !got_object_tree_entry_is_submodule(te) &&
784 (entry_name == NULL ||
785 strcmp(te_name, entry_name) == 0)) {
786 err = cb->diff_old_new(cb_arg, *ie, te,
788 if (err || entry_name)
791 *ie = walk_fileindex(fileindex, *ie);
792 err = walk_tree(&te, fileindex, ie, tree, &tidx,
793 path, entry_name, repo, cb, cb_arg);
794 } else if (cmp < 0) {
795 next = walk_fileindex(fileindex, *ie);
796 if (got_path_is_child((*ie)->path, path,
797 path_len) && (entry_name == NULL ||
798 strcmp(te_name, entry_name) == 0)) {
799 err = cb->diff_old(cb_arg, *ie, path);
800 if (err || entry_name)
805 if ((entry_name == NULL ||
806 strcmp(te_name, entry_name) == 0)) {
807 err = cb->diff_new(cb_arg, te, path);
808 if (err || entry_name)
811 err = walk_tree(&te, fileindex, ie, tree, &tidx,
812 path, entry_name, repo, cb, cb_arg);
817 next = walk_fileindex(fileindex, *ie);
818 if (got_path_is_child((*ie)->path, path, path_len) &&
819 (entry_name == NULL ||
820 (te && strcmp(got_tree_entry_get_name(te),
821 entry_name) == 0))) {
822 err = cb->diff_old(cb_arg, *ie, path);
823 if (err || entry_name)
828 if (!got_object_tree_entry_is_submodule(te) &&
829 (entry_name == NULL ||
830 strcmp(got_tree_entry_get_name(te), entry_name)
832 err = cb->diff_new(cb_arg, te, path);
833 if (err || entry_name)
836 err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
837 entry_name, repo, cb, cb_arg);
846 const struct got_error *
847 got_fileindex_diff_tree(struct got_fileindex *fileindex,
848 struct got_tree_object *tree, const char *path, const char *entry_name,
849 struct got_repository *repo,
850 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
852 struct got_fileindex_entry *ie;
853 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
854 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
855 ie = walk_fileindex(fileindex, ie);
856 return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
860 static const struct got_error *
861 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
862 struct got_pathlist_head *, int, const char *, const char *,
863 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
865 static const struct got_error *
866 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
868 const struct got_error *err = NULL;
869 struct got_pathlist_entry *new = NULL;
870 struct dirent *dep = NULL;
871 struct dirent *de = NULL;
874 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
876 err = got_error_from_errno("malloc");
880 if (readdir_r(dir, de, &dep) != 0) {
881 err = got_error_from_errno("readdir_r");
890 if (strcmp(de->d_name, ".") == 0 ||
891 strcmp(de->d_name, "..") == 0 ||
893 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
898 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
904 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
914 free_dirlist(struct got_pathlist_head *dirlist)
916 struct got_pathlist_entry *dle;
918 TAILQ_FOREACH(dle, dirlist, entry)
920 got_pathlist_free(dirlist);
923 static const struct got_error *
924 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
925 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
926 const char *path, const char *rootpath, struct got_repository *repo,
927 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
929 const struct got_error *err = NULL;
930 struct dirent *de = dle->data;
936 if (de->d_type == DT_DIR) {
939 struct got_pathlist_head subdirlist;
941 TAILQ_INIT(&subdirlist);
943 if (asprintf(&subpath, "%s%s%s", path,
944 path[0] == '\0' ? "" : "/", de->d_name) == -1)
945 return got_error_from_errno("asprintf");
947 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
949 return got_error_from_errno("asprintf");
952 subdirfd = openat(fd, de->d_name,
953 O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
954 if (subdirfd == -1) {
955 if (errno == EACCES) {
956 *next = TAILQ_NEXT(dle, entry);
959 err = got_error_from_errno2("openat", subdirpath);
965 subdir = fdopendir(subdirfd);
967 return got_error_from_errno2("fdopendir", path);
969 err = read_dirlist(&subdirlist, subdir, subdirpath);
976 err = diff_fileindex_dir(fileindex, ie, &subdirlist,
977 dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
978 if (subdir && closedir(subdir) == -1 && err == NULL)
979 err = got_error_from_errno2("closedir", subdirpath);
982 free_dirlist(&subdirlist);
987 *next = TAILQ_NEXT(dle, entry);
991 static const struct got_error *
992 diff_fileindex_dir(struct got_fileindex *fileindex,
993 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
994 int dirfd, const char *rootpath, const char *path,
995 struct got_repository *repo,
996 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
998 const struct got_error *err = NULL;
999 struct dirent *de = NULL;
1000 size_t path_len = strlen(path);
1001 struct got_pathlist_entry *dle;
1003 dle = TAILQ_FIRST(dirlist);
1004 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1009 if (asprintf(&de_path, "%s/%s", path,
1010 de->d_name) == -1) {
1011 err = got_error_from_errno("asprintf");
1014 cmp = got_path_cmp((*ie)->path, de_path,
1015 got_fileindex_entry_path_len(*ie),
1016 strlen(path) + 1 + de->d_namlen);
1019 err = cb->diff_old_new(cb_arg, *ie, de, path,
1023 *ie = walk_fileindex(fileindex, *ie);
1024 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1025 path, rootpath, repo, cb, cb_arg);
1026 } else if (cmp < 0 ) {
1027 err = cb->diff_old(cb_arg, *ie, path);
1030 *ie = walk_fileindex(fileindex, *ie);
1032 err = cb->diff_new(cb_arg, de, path, dirfd);
1035 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1036 path, rootpath, repo, cb, cb_arg);
1041 err = cb->diff_old(cb_arg, *ie, path);
1044 *ie = walk_fileindex(fileindex, *ie);
1047 err = cb->diff_new(cb_arg, de, path, dirfd);
1050 err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1051 rootpath, repo, cb, cb_arg);
1060 const struct got_error *
1061 got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1062 const char *rootpath, const char *path, struct got_repository *repo,
1063 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1065 const struct got_error *err;
1066 struct got_fileindex_entry *ie;
1067 struct got_pathlist_head dirlist;
1071 TAILQ_INIT(&dirlist);
1074 * Duplicate the file descriptor so we can call closedir() below
1075 * without closing the file descriptor passed in by our caller.
1079 return got_error_from_errno2("dup", path);
1080 if (lseek(fd2, 0, SEEK_SET) == -1) {
1081 err = got_error_from_errno2("lseek", path);
1085 dir = fdopendir(fd2);
1087 err = got_error_from_errno2("fdopendir", path);
1091 err = read_dirlist(&dirlist, dir, path);
1097 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1098 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1099 ie = walk_fileindex(fileindex, ie);
1100 err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1101 rootpath, path, repo, cb, cb_arg);
1103 if (closedir(dir) == -1 && err == NULL)
1104 err = got_error_from_errno2("closedir", path);
1105 free_dirlist(&dirlist);
1109 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);