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
45 #define GOT_FILEIDX_F_NO_FILE_ON_DISK 0x00080000
47 struct got_fileindex {
48 struct got_fileindex_tree entries;
49 int nentries;
50 #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
51 };
53 const struct got_error *
54 got_fileindex_entry_update(struct got_fileindex_entry *entry,
55 const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
56 int update_timestamps)
57 {
58 struct stat sb;
60 if (lstat(ondisk_path, &sb) != 0)
61 return got_error_from_errno();
63 entry->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
65 if (update_timestamps) {
66 entry->ctime_sec = sb.st_ctime;
67 entry->ctime_nsec = sb.st_ctimensec;
68 entry->mtime_sec = sb.st_mtime;
69 entry->mtime_nsec = sb.st_mtimensec;
70 }
71 entry->uid = sb.st_uid;
72 entry->gid = sb.st_gid;
73 entry->size = (sb.st_size & 0xffffffff);
74 if (sb.st_mode & S_IFLNK)
75 entry->mode = GOT_FILEIDX_MODE_SYMLINK;
76 else
77 entry->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
78 entry->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
79 GOT_FILEIDX_MODE_PERMS_SHIFT);
81 if (blob_sha1) {
82 memcpy(entry->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
83 entry->flags &= ~GOT_FILEIDX_F_NO_BLOB;
84 } else
85 entry->flags |= GOT_FILEIDX_F_NO_BLOB;
87 if (commit_sha1) {
88 memcpy(entry->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
89 entry->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
90 } else
91 entry->flags |= GOT_FILEIDX_F_NO_COMMIT;
93 return NULL;
94 }
96 void
97 got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *entry)
98 {
99 entry->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
102 const struct got_error *
103 got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
104 const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
105 uint8_t *commit_sha1)
107 size_t len;
109 *entry = calloc(1, sizeof(**entry));
110 if (*entry == NULL)
111 return got_error_from_errno();
113 (*entry)->path = strdup(relpath);
114 if ((*entry)->path == NULL) {
115 const struct got_error *err = got_error_from_errno();
116 free(*entry);
117 *entry = NULL;
118 return err;
121 len = strlen(relpath);
122 if (len > GOT_FILEIDX_F_PATH_LEN)
123 len = GOT_FILEIDX_F_PATH_LEN;
124 (*entry)->flags |= len;
126 return got_fileindex_entry_update(*entry, ondisk_path, blob_sha1,
127 commit_sha1, 1);
130 void
131 got_fileindex_entry_free(struct got_fileindex_entry *entry)
133 free(entry->path);
134 free(entry);
137 int
138 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
140 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
143 int
144 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
146 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
149 int
150 got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
152 return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
155 static const struct got_error *
156 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *entry)
158 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
159 return got_error(GOT_ERR_NO_SPACE);
161 RB_INSERT(got_fileindex_tree, &fileindex->entries, entry);
162 fileindex->nentries++;
163 return NULL;
166 const struct got_error *
167 got_fileindex_entry_add(struct got_fileindex *fileindex,
168 struct got_fileindex_entry *entry)
170 /* Flag this entry until it gets written out to disk. */
171 entry->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
173 return add_entry(fileindex, entry);
176 void
177 got_fileindex_entry_remove(struct got_fileindex *fileindex,
178 struct got_fileindex_entry *entry)
180 RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
181 fileindex->nentries--;
184 struct got_fileindex_entry *
185 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
187 struct got_fileindex_entry key;
188 memset(&key, 0, sizeof(key));
189 key.path = (char *)path;
190 return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
193 const struct got_error *
194 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
195 got_fileindex_cb cb, void *cb_arg)
197 const struct got_error *err;
198 struct got_fileindex_entry *entry, *tmp;
200 RB_FOREACH_SAFE(entry, got_fileindex_tree, &fileindex->entries, tmp) {
201 err = (*cb)(cb_arg, entry);
202 if (err)
203 return err;
205 return NULL;
208 struct got_fileindex *
209 got_fileindex_alloc(void)
211 struct got_fileindex *fileindex;
213 fileindex = calloc(1, sizeof(*fileindex));
214 if (fileindex == NULL)
215 return NULL;
217 RB_INIT(&fileindex->entries);
218 return fileindex;
221 void
222 got_fileindex_free(struct got_fileindex *fileindex)
224 struct got_fileindex_entry *entry;
226 while ((entry = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
227 RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
228 got_fileindex_entry_free(entry);
230 free(fileindex);
233 static const struct got_error *
234 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
236 size_t n;
238 val = htobe64(val);
239 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
240 n = fwrite(&val, 1, sizeof(val), outfile);
241 if (n != sizeof(val))
242 return got_ferror(outfile, GOT_ERR_IO);
243 return NULL;
246 static const struct got_error *
247 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
249 size_t n;
251 val = htobe32(val);
252 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
253 n = fwrite(&val, 1, sizeof(val), outfile);
254 if (n != sizeof(val))
255 return got_ferror(outfile, GOT_ERR_IO);
256 return NULL;
259 static const struct got_error *
260 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
262 size_t n;
264 val = htobe16(val);
265 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
266 n = fwrite(&val, 1, sizeof(val), outfile);
267 if (n != sizeof(val))
268 return got_ferror(outfile, GOT_ERR_IO);
269 return NULL;
272 static const struct got_error *
273 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
275 size_t n, len, pad = 0;
276 static const uint8_t zero[8] = { 0 };
278 len = strlen(path);
279 while ((len + pad) % 8 != 0)
280 pad++;
281 if (pad == 0)
282 pad = 8; /* NUL-terminate */
284 SHA1Update(ctx, path, len);
285 n = fwrite(path, 1, len, outfile);
286 if (n != len)
287 return got_ferror(outfile, GOT_ERR_IO);
288 SHA1Update(ctx, zero, pad);
289 n = fwrite(zero, 1, pad, outfile);
290 if (n != pad)
291 return got_ferror(outfile, GOT_ERR_IO);
292 return NULL;
295 static const struct got_error *
296 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
297 FILE *outfile)
299 const struct got_error *err;
300 size_t n;
302 err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
303 if (err)
304 return err;
305 err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
306 if (err)
307 return err;
308 err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
309 if (err)
310 return err;
311 err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
312 if (err)
313 return err;
315 err = write_fileindex_val32(ctx, entry->uid, outfile);
316 if (err)
317 return err;
318 err = write_fileindex_val32(ctx, entry->gid, outfile);
319 if (err)
320 return err;
321 err = write_fileindex_val32(ctx, entry->size, outfile);
322 if (err)
323 return err;
325 err = write_fileindex_val16(ctx, entry->mode, outfile);
326 if (err)
327 return err;
329 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
330 n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
331 if (n != SHA1_DIGEST_LENGTH)
332 return got_ferror(outfile, GOT_ERR_IO);
334 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
335 n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
336 if (n != SHA1_DIGEST_LENGTH)
337 return got_ferror(outfile, GOT_ERR_IO);
339 err = write_fileindex_val32(ctx, entry->flags, outfile);
340 if (err)
341 return err;
343 err = write_fileindex_path(ctx, entry->path, outfile);
344 return err;
347 const struct got_error *
348 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
350 const struct got_error *err = NULL;
351 struct got_fileindex_hdr hdr;
352 SHA1_CTX ctx;
353 uint8_t sha1[SHA1_DIGEST_LENGTH];
354 size_t n;
355 struct got_fileindex_entry *entry;
357 SHA1Init(&ctx);
359 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
360 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
361 hdr.nentries = htobe32(fileindex->nentries);
363 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
364 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
365 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
366 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
367 if (n != sizeof(hdr.signature))
368 return got_ferror(outfile, GOT_ERR_IO);
369 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
370 if (n != sizeof(hdr.version))
371 return got_ferror(outfile, GOT_ERR_IO);
372 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
373 if (n != sizeof(hdr.nentries))
374 return got_ferror(outfile, GOT_ERR_IO);
376 RB_FOREACH(entry, got_fileindex_tree, &fileindex->entries) {
377 entry->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
378 err = write_fileindex_entry(&ctx, entry, outfile);
379 if (err)
380 return err;
383 SHA1Final(sha1, &ctx);
384 n = fwrite(sha1, 1, sizeof(sha1), outfile);
385 if (n != sizeof(sha1))
386 return got_ferror(outfile, GOT_ERR_IO);
388 if (fflush(outfile) != 0)
389 return got_error_from_errno();
391 return NULL;
394 static const struct got_error *
395 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
397 size_t n;
399 n = fread(val, 1, sizeof(*val), infile);
400 if (n != sizeof(*val))
401 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
402 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
403 *val = be64toh(*val);
404 return NULL;
407 static const struct got_error *
408 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
410 size_t n;
412 n = fread(val, 1, sizeof(*val), infile);
413 if (n != sizeof(*val))
414 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
415 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
416 *val = be32toh(*val);
417 return NULL;
420 static const struct got_error *
421 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
423 size_t n;
425 n = fread(val, 1, sizeof(*val), infile);
426 if (n != sizeof(*val))
427 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
428 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
429 *val = be16toh(*val);
430 return NULL;
433 static const struct got_error *
434 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
436 const struct got_error *err = NULL;
437 uint8_t buf[8];
438 size_t n, len = 0, totlen = sizeof(buf);
440 *path = malloc(totlen);
441 if (*path == NULL)
442 return got_error_from_errno();
444 do {
445 n = fread(buf, 1, sizeof(buf), infile);
446 if (n != sizeof(buf))
447 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
448 if (len + sizeof(buf) > totlen) {
449 char *p = reallocarray(*path, totlen + sizeof(buf), 1);
450 if (p == NULL) {
451 err = got_error_from_errno();
452 break;
454 totlen += sizeof(buf);
455 *path = p;
457 SHA1Update(ctx, buf, sizeof(buf));
458 memcpy(*path + len, buf, sizeof(buf));
459 len += sizeof(buf);
460 } while (memchr(buf, '\0', sizeof(buf)) == NULL);
462 if (err) {
463 free(*path);
464 *path = NULL;
466 return err;
469 static const struct got_error *
470 read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
471 FILE *infile)
473 const struct got_error *err;
474 struct got_fileindex_entry *entry;
475 size_t n;
477 *entryp = NULL;
479 entry = calloc(1, sizeof(*entry));
480 if (entry == NULL)
481 return got_error_from_errno();
483 err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
484 if (err)
485 goto done;
486 err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
487 if (err)
488 goto done;
489 err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
490 if (err)
491 goto done;
492 err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
493 if (err)
494 goto done;
496 err = read_fileindex_val32(&entry->uid, ctx, infile);
497 if (err)
498 goto done;
499 err = read_fileindex_val32(&entry->gid, ctx, infile);
500 if (err)
501 goto done;
502 err = read_fileindex_val32(&entry->size, ctx, infile);
503 if (err)
504 goto done;
506 err = read_fileindex_val16(&entry->mode, ctx, infile);
507 if (err)
508 goto done;
510 n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
511 if (n != SHA1_DIGEST_LENGTH) {
512 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
513 goto done;
515 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
517 n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
518 if (n != SHA1_DIGEST_LENGTH) {
519 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
520 goto done;
522 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
524 err = read_fileindex_val32(&entry->flags, ctx, infile);
525 if (err)
526 goto done;
528 err = read_fileindex_path(&entry->path, ctx, infile);
529 done:
530 if (err)
531 free(entry);
532 else
533 *entryp = entry;
534 return err;
537 const struct got_error *
538 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
540 const struct got_error *err = NULL;
541 struct got_fileindex_hdr hdr;
542 SHA1_CTX ctx;
543 struct got_fileindex_entry *entry;
544 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
545 uint8_t sha1[SHA1_DIGEST_LENGTH];
546 size_t n;
547 int i;
549 SHA1Init(&ctx);
551 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
552 if (n != sizeof(hdr.signature)) {
553 if (n == 0) /* EOF */
554 return NULL;
555 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
557 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
558 if (n != sizeof(hdr.version)) {
559 if (n == 0) /* EOF */
560 return NULL;
561 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
563 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
564 if (n != sizeof(hdr.nentries)) {
565 if (n == 0) /* EOF */
566 return NULL;
567 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
570 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
571 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
572 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
574 hdr.signature = be32toh(hdr.signature);
575 hdr.version = be32toh(hdr.version);
576 hdr.nentries = be32toh(hdr.nentries);
578 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
579 return got_error(GOT_ERR_FILEIDX_SIG);
580 if (hdr.version != GOT_FILE_INDEX_VERSION)
581 return got_error(GOT_ERR_FILEIDX_VER);
583 for (i = 0; i < hdr.nentries; i++) {
584 err = read_fileindex_entry(&entry, &ctx, infile);
585 if (err)
586 return err;
587 err = add_entry(fileindex, entry);
588 if (err)
589 return err;
592 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
593 if (n != sizeof(sha1_expected))
594 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
595 SHA1Final(sha1, &ctx);
596 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
597 return got_error(GOT_ERR_FILEIDX_CSUM);
599 return NULL;
602 static struct got_fileindex_entry *
603 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
605 struct got_fileindex_entry *next;
607 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
609 /* Skip entries which were newly added by diff callbacks. */
610 while (next && (next->flags & GOT_FILEIDX_F_NOT_FLUSHED))
611 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
613 return next;
616 static const struct got_error *
617 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
618 struct got_tree_object *, const char *, struct got_repository *,
619 struct got_fileindex_diff_tree_cb *, void *);
621 static const struct got_error *
622 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
623 struct got_fileindex_entry **ie, struct got_tree_entry *te,
624 const char *path, struct got_repository *repo,
625 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
627 const struct got_error *err = NULL;
629 if (S_ISDIR(te->mode)) {
630 char *subpath;
631 struct got_tree_object *subtree;
633 if (asprintf(&subpath, "%s%s%s", path,
634 path[0] == '\0' ? "" : "/", te->name) == -1)
635 return got_error_from_errno();
637 err = got_object_open_as_tree(&subtree, repo, te->id);
638 if (err) {
639 free(subpath);
640 return err;
643 err = diff_fileindex_tree(fileindex, ie, subtree,
644 subpath, repo, cb, cb_arg);
645 free(subpath);
646 got_object_tree_close(subtree);
647 if (err)
648 return err;
651 *next = SIMPLEQ_NEXT(te, entry);
652 return NULL;
655 static const struct got_error *
656 diff_fileindex_tree(struct got_fileindex *fileindex,
657 struct got_fileindex_entry **ie, struct got_tree_object *tree,
658 const char *path, struct got_repository *repo,
659 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
661 const struct got_error *err = NULL;
662 struct got_tree_entry *te = NULL;
663 size_t path_len = strlen(path);
664 const struct got_tree_entries *entries;
665 struct got_fileindex_entry *next;
667 entries = got_object_tree_get_entries(tree);
668 te = SIMPLEQ_FIRST(&entries->head);
669 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
670 if (te && *ie) {
671 char *te_path;
672 int cmp;
673 if (asprintf(&te_path, "%s/%s", path, te->name) == -1) {
674 err = got_error_from_errno();
675 break;
677 cmp = got_path_cmp((*ie)->path, te_path);
678 free(te_path);
679 if (cmp == 0) {
680 err = cb->diff_old_new(cb_arg, *ie, te,
681 path);
682 if (err)
683 break;
684 *ie = walk_fileindex(fileindex, *ie);
685 err = walk_tree(&te, fileindex, ie, te,
686 path, repo, cb, cb_arg);
687 } else if (cmp < 0 ) {
688 next = walk_fileindex(fileindex, *ie);
689 err = cb->diff_old(cb_arg, *ie, path);
690 if (err)
691 break;
692 *ie = next;
693 } else {
694 err = cb->diff_new(cb_arg, te, path);
695 if (err)
696 break;
697 err = walk_tree(&te, fileindex, ie, te,
698 path, repo, cb, cb_arg);
700 if (err)
701 break;
702 } else if (*ie) {
703 next = walk_fileindex(fileindex, *ie);
704 err = cb->diff_old(cb_arg, *ie, path);
705 if (err)
706 break;
707 *ie = next;
708 } else if (te) {
709 err = cb->diff_new(cb_arg, te, path);
710 if (err)
711 break;
712 err = walk_tree(&te, fileindex, ie, te, path, repo, cb,
713 cb_arg);
714 if (err)
715 break;
719 return err;
722 const struct got_error *
723 got_fileindex_diff_tree(struct got_fileindex *fileindex,
724 struct got_tree_object *tree, struct got_repository *repo,
725 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
727 struct got_fileindex_entry *min;
728 min = RB_MIN(got_fileindex_tree, &fileindex->entries);
729 return diff_fileindex_tree(fileindex, &min, tree, "", repo, cb, cb_arg);
732 static const struct got_error *
733 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **, DIR *,
734 const char *, const char *, struct got_repository *,
735 struct got_fileindex_diff_dir_cb *, void *);
737 static const struct got_error *
738 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
739 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle,
740 const char *path, DIR *dir, const char *rootpath,
741 struct got_repository *repo, struct got_fileindex_diff_dir_cb *cb,
742 void *cb_arg)
744 const struct got_error *err = NULL;
745 struct dirent *de = dle->data;
747 if (de->d_type == DT_DIR) {
748 char *subpath;
749 char *subdirpath;
750 DIR *subdir;
752 if (asprintf(&subpath, "%s%s%s", path,
753 path[0] == '\0' ? "" : "/", de->d_name) == -1)
754 return got_error_from_errno();
756 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
757 free(subpath);
758 return got_error_from_errno();
761 subdir = opendir(subdirpath);
762 if (subdir == NULL) {
763 free(subpath);
764 free(subdirpath);
765 return got_error_from_errno();
768 err = diff_fileindex_dir(fileindex, ie, subdir, rootpath,
769 subpath, repo, cb, cb_arg);
770 free(subpath);
771 free(subdirpath);
772 closedir(subdir);
773 if (err)
774 return err;
777 *next = TAILQ_NEXT(dle, entry);
778 return NULL;
781 static const struct got_error *
782 diff_fileindex_dir(struct got_fileindex *fileindex,
783 struct got_fileindex_entry **ie, DIR *dir, const char *rootpath,
784 const char *path, struct got_repository *repo,
785 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
787 const struct got_error *err = NULL;
788 struct dirent *de = NULL;
789 size_t path_len = strlen(path);
790 struct got_fileindex_entry *next;
791 struct got_pathlist_head dirlist;
792 struct got_pathlist_entry *dle;
794 TAILQ_INIT(&dirlist);
796 while (1) {
797 struct got_pathlist_entry *new = NULL;
798 struct dirent *dep = NULL;
800 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
801 if (de == NULL) {
802 err = got_error_from_errno();
803 goto done;
806 if (readdir_r(dir, de, &dep) != 0) {
807 err = got_error_from_errno();
808 free(de);
809 goto done;
811 if (dep == NULL) {
812 free(de);
813 break;
816 if (strcmp(de->d_name, ".") == 0 ||
817 strcmp(de->d_name, "..") == 0 ||
818 (path[0] == '\0' &&
819 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
820 free(de);
821 continue;
824 err = got_pathlist_insert(&new, &dirlist, de->d_name, de);
825 if (err)
826 goto done;
827 if (new == NULL) {
828 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
829 goto done;
833 dle = TAILQ_FIRST(&dirlist);
834 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
835 if (dle && *ie) {
836 char *de_path;
837 int cmp;
838 de = dle->data;
839 if (asprintf(&de_path, "%s/%s", path,
840 de->d_name) == -1) {
841 err = got_error_from_errno();
842 break;
844 cmp = got_path_cmp((*ie)->path, de_path);
845 free(de_path);
846 if (cmp == 0) {
847 err = cb->diff_old_new(cb_arg, *ie, de, path);
848 if (err)
849 break;
850 *ie = walk_fileindex(fileindex, *ie);
851 err = walk_dir(&dle, fileindex, ie, dle, path,
852 dir, rootpath, repo, cb, cb_arg);
853 } else if (cmp < 0 ) {
854 next = walk_fileindex(fileindex, *ie);
855 err = cb->diff_old(cb_arg, *ie, path);
856 if (err)
857 break;
858 *ie = next;
859 } else {
860 err = cb->diff_new(cb_arg, de, path);
861 if (err)
862 break;
863 err = walk_dir(&dle, fileindex, ie, dle, path,
864 dir, rootpath, repo, cb, cb_arg);
866 if (err)
867 break;
868 } else if (*ie) {
869 next = walk_fileindex(fileindex, *ie);
870 err = cb->diff_old(cb_arg, *ie, path);
871 if (err)
872 break;
873 *ie = next;
874 } else if (dle) {
875 de = dle->data;
876 err = cb->diff_new(cb_arg, de, path);
877 if (err)
878 break;
879 err = walk_dir(&dle, fileindex, ie, dle, path, dir,
880 rootpath, repo, cb, cb_arg);
881 if (err)
882 break;
885 done:
886 TAILQ_FOREACH(dle, &dirlist, entry)
887 free(dle->data);
888 got_pathlist_free(&dirlist);
889 return err;
892 const struct got_error *
893 got_fileindex_diff_dir(struct got_fileindex *fileindex, DIR *rootdir,
894 const char *rootpath, const char *path, struct got_repository *repo,
895 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
897 struct got_fileindex_entry *min;
898 min = RB_MIN(got_fileindex_tree, &fileindex->entries);
899 return diff_fileindex_dir(fileindex, &min, rootdir, rootpath, path,
900 repo, cb, cb_arg);
903 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);