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>
29 #include "got_error.h"
30 #include "got_object.h"
32 #include "got_lib_path.h"
33 #include "got_lib_fileindex.h"
34 #include "got_lib_worktree.h"
36 struct got_fileindex {
37 struct got_fileindex_tree entries;
38 int nentries;
39 #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
40 };
42 const struct got_error *
43 got_fileindex_entry_update(struct got_fileindex_entry *entry,
44 const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
45 int update_timestamps)
46 {
47 struct stat sb;
49 if (lstat(ondisk_path, &sb) != 0)
50 return got_error_from_errno();
52 if (update_timestamps) {
53 entry->ctime_sec = sb.st_ctime;
54 entry->ctime_nsec = sb.st_ctimensec;
55 entry->mtime_sec = sb.st_mtime;
56 entry->mtime_nsec = sb.st_mtimensec;
57 }
58 entry->uid = sb.st_uid;
59 entry->gid = sb.st_gid;
60 entry->size = (sb.st_size & 0xffffffff);
61 if (sb.st_mode & S_IFLNK)
62 entry->mode = GOT_FILEIDX_MODE_SYMLINK;
63 else
64 entry->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
65 entry->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
66 GOT_FILEIDX_MODE_PERMS_SHIFT);
67 memcpy(entry->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
68 memcpy(entry->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
70 return NULL;
71 }
73 const struct got_error *
74 got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
75 const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
76 uint8_t *commit_sha1)
77 {
78 size_t len;
80 *entry = calloc(1, sizeof(**entry));
81 if (*entry == NULL)
82 return got_error_from_errno();
84 (*entry)->path = strdup(relpath);
85 if ((*entry)->path == NULL) {
86 const struct got_error *err = got_error_from_errno();
87 free(*entry);
88 *entry = NULL;
89 return err;
90 }
92 len = strlen(relpath);
93 if (len > GOT_FILEIDX_F_PATH_LEN)
94 len = GOT_FILEIDX_F_PATH_LEN;
95 (*entry)->flags |= len;
97 return got_fileindex_entry_update(*entry, ondisk_path, blob_sha1,
98 commit_sha1, 1);
99 }
101 void
102 got_fileindex_entry_free(struct got_fileindex_entry *entry)
104 free(entry->path);
105 free(entry);
108 static const struct got_error *
109 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *entry)
111 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
112 return got_error(GOT_ERR_NO_SPACE);
114 RB_INSERT(got_fileindex_tree, &fileindex->entries, entry);
115 fileindex->nentries++;
116 return NULL;
119 const struct got_error *
120 got_fileindex_entry_add(struct got_fileindex *fileindex,
121 struct got_fileindex_entry *entry)
123 /* Flag this entry until it gets written out to disk. */
124 entry->flags |= GOT_FILEIDX_F_INTENT_TO_ADD;
126 return add_entry(fileindex, entry);
129 void
130 got_fileindex_entry_remove(struct got_fileindex *fileindex,
131 struct got_fileindex_entry *entry)
133 RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
134 fileindex->nentries--;
137 struct got_fileindex_entry *
138 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
140 struct got_fileindex_entry key;
141 memset(&key, 0, sizeof(key));
142 key.path = (char *)path;
143 return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
146 const struct got_error *
147 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
148 got_fileindex_cb cb, void *cb_arg)
150 const struct got_error *err;
151 struct got_fileindex_entry *entry, *tmp;
153 RB_FOREACH_SAFE(entry, got_fileindex_tree, &fileindex->entries, tmp) {
154 err = (*cb)(cb_arg, entry);
155 if (err)
156 return err;
158 return NULL;
161 struct got_fileindex *
162 got_fileindex_alloc(void)
164 struct got_fileindex *fileindex;
166 fileindex = calloc(1, sizeof(*fileindex));
167 if (fileindex == NULL)
168 return NULL;
170 RB_INIT(&fileindex->entries);
171 return fileindex;
174 void
175 got_fileindex_free(struct got_fileindex *fileindex)
177 struct got_fileindex_entry *entry;
179 while ((entry = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
180 RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
181 got_fileindex_entry_free(entry);
183 free(fileindex);
186 static const struct got_error *
187 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
189 size_t n;
191 val = htobe64(val);
192 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
193 n = fwrite(&val, 1, sizeof(val), outfile);
194 if (n != sizeof(val))
195 return got_ferror(outfile, GOT_ERR_IO);
196 return NULL;
199 static const struct got_error *
200 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
202 size_t n;
204 val = htobe32(val);
205 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
206 n = fwrite(&val, 1, sizeof(val), outfile);
207 if (n != sizeof(val))
208 return got_ferror(outfile, GOT_ERR_IO);
209 return NULL;
212 static const struct got_error *
213 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
215 size_t n;
217 val = htobe16(val);
218 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
219 n = fwrite(&val, 1, sizeof(val), outfile);
220 if (n != sizeof(val))
221 return got_ferror(outfile, GOT_ERR_IO);
222 return NULL;
225 static const struct got_error *
226 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
228 size_t n, len, pad = 0;
229 static const uint8_t zero[8] = { 0 };
231 len = strlen(path);
232 while ((len + pad) % 8 != 0)
233 pad++;
234 if (pad == 0)
235 pad = 8; /* NUL-terminate */
237 SHA1Update(ctx, path, len);
238 n = fwrite(path, 1, len, outfile);
239 if (n != len)
240 return got_ferror(outfile, GOT_ERR_IO);
241 SHA1Update(ctx, zero, pad);
242 n = fwrite(zero, 1, pad, outfile);
243 if (n != pad)
244 return got_ferror(outfile, GOT_ERR_IO);
245 return NULL;
248 static const struct got_error *
249 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
250 FILE *outfile)
252 const struct got_error *err;
253 size_t n;
255 err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
256 if (err)
257 return err;
258 err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
259 if (err)
260 return err;
261 err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
262 if (err)
263 return err;
264 err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
265 if (err)
266 return err;
268 err = write_fileindex_val32(ctx, entry->uid, outfile);
269 if (err)
270 return err;
271 err = write_fileindex_val32(ctx, entry->gid, outfile);
272 if (err)
273 return err;
274 err = write_fileindex_val32(ctx, entry->size, outfile);
275 if (err)
276 return err;
278 err = write_fileindex_val16(ctx, entry->mode, outfile);
279 if (err)
280 return err;
282 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
283 n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
284 if (n != SHA1_DIGEST_LENGTH)
285 return got_ferror(outfile, GOT_ERR_IO);
287 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
288 n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
289 if (n != SHA1_DIGEST_LENGTH)
290 return got_ferror(outfile, GOT_ERR_IO);
292 err = write_fileindex_val32(ctx, entry->flags, outfile);
293 if (err)
294 return err;
296 err = write_fileindex_path(ctx, entry->path, outfile);
297 return err;
300 const struct got_error *
301 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
303 const struct got_error *err = NULL;
304 struct got_fileindex_hdr hdr;
305 SHA1_CTX ctx;
306 uint8_t sha1[SHA1_DIGEST_LENGTH];
307 size_t n;
308 struct got_fileindex_entry *entry;
310 SHA1Init(&ctx);
312 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
313 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
314 hdr.nentries = htobe32(fileindex->nentries);
316 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
317 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
318 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
319 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
320 if (n != sizeof(hdr.signature))
321 return got_ferror(outfile, GOT_ERR_IO);
322 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
323 if (n != sizeof(hdr.version))
324 return got_ferror(outfile, GOT_ERR_IO);
325 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
326 if (n != sizeof(hdr.nentries))
327 return got_ferror(outfile, GOT_ERR_IO);
329 RB_FOREACH(entry, got_fileindex_tree, &fileindex->entries) {
330 entry->flags &= ~GOT_FILEIDX_F_INTENT_TO_ADD;
331 err = write_fileindex_entry(&ctx, entry, outfile);
332 if (err)
333 return err;
336 SHA1Final(sha1, &ctx);
337 n = fwrite(sha1, 1, sizeof(sha1), outfile);
338 if (n != sizeof(sha1))
339 return got_ferror(outfile, GOT_ERR_IO);
341 if (fflush(outfile) != 0)
342 return got_error_from_errno();
344 return NULL;
347 static const struct got_error *
348 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
350 size_t n;
352 n = fread(val, 1, sizeof(*val), infile);
353 if (n != sizeof(*val))
354 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
355 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
356 *val = be64toh(*val);
357 return NULL;
360 static const struct got_error *
361 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
363 size_t n;
365 n = fread(val, 1, sizeof(*val), infile);
366 if (n != sizeof(*val))
367 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
368 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
369 *val = be32toh(*val);
370 return NULL;
373 static const struct got_error *
374 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
376 size_t n;
378 n = fread(val, 1, sizeof(*val), infile);
379 if (n != sizeof(*val))
380 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
381 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
382 *val = be16toh(*val);
383 return NULL;
386 static const struct got_error *
387 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
389 const struct got_error *err = NULL;
390 uint8_t buf[8];
391 size_t n, len = 0, totlen = sizeof(buf);
393 *path = malloc(totlen);
394 if (*path == NULL)
395 return got_error_from_errno();
397 do {
398 n = fread(buf, 1, sizeof(buf), infile);
399 if (n != sizeof(buf))
400 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
401 if (len + sizeof(buf) > totlen) {
402 char *p = reallocarray(*path, totlen + sizeof(buf), 1);
403 if (p == NULL) {
404 err = got_error_from_errno();
405 break;
407 totlen += sizeof(buf);
408 *path = p;
410 SHA1Update(ctx, buf, sizeof(buf));
411 memcpy(*path + len, buf, sizeof(buf));
412 len += sizeof(buf);
413 } while (memchr(buf, '\0', sizeof(buf)) == NULL);
415 if (err) {
416 free(*path);
417 *path = NULL;
419 return err;
422 static const struct got_error *
423 read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
424 FILE *infile)
426 const struct got_error *err;
427 struct got_fileindex_entry *entry;
428 size_t n;
430 *entryp = NULL;
432 entry = calloc(1, sizeof(*entry));
433 if (entry == NULL)
434 return got_error_from_errno();
436 err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
437 if (err)
438 goto done;
439 err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
440 if (err)
441 goto done;
442 err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
443 if (err)
444 goto done;
445 err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
446 if (err)
447 goto done;
449 err = read_fileindex_val32(&entry->uid, ctx, infile);
450 if (err)
451 goto done;
452 err = read_fileindex_val32(&entry->gid, ctx, infile);
453 if (err)
454 goto done;
455 err = read_fileindex_val32(&entry->size, ctx, infile);
456 if (err)
457 goto done;
459 err = read_fileindex_val16(&entry->mode, ctx, infile);
460 if (err)
461 goto done;
463 n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
464 if (n != SHA1_DIGEST_LENGTH) {
465 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
466 goto done;
468 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
470 n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
471 if (n != SHA1_DIGEST_LENGTH) {
472 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
473 goto done;
475 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
477 err = read_fileindex_val32(&entry->flags, ctx, infile);
478 if (err)
479 goto done;
481 err = read_fileindex_path(&entry->path, ctx, infile);
482 done:
483 if (err)
484 free(entry);
485 else
486 *entryp = entry;
487 return err;
490 const struct got_error *
491 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
493 const struct got_error *err = NULL;
494 struct got_fileindex_hdr hdr;
495 SHA1_CTX ctx;
496 struct got_fileindex_entry *entry;
497 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
498 uint8_t sha1[SHA1_DIGEST_LENGTH];
499 size_t n;
500 int i;
502 SHA1Init(&ctx);
504 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
505 if (n != sizeof(hdr.signature)) {
506 if (n == 0) /* EOF */
507 return NULL;
508 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
510 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
511 if (n != sizeof(hdr.version)) {
512 if (n == 0) /* EOF */
513 return NULL;
514 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
516 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
517 if (n != sizeof(hdr.nentries)) {
518 if (n == 0) /* EOF */
519 return NULL;
520 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
523 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
524 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
525 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
527 hdr.signature = be32toh(hdr.signature);
528 hdr.version = be32toh(hdr.version);
529 hdr.nentries = be32toh(hdr.nentries);
531 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
532 return got_error(GOT_ERR_FILEIDX_SIG);
533 if (hdr.version != GOT_FILE_INDEX_VERSION)
534 return got_error(GOT_ERR_FILEIDX_VER);
536 for (i = 0; i < hdr.nentries; i++) {
537 err = read_fileindex_entry(&entry, &ctx, infile);
538 if (err)
539 return err;
540 err = add_entry(fileindex, entry);
541 if (err)
542 return err;
545 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
546 if (n != sizeof(sha1_expected))
547 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
548 SHA1Final(sha1, &ctx);
549 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
550 return got_error(GOT_ERR_FILEIDX_CSUM);
552 return NULL;
555 struct got_fileindex_entry *
556 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
558 struct got_fileindex_entry *next;
560 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
562 /* Skip entries which were newly added by diff callbacks. */
563 while (next && (next->flags & GOT_FILEIDX_F_INTENT_TO_ADD))
564 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
566 return next;
569 static const struct got_error *
570 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
571 struct got_tree_object *, const char *, struct got_repository *,
572 struct got_fileindex_diff_tree_cb *, void *);
574 static const struct got_error *
575 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
576 struct got_fileindex_entry **ie, struct got_tree_entry *te,
577 const char *path, struct got_repository *repo,
578 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
580 const struct got_error *err = NULL;
582 if (S_ISDIR(te->mode)) {
583 char *subpath;
584 struct got_tree_object *subtree;
586 if (asprintf(&subpath, "%s%s%s", path,
587 path[0] == '\0' ? "" : "/", te->name) == -1)
588 return got_error_from_errno();
590 err = got_object_open_as_tree(&subtree, repo, te->id);
591 if (err) {
592 free(subpath);
593 return err;
596 err = diff_fileindex_tree(fileindex, ie, subtree,
597 subpath, repo, cb, cb_arg);
598 free(subpath);
599 got_object_tree_close(subtree);
600 if (err)
601 return err;
604 *next = SIMPLEQ_NEXT(te, entry);
605 return NULL;
608 static const struct got_error *
609 diff_fileindex_tree(struct got_fileindex *fileindex,
610 struct got_fileindex_entry **ie, struct got_tree_object *tree,
611 const char *path, struct got_repository *repo,
612 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
614 const struct got_error *err = NULL;
615 struct got_tree_entry *te = NULL;
616 size_t path_len = strlen(path);
617 const struct got_tree_entries *entries;
618 struct got_fileindex_entry *next;
620 entries = got_object_tree_get_entries(tree);
621 te = SIMPLEQ_FIRST(&entries->head);
622 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
623 if (te && *ie) {
624 char *te_path;
625 int cmp;
626 if (asprintf(&te_path, "%s/%s", path, te->name) == -1) {
627 err = got_error_from_errno();
628 break;
630 cmp = got_path_cmp((*ie)->path, te_path);
631 free(te_path);
632 if (cmp == 0) {
633 err = cb->diff_old_new(cb_arg, *ie, te,
634 path);
635 if (err)
636 break;
637 *ie = walk_fileindex(fileindex, *ie);
638 err = walk_tree(&te, fileindex, ie, te,
639 path, repo, cb, cb_arg);
640 } else if (cmp < 0 ) {
641 next = walk_fileindex(fileindex, *ie);
642 err = cb->diff_old(cb_arg, *ie, path);
643 if (err)
644 break;
645 *ie = next;
646 } else {
647 err = cb->diff_new(cb_arg, te, path);
648 if (err)
649 break;
650 err = walk_tree(&te, fileindex, ie, te,
651 path, repo, cb, cb_arg);
653 if (err)
654 break;
655 } else if (*ie) {
656 next = walk_fileindex(fileindex, *ie);
657 err = cb->diff_old(cb_arg, *ie, path);
658 if (err)
659 break;
660 *ie = next;
661 } else if (te) {
662 err = cb->diff_new(cb_arg, te, path);
663 if (err)
664 break;
665 err = walk_tree(&te, fileindex, ie, te, path, repo, cb,
666 cb_arg);
667 if (err)
668 break;
672 return err;
675 const struct got_error *
676 got_fileindex_diff_tree(struct got_fileindex *fileindex,
677 struct got_tree_object *tree, struct got_repository *repo,
678 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
680 struct got_fileindex_entry *min;
681 min = RB_MIN(got_fileindex_tree, &fileindex->entries);
682 return diff_fileindex_tree(fileindex, &min, tree, "", repo, cb, cb_arg);
685 static const struct got_error *
686 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **, DIR *,
687 const char *, const char *, struct got_repository *,
688 struct got_fileindex_diff_dir_cb *, void *);
690 static const struct got_error *
691 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
692 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle,
693 const char *path, DIR *dir, const char *rootpath,
694 struct got_repository *repo, struct got_fileindex_diff_dir_cb *cb,
695 void *cb_arg)
697 const struct got_error *err = NULL;
698 struct dirent *de = dle->data;
700 if (de->d_type == DT_DIR) {
701 char *subpath;
702 char *subdirpath;
703 DIR *subdir;
705 if (asprintf(&subpath, "%s%s%s", path,
706 path[0] == '\0' ? "" : "/", de->d_name) == -1)
707 return got_error_from_errno();
709 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
710 free(subpath);
711 return got_error_from_errno();
714 subdir = opendir(subdirpath);
715 if (subdir == NULL) {
716 free(subpath);
717 free(subdirpath);
718 return got_error_from_errno();
721 err = diff_fileindex_dir(fileindex, ie, subdir, rootpath,
722 subpath, repo, cb, cb_arg);
723 free(subpath);
724 free(subdirpath);
725 closedir(subdir);
726 if (err)
727 return err;
730 *next = TAILQ_NEXT(dle, entry);
731 return NULL;
734 static const struct got_error *
735 diff_fileindex_dir(struct got_fileindex *fileindex,
736 struct got_fileindex_entry **ie, DIR *dir, const char *rootpath,
737 const char *path, struct got_repository *repo,
738 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
740 const struct got_error *err = NULL;
741 struct dirent *de = NULL;
742 size_t path_len = strlen(path);
743 struct got_fileindex_entry *next;
744 struct got_pathlist_head dirlist;
745 struct got_pathlist_entry *dle;
747 TAILQ_INIT(&dirlist);
749 while (1) {
750 struct got_pathlist_entry *new = NULL;
751 struct dirent *dep = NULL;
753 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
754 if (de == NULL) {
755 err = got_error_from_errno();
756 goto done;
759 if (readdir_r(dir, de, &dep) != 0) {
760 err = got_error_from_errno();
761 free(de);
762 goto done;
764 if (dep == NULL) {
765 free(de);
766 break;
769 if (strcmp(de->d_name, ".") == 0 ||
770 strcmp(de->d_name, "..") == 0 ||
771 (path[0] == '\0' &&
772 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
773 free(de);
774 continue;
777 err = got_pathlist_insert(&new, &dirlist, de->d_name, de);
778 if (err)
779 goto done;
780 if (new == NULL) {
781 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
782 goto done;
786 dle = TAILQ_FIRST(&dirlist);
787 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
788 if (dle && *ie) {
789 char *de_path;
790 int cmp;
791 de = dle->data;
792 if (asprintf(&de_path, "%s/%s", path,
793 de->d_name) == -1) {
794 err = got_error_from_errno();
795 break;
797 cmp = got_path_cmp((*ie)->path, de_path);
798 free(de_path);
799 if (cmp == 0) {
800 err = cb->diff_old_new(cb_arg, *ie, de, path);
801 if (err)
802 break;
803 *ie = walk_fileindex(fileindex, *ie);
804 err = walk_dir(&dle, fileindex, ie, dle, path,
805 dir, rootpath, repo, cb, cb_arg);
806 } else if (cmp < 0 ) {
807 next = walk_fileindex(fileindex, *ie);
808 err = cb->diff_old(cb_arg, *ie, path);
809 if (err)
810 break;
811 *ie = next;
812 } else {
813 err = cb->diff_new(cb_arg, de, path);
814 if (err)
815 break;
816 err = walk_dir(&dle, fileindex, ie, dle, path,
817 dir, rootpath, repo, cb, cb_arg);
819 if (err)
820 break;
821 } else if (*ie) {
822 next = walk_fileindex(fileindex, *ie);
823 err = cb->diff_old(cb_arg, *ie, path);
824 if (err)
825 break;
826 *ie = next;
827 } else if (dle) {
828 de = dle->data;
829 err = cb->diff_new(cb_arg, de, path);
830 if (err)
831 break;
832 err = walk_dir(&dle, fileindex, ie, dle, path, dir,
833 rootpath, repo, cb, cb_arg);
834 if (err)
835 break;
838 done:
839 TAILQ_FOREACH(dle, &dirlist, entry)
840 free(dle->data);
841 got_pathlist_free(&dirlist);
842 return err;
845 const struct got_error *
846 got_fileindex_diff_dir(struct got_fileindex *fileindex, DIR *rootdir,
847 const char *rootpath, const char *path, struct got_repository *repo,
848 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
850 struct got_fileindex_entry *min;
851 min = RB_MIN(got_fileindex_tree, &fileindex->entries);
852 return diff_fileindex_dir(fileindex, &min, rootdir, rootpath, path,
853 repo, cb, cb_arg);
856 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);