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>
31 #include "got_error.h"
32 #include "got_object.h"
35 #include "got_lib_fileindex.h"
36 #include "got_lib_worktree.h"
38 /* got_fileindex_entry flags */
39 #define GOT_FILEIDX_F_PATH_LEN 0x00000fff
40 #define GOT_FILEIDX_F_STAGE 0x0000f000
41 #define GOT_FILEIDX_F_STAGE_SHIFT 12
42 #define GOT_FILEIDX_F_NOT_FLUSHED 0x00010000
43 #define GOT_FILEIDX_F_NO_BLOB 0x00020000
44 #define GOT_FILEIDX_F_NO_COMMIT 0x00040000
45 #define GOT_FILEIDX_F_NO_FILE_ON_DISK 0x00080000
47 struct got_fileindex {
48 struct got_fileindex_tree entries;
50 #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
54 got_fileindex_perms_from_st(struct stat *sb)
56 uint16_t perms = (sb->st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
57 return (perms << GOT_FILEIDX_MODE_PERMS_SHIFT);
61 got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
63 mode_t perms = (ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT);
64 return (S_IFREG | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
67 const struct got_error *
68 got_fileindex_entry_update(struct got_fileindex_entry *ie,
69 const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
70 int update_timestamps)
74 if (lstat(ondisk_path, &sb) != 0) {
75 if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
77 return got_error_from_errno2("lstat", ondisk_path);
79 if (sb.st_mode & S_IFDIR)
80 return got_error_set_errno(EISDIR, ondisk_path);
81 ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
85 if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
86 if (update_timestamps) {
87 ie->ctime_sec = sb.st_ctime;
88 ie->ctime_nsec = sb.st_ctimensec;
89 ie->mtime_sec = sb.st_mtime;
90 ie->mtime_nsec = sb.st_mtimensec;
94 ie->size = (sb.st_size & 0xffffffff);
95 if (sb.st_mode & S_IFLNK)
96 ie->mode = GOT_FILEIDX_MODE_SYMLINK;
98 ie->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
99 ie->mode |= got_fileindex_perms_from_st(&sb);
103 memcpy(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
104 ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
106 ie->flags |= GOT_FILEIDX_F_NO_BLOB;
109 memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
110 ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
112 ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
118 got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
120 ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
123 const struct got_error *
124 got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
125 const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
126 uint8_t *commit_sha1)
130 *ie = calloc(1, sizeof(**ie));
132 return got_error_from_errno("calloc");
134 (*ie)->path = strdup(relpath);
135 if ((*ie)->path == NULL) {
136 const struct got_error *err = got_error_from_errno("strdup");
142 len = strlen(relpath);
143 if (len > GOT_FILEIDX_F_PATH_LEN)
144 len = GOT_FILEIDX_F_PATH_LEN;
147 return got_fileindex_entry_update(*ie, ondisk_path, blob_sha1,
152 got_fileindex_entry_free(struct got_fileindex_entry *ie)
159 got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
161 return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
165 got_fileindex_entry_stage_get(const struct got_fileindex_entry *ie)
167 return ((ie->flags & GOT_FILEIDX_F_STAGE) >> GOT_FILEIDX_F_STAGE_SHIFT);
171 got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage)
173 ie->flags &= ~GOT_FILEIDX_F_STAGE;
174 ie->flags |= ((stage << GOT_FILEIDX_F_STAGE_SHIFT) &
175 GOT_FILEIDX_F_STAGE);
179 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
181 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
185 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
187 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
191 got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
193 return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
196 static const struct got_error *
197 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
199 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
200 return got_error(GOT_ERR_NO_SPACE);
202 RB_INSERT(got_fileindex_tree, &fileindex->entries, ie);
203 fileindex->nentries++;
207 const struct got_error *
208 got_fileindex_entry_add(struct got_fileindex *fileindex,
209 struct got_fileindex_entry *ie)
211 /* Flag this entry until it gets written out to disk. */
212 ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
214 return add_entry(fileindex, ie);
218 got_fileindex_entry_remove(struct got_fileindex *fileindex,
219 struct got_fileindex_entry *ie)
221 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
222 fileindex->nentries--;
225 struct got_fileindex_entry *
226 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
229 struct got_fileindex_entry key;
230 memset(&key, 0, sizeof(key));
231 key.path = (char *)path;
232 key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
233 return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
236 const struct got_error *
237 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
238 got_fileindex_cb cb, void *cb_arg)
240 const struct got_error *err;
241 struct got_fileindex_entry *ie, *tmp;
243 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
244 err = (*cb)(cb_arg, ie);
251 struct got_fileindex *
252 got_fileindex_alloc(void)
254 struct got_fileindex *fileindex;
256 fileindex = calloc(1, sizeof(*fileindex));
257 if (fileindex == NULL)
260 RB_INIT(&fileindex->entries);
265 got_fileindex_free(struct got_fileindex *fileindex)
267 struct got_fileindex_entry *ie;
269 while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
270 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
271 got_fileindex_entry_free(ie);
276 static const struct got_error *
277 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
282 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
283 n = fwrite(&val, 1, sizeof(val), outfile);
284 if (n != sizeof(val))
285 return got_ferror(outfile, GOT_ERR_IO);
289 static const struct got_error *
290 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
295 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
296 n = fwrite(&val, 1, sizeof(val), outfile);
297 if (n != sizeof(val))
298 return got_ferror(outfile, GOT_ERR_IO);
302 static const struct got_error *
303 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
308 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
309 n = fwrite(&val, 1, sizeof(val), outfile);
310 if (n != sizeof(val))
311 return got_ferror(outfile, GOT_ERR_IO);
315 static const struct got_error *
316 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
318 size_t n, len, pad = 0;
319 static const uint8_t zero[8] = { 0 };
322 while ((len + pad) % 8 != 0)
325 pad = 8; /* NUL-terminate */
327 SHA1Update(ctx, path, len);
328 n = fwrite(path, 1, len, outfile);
330 return got_ferror(outfile, GOT_ERR_IO);
331 SHA1Update(ctx, zero, pad);
332 n = fwrite(zero, 1, pad, outfile);
334 return got_ferror(outfile, GOT_ERR_IO);
338 static const struct got_error *
339 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
342 const struct got_error *err;
346 err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
349 err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
352 err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
355 err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
359 err = write_fileindex_val32(ctx, ie->uid, outfile);
362 err = write_fileindex_val32(ctx, ie->gid, outfile);
365 err = write_fileindex_val32(ctx, ie->size, outfile);
369 err = write_fileindex_val16(ctx, ie->mode, outfile);
373 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
374 n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
375 if (n != SHA1_DIGEST_LENGTH)
376 return got_ferror(outfile, GOT_ERR_IO);
378 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
379 n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
380 if (n != SHA1_DIGEST_LENGTH)
381 return got_ferror(outfile, GOT_ERR_IO);
383 err = write_fileindex_val32(ctx, ie->flags, outfile);
387 err = write_fileindex_path(ctx, ie->path, outfile);
391 stage = got_fileindex_entry_stage_get(ie);
392 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
393 stage == GOT_FILEIDX_STAGE_ADD) {
394 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
395 n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
397 if (n != SHA1_DIGEST_LENGTH)
398 return got_ferror(outfile, GOT_ERR_IO);
404 const struct got_error *
405 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
407 const struct got_error *err = NULL;
408 struct got_fileindex_hdr hdr;
410 uint8_t sha1[SHA1_DIGEST_LENGTH];
412 struct got_fileindex_entry *ie;
416 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
417 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
418 hdr.nentries = htobe32(fileindex->nentries);
420 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
421 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
422 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
423 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
424 if (n != sizeof(hdr.signature))
425 return got_ferror(outfile, GOT_ERR_IO);
426 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
427 if (n != sizeof(hdr.version))
428 return got_ferror(outfile, GOT_ERR_IO);
429 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
430 if (n != sizeof(hdr.nentries))
431 return got_ferror(outfile, GOT_ERR_IO);
433 RB_FOREACH(ie, got_fileindex_tree, &fileindex->entries) {
434 ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
435 err = write_fileindex_entry(&ctx, ie, outfile);
440 SHA1Final(sha1, &ctx);
441 n = fwrite(sha1, 1, sizeof(sha1), outfile);
442 if (n != sizeof(sha1))
443 return got_ferror(outfile, GOT_ERR_IO);
445 if (fflush(outfile) != 0)
446 return got_error_from_errno("fflush");
451 static const struct got_error *
452 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
456 n = fread(val, 1, sizeof(*val), infile);
457 if (n != sizeof(*val))
458 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
459 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
460 *val = be64toh(*val);
464 static const struct got_error *
465 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
469 n = fread(val, 1, sizeof(*val), infile);
470 if (n != sizeof(*val))
471 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
472 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
473 *val = be32toh(*val);
477 static const struct got_error *
478 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
482 n = fread(val, 1, sizeof(*val), infile);
483 if (n != sizeof(*val))
484 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
485 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
486 *val = be16toh(*val);
490 static const struct got_error *
491 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
493 const struct got_error *err = NULL;
494 const size_t chunk_size = 8;
495 size_t n, len = 0, totlen = chunk_size;
497 *path = malloc(totlen);
499 return got_error_from_errno("malloc");
502 if (len + chunk_size > totlen) {
503 char *p = reallocarray(*path, totlen + chunk_size, 1);
505 err = got_error_from_errno("reallocarray");
508 totlen += chunk_size;
511 n = fread(*path + len, 1, chunk_size, infile);
512 if (n != chunk_size) {
513 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
516 SHA1Update(ctx, *path + len, chunk_size);
518 } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
527 static const struct got_error *
528 read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
529 FILE *infile, uint32_t version)
531 const struct got_error *err;
532 struct got_fileindex_entry *ie;
537 ie = calloc(1, sizeof(*ie));
539 return got_error_from_errno("calloc");
541 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
544 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
547 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
550 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
554 err = read_fileindex_val32(&ie->uid, ctx, infile);
557 err = read_fileindex_val32(&ie->gid, ctx, infile);
560 err = read_fileindex_val32(&ie->size, ctx, infile);
564 err = read_fileindex_val16(&ie->mode, ctx, infile);
568 n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
569 if (n != SHA1_DIGEST_LENGTH) {
570 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
573 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
575 n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
576 if (n != SHA1_DIGEST_LENGTH) {
577 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
580 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
582 err = read_fileindex_val32(&ie->flags, ctx, infile);
586 err = read_fileindex_path(&ie->path, ctx, infile);
591 uint32_t stage = got_fileindex_entry_stage_get(ie);
592 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
593 stage == GOT_FILEIDX_STAGE_ADD) {
594 n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
596 if (n != SHA1_DIGEST_LENGTH) {
597 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
600 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
603 /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
604 ie->flags &= ~GOT_FILEIDX_F_STAGE;
609 got_fileindex_entry_free(ie);
615 const struct got_error *
616 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
618 const struct got_error *err = NULL;
619 struct got_fileindex_hdr hdr;
621 struct got_fileindex_entry *ie;
622 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
623 uint8_t sha1[SHA1_DIGEST_LENGTH];
629 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
630 if (n != sizeof(hdr.signature)) {
631 if (n == 0) /* EOF */
633 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
635 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
636 if (n != sizeof(hdr.version)) {
637 if (n == 0) /* EOF */
639 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
641 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
642 if (n != sizeof(hdr.nentries)) {
643 if (n == 0) /* EOF */
645 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
648 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
649 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
650 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
652 hdr.signature = be32toh(hdr.signature);
653 hdr.version = be32toh(hdr.version);
654 hdr.nentries = be32toh(hdr.nentries);
656 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
657 return got_error(GOT_ERR_FILEIDX_SIG);
658 if (hdr.version > GOT_FILE_INDEX_VERSION)
659 return got_error(GOT_ERR_FILEIDX_VER);
661 for (i = 0; i < hdr.nentries; i++) {
662 err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
665 err = add_entry(fileindex, ie);
670 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
671 if (n != sizeof(sha1_expected))
672 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
673 SHA1Final(sha1, &ctx);
674 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
675 return got_error(GOT_ERR_FILEIDX_CSUM);
680 static struct got_fileindex_entry *
681 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
683 struct got_fileindex_entry *next;
685 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
687 /* Skip entries which were newly added by diff callbacks. */
688 while (next && (next->flags & GOT_FILEIDX_F_NOT_FLUSHED))
689 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
694 static const struct got_error *
695 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
696 const struct got_tree_entries *, const char *, const char *,
697 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
699 static const struct got_error *
700 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
701 struct got_fileindex_entry **ie, struct got_tree_entry *te,
702 const char *path, const char *entry_name, struct got_repository *repo,
703 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
705 const struct got_error *err = NULL;
707 if (!got_object_tree_entry_is_submodule(te) && S_ISDIR(te->mode)) {
709 struct got_tree_object *subtree;
711 if (asprintf(&subpath, "%s%s%s", path,
712 path[0] == '\0' ? "" : "/", te->name) == -1)
713 return got_error_from_errno("asprintf");
715 err = got_object_open_as_tree(&subtree, repo, te->id);
721 err = diff_fileindex_tree(fileindex, ie,
722 got_object_tree_get_entries(subtree), subpath, entry_name,
725 got_object_tree_close(subtree);
730 *next = SIMPLEQ_NEXT(te, entry);
734 static const struct got_error *
735 diff_fileindex_tree(struct got_fileindex *fileindex,
736 struct got_fileindex_entry **ie, const struct got_tree_entries *entries,
737 const char *path, const char *entry_name, struct got_repository *repo,
738 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
740 const struct got_error *err = NULL;
741 struct got_tree_entry *te = NULL;
742 size_t path_len = strlen(path);
743 struct got_fileindex_entry *next;
745 te = SIMPLEQ_FIRST(&entries->head);
746 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
750 if (asprintf(&te_path, "%s/%s", path, te->name) == -1) {
751 err = got_error_from_errno("asprintf");
754 cmp = got_path_cmp((*ie)->path, te_path,
755 got_fileindex_entry_path_len(*ie), strlen(te_path));
758 if (got_path_is_child((*ie)->path, path,
760 !got_object_tree_entry_is_submodule(te) &&
761 (entry_name == NULL ||
762 strcmp(te->name, entry_name) == 0)) {
763 err = cb->diff_old_new(cb_arg, *ie, te,
765 if (err || entry_name)
768 *ie = walk_fileindex(fileindex, *ie);
769 err = walk_tree(&te, fileindex, ie, te,
770 path, entry_name, repo, cb, cb_arg);
771 } else if (cmp < 0) {
772 next = walk_fileindex(fileindex, *ie);
773 if (got_path_is_child((*ie)->path, path,
774 path_len) && (entry_name == NULL ||
775 strcmp(te->name, entry_name) == 0)) {
776 err = cb->diff_old(cb_arg, *ie, path);
777 if (err || entry_name)
782 if ((entry_name == NULL ||
783 strcmp(te->name, entry_name) == 0)) {
784 err = cb->diff_new(cb_arg, te, path);
785 if (err || entry_name)
788 err = walk_tree(&te, fileindex, ie, te,
789 path, entry_name, repo, cb, cb_arg);
794 next = walk_fileindex(fileindex, *ie);
795 if (got_path_is_child((*ie)->path, path, path_len) &&
796 (entry_name == NULL ||
797 (te && strcmp(te->name, entry_name) == 0))) {
798 err = cb->diff_old(cb_arg, *ie, path);
799 if (err || entry_name)
804 if (!got_object_tree_entry_is_submodule(te) &&
805 (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, te, path,
812 entry_name, repo, cb, cb_arg);
821 const struct got_error *
822 got_fileindex_diff_tree(struct got_fileindex *fileindex,
823 struct got_tree_object *tree, const char *path, const char *entry_name,
824 struct got_repository *repo,
825 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
827 struct got_fileindex_entry *ie;
828 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
829 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
830 ie = walk_fileindex(fileindex, ie);
831 return diff_fileindex_tree(fileindex, &ie,
832 got_object_tree_get_entries(tree), path, entry_name, repo,
836 static const struct got_error *
837 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
838 struct got_pathlist_head *, const char *, const char *,
839 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
841 static const struct got_error *
842 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
844 const struct got_error *err = NULL;
845 struct got_pathlist_entry *new = NULL;
846 struct dirent *dep = NULL;
847 struct dirent *de = NULL;
850 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
852 err = got_error_from_errno("malloc");
856 if (readdir_r(dir, de, &dep) != 0) {
857 err = got_error_from_errno("readdir_r");
866 if (strcmp(de->d_name, ".") == 0 ||
867 strcmp(de->d_name, "..") == 0 ||
869 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
874 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
880 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
890 free_dirlist(struct got_pathlist_head *dirlist)
892 struct got_pathlist_entry *dle;
894 TAILQ_FOREACH(dle, dirlist, entry)
896 got_pathlist_free(dirlist);
899 static const struct got_error *
900 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
901 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle,
902 const char *path, const char *rootpath, struct got_repository *repo,
903 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
905 const struct got_error *err = NULL;
906 struct dirent *de = dle->data;
910 if (de->d_type == DT_DIR) {
914 struct got_pathlist_head subdirlist;
916 TAILQ_INIT(&subdirlist);
918 if (asprintf(&subpath, "%s%s%s", path,
919 path[0] == '\0' ? "" : "/", de->d_name) == -1)
920 return got_error_from_errno("asprintf");
922 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
924 return got_error_from_errno("asprintf");
927 subdir = opendir(subdirpath);
928 if (subdir == NULL) {
929 if (errno == EACCES) {
930 *next = TAILQ_NEXT(dle, entry);
933 err = got_error_from_errno2("opendir", subdirpath);
939 err = read_dirlist(&subdirlist, subdir, subdirpath);
946 err = diff_fileindex_dir(fileindex, ie, &subdirlist, rootpath,
947 subpath, repo, cb, cb_arg);
951 free_dirlist(&subdirlist);
956 *next = TAILQ_NEXT(dle, entry);
960 static const struct got_error *
961 diff_fileindex_dir(struct got_fileindex *fileindex,
962 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
963 const char *rootpath, const char *path, struct got_repository *repo,
964 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
966 const struct got_error *err = NULL;
967 struct dirent *de = NULL;
968 size_t path_len = strlen(path);
969 struct got_pathlist_entry *dle;
971 dle = TAILQ_FIRST(dirlist);
972 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
977 if (asprintf(&de_path, "%s/%s", path,
979 err = got_error_from_errno("asprintf");
982 cmp = got_path_cmp((*ie)->path, de_path,
983 got_fileindex_entry_path_len(*ie),
984 strlen(path) + 1 + de->d_namlen);
987 err = cb->diff_old_new(cb_arg, *ie, de, path);
990 *ie = walk_fileindex(fileindex, *ie);
991 err = walk_dir(&dle, fileindex, ie, dle, path,
992 rootpath, repo, cb, cb_arg);
993 } else if (cmp < 0 ) {
994 err = cb->diff_old(cb_arg, *ie, path);
997 *ie = walk_fileindex(fileindex, *ie);
999 err = cb->diff_new(cb_arg, de, path);
1002 err = walk_dir(&dle, fileindex, ie, dle, path,
1003 rootpath, repo, cb, cb_arg);
1008 err = cb->diff_old(cb_arg, *ie, path);
1011 *ie = walk_fileindex(fileindex, *ie);
1014 err = cb->diff_new(cb_arg, de, path);
1017 err = walk_dir(&dle, fileindex, ie, dle, path,
1018 rootpath, repo, cb, cb_arg);
1027 const struct got_error *
1028 got_fileindex_diff_dir(struct got_fileindex *fileindex, DIR *rootdir,
1029 const char *rootpath, const char *path, struct got_repository *repo,
1030 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1032 const struct got_error *err;
1033 struct got_fileindex_entry *ie;
1034 struct got_pathlist_head dirlist;
1036 TAILQ_INIT(&dirlist);
1037 err = read_dirlist(&dirlist, rootdir, path);
1040 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1041 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1042 ie = walk_fileindex(fileindex, ie);
1043 err = diff_fileindex_dir(fileindex, &ie, &dirlist, rootpath, path,
1045 free_dirlist(&dirlist);
1049 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);