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_ONDISK);
187 got_fileindex_entry_filetype_set(struct got_fileindex_entry *ie, int type)
189 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_ONDISK;
190 ie->mode |= (type & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
194 got_fileindex_entry_staged_filetype_set(struct got_fileindex_entry *ie, int type)
196 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_STAGED;
197 ie->mode |= ((type << GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT) &
198 GOT_FILEIDX_MODE_FILE_TYPE_STAGED);
202 got_fileindex_entry_staged_filetype_get(struct got_fileindex_entry *ie)
204 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_STAGED) >>
205 GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT;
209 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
211 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
215 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
217 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
221 got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
223 return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
226 static const struct got_error *
227 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
229 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
230 return got_error(GOT_ERR_NO_SPACE);
232 RB_INSERT(got_fileindex_tree, &fileindex->entries, ie);
233 fileindex->nentries++;
237 const struct got_error *
238 got_fileindex_entry_add(struct got_fileindex *fileindex,
239 struct got_fileindex_entry *ie)
241 /* Flag this entry until it gets written out to disk. */
242 ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
244 return add_entry(fileindex, ie);
248 got_fileindex_entry_remove(struct got_fileindex *fileindex,
249 struct got_fileindex_entry *ie)
252 * Removing an entry from the RB tree immediately breaks
253 * in-progress iterations over file index entries.
254 * So flag this entry for removal and remove it once the index
255 * is written out to disk. Meanwhile, pretend this entry no longer
256 * exists if we get queried for it again before then.
258 ie->flags |= GOT_FILEIDX_F_REMOVE_ON_FLUSH;
259 fileindex->nentries--;
262 struct got_fileindex_entry *
263 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
266 struct got_fileindex_entry *ie;
267 struct got_fileindex_entry key;
268 memset(&key, 0, sizeof(key));
269 key.path = (char *)path;
270 key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
271 ie = RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
272 if (ie && (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH))
277 const struct got_error *
278 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
279 got_fileindex_cb cb, void *cb_arg)
281 const struct got_error *err;
282 struct got_fileindex_entry *ie, *tmp;
284 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
285 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
287 err = (*cb)(cb_arg, ie);
294 struct got_fileindex *
295 got_fileindex_alloc(void)
297 struct got_fileindex *fileindex;
299 fileindex = calloc(1, sizeof(*fileindex));
300 if (fileindex == NULL)
303 RB_INIT(&fileindex->entries);
308 got_fileindex_free(struct got_fileindex *fileindex)
310 struct got_fileindex_entry *ie;
312 while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
313 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
314 got_fileindex_entry_free(ie);
319 static const struct got_error *
320 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
325 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
326 n = fwrite(&val, 1, sizeof(val), outfile);
327 if (n != sizeof(val))
328 return got_ferror(outfile, GOT_ERR_IO);
332 static const struct got_error *
333 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
338 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
339 n = fwrite(&val, 1, sizeof(val), outfile);
340 if (n != sizeof(val))
341 return got_ferror(outfile, GOT_ERR_IO);
345 static const struct got_error *
346 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
351 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
352 n = fwrite(&val, 1, sizeof(val), outfile);
353 if (n != sizeof(val))
354 return got_ferror(outfile, GOT_ERR_IO);
358 static const struct got_error *
359 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
361 size_t n, len, pad = 0;
362 static const uint8_t zero[8] = { 0 };
365 while ((len + pad) % 8 != 0)
368 pad = 8; /* NUL-terminate */
370 SHA1Update(ctx, path, len);
371 n = fwrite(path, 1, len, outfile);
373 return got_ferror(outfile, GOT_ERR_IO);
374 SHA1Update(ctx, zero, pad);
375 n = fwrite(zero, 1, pad, outfile);
377 return got_ferror(outfile, GOT_ERR_IO);
381 static const struct got_error *
382 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
385 const struct got_error *err;
389 err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
392 err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
395 err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
398 err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
402 err = write_fileindex_val32(ctx, ie->uid, outfile);
405 err = write_fileindex_val32(ctx, ie->gid, outfile);
408 err = write_fileindex_val32(ctx, ie->size, outfile);
412 err = write_fileindex_val16(ctx, ie->mode, outfile);
416 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
417 n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
418 if (n != SHA1_DIGEST_LENGTH)
419 return got_ferror(outfile, GOT_ERR_IO);
421 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
422 n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
423 if (n != SHA1_DIGEST_LENGTH)
424 return got_ferror(outfile, GOT_ERR_IO);
426 err = write_fileindex_val32(ctx, ie->flags, outfile);
430 err = write_fileindex_path(ctx, ie->path, outfile);
434 stage = got_fileindex_entry_stage_get(ie);
435 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
436 stage == GOT_FILEIDX_STAGE_ADD) {
437 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
438 n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
440 if (n != SHA1_DIGEST_LENGTH)
441 return got_ferror(outfile, GOT_ERR_IO);
447 const struct got_error *
448 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
450 const struct got_error *err = NULL;
451 struct got_fileindex_hdr hdr;
453 uint8_t sha1[SHA1_DIGEST_LENGTH];
455 struct got_fileindex_entry *ie, *tmp;
459 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
460 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
461 hdr.nentries = htobe32(fileindex->nentries);
463 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
464 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
465 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
466 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
467 if (n != sizeof(hdr.signature))
468 return got_ferror(outfile, GOT_ERR_IO);
469 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
470 if (n != sizeof(hdr.version))
471 return got_ferror(outfile, GOT_ERR_IO);
472 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
473 if (n != sizeof(hdr.nentries))
474 return got_ferror(outfile, GOT_ERR_IO);
476 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
477 ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
478 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH) {
479 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
480 got_fileindex_entry_free(ie);
483 err = write_fileindex_entry(&ctx, ie, outfile);
488 SHA1Final(sha1, &ctx);
489 n = fwrite(sha1, 1, sizeof(sha1), outfile);
490 if (n != sizeof(sha1))
491 return got_ferror(outfile, GOT_ERR_IO);
493 if (fflush(outfile) != 0)
494 return got_error_from_errno("fflush");
499 static const struct got_error *
500 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
504 n = fread(val, 1, sizeof(*val), infile);
505 if (n != sizeof(*val))
506 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
507 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
508 *val = be64toh(*val);
512 static const struct got_error *
513 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
517 n = fread(val, 1, sizeof(*val), infile);
518 if (n != sizeof(*val))
519 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
520 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
521 *val = be32toh(*val);
525 static const struct got_error *
526 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
530 n = fread(val, 1, sizeof(*val), infile);
531 if (n != sizeof(*val))
532 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
533 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
534 *val = be16toh(*val);
538 static const struct got_error *
539 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
541 const struct got_error *err = NULL;
542 const size_t chunk_size = 8;
543 size_t n, len = 0, totlen = chunk_size;
545 *path = malloc(totlen);
547 return got_error_from_errno("malloc");
550 if (len + chunk_size > totlen) {
551 char *p = reallocarray(*path, totlen + chunk_size, 1);
553 err = got_error_from_errno("reallocarray");
556 totlen += chunk_size;
559 n = fread(*path + len, 1, chunk_size, infile);
560 if (n != chunk_size) {
561 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
564 SHA1Update(ctx, *path + len, chunk_size);
566 } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
575 static const struct got_error *
576 read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
577 FILE *infile, uint32_t version)
579 const struct got_error *err;
580 struct got_fileindex_entry *ie;
585 ie = calloc(1, sizeof(*ie));
587 return got_error_from_errno("calloc");
589 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
592 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
595 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
598 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
602 err = read_fileindex_val32(&ie->uid, ctx, infile);
605 err = read_fileindex_val32(&ie->gid, ctx, infile);
608 err = read_fileindex_val32(&ie->size, ctx, infile);
612 err = read_fileindex_val16(&ie->mode, ctx, infile);
616 n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
617 if (n != SHA1_DIGEST_LENGTH) {
618 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
621 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
623 n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
624 if (n != SHA1_DIGEST_LENGTH) {
625 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
628 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
630 err = read_fileindex_val32(&ie->flags, ctx, infile);
634 err = read_fileindex_path(&ie->path, ctx, infile);
639 uint32_t stage = got_fileindex_entry_stage_get(ie);
640 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
641 stage == GOT_FILEIDX_STAGE_ADD) {
642 n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
644 if (n != SHA1_DIGEST_LENGTH) {
645 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
648 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
651 /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
652 ie->flags &= ~GOT_FILEIDX_F_STAGE;
657 got_fileindex_entry_free(ie);
663 const struct got_error *
664 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
666 const struct got_error *err = NULL;
667 struct got_fileindex_hdr hdr;
669 struct got_fileindex_entry *ie;
670 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
671 uint8_t sha1[SHA1_DIGEST_LENGTH];
677 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
678 if (n != sizeof(hdr.signature)) {
679 if (n == 0) /* EOF */
681 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
683 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
684 if (n != sizeof(hdr.version)) {
685 if (n == 0) /* EOF */
687 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
689 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
690 if (n != sizeof(hdr.nentries)) {
691 if (n == 0) /* EOF */
693 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
696 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
697 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
698 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
700 hdr.signature = be32toh(hdr.signature);
701 hdr.version = be32toh(hdr.version);
702 hdr.nentries = be32toh(hdr.nentries);
704 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
705 return got_error(GOT_ERR_FILEIDX_SIG);
706 if (hdr.version > GOT_FILE_INDEX_VERSION)
707 return got_error(GOT_ERR_FILEIDX_VER);
709 for (i = 0; i < hdr.nentries; i++) {
710 err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
713 err = add_entry(fileindex, ie);
718 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
719 if (n != sizeof(sha1_expected))
720 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
721 SHA1Final(sha1, &ctx);
722 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
723 return got_error(GOT_ERR_FILEIDX_CSUM);
728 static struct got_fileindex_entry *
729 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
731 struct got_fileindex_entry *next;
733 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
735 /* Skip entries which were added or removed by diff callbacks. */
736 while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
737 GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
738 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
743 static const struct got_error *
744 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
745 struct got_tree_object *tree, const char *, const char *,
746 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
748 static const struct got_error *
749 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
750 struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
751 const char *path, const char *entry_name, struct got_repository *repo,
752 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
754 const struct got_error *err = NULL;
755 struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
757 if (!got_object_tree_entry_is_submodule(te) &&
758 S_ISDIR(got_tree_entry_get_mode(te))) {
760 struct got_tree_object *subtree;
762 if (asprintf(&subpath, "%s%s%s", path,
763 path[0] == '\0' ? "" : "/",
764 got_tree_entry_get_name(te)) == -1)
765 return got_error_from_errno("asprintf");
767 err = got_object_open_as_tree(&subtree, repo,
768 got_tree_entry_get_id(te));
774 err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
775 entry_name, repo, cb, cb_arg);
777 got_object_tree_close(subtree);
783 *next = got_object_tree_get_entry(tree, *tidx);
787 static const struct got_error *
788 diff_fileindex_tree(struct got_fileindex *fileindex,
789 struct got_fileindex_entry **ie, struct got_tree_object *tree,
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 = NULL;
795 size_t path_len = strlen(path);
796 struct got_fileindex_entry *next;
799 te = got_object_tree_get_entry(tree, tidx);
800 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
803 const char *te_name = got_tree_entry_get_name(te);
805 if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
806 err = got_error_from_errno("asprintf");
809 cmp = got_path_cmp((*ie)->path, te_path,
810 got_fileindex_entry_path_len(*ie), strlen(te_path));
813 if (got_path_is_child((*ie)->path, path,
815 !got_object_tree_entry_is_submodule(te) &&
816 (entry_name == NULL ||
817 strcmp(te_name, entry_name) == 0)) {
818 err = cb->diff_old_new(cb_arg, *ie, te,
820 if (err || entry_name)
823 *ie = walk_fileindex(fileindex, *ie);
824 err = walk_tree(&te, fileindex, ie, tree, &tidx,
825 path, entry_name, repo, cb, cb_arg);
826 } else if (cmp < 0) {
827 next = walk_fileindex(fileindex, *ie);
828 if (got_path_is_child((*ie)->path, path,
829 path_len) && (entry_name == NULL ||
830 strcmp(te_name, entry_name) == 0)) {
831 err = cb->diff_old(cb_arg, *ie, path);
832 if (err || entry_name)
837 if ((entry_name == NULL ||
838 strcmp(te_name, entry_name) == 0)) {
839 err = cb->diff_new(cb_arg, te, path);
840 if (err || entry_name)
843 err = walk_tree(&te, fileindex, ie, tree, &tidx,
844 path, entry_name, repo, cb, cb_arg);
849 next = walk_fileindex(fileindex, *ie);
850 if (got_path_is_child((*ie)->path, path, path_len) &&
851 (entry_name == NULL ||
852 (te && strcmp(got_tree_entry_get_name(te),
853 entry_name) == 0))) {
854 err = cb->diff_old(cb_arg, *ie, path);
855 if (err || entry_name)
860 if (!got_object_tree_entry_is_submodule(te) &&
861 (entry_name == NULL ||
862 strcmp(got_tree_entry_get_name(te), entry_name)
864 err = cb->diff_new(cb_arg, te, path);
865 if (err || entry_name)
868 err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
869 entry_name, repo, cb, cb_arg);
878 const struct got_error *
879 got_fileindex_diff_tree(struct got_fileindex *fileindex,
880 struct got_tree_object *tree, const char *path, const char *entry_name,
881 struct got_repository *repo,
882 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
884 struct got_fileindex_entry *ie;
885 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
886 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
887 ie = walk_fileindex(fileindex, ie);
888 return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
892 static const struct got_error *
893 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
894 struct got_pathlist_head *, int, const char *, const char *,
895 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
897 static const struct got_error *
898 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
900 const struct got_error *err = NULL;
901 struct got_pathlist_entry *new = NULL;
902 struct dirent *dep = NULL;
903 struct dirent *de = NULL;
906 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
908 err = got_error_from_errno("malloc");
912 if (readdir_r(dir, de, &dep) != 0) {
913 err = got_error_from_errno("readdir_r");
922 if (strcmp(de->d_name, ".") == 0 ||
923 strcmp(de->d_name, "..") == 0 ||
925 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
930 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
936 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
946 free_dirlist(struct got_pathlist_head *dirlist)
948 struct got_pathlist_entry *dle;
950 TAILQ_FOREACH(dle, dirlist, entry)
952 got_pathlist_free(dirlist);
955 static const struct got_error *
956 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
957 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
958 const char *path, const char *rootpath, struct got_repository *repo,
959 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
961 const struct got_error *err = NULL;
962 struct dirent *de = dle->data;
969 if (de->d_type == DT_UNKNOWN) {
970 /* Occurs on NFS mounts without "readdir plus" RPC. */
972 if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
973 return got_error_from_errno("asprintf");
974 err = got_path_dirent_type(&type, dir_path, de);
981 if (type == DT_DIR) {
984 struct got_pathlist_head subdirlist;
986 TAILQ_INIT(&subdirlist);
988 if (asprintf(&subpath, "%s%s%s", path,
989 path[0] == '\0' ? "" : "/", de->d_name) == -1)
990 return got_error_from_errno("asprintf");
992 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
994 return got_error_from_errno("asprintf");
997 subdirfd = openat(fd, de->d_name,
998 O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
999 if (subdirfd == -1) {
1000 if (errno == EACCES) {
1001 *next = TAILQ_NEXT(dle, entry);
1004 err = got_error_from_errno2("openat", subdirpath);
1010 subdir = fdopendir(subdirfd);
1012 return got_error_from_errno2("fdopendir", path);
1014 err = read_dirlist(&subdirlist, subdir, subdirpath);
1021 err = diff_fileindex_dir(fileindex, ie, &subdirlist,
1022 dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
1023 if (subdir && closedir(subdir) == -1 && err == NULL)
1024 err = got_error_from_errno2("closedir", subdirpath);
1027 free_dirlist(&subdirlist);
1032 *next = TAILQ_NEXT(dle, entry);
1036 static const struct got_error *
1037 diff_fileindex_dir(struct got_fileindex *fileindex,
1038 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1039 int dirfd, const char *rootpath, const char *path,
1040 struct got_repository *repo,
1041 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1043 const struct got_error *err = NULL;
1044 struct dirent *de = NULL;
1045 size_t path_len = strlen(path);
1046 struct got_pathlist_entry *dle;
1048 if (cb->diff_traverse) {
1049 err = cb->diff_traverse(cb_arg, path, dirfd);
1054 dle = TAILQ_FIRST(dirlist);
1055 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1060 if (asprintf(&de_path, "%s/%s", path,
1061 de->d_name) == -1) {
1062 err = got_error_from_errno("asprintf");
1065 cmp = got_path_cmp((*ie)->path, de_path,
1066 got_fileindex_entry_path_len(*ie),
1067 strlen(path) + 1 + de->d_namlen);
1070 err = cb->diff_old_new(cb_arg, *ie, de, path,
1074 *ie = walk_fileindex(fileindex, *ie);
1075 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1076 path, rootpath, repo, cb, cb_arg);
1077 } else if (cmp < 0 ) {
1078 err = cb->diff_old(cb_arg, *ie, path);
1081 *ie = walk_fileindex(fileindex, *ie);
1083 err = cb->diff_new(cb_arg, de, path, dirfd);
1086 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1087 path, rootpath, repo, cb, cb_arg);
1092 err = cb->diff_old(cb_arg, *ie, path);
1095 *ie = walk_fileindex(fileindex, *ie);
1098 err = cb->diff_new(cb_arg, de, path, dirfd);
1101 err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1102 rootpath, repo, cb, cb_arg);
1111 const struct got_error *
1112 got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1113 const char *rootpath, const char *path, struct got_repository *repo,
1114 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1116 const struct got_error *err;
1117 struct got_fileindex_entry *ie;
1118 struct got_pathlist_head dirlist;
1122 TAILQ_INIT(&dirlist);
1125 * Duplicate the file descriptor so we can call closedir() below
1126 * without closing the file descriptor passed in by our caller.
1130 return got_error_from_errno2("dup", path);
1131 if (lseek(fd2, 0, SEEK_SET) == -1) {
1132 err = got_error_from_errno2("lseek", path);
1136 dir = fdopendir(fd2);
1138 err = got_error_from_errno2("fdopendir", path);
1142 err = read_dirlist(&dirlist, dir, path);
1148 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1149 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1150 ie = walk_fileindex(fileindex, ie);
1151 err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1152 rootpath, path, repo, cb, cb_arg);
1154 if (closedir(dir) == -1 && err == NULL)
1155 err = got_error_from_errno2("closedir", path);
1156 free_dirlist(&dirlist);
1160 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);