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 0x20000000
44 struct got_fileindex {
45 struct got_fileindex_tree entries;
46 int nentries;
47 #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
48 };
50 const struct got_error *
51 got_fileindex_entry_update(struct got_fileindex_entry *entry,
52 const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
53 int update_timestamps)
54 {
55 struct stat sb;
57 if (lstat(ondisk_path, &sb) != 0)
58 return got_error_from_errno();
60 if (update_timestamps) {
61 entry->ctime_sec = sb.st_ctime;
62 entry->ctime_nsec = sb.st_ctimensec;
63 entry->mtime_sec = sb.st_mtime;
64 entry->mtime_nsec = sb.st_mtimensec;
65 }
66 entry->uid = sb.st_uid;
67 entry->gid = sb.st_gid;
68 entry->size = (sb.st_size & 0xffffffff);
69 if (sb.st_mode & S_IFLNK)
70 entry->mode = GOT_FILEIDX_MODE_SYMLINK;
71 else
72 entry->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
73 entry->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
74 GOT_FILEIDX_MODE_PERMS_SHIFT);
75 memcpy(entry->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
76 memcpy(entry->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
78 return NULL;
79 }
81 const struct got_error *
82 got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
83 const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
84 uint8_t *commit_sha1)
85 {
86 size_t len;
88 *entry = calloc(1, sizeof(**entry));
89 if (*entry == NULL)
90 return got_error_from_errno();
92 (*entry)->path = strdup(relpath);
93 if ((*entry)->path == NULL) {
94 const struct got_error *err = got_error_from_errno();
95 free(*entry);
96 *entry = NULL;
97 return err;
98 }
100 len = strlen(relpath);
101 if (len > GOT_FILEIDX_F_PATH_LEN)
102 len = GOT_FILEIDX_F_PATH_LEN;
103 (*entry)->flags |= len;
105 return got_fileindex_entry_update(*entry, ondisk_path, blob_sha1,
106 commit_sha1, 1);
109 void
110 got_fileindex_entry_free(struct got_fileindex_entry *entry)
112 free(entry->path);
113 free(entry);
116 static const struct got_error *
117 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *entry)
119 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
120 return got_error(GOT_ERR_NO_SPACE);
122 RB_INSERT(got_fileindex_tree, &fileindex->entries, entry);
123 fileindex->nentries++;
124 return NULL;
127 const struct got_error *
128 got_fileindex_entry_add(struct got_fileindex *fileindex,
129 struct got_fileindex_entry *entry)
131 /* Flag this entry until it gets written out to disk. */
132 entry->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
134 return add_entry(fileindex, entry);
137 void
138 got_fileindex_entry_remove(struct got_fileindex *fileindex,
139 struct got_fileindex_entry *entry)
141 RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
142 fileindex->nentries--;
145 struct got_fileindex_entry *
146 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
148 struct got_fileindex_entry key;
149 memset(&key, 0, sizeof(key));
150 key.path = (char *)path;
151 return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
154 const struct got_error *
155 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
156 got_fileindex_cb cb, void *cb_arg)
158 const struct got_error *err;
159 struct got_fileindex_entry *entry, *tmp;
161 RB_FOREACH_SAFE(entry, got_fileindex_tree, &fileindex->entries, tmp) {
162 err = (*cb)(cb_arg, entry);
163 if (err)
164 return err;
166 return NULL;
169 struct got_fileindex *
170 got_fileindex_alloc(void)
172 struct got_fileindex *fileindex;
174 fileindex = calloc(1, sizeof(*fileindex));
175 if (fileindex == NULL)
176 return NULL;
178 RB_INIT(&fileindex->entries);
179 return fileindex;
182 void
183 got_fileindex_free(struct got_fileindex *fileindex)
185 struct got_fileindex_entry *entry;
187 while ((entry = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
188 RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
189 got_fileindex_entry_free(entry);
191 free(fileindex);
194 static const struct got_error *
195 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
197 size_t n;
199 val = htobe64(val);
200 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
201 n = fwrite(&val, 1, sizeof(val), outfile);
202 if (n != sizeof(val))
203 return got_ferror(outfile, GOT_ERR_IO);
204 return NULL;
207 static const struct got_error *
208 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
210 size_t n;
212 val = htobe32(val);
213 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
214 n = fwrite(&val, 1, sizeof(val), outfile);
215 if (n != sizeof(val))
216 return got_ferror(outfile, GOT_ERR_IO);
217 return NULL;
220 static const struct got_error *
221 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
223 size_t n;
225 val = htobe16(val);
226 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
227 n = fwrite(&val, 1, sizeof(val), outfile);
228 if (n != sizeof(val))
229 return got_ferror(outfile, GOT_ERR_IO);
230 return NULL;
233 static const struct got_error *
234 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
236 size_t n, len, pad = 0;
237 static const uint8_t zero[8] = { 0 };
239 len = strlen(path);
240 while ((len + pad) % 8 != 0)
241 pad++;
242 if (pad == 0)
243 pad = 8; /* NUL-terminate */
245 SHA1Update(ctx, path, len);
246 n = fwrite(path, 1, len, outfile);
247 if (n != len)
248 return got_ferror(outfile, GOT_ERR_IO);
249 SHA1Update(ctx, zero, pad);
250 n = fwrite(zero, 1, pad, outfile);
251 if (n != pad)
252 return got_ferror(outfile, GOT_ERR_IO);
253 return NULL;
256 static const struct got_error *
257 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
258 FILE *outfile)
260 const struct got_error *err;
261 size_t n;
263 err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
264 if (err)
265 return err;
266 err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
267 if (err)
268 return err;
269 err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
270 if (err)
271 return err;
272 err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
273 if (err)
274 return err;
276 err = write_fileindex_val32(ctx, entry->uid, outfile);
277 if (err)
278 return err;
279 err = write_fileindex_val32(ctx, entry->gid, outfile);
280 if (err)
281 return err;
282 err = write_fileindex_val32(ctx, entry->size, outfile);
283 if (err)
284 return err;
286 err = write_fileindex_val16(ctx, entry->mode, outfile);
287 if (err)
288 return err;
290 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
291 n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
292 if (n != SHA1_DIGEST_LENGTH)
293 return got_ferror(outfile, GOT_ERR_IO);
295 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
296 n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
297 if (n != SHA1_DIGEST_LENGTH)
298 return got_ferror(outfile, GOT_ERR_IO);
300 err = write_fileindex_val32(ctx, entry->flags, outfile);
301 if (err)
302 return err;
304 err = write_fileindex_path(ctx, entry->path, outfile);
305 return err;
308 const struct got_error *
309 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
311 const struct got_error *err = NULL;
312 struct got_fileindex_hdr hdr;
313 SHA1_CTX ctx;
314 uint8_t sha1[SHA1_DIGEST_LENGTH];
315 size_t n;
316 struct got_fileindex_entry *entry;
318 SHA1Init(&ctx);
320 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
321 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
322 hdr.nentries = htobe32(fileindex->nentries);
324 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
325 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
326 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
327 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
328 if (n != sizeof(hdr.signature))
329 return got_ferror(outfile, GOT_ERR_IO);
330 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
331 if (n != sizeof(hdr.version))
332 return got_ferror(outfile, GOT_ERR_IO);
333 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
334 if (n != sizeof(hdr.nentries))
335 return got_ferror(outfile, GOT_ERR_IO);
337 RB_FOREACH(entry, got_fileindex_tree, &fileindex->entries) {
338 entry->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
339 err = write_fileindex_entry(&ctx, entry, outfile);
340 if (err)
341 return err;
344 SHA1Final(sha1, &ctx);
345 n = fwrite(sha1, 1, sizeof(sha1), outfile);
346 if (n != sizeof(sha1))
347 return got_ferror(outfile, GOT_ERR_IO);
349 if (fflush(outfile) != 0)
350 return got_error_from_errno();
352 return NULL;
355 static const struct got_error *
356 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
358 size_t n;
360 n = fread(val, 1, sizeof(*val), infile);
361 if (n != sizeof(*val))
362 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
363 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
364 *val = be64toh(*val);
365 return NULL;
368 static const struct got_error *
369 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
371 size_t n;
373 n = fread(val, 1, sizeof(*val), infile);
374 if (n != sizeof(*val))
375 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
376 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
377 *val = be32toh(*val);
378 return NULL;
381 static const struct got_error *
382 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
384 size_t n;
386 n = fread(val, 1, sizeof(*val), infile);
387 if (n != sizeof(*val))
388 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
389 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
390 *val = be16toh(*val);
391 return NULL;
394 static const struct got_error *
395 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
397 const struct got_error *err = NULL;
398 uint8_t buf[8];
399 size_t n, len = 0, totlen = sizeof(buf);
401 *path = malloc(totlen);
402 if (*path == NULL)
403 return got_error_from_errno();
405 do {
406 n = fread(buf, 1, sizeof(buf), infile);
407 if (n != sizeof(buf))
408 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
409 if (len + sizeof(buf) > totlen) {
410 char *p = reallocarray(*path, totlen + sizeof(buf), 1);
411 if (p == NULL) {
412 err = got_error_from_errno();
413 break;
415 totlen += sizeof(buf);
416 *path = p;
418 SHA1Update(ctx, buf, sizeof(buf));
419 memcpy(*path + len, buf, sizeof(buf));
420 len += sizeof(buf);
421 } while (memchr(buf, '\0', sizeof(buf)) == NULL);
423 if (err) {
424 free(*path);
425 *path = NULL;
427 return err;
430 static const struct got_error *
431 read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
432 FILE *infile)
434 const struct got_error *err;
435 struct got_fileindex_entry *entry;
436 size_t n;
438 *entryp = NULL;
440 entry = calloc(1, sizeof(*entry));
441 if (entry == NULL)
442 return got_error_from_errno();
444 err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
445 if (err)
446 goto done;
447 err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
448 if (err)
449 goto done;
450 err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
451 if (err)
452 goto done;
453 err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
454 if (err)
455 goto done;
457 err = read_fileindex_val32(&entry->uid, ctx, infile);
458 if (err)
459 goto done;
460 err = read_fileindex_val32(&entry->gid, ctx, infile);
461 if (err)
462 goto done;
463 err = read_fileindex_val32(&entry->size, ctx, infile);
464 if (err)
465 goto done;
467 err = read_fileindex_val16(&entry->mode, ctx, infile);
468 if (err)
469 goto done;
471 n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
472 if (n != SHA1_DIGEST_LENGTH) {
473 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
474 goto done;
476 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
478 n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
479 if (n != SHA1_DIGEST_LENGTH) {
480 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
481 goto done;
483 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
485 err = read_fileindex_val32(&entry->flags, ctx, infile);
486 if (err)
487 goto done;
489 err = read_fileindex_path(&entry->path, ctx, infile);
490 done:
491 if (err)
492 free(entry);
493 else
494 *entryp = entry;
495 return err;
498 const struct got_error *
499 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
501 const struct got_error *err = NULL;
502 struct got_fileindex_hdr hdr;
503 SHA1_CTX ctx;
504 struct got_fileindex_entry *entry;
505 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
506 uint8_t sha1[SHA1_DIGEST_LENGTH];
507 size_t n;
508 int i;
510 SHA1Init(&ctx);
512 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
513 if (n != sizeof(hdr.signature)) {
514 if (n == 0) /* EOF */
515 return NULL;
516 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
518 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
519 if (n != sizeof(hdr.version)) {
520 if (n == 0) /* EOF */
521 return NULL;
522 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
524 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
525 if (n != sizeof(hdr.nentries)) {
526 if (n == 0) /* EOF */
527 return NULL;
528 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
531 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
532 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
533 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
535 hdr.signature = be32toh(hdr.signature);
536 hdr.version = be32toh(hdr.version);
537 hdr.nentries = be32toh(hdr.nentries);
539 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
540 return got_error(GOT_ERR_FILEIDX_SIG);
541 if (hdr.version != GOT_FILE_INDEX_VERSION)
542 return got_error(GOT_ERR_FILEIDX_VER);
544 for (i = 0; i < hdr.nentries; i++) {
545 err = read_fileindex_entry(&entry, &ctx, infile);
546 if (err)
547 return err;
548 err = add_entry(fileindex, entry);
549 if (err)
550 return err;
553 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
554 if (n != sizeof(sha1_expected))
555 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
556 SHA1Final(sha1, &ctx);
557 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
558 return got_error(GOT_ERR_FILEIDX_CSUM);
560 return NULL;
563 static struct got_fileindex_entry *
564 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
566 struct got_fileindex_entry *next;
568 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
570 /* Skip entries which were newly added by diff callbacks. */
571 while (next && (next->flags & GOT_FILEIDX_F_NOT_FLUSHED))
572 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
574 return next;
577 static const struct got_error *
578 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
579 struct got_tree_object *, const char *, struct got_repository *,
580 struct got_fileindex_diff_tree_cb *, void *);
582 static const struct got_error *
583 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
584 struct got_fileindex_entry **ie, struct got_tree_entry *te,
585 const char *path, struct got_repository *repo,
586 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
588 const struct got_error *err = NULL;
590 if (S_ISDIR(te->mode)) {
591 char *subpath;
592 struct got_tree_object *subtree;
594 if (asprintf(&subpath, "%s%s%s", path,
595 path[0] == '\0' ? "" : "/", te->name) == -1)
596 return got_error_from_errno();
598 err = got_object_open_as_tree(&subtree, repo, te->id);
599 if (err) {
600 free(subpath);
601 return err;
604 err = diff_fileindex_tree(fileindex, ie, subtree,
605 subpath, repo, cb, cb_arg);
606 free(subpath);
607 got_object_tree_close(subtree);
608 if (err)
609 return err;
612 *next = SIMPLEQ_NEXT(te, entry);
613 return NULL;
616 static const struct got_error *
617 diff_fileindex_tree(struct got_fileindex *fileindex,
618 struct got_fileindex_entry **ie, struct got_tree_object *tree,
619 const char *path, struct got_repository *repo,
620 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
622 const struct got_error *err = NULL;
623 struct got_tree_entry *te = NULL;
624 size_t path_len = strlen(path);
625 const struct got_tree_entries *entries;
626 struct got_fileindex_entry *next;
628 entries = got_object_tree_get_entries(tree);
629 te = SIMPLEQ_FIRST(&entries->head);
630 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
631 if (te && *ie) {
632 char *te_path;
633 int cmp;
634 if (asprintf(&te_path, "%s/%s", path, te->name) == -1) {
635 err = got_error_from_errno();
636 break;
638 cmp = got_path_cmp((*ie)->path, te_path);
639 free(te_path);
640 if (cmp == 0) {
641 err = cb->diff_old_new(cb_arg, *ie, te,
642 path);
643 if (err)
644 break;
645 *ie = walk_fileindex(fileindex, *ie);
646 err = walk_tree(&te, fileindex, ie, te,
647 path, repo, cb, cb_arg);
648 } else if (cmp < 0 ) {
649 next = walk_fileindex(fileindex, *ie);
650 err = cb->diff_old(cb_arg, *ie, path);
651 if (err)
652 break;
653 *ie = next;
654 } else {
655 err = cb->diff_new(cb_arg, te, path);
656 if (err)
657 break;
658 err = walk_tree(&te, fileindex, ie, te,
659 path, repo, cb, cb_arg);
661 if (err)
662 break;
663 } else if (*ie) {
664 next = walk_fileindex(fileindex, *ie);
665 err = cb->diff_old(cb_arg, *ie, path);
666 if (err)
667 break;
668 *ie = next;
669 } else if (te) {
670 err = cb->diff_new(cb_arg, te, path);
671 if (err)
672 break;
673 err = walk_tree(&te, fileindex, ie, te, path, repo, cb,
674 cb_arg);
675 if (err)
676 break;
680 return err;
683 const struct got_error *
684 got_fileindex_diff_tree(struct got_fileindex *fileindex,
685 struct got_tree_object *tree, struct got_repository *repo,
686 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
688 struct got_fileindex_entry *min;
689 min = RB_MIN(got_fileindex_tree, &fileindex->entries);
690 return diff_fileindex_tree(fileindex, &min, tree, "", repo, cb, cb_arg);
693 static const struct got_error *
694 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **, DIR *,
695 const char *, const char *, struct got_repository *,
696 struct got_fileindex_diff_dir_cb *, void *);
698 static const struct got_error *
699 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
700 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle,
701 const char *path, DIR *dir, const char *rootpath,
702 struct got_repository *repo, struct got_fileindex_diff_dir_cb *cb,
703 void *cb_arg)
705 const struct got_error *err = NULL;
706 struct dirent *de = dle->data;
708 if (de->d_type == DT_DIR) {
709 char *subpath;
710 char *subdirpath;
711 DIR *subdir;
713 if (asprintf(&subpath, "%s%s%s", path,
714 path[0] == '\0' ? "" : "/", de->d_name) == -1)
715 return got_error_from_errno();
717 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
718 free(subpath);
719 return got_error_from_errno();
722 subdir = opendir(subdirpath);
723 if (subdir == NULL) {
724 free(subpath);
725 free(subdirpath);
726 return got_error_from_errno();
729 err = diff_fileindex_dir(fileindex, ie, subdir, rootpath,
730 subpath, repo, cb, cb_arg);
731 free(subpath);
732 free(subdirpath);
733 closedir(subdir);
734 if (err)
735 return err;
738 *next = TAILQ_NEXT(dle, entry);
739 return NULL;
742 static const struct got_error *
743 diff_fileindex_dir(struct got_fileindex *fileindex,
744 struct got_fileindex_entry **ie, DIR *dir, const char *rootpath,
745 const char *path, struct got_repository *repo,
746 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
748 const struct got_error *err = NULL;
749 struct dirent *de = NULL;
750 size_t path_len = strlen(path);
751 struct got_fileindex_entry *next;
752 struct got_pathlist_head dirlist;
753 struct got_pathlist_entry *dle;
755 TAILQ_INIT(&dirlist);
757 while (1) {
758 struct got_pathlist_entry *new = NULL;
759 struct dirent *dep = NULL;
761 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
762 if (de == NULL) {
763 err = got_error_from_errno();
764 goto done;
767 if (readdir_r(dir, de, &dep) != 0) {
768 err = got_error_from_errno();
769 free(de);
770 goto done;
772 if (dep == NULL) {
773 free(de);
774 break;
777 if (strcmp(de->d_name, ".") == 0 ||
778 strcmp(de->d_name, "..") == 0 ||
779 (path[0] == '\0' &&
780 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
781 free(de);
782 continue;
785 err = got_pathlist_insert(&new, &dirlist, de->d_name, de);
786 if (err)
787 goto done;
788 if (new == NULL) {
789 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
790 goto done;
794 dle = TAILQ_FIRST(&dirlist);
795 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
796 if (dle && *ie) {
797 char *de_path;
798 int cmp;
799 de = dle->data;
800 if (asprintf(&de_path, "%s/%s", path,
801 de->d_name) == -1) {
802 err = got_error_from_errno();
803 break;
805 cmp = got_path_cmp((*ie)->path, de_path);
806 free(de_path);
807 if (cmp == 0) {
808 err = cb->diff_old_new(cb_arg, *ie, de, path);
809 if (err)
810 break;
811 *ie = walk_fileindex(fileindex, *ie);
812 err = walk_dir(&dle, fileindex, ie, dle, path,
813 dir, rootpath, repo, cb, cb_arg);
814 } else if (cmp < 0 ) {
815 next = walk_fileindex(fileindex, *ie);
816 err = cb->diff_old(cb_arg, *ie, path);
817 if (err)
818 break;
819 *ie = next;
820 } else {
821 err = cb->diff_new(cb_arg, de, path);
822 if (err)
823 break;
824 err = walk_dir(&dle, fileindex, ie, dle, path,
825 dir, rootpath, repo, cb, cb_arg);
827 if (err)
828 break;
829 } else if (*ie) {
830 next = walk_fileindex(fileindex, *ie);
831 err = cb->diff_old(cb_arg, *ie, path);
832 if (err)
833 break;
834 *ie = next;
835 } else if (dle) {
836 de = dle->data;
837 err = cb->diff_new(cb_arg, de, path);
838 if (err)
839 break;
840 err = walk_dir(&dle, fileindex, ie, dle, path, dir,
841 rootpath, repo, cb, cb_arg);
842 if (err)
843 break;
846 done:
847 TAILQ_FOREACH(dle, &dirlist, entry)
848 free(dle->data);
849 got_pathlist_free(&dirlist);
850 return err;
853 const struct got_error *
854 got_fileindex_diff_dir(struct got_fileindex *fileindex, DIR *rootdir,
855 const char *rootpath, const char *path, struct got_repository *repo,
856 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
858 struct got_fileindex_entry *min;
859 min = RB_MIN(got_fileindex_tree, &fileindex->entries);
860 return diff_fileindex_dir(fileindex, &min, rootdir, rootpath, path,
861 repo, cb, cb_arg);
864 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);