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
49 #define GOT_FILEIDX_F_SKIPPED 0x00200000
51 struct got_fileindex {
52 struct got_fileindex_tree entries;
53 int nentries; /* Does not include entries marked for removal. */
54 #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
58 got_fileindex_entry_perms_get(struct got_fileindex_entry *ie)
60 return ((ie->mode & GOT_FILEIDX_MODE_PERMS) >>
61 GOT_FILEIDX_MODE_PERMS_SHIFT);
65 fileindex_entry_perms_set(struct got_fileindex_entry *ie, mode_t mode)
67 ie->mode &= ~GOT_FILEIDX_MODE_PERMS;
68 ie->mode |= ((mode << GOT_FILEIDX_MODE_PERMS_SHIFT) &
69 GOT_FILEIDX_MODE_PERMS);
73 got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
75 mode_t perms = got_fileindex_entry_perms_get(ie);
76 int type = got_fileindex_entry_filetype_get(ie);
79 if (type == GOT_FILEIDX_MODE_REGULAR_FILE ||
80 type == GOT_FILEIDX_MODE_BAD_SYMLINK)
85 return (ftype | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
88 const struct got_error *
89 got_fileindex_entry_update(struct got_fileindex_entry *ie,
90 int wt_fd, const char *ondisk_path, uint8_t *blob_sha1,
91 uint8_t *commit_sha1, int update_timestamps)
95 if (fstatat(wt_fd, ondisk_path, &sb, AT_SYMLINK_NOFOLLOW) != 0) {
96 if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
98 return got_error_from_errno2("fstatat", ondisk_path);
99 sb.st_mode = GOT_DEFAULT_FILE_MODE;
101 if (sb.st_mode & S_IFDIR)
102 return got_error_set_errno(EISDIR, ondisk_path);
103 ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
107 if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
108 if (update_timestamps) {
109 ie->ctime_sec = sb.st_ctim.tv_sec;
110 ie->ctime_nsec = sb.st_ctim.tv_nsec;
111 ie->mtime_sec = sb.st_mtim.tv_sec;
112 ie->mtime_nsec = sb.st_mtim.tv_nsec;
116 ie->size = (sb.st_size & 0xffffffff);
117 if (S_ISLNK(sb.st_mode)) {
118 got_fileindex_entry_filetype_set(ie,
119 GOT_FILEIDX_MODE_SYMLINK);
120 fileindex_entry_perms_set(ie, 0);
122 got_fileindex_entry_filetype_set(ie,
123 GOT_FILEIDX_MODE_REGULAR_FILE);
124 fileindex_entry_perms_set(ie,
125 sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
130 memcpy(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
131 ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
133 ie->flags |= GOT_FILEIDX_F_NO_BLOB;
136 memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
137 ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
139 ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
145 got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
147 ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
151 got_fileindex_entry_mark_skipped(struct got_fileindex_entry *ie)
153 ie->flags |= GOT_FILEIDX_F_SKIPPED;
156 const struct got_error *
157 got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
162 *ie = calloc(1, sizeof(**ie));
164 return got_error_from_errno("calloc");
166 (*ie)->path = strdup(relpath);
167 if ((*ie)->path == NULL) {
168 const struct got_error *err = got_error_from_errno("strdup");
174 len = strlen(relpath);
175 if (len > GOT_FILEIDX_F_PATH_LEN)
176 len = GOT_FILEIDX_F_PATH_LEN;
183 got_fileindex_entry_free(struct got_fileindex_entry *ie)
190 got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
192 return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
196 got_fileindex_entry_stage_get(const struct got_fileindex_entry *ie)
198 return ((ie->flags & GOT_FILEIDX_F_STAGE) >> GOT_FILEIDX_F_STAGE_SHIFT);
202 got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage)
204 ie->flags &= ~GOT_FILEIDX_F_STAGE;
205 ie->flags |= ((stage << GOT_FILEIDX_F_STAGE_SHIFT) &
206 GOT_FILEIDX_F_STAGE);
210 got_fileindex_entry_filetype_get(struct got_fileindex_entry *ie)
212 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
216 got_fileindex_entry_filetype_set(struct got_fileindex_entry *ie, int type)
218 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_ONDISK;
219 ie->mode |= (type & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
223 got_fileindex_entry_staged_filetype_set(struct got_fileindex_entry *ie,
226 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_STAGED;
227 ie->mode |= ((type << GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT) &
228 GOT_FILEIDX_MODE_FILE_TYPE_STAGED);
232 got_fileindex_entry_staged_filetype_get(struct got_fileindex_entry *ie)
234 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_STAGED) >>
235 GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT;
239 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
241 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
245 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
247 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
251 got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
253 return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
257 got_fileindex_entry_was_skipped(struct got_fileindex_entry *ie)
259 return (ie->flags & GOT_FILEIDX_F_SKIPPED) != 0;
262 static const struct got_error *
263 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
265 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
266 return got_error(GOT_ERR_NO_SPACE);
268 if (RB_INSERT(got_fileindex_tree, &fileindex->entries, ie) != NULL)
269 return got_error_path(ie->path, GOT_ERR_FILEIDX_DUP_ENTRY);
271 fileindex->nentries++;
275 const struct got_error *
276 got_fileindex_entry_add(struct got_fileindex *fileindex,
277 struct got_fileindex_entry *ie)
279 /* Flag this entry until it gets written out to disk. */
280 ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
282 return add_entry(fileindex, ie);
286 got_fileindex_entry_remove(struct got_fileindex *fileindex,
287 struct got_fileindex_entry *ie)
290 * Removing an entry from the RB tree immediately breaks
291 * in-progress iterations over file index entries.
292 * So flag this entry for removal and remove it once the index
293 * is written out to disk. Meanwhile, pretend this entry no longer
294 * exists if we get queried for it again before then.
296 ie->flags |= GOT_FILEIDX_F_REMOVE_ON_FLUSH;
297 fileindex->nentries--;
300 struct got_fileindex_entry *
301 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
304 struct got_fileindex_entry *ie;
305 struct got_fileindex_entry key;
306 memset(&key, 0, sizeof(key));
307 key.path = (char *)path;
308 key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
309 ie = RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
310 if (ie && (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH))
315 const struct got_error *
316 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
317 got_fileindex_cb cb, void *cb_arg)
319 const struct got_error *err;
320 struct got_fileindex_entry *ie, *tmp;
322 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
323 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
325 err = (*cb)(cb_arg, ie);
332 struct got_fileindex *
333 got_fileindex_alloc(void)
335 struct got_fileindex *fileindex;
337 fileindex = calloc(1, sizeof(*fileindex));
338 if (fileindex == NULL)
341 RB_INIT(&fileindex->entries);
346 got_fileindex_free(struct got_fileindex *fileindex)
348 struct got_fileindex_entry *ie;
350 while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
351 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
352 got_fileindex_entry_free(ie);
357 static const struct got_error *
358 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
363 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
364 n = fwrite(&val, 1, sizeof(val), outfile);
365 if (n != sizeof(val))
366 return got_ferror(outfile, GOT_ERR_IO);
370 static const struct got_error *
371 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
376 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
377 n = fwrite(&val, 1, sizeof(val), outfile);
378 if (n != sizeof(val))
379 return got_ferror(outfile, GOT_ERR_IO);
383 static const struct got_error *
384 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
389 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
390 n = fwrite(&val, 1, sizeof(val), outfile);
391 if (n != sizeof(val))
392 return got_ferror(outfile, GOT_ERR_IO);
396 static const struct got_error *
397 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
399 size_t n, len, pad = 0;
400 static const uint8_t zero[8] = { 0 };
403 while ((len + pad) % 8 != 0)
406 pad = 8; /* NUL-terminate */
408 SHA1Update(ctx, path, len);
409 n = fwrite(path, 1, len, outfile);
411 return got_ferror(outfile, GOT_ERR_IO);
412 SHA1Update(ctx, zero, pad);
413 n = fwrite(zero, 1, pad, outfile);
415 return got_ferror(outfile, GOT_ERR_IO);
419 static const struct got_error *
420 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
423 const struct got_error *err;
427 err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
430 err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
433 err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
436 err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
440 err = write_fileindex_val32(ctx, ie->uid, outfile);
443 err = write_fileindex_val32(ctx, ie->gid, outfile);
446 err = write_fileindex_val32(ctx, ie->size, outfile);
450 err = write_fileindex_val16(ctx, ie->mode, outfile);
454 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
455 n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
456 if (n != SHA1_DIGEST_LENGTH)
457 return got_ferror(outfile, GOT_ERR_IO);
459 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
460 n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
461 if (n != SHA1_DIGEST_LENGTH)
462 return got_ferror(outfile, GOT_ERR_IO);
464 err = write_fileindex_val32(ctx, ie->flags, outfile);
468 err = write_fileindex_path(ctx, ie->path, outfile);
472 stage = got_fileindex_entry_stage_get(ie);
473 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
474 stage == GOT_FILEIDX_STAGE_ADD) {
475 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
476 n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
478 if (n != SHA1_DIGEST_LENGTH)
479 return got_ferror(outfile, GOT_ERR_IO);
485 const struct got_error *
486 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
488 const struct got_error *err = NULL;
489 struct got_fileindex_hdr hdr;
491 uint8_t sha1[SHA1_DIGEST_LENGTH];
493 struct got_fileindex_entry *ie, *tmp;
497 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
498 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
499 hdr.nentries = htobe32(fileindex->nentries);
501 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
502 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
503 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
504 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
505 if (n != sizeof(hdr.signature))
506 return got_ferror(outfile, GOT_ERR_IO);
507 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
508 if (n != sizeof(hdr.version))
509 return got_ferror(outfile, GOT_ERR_IO);
510 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
511 if (n != sizeof(hdr.nentries))
512 return got_ferror(outfile, GOT_ERR_IO);
514 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
515 ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
516 ie->flags &= ~GOT_FILEIDX_F_SKIPPED;
517 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH) {
518 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
519 got_fileindex_entry_free(ie);
522 err = write_fileindex_entry(&ctx, ie, outfile);
527 SHA1Final(sha1, &ctx);
528 n = fwrite(sha1, 1, sizeof(sha1), outfile);
529 if (n != sizeof(sha1))
530 return got_ferror(outfile, GOT_ERR_IO);
532 if (fflush(outfile) != 0)
533 return got_error_from_errno("fflush");
538 static const struct got_error *
539 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
543 n = fread(val, 1, sizeof(*val), infile);
544 if (n != sizeof(*val))
545 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
546 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
547 *val = be64toh(*val);
551 static const struct got_error *
552 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
556 n = fread(val, 1, sizeof(*val), infile);
557 if (n != sizeof(*val))
558 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
559 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
560 *val = be32toh(*val);
564 static const struct got_error *
565 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
569 n = fread(val, 1, sizeof(*val), infile);
570 if (n != sizeof(*val))
571 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
572 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
573 *val = be16toh(*val);
577 static const struct got_error *
578 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
580 const struct got_error *err = NULL;
581 const size_t chunk_size = 8;
582 size_t n, len = 0, totlen = chunk_size;
584 *path = malloc(totlen);
586 return got_error_from_errno("malloc");
589 if (len + chunk_size > totlen) {
590 char *p = reallocarray(*path, totlen + chunk_size, 1);
592 err = got_error_from_errno("reallocarray");
595 totlen += chunk_size;
598 n = fread(*path + len, 1, chunk_size, infile);
599 if (n != chunk_size) {
600 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
603 SHA1Update(ctx, *path + len, chunk_size);
605 } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
614 static const struct got_error *
615 read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
616 FILE *infile, uint32_t version)
618 const struct got_error *err;
619 struct got_fileindex_entry *ie;
624 ie = calloc(1, sizeof(*ie));
626 return got_error_from_errno("calloc");
628 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
631 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
634 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
637 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
641 err = read_fileindex_val32(&ie->uid, ctx, infile);
644 err = read_fileindex_val32(&ie->gid, ctx, infile);
647 err = read_fileindex_val32(&ie->size, ctx, infile);
651 err = read_fileindex_val16(&ie->mode, ctx, infile);
655 n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
656 if (n != SHA1_DIGEST_LENGTH) {
657 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
660 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
662 n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
663 if (n != SHA1_DIGEST_LENGTH) {
664 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
667 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
669 err = read_fileindex_val32(&ie->flags, ctx, infile);
673 err = read_fileindex_path(&ie->path, ctx, infile);
678 uint32_t stage = got_fileindex_entry_stage_get(ie);
679 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
680 stage == GOT_FILEIDX_STAGE_ADD) {
681 n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
683 if (n != SHA1_DIGEST_LENGTH) {
684 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
687 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
690 /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
691 ie->flags &= ~GOT_FILEIDX_F_STAGE;
696 got_fileindex_entry_free(ie);
702 const struct got_error *
703 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
705 const struct got_error *err = NULL;
706 struct got_fileindex_hdr hdr;
708 struct got_fileindex_entry *ie;
709 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
710 uint8_t sha1[SHA1_DIGEST_LENGTH];
716 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
717 if (n != sizeof(hdr.signature)) {
718 if (n == 0) /* EOF */
720 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
722 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
723 if (n != sizeof(hdr.version)) {
724 if (n == 0) /* EOF */
726 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
728 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
729 if (n != sizeof(hdr.nentries)) {
730 if (n == 0) /* EOF */
732 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
735 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
736 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
737 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
739 hdr.signature = be32toh(hdr.signature);
740 hdr.version = be32toh(hdr.version);
741 hdr.nentries = be32toh(hdr.nentries);
743 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
744 return got_error(GOT_ERR_FILEIDX_SIG);
745 if (hdr.version > GOT_FILE_INDEX_VERSION)
746 return got_error(GOT_ERR_FILEIDX_VER);
748 for (i = 0; i < hdr.nentries; i++) {
749 err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
752 err = add_entry(fileindex, ie);
757 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
758 if (n != sizeof(sha1_expected))
759 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
760 SHA1Final(sha1, &ctx);
761 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
762 return got_error(GOT_ERR_FILEIDX_CSUM);
767 static struct got_fileindex_entry *
768 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
770 struct got_fileindex_entry *next;
772 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
774 /* Skip entries which were added or removed by diff callbacks. */
775 while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
776 GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
777 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
782 static const struct got_error *
783 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
784 struct got_tree_object *tree, const char *, const char *,
785 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
787 static const struct got_error *
788 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
789 struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
790 const char *path, const char *entry_name, struct got_repository *repo,
791 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
793 const struct got_error *err = NULL;
794 struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
796 if (!got_object_tree_entry_is_submodule(te) &&
797 S_ISDIR(got_tree_entry_get_mode(te))) {
799 struct got_tree_object *subtree;
801 if (asprintf(&subpath, "%s%s%s", path,
802 path[0] == '\0' ? "" : "/",
803 got_tree_entry_get_name(te)) == -1)
804 return got_error_from_errno("asprintf");
806 err = got_object_open_as_tree(&subtree, repo,
807 got_tree_entry_get_id(te));
813 err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
814 entry_name, repo, cb, cb_arg);
816 got_object_tree_close(subtree);
822 *next = got_object_tree_get_entry(tree, *tidx);
826 static const struct got_error *
827 diff_fileindex_tree(struct got_fileindex *fileindex,
828 struct got_fileindex_entry **ie, struct got_tree_object *tree,
829 const char *path, const char *entry_name, struct got_repository *repo,
830 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
832 const struct got_error *err = NULL;
833 struct got_tree_entry *te = NULL;
834 size_t path_len = strlen(path);
835 struct got_fileindex_entry *next;
838 te = got_object_tree_get_entry(tree, tidx);
839 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
842 const char *te_name = got_tree_entry_get_name(te);
844 if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
845 err = got_error_from_errno("asprintf");
848 cmp = got_path_cmp((*ie)->path, te_path,
849 got_fileindex_entry_path_len(*ie), strlen(te_path));
852 if (got_path_is_child((*ie)->path, path,
854 !got_object_tree_entry_is_submodule(te) &&
855 (entry_name == NULL ||
856 strcmp(te_name, entry_name) == 0)) {
857 err = cb->diff_old_new(cb_arg, *ie, te,
859 if (err || entry_name)
862 *ie = walk_fileindex(fileindex, *ie);
863 err = walk_tree(&te, fileindex, ie, tree, &tidx,
864 path, entry_name, repo, cb, cb_arg);
865 } else if (cmp < 0) {
866 next = walk_fileindex(fileindex, *ie);
867 if (got_path_is_child((*ie)->path, path,
868 path_len) && entry_name == NULL) {
869 err = cb->diff_old(cb_arg, *ie, path);
870 if (err || entry_name)
875 if ((entry_name == NULL ||
876 strcmp(te_name, entry_name) == 0)) {
877 err = cb->diff_new(cb_arg, te, path);
878 if (err || entry_name)
881 err = walk_tree(&te, fileindex, ie, tree, &tidx,
882 path, entry_name, repo, cb, cb_arg);
887 next = walk_fileindex(fileindex, *ie);
888 if (got_path_is_child((*ie)->path, path, path_len) &&
889 (entry_name == NULL ||
890 (te && strcmp(got_tree_entry_get_name(te),
891 entry_name) == 0))) {
892 err = cb->diff_old(cb_arg, *ie, path);
893 if (err || entry_name)
898 if (!got_object_tree_entry_is_submodule(te) &&
899 (entry_name == NULL ||
900 strcmp(got_tree_entry_get_name(te), entry_name)
902 err = cb->diff_new(cb_arg, te, path);
903 if (err || entry_name)
906 err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
907 entry_name, repo, cb, cb_arg);
916 const struct got_error *
917 got_fileindex_diff_tree(struct got_fileindex *fileindex,
918 struct got_tree_object *tree, const char *path, const char *entry_name,
919 struct got_repository *repo,
920 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
922 struct got_fileindex_entry *ie;
923 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
924 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
925 ie = walk_fileindex(fileindex, ie);
926 return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
930 static const struct got_error *
931 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
932 struct got_pathlist_head *, int, const char *, const char *,
933 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
935 static const struct got_error *
936 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
938 const struct got_error *err = NULL;
939 struct got_pathlist_entry *new = NULL;
940 struct dirent *dep = NULL;
941 struct dirent *de = NULL;
944 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
946 err = got_error_from_errno("malloc");
950 if (readdir_r(dir, de, &dep) != 0) {
951 err = got_error_from_errno("readdir_r");
960 if (strcmp(de->d_name, ".") == 0 ||
961 strcmp(de->d_name, "..") == 0 ||
963 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
968 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
974 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
984 free_dirlist(struct got_pathlist_head *dirlist)
986 struct got_pathlist_entry *dle;
988 TAILQ_FOREACH(dle, dirlist, entry)
990 got_pathlist_free(dirlist);
994 have_tracked_file_in_dir(struct got_fileindex *fileindex, const char *path)
996 struct got_fileindex_entry *ie;
997 size_t path_len = strlen(path);
1000 ie = RB_ROOT(&fileindex->entries);
1002 if (got_path_is_child(ie->path, path, path_len))
1004 cmp = got_path_cmp(path, ie->path, path_len,
1005 got_fileindex_entry_path_len(ie));
1007 ie = RB_LEFT(ie, entry);
1009 ie = RB_RIGHT(ie, entry);
1017 static const struct got_error *
1018 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
1019 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
1020 const char *path, const char *rootpath, struct got_repository *repo,
1021 int ignore, struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1023 const struct got_error *err = NULL;
1024 struct dirent *de = dle->data;
1030 /* Must traverse ignored directories if they contain tracked files. */
1031 if (de->d_type == DT_DIR && ignore &&
1032 have_tracked_file_in_dir(fileindex, path))
1035 if (de->d_type == DT_DIR && !ignore) {
1038 struct got_pathlist_head subdirlist;
1040 TAILQ_INIT(&subdirlist);
1042 if (asprintf(&subpath, "%s%s%s", path,
1043 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1044 return got_error_from_errno("asprintf");
1046 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
1048 return got_error_from_errno("asprintf");
1051 subdirfd = openat(fd, de->d_name,
1052 O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
1053 if (subdirfd == -1) {
1054 if (errno == EACCES) {
1055 *next = TAILQ_NEXT(dle, entry);
1058 err = got_error_from_errno2("openat", subdirpath);
1064 subdir = fdopendir(subdirfd);
1066 return got_error_from_errno2("fdopendir", path);
1068 err = read_dirlist(&subdirlist, subdir, subdirpath);
1075 err = diff_fileindex_dir(fileindex, ie, &subdirlist,
1076 dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
1077 if (subdir && closedir(subdir) == -1 && err == NULL)
1078 err = got_error_from_errno2("closedir", subdirpath);
1081 free_dirlist(&subdirlist);
1086 *next = TAILQ_NEXT(dle, entry);
1090 static const struct got_error *
1091 dirent_type_fixup(struct dirent *de, const char *rootpath, const char *path)
1093 const struct got_error *err;
1097 if (de->d_type != DT_UNKNOWN)
1100 /* DT_UNKNOWN occurs on NFS mounts without "readdir plus" RPC. */
1101 if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
1102 return got_error_from_errno("asprintf");
1103 err = got_path_dirent_type(&type, dir_path, de);
1112 static const struct got_error *
1113 diff_fileindex_dir(struct got_fileindex *fileindex,
1114 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1115 int dirfd, const char *rootpath, const char *path,
1116 struct got_repository *repo,
1117 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1119 const struct got_error *err = NULL;
1120 struct dirent *de = NULL;
1121 size_t path_len = strlen(path);
1122 struct got_pathlist_entry *dle;
1125 if (cb->diff_traverse) {
1126 err = cb->diff_traverse(cb_arg, path, dirfd);
1131 dle = TAILQ_FIRST(dirlist);
1132 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1137 err = dirent_type_fixup(de, rootpath, path);
1140 if (asprintf(&de_path, "%s/%s", path,
1141 de->d_name) == -1) {
1142 err = got_error_from_errno("asprintf");
1145 cmp = got_path_cmp((*ie)->path, de_path,
1146 got_fileindex_entry_path_len(*ie),
1147 strlen(path) + 1 + de->d_namlen);
1150 err = cb->diff_old_new(cb_arg, *ie, de, path,
1154 *ie = walk_fileindex(fileindex, *ie);
1155 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1156 path, rootpath, repo, 0, cb, cb_arg);
1157 } else if (cmp < 0 ) {
1158 err = cb->diff_old(cb_arg, *ie, path);
1161 *ie = walk_fileindex(fileindex, *ie);
1163 err = cb->diff_new(&ignore, cb_arg, de, path,
1167 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1168 path, rootpath, repo, ignore, cb, cb_arg);
1173 err = cb->diff_old(cb_arg, *ie, path);
1176 *ie = walk_fileindex(fileindex, *ie);
1179 err = dirent_type_fixup(de, rootpath, path);
1182 err = cb->diff_new(&ignore, cb_arg, de, path, dirfd);
1185 err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1186 rootpath, repo, ignore, cb, cb_arg);
1195 const struct got_error *
1196 got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1197 const char *rootpath, const char *path, struct got_repository *repo,
1198 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1200 const struct got_error *err;
1201 struct got_fileindex_entry *ie;
1202 struct got_pathlist_head dirlist;
1206 TAILQ_INIT(&dirlist);
1209 * Duplicate the file descriptor so we can call closedir() below
1210 * without closing the file descriptor passed in by our caller.
1214 return got_error_from_errno2("dup", path);
1215 if (lseek(fd2, 0, SEEK_SET) == -1) {
1216 err = got_error_from_errno2("lseek", path);
1220 dir = fdopendir(fd2);
1222 err = got_error_from_errno2("fdopendir", path);
1226 err = read_dirlist(&dirlist, dir, path);
1232 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1233 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1234 ie = walk_fileindex(fileindex, ie);
1235 err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1236 rootpath, path, repo, cb, cb_arg);
1238 if (closedir(dir) == -1 && err == NULL)
1239 err = got_error_from_errno2("closedir", path);
1240 free_dirlist(&dirlist);
1244 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);