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 static const struct got_error *
129 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *entry)
131 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
132 return got_error(GOT_ERR_NO_SPACE);
134 RB_INSERT(got_fileindex_tree, &fileindex->entries, entry);
135 fileindex->nentries++;
136 return NULL;
139 const struct got_error *
140 got_fileindex_entry_add(struct got_fileindex *fileindex,
141 struct got_fileindex_entry *entry)
143 /* Flag this entry until it gets written out to disk. */
144 entry->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
146 return add_entry(fileindex, entry);
149 void
150 got_fileindex_entry_remove(struct got_fileindex *fileindex,
151 struct got_fileindex_entry *entry)
153 RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
154 fileindex->nentries--;
157 struct got_fileindex_entry *
158 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
160 struct got_fileindex_entry key;
161 memset(&key, 0, sizeof(key));
162 key.path = (char *)path;
163 return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
166 const struct got_error *
167 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
168 got_fileindex_cb cb, void *cb_arg)
170 const struct got_error *err;
171 struct got_fileindex_entry *entry, *tmp;
173 RB_FOREACH_SAFE(entry, got_fileindex_tree, &fileindex->entries, tmp) {
174 err = (*cb)(cb_arg, entry);
175 if (err)
176 return err;
178 return NULL;
181 struct got_fileindex *
182 got_fileindex_alloc(void)
184 struct got_fileindex *fileindex;
186 fileindex = calloc(1, sizeof(*fileindex));
187 if (fileindex == NULL)
188 return NULL;
190 RB_INIT(&fileindex->entries);
191 return fileindex;
194 void
195 got_fileindex_free(struct got_fileindex *fileindex)
197 struct got_fileindex_entry *entry;
199 while ((entry = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
200 RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
201 got_fileindex_entry_free(entry);
203 free(fileindex);
206 static const struct got_error *
207 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
209 size_t n;
211 val = htobe64(val);
212 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
213 n = fwrite(&val, 1, sizeof(val), outfile);
214 if (n != sizeof(val))
215 return got_ferror(outfile, GOT_ERR_IO);
216 return NULL;
219 static const struct got_error *
220 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
222 size_t n;
224 val = htobe32(val);
225 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
226 n = fwrite(&val, 1, sizeof(val), outfile);
227 if (n != sizeof(val))
228 return got_ferror(outfile, GOT_ERR_IO);
229 return NULL;
232 static const struct got_error *
233 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
235 size_t n;
237 val = htobe16(val);
238 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
239 n = fwrite(&val, 1, sizeof(val), outfile);
240 if (n != sizeof(val))
241 return got_ferror(outfile, GOT_ERR_IO);
242 return NULL;
245 static const struct got_error *
246 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
248 size_t n, len, pad = 0;
249 static const uint8_t zero[8] = { 0 };
251 len = strlen(path);
252 while ((len + pad) % 8 != 0)
253 pad++;
254 if (pad == 0)
255 pad = 8; /* NUL-terminate */
257 SHA1Update(ctx, path, len);
258 n = fwrite(path, 1, len, outfile);
259 if (n != len)
260 return got_ferror(outfile, GOT_ERR_IO);
261 SHA1Update(ctx, zero, pad);
262 n = fwrite(zero, 1, pad, outfile);
263 if (n != pad)
264 return got_ferror(outfile, GOT_ERR_IO);
265 return NULL;
268 static const struct got_error *
269 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
270 FILE *outfile)
272 const struct got_error *err;
273 size_t n;
275 err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
276 if (err)
277 return err;
278 err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
279 if (err)
280 return err;
281 err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
282 if (err)
283 return err;
284 err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
285 if (err)
286 return err;
288 err = write_fileindex_val32(ctx, entry->uid, outfile);
289 if (err)
290 return err;
291 err = write_fileindex_val32(ctx, entry->gid, outfile);
292 if (err)
293 return err;
294 err = write_fileindex_val32(ctx, entry->size, outfile);
295 if (err)
296 return err;
298 err = write_fileindex_val16(ctx, entry->mode, outfile);
299 if (err)
300 return err;
302 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
303 n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
304 if (n != SHA1_DIGEST_LENGTH)
305 return got_ferror(outfile, GOT_ERR_IO);
307 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
308 n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
309 if (n != SHA1_DIGEST_LENGTH)
310 return got_ferror(outfile, GOT_ERR_IO);
312 err = write_fileindex_val32(ctx, entry->flags, outfile);
313 if (err)
314 return err;
316 err = write_fileindex_path(ctx, entry->path, outfile);
317 return err;
320 const struct got_error *
321 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
323 const struct got_error *err = NULL;
324 struct got_fileindex_hdr hdr;
325 SHA1_CTX ctx;
326 uint8_t sha1[SHA1_DIGEST_LENGTH];
327 size_t n;
328 struct got_fileindex_entry *entry;
330 SHA1Init(&ctx);
332 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
333 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
334 hdr.nentries = htobe32(fileindex->nentries);
336 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
337 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
338 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
339 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
340 if (n != sizeof(hdr.signature))
341 return got_ferror(outfile, GOT_ERR_IO);
342 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
343 if (n != sizeof(hdr.version))
344 return got_ferror(outfile, GOT_ERR_IO);
345 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
346 if (n != sizeof(hdr.nentries))
347 return got_ferror(outfile, GOT_ERR_IO);
349 RB_FOREACH(entry, got_fileindex_tree, &fileindex->entries) {
350 entry->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
351 err = write_fileindex_entry(&ctx, entry, outfile);
352 if (err)
353 return err;
356 SHA1Final(sha1, &ctx);
357 n = fwrite(sha1, 1, sizeof(sha1), outfile);
358 if (n != sizeof(sha1))
359 return got_ferror(outfile, GOT_ERR_IO);
361 if (fflush(outfile) != 0)
362 return got_error_from_errno();
364 return NULL;
367 static const struct got_error *
368 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
370 size_t n;
372 n = fread(val, 1, sizeof(*val), infile);
373 if (n != sizeof(*val))
374 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
375 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
376 *val = be64toh(*val);
377 return NULL;
380 static const struct got_error *
381 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
383 size_t n;
385 n = fread(val, 1, sizeof(*val), infile);
386 if (n != sizeof(*val))
387 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
388 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
389 *val = be32toh(*val);
390 return NULL;
393 static const struct got_error *
394 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
396 size_t n;
398 n = fread(val, 1, sizeof(*val), infile);
399 if (n != sizeof(*val))
400 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
401 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
402 *val = be16toh(*val);
403 return NULL;
406 static const struct got_error *
407 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
409 const struct got_error *err = NULL;
410 uint8_t buf[8];
411 size_t n, len = 0, totlen = sizeof(buf);
413 *path = malloc(totlen);
414 if (*path == NULL)
415 return got_error_from_errno();
417 do {
418 n = fread(buf, 1, sizeof(buf), infile);
419 if (n != sizeof(buf))
420 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
421 if (len + sizeof(buf) > totlen) {
422 char *p = reallocarray(*path, totlen + sizeof(buf), 1);
423 if (p == NULL) {
424 err = got_error_from_errno();
425 break;
427 totlen += sizeof(buf);
428 *path = p;
430 SHA1Update(ctx, buf, sizeof(buf));
431 memcpy(*path + len, buf, sizeof(buf));
432 len += sizeof(buf);
433 } while (memchr(buf, '\0', sizeof(buf)) == NULL);
435 if (err) {
436 free(*path);
437 *path = NULL;
439 return err;
442 static const struct got_error *
443 read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
444 FILE *infile)
446 const struct got_error *err;
447 struct got_fileindex_entry *entry;
448 size_t n;
450 *entryp = NULL;
452 entry = calloc(1, sizeof(*entry));
453 if (entry == NULL)
454 return got_error_from_errno();
456 err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
457 if (err)
458 goto done;
459 err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
460 if (err)
461 goto done;
462 err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
463 if (err)
464 goto done;
465 err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
466 if (err)
467 goto done;
469 err = read_fileindex_val32(&entry->uid, ctx, infile);
470 if (err)
471 goto done;
472 err = read_fileindex_val32(&entry->gid, ctx, infile);
473 if (err)
474 goto done;
475 err = read_fileindex_val32(&entry->size, ctx, infile);
476 if (err)
477 goto done;
479 err = read_fileindex_val16(&entry->mode, ctx, infile);
480 if (err)
481 goto done;
483 n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
484 if (n != SHA1_DIGEST_LENGTH) {
485 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
486 goto done;
488 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
490 n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
491 if (n != SHA1_DIGEST_LENGTH) {
492 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
493 goto done;
495 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
497 err = read_fileindex_val32(&entry->flags, ctx, infile);
498 if (err)
499 goto done;
501 err = read_fileindex_path(&entry->path, ctx, infile);
502 done:
503 if (err)
504 free(entry);
505 else
506 *entryp = entry;
507 return err;
510 const struct got_error *
511 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
513 const struct got_error *err = NULL;
514 struct got_fileindex_hdr hdr;
515 SHA1_CTX ctx;
516 struct got_fileindex_entry *entry;
517 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
518 uint8_t sha1[SHA1_DIGEST_LENGTH];
519 size_t n;
520 int i;
522 SHA1Init(&ctx);
524 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
525 if (n != sizeof(hdr.signature)) {
526 if (n == 0) /* EOF */
527 return NULL;
528 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
530 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
531 if (n != sizeof(hdr.version)) {
532 if (n == 0) /* EOF */
533 return NULL;
534 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
536 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
537 if (n != sizeof(hdr.nentries)) {
538 if (n == 0) /* EOF */
539 return NULL;
540 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
543 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
544 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
545 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
547 hdr.signature = be32toh(hdr.signature);
548 hdr.version = be32toh(hdr.version);
549 hdr.nentries = be32toh(hdr.nentries);
551 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
552 return got_error(GOT_ERR_FILEIDX_SIG);
553 if (hdr.version != GOT_FILE_INDEX_VERSION)
554 return got_error(GOT_ERR_FILEIDX_VER);
556 for (i = 0; i < hdr.nentries; i++) {
557 err = read_fileindex_entry(&entry, &ctx, infile);
558 if (err)
559 return err;
560 err = add_entry(fileindex, entry);
561 if (err)
562 return err;
565 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
566 if (n != sizeof(sha1_expected))
567 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
568 SHA1Final(sha1, &ctx);
569 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
570 return got_error(GOT_ERR_FILEIDX_CSUM);
572 return NULL;
575 static struct got_fileindex_entry *
576 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
578 struct got_fileindex_entry *next;
580 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
582 /* Skip entries which were newly added by diff callbacks. */
583 while (next && (next->flags & GOT_FILEIDX_F_NOT_FLUSHED))
584 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
586 return next;
589 static const struct got_error *
590 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
591 struct got_tree_object *, const char *, struct got_repository *,
592 struct got_fileindex_diff_tree_cb *, void *);
594 static const struct got_error *
595 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
596 struct got_fileindex_entry **ie, struct got_tree_entry *te,
597 const char *path, struct got_repository *repo,
598 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
600 const struct got_error *err = NULL;
602 if (S_ISDIR(te->mode)) {
603 char *subpath;
604 struct got_tree_object *subtree;
606 if (asprintf(&subpath, "%s%s%s", path,
607 path[0] == '\0' ? "" : "/", te->name) == -1)
608 return got_error_from_errno();
610 err = got_object_open_as_tree(&subtree, repo, te->id);
611 if (err) {
612 free(subpath);
613 return err;
616 err = diff_fileindex_tree(fileindex, ie, subtree,
617 subpath, repo, cb, cb_arg);
618 free(subpath);
619 got_object_tree_close(subtree);
620 if (err)
621 return err;
624 *next = SIMPLEQ_NEXT(te, entry);
625 return NULL;
628 static const struct got_error *
629 diff_fileindex_tree(struct got_fileindex *fileindex,
630 struct got_fileindex_entry **ie, struct got_tree_object *tree,
631 const char *path, struct got_repository *repo,
632 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
634 const struct got_error *err = NULL;
635 struct got_tree_entry *te = NULL;
636 size_t path_len = strlen(path);
637 const struct got_tree_entries *entries;
638 struct got_fileindex_entry *next;
640 entries = got_object_tree_get_entries(tree);
641 te = SIMPLEQ_FIRST(&entries->head);
642 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
643 if (te && *ie) {
644 char *te_path;
645 int cmp;
646 if (asprintf(&te_path, "%s/%s", path, te->name) == -1) {
647 err = got_error_from_errno();
648 break;
650 cmp = got_path_cmp((*ie)->path, te_path);
651 free(te_path);
652 if (cmp == 0) {
653 err = cb->diff_old_new(cb_arg, *ie, te,
654 path);
655 if (err)
656 break;
657 *ie = walk_fileindex(fileindex, *ie);
658 err = walk_tree(&te, fileindex, ie, te,
659 path, repo, cb, cb_arg);
660 } else if (cmp < 0 ) {
661 next = walk_fileindex(fileindex, *ie);
662 err = cb->diff_old(cb_arg, *ie, path);
663 if (err)
664 break;
665 *ie = next;
666 } else {
667 err = cb->diff_new(cb_arg, te, path);
668 if (err)
669 break;
670 err = walk_tree(&te, fileindex, ie, te,
671 path, repo, cb, cb_arg);
673 if (err)
674 break;
675 } else if (*ie) {
676 next = walk_fileindex(fileindex, *ie);
677 err = cb->diff_old(cb_arg, *ie, path);
678 if (err)
679 break;
680 *ie = next;
681 } else if (te) {
682 err = cb->diff_new(cb_arg, te, path);
683 if (err)
684 break;
685 err = walk_tree(&te, fileindex, ie, te, path, repo, cb,
686 cb_arg);
687 if (err)
688 break;
692 return err;
695 const struct got_error *
696 got_fileindex_diff_tree(struct got_fileindex *fileindex,
697 struct got_tree_object *tree, struct got_repository *repo,
698 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
700 struct got_fileindex_entry *min;
701 min = RB_MIN(got_fileindex_tree, &fileindex->entries);
702 return diff_fileindex_tree(fileindex, &min, tree, "", repo, cb, cb_arg);
705 static const struct got_error *
706 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **, DIR *,
707 const char *, const char *, struct got_repository *,
708 struct got_fileindex_diff_dir_cb *, void *);
710 static const struct got_error *
711 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
712 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle,
713 const char *path, DIR *dir, const char *rootpath,
714 struct got_repository *repo, struct got_fileindex_diff_dir_cb *cb,
715 void *cb_arg)
717 const struct got_error *err = NULL;
718 struct dirent *de = dle->data;
720 if (de->d_type == DT_DIR) {
721 char *subpath;
722 char *subdirpath;
723 DIR *subdir;
725 if (asprintf(&subpath, "%s%s%s", path,
726 path[0] == '\0' ? "" : "/", de->d_name) == -1)
727 return got_error_from_errno();
729 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
730 free(subpath);
731 return got_error_from_errno();
734 subdir = opendir(subdirpath);
735 if (subdir == NULL) {
736 free(subpath);
737 free(subdirpath);
738 return got_error_from_errno();
741 err = diff_fileindex_dir(fileindex, ie, subdir, rootpath,
742 subpath, repo, cb, cb_arg);
743 free(subpath);
744 free(subdirpath);
745 closedir(subdir);
746 if (err)
747 return err;
750 *next = TAILQ_NEXT(dle, entry);
751 return NULL;
754 static const struct got_error *
755 diff_fileindex_dir(struct got_fileindex *fileindex,
756 struct got_fileindex_entry **ie, DIR *dir, const char *rootpath,
757 const char *path, struct got_repository *repo,
758 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
760 const struct got_error *err = NULL;
761 struct dirent *de = NULL;
762 size_t path_len = strlen(path);
763 struct got_fileindex_entry *next;
764 struct got_pathlist_head dirlist;
765 struct got_pathlist_entry *dle;
767 TAILQ_INIT(&dirlist);
769 while (1) {
770 struct got_pathlist_entry *new = NULL;
771 struct dirent *dep = NULL;
773 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
774 if (de == NULL) {
775 err = got_error_from_errno();
776 goto done;
779 if (readdir_r(dir, de, &dep) != 0) {
780 err = got_error_from_errno();
781 free(de);
782 goto done;
784 if (dep == NULL) {
785 free(de);
786 break;
789 if (strcmp(de->d_name, ".") == 0 ||
790 strcmp(de->d_name, "..") == 0 ||
791 (path[0] == '\0' &&
792 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
793 free(de);
794 continue;
797 err = got_pathlist_insert(&new, &dirlist, de->d_name, de);
798 if (err)
799 goto done;
800 if (new == NULL) {
801 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
802 goto done;
806 dle = TAILQ_FIRST(&dirlist);
807 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
808 if (dle && *ie) {
809 char *de_path;
810 int cmp;
811 de = dle->data;
812 if (asprintf(&de_path, "%s/%s", path,
813 de->d_name) == -1) {
814 err = got_error_from_errno();
815 break;
817 cmp = got_path_cmp((*ie)->path, de_path);
818 free(de_path);
819 if (cmp == 0) {
820 err = cb->diff_old_new(cb_arg, *ie, de, path);
821 if (err)
822 break;
823 *ie = walk_fileindex(fileindex, *ie);
824 err = walk_dir(&dle, fileindex, ie, dle, path,
825 dir, rootpath, repo, cb, cb_arg);
826 } else if (cmp < 0 ) {
827 next = walk_fileindex(fileindex, *ie);
828 err = cb->diff_old(cb_arg, *ie, path);
829 if (err)
830 break;
831 *ie = next;
832 } else {
833 err = cb->diff_new(cb_arg, de, path);
834 if (err)
835 break;
836 err = walk_dir(&dle, fileindex, ie, dle, path,
837 dir, rootpath, repo, cb, cb_arg);
839 if (err)
840 break;
841 } else if (*ie) {
842 next = walk_fileindex(fileindex, *ie);
843 err = cb->diff_old(cb_arg, *ie, path);
844 if (err)
845 break;
846 *ie = next;
847 } else if (dle) {
848 de = dle->data;
849 err = cb->diff_new(cb_arg, de, path);
850 if (err)
851 break;
852 err = walk_dir(&dle, fileindex, ie, dle, path, dir,
853 rootpath, repo, cb, cb_arg);
854 if (err)
855 break;
858 done:
859 TAILQ_FOREACH(dle, &dirlist, entry)
860 free(dle->data);
861 got_pathlist_free(&dirlist);
862 return err;
865 const struct got_error *
866 got_fileindex_diff_dir(struct got_fileindex *fileindex, DIR *rootdir,
867 const char *rootpath, const char *path, struct got_repository *repo,
868 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
870 struct got_fileindex_entry *min;
871 min = RB_MIN(got_fileindex_tree, &fileindex->entries);
872 return diff_fileindex_dir(fileindex, &min, rootdir, rootpath, path,
873 repo, cb, cb_arg);
876 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);