Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/queue.h>
18 #include <sys/tree.h>
19 #include <sys/stat.h>
21 #include <dirent.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sha1.h>
26 #include <endian.h>
27 #include <limits.h>
28 #include <uuid.h>
30 #include "got_error.h"
31 #include "got_object.h"
33 #include "got_lib_path.h"
34 #include "got_lib_fileindex.h"
35 #include "got_lib_worktree.h"
37 /* got_fileindex_entry flags */
38 #define GOT_FILEIDX_F_PATH_LEN 0x00000fff
39 #define GOT_FILEIDX_F_STAGE 0x00003000
40 #define GOT_FILEIDX_F_EXTENDED 0x00004000
41 #define GOT_FILEIDX_F_ASSUME_VALID 0x00008000
42 #define GOT_FILEIDX_F_NOT_FLUSHED 0x00010000
43 #define GOT_FILEIDX_F_NO_BLOB 0x00020000
44 #define GOT_FILEIDX_F_NO_COMMIT 0x00040000
46 struct got_fileindex {
47 struct got_fileindex_tree entries;
48 int nentries;
49 #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
50 };
52 const struct got_error *
53 got_fileindex_entry_update(struct got_fileindex_entry *entry,
54 const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
55 int update_timestamps)
56 {
57 struct stat sb;
59 if (lstat(ondisk_path, &sb) != 0)
60 return got_error_from_errno();
62 if (update_timestamps) {
63 entry->ctime_sec = sb.st_ctime;
64 entry->ctime_nsec = sb.st_ctimensec;
65 entry->mtime_sec = sb.st_mtime;
66 entry->mtime_nsec = sb.st_mtimensec;
67 }
68 entry->uid = sb.st_uid;
69 entry->gid = sb.st_gid;
70 entry->size = (sb.st_size & 0xffffffff);
71 if (sb.st_mode & S_IFLNK)
72 entry->mode = GOT_FILEIDX_MODE_SYMLINK;
73 else
74 entry->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
75 entry->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
76 GOT_FILEIDX_MODE_PERMS_SHIFT);
78 if (blob_sha1) {
79 memcpy(entry->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
80 entry->flags &= ~GOT_FILEIDX_F_NO_BLOB;
81 } else
82 entry->flags |= GOT_FILEIDX_F_NO_BLOB;
84 if (commit_sha1) {
85 memcpy(entry->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
86 entry->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
87 } else
88 entry->flags |= GOT_FILEIDX_F_NO_COMMIT;
90 return NULL;
91 }
93 const struct got_error *
94 got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
95 const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
96 uint8_t *commit_sha1)
97 {
98 size_t len;
100 *entry = calloc(1, sizeof(**entry));
101 if (*entry == NULL)
102 return got_error_from_errno();
104 (*entry)->path = strdup(relpath);
105 if ((*entry)->path == NULL) {
106 const struct got_error *err = got_error_from_errno();
107 free(*entry);
108 *entry = NULL;
109 return err;
112 len = strlen(relpath);
113 if (len > GOT_FILEIDX_F_PATH_LEN)
114 len = GOT_FILEIDX_F_PATH_LEN;
115 (*entry)->flags |= len;
117 return got_fileindex_entry_update(*entry, ondisk_path, blob_sha1,
118 commit_sha1, 1);
121 void
122 got_fileindex_entry_free(struct got_fileindex_entry *entry)
124 free(entry->path);
125 free(entry);
128 int
129 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
131 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
134 int
135 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
137 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
140 static const struct got_error *
141 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *entry)
143 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
144 return got_error(GOT_ERR_NO_SPACE);
146 RB_INSERT(got_fileindex_tree, &fileindex->entries, entry);
147 fileindex->nentries++;
148 return NULL;
151 const struct got_error *
152 got_fileindex_entry_add(struct got_fileindex *fileindex,
153 struct got_fileindex_entry *entry)
155 /* Flag this entry until it gets written out to disk. */
156 entry->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
158 return add_entry(fileindex, entry);
161 void
162 got_fileindex_entry_remove(struct got_fileindex *fileindex,
163 struct got_fileindex_entry *entry)
165 RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
166 fileindex->nentries--;
169 struct got_fileindex_entry *
170 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
172 struct got_fileindex_entry key;
173 memset(&key, 0, sizeof(key));
174 key.path = (char *)path;
175 return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
178 const struct got_error *
179 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
180 got_fileindex_cb cb, void *cb_arg)
182 const struct got_error *err;
183 struct got_fileindex_entry *entry, *tmp;
185 RB_FOREACH_SAFE(entry, got_fileindex_tree, &fileindex->entries, tmp) {
186 err = (*cb)(cb_arg, entry);
187 if (err)
188 return err;
190 return NULL;
193 struct got_fileindex *
194 got_fileindex_alloc(void)
196 struct got_fileindex *fileindex;
198 fileindex = calloc(1, sizeof(*fileindex));
199 if (fileindex == NULL)
200 return NULL;
202 RB_INIT(&fileindex->entries);
203 return fileindex;
206 void
207 got_fileindex_free(struct got_fileindex *fileindex)
209 struct got_fileindex_entry *entry;
211 while ((entry = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
212 RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
213 got_fileindex_entry_free(entry);
215 free(fileindex);
218 static const struct got_error *
219 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
221 size_t n;
223 val = htobe64(val);
224 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
225 n = fwrite(&val, 1, sizeof(val), outfile);
226 if (n != sizeof(val))
227 return got_ferror(outfile, GOT_ERR_IO);
228 return NULL;
231 static const struct got_error *
232 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
234 size_t n;
236 val = htobe32(val);
237 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
238 n = fwrite(&val, 1, sizeof(val), outfile);
239 if (n != sizeof(val))
240 return got_ferror(outfile, GOT_ERR_IO);
241 return NULL;
244 static const struct got_error *
245 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
247 size_t n;
249 val = htobe16(val);
250 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
251 n = fwrite(&val, 1, sizeof(val), outfile);
252 if (n != sizeof(val))
253 return got_ferror(outfile, GOT_ERR_IO);
254 return NULL;
257 static const struct got_error *
258 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
260 size_t n, len, pad = 0;
261 static const uint8_t zero[8] = { 0 };
263 len = strlen(path);
264 while ((len + pad) % 8 != 0)
265 pad++;
266 if (pad == 0)
267 pad = 8; /* NUL-terminate */
269 SHA1Update(ctx, path, len);
270 n = fwrite(path, 1, len, outfile);
271 if (n != len)
272 return got_ferror(outfile, GOT_ERR_IO);
273 SHA1Update(ctx, zero, pad);
274 n = fwrite(zero, 1, pad, outfile);
275 if (n != pad)
276 return got_ferror(outfile, GOT_ERR_IO);
277 return NULL;
280 static const struct got_error *
281 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
282 FILE *outfile)
284 const struct got_error *err;
285 size_t n;
287 err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
288 if (err)
289 return err;
290 err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
291 if (err)
292 return err;
293 err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
294 if (err)
295 return err;
296 err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
297 if (err)
298 return err;
300 err = write_fileindex_val32(ctx, entry->uid, outfile);
301 if (err)
302 return err;
303 err = write_fileindex_val32(ctx, entry->gid, outfile);
304 if (err)
305 return err;
306 err = write_fileindex_val32(ctx, entry->size, outfile);
307 if (err)
308 return err;
310 err = write_fileindex_val16(ctx, entry->mode, outfile);
311 if (err)
312 return err;
314 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
315 n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
316 if (n != SHA1_DIGEST_LENGTH)
317 return got_ferror(outfile, GOT_ERR_IO);
319 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
320 n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
321 if (n != SHA1_DIGEST_LENGTH)
322 return got_ferror(outfile, GOT_ERR_IO);
324 err = write_fileindex_val32(ctx, entry->flags, outfile);
325 if (err)
326 return err;
328 err = write_fileindex_path(ctx, entry->path, outfile);
329 return err;
332 const struct got_error *
333 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
335 const struct got_error *err = NULL;
336 struct got_fileindex_hdr hdr;
337 SHA1_CTX ctx;
338 uint8_t sha1[SHA1_DIGEST_LENGTH];
339 size_t n;
340 struct got_fileindex_entry *entry;
342 SHA1Init(&ctx);
344 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
345 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
346 hdr.nentries = htobe32(fileindex->nentries);
348 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
349 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
350 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
351 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
352 if (n != sizeof(hdr.signature))
353 return got_ferror(outfile, GOT_ERR_IO);
354 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
355 if (n != sizeof(hdr.version))
356 return got_ferror(outfile, GOT_ERR_IO);
357 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
358 if (n != sizeof(hdr.nentries))
359 return got_ferror(outfile, GOT_ERR_IO);
361 RB_FOREACH(entry, got_fileindex_tree, &fileindex->entries) {
362 entry->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
363 err = write_fileindex_entry(&ctx, entry, outfile);
364 if (err)
365 return err;
368 SHA1Final(sha1, &ctx);
369 n = fwrite(sha1, 1, sizeof(sha1), outfile);
370 if (n != sizeof(sha1))
371 return got_ferror(outfile, GOT_ERR_IO);
373 if (fflush(outfile) != 0)
374 return got_error_from_errno();
376 return NULL;
379 static const struct got_error *
380 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
382 size_t n;
384 n = fread(val, 1, sizeof(*val), infile);
385 if (n != sizeof(*val))
386 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
387 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
388 *val = be64toh(*val);
389 return NULL;
392 static const struct got_error *
393 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
395 size_t n;
397 n = fread(val, 1, sizeof(*val), infile);
398 if (n != sizeof(*val))
399 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
400 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
401 *val = be32toh(*val);
402 return NULL;
405 static const struct got_error *
406 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
408 size_t n;
410 n = fread(val, 1, sizeof(*val), infile);
411 if (n != sizeof(*val))
412 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
413 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
414 *val = be16toh(*val);
415 return NULL;
418 static const struct got_error *
419 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
421 const struct got_error *err = NULL;
422 uint8_t buf[8];
423 size_t n, len = 0, totlen = sizeof(buf);
425 *path = malloc(totlen);
426 if (*path == NULL)
427 return got_error_from_errno();
429 do {
430 n = fread(buf, 1, sizeof(buf), infile);
431 if (n != sizeof(buf))
432 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
433 if (len + sizeof(buf) > totlen) {
434 char *p = reallocarray(*path, totlen + sizeof(buf), 1);
435 if (p == NULL) {
436 err = got_error_from_errno();
437 break;
439 totlen += sizeof(buf);
440 *path = p;
442 SHA1Update(ctx, buf, sizeof(buf));
443 memcpy(*path + len, buf, sizeof(buf));
444 len += sizeof(buf);
445 } while (memchr(buf, '\0', sizeof(buf)) == NULL);
447 if (err) {
448 free(*path);
449 *path = NULL;
451 return err;
454 static const struct got_error *
455 read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
456 FILE *infile)
458 const struct got_error *err;
459 struct got_fileindex_entry *entry;
460 size_t n;
462 *entryp = NULL;
464 entry = calloc(1, sizeof(*entry));
465 if (entry == NULL)
466 return got_error_from_errno();
468 err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
469 if (err)
470 goto done;
471 err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
472 if (err)
473 goto done;
474 err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
475 if (err)
476 goto done;
477 err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
478 if (err)
479 goto done;
481 err = read_fileindex_val32(&entry->uid, ctx, infile);
482 if (err)
483 goto done;
484 err = read_fileindex_val32(&entry->gid, ctx, infile);
485 if (err)
486 goto done;
487 err = read_fileindex_val32(&entry->size, ctx, infile);
488 if (err)
489 goto done;
491 err = read_fileindex_val16(&entry->mode, ctx, infile);
492 if (err)
493 goto done;
495 n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
496 if (n != SHA1_DIGEST_LENGTH) {
497 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
498 goto done;
500 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
502 n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
503 if (n != SHA1_DIGEST_LENGTH) {
504 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
505 goto done;
507 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
509 err = read_fileindex_val32(&entry->flags, ctx, infile);
510 if (err)
511 goto done;
513 err = read_fileindex_path(&entry->path, ctx, infile);
514 done:
515 if (err)
516 free(entry);
517 else
518 *entryp = entry;
519 return err;
522 const struct got_error *
523 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
525 const struct got_error *err = NULL;
526 struct got_fileindex_hdr hdr;
527 SHA1_CTX ctx;
528 struct got_fileindex_entry *entry;
529 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
530 uint8_t sha1[SHA1_DIGEST_LENGTH];
531 size_t n;
532 int i;
534 SHA1Init(&ctx);
536 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
537 if (n != sizeof(hdr.signature)) {
538 if (n == 0) /* EOF */
539 return NULL;
540 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
542 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
543 if (n != sizeof(hdr.version)) {
544 if (n == 0) /* EOF */
545 return NULL;
546 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
548 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
549 if (n != sizeof(hdr.nentries)) {
550 if (n == 0) /* EOF */
551 return NULL;
552 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
555 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
556 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
557 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
559 hdr.signature = be32toh(hdr.signature);
560 hdr.version = be32toh(hdr.version);
561 hdr.nentries = be32toh(hdr.nentries);
563 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
564 return got_error(GOT_ERR_FILEIDX_SIG);
565 if (hdr.version != GOT_FILE_INDEX_VERSION)
566 return got_error(GOT_ERR_FILEIDX_VER);
568 for (i = 0; i < hdr.nentries; i++) {
569 err = read_fileindex_entry(&entry, &ctx, infile);
570 if (err)
571 return err;
572 err = add_entry(fileindex, entry);
573 if (err)
574 return err;
577 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
578 if (n != sizeof(sha1_expected))
579 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
580 SHA1Final(sha1, &ctx);
581 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
582 return got_error(GOT_ERR_FILEIDX_CSUM);
584 return NULL;
587 static struct got_fileindex_entry *
588 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
590 struct got_fileindex_entry *next;
592 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
594 /* Skip entries which were newly added by diff callbacks. */
595 while (next && (next->flags & GOT_FILEIDX_F_NOT_FLUSHED))
596 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
598 return next;
601 static const struct got_error *
602 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
603 struct got_tree_object *, const char *, struct got_repository *,
604 struct got_fileindex_diff_tree_cb *, void *);
606 static const struct got_error *
607 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
608 struct got_fileindex_entry **ie, struct got_tree_entry *te,
609 const char *path, struct got_repository *repo,
610 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
612 const struct got_error *err = NULL;
614 if (S_ISDIR(te->mode)) {
615 char *subpath;
616 struct got_tree_object *subtree;
618 if (asprintf(&subpath, "%s%s%s", path,
619 path[0] == '\0' ? "" : "/", te->name) == -1)
620 return got_error_from_errno();
622 err = got_object_open_as_tree(&subtree, repo, te->id);
623 if (err) {
624 free(subpath);
625 return err;
628 err = diff_fileindex_tree(fileindex, ie, subtree,
629 subpath, repo, cb, cb_arg);
630 free(subpath);
631 got_object_tree_close(subtree);
632 if (err)
633 return err;
636 *next = SIMPLEQ_NEXT(te, entry);
637 return NULL;
640 static const struct got_error *
641 diff_fileindex_tree(struct got_fileindex *fileindex,
642 struct got_fileindex_entry **ie, struct got_tree_object *tree,
643 const char *path, struct got_repository *repo,
644 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
646 const struct got_error *err = NULL;
647 struct got_tree_entry *te = NULL;
648 size_t path_len = strlen(path);
649 const struct got_tree_entries *entries;
650 struct got_fileindex_entry *next;
652 entries = got_object_tree_get_entries(tree);
653 te = SIMPLEQ_FIRST(&entries->head);
654 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
655 if (te && *ie) {
656 char *te_path;
657 int cmp;
658 if (asprintf(&te_path, "%s/%s", path, te->name) == -1) {
659 err = got_error_from_errno();
660 break;
662 cmp = got_path_cmp((*ie)->path, te_path);
663 free(te_path);
664 if (cmp == 0) {
665 err = cb->diff_old_new(cb_arg, *ie, te,
666 path);
667 if (err)
668 break;
669 *ie = walk_fileindex(fileindex, *ie);
670 err = walk_tree(&te, fileindex, ie, te,
671 path, repo, cb, cb_arg);
672 } else if (cmp < 0 ) {
673 next = walk_fileindex(fileindex, *ie);
674 err = cb->diff_old(cb_arg, *ie, path);
675 if (err)
676 break;
677 *ie = next;
678 } else {
679 err = cb->diff_new(cb_arg, te, path);
680 if (err)
681 break;
682 err = walk_tree(&te, fileindex, ie, te,
683 path, repo, cb, cb_arg);
685 if (err)
686 break;
687 } else if (*ie) {
688 next = walk_fileindex(fileindex, *ie);
689 err = cb->diff_old(cb_arg, *ie, path);
690 if (err)
691 break;
692 *ie = next;
693 } else if (te) {
694 err = cb->diff_new(cb_arg, te, path);
695 if (err)
696 break;
697 err = walk_tree(&te, fileindex, ie, te, path, repo, cb,
698 cb_arg);
699 if (err)
700 break;
704 return err;
707 const struct got_error *
708 got_fileindex_diff_tree(struct got_fileindex *fileindex,
709 struct got_tree_object *tree, struct got_repository *repo,
710 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
712 struct got_fileindex_entry *min;
713 min = RB_MIN(got_fileindex_tree, &fileindex->entries);
714 return diff_fileindex_tree(fileindex, &min, tree, "", repo, cb, cb_arg);
717 static const struct got_error *
718 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **, DIR *,
719 const char *, const char *, struct got_repository *,
720 struct got_fileindex_diff_dir_cb *, void *);
722 static const struct got_error *
723 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
724 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle,
725 const char *path, DIR *dir, const char *rootpath,
726 struct got_repository *repo, struct got_fileindex_diff_dir_cb *cb,
727 void *cb_arg)
729 const struct got_error *err = NULL;
730 struct dirent *de = dle->data;
732 if (de->d_type == DT_DIR) {
733 char *subpath;
734 char *subdirpath;
735 DIR *subdir;
737 if (asprintf(&subpath, "%s%s%s", path,
738 path[0] == '\0' ? "" : "/", de->d_name) == -1)
739 return got_error_from_errno();
741 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
742 free(subpath);
743 return got_error_from_errno();
746 subdir = opendir(subdirpath);
747 if (subdir == NULL) {
748 free(subpath);
749 free(subdirpath);
750 return got_error_from_errno();
753 err = diff_fileindex_dir(fileindex, ie, subdir, rootpath,
754 subpath, repo, cb, cb_arg);
755 free(subpath);
756 free(subdirpath);
757 closedir(subdir);
758 if (err)
759 return err;
762 *next = TAILQ_NEXT(dle, entry);
763 return NULL;
766 static const struct got_error *
767 diff_fileindex_dir(struct got_fileindex *fileindex,
768 struct got_fileindex_entry **ie, DIR *dir, const char *rootpath,
769 const char *path, struct got_repository *repo,
770 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
772 const struct got_error *err = NULL;
773 struct dirent *de = NULL;
774 size_t path_len = strlen(path);
775 struct got_fileindex_entry *next;
776 struct got_pathlist_head dirlist;
777 struct got_pathlist_entry *dle;
779 TAILQ_INIT(&dirlist);
781 while (1) {
782 struct got_pathlist_entry *new = NULL;
783 struct dirent *dep = NULL;
785 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
786 if (de == NULL) {
787 err = got_error_from_errno();
788 goto done;
791 if (readdir_r(dir, de, &dep) != 0) {
792 err = got_error_from_errno();
793 free(de);
794 goto done;
796 if (dep == NULL) {
797 free(de);
798 break;
801 if (strcmp(de->d_name, ".") == 0 ||
802 strcmp(de->d_name, "..") == 0 ||
803 (path[0] == '\0' &&
804 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
805 free(de);
806 continue;
809 err = got_pathlist_insert(&new, &dirlist, de->d_name, de);
810 if (err)
811 goto done;
812 if (new == NULL) {
813 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
814 goto done;
818 dle = TAILQ_FIRST(&dirlist);
819 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
820 if (dle && *ie) {
821 char *de_path;
822 int cmp;
823 de = dle->data;
824 if (asprintf(&de_path, "%s/%s", path,
825 de->d_name) == -1) {
826 err = got_error_from_errno();
827 break;
829 cmp = got_path_cmp((*ie)->path, de_path);
830 free(de_path);
831 if (cmp == 0) {
832 err = cb->diff_old_new(cb_arg, *ie, de, path);
833 if (err)
834 break;
835 *ie = walk_fileindex(fileindex, *ie);
836 err = walk_dir(&dle, fileindex, ie, dle, path,
837 dir, rootpath, repo, cb, cb_arg);
838 } else if (cmp < 0 ) {
839 next = walk_fileindex(fileindex, *ie);
840 err = cb->diff_old(cb_arg, *ie, path);
841 if (err)
842 break;
843 *ie = next;
844 } else {
845 err = cb->diff_new(cb_arg, de, path);
846 if (err)
847 break;
848 err = walk_dir(&dle, fileindex, ie, dle, path,
849 dir, rootpath, repo, cb, cb_arg);
851 if (err)
852 break;
853 } else if (*ie) {
854 next = walk_fileindex(fileindex, *ie);
855 err = cb->diff_old(cb_arg, *ie, path);
856 if (err)
857 break;
858 *ie = next;
859 } else if (dle) {
860 de = dle->data;
861 err = cb->diff_new(cb_arg, de, path);
862 if (err)
863 break;
864 err = walk_dir(&dle, fileindex, ie, dle, path, dir,
865 rootpath, repo, cb, cb_arg);
866 if (err)
867 break;
870 done:
871 TAILQ_FOREACH(dle, &dirlist, entry)
872 free(dle->data);
873 got_pathlist_free(&dirlist);
874 return err;
877 const struct got_error *
878 got_fileindex_diff_dir(struct got_fileindex *fileindex, DIR *rootdir,
879 const char *rootpath, const char *path, struct got_repository *repo,
880 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
882 struct got_fileindex_entry *min;
883 min = RB_MIN(got_fileindex_tree, &fileindex->entries);
884 return diff_fileindex_dir(fileindex, &min, rootdir, rootpath, path,
885 repo, cb, cb_arg);
888 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);