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 {
46 struct stat sb;
48 if (lstat(ondisk_path, &sb) != 0)
49 return got_error_from_errno();
51 entry->ctime_sec = sb.st_ctime;
52 entry->ctime_nsec = sb.st_ctimensec;
53 entry->mtime_sec = sb.st_mtime;
54 entry->mtime_nsec = sb.st_mtimensec;
55 entry->uid = sb.st_uid;
56 entry->gid = sb.st_gid;
57 entry->size = (sb.st_size & 0xffffffff);
58 if (sb.st_mode & S_IFLNK)
59 entry->mode = GOT_FILEIDX_MODE_SYMLINK;
60 else
61 entry->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
62 entry->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
63 GOT_FILEIDX_MODE_PERMS_SHIFT);
64 memcpy(entry->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
65 memcpy(entry->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
67 return NULL;
68 }
70 const struct got_error *
71 got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
72 const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
73 uint8_t *commit_sha1)
74 {
75 size_t len;
77 *entry = calloc(1, sizeof(**entry));
78 if (*entry == NULL)
79 return got_error_from_errno();
81 (*entry)->path = strdup(relpath);
82 if ((*entry)->path == NULL) {
83 const struct got_error *err = got_error_from_errno();
84 free(*entry);
85 *entry = NULL;
86 return err;
87 }
89 len = strlen(relpath);
90 if (len > GOT_FILEIDX_F_PATH_LEN)
91 len = GOT_FILEIDX_F_PATH_LEN;
92 (*entry)->flags |= len;
94 return got_fileindex_entry_update(*entry, ondisk_path, blob_sha1,
95 commit_sha1);
96 }
98 void
99 got_fileindex_entry_free(struct got_fileindex_entry *entry)
101 free(entry->path);
102 free(entry);
105 static const struct got_error *
106 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *entry)
108 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
109 return got_error(GOT_ERR_NO_SPACE);
111 RB_INSERT(got_fileindex_tree, &fileindex->entries, entry);
112 fileindex->nentries++;
113 return NULL;
116 const struct got_error *
117 got_fileindex_entry_add(struct got_fileindex *fileindex,
118 struct got_fileindex_entry *entry)
120 /* Flag this entry until it gets written out to disk. */
121 entry->flags |= GOT_FILEIDX_F_INTENT_TO_ADD;
123 return add_entry(fileindex, entry);
126 void
127 got_fileindex_entry_remove(struct got_fileindex *fileindex,
128 struct got_fileindex_entry *entry)
130 RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
131 fileindex->nentries--;
134 struct got_fileindex_entry *
135 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
137 struct got_fileindex_entry key;
138 memset(&key, 0, sizeof(key));
139 key.path = (char *)path;
140 return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
143 const struct got_error *
144 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
145 got_fileindex_cb cb, void *cb_arg)
147 const struct got_error *err;
148 struct got_fileindex_entry *entry, *tmp;
150 RB_FOREACH_SAFE(entry, got_fileindex_tree, &fileindex->entries, tmp) {
151 err = (*cb)(cb_arg, entry);
152 if (err)
153 return err;
155 return NULL;
158 struct got_fileindex *
159 got_fileindex_alloc(void)
161 struct got_fileindex *fileindex;
163 fileindex = calloc(1, sizeof(*fileindex));
164 if (fileindex == NULL)
165 return NULL;
167 RB_INIT(&fileindex->entries);
168 return fileindex;
171 void
172 got_fileindex_free(struct got_fileindex *fileindex)
174 struct got_fileindex_entry *entry;
176 while ((entry = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
177 RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
178 got_fileindex_entry_free(entry);
180 free(fileindex);
183 static const struct got_error *
184 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
186 size_t n;
188 val = htobe64(val);
189 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
190 n = fwrite(&val, 1, sizeof(val), outfile);
191 if (n != sizeof(val))
192 return got_ferror(outfile, GOT_ERR_IO);
193 return NULL;
196 static const struct got_error *
197 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
199 size_t n;
201 val = htobe32(val);
202 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
203 n = fwrite(&val, 1, sizeof(val), outfile);
204 if (n != sizeof(val))
205 return got_ferror(outfile, GOT_ERR_IO);
206 return NULL;
209 static const struct got_error *
210 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
212 size_t n;
214 val = htobe16(val);
215 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
216 n = fwrite(&val, 1, sizeof(val), outfile);
217 if (n != sizeof(val))
218 return got_ferror(outfile, GOT_ERR_IO);
219 return NULL;
222 static const struct got_error *
223 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
225 size_t n, len, pad = 0;
226 static const uint8_t zero[8] = { 0 };
228 len = strlen(path);
229 while ((len + pad) % 8 != 0)
230 pad++;
231 if (pad == 0)
232 pad = 8; /* NUL-terminate */
234 SHA1Update(ctx, path, len);
235 n = fwrite(path, 1, len, outfile);
236 if (n != len)
237 return got_ferror(outfile, GOT_ERR_IO);
238 SHA1Update(ctx, zero, pad);
239 n = fwrite(zero, 1, pad, outfile);
240 if (n != pad)
241 return got_ferror(outfile, GOT_ERR_IO);
242 return NULL;
245 static const struct got_error *
246 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
247 FILE *outfile)
249 const struct got_error *err;
250 size_t n;
252 err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
253 if (err)
254 return err;
255 err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
256 if (err)
257 return err;
258 err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
259 if (err)
260 return err;
261 err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
262 if (err)
263 return err;
265 err = write_fileindex_val32(ctx, entry->uid, outfile);
266 if (err)
267 return err;
268 err = write_fileindex_val32(ctx, entry->gid, outfile);
269 if (err)
270 return err;
271 err = write_fileindex_val32(ctx, entry->size, outfile);
272 if (err)
273 return err;
275 err = write_fileindex_val16(ctx, entry->mode, outfile);
276 if (err)
277 return err;
279 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
280 n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
281 if (n != SHA1_DIGEST_LENGTH)
282 return got_ferror(outfile, GOT_ERR_IO);
284 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
285 n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
286 if (n != SHA1_DIGEST_LENGTH)
287 return got_ferror(outfile, GOT_ERR_IO);
289 err = write_fileindex_val32(ctx, entry->flags, outfile);
290 if (err)
291 return err;
293 err = write_fileindex_path(ctx, entry->path, outfile);
294 return err;
297 const struct got_error *
298 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
300 const struct got_error *err = NULL;
301 struct got_fileindex_hdr hdr;
302 SHA1_CTX ctx;
303 uint8_t sha1[SHA1_DIGEST_LENGTH];
304 size_t n;
305 struct got_fileindex_entry *entry;
307 SHA1Init(&ctx);
309 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
310 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
311 hdr.nentries = htobe32(fileindex->nentries);
313 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
314 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
315 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
316 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
317 if (n != sizeof(hdr.signature))
318 return got_ferror(outfile, GOT_ERR_IO);
319 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
320 if (n != sizeof(hdr.version))
321 return got_ferror(outfile, GOT_ERR_IO);
322 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
323 if (n != sizeof(hdr.nentries))
324 return got_ferror(outfile, GOT_ERR_IO);
326 RB_FOREACH(entry, got_fileindex_tree, &fileindex->entries) {
327 entry->flags &= ~GOT_FILEIDX_F_INTENT_TO_ADD;
328 err = write_fileindex_entry(&ctx, entry, outfile);
329 if (err)
330 return err;
333 SHA1Final(sha1, &ctx);
334 n = fwrite(sha1, 1, sizeof(sha1), outfile);
335 if (n != sizeof(sha1))
336 return got_ferror(outfile, GOT_ERR_IO);
338 if (fflush(outfile) != 0)
339 return got_error_from_errno();
341 return NULL;
344 static const struct got_error *
345 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
347 size_t n;
349 n = fread(val, 1, sizeof(*val), infile);
350 if (n != sizeof(*val))
351 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
352 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
353 *val = be64toh(*val);
354 return NULL;
357 static const struct got_error *
358 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
360 size_t n;
362 n = fread(val, 1, sizeof(*val), infile);
363 if (n != sizeof(*val))
364 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
365 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
366 *val = be32toh(*val);
367 return NULL;
370 static const struct got_error *
371 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
373 size_t n;
375 n = fread(val, 1, sizeof(*val), infile);
376 if (n != sizeof(*val))
377 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
378 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
379 *val = be16toh(*val);
380 return NULL;
383 static const struct got_error *
384 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
386 const struct got_error *err = NULL;
387 uint8_t buf[8];
388 size_t n, len = 0, totlen = sizeof(buf);
390 *path = malloc(totlen);
391 if (*path == NULL)
392 return got_error_from_errno();
394 do {
395 n = fread(buf, 1, sizeof(buf), infile);
396 if (n != sizeof(buf))
397 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
398 if (len + sizeof(buf) > totlen) {
399 char *p = reallocarray(*path, totlen + sizeof(buf), 1);
400 if (p == NULL) {
401 err = got_error_from_errno();
402 break;
404 totlen += sizeof(buf);
405 *path = p;
407 SHA1Update(ctx, buf, sizeof(buf));
408 memcpy(*path + len, buf, sizeof(buf));
409 len += sizeof(buf);
410 } while (memchr(buf, '\0', sizeof(buf)) == NULL);
412 if (err) {
413 free(*path);
414 *path = NULL;
416 return err;
419 static const struct got_error *
420 read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
421 FILE *infile)
423 const struct got_error *err;
424 struct got_fileindex_entry *entry;
425 size_t n;
427 *entryp = NULL;
429 entry = calloc(1, sizeof(*entry));
430 if (entry == NULL)
431 return got_error_from_errno();
433 err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
434 if (err)
435 goto done;
436 err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
437 if (err)
438 goto done;
439 err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
440 if (err)
441 goto done;
442 err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
443 if (err)
444 goto done;
446 err = read_fileindex_val32(&entry->uid, ctx, infile);
447 if (err)
448 goto done;
449 err = read_fileindex_val32(&entry->gid, ctx, infile);
450 if (err)
451 goto done;
452 err = read_fileindex_val32(&entry->size, ctx, infile);
453 if (err)
454 goto done;
456 err = read_fileindex_val16(&entry->mode, ctx, infile);
457 if (err)
458 goto done;
460 n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
461 if (n != SHA1_DIGEST_LENGTH) {
462 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
463 goto done;
465 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
467 n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
468 if (n != SHA1_DIGEST_LENGTH) {
469 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
470 goto done;
472 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
474 err = read_fileindex_val32(&entry->flags, ctx, infile);
475 if (err)
476 goto done;
478 err = read_fileindex_path(&entry->path, ctx, infile);
479 done:
480 if (err)
481 free(entry);
482 else
483 *entryp = entry;
484 return err;
487 const struct got_error *
488 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
490 const struct got_error *err = NULL;
491 struct got_fileindex_hdr hdr;
492 SHA1_CTX ctx;
493 struct got_fileindex_entry *entry;
494 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
495 uint8_t sha1[SHA1_DIGEST_LENGTH];
496 size_t n;
497 int i;
499 SHA1Init(&ctx);
501 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
502 if (n != sizeof(hdr.signature)) {
503 if (n == 0) /* EOF */
504 return NULL;
505 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
507 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
508 if (n != sizeof(hdr.version)) {
509 if (n == 0) /* EOF */
510 return NULL;
511 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
513 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
514 if (n != sizeof(hdr.nentries)) {
515 if (n == 0) /* EOF */
516 return NULL;
517 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
520 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
521 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
522 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
524 hdr.signature = be32toh(hdr.signature);
525 hdr.version = be32toh(hdr.version);
526 hdr.nentries = be32toh(hdr.nentries);
528 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
529 return got_error(GOT_ERR_FILEIDX_SIG);
530 if (hdr.version != GOT_FILE_INDEX_VERSION)
531 return got_error(GOT_ERR_FILEIDX_VER);
533 for (i = 0; i < hdr.nentries; i++) {
534 err = read_fileindex_entry(&entry, &ctx, infile);
535 if (err)
536 return err;
537 err = add_entry(fileindex, entry);
538 if (err)
539 return err;
542 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
543 if (n != sizeof(sha1_expected))
544 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
545 SHA1Final(sha1, &ctx);
546 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
547 return got_error(GOT_ERR_FILEIDX_CSUM);
549 return NULL;
552 struct got_fileindex_entry *
553 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
555 struct got_fileindex_entry *next;
557 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
559 /* Skip entries which were newly added by diff callbacks. */
560 while (next && (next->flags & GOT_FILEIDX_F_INTENT_TO_ADD))
561 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
563 return next;
566 static const struct got_error *
567 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
568 struct got_tree_object *, const char *, struct got_repository *,
569 struct got_fileindex_diff_tree_cb *, void *);
571 static const struct got_error *
572 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
573 struct got_fileindex_entry **ie, struct got_tree_entry *te,
574 const char *path, struct got_repository *repo,
575 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
577 const struct got_error *err = NULL;
579 if (S_ISDIR(te->mode)) {
580 char *subpath;
581 struct got_tree_object *subtree;
583 if (asprintf(&subpath, "%s%s%s", path,
584 path[0] == '\0' ? "" : "/", te->name) == -1)
585 return got_error_from_errno();
587 err = got_object_open_as_tree(&subtree, repo, te->id);
588 if (err) {
589 free(subpath);
590 return err;
593 err = diff_fileindex_tree(fileindex, ie, subtree,
594 subpath, repo, cb, cb_arg);
595 free(subpath);
596 got_object_tree_close(subtree);
597 if (err)
598 return err;
601 *next = SIMPLEQ_NEXT(te, entry);
602 return NULL;
605 /*
606 * Decide whether a fileindex entry path is equivalent to a tree entry path,
607 * and if it is not, then decide which of the two should be processed first.
608 */
609 static int
610 cmp_entries(const char *ie_path, const char *parent_path,
611 size_t parent_len, const char *te_name)
613 int cmp = strncmp(ie_path, parent_path, parent_len);
614 if (cmp == 0) {
615 const char *ie_name = ie_path + parent_len;
616 while (ie_name[0] == '/')
617 ie_name++;
618 cmp = strcmp(ie_name, te_name);
620 return cmp;
624 static const struct got_error *
625 diff_fileindex_tree(struct got_fileindex *fileindex,
626 struct got_fileindex_entry **ie, struct got_tree_object *tree,
627 const char *path, struct got_repository *repo,
628 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
630 const struct got_error *err = NULL;
631 struct got_tree_entry *te = NULL;
632 size_t path_len = strlen(path);
633 const struct got_tree_entries *entries;
634 struct got_fileindex_entry *next;
636 entries = got_object_tree_get_entries(tree);
637 te = SIMPLEQ_FIRST(&entries->head);
638 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
639 if (te && *ie) {
640 int cmp = cmp_entries((*ie)->path, path, path_len,
641 te->name);
642 if (cmp == 0) {
643 err = cb->diff_old_new(cb_arg, *ie, te,
644 path);
645 if (err)
646 break;
647 *ie = walk_fileindex(fileindex, *ie);
648 err = walk_tree(&te, fileindex, ie, te,
649 path, repo, cb, cb_arg);
650 } else if (cmp < 0 ) {
651 next = walk_fileindex(fileindex, *ie);
652 err = cb->diff_old(cb_arg, *ie, path);
653 if (err)
654 break;
655 *ie = next;
656 } else {
657 err = cb->diff_new(cb_arg, te, path);
658 if (err)
659 break;
660 err = walk_tree(&te, fileindex, ie, te,
661 path, repo, cb, cb_arg);
663 if (err)
664 break;
665 } else if (*ie) {
666 next = walk_fileindex(fileindex, *ie);
667 err = cb->diff_old(cb_arg, *ie, path);
668 if (err)
669 break;
670 *ie = next;
671 } else if (te) {
672 err = cb->diff_new(cb_arg, te, path);
673 if (err)
674 break;
675 err = walk_tree(&te, fileindex, ie, te, path, repo, cb,
676 cb_arg);
677 if (err)
678 break;
682 return err;
685 const struct got_error *
686 got_fileindex_diff_tree(struct got_fileindex *fileindex,
687 struct got_tree_object *tree, struct got_repository *repo,
688 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
690 struct got_fileindex_entry *min;
691 min = RB_MIN(got_fileindex_tree, &fileindex->entries);
692 return diff_fileindex_tree(fileindex, &min, tree, "", repo, cb, cb_arg);
695 static const struct got_error *
696 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **, DIR *,
697 const char *, const char *, struct got_repository *,
698 struct got_fileindex_diff_dir_cb *, void *);
700 static const struct got_error *
701 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
702 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle,
703 const char *path, DIR *dir, const char *rootpath,
704 struct got_repository *repo, struct got_fileindex_diff_dir_cb *cb,
705 void *cb_arg)
707 const struct got_error *err = NULL;
708 struct dirent *de = dle->data;
710 if (de->d_type == DT_DIR) {
711 char *subpath;
712 char *subdirpath;
713 DIR *subdir;
715 if (asprintf(&subpath, "%s%s%s", path,
716 path[0] == '\0' ? "" : "/", de->d_name) == -1)
717 return got_error_from_errno();
719 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
720 free(subpath);
721 return got_error_from_errno();
724 subdir = opendir(subdirpath);
725 if (subdir == NULL) {
726 free(subpath);
727 free(subdirpath);
728 return got_error_from_errno();
731 err = diff_fileindex_dir(fileindex, ie, subdir, rootpath,
732 subpath, repo, cb, cb_arg);
733 free(subpath);
734 free(subdirpath);
735 closedir(subdir);
736 if (err)
737 return err;
740 *next = TAILQ_NEXT(dle, entry);
741 return NULL;
744 static const struct got_error *
745 diff_fileindex_dir(struct got_fileindex *fileindex,
746 struct got_fileindex_entry **ie, DIR *dir, const char *rootpath,
747 const char *path, struct got_repository *repo,
748 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
750 const struct got_error *err = NULL;
751 struct dirent *de = NULL;
752 size_t path_len = strlen(path);
753 struct got_fileindex_entry *next;
754 struct got_pathlist_head dirlist;
755 struct got_pathlist_entry *dle;
757 TAILQ_INIT(&dirlist);
759 while (1) {
760 struct got_pathlist_entry *new = NULL;
761 struct dirent *dep = NULL;
763 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
764 if (de == NULL) {
765 err = got_error_from_errno();
766 goto done;
769 if (readdir_r(dir, de, &dep) != 0) {
770 err = got_error_from_errno();
771 free(de);
772 goto done;
774 if (dep == NULL) {
775 free(de);
776 break;
779 if (strcmp(de->d_name, ".") == 0 ||
780 strcmp(de->d_name, "..") == 0 ||
781 (path[0] == '\0' &&
782 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
783 free(de);
784 continue;
787 err = got_pathlist_insert(&new, &dirlist, de->d_name, de);
788 if (err)
789 goto done;
790 if (new == NULL) {
791 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
792 goto done;
796 dle = TAILQ_FIRST(&dirlist);
797 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
798 if (dle && *ie) {
799 int cmp;
800 de = dle->data;
801 cmp = cmp_entries((*ie)->path, path, path_len,
802 de->d_name);
803 if (cmp == 0) {
804 err = cb->diff_old_new(cb_arg, *ie, de, path);
805 if (err)
806 break;
807 *ie = walk_fileindex(fileindex, *ie);
808 err = walk_dir(&dle, fileindex, ie, dle, path,
809 dir, rootpath, repo, cb, cb_arg);
810 } else if (cmp < 0 ) {
811 next = walk_fileindex(fileindex, *ie);
812 err = cb->diff_old(cb_arg, *ie, path);
813 if (err)
814 break;
815 *ie = next;
816 } else {
817 err = cb->diff_new(cb_arg, de, path);
818 if (err)
819 break;
820 err = walk_dir(&dle, fileindex, ie, dle, path,
821 dir, rootpath, repo, cb, cb_arg);
823 if (err)
824 break;
825 } else if (*ie) {
826 next = walk_fileindex(fileindex, *ie);
827 err = cb->diff_old(cb_arg, *ie, path);
828 if (err)
829 break;
830 *ie = next;
831 } else if (dle) {
832 de = dle->data;
833 err = cb->diff_new(cb_arg, de, path);
834 if (err)
835 break;
836 err = walk_dir(&dle, fileindex, ie, dle, path, dir,
837 rootpath, repo, cb, cb_arg);
838 if (err)
839 break;
842 done:
843 TAILQ_FOREACH(dle, &dirlist, entry)
844 free(dle->data);
845 got_pathlist_free(&dirlist);
846 return err;
849 const struct got_error *
850 got_fileindex_diff_dir(struct got_fileindex *fileindex, DIR *rootdir,
851 const char *rootpath, struct got_repository *repo,
852 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
854 struct got_fileindex_entry *min;
855 min = RB_MIN(got_fileindex_tree, &fileindex->entries);
856 return diff_fileindex_dir(fileindex, &min, rootdir, rootpath, "",
857 repo, cb, cb_arg);
860 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);