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 <errno.h>
22 #include <dirent.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sha1.h>
27 #include <endian.h>
28 #include <limits.h>
29 #include <uuid.h>
31 #include "got_error.h"
32 #include "got_object.h"
33 #include "got_path.h"
35 #include "got_lib_fileindex.h"
36 #include "got_lib_worktree.h"
38 /* got_fileindex_entry flags */
39 #define GOT_FILEIDX_F_PATH_LEN 0x00000fff
40 #define GOT_FILEIDX_F_STAGE 0x00003000
41 #define GOT_FILEIDX_F_EXTENDED 0x00004000
42 #define GOT_FILEIDX_F_ASSUME_VALID 0x00008000
43 #define GOT_FILEIDX_F_NOT_FLUSHED 0x00010000
44 #define GOT_FILEIDX_F_NO_BLOB 0x00020000
45 #define GOT_FILEIDX_F_NO_COMMIT 0x00040000
46 #define GOT_FILEIDX_F_NO_FILE_ON_DISK 0x00080000
48 struct got_fileindex {
49 struct got_fileindex_tree entries;
50 int nentries;
51 #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
52 };
54 uint16_t
55 got_fileindex_perms_from_st(struct stat *sb)
56 {
57 uint16_t perms = (sb->st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
58 return (perms << GOT_FILEIDX_MODE_PERMS_SHIFT);
59 }
61 mode_t
62 got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
63 {
64 mode_t perms = (ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT);
65 return (perms & (S_IRWXU | S_IRWXG | S_IRWXO));
66 }
68 const struct got_error *
69 got_fileindex_entry_update(struct got_fileindex_entry *entry,
70 const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
71 int update_timestamps)
72 {
73 struct stat sb;
75 if (lstat(ondisk_path, &sb) != 0) {
76 if ((entry->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0)
77 return got_error_from_errno2("lstat", ondisk_path);
78 } else {
79 if (sb.st_mode & S_IFDIR)
80 return got_error_set_errno(EISDIR, ondisk_path);
81 entry->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
82 }
85 if ((entry->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
86 if (update_timestamps) {
87 entry->ctime_sec = sb.st_ctime;
88 entry->ctime_nsec = sb.st_ctimensec;
89 entry->mtime_sec = sb.st_mtime;
90 entry->mtime_nsec = sb.st_mtimensec;
91 }
92 entry->uid = sb.st_uid;
93 entry->gid = sb.st_gid;
94 entry->size = (sb.st_size & 0xffffffff);
95 if (sb.st_mode & S_IFLNK)
96 entry->mode = GOT_FILEIDX_MODE_SYMLINK;
97 else
98 entry->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
99 entry->mode |= got_fileindex_perms_from_st(&sb);
102 if (blob_sha1) {
103 memcpy(entry->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
104 entry->flags &= ~GOT_FILEIDX_F_NO_BLOB;
105 } else
106 entry->flags |= GOT_FILEIDX_F_NO_BLOB;
108 if (commit_sha1) {
109 memcpy(entry->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
110 entry->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
111 } else
112 entry->flags |= GOT_FILEIDX_F_NO_COMMIT;
114 return NULL;
117 void
118 got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *entry)
120 entry->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
123 const struct got_error *
124 got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
125 const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
126 uint8_t *commit_sha1)
128 size_t len;
130 *entry = calloc(1, sizeof(**entry));
131 if (*entry == NULL)
132 return got_error_from_errno("calloc");
134 (*entry)->path = strdup(relpath);
135 if ((*entry)->path == NULL) {
136 const struct got_error *err = got_error_from_errno("strdup");
137 free(*entry);
138 *entry = NULL;
139 return err;
142 len = strlen(relpath);
143 if (len > GOT_FILEIDX_F_PATH_LEN)
144 len = GOT_FILEIDX_F_PATH_LEN;
145 (*entry)->flags |= len;
147 return got_fileindex_entry_update(*entry, ondisk_path, blob_sha1,
148 commit_sha1, 1);
151 void
152 got_fileindex_entry_free(struct got_fileindex_entry *entry)
154 free(entry->path);
155 free(entry);
158 int
159 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
161 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
164 int
165 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
167 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
170 int
171 got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
173 return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
176 static const struct got_error *
177 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *entry)
179 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
180 return got_error(GOT_ERR_NO_SPACE);
182 RB_INSERT(got_fileindex_tree, &fileindex->entries, entry);
183 fileindex->nentries++;
184 return NULL;
187 const struct got_error *
188 got_fileindex_entry_add(struct got_fileindex *fileindex,
189 struct got_fileindex_entry *entry)
191 /* Flag this entry until it gets written out to disk. */
192 entry->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
194 return add_entry(fileindex, entry);
197 void
198 got_fileindex_entry_remove(struct got_fileindex *fileindex,
199 struct got_fileindex_entry *entry)
201 RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
202 fileindex->nentries--;
205 struct got_fileindex_entry *
206 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
208 struct got_fileindex_entry key;
209 memset(&key, 0, sizeof(key));
210 key.path = (char *)path;
211 return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
214 const struct got_error *
215 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
216 got_fileindex_cb cb, void *cb_arg)
218 const struct got_error *err;
219 struct got_fileindex_entry *entry, *tmp;
221 RB_FOREACH_SAFE(entry, got_fileindex_tree, &fileindex->entries, tmp) {
222 err = (*cb)(cb_arg, entry);
223 if (err)
224 return err;
226 return NULL;
229 struct got_fileindex *
230 got_fileindex_alloc(void)
232 struct got_fileindex *fileindex;
234 fileindex = calloc(1, sizeof(*fileindex));
235 if (fileindex == NULL)
236 return NULL;
238 RB_INIT(&fileindex->entries);
239 return fileindex;
242 void
243 got_fileindex_free(struct got_fileindex *fileindex)
245 struct got_fileindex_entry *entry;
247 while ((entry = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
248 RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
249 got_fileindex_entry_free(entry);
251 free(fileindex);
254 static const struct got_error *
255 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
257 size_t n;
259 val = htobe64(val);
260 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
261 n = fwrite(&val, 1, sizeof(val), outfile);
262 if (n != sizeof(val))
263 return got_ferror(outfile, GOT_ERR_IO);
264 return NULL;
267 static const struct got_error *
268 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
270 size_t n;
272 val = htobe32(val);
273 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
274 n = fwrite(&val, 1, sizeof(val), outfile);
275 if (n != sizeof(val))
276 return got_ferror(outfile, GOT_ERR_IO);
277 return NULL;
280 static const struct got_error *
281 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
283 size_t n;
285 val = htobe16(val);
286 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
287 n = fwrite(&val, 1, sizeof(val), outfile);
288 if (n != sizeof(val))
289 return got_ferror(outfile, GOT_ERR_IO);
290 return NULL;
293 static const struct got_error *
294 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
296 size_t n, len, pad = 0;
297 static const uint8_t zero[8] = { 0 };
299 len = strlen(path);
300 while ((len + pad) % 8 != 0)
301 pad++;
302 if (pad == 0)
303 pad = 8; /* NUL-terminate */
305 SHA1Update(ctx, path, len);
306 n = fwrite(path, 1, len, outfile);
307 if (n != len)
308 return got_ferror(outfile, GOT_ERR_IO);
309 SHA1Update(ctx, zero, pad);
310 n = fwrite(zero, 1, pad, outfile);
311 if (n != pad)
312 return got_ferror(outfile, GOT_ERR_IO);
313 return NULL;
316 static const struct got_error *
317 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
318 FILE *outfile)
320 const struct got_error *err;
321 size_t n;
323 err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
324 if (err)
325 return err;
326 err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
327 if (err)
328 return err;
329 err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
330 if (err)
331 return err;
332 err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
333 if (err)
334 return err;
336 err = write_fileindex_val32(ctx, entry->uid, outfile);
337 if (err)
338 return err;
339 err = write_fileindex_val32(ctx, entry->gid, outfile);
340 if (err)
341 return err;
342 err = write_fileindex_val32(ctx, entry->size, outfile);
343 if (err)
344 return err;
346 err = write_fileindex_val16(ctx, entry->mode, outfile);
347 if (err)
348 return err;
350 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
351 n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
352 if (n != SHA1_DIGEST_LENGTH)
353 return got_ferror(outfile, GOT_ERR_IO);
355 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
356 n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
357 if (n != SHA1_DIGEST_LENGTH)
358 return got_ferror(outfile, GOT_ERR_IO);
360 err = write_fileindex_val32(ctx, entry->flags, outfile);
361 if (err)
362 return err;
364 err = write_fileindex_path(ctx, entry->path, outfile);
365 return err;
368 const struct got_error *
369 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
371 const struct got_error *err = NULL;
372 struct got_fileindex_hdr hdr;
373 SHA1_CTX ctx;
374 uint8_t sha1[SHA1_DIGEST_LENGTH];
375 size_t n;
376 struct got_fileindex_entry *entry;
378 SHA1Init(&ctx);
380 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
381 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
382 hdr.nentries = htobe32(fileindex->nentries);
384 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
385 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
386 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
387 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
388 if (n != sizeof(hdr.signature))
389 return got_ferror(outfile, GOT_ERR_IO);
390 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
391 if (n != sizeof(hdr.version))
392 return got_ferror(outfile, GOT_ERR_IO);
393 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
394 if (n != sizeof(hdr.nentries))
395 return got_ferror(outfile, GOT_ERR_IO);
397 RB_FOREACH(entry, got_fileindex_tree, &fileindex->entries) {
398 entry->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
399 err = write_fileindex_entry(&ctx, entry, outfile);
400 if (err)
401 return err;
404 SHA1Final(sha1, &ctx);
405 n = fwrite(sha1, 1, sizeof(sha1), outfile);
406 if (n != sizeof(sha1))
407 return got_ferror(outfile, GOT_ERR_IO);
409 if (fflush(outfile) != 0)
410 return got_error_from_errno("fflush");
412 return NULL;
415 static const struct got_error *
416 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
418 size_t n;
420 n = fread(val, 1, sizeof(*val), infile);
421 if (n != sizeof(*val))
422 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
423 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
424 *val = be64toh(*val);
425 return NULL;
428 static const struct got_error *
429 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
431 size_t n;
433 n = fread(val, 1, sizeof(*val), infile);
434 if (n != sizeof(*val))
435 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
436 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
437 *val = be32toh(*val);
438 return NULL;
441 static const struct got_error *
442 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
444 size_t n;
446 n = fread(val, 1, sizeof(*val), infile);
447 if (n != sizeof(*val))
448 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
449 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
450 *val = be16toh(*val);
451 return NULL;
454 static const struct got_error *
455 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
457 const struct got_error *err = NULL;
458 uint8_t buf[8];
459 size_t n, len = 0, totlen = sizeof(buf);
461 *path = malloc(totlen);
462 if (*path == NULL)
463 return got_error_from_errno("malloc");
465 do {
466 n = fread(buf, 1, sizeof(buf), infile);
467 if (n != sizeof(buf))
468 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
469 if (len + sizeof(buf) > totlen) {
470 char *p = reallocarray(*path, totlen + sizeof(buf), 1);
471 if (p == NULL) {
472 err = got_error_from_errno("reallocarray");
473 break;
475 totlen += sizeof(buf);
476 *path = p;
478 SHA1Update(ctx, buf, sizeof(buf));
479 memcpy(*path + len, buf, sizeof(buf));
480 len += sizeof(buf);
481 } while (memchr(buf, '\0', sizeof(buf)) == NULL);
483 if (err) {
484 free(*path);
485 *path = NULL;
487 return err;
490 static const struct got_error *
491 read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
492 FILE *infile)
494 const struct got_error *err;
495 struct got_fileindex_entry *entry;
496 size_t n;
498 *entryp = NULL;
500 entry = calloc(1, sizeof(*entry));
501 if (entry == NULL)
502 return got_error_from_errno("calloc");
504 err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
505 if (err)
506 goto done;
507 err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
508 if (err)
509 goto done;
510 err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
511 if (err)
512 goto done;
513 err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
514 if (err)
515 goto done;
517 err = read_fileindex_val32(&entry->uid, ctx, infile);
518 if (err)
519 goto done;
520 err = read_fileindex_val32(&entry->gid, ctx, infile);
521 if (err)
522 goto done;
523 err = read_fileindex_val32(&entry->size, ctx, infile);
524 if (err)
525 goto done;
527 err = read_fileindex_val16(&entry->mode, ctx, infile);
528 if (err)
529 goto done;
531 n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
532 if (n != SHA1_DIGEST_LENGTH) {
533 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
534 goto done;
536 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
538 n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
539 if (n != SHA1_DIGEST_LENGTH) {
540 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
541 goto done;
543 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
545 err = read_fileindex_val32(&entry->flags, ctx, infile);
546 if (err)
547 goto done;
549 err = read_fileindex_path(&entry->path, ctx, infile);
550 done:
551 if (err)
552 free(entry);
553 else
554 *entryp = entry;
555 return err;
558 const struct got_error *
559 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
561 const struct got_error *err = NULL;
562 struct got_fileindex_hdr hdr;
563 SHA1_CTX ctx;
564 struct got_fileindex_entry *entry;
565 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
566 uint8_t sha1[SHA1_DIGEST_LENGTH];
567 size_t n;
568 int i;
570 SHA1Init(&ctx);
572 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
573 if (n != sizeof(hdr.signature)) {
574 if (n == 0) /* EOF */
575 return NULL;
576 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
578 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
579 if (n != sizeof(hdr.version)) {
580 if (n == 0) /* EOF */
581 return NULL;
582 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
584 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
585 if (n != sizeof(hdr.nentries)) {
586 if (n == 0) /* EOF */
587 return NULL;
588 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
591 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
592 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
593 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
595 hdr.signature = be32toh(hdr.signature);
596 hdr.version = be32toh(hdr.version);
597 hdr.nentries = be32toh(hdr.nentries);
599 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
600 return got_error(GOT_ERR_FILEIDX_SIG);
601 if (hdr.version != GOT_FILE_INDEX_VERSION)
602 return got_error(GOT_ERR_FILEIDX_VER);
604 for (i = 0; i < hdr.nentries; i++) {
605 err = read_fileindex_entry(&entry, &ctx, infile);
606 if (err)
607 return err;
608 err = add_entry(fileindex, entry);
609 if (err)
610 return err;
613 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
614 if (n != sizeof(sha1_expected))
615 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
616 SHA1Final(sha1, &ctx);
617 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
618 return got_error(GOT_ERR_FILEIDX_CSUM);
620 return NULL;
623 static struct got_fileindex_entry *
624 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
626 struct got_fileindex_entry *next;
628 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
630 /* Skip entries which were newly added by diff callbacks. */
631 while (next && (next->flags & GOT_FILEIDX_F_NOT_FLUSHED))
632 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
634 return next;
637 static const struct got_error *
638 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
639 const struct got_tree_entries *, const char *, const char *,
640 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
642 static const struct got_error *
643 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
644 struct got_fileindex_entry **ie, struct got_tree_entry *te,
645 const char *path, const char *entry_name, struct got_repository *repo,
646 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
648 const struct got_error *err = NULL;
650 if (S_ISDIR(te->mode)) {
651 char *subpath;
652 struct got_tree_object *subtree;
654 if (asprintf(&subpath, "%s%s%s", path,
655 path[0] == '\0' ? "" : "/", te->name) == -1)
656 return got_error_from_errno("asprintf");
658 err = got_object_open_as_tree(&subtree, repo, te->id);
659 if (err) {
660 free(subpath);
661 return err;
664 err = diff_fileindex_tree(fileindex, ie,
665 got_object_tree_get_entries(subtree), subpath, entry_name,
666 repo, cb, cb_arg);
667 free(subpath);
668 got_object_tree_close(subtree);
669 if (err)
670 return err;
673 *next = SIMPLEQ_NEXT(te, entry);
674 return NULL;
677 static const struct got_error *
678 diff_fileindex_tree(struct got_fileindex *fileindex,
679 struct got_fileindex_entry **ie, const struct got_tree_entries *entries,
680 const char *path, const char *entry_name, struct got_repository *repo,
681 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
683 const struct got_error *err = NULL;
684 struct got_tree_entry *te = NULL;
685 size_t path_len = strlen(path);
686 struct got_fileindex_entry *next;
688 te = SIMPLEQ_FIRST(&entries->head);
689 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
690 if (te && *ie) {
691 char *te_path;
692 int cmp;
693 if (asprintf(&te_path, "%s/%s", path, te->name) == -1) {
694 err = got_error_from_errno("asprintf");
695 break;
697 cmp = got_path_cmp((*ie)->path, te_path);
698 free(te_path);
699 if (cmp == 0) {
700 if (got_path_is_child((*ie)->path, path,
701 path_len) && (entry_name == NULL ||
702 strcmp(te->name, entry_name) == 0)) {
703 err = cb->diff_old_new(cb_arg, *ie, te,
704 path);
705 if (err || entry_name)
706 break;
708 *ie = walk_fileindex(fileindex, *ie);
709 err = walk_tree(&te, fileindex, ie, te,
710 path, entry_name, repo, cb, cb_arg);
711 } else if (cmp < 0) {
712 next = walk_fileindex(fileindex, *ie);
713 if (got_path_is_child((*ie)->path, path,
714 path_len) && (entry_name == NULL ||
715 strcmp(te->name, entry_name) == 0)) {
716 err = cb->diff_old(cb_arg, *ie, path);
717 if (err || entry_name)
718 break;
720 *ie = next;
721 } else {
722 if ((entry_name == NULL ||
723 strcmp(te->name, entry_name) == 0)) {
724 err = cb->diff_new(cb_arg, te, path);
725 if (err || entry_name)
726 break;
728 err = walk_tree(&te, fileindex, ie, te,
729 path, entry_name, repo, cb, cb_arg);
731 if (err)
732 break;
733 } else if (*ie) {
734 next = walk_fileindex(fileindex, *ie);
735 if (got_path_is_child((*ie)->path, path, path_len) &&
736 (entry_name == NULL ||
737 strcmp(te->name, entry_name) == 0)) {
738 err = cb->diff_old(cb_arg, *ie, path);
739 if (err || entry_name)
740 break;
742 *ie = next;
743 } else if (te) {
744 if (entry_name == NULL ||
745 strcmp(te->name, entry_name) == 0) {
746 err = cb->diff_new(cb_arg, te, path);
747 if (err || entry_name)
748 break;
750 err = walk_tree(&te, fileindex, ie, te, path,
751 entry_name, repo, cb, cb_arg);
752 if (err)
753 break;
757 return err;
760 const struct got_error *
761 got_fileindex_diff_tree(struct got_fileindex *fileindex,
762 struct got_tree_object *tree, const char *path, const char *entry_name,
763 struct got_repository *repo,
764 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
766 struct got_fileindex_entry *ie;
767 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
768 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
769 ie = walk_fileindex(fileindex, ie);
770 return diff_fileindex_tree(fileindex, &ie,
771 got_object_tree_get_entries(tree), path, entry_name, repo,
772 cb, cb_arg);
775 static const struct got_error *
776 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
777 struct got_pathlist_head *, const char *, const char *,
778 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
780 static const struct got_error *
781 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
783 const struct got_error *err = NULL;
784 struct got_pathlist_entry *new = NULL;
785 struct dirent *dep = NULL;
786 struct dirent *de = NULL;
788 for (;;) {
789 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
790 if (de == NULL) {
791 err = got_error_from_errno("malloc");
792 break;
795 if (readdir_r(dir, de, &dep) != 0) {
796 err = got_error_from_errno("readdir_r");
797 free(de);
798 break;
800 if (dep == NULL) {
801 free(de);
802 break;
805 if (strcmp(de->d_name, ".") == 0 ||
806 strcmp(de->d_name, "..") == 0 ||
807 (path[0] == '\0' &&
808 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
809 free(de);
810 continue;
813 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
814 if (err) {
815 free(de);
816 break;
818 if (new == NULL) {
819 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
820 free(de);
821 break;
825 return err;
828 void
829 free_dirlist(struct got_pathlist_head *dirlist)
831 struct got_pathlist_entry *dle;
833 TAILQ_FOREACH(dle, dirlist, entry)
834 free(dle->data);
835 got_pathlist_free(dirlist);
838 static const struct got_error *
839 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
840 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle,
841 const char *path, const char *rootpath, struct got_repository *repo,
842 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
844 const struct got_error *err = NULL;
845 struct dirent *de = dle->data;
847 *next = NULL;
849 if (de->d_type == DT_DIR) {
850 char *subpath;
851 char *subdirpath;
852 DIR *subdir;
853 struct got_pathlist_head subdirlist;
855 TAILQ_INIT(&subdirlist);
857 if (asprintf(&subpath, "%s%s%s", path,
858 path[0] == '\0' ? "" : "/", de->d_name) == -1)
859 return got_error_from_errno("asprintf");
861 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
862 free(subpath);
863 return got_error_from_errno("asprintf");
866 subdir = opendir(subdirpath);
867 if (subdir == NULL) {
868 free(subpath);
869 free(subdirpath);
870 return got_error_from_errno2("opendir", subdirpath);
873 err = read_dirlist(&subdirlist, subdir, subdirpath);
874 if (err) {
875 free(subpath);
876 free(subdirpath);
877 closedir(subdir);
878 return err;
880 err = diff_fileindex_dir(fileindex, ie, &subdirlist, rootpath,
881 subpath, repo, cb, cb_arg);
882 free(subpath);
883 free(subdirpath);
884 closedir(subdir);
885 free_dirlist(&subdirlist);
886 if (err)
887 return err;
890 *next = TAILQ_NEXT(dle, entry);
891 return NULL;
894 static const struct got_error *
895 diff_fileindex_dir(struct got_fileindex *fileindex,
896 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
897 const char *rootpath, const char *path, struct got_repository *repo,
898 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
900 const struct got_error *err = NULL;
901 struct dirent *de = NULL;
902 size_t path_len = strlen(path);
903 struct got_pathlist_entry *dle;
905 dle = TAILQ_FIRST(dirlist);
906 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
907 if (dle && *ie) {
908 char *de_path;
909 int cmp;
910 de = dle->data;
911 if (asprintf(&de_path, "%s/%s", path,
912 de->d_name) == -1) {
913 err = got_error_from_errno("asprintf");
914 break;
916 cmp = got_path_cmp((*ie)->path, de_path);
917 free(de_path);
918 if (cmp == 0) {
919 err = cb->diff_old_new(cb_arg, *ie, de, path);
920 if (err)
921 break;
922 *ie = walk_fileindex(fileindex, *ie);
923 err = walk_dir(&dle, fileindex, ie, dle, path,
924 rootpath, repo, cb, cb_arg);
925 } else if (cmp < 0 ) {
926 err = cb->diff_old(cb_arg, *ie, path);
927 if (err)
928 break;
929 *ie = walk_fileindex(fileindex, *ie);
930 } else {
931 err = cb->diff_new(cb_arg, de, path);
932 if (err)
933 break;
934 err = walk_dir(&dle, fileindex, ie, dle, path,
935 rootpath, repo, cb, cb_arg);
937 if (err)
938 break;
939 } else if (*ie) {
940 err = cb->diff_old(cb_arg, *ie, path);
941 if (err)
942 break;
943 *ie = walk_fileindex(fileindex, *ie);
944 } else if (dle) {
945 de = dle->data;
946 err = cb->diff_new(cb_arg, de, path);
947 if (err)
948 break;
949 err = walk_dir(&dle, fileindex, ie, dle, path,
950 rootpath, repo, cb, cb_arg);
951 if (err)
952 break;
956 return err;
959 const struct got_error *
960 got_fileindex_diff_dir(struct got_fileindex *fileindex, DIR *rootdir,
961 const char *rootpath, const char *path, struct got_repository *repo,
962 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
964 const struct got_error *err;
965 struct got_fileindex_entry *ie;
966 struct got_pathlist_head dirlist;
968 TAILQ_INIT(&dirlist);
969 err = read_dirlist(&dirlist, rootdir, path);
970 if (err)
971 return err;
972 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
973 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
974 ie = walk_fileindex(fileindex, ie);
975 err = diff_fileindex_dir(fileindex, &ie, &dirlist, rootpath, path,
976 repo, cb, cb_arg);
977 free_dirlist(&dirlist);
978 return err;
981 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);