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
49 struct got_fileindex {
50 struct got_fileindex_tree entries;
52 #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
56 got_fileindex_perms_from_st(struct stat *sb)
58 uint16_t perms = (sb->st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
59 return (perms << GOT_FILEIDX_MODE_PERMS_SHIFT);
63 got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
65 mode_t perms = (ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT);
66 return (S_IFREG | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
69 const struct got_error *
70 got_fileindex_entry_update(struct got_fileindex_entry *ie,
71 const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
72 int update_timestamps)
76 if (lstat(ondisk_path, &sb) != 0) {
77 if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
79 return got_error_from_errno2("lstat", ondisk_path);
81 if (sb.st_mode & S_IFDIR)
82 return got_error_set_errno(EISDIR, ondisk_path);
83 ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
87 if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
88 if (update_timestamps) {
89 ie->ctime_sec = sb.st_ctime;
90 ie->ctime_nsec = sb.st_ctimensec;
91 ie->mtime_sec = sb.st_mtime;
92 ie->mtime_nsec = sb.st_mtimensec;
96 ie->size = (sb.st_size & 0xffffffff);
97 if (sb.st_mode & S_IFLNK)
98 ie->mode = GOT_FILEIDX_MODE_SYMLINK;
100 ie->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
101 ie->mode |= got_fileindex_perms_from_st(&sb);
105 memcpy(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
106 ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
108 ie->flags |= GOT_FILEIDX_F_NO_BLOB;
111 memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
112 ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
114 ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
120 got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
122 ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
125 const struct got_error *
126 got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
127 const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
128 uint8_t *commit_sha1)
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;
149 return got_fileindex_entry_update(*ie, ondisk_path, blob_sha1,
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)
223 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
224 fileindex->nentries--;
227 struct got_fileindex_entry *
228 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
231 struct got_fileindex_entry key;
232 memset(&key, 0, sizeof(key));
233 key.path = (char *)path;
234 key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
235 return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
238 const struct got_error *
239 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
240 got_fileindex_cb cb, void *cb_arg)
242 const struct got_error *err;
243 struct got_fileindex_entry *ie, *tmp;
245 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
246 err = (*cb)(cb_arg, ie);
253 struct got_fileindex *
254 got_fileindex_alloc(void)
256 struct got_fileindex *fileindex;
258 fileindex = calloc(1, sizeof(*fileindex));
259 if (fileindex == NULL)
262 RB_INIT(&fileindex->entries);
267 got_fileindex_free(struct got_fileindex *fileindex)
269 struct got_fileindex_entry *ie;
271 while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
272 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
273 got_fileindex_entry_free(ie);
278 static const struct got_error *
279 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
284 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
285 n = fwrite(&val, 1, sizeof(val), outfile);
286 if (n != sizeof(val))
287 return got_ferror(outfile, GOT_ERR_IO);
291 static const struct got_error *
292 write_fileindex_val32(SHA1_CTX *ctx, uint32_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_val16(SHA1_CTX *ctx, uint16_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_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
320 size_t n, len, pad = 0;
321 static const uint8_t zero[8] = { 0 };
324 while ((len + pad) % 8 != 0)
327 pad = 8; /* NUL-terminate */
329 SHA1Update(ctx, path, len);
330 n = fwrite(path, 1, len, outfile);
332 return got_ferror(outfile, GOT_ERR_IO);
333 SHA1Update(ctx, zero, pad);
334 n = fwrite(zero, 1, pad, outfile);
336 return got_ferror(outfile, GOT_ERR_IO);
340 static const struct got_error *
341 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
344 const struct got_error *err;
348 err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
351 err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
354 err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
357 err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
361 err = write_fileindex_val32(ctx, ie->uid, outfile);
364 err = write_fileindex_val32(ctx, ie->gid, outfile);
367 err = write_fileindex_val32(ctx, ie->size, outfile);
371 err = write_fileindex_val16(ctx, ie->mode, outfile);
375 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
376 n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
377 if (n != SHA1_DIGEST_LENGTH)
378 return got_ferror(outfile, GOT_ERR_IO);
380 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
381 n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
382 if (n != SHA1_DIGEST_LENGTH)
383 return got_ferror(outfile, GOT_ERR_IO);
385 err = write_fileindex_val32(ctx, ie->flags, outfile);
389 err = write_fileindex_path(ctx, ie->path, outfile);
393 stage = got_fileindex_entry_stage_get(ie);
394 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
395 stage == GOT_FILEIDX_STAGE_ADD) {
396 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
397 n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
399 if (n != SHA1_DIGEST_LENGTH)
400 return got_ferror(outfile, GOT_ERR_IO);
406 const struct got_error *
407 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
409 const struct got_error *err = NULL;
410 struct got_fileindex_hdr hdr;
412 uint8_t sha1[SHA1_DIGEST_LENGTH];
414 struct got_fileindex_entry *ie;
418 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
419 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
420 hdr.nentries = htobe32(fileindex->nentries);
422 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
423 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
424 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
425 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
426 if (n != sizeof(hdr.signature))
427 return got_ferror(outfile, GOT_ERR_IO);
428 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
429 if (n != sizeof(hdr.version))
430 return got_ferror(outfile, GOT_ERR_IO);
431 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
432 if (n != sizeof(hdr.nentries))
433 return got_ferror(outfile, GOT_ERR_IO);
435 RB_FOREACH(ie, got_fileindex_tree, &fileindex->entries) {
436 ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
437 err = write_fileindex_entry(&ctx, ie, outfile);
442 SHA1Final(sha1, &ctx);
443 n = fwrite(sha1, 1, sizeof(sha1), outfile);
444 if (n != sizeof(sha1))
445 return got_ferror(outfile, GOT_ERR_IO);
447 if (fflush(outfile) != 0)
448 return got_error_from_errno("fflush");
453 static const struct got_error *
454 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
458 n = fread(val, 1, sizeof(*val), infile);
459 if (n != sizeof(*val))
460 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
461 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
462 *val = be64toh(*val);
466 static const struct got_error *
467 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
471 n = fread(val, 1, sizeof(*val), infile);
472 if (n != sizeof(*val))
473 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
474 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
475 *val = be32toh(*val);
479 static const struct got_error *
480 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
484 n = fread(val, 1, sizeof(*val), infile);
485 if (n != sizeof(*val))
486 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
487 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
488 *val = be16toh(*val);
492 static const struct got_error *
493 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
495 const struct got_error *err = NULL;
496 const size_t chunk_size = 8;
497 size_t n, len = 0, totlen = chunk_size;
499 *path = malloc(totlen);
501 return got_error_from_errno("malloc");
504 if (len + chunk_size > totlen) {
505 char *p = reallocarray(*path, totlen + chunk_size, 1);
507 err = got_error_from_errno("reallocarray");
510 totlen += chunk_size;
513 n = fread(*path + len, 1, chunk_size, infile);
514 if (n != chunk_size) {
515 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
518 SHA1Update(ctx, *path + len, chunk_size);
520 } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
529 static const struct got_error *
530 read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
531 FILE *infile, uint32_t version)
533 const struct got_error *err;
534 struct got_fileindex_entry *ie;
539 ie = calloc(1, sizeof(*ie));
541 return got_error_from_errno("calloc");
543 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
546 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
549 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
552 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
556 err = read_fileindex_val32(&ie->uid, ctx, infile);
559 err = read_fileindex_val32(&ie->gid, ctx, infile);
562 err = read_fileindex_val32(&ie->size, ctx, infile);
566 err = read_fileindex_val16(&ie->mode, ctx, infile);
570 n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
571 if (n != SHA1_DIGEST_LENGTH) {
572 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
575 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
577 n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
578 if (n != SHA1_DIGEST_LENGTH) {
579 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
582 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
584 err = read_fileindex_val32(&ie->flags, ctx, infile);
588 err = read_fileindex_path(&ie->path, ctx, infile);
593 uint32_t stage = got_fileindex_entry_stage_get(ie);
594 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
595 stage == GOT_FILEIDX_STAGE_ADD) {
596 n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
598 if (n != SHA1_DIGEST_LENGTH) {
599 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
602 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
605 /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
606 ie->flags &= ~GOT_FILEIDX_F_STAGE;
611 got_fileindex_entry_free(ie);
617 const struct got_error *
618 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
620 const struct got_error *err = NULL;
621 struct got_fileindex_hdr hdr;
623 struct got_fileindex_entry *ie;
624 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
625 uint8_t sha1[SHA1_DIGEST_LENGTH];
631 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
632 if (n != sizeof(hdr.signature)) {
633 if (n == 0) /* EOF */
635 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
637 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
638 if (n != sizeof(hdr.version)) {
639 if (n == 0) /* EOF */
641 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
643 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
644 if (n != sizeof(hdr.nentries)) {
645 if (n == 0) /* EOF */
647 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
650 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
651 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
652 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
654 hdr.signature = be32toh(hdr.signature);
655 hdr.version = be32toh(hdr.version);
656 hdr.nentries = be32toh(hdr.nentries);
658 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
659 return got_error(GOT_ERR_FILEIDX_SIG);
660 if (hdr.version > GOT_FILE_INDEX_VERSION)
661 return got_error(GOT_ERR_FILEIDX_VER);
663 for (i = 0; i < hdr.nentries; i++) {
664 err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
667 err = add_entry(fileindex, ie);
672 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
673 if (n != sizeof(sha1_expected))
674 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
675 SHA1Final(sha1, &ctx);
676 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
677 return got_error(GOT_ERR_FILEIDX_CSUM);
682 static struct got_fileindex_entry *
683 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
685 struct got_fileindex_entry *next;
687 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
689 /* Skip entries which were newly added by diff callbacks. */
690 while (next && (next->flags & GOT_FILEIDX_F_NOT_FLUSHED))
691 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
696 static const struct got_error *
697 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
698 struct got_tree_object *tree, const char *, const char *,
699 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
701 static const struct got_error *
702 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
703 struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
704 const char *path, const char *entry_name, struct got_repository *repo,
705 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
707 const struct got_error *err = NULL;
708 struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
710 if (!got_object_tree_entry_is_submodule(te) &&
711 S_ISDIR(got_tree_entry_get_mode(te))) {
713 struct got_tree_object *subtree;
715 if (asprintf(&subpath, "%s%s%s", path,
716 path[0] == '\0' ? "" : "/",
717 got_tree_entry_get_name(te)) == -1)
718 return got_error_from_errno("asprintf");
720 err = got_object_open_as_tree(&subtree, repo,
721 got_tree_entry_get_id(te));
727 err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
728 entry_name, repo, cb, cb_arg);
730 got_object_tree_close(subtree);
736 *next = got_object_tree_get_entry(tree, *tidx);
740 static const struct got_error *
741 diff_fileindex_tree(struct got_fileindex *fileindex,
742 struct got_fileindex_entry **ie, struct got_tree_object *tree,
743 const char *path, const char *entry_name, struct got_repository *repo,
744 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
746 const struct got_error *err = NULL;
747 struct got_tree_entry *te = NULL;
748 size_t path_len = strlen(path);
749 struct got_fileindex_entry *next;
752 te = got_object_tree_get_entry(tree, tidx);
753 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
756 const char *te_name = got_tree_entry_get_name(te);
758 if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
759 err = got_error_from_errno("asprintf");
762 cmp = got_path_cmp((*ie)->path, te_path,
763 got_fileindex_entry_path_len(*ie), strlen(te_path));
766 if (got_path_is_child((*ie)->path, path,
768 !got_object_tree_entry_is_submodule(te) &&
769 (entry_name == NULL ||
770 strcmp(te_name, entry_name) == 0)) {
771 err = cb->diff_old_new(cb_arg, *ie, te,
773 if (err || entry_name)
776 *ie = walk_fileindex(fileindex, *ie);
777 err = walk_tree(&te, fileindex, ie, tree, &tidx,
778 path, entry_name, repo, cb, cb_arg);
779 } else if (cmp < 0) {
780 next = walk_fileindex(fileindex, *ie);
781 if (got_path_is_child((*ie)->path, path,
782 path_len) && (entry_name == NULL ||
783 strcmp(te_name, entry_name) == 0)) {
784 err = cb->diff_old(cb_arg, *ie, path);
785 if (err || entry_name)
790 if ((entry_name == NULL ||
791 strcmp(te_name, entry_name) == 0)) {
792 err = cb->diff_new(cb_arg, te, path);
793 if (err || entry_name)
796 err = walk_tree(&te, fileindex, ie, tree, &tidx,
797 path, entry_name, repo, cb, cb_arg);
802 next = walk_fileindex(fileindex, *ie);
803 if (got_path_is_child((*ie)->path, path, path_len) &&
804 (entry_name == NULL ||
805 (te && strcmp(got_tree_entry_get_name(te),
806 entry_name) == 0))) {
807 err = cb->diff_old(cb_arg, *ie, path);
808 if (err || entry_name)
813 if (!got_object_tree_entry_is_submodule(te) &&
814 (entry_name == NULL ||
815 strcmp(got_tree_entry_get_name(te), entry_name)
817 err = cb->diff_new(cb_arg, te, path);
818 if (err || entry_name)
821 err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
822 entry_name, repo, cb, cb_arg);
831 const struct got_error *
832 got_fileindex_diff_tree(struct got_fileindex *fileindex,
833 struct got_tree_object *tree, const char *path, const char *entry_name,
834 struct got_repository *repo,
835 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
837 struct got_fileindex_entry *ie;
838 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
839 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
840 ie = walk_fileindex(fileindex, ie);
841 return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
845 static const struct got_error *
846 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
847 struct got_pathlist_head *, int, const char *, const char *,
848 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
850 static const struct got_error *
851 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
853 const struct got_error *err = NULL;
854 struct got_pathlist_entry *new = NULL;
855 struct dirent *dep = NULL;
856 struct dirent *de = NULL;
859 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
861 err = got_error_from_errno("malloc");
865 if (readdir_r(dir, de, &dep) != 0) {
866 err = got_error_from_errno("readdir_r");
875 if (strcmp(de->d_name, ".") == 0 ||
876 strcmp(de->d_name, "..") == 0 ||
878 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
883 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
889 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
899 free_dirlist(struct got_pathlist_head *dirlist)
901 struct got_pathlist_entry *dle;
903 TAILQ_FOREACH(dle, dirlist, entry)
905 got_pathlist_free(dirlist);
908 static const struct got_error *
909 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
910 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
911 const char *path, const char *rootpath, struct got_repository *repo,
912 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
914 const struct got_error *err = NULL;
915 struct dirent *de = dle->data;
921 if (de->d_type == DT_DIR) {
924 struct got_pathlist_head subdirlist;
926 TAILQ_INIT(&subdirlist);
928 if (asprintf(&subpath, "%s%s%s", path,
929 path[0] == '\0' ? "" : "/", de->d_name) == -1)
930 return got_error_from_errno("asprintf");
932 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
934 return got_error_from_errno("asprintf");
937 subdirfd = openat(fd, de->d_name,
938 O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
939 if (subdirfd == -1) {
940 if (errno == EACCES) {
941 *next = TAILQ_NEXT(dle, entry);
944 err = got_error_from_errno2("openat", subdirpath);
950 subdir = fdopendir(subdirfd);
952 return got_error_from_errno2("fdopendir", path);
954 err = read_dirlist(&subdirlist, subdir, subdirpath);
961 err = diff_fileindex_dir(fileindex, ie, &subdirlist,
962 dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
963 if (subdir && closedir(subdir) == -1 && err == NULL)
964 err = got_error_from_errno2("closedir", subdirpath);
967 free_dirlist(&subdirlist);
972 *next = TAILQ_NEXT(dle, entry);
976 static const struct got_error *
977 diff_fileindex_dir(struct got_fileindex *fileindex,
978 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
979 int dirfd, const char *rootpath, const char *path,
980 struct got_repository *repo,
981 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
983 const struct got_error *err = NULL;
984 struct dirent *de = NULL;
985 size_t path_len = strlen(path);
986 struct got_pathlist_entry *dle;
988 dle = TAILQ_FIRST(dirlist);
989 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
994 if (asprintf(&de_path, "%s/%s", path,
996 err = got_error_from_errno("asprintf");
999 cmp = got_path_cmp((*ie)->path, de_path,
1000 got_fileindex_entry_path_len(*ie),
1001 strlen(path) + 1 + de->d_namlen);
1004 err = cb->diff_old_new(cb_arg, *ie, de, path,
1008 *ie = walk_fileindex(fileindex, *ie);
1009 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1010 path, rootpath, repo, cb, cb_arg);
1011 } else if (cmp < 0 ) {
1012 err = cb->diff_old(cb_arg, *ie, path);
1015 *ie = walk_fileindex(fileindex, *ie);
1017 err = cb->diff_new(cb_arg, de, path, dirfd);
1020 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1021 path, rootpath, repo, cb, cb_arg);
1026 err = cb->diff_old(cb_arg, *ie, path);
1029 *ie = walk_fileindex(fileindex, *ie);
1032 err = cb->diff_new(cb_arg, de, path, dirfd);
1035 err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1036 rootpath, repo, cb, cb_arg);
1045 const struct got_error *
1046 got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1047 const char *rootpath, const char *path, struct got_repository *repo,
1048 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1050 const struct got_error *err;
1051 struct got_fileindex_entry *ie;
1052 struct got_pathlist_head dirlist;
1056 TAILQ_INIT(&dirlist);
1059 * Duplicate the file descriptor so we can call closedir() below
1060 * without closing the file descriptor passed in by our caller.
1064 return got_error_from_errno2("dup", path);
1065 dir = fdopendir(fd2);
1067 return got_error_from_errno2("fdopendir", path);
1068 err = read_dirlist(&dirlist, dir, path);
1074 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1075 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1076 ie = walk_fileindex(fileindex, ie);
1077 err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1078 rootpath, path, repo, cb, cb_arg);
1080 if (closedir(dir) == -1 && err == NULL)
1081 err = got_error_from_errno2("closedir", path);
1082 free_dirlist(&dirlist);
1086 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);