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_filetype_get(struct got_fileindex_entry *ie)
183 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE);
186 const struct got_error *
187 got_fileindex_entry_filetype_set(struct got_fileindex_entry *ie, int type)
190 case GOT_FILEIDX_MODE_REGULAR_FILE:
191 case GOT_FILEIDX_MODE_SYMLINK:
192 case GOT_FILEIDX_MODE_BAD_SYMLINK:
195 return got_error(GOT_ERR_BAD_FILETYPE);
198 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE;
204 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
206 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
210 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
212 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
216 got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
218 return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
221 static const struct got_error *
222 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
224 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
225 return got_error(GOT_ERR_NO_SPACE);
227 RB_INSERT(got_fileindex_tree, &fileindex->entries, ie);
228 fileindex->nentries++;
232 const struct got_error *
233 got_fileindex_entry_add(struct got_fileindex *fileindex,
234 struct got_fileindex_entry *ie)
236 /* Flag this entry until it gets written out to disk. */
237 ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
239 return add_entry(fileindex, ie);
243 got_fileindex_entry_remove(struct got_fileindex *fileindex,
244 struct got_fileindex_entry *ie)
247 * Removing an entry from the RB tree immediately breaks
248 * in-progress iterations over file index entries.
249 * So flag this entry for removal and remove it once the index
250 * is written out to disk. Meanwhile, pretend this entry no longer
251 * exists if we get queried for it again before then.
253 ie->flags |= GOT_FILEIDX_F_REMOVE_ON_FLUSH;
254 fileindex->nentries--;
257 struct got_fileindex_entry *
258 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
261 struct got_fileindex_entry *ie;
262 struct got_fileindex_entry key;
263 memset(&key, 0, sizeof(key));
264 key.path = (char *)path;
265 key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
266 ie = RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
267 if (ie && (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH))
272 const struct got_error *
273 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
274 got_fileindex_cb cb, void *cb_arg)
276 const struct got_error *err;
277 struct got_fileindex_entry *ie, *tmp;
279 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
280 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
282 err = (*cb)(cb_arg, ie);
289 struct got_fileindex *
290 got_fileindex_alloc(void)
292 struct got_fileindex *fileindex;
294 fileindex = calloc(1, sizeof(*fileindex));
295 if (fileindex == NULL)
298 RB_INIT(&fileindex->entries);
303 got_fileindex_free(struct got_fileindex *fileindex)
305 struct got_fileindex_entry *ie;
307 while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
308 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
309 got_fileindex_entry_free(ie);
314 static const struct got_error *
315 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
320 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
321 n = fwrite(&val, 1, sizeof(val), outfile);
322 if (n != sizeof(val))
323 return got_ferror(outfile, GOT_ERR_IO);
327 static const struct got_error *
328 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
333 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
334 n = fwrite(&val, 1, sizeof(val), outfile);
335 if (n != sizeof(val))
336 return got_ferror(outfile, GOT_ERR_IO);
340 static const struct got_error *
341 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
346 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
347 n = fwrite(&val, 1, sizeof(val), outfile);
348 if (n != sizeof(val))
349 return got_ferror(outfile, GOT_ERR_IO);
353 static const struct got_error *
354 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
356 size_t n, len, pad = 0;
357 static const uint8_t zero[8] = { 0 };
360 while ((len + pad) % 8 != 0)
363 pad = 8; /* NUL-terminate */
365 SHA1Update(ctx, path, len);
366 n = fwrite(path, 1, len, outfile);
368 return got_ferror(outfile, GOT_ERR_IO);
369 SHA1Update(ctx, zero, pad);
370 n = fwrite(zero, 1, pad, outfile);
372 return got_ferror(outfile, GOT_ERR_IO);
376 static const struct got_error *
377 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
380 const struct got_error *err;
384 err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
387 err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
390 err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
393 err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
397 err = write_fileindex_val32(ctx, ie->uid, outfile);
400 err = write_fileindex_val32(ctx, ie->gid, outfile);
403 err = write_fileindex_val32(ctx, ie->size, outfile);
407 err = write_fileindex_val16(ctx, ie->mode, outfile);
411 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
412 n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
413 if (n != SHA1_DIGEST_LENGTH)
414 return got_ferror(outfile, GOT_ERR_IO);
416 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
417 n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
418 if (n != SHA1_DIGEST_LENGTH)
419 return got_ferror(outfile, GOT_ERR_IO);
421 err = write_fileindex_val32(ctx, ie->flags, outfile);
425 err = write_fileindex_path(ctx, ie->path, outfile);
429 stage = got_fileindex_entry_stage_get(ie);
430 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
431 stage == GOT_FILEIDX_STAGE_ADD) {
432 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
433 n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
435 if (n != SHA1_DIGEST_LENGTH)
436 return got_ferror(outfile, GOT_ERR_IO);
442 const struct got_error *
443 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
445 const struct got_error *err = NULL;
446 struct got_fileindex_hdr hdr;
448 uint8_t sha1[SHA1_DIGEST_LENGTH];
450 struct got_fileindex_entry *ie, *tmp;
454 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
455 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
456 hdr.nentries = htobe32(fileindex->nentries);
458 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
459 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
460 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
461 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
462 if (n != sizeof(hdr.signature))
463 return got_ferror(outfile, GOT_ERR_IO);
464 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
465 if (n != sizeof(hdr.version))
466 return got_ferror(outfile, GOT_ERR_IO);
467 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
468 if (n != sizeof(hdr.nentries))
469 return got_ferror(outfile, GOT_ERR_IO);
471 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
472 ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
473 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH) {
474 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
475 got_fileindex_entry_free(ie);
478 err = write_fileindex_entry(&ctx, ie, outfile);
483 SHA1Final(sha1, &ctx);
484 n = fwrite(sha1, 1, sizeof(sha1), outfile);
485 if (n != sizeof(sha1))
486 return got_ferror(outfile, GOT_ERR_IO);
488 if (fflush(outfile) != 0)
489 return got_error_from_errno("fflush");
494 static const struct got_error *
495 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
499 n = fread(val, 1, sizeof(*val), infile);
500 if (n != sizeof(*val))
501 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
502 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
503 *val = be64toh(*val);
507 static const struct got_error *
508 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
512 n = fread(val, 1, sizeof(*val), infile);
513 if (n != sizeof(*val))
514 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
515 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
516 *val = be32toh(*val);
520 static const struct got_error *
521 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
525 n = fread(val, 1, sizeof(*val), infile);
526 if (n != sizeof(*val))
527 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
528 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
529 *val = be16toh(*val);
533 static const struct got_error *
534 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
536 const struct got_error *err = NULL;
537 const size_t chunk_size = 8;
538 size_t n, len = 0, totlen = chunk_size;
540 *path = malloc(totlen);
542 return got_error_from_errno("malloc");
545 if (len + chunk_size > totlen) {
546 char *p = reallocarray(*path, totlen + chunk_size, 1);
548 err = got_error_from_errno("reallocarray");
551 totlen += chunk_size;
554 n = fread(*path + len, 1, chunk_size, infile);
555 if (n != chunk_size) {
556 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
559 SHA1Update(ctx, *path + len, chunk_size);
561 } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
570 static const struct got_error *
571 read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
572 FILE *infile, uint32_t version)
574 const struct got_error *err;
575 struct got_fileindex_entry *ie;
580 ie = calloc(1, sizeof(*ie));
582 return got_error_from_errno("calloc");
584 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
587 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
590 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
593 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
597 err = read_fileindex_val32(&ie->uid, ctx, infile);
600 err = read_fileindex_val32(&ie->gid, ctx, infile);
603 err = read_fileindex_val32(&ie->size, ctx, infile);
607 err = read_fileindex_val16(&ie->mode, ctx, infile);
611 n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
612 if (n != SHA1_DIGEST_LENGTH) {
613 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
616 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
618 n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
619 if (n != SHA1_DIGEST_LENGTH) {
620 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
623 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
625 err = read_fileindex_val32(&ie->flags, ctx, infile);
629 err = read_fileindex_path(&ie->path, ctx, infile);
634 uint32_t stage = got_fileindex_entry_stage_get(ie);
635 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
636 stage == GOT_FILEIDX_STAGE_ADD) {
637 n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
639 if (n != SHA1_DIGEST_LENGTH) {
640 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
643 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
646 /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
647 ie->flags &= ~GOT_FILEIDX_F_STAGE;
652 got_fileindex_entry_free(ie);
658 const struct got_error *
659 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
661 const struct got_error *err = NULL;
662 struct got_fileindex_hdr hdr;
664 struct got_fileindex_entry *ie;
665 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
666 uint8_t sha1[SHA1_DIGEST_LENGTH];
672 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
673 if (n != sizeof(hdr.signature)) {
674 if (n == 0) /* EOF */
676 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
678 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
679 if (n != sizeof(hdr.version)) {
680 if (n == 0) /* EOF */
682 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
684 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
685 if (n != sizeof(hdr.nentries)) {
686 if (n == 0) /* EOF */
688 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
691 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
692 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
693 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
695 hdr.signature = be32toh(hdr.signature);
696 hdr.version = be32toh(hdr.version);
697 hdr.nentries = be32toh(hdr.nentries);
699 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
700 return got_error(GOT_ERR_FILEIDX_SIG);
701 if (hdr.version > GOT_FILE_INDEX_VERSION)
702 return got_error(GOT_ERR_FILEIDX_VER);
704 for (i = 0; i < hdr.nentries; i++) {
705 err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
708 err = add_entry(fileindex, ie);
713 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
714 if (n != sizeof(sha1_expected))
715 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
716 SHA1Final(sha1, &ctx);
717 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
718 return got_error(GOT_ERR_FILEIDX_CSUM);
723 static struct got_fileindex_entry *
724 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
726 struct got_fileindex_entry *next;
728 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
730 /* Skip entries which were added or removed by diff callbacks. */
731 while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
732 GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
733 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
738 static const struct got_error *
739 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
740 struct got_tree_object *tree, const char *, const char *,
741 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
743 static const struct got_error *
744 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
745 struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
746 const char *path, const char *entry_name, struct got_repository *repo,
747 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
749 const struct got_error *err = NULL;
750 struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
752 if (!got_object_tree_entry_is_submodule(te) &&
753 S_ISDIR(got_tree_entry_get_mode(te))) {
755 struct got_tree_object *subtree;
757 if (asprintf(&subpath, "%s%s%s", path,
758 path[0] == '\0' ? "" : "/",
759 got_tree_entry_get_name(te)) == -1)
760 return got_error_from_errno("asprintf");
762 err = got_object_open_as_tree(&subtree, repo,
763 got_tree_entry_get_id(te));
769 err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
770 entry_name, repo, cb, cb_arg);
772 got_object_tree_close(subtree);
778 *next = got_object_tree_get_entry(tree, *tidx);
782 static const struct got_error *
783 diff_fileindex_tree(struct got_fileindex *fileindex,
784 struct got_fileindex_entry **ie, struct got_tree_object *tree,
785 const char *path, const char *entry_name, struct got_repository *repo,
786 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
788 const struct got_error *err = NULL;
789 struct got_tree_entry *te = NULL;
790 size_t path_len = strlen(path);
791 struct got_fileindex_entry *next;
794 te = got_object_tree_get_entry(tree, tidx);
795 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
798 const char *te_name = got_tree_entry_get_name(te);
800 if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
801 err = got_error_from_errno("asprintf");
804 cmp = got_path_cmp((*ie)->path, te_path,
805 got_fileindex_entry_path_len(*ie), strlen(te_path));
808 if (got_path_is_child((*ie)->path, path,
810 !got_object_tree_entry_is_submodule(te) &&
811 (entry_name == NULL ||
812 strcmp(te_name, entry_name) == 0)) {
813 err = cb->diff_old_new(cb_arg, *ie, te,
815 if (err || entry_name)
818 *ie = walk_fileindex(fileindex, *ie);
819 err = walk_tree(&te, fileindex, ie, tree, &tidx,
820 path, entry_name, repo, cb, cb_arg);
821 } else if (cmp < 0) {
822 next = walk_fileindex(fileindex, *ie);
823 if (got_path_is_child((*ie)->path, path,
824 path_len) && (entry_name == NULL ||
825 strcmp(te_name, entry_name) == 0)) {
826 err = cb->diff_old(cb_arg, *ie, path);
827 if (err || entry_name)
832 if ((entry_name == NULL ||
833 strcmp(te_name, entry_name) == 0)) {
834 err = cb->diff_new(cb_arg, te, path);
835 if (err || entry_name)
838 err = walk_tree(&te, fileindex, ie, tree, &tidx,
839 path, entry_name, repo, cb, cb_arg);
844 next = walk_fileindex(fileindex, *ie);
845 if (got_path_is_child((*ie)->path, path, path_len) &&
846 (entry_name == NULL ||
847 (te && strcmp(got_tree_entry_get_name(te),
848 entry_name) == 0))) {
849 err = cb->diff_old(cb_arg, *ie, path);
850 if (err || entry_name)
855 if (!got_object_tree_entry_is_submodule(te) &&
856 (entry_name == NULL ||
857 strcmp(got_tree_entry_get_name(te), entry_name)
859 err = cb->diff_new(cb_arg, te, path);
860 if (err || entry_name)
863 err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
864 entry_name, repo, cb, cb_arg);
873 const struct got_error *
874 got_fileindex_diff_tree(struct got_fileindex *fileindex,
875 struct got_tree_object *tree, const char *path, const char *entry_name,
876 struct got_repository *repo,
877 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
879 struct got_fileindex_entry *ie;
880 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
881 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
882 ie = walk_fileindex(fileindex, ie);
883 return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
887 static const struct got_error *
888 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
889 struct got_pathlist_head *, int, const char *, const char *,
890 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
892 static const struct got_error *
893 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
895 const struct got_error *err = NULL;
896 struct got_pathlist_entry *new = NULL;
897 struct dirent *dep = NULL;
898 struct dirent *de = NULL;
901 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
903 err = got_error_from_errno("malloc");
907 if (readdir_r(dir, de, &dep) != 0) {
908 err = got_error_from_errno("readdir_r");
917 if (strcmp(de->d_name, ".") == 0 ||
918 strcmp(de->d_name, "..") == 0 ||
920 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
925 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
931 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
941 free_dirlist(struct got_pathlist_head *dirlist)
943 struct got_pathlist_entry *dle;
945 TAILQ_FOREACH(dle, dirlist, entry)
947 got_pathlist_free(dirlist);
950 static const struct got_error *
951 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
952 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
953 const char *path, const char *rootpath, struct got_repository *repo,
954 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
956 const struct got_error *err = NULL;
957 struct dirent *de = dle->data;
964 if (de->d_type == DT_UNKNOWN) {
965 /* Occurs on NFS mounts without "readdir plus" RPC. */
967 if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
968 return got_error_from_errno("asprintf");
969 err = got_path_dirent_type(&type, dir_path, de);
976 if (type == DT_DIR) {
979 struct got_pathlist_head subdirlist;
981 TAILQ_INIT(&subdirlist);
983 if (asprintf(&subpath, "%s%s%s", path,
984 path[0] == '\0' ? "" : "/", de->d_name) == -1)
985 return got_error_from_errno("asprintf");
987 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
989 return got_error_from_errno("asprintf");
992 subdirfd = openat(fd, de->d_name,
993 O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
994 if (subdirfd == -1) {
995 if (errno == EACCES) {
996 *next = TAILQ_NEXT(dle, entry);
999 err = got_error_from_errno2("openat", subdirpath);
1005 subdir = fdopendir(subdirfd);
1007 return got_error_from_errno2("fdopendir", path);
1009 err = read_dirlist(&subdirlist, subdir, subdirpath);
1016 err = diff_fileindex_dir(fileindex, ie, &subdirlist,
1017 dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
1018 if (subdir && closedir(subdir) == -1 && err == NULL)
1019 err = got_error_from_errno2("closedir", subdirpath);
1022 free_dirlist(&subdirlist);
1027 *next = TAILQ_NEXT(dle, entry);
1031 static const struct got_error *
1032 diff_fileindex_dir(struct got_fileindex *fileindex,
1033 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1034 int dirfd, const char *rootpath, const char *path,
1035 struct got_repository *repo,
1036 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1038 const struct got_error *err = NULL;
1039 struct dirent *de = NULL;
1040 size_t path_len = strlen(path);
1041 struct got_pathlist_entry *dle;
1043 if (cb->diff_traverse) {
1044 err = cb->diff_traverse(cb_arg, path, dirfd);
1049 dle = TAILQ_FIRST(dirlist);
1050 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1055 if (asprintf(&de_path, "%s/%s", path,
1056 de->d_name) == -1) {
1057 err = got_error_from_errno("asprintf");
1060 cmp = got_path_cmp((*ie)->path, de_path,
1061 got_fileindex_entry_path_len(*ie),
1062 strlen(path) + 1 + de->d_namlen);
1065 err = cb->diff_old_new(cb_arg, *ie, de, path,
1069 *ie = walk_fileindex(fileindex, *ie);
1070 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1071 path, rootpath, repo, cb, cb_arg);
1072 } else if (cmp < 0 ) {
1073 err = cb->diff_old(cb_arg, *ie, path);
1076 *ie = walk_fileindex(fileindex, *ie);
1078 err = cb->diff_new(cb_arg, de, path, dirfd);
1081 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1082 path, rootpath, repo, cb, cb_arg);
1087 err = cb->diff_old(cb_arg, *ie, path);
1090 *ie = walk_fileindex(fileindex, *ie);
1093 err = cb->diff_new(cb_arg, de, path, dirfd);
1096 err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1097 rootpath, repo, cb, cb_arg);
1106 const struct got_error *
1107 got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1108 const char *rootpath, const char *path, struct got_repository *repo,
1109 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1111 const struct got_error *err;
1112 struct got_fileindex_entry *ie;
1113 struct got_pathlist_head dirlist;
1117 TAILQ_INIT(&dirlist);
1120 * Duplicate the file descriptor so we can call closedir() below
1121 * without closing the file descriptor passed in by our caller.
1125 return got_error_from_errno2("dup", path);
1126 if (lseek(fd2, 0, SEEK_SET) == -1) {
1127 err = got_error_from_errno2("lseek", path);
1131 dir = fdopendir(fd2);
1133 err = got_error_from_errno2("fdopendir", path);
1137 err = read_dirlist(&dirlist, dir, path);
1143 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1144 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1145 ie = walk_fileindex(fileindex, ie);
1146 err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1147 rootpath, path, repo, cb, cb_arg);
1149 if (closedir(dir) == -1 && err == NULL)
1150 err = got_error_from_errno2("closedir", path);
1151 free_dirlist(&dirlist);
1155 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);