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 const size_t chunk_size = 8;
459 size_t n, len = 0, totlen = chunk_size;
461 *path = malloc(totlen);
462 if (*path == NULL)
463 return got_error_from_errno("malloc");
465 do {
466 if (len + chunk_size > totlen) {
467 char *p = reallocarray(*path, totlen + chunk_size, 1);
468 if (p == NULL) {
469 err = got_error_from_errno("reallocarray");
470 break;
472 totlen += chunk_size;
473 *path = p;
475 n = fread(*path + len, 1, chunk_size, infile);
476 if (n != chunk_size) {
477 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
478 break;
480 SHA1Update(ctx, *path + len, chunk_size);
481 len += chunk_size;
482 } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
484 if (err) {
485 free(*path);
486 *path = NULL;
488 return err;
491 static const struct got_error *
492 read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
493 FILE *infile)
495 const struct got_error *err;
496 struct got_fileindex_entry *entry;
497 size_t n;
499 *entryp = NULL;
501 entry = calloc(1, sizeof(*entry));
502 if (entry == NULL)
503 return got_error_from_errno("calloc");
505 err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
506 if (err)
507 goto done;
508 err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
509 if (err)
510 goto done;
511 err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
512 if (err)
513 goto done;
514 err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
515 if (err)
516 goto done;
518 err = read_fileindex_val32(&entry->uid, ctx, infile);
519 if (err)
520 goto done;
521 err = read_fileindex_val32(&entry->gid, ctx, infile);
522 if (err)
523 goto done;
524 err = read_fileindex_val32(&entry->size, ctx, infile);
525 if (err)
526 goto done;
528 err = read_fileindex_val16(&entry->mode, ctx, infile);
529 if (err)
530 goto done;
532 n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
533 if (n != SHA1_DIGEST_LENGTH) {
534 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
535 goto done;
537 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
539 n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
540 if (n != SHA1_DIGEST_LENGTH) {
541 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
542 goto done;
544 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
546 err = read_fileindex_val32(&entry->flags, ctx, infile);
547 if (err)
548 goto done;
550 err = read_fileindex_path(&entry->path, ctx, infile);
551 if (err == NULL)
552 entry->path_len = strlen(entry->path);
553 done:
554 if (err)
555 got_fileindex_entry_free(entry);
556 else
557 *entryp = entry;
558 return err;
561 const struct got_error *
562 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
564 const struct got_error *err = NULL;
565 struct got_fileindex_hdr hdr;
566 SHA1_CTX ctx;
567 struct got_fileindex_entry *entry;
568 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
569 uint8_t sha1[SHA1_DIGEST_LENGTH];
570 size_t n;
571 int i;
573 SHA1Init(&ctx);
575 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
576 if (n != sizeof(hdr.signature)) {
577 if (n == 0) /* EOF */
578 return NULL;
579 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
581 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
582 if (n != sizeof(hdr.version)) {
583 if (n == 0) /* EOF */
584 return NULL;
585 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
587 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
588 if (n != sizeof(hdr.nentries)) {
589 if (n == 0) /* EOF */
590 return NULL;
591 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
594 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
595 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
596 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
598 hdr.signature = be32toh(hdr.signature);
599 hdr.version = be32toh(hdr.version);
600 hdr.nentries = be32toh(hdr.nentries);
602 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
603 return got_error(GOT_ERR_FILEIDX_SIG);
604 if (hdr.version != GOT_FILE_INDEX_VERSION)
605 return got_error(GOT_ERR_FILEIDX_VER);
607 for (i = 0; i < hdr.nentries; i++) {
608 err = read_fileindex_entry(&entry, &ctx, infile);
609 if (err)
610 return err;
611 err = add_entry(fileindex, entry);
612 if (err)
613 return err;
616 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
617 if (n != sizeof(sha1_expected))
618 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
619 SHA1Final(sha1, &ctx);
620 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
621 return got_error(GOT_ERR_FILEIDX_CSUM);
623 return NULL;
626 static struct got_fileindex_entry *
627 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
629 struct got_fileindex_entry *next;
631 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
633 /* Skip entries which were newly added by diff callbacks. */
634 while (next && (next->flags & GOT_FILEIDX_F_NOT_FLUSHED))
635 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
637 return next;
640 static const struct got_error *
641 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
642 const struct got_tree_entries *, const char *, const char *,
643 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
645 static const struct got_error *
646 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
647 struct got_fileindex_entry **ie, struct got_tree_entry *te,
648 const char *path, const char *entry_name, struct got_repository *repo,
649 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
651 const struct got_error *err = NULL;
653 if (S_ISDIR(te->mode)) {
654 char *subpath;
655 struct got_tree_object *subtree;
657 if (asprintf(&subpath, "%s%s%s", path,
658 path[0] == '\0' ? "" : "/", te->name) == -1)
659 return got_error_from_errno("asprintf");
661 err = got_object_open_as_tree(&subtree, repo, te->id);
662 if (err) {
663 free(subpath);
664 return err;
667 err = diff_fileindex_tree(fileindex, ie,
668 got_object_tree_get_entries(subtree), subpath, entry_name,
669 repo, cb, cb_arg);
670 free(subpath);
671 got_object_tree_close(subtree);
672 if (err)
673 return err;
676 *next = SIMPLEQ_NEXT(te, entry);
677 return NULL;
680 static const struct got_error *
681 diff_fileindex_tree(struct got_fileindex *fileindex,
682 struct got_fileindex_entry **ie, const struct got_tree_entries *entries,
683 const char *path, const char *entry_name, struct got_repository *repo,
684 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
686 const struct got_error *err = NULL;
687 struct got_tree_entry *te = NULL;
688 size_t path_len = strlen(path);
689 struct got_fileindex_entry *next;
691 te = SIMPLEQ_FIRST(&entries->head);
692 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
693 if (te && *ie) {
694 char *te_path;
695 int cmp;
696 if (asprintf(&te_path, "%s/%s", path, te->name) == -1) {
697 err = got_error_from_errno("asprintf");
698 break;
700 cmp = got_path_cmp((*ie)->path, te_path,
701 (*ie)->path_len, strlen(te_path));
702 free(te_path);
703 if (cmp == 0) {
704 if (got_path_is_child((*ie)->path, path,
705 path_len) && (entry_name == NULL ||
706 strcmp(te->name, entry_name) == 0)) {
707 err = cb->diff_old_new(cb_arg, *ie, te,
708 path);
709 if (err || entry_name)
710 break;
712 *ie = walk_fileindex(fileindex, *ie);
713 err = walk_tree(&te, fileindex, ie, te,
714 path, entry_name, repo, cb, cb_arg);
715 } else if (cmp < 0) {
716 next = walk_fileindex(fileindex, *ie);
717 if (got_path_is_child((*ie)->path, path,
718 path_len) && (entry_name == NULL ||
719 strcmp(te->name, entry_name) == 0)) {
720 err = cb->diff_old(cb_arg, *ie, path);
721 if (err || entry_name)
722 break;
724 *ie = next;
725 } else {
726 if ((entry_name == NULL ||
727 strcmp(te->name, entry_name) == 0)) {
728 err = cb->diff_new(cb_arg, te, path);
729 if (err || entry_name)
730 break;
732 err = walk_tree(&te, fileindex, ie, te,
733 path, entry_name, repo, cb, cb_arg);
735 if (err)
736 break;
737 } else if (*ie) {
738 next = walk_fileindex(fileindex, *ie);
739 if (got_path_is_child((*ie)->path, path, path_len) &&
740 (entry_name == NULL ||
741 strcmp(te->name, entry_name) == 0)) {
742 err = cb->diff_old(cb_arg, *ie, path);
743 if (err || entry_name)
744 break;
746 *ie = next;
747 } else if (te) {
748 if (entry_name == NULL ||
749 strcmp(te->name, entry_name) == 0) {
750 err = cb->diff_new(cb_arg, te, path);
751 if (err || entry_name)
752 break;
754 err = walk_tree(&te, fileindex, ie, te, path,
755 entry_name, repo, cb, cb_arg);
756 if (err)
757 break;
761 return err;
764 const struct got_error *
765 got_fileindex_diff_tree(struct got_fileindex *fileindex,
766 struct got_tree_object *tree, const char *path, const char *entry_name,
767 struct got_repository *repo,
768 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
770 struct got_fileindex_entry *ie;
771 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
772 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
773 ie = walk_fileindex(fileindex, ie);
774 return diff_fileindex_tree(fileindex, &ie,
775 got_object_tree_get_entries(tree), path, entry_name, repo,
776 cb, cb_arg);
779 static const struct got_error *
780 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
781 struct got_pathlist_head *, const char *, const char *,
782 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
784 static const struct got_error *
785 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
787 const struct got_error *err = NULL;
788 struct got_pathlist_entry *new = NULL;
789 struct dirent *dep = NULL;
790 struct dirent *de = NULL;
792 for (;;) {
793 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
794 if (de == NULL) {
795 err = got_error_from_errno("malloc");
796 break;
799 if (readdir_r(dir, de, &dep) != 0) {
800 err = got_error_from_errno("readdir_r");
801 free(de);
802 break;
804 if (dep == NULL) {
805 free(de);
806 break;
809 if (strcmp(de->d_name, ".") == 0 ||
810 strcmp(de->d_name, "..") == 0 ||
811 (path[0] == '\0' &&
812 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
813 free(de);
814 continue;
817 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
818 if (err) {
819 free(de);
820 break;
822 if (new == NULL) {
823 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
824 free(de);
825 break;
829 return err;
832 void
833 free_dirlist(struct got_pathlist_head *dirlist)
835 struct got_pathlist_entry *dle;
837 TAILQ_FOREACH(dle, dirlist, entry)
838 free(dle->data);
839 got_pathlist_free(dirlist);
842 static const struct got_error *
843 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
844 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle,
845 const char *path, const char *rootpath, struct got_repository *repo,
846 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
848 const struct got_error *err = NULL;
849 struct dirent *de = dle->data;
851 *next = NULL;
853 if (de->d_type == DT_DIR) {
854 char *subpath;
855 char *subdirpath;
856 DIR *subdir;
857 struct got_pathlist_head subdirlist;
859 TAILQ_INIT(&subdirlist);
861 if (asprintf(&subpath, "%s%s%s", path,
862 path[0] == '\0' ? "" : "/", de->d_name) == -1)
863 return got_error_from_errno("asprintf");
865 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
866 free(subpath);
867 return got_error_from_errno("asprintf");
870 subdir = opendir(subdirpath);
871 if (subdir == NULL) {
872 free(subpath);
873 free(subdirpath);
874 return got_error_from_errno2("opendir", subdirpath);
877 err = read_dirlist(&subdirlist, subdir, subdirpath);
878 if (err) {
879 free(subpath);
880 free(subdirpath);
881 closedir(subdir);
882 return err;
884 err = diff_fileindex_dir(fileindex, ie, &subdirlist, rootpath,
885 subpath, repo, cb, cb_arg);
886 free(subpath);
887 free(subdirpath);
888 closedir(subdir);
889 free_dirlist(&subdirlist);
890 if (err)
891 return err;
894 *next = TAILQ_NEXT(dle, entry);
895 return NULL;
898 static const struct got_error *
899 diff_fileindex_dir(struct got_fileindex *fileindex,
900 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
901 const char *rootpath, const char *path, struct got_repository *repo,
902 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
904 const struct got_error *err = NULL;
905 struct dirent *de = NULL;
906 size_t path_len = strlen(path);
907 struct got_pathlist_entry *dle;
909 dle = TAILQ_FIRST(dirlist);
910 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
911 if (dle && *ie) {
912 char *de_path;
913 int cmp;
914 de = dle->data;
915 if (asprintf(&de_path, "%s/%s", path,
916 de->d_name) == -1) {
917 err = got_error_from_errno("asprintf");
918 break;
920 cmp = got_path_cmp((*ie)->path, de_path,
921 (*ie)->path_len, strlen(path) + 1 + de->d_namlen);
922 free(de_path);
923 if (cmp == 0) {
924 err = cb->diff_old_new(cb_arg, *ie, de, path);
925 if (err)
926 break;
927 *ie = walk_fileindex(fileindex, *ie);
928 err = walk_dir(&dle, fileindex, ie, dle, path,
929 rootpath, repo, cb, cb_arg);
930 } else if (cmp < 0 ) {
931 err = cb->diff_old(cb_arg, *ie, path);
932 if (err)
933 break;
934 *ie = walk_fileindex(fileindex, *ie);
935 } else {
936 err = cb->diff_new(cb_arg, de, path);
937 if (err)
938 break;
939 err = walk_dir(&dle, fileindex, ie, dle, path,
940 rootpath, repo, cb, cb_arg);
942 if (err)
943 break;
944 } else if (*ie) {
945 err = cb->diff_old(cb_arg, *ie, path);
946 if (err)
947 break;
948 *ie = walk_fileindex(fileindex, *ie);
949 } else if (dle) {
950 de = dle->data;
951 err = cb->diff_new(cb_arg, de, path);
952 if (err)
953 break;
954 err = walk_dir(&dle, fileindex, ie, dle, path,
955 rootpath, repo, cb, cb_arg);
956 if (err)
957 break;
961 return err;
964 const struct got_error *
965 got_fileindex_diff_dir(struct got_fileindex *fileindex, DIR *rootdir,
966 const char *rootpath, const char *path, struct got_repository *repo,
967 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
969 const struct got_error *err;
970 struct got_fileindex_entry *ie;
971 struct got_pathlist_head dirlist;
973 TAILQ_INIT(&dirlist);
974 err = read_dirlist(&dirlist, rootdir, path);
975 if (err)
976 return err;
977 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
978 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
979 ie = walk_fileindex(fileindex, ie);
980 err = diff_fileindex_dir(fileindex, &ie, &dirlist, rootpath, path,
981 repo, cb, cb_arg);
982 free_dirlist(&dirlist);
983 return err;
986 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);