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_entry_perms_get(struct got_fileindex_entry *ie)
59 return ((ie->mode & GOT_FILEIDX_MODE_PERMS) >>
60 GOT_FILEIDX_MODE_PERMS_SHIFT);
64 fileindex_entry_perms_set(struct got_fileindex_entry *ie, mode_t mode)
66 ie->mode &= ~GOT_FILEIDX_MODE_PERMS;
67 ie->mode |= ((mode << GOT_FILEIDX_MODE_PERMS_SHIFT) &
68 GOT_FILEIDX_MODE_PERMS);
72 got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
74 mode_t perms = got_fileindex_entry_perms_get(ie);
75 int type = got_fileindex_entry_filetype_get(ie);
78 if (type == GOT_FILEIDX_MODE_REGULAR_FILE ||
79 type == GOT_FILEIDX_MODE_BAD_SYMLINK)
84 return (ftype | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
87 const struct got_error *
88 got_fileindex_entry_update(struct got_fileindex_entry *ie,
89 const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
90 int update_timestamps)
94 if (lstat(ondisk_path, &sb) != 0) {
95 if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
97 return got_error_from_errno2("lstat", ondisk_path);
99 if (sb.st_mode & S_IFDIR)
100 return got_error_set_errno(EISDIR, ondisk_path);
101 ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
105 if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
106 if (update_timestamps) {
107 ie->ctime_sec = sb.st_ctime;
108 ie->ctime_nsec = sb.st_ctimensec;
109 ie->mtime_sec = sb.st_mtime;
110 ie->mtime_nsec = sb.st_mtimensec;
114 ie->size = (sb.st_size & 0xffffffff);
115 if (S_ISLNK(sb.st_mode)) {
116 got_fileindex_entry_filetype_set(ie,
117 GOT_FILEIDX_MODE_SYMLINK);
118 fileindex_entry_perms_set(ie, 0);
120 got_fileindex_entry_filetype_set(ie,
121 GOT_FILEIDX_MODE_REGULAR_FILE);
122 fileindex_entry_perms_set(ie,
123 sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
128 memcpy(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
129 ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
131 ie->flags |= GOT_FILEIDX_F_NO_BLOB;
134 memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
135 ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
137 ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
143 got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
145 ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
148 const struct got_error *
149 got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
154 *ie = calloc(1, sizeof(**ie));
156 return got_error_from_errno("calloc");
158 (*ie)->path = strdup(relpath);
159 if ((*ie)->path == NULL) {
160 const struct got_error *err = got_error_from_errno("strdup");
166 len = strlen(relpath);
167 if (len > GOT_FILEIDX_F_PATH_LEN)
168 len = GOT_FILEIDX_F_PATH_LEN;
175 got_fileindex_entry_free(struct got_fileindex_entry *ie)
182 got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
184 return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
188 got_fileindex_entry_stage_get(const struct got_fileindex_entry *ie)
190 return ((ie->flags & GOT_FILEIDX_F_STAGE) >> GOT_FILEIDX_F_STAGE_SHIFT);
194 got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage)
196 ie->flags &= ~GOT_FILEIDX_F_STAGE;
197 ie->flags |= ((stage << GOT_FILEIDX_F_STAGE_SHIFT) &
198 GOT_FILEIDX_F_STAGE);
202 got_fileindex_entry_filetype_get(struct got_fileindex_entry *ie)
204 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
208 got_fileindex_entry_filetype_set(struct got_fileindex_entry *ie, int type)
210 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_ONDISK;
211 ie->mode |= (type & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
215 got_fileindex_entry_staged_filetype_set(struct got_fileindex_entry *ie, int type)
217 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_STAGED;
218 ie->mode |= ((type << GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT) &
219 GOT_FILEIDX_MODE_FILE_TYPE_STAGED);
223 got_fileindex_entry_staged_filetype_get(struct got_fileindex_entry *ie)
225 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_STAGED) >>
226 GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT;
230 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
232 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
236 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
238 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
242 got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
244 return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
247 static const struct got_error *
248 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
250 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
251 return got_error(GOT_ERR_NO_SPACE);
253 RB_INSERT(got_fileindex_tree, &fileindex->entries, ie);
254 fileindex->nentries++;
258 const struct got_error *
259 got_fileindex_entry_add(struct got_fileindex *fileindex,
260 struct got_fileindex_entry *ie)
262 /* Flag this entry until it gets written out to disk. */
263 ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
265 return add_entry(fileindex, ie);
269 got_fileindex_entry_remove(struct got_fileindex *fileindex,
270 struct got_fileindex_entry *ie)
273 * Removing an entry from the RB tree immediately breaks
274 * in-progress iterations over file index entries.
275 * So flag this entry for removal and remove it once the index
276 * is written out to disk. Meanwhile, pretend this entry no longer
277 * exists if we get queried for it again before then.
279 ie->flags |= GOT_FILEIDX_F_REMOVE_ON_FLUSH;
280 fileindex->nentries--;
283 struct got_fileindex_entry *
284 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
287 struct got_fileindex_entry *ie;
288 struct got_fileindex_entry key;
289 memset(&key, 0, sizeof(key));
290 key.path = (char *)path;
291 key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
292 ie = RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
293 if (ie && (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH))
298 const struct got_error *
299 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
300 got_fileindex_cb cb, void *cb_arg)
302 const struct got_error *err;
303 struct got_fileindex_entry *ie, *tmp;
305 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
306 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
308 err = (*cb)(cb_arg, ie);
315 struct got_fileindex *
316 got_fileindex_alloc(void)
318 struct got_fileindex *fileindex;
320 fileindex = calloc(1, sizeof(*fileindex));
321 if (fileindex == NULL)
324 RB_INIT(&fileindex->entries);
329 got_fileindex_free(struct got_fileindex *fileindex)
331 struct got_fileindex_entry *ie;
333 while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
334 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
335 got_fileindex_entry_free(ie);
340 static const struct got_error *
341 write_fileindex_val64(SHA1_CTX *ctx, uint64_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_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
359 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
360 n = fwrite(&val, 1, sizeof(val), outfile);
361 if (n != sizeof(val))
362 return got_ferror(outfile, GOT_ERR_IO);
366 static const struct got_error *
367 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
372 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
373 n = fwrite(&val, 1, sizeof(val), outfile);
374 if (n != sizeof(val))
375 return got_ferror(outfile, GOT_ERR_IO);
379 static const struct got_error *
380 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
382 size_t n, len, pad = 0;
383 static const uint8_t zero[8] = { 0 };
386 while ((len + pad) % 8 != 0)
389 pad = 8; /* NUL-terminate */
391 SHA1Update(ctx, path, len);
392 n = fwrite(path, 1, len, outfile);
394 return got_ferror(outfile, GOT_ERR_IO);
395 SHA1Update(ctx, zero, pad);
396 n = fwrite(zero, 1, pad, outfile);
398 return got_ferror(outfile, GOT_ERR_IO);
402 static const struct got_error *
403 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
406 const struct got_error *err;
410 err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
413 err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
416 err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
419 err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
423 err = write_fileindex_val32(ctx, ie->uid, outfile);
426 err = write_fileindex_val32(ctx, ie->gid, outfile);
429 err = write_fileindex_val32(ctx, ie->size, outfile);
433 err = write_fileindex_val16(ctx, ie->mode, outfile);
437 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
438 n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
439 if (n != SHA1_DIGEST_LENGTH)
440 return got_ferror(outfile, GOT_ERR_IO);
442 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
443 n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
444 if (n != SHA1_DIGEST_LENGTH)
445 return got_ferror(outfile, GOT_ERR_IO);
447 err = write_fileindex_val32(ctx, ie->flags, outfile);
451 err = write_fileindex_path(ctx, ie->path, outfile);
455 stage = got_fileindex_entry_stage_get(ie);
456 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
457 stage == GOT_FILEIDX_STAGE_ADD) {
458 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
459 n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
461 if (n != SHA1_DIGEST_LENGTH)
462 return got_ferror(outfile, GOT_ERR_IO);
468 const struct got_error *
469 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
471 const struct got_error *err = NULL;
472 struct got_fileindex_hdr hdr;
474 uint8_t sha1[SHA1_DIGEST_LENGTH];
476 struct got_fileindex_entry *ie, *tmp;
480 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
481 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
482 hdr.nentries = htobe32(fileindex->nentries);
484 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
485 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
486 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
487 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
488 if (n != sizeof(hdr.signature))
489 return got_ferror(outfile, GOT_ERR_IO);
490 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
491 if (n != sizeof(hdr.version))
492 return got_ferror(outfile, GOT_ERR_IO);
493 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
494 if (n != sizeof(hdr.nentries))
495 return got_ferror(outfile, GOT_ERR_IO);
497 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
498 ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
499 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH) {
500 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
501 got_fileindex_entry_free(ie);
504 err = write_fileindex_entry(&ctx, ie, outfile);
509 SHA1Final(sha1, &ctx);
510 n = fwrite(sha1, 1, sizeof(sha1), outfile);
511 if (n != sizeof(sha1))
512 return got_ferror(outfile, GOT_ERR_IO);
514 if (fflush(outfile) != 0)
515 return got_error_from_errno("fflush");
520 static const struct got_error *
521 read_fileindex_val64(uint64_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 = be64toh(*val);
533 static const struct got_error *
534 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
538 n = fread(val, 1, sizeof(*val), infile);
539 if (n != sizeof(*val))
540 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
541 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
542 *val = be32toh(*val);
546 static const struct got_error *
547 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
551 n = fread(val, 1, sizeof(*val), infile);
552 if (n != sizeof(*val))
553 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
554 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
555 *val = be16toh(*val);
559 static const struct got_error *
560 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
562 const struct got_error *err = NULL;
563 const size_t chunk_size = 8;
564 size_t n, len = 0, totlen = chunk_size;
566 *path = malloc(totlen);
568 return got_error_from_errno("malloc");
571 if (len + chunk_size > totlen) {
572 char *p = reallocarray(*path, totlen + chunk_size, 1);
574 err = got_error_from_errno("reallocarray");
577 totlen += chunk_size;
580 n = fread(*path + len, 1, chunk_size, infile);
581 if (n != chunk_size) {
582 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
585 SHA1Update(ctx, *path + len, chunk_size);
587 } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
596 static const struct got_error *
597 read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
598 FILE *infile, uint32_t version)
600 const struct got_error *err;
601 struct got_fileindex_entry *ie;
606 ie = calloc(1, sizeof(*ie));
608 return got_error_from_errno("calloc");
610 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
613 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
616 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
619 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
623 err = read_fileindex_val32(&ie->uid, ctx, infile);
626 err = read_fileindex_val32(&ie->gid, ctx, infile);
629 err = read_fileindex_val32(&ie->size, ctx, infile);
633 err = read_fileindex_val16(&ie->mode, ctx, infile);
637 n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
638 if (n != SHA1_DIGEST_LENGTH) {
639 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
642 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
644 n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
645 if (n != SHA1_DIGEST_LENGTH) {
646 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
649 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
651 err = read_fileindex_val32(&ie->flags, ctx, infile);
655 err = read_fileindex_path(&ie->path, ctx, infile);
660 uint32_t stage = got_fileindex_entry_stage_get(ie);
661 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
662 stage == GOT_FILEIDX_STAGE_ADD) {
663 n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
665 if (n != SHA1_DIGEST_LENGTH) {
666 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
669 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
672 /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
673 ie->flags &= ~GOT_FILEIDX_F_STAGE;
678 got_fileindex_entry_free(ie);
684 const struct got_error *
685 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
687 const struct got_error *err = NULL;
688 struct got_fileindex_hdr hdr;
690 struct got_fileindex_entry *ie;
691 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
692 uint8_t sha1[SHA1_DIGEST_LENGTH];
698 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
699 if (n != sizeof(hdr.signature)) {
700 if (n == 0) /* EOF */
702 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
704 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
705 if (n != sizeof(hdr.version)) {
706 if (n == 0) /* EOF */
708 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
710 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
711 if (n != sizeof(hdr.nentries)) {
712 if (n == 0) /* EOF */
714 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
717 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
718 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
719 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
721 hdr.signature = be32toh(hdr.signature);
722 hdr.version = be32toh(hdr.version);
723 hdr.nentries = be32toh(hdr.nentries);
725 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
726 return got_error(GOT_ERR_FILEIDX_SIG);
727 if (hdr.version > GOT_FILE_INDEX_VERSION)
728 return got_error(GOT_ERR_FILEIDX_VER);
730 for (i = 0; i < hdr.nentries; i++) {
731 err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
734 err = add_entry(fileindex, ie);
739 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
740 if (n != sizeof(sha1_expected))
741 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
742 SHA1Final(sha1, &ctx);
743 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
744 return got_error(GOT_ERR_FILEIDX_CSUM);
749 static struct got_fileindex_entry *
750 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
752 struct got_fileindex_entry *next;
754 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
756 /* Skip entries which were added or removed by diff callbacks. */
757 while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
758 GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
759 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
764 static const struct got_error *
765 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
766 struct got_tree_object *tree, const char *, const char *,
767 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
769 static const struct got_error *
770 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
771 struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
772 const char *path, const char *entry_name, struct got_repository *repo,
773 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
775 const struct got_error *err = NULL;
776 struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
778 if (!got_object_tree_entry_is_submodule(te) &&
779 S_ISDIR(got_tree_entry_get_mode(te))) {
781 struct got_tree_object *subtree;
783 if (asprintf(&subpath, "%s%s%s", path,
784 path[0] == '\0' ? "" : "/",
785 got_tree_entry_get_name(te)) == -1)
786 return got_error_from_errno("asprintf");
788 err = got_object_open_as_tree(&subtree, repo,
789 got_tree_entry_get_id(te));
795 err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
796 entry_name, repo, cb, cb_arg);
798 got_object_tree_close(subtree);
804 *next = got_object_tree_get_entry(tree, *tidx);
808 static const struct got_error *
809 diff_fileindex_tree(struct got_fileindex *fileindex,
810 struct got_fileindex_entry **ie, struct got_tree_object *tree,
811 const char *path, const char *entry_name, struct got_repository *repo,
812 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
814 const struct got_error *err = NULL;
815 struct got_tree_entry *te = NULL;
816 size_t path_len = strlen(path);
817 struct got_fileindex_entry *next;
820 te = got_object_tree_get_entry(tree, tidx);
821 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
824 const char *te_name = got_tree_entry_get_name(te);
826 if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
827 err = got_error_from_errno("asprintf");
830 cmp = got_path_cmp((*ie)->path, te_path,
831 got_fileindex_entry_path_len(*ie), strlen(te_path));
834 if (got_path_is_child((*ie)->path, path,
836 !got_object_tree_entry_is_submodule(te) &&
837 (entry_name == NULL ||
838 strcmp(te_name, entry_name) == 0)) {
839 err = cb->diff_old_new(cb_arg, *ie, te,
841 if (err || entry_name)
844 *ie = walk_fileindex(fileindex, *ie);
845 err = walk_tree(&te, fileindex, ie, tree, &tidx,
846 path, entry_name, repo, cb, cb_arg);
847 } else if (cmp < 0) {
848 next = walk_fileindex(fileindex, *ie);
849 if (got_path_is_child((*ie)->path, path,
850 path_len) && (entry_name == NULL ||
851 strcmp(te_name, entry_name) == 0)) {
852 err = cb->diff_old(cb_arg, *ie, path);
853 if (err || entry_name)
858 if ((entry_name == NULL ||
859 strcmp(te_name, entry_name) == 0)) {
860 err = cb->diff_new(cb_arg, te, path);
861 if (err || entry_name)
864 err = walk_tree(&te, fileindex, ie, tree, &tidx,
865 path, entry_name, repo, cb, cb_arg);
870 next = walk_fileindex(fileindex, *ie);
871 if (got_path_is_child((*ie)->path, path, path_len) &&
872 (entry_name == NULL ||
873 (te && strcmp(got_tree_entry_get_name(te),
874 entry_name) == 0))) {
875 err = cb->diff_old(cb_arg, *ie, path);
876 if (err || entry_name)
881 if (!got_object_tree_entry_is_submodule(te) &&
882 (entry_name == NULL ||
883 strcmp(got_tree_entry_get_name(te), entry_name)
885 err = cb->diff_new(cb_arg, te, path);
886 if (err || entry_name)
889 err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
890 entry_name, repo, cb, cb_arg);
899 const struct got_error *
900 got_fileindex_diff_tree(struct got_fileindex *fileindex,
901 struct got_tree_object *tree, const char *path, const char *entry_name,
902 struct got_repository *repo,
903 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
905 struct got_fileindex_entry *ie;
906 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
907 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
908 ie = walk_fileindex(fileindex, ie);
909 return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
913 static const struct got_error *
914 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
915 struct got_pathlist_head *, int, const char *, const char *,
916 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
918 static const struct got_error *
919 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
921 const struct got_error *err = NULL;
922 struct got_pathlist_entry *new = NULL;
923 struct dirent *dep = NULL;
924 struct dirent *de = NULL;
927 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
929 err = got_error_from_errno("malloc");
933 if (readdir_r(dir, de, &dep) != 0) {
934 err = got_error_from_errno("readdir_r");
943 if (strcmp(de->d_name, ".") == 0 ||
944 strcmp(de->d_name, "..") == 0 ||
946 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
951 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
957 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
967 free_dirlist(struct got_pathlist_head *dirlist)
969 struct got_pathlist_entry *dle;
971 TAILQ_FOREACH(dle, dirlist, entry)
973 got_pathlist_free(dirlist);
976 static const struct got_error *
977 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
978 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
979 const char *path, const char *rootpath, struct got_repository *repo,
980 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
982 const struct got_error *err = NULL;
983 struct dirent *de = dle->data;
990 if (de->d_type == DT_UNKNOWN) {
991 /* Occurs on NFS mounts without "readdir plus" RPC. */
993 if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
994 return got_error_from_errno("asprintf");
995 err = got_path_dirent_type(&type, dir_path, de);
1002 if (type == DT_DIR) {
1005 struct got_pathlist_head subdirlist;
1007 TAILQ_INIT(&subdirlist);
1009 if (asprintf(&subpath, "%s%s%s", path,
1010 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1011 return got_error_from_errno("asprintf");
1013 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
1015 return got_error_from_errno("asprintf");
1018 subdirfd = openat(fd, de->d_name,
1019 O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
1020 if (subdirfd == -1) {
1021 if (errno == EACCES) {
1022 *next = TAILQ_NEXT(dle, entry);
1025 err = got_error_from_errno2("openat", subdirpath);
1031 subdir = fdopendir(subdirfd);
1033 return got_error_from_errno2("fdopendir", path);
1035 err = read_dirlist(&subdirlist, subdir, subdirpath);
1042 err = diff_fileindex_dir(fileindex, ie, &subdirlist,
1043 dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
1044 if (subdir && closedir(subdir) == -1 && err == NULL)
1045 err = got_error_from_errno2("closedir", subdirpath);
1048 free_dirlist(&subdirlist);
1053 *next = TAILQ_NEXT(dle, entry);
1057 static const struct got_error *
1058 diff_fileindex_dir(struct got_fileindex *fileindex,
1059 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1060 int dirfd, const char *rootpath, const char *path,
1061 struct got_repository *repo,
1062 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1064 const struct got_error *err = NULL;
1065 struct dirent *de = NULL;
1066 size_t path_len = strlen(path);
1067 struct got_pathlist_entry *dle;
1069 if (cb->diff_traverse) {
1070 err = cb->diff_traverse(cb_arg, path, dirfd);
1075 dle = TAILQ_FIRST(dirlist);
1076 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1081 if (asprintf(&de_path, "%s/%s", path,
1082 de->d_name) == -1) {
1083 err = got_error_from_errno("asprintf");
1086 cmp = got_path_cmp((*ie)->path, de_path,
1087 got_fileindex_entry_path_len(*ie),
1088 strlen(path) + 1 + de->d_namlen);
1091 err = cb->diff_old_new(cb_arg, *ie, de, path,
1095 *ie = walk_fileindex(fileindex, *ie);
1096 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1097 path, rootpath, repo, cb, cb_arg);
1098 } else if (cmp < 0 ) {
1099 err = cb->diff_old(cb_arg, *ie, path);
1102 *ie = walk_fileindex(fileindex, *ie);
1104 err = cb->diff_new(cb_arg, de, path, dirfd);
1107 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1108 path, rootpath, repo, cb, cb_arg);
1113 err = cb->diff_old(cb_arg, *ie, path);
1116 *ie = walk_fileindex(fileindex, *ie);
1119 err = cb->diff_new(cb_arg, de, path, dirfd);
1122 err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1123 rootpath, repo, cb, cb_arg);
1132 const struct got_error *
1133 got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1134 const char *rootpath, const char *path, struct got_repository *repo,
1135 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1137 const struct got_error *err;
1138 struct got_fileindex_entry *ie;
1139 struct got_pathlist_head dirlist;
1143 TAILQ_INIT(&dirlist);
1146 * Duplicate the file descriptor so we can call closedir() below
1147 * without closing the file descriptor passed in by our caller.
1151 return got_error_from_errno2("dup", path);
1152 if (lseek(fd2, 0, SEEK_SET) == -1) {
1153 err = got_error_from_errno2("lseek", path);
1157 dir = fdopendir(fd2);
1159 err = got_error_from_errno2("fdopendir", path);
1163 err = read_dirlist(&dirlist, dir, path);
1169 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1170 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1171 ie = walk_fileindex(fileindex, ie);
1172 err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1173 rootpath, path, repo, cb, cb_arg);
1175 if (closedir(dir) == -1 && err == NULL)
1176 err = got_error_from_errno2("closedir", path);
1177 free_dirlist(&dirlist);
1181 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);