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>
34 #include "got_error.h"
35 #include "got_object.h"
38 #include "got_lib_fileindex.h"
39 #include "got_lib_worktree.h"
41 /* got_fileindex_entry flags */
42 #define GOT_FILEIDX_F_PATH_LEN 0x00000fff
43 #define GOT_FILEIDX_F_STAGE 0x0000f000
44 #define GOT_FILEIDX_F_STAGE_SHIFT 12
45 #define GOT_FILEIDX_F_NOT_FLUSHED 0x00010000
46 #define GOT_FILEIDX_F_NO_BLOB 0x00020000
47 #define GOT_FILEIDX_F_NO_COMMIT 0x00040000
48 #define GOT_FILEIDX_F_NO_FILE_ON_DISK 0x00080000
49 #define GOT_FILEIDX_F_REMOVE_ON_FLUSH 0x00100000
50 #define GOT_FILEIDX_F_SKIPPED 0x00200000
52 struct got_fileindex {
53 struct got_fileindex_tree entries;
54 int nentries; /* Does not include entries marked for removal. */
55 #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
59 got_fileindex_entry_perms_get(struct got_fileindex_entry *ie)
61 return ((ie->mode & GOT_FILEIDX_MODE_PERMS) >>
62 GOT_FILEIDX_MODE_PERMS_SHIFT);
66 fileindex_entry_perms_set(struct got_fileindex_entry *ie, mode_t mode)
68 ie->mode &= ~GOT_FILEIDX_MODE_PERMS;
69 ie->mode |= ((mode << GOT_FILEIDX_MODE_PERMS_SHIFT) &
70 GOT_FILEIDX_MODE_PERMS);
74 got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
76 mode_t perms = got_fileindex_entry_perms_get(ie);
77 int type = got_fileindex_entry_filetype_get(ie);
80 if (type == GOT_FILEIDX_MODE_REGULAR_FILE ||
81 type == GOT_FILEIDX_MODE_BAD_SYMLINK)
86 return (ftype | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
89 const struct got_error *
90 got_fileindex_entry_update(struct got_fileindex_entry *ie,
91 int wt_fd, const char *ondisk_path, uint8_t *blob_hash,
92 uint8_t *commit_hash, int update_timestamps)
96 if (fstatat(wt_fd, ondisk_path, &sb, AT_SYMLINK_NOFOLLOW) != 0) {
97 if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
99 return got_error_from_errno2("fstatat", ondisk_path);
100 sb.st_mode = GOT_DEFAULT_FILE_MODE;
102 if (sb.st_mode & S_IFDIR)
103 return got_error_set_errno(EISDIR, ondisk_path);
104 ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
108 if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
109 if (update_timestamps) {
110 ie->ctime_sec = sb.st_ctim.tv_sec;
111 ie->ctime_nsec = sb.st_ctim.tv_nsec;
112 ie->mtime_sec = sb.st_mtim.tv_sec;
113 ie->mtime_nsec = sb.st_mtim.tv_nsec;
117 ie->size = (sb.st_size & 0xffffffff);
118 if (S_ISLNK(sb.st_mode)) {
119 got_fileindex_entry_filetype_set(ie,
120 GOT_FILEIDX_MODE_SYMLINK);
121 fileindex_entry_perms_set(ie, 0);
123 got_fileindex_entry_filetype_set(ie,
124 GOT_FILEIDX_MODE_REGULAR_FILE);
125 fileindex_entry_perms_set(ie,
126 sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
131 memcpy(ie->blob_hash, blob_hash, SHA1_DIGEST_LENGTH);
132 ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
134 ie->flags |= GOT_FILEIDX_F_NO_BLOB;
137 memcpy(ie->commit_hash, commit_hash, SHA1_DIGEST_LENGTH);
138 ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
140 ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
146 got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
148 ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
152 got_fileindex_entry_mark_skipped(struct got_fileindex_entry *ie)
154 ie->flags |= GOT_FILEIDX_F_SKIPPED;
157 const struct got_error *
158 got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
163 *ie = calloc(1, sizeof(**ie));
165 return got_error_from_errno("calloc");
167 (*ie)->path = strdup(relpath);
168 if ((*ie)->path == NULL) {
169 const struct got_error *err = got_error_from_errno("strdup");
175 len = strlen(relpath);
176 if (len > GOT_FILEIDX_F_PATH_LEN)
177 len = GOT_FILEIDX_F_PATH_LEN;
184 got_fileindex_entry_free(struct got_fileindex_entry *ie)
191 got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
193 return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
197 got_fileindex_entry_stage_get(const struct got_fileindex_entry *ie)
199 return ((ie->flags & GOT_FILEIDX_F_STAGE) >> GOT_FILEIDX_F_STAGE_SHIFT);
203 got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage)
205 ie->flags &= ~GOT_FILEIDX_F_STAGE;
206 ie->flags |= ((stage << GOT_FILEIDX_F_STAGE_SHIFT) &
207 GOT_FILEIDX_F_STAGE);
211 got_fileindex_entry_filetype_get(struct got_fileindex_entry *ie)
213 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
217 got_fileindex_entry_filetype_set(struct got_fileindex_entry *ie, int type)
219 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_ONDISK;
220 ie->mode |= (type & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
224 got_fileindex_entry_staged_filetype_set(struct got_fileindex_entry *ie,
227 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_STAGED;
228 ie->mode |= ((type << GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT) &
229 GOT_FILEIDX_MODE_FILE_TYPE_STAGED);
233 got_fileindex_entry_staged_filetype_get(struct got_fileindex_entry *ie)
235 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_STAGED) >>
236 GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT;
240 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
242 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
246 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
248 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
252 got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
254 return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
258 got_fileindex_entry_was_skipped(struct got_fileindex_entry *ie)
260 return (ie->flags & GOT_FILEIDX_F_SKIPPED) != 0;
263 static const struct got_error *
264 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
266 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
267 return got_error(GOT_ERR_NO_SPACE);
269 if (RB_INSERT(got_fileindex_tree, &fileindex->entries, ie) != NULL)
270 return got_error_path(ie->path, GOT_ERR_FILEIDX_DUP_ENTRY);
272 fileindex->nentries++;
276 const struct got_error *
277 got_fileindex_entry_add(struct got_fileindex *fileindex,
278 struct got_fileindex_entry *ie)
280 /* Flag this entry until it gets written out to disk. */
281 ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
283 return add_entry(fileindex, ie);
287 got_fileindex_entry_remove(struct got_fileindex *fileindex,
288 struct got_fileindex_entry *ie)
291 * Removing an entry from the RB tree immediately breaks
292 * in-progress iterations over file index entries.
293 * So flag this entry for removal and remove it once the index
294 * is written out to disk. Meanwhile, pretend this entry no longer
295 * exists if we get queried for it again before then.
297 ie->flags |= GOT_FILEIDX_F_REMOVE_ON_FLUSH;
298 fileindex->nentries--;
301 struct got_fileindex_entry *
302 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
305 struct got_fileindex_entry *ie;
306 struct got_fileindex_entry key;
307 memset(&key, 0, sizeof(key));
308 key.path = (char *)path;
309 key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
310 ie = RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
311 if (ie && (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH))
316 const struct got_error *
317 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
318 got_fileindex_cb cb, void *cb_arg)
320 const struct got_error *err;
321 struct got_fileindex_entry *ie, *tmp;
323 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
324 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
326 err = (*cb)(cb_arg, ie);
333 struct got_fileindex *
334 got_fileindex_alloc(void)
336 struct got_fileindex *fileindex;
338 fileindex = calloc(1, sizeof(*fileindex));
339 if (fileindex == NULL)
342 RB_INIT(&fileindex->entries);
347 got_fileindex_free(struct got_fileindex *fileindex)
349 struct got_fileindex_entry *ie;
351 while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
352 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
353 got_fileindex_entry_free(ie);
358 static const struct got_error *
359 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
364 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
365 n = fwrite(&val, 1, sizeof(val), outfile);
366 if (n != sizeof(val))
367 return got_ferror(outfile, GOT_ERR_IO);
371 static const struct got_error *
372 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
377 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
378 n = fwrite(&val, 1, sizeof(val), outfile);
379 if (n != sizeof(val))
380 return got_ferror(outfile, GOT_ERR_IO);
384 static const struct got_error *
385 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
390 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
391 n = fwrite(&val, 1, sizeof(val), outfile);
392 if (n != sizeof(val))
393 return got_ferror(outfile, GOT_ERR_IO);
397 static const struct got_error *
398 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
400 size_t n, len, pad = 0;
401 static const uint8_t zero[8] = { 0 };
404 while ((len + pad) % 8 != 0)
407 pad = 8; /* NUL-terminate */
409 SHA1Update(ctx, path, len);
410 n = fwrite(path, 1, len, outfile);
412 return got_ferror(outfile, GOT_ERR_IO);
413 SHA1Update(ctx, zero, pad);
414 n = fwrite(zero, 1, pad, outfile);
416 return got_ferror(outfile, GOT_ERR_IO);
420 static const struct got_error *
421 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
424 const struct got_error *err;
428 err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
431 err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
434 err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
437 err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
441 err = write_fileindex_val32(ctx, ie->uid, outfile);
444 err = write_fileindex_val32(ctx, ie->gid, outfile);
447 err = write_fileindex_val32(ctx, ie->size, outfile);
451 err = write_fileindex_val16(ctx, ie->mode, outfile);
455 SHA1Update(ctx, ie->blob_hash, SHA1_DIGEST_LENGTH);
456 n = fwrite(ie->blob_hash, 1, SHA1_DIGEST_LENGTH, outfile);
457 if (n != SHA1_DIGEST_LENGTH)
458 return got_ferror(outfile, GOT_ERR_IO);
460 SHA1Update(ctx, ie->commit_hash, SHA1_DIGEST_LENGTH);
461 n = fwrite(ie->commit_hash, 1, SHA1_DIGEST_LENGTH, outfile);
462 if (n != SHA1_DIGEST_LENGTH)
463 return got_ferror(outfile, GOT_ERR_IO);
465 err = write_fileindex_val32(ctx, ie->flags, outfile);
469 err = write_fileindex_path(ctx, ie->path, outfile);
473 stage = got_fileindex_entry_stage_get(ie);
474 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
475 stage == GOT_FILEIDX_STAGE_ADD) {
476 SHA1Update(ctx, ie->staged_blob_hash, SHA1_DIGEST_LENGTH);
477 n = fwrite(ie->staged_blob_hash, 1, SHA1_DIGEST_LENGTH,
479 if (n != SHA1_DIGEST_LENGTH)
480 return got_ferror(outfile, GOT_ERR_IO);
486 const struct got_error *
487 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
489 const struct got_error *err = NULL;
490 struct got_fileindex_hdr hdr;
492 uint8_t hash[SHA1_DIGEST_LENGTH];
494 struct got_fileindex_entry *ie, *tmp;
498 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
499 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
500 hdr.nentries = htobe32(fileindex->nentries);
502 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
503 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
504 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
505 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
506 if (n != sizeof(hdr.signature))
507 return got_ferror(outfile, GOT_ERR_IO);
508 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
509 if (n != sizeof(hdr.version))
510 return got_ferror(outfile, GOT_ERR_IO);
511 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
512 if (n != sizeof(hdr.nentries))
513 return got_ferror(outfile, GOT_ERR_IO);
515 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
516 ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
517 ie->flags &= ~GOT_FILEIDX_F_SKIPPED;
518 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH) {
519 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
520 got_fileindex_entry_free(ie);
523 err = write_fileindex_entry(&ctx, ie, outfile);
528 SHA1Final(hash, &ctx);
529 n = fwrite(hash, 1, sizeof(hash), outfile);
530 if (n != sizeof(hash))
531 return got_ferror(outfile, GOT_ERR_IO);
533 if (fflush(outfile) != 0)
534 return got_error_from_errno("fflush");
539 static const struct got_error *
540 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
544 n = fread(val, 1, sizeof(*val), infile);
545 if (n != sizeof(*val))
546 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
547 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
548 *val = be64toh(*val);
552 static const struct got_error *
553 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
557 n = fread(val, 1, sizeof(*val), infile);
558 if (n != sizeof(*val))
559 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
560 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
561 *val = be32toh(*val);
565 static const struct got_error *
566 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
570 n = fread(val, 1, sizeof(*val), infile);
571 if (n != sizeof(*val))
572 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
573 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
574 *val = be16toh(*val);
578 static const struct got_error *
579 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
581 const struct got_error *err = NULL;
582 const size_t chunk_size = 8;
583 size_t n, len = 0, totlen = chunk_size;
585 *path = malloc(totlen);
587 return got_error_from_errno("malloc");
590 if (len + chunk_size > totlen) {
591 char *p = reallocarray(*path, totlen + chunk_size, 1);
593 err = got_error_from_errno("reallocarray");
596 totlen += chunk_size;
599 n = fread(*path + len, 1, chunk_size, infile);
600 if (n != chunk_size) {
601 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
604 SHA1Update(ctx, *path + len, chunk_size);
606 } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
615 static const struct got_error *
616 read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
617 FILE *infile, uint32_t version)
619 const struct got_error *err;
620 struct got_fileindex_entry *ie;
625 ie = calloc(1, sizeof(*ie));
627 return got_error_from_errno("calloc");
629 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
632 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
635 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
638 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
642 err = read_fileindex_val32(&ie->uid, ctx, infile);
645 err = read_fileindex_val32(&ie->gid, ctx, infile);
648 err = read_fileindex_val32(&ie->size, ctx, infile);
652 err = read_fileindex_val16(&ie->mode, ctx, infile);
656 n = fread(ie->blob_hash, 1, SHA1_DIGEST_LENGTH, infile);
657 if (n != SHA1_DIGEST_LENGTH) {
658 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
661 SHA1Update(ctx, ie->blob_hash, SHA1_DIGEST_LENGTH);
663 n = fread(ie->commit_hash, 1, SHA1_DIGEST_LENGTH, infile);
664 if (n != SHA1_DIGEST_LENGTH) {
665 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
668 SHA1Update(ctx, ie->commit_hash, SHA1_DIGEST_LENGTH);
670 err = read_fileindex_val32(&ie->flags, ctx, infile);
674 err = read_fileindex_path(&ie->path, ctx, infile);
679 uint32_t stage = got_fileindex_entry_stage_get(ie);
680 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
681 stage == GOT_FILEIDX_STAGE_ADD) {
682 n = fread(ie->staged_blob_hash, 1, SHA1_DIGEST_LENGTH,
684 if (n != SHA1_DIGEST_LENGTH) {
685 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
688 SHA1Update(ctx, ie->staged_blob_hash, SHA1_DIGEST_LENGTH);
691 /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
692 ie->flags &= ~GOT_FILEIDX_F_STAGE;
697 got_fileindex_entry_free(ie);
703 const struct got_error *
704 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
706 const struct got_error *err = NULL;
707 struct got_fileindex_hdr hdr;
709 struct got_fileindex_entry *ie;
710 uint8_t hash_expected[SHA1_DIGEST_LENGTH];
711 uint8_t hash[SHA1_DIGEST_LENGTH];
717 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
718 if (n != sizeof(hdr.signature)) {
719 if (n == 0) /* EOF */
721 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
723 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
724 if (n != sizeof(hdr.version)) {
725 if (n == 0) /* EOF */
727 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
729 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
730 if (n != sizeof(hdr.nentries)) {
731 if (n == 0) /* EOF */
733 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
736 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
737 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
738 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
740 hdr.signature = be32toh(hdr.signature);
741 hdr.version = be32toh(hdr.version);
742 hdr.nentries = be32toh(hdr.nentries);
744 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
745 return got_error(GOT_ERR_FILEIDX_SIG);
746 if (hdr.version > GOT_FILE_INDEX_VERSION)
747 return got_error(GOT_ERR_FILEIDX_VER);
749 for (i = 0; i < hdr.nentries; i++) {
750 err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
753 err = add_entry(fileindex, ie);
758 n = fread(hash_expected, 1, sizeof(hash_expected), infile);
759 if (n != sizeof(hash_expected))
760 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
761 SHA1Final(hash, &ctx);
762 if (memcmp(hash, hash_expected, SHA1_DIGEST_LENGTH) != 0)
763 return got_error(GOT_ERR_FILEIDX_CSUM);
768 static struct got_fileindex_entry *
769 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
771 struct got_fileindex_entry *next;
773 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
775 /* Skip entries which were added or removed by diff callbacks. */
776 while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
777 GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
778 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
783 static const struct got_error *
784 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
785 struct got_tree_object *tree, const char *, const char *,
786 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
788 static const struct got_error *
789 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
790 struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
791 const char *path, const char *entry_name, struct got_repository *repo,
792 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
794 const struct got_error *err = NULL;
795 struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
797 if (!got_object_tree_entry_is_submodule(te) &&
798 S_ISDIR(got_tree_entry_get_mode(te))) {
800 struct got_tree_object *subtree;
802 if (asprintf(&subpath, "%s%s%s", path,
803 path[0] == '\0' ? "" : "/",
804 got_tree_entry_get_name(te)) == -1)
805 return got_error_from_errno("asprintf");
807 err = got_object_open_as_tree(&subtree, repo,
808 got_tree_entry_get_id(te));
814 err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
815 entry_name, repo, cb, cb_arg);
817 got_object_tree_close(subtree);
823 *next = got_object_tree_get_entry(tree, *tidx);
827 static const struct got_error *
828 diff_fileindex_tree(struct got_fileindex *fileindex,
829 struct got_fileindex_entry **ie, struct got_tree_object *tree,
830 const char *path, const char *entry_name, struct got_repository *repo,
831 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
833 const struct got_error *err = NULL;
834 struct got_tree_entry *te = NULL;
835 size_t path_len = strlen(path);
836 struct got_fileindex_entry *next;
839 te = got_object_tree_get_entry(tree, tidx);
840 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
843 const char *te_name = got_tree_entry_get_name(te);
845 if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
846 err = got_error_from_errno("asprintf");
849 cmp = got_path_cmp((*ie)->path, te_path,
850 got_fileindex_entry_path_len(*ie), strlen(te_path));
853 if (got_path_is_child((*ie)->path, path,
855 !got_object_tree_entry_is_submodule(te) &&
856 (entry_name == NULL ||
857 strcmp(te_name, entry_name) == 0)) {
858 err = cb->diff_old_new(cb_arg, *ie, te,
860 if (err || entry_name)
863 *ie = walk_fileindex(fileindex, *ie);
864 err = walk_tree(&te, fileindex, ie, tree, &tidx,
865 path, entry_name, repo, cb, cb_arg);
866 } else if (cmp < 0) {
867 next = walk_fileindex(fileindex, *ie);
868 if (got_path_is_child((*ie)->path, path,
869 path_len) && entry_name == NULL) {
870 err = cb->diff_old(cb_arg, *ie, path);
871 if (err || entry_name)
876 if ((entry_name == NULL ||
877 strcmp(te_name, entry_name) == 0)) {
878 err = cb->diff_new(cb_arg, te, path);
879 if (err || entry_name)
882 err = walk_tree(&te, fileindex, ie, tree, &tidx,
883 path, entry_name, repo, cb, cb_arg);
888 next = walk_fileindex(fileindex, *ie);
889 if (got_path_is_child((*ie)->path, path, path_len) &&
890 (entry_name == NULL ||
891 (te && strcmp(got_tree_entry_get_name(te),
892 entry_name) == 0))) {
893 err = cb->diff_old(cb_arg, *ie, path);
894 if (err || entry_name)
899 if (!got_object_tree_entry_is_submodule(te) &&
900 (entry_name == NULL ||
901 strcmp(got_tree_entry_get_name(te), entry_name)
903 err = cb->diff_new(cb_arg, te, path);
904 if (err || entry_name)
907 err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
908 entry_name, repo, cb, cb_arg);
917 const struct got_error *
918 got_fileindex_diff_tree(struct got_fileindex *fileindex,
919 struct got_tree_object *tree, const char *path, const char *entry_name,
920 struct got_repository *repo,
921 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
923 struct got_fileindex_entry *ie;
924 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
925 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
926 ie = walk_fileindex(fileindex, ie);
927 return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
931 static const struct got_error *
932 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
933 struct got_pathlist_head *, int, const char *, const char *,
934 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
936 static const struct got_error *
937 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
939 const struct got_error *err = NULL;
940 struct got_pathlist_entry *new = NULL;
941 struct dirent *dep = NULL;
942 struct dirent *de = NULL;
945 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
947 err = got_error_from_errno("malloc");
951 if (readdir_r(dir, de, &dep) != 0) {
952 err = got_error_from_errno("readdir_r");
961 if (strcmp(de->d_name, ".") == 0 ||
962 strcmp(de->d_name, "..") == 0 ||
964 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
969 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
975 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
985 have_tracked_file_in_dir(struct got_fileindex *fileindex, const char *path)
987 struct got_fileindex_entry *ie;
988 size_t path_len = strlen(path);
991 ie = RB_ROOT(&fileindex->entries);
993 if (got_path_is_child(ie->path, path, path_len))
995 cmp = got_path_cmp(path, ie->path, path_len,
996 got_fileindex_entry_path_len(ie));
998 ie = RB_LEFT(ie, entry);
1000 ie = RB_RIGHT(ie, entry);
1008 static const struct got_error *
1009 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
1010 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
1011 const char *path, const char *rootpath, struct got_repository *repo,
1012 int ignore, struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1014 const struct got_error *err = NULL;
1015 struct dirent *de = dle->data;
1021 /* Must traverse ignored directories if they contain tracked files. */
1022 if (de->d_type == DT_DIR && ignore &&
1023 have_tracked_file_in_dir(fileindex, path))
1026 if (de->d_type == DT_DIR && !ignore) {
1029 struct got_pathlist_head subdirlist;
1031 TAILQ_INIT(&subdirlist);
1033 if (asprintf(&subpath, "%s%s%s", path,
1034 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1035 return got_error_from_errno("asprintf");
1037 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
1039 return got_error_from_errno("asprintf");
1042 subdirfd = openat(fd, de->d_name,
1043 O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
1044 if (subdirfd == -1) {
1045 if (errno == EACCES) {
1046 *next = TAILQ_NEXT(dle, entry);
1049 err = got_error_from_errno2("openat", subdirpath);
1055 subdir = fdopendir(subdirfd);
1057 return got_error_from_errno2("fdopendir", path);
1059 err = read_dirlist(&subdirlist, subdir, subdirpath);
1066 err = diff_fileindex_dir(fileindex, ie, &subdirlist,
1067 dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
1068 if (subdir && closedir(subdir) == -1 && err == NULL)
1069 err = got_error_from_errno2("closedir", subdirpath);
1072 got_pathlist_free(&subdirlist, GOT_PATHLIST_FREE_DATA);
1077 *next = TAILQ_NEXT(dle, entry);
1081 static const struct got_error *
1082 dirent_type_fixup(struct dirent *de, const char *rootpath, const char *path)
1084 const struct got_error *err;
1088 if (de->d_type != DT_UNKNOWN)
1091 /* DT_UNKNOWN occurs on NFS mounts without "readdir plus" RPC. */
1092 if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
1093 return got_error_from_errno("asprintf");
1094 err = got_path_dirent_type(&type, dir_path, de);
1103 static const struct got_error *
1104 diff_fileindex_dir(struct got_fileindex *fileindex,
1105 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1106 int dirfd, const char *rootpath, const char *path,
1107 struct got_repository *repo,
1108 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1110 const struct got_error *err = NULL;
1111 struct dirent *de = NULL;
1112 size_t path_len = strlen(path);
1113 struct got_pathlist_entry *dle;
1116 if (cb->diff_traverse) {
1117 err = cb->diff_traverse(cb_arg, path, dirfd);
1122 dle = TAILQ_FIRST(dirlist);
1123 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1128 err = dirent_type_fixup(de, rootpath, path);
1131 if (asprintf(&de_path, "%s/%s", path,
1132 de->d_name) == -1) {
1133 err = got_error_from_errno("asprintf");
1136 cmp = got_path_cmp((*ie)->path, de_path,
1137 got_fileindex_entry_path_len(*ie),
1138 strlen(path) + 1 + de->d_namlen);
1141 err = cb->diff_old_new(cb_arg, *ie, de, path,
1145 *ie = walk_fileindex(fileindex, *ie);
1146 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1147 path, rootpath, repo, 0, cb, cb_arg);
1148 } else if (cmp < 0 ) {
1149 err = cb->diff_old(cb_arg, *ie, path);
1152 *ie = walk_fileindex(fileindex, *ie);
1154 err = cb->diff_new(&ignore, cb_arg, de, path,
1158 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1159 path, rootpath, repo, ignore, cb, cb_arg);
1164 err = cb->diff_old(cb_arg, *ie, path);
1167 *ie = walk_fileindex(fileindex, *ie);
1170 err = dirent_type_fixup(de, rootpath, path);
1173 err = cb->diff_new(&ignore, cb_arg, de, path, dirfd);
1176 err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1177 rootpath, repo, ignore, cb, cb_arg);
1186 const struct got_error *
1187 got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1188 const char *rootpath, const char *path, struct got_repository *repo,
1189 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1191 const struct got_error *err;
1192 struct got_fileindex_entry *ie;
1193 struct got_pathlist_head dirlist;
1197 TAILQ_INIT(&dirlist);
1200 * Duplicate the file descriptor so we can call closedir() below
1201 * without closing the file descriptor passed in by our caller.
1205 return got_error_from_errno2("dup", path);
1206 if (lseek(fd2, 0, SEEK_SET) == -1) {
1207 err = got_error_from_errno2("lseek", path);
1211 dir = fdopendir(fd2);
1213 err = got_error_from_errno2("fdopendir", path);
1217 err = read_dirlist(&dirlist, dir, path);
1223 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1224 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1225 ie = walk_fileindex(fileindex, ie);
1226 err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1227 rootpath, path, repo, cb, cb_arg);
1229 if (closedir(dir) == -1 && err == NULL)
1230 err = got_error_from_errno2("closedir", path);
1231 got_pathlist_free(&dirlist, GOT_PATHLIST_FREE_DATA);
1235 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);