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_NOT_FLUSHED 0x00010000
41 #define GOT_FILEIDX_F_NO_BLOB 0x00020000
42 #define GOT_FILEIDX_F_NO_COMMIT 0x00040000
43 #define GOT_FILEIDX_F_NO_FILE_ON_DISK 0x00080000
45 struct got_fileindex {
46 struct got_fileindex_tree entries;
47 int nentries;
48 #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
49 };
51 uint16_t
52 got_fileindex_perms_from_st(struct stat *sb)
53 {
54 uint16_t perms = (sb->st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
55 return (perms << GOT_FILEIDX_MODE_PERMS_SHIFT);
56 }
58 mode_t
59 got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
60 {
61 mode_t perms = (ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT);
62 return (perms & (S_IRWXU | S_IRWXG | S_IRWXO));
63 }
65 const struct got_error *
66 got_fileindex_entry_update(struct got_fileindex_entry *ie,
67 const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
68 int update_timestamps)
69 {
70 struct stat sb;
72 if (lstat(ondisk_path, &sb) != 0) {
73 if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0)
74 return got_error_from_errno2("lstat", ondisk_path);
75 } else {
76 if (sb.st_mode & S_IFDIR)
77 return got_error_set_errno(EISDIR, ondisk_path);
78 ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
79 }
82 if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
83 if (update_timestamps) {
84 ie->ctime_sec = sb.st_ctime;
85 ie->ctime_nsec = sb.st_ctimensec;
86 ie->mtime_sec = sb.st_mtime;
87 ie->mtime_nsec = sb.st_mtimensec;
88 }
89 ie->uid = sb.st_uid;
90 ie->gid = sb.st_gid;
91 ie->size = (sb.st_size & 0xffffffff);
92 if (sb.st_mode & S_IFLNK)
93 ie->mode = GOT_FILEIDX_MODE_SYMLINK;
94 else
95 ie->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
96 ie->mode |= got_fileindex_perms_from_st(&sb);
97 }
99 if (blob_sha1) {
100 memcpy(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
101 ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
102 } else
103 ie->flags |= GOT_FILEIDX_F_NO_BLOB;
105 if (commit_sha1) {
106 memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
107 ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
108 } else
109 ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
111 return NULL;
114 void
115 got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
117 ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
120 const struct got_error *
121 got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
122 const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
123 uint8_t *commit_sha1)
125 size_t len;
127 *ie = calloc(1, sizeof(**ie));
128 if (*ie == NULL)
129 return got_error_from_errno("calloc");
131 (*ie)->path = strdup(relpath);
132 if ((*ie)->path == NULL) {
133 const struct got_error *err = got_error_from_errno("strdup");
134 free(*ie);
135 *ie = NULL;
136 return err;
139 len = strlen(relpath);
140 if (len > GOT_FILEIDX_F_PATH_LEN)
141 len = GOT_FILEIDX_F_PATH_LEN;
142 (*ie)->flags |= len;
144 return got_fileindex_entry_update(*ie, ondisk_path, blob_sha1,
145 commit_sha1, 1);
148 void
149 got_fileindex_entry_free(struct got_fileindex_entry *ie)
151 free(ie->path);
152 free(ie);
155 size_t
156 got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
158 return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
161 int
162 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
164 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
167 int
168 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
170 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
173 int
174 got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
176 return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
179 static const struct got_error *
180 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
182 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
183 return got_error(GOT_ERR_NO_SPACE);
185 RB_INSERT(got_fileindex_tree, &fileindex->entries, ie);
186 fileindex->nentries++;
187 return NULL;
190 const struct got_error *
191 got_fileindex_entry_add(struct got_fileindex *fileindex,
192 struct got_fileindex_entry *ie)
194 /* Flag this entry until it gets written out to disk. */
195 ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
197 return add_entry(fileindex, ie);
200 void
201 got_fileindex_entry_remove(struct got_fileindex *fileindex,
202 struct got_fileindex_entry *ie)
204 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
205 fileindex->nentries--;
208 struct got_fileindex_entry *
209 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
210 size_t path_len)
212 struct got_fileindex_entry key;
213 memset(&key, 0, sizeof(key));
214 key.path = (char *)path;
215 key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
216 return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
219 const struct got_error *
220 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
221 got_fileindex_cb cb, void *cb_arg)
223 const struct got_error *err;
224 struct got_fileindex_entry *ie, *tmp;
226 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
227 err = (*cb)(cb_arg, ie);
228 if (err)
229 return err;
231 return NULL;
234 struct got_fileindex *
235 got_fileindex_alloc(void)
237 struct got_fileindex *fileindex;
239 fileindex = calloc(1, sizeof(*fileindex));
240 if (fileindex == NULL)
241 return NULL;
243 RB_INIT(&fileindex->entries);
244 return fileindex;
247 void
248 got_fileindex_free(struct got_fileindex *fileindex)
250 struct got_fileindex_entry *ie;
252 while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
253 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
254 got_fileindex_entry_free(ie);
256 free(fileindex);
259 static const struct got_error *
260 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
262 size_t n;
264 val = htobe64(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_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
275 size_t n;
277 val = htobe32(val);
278 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
279 n = fwrite(&val, 1, sizeof(val), outfile);
280 if (n != sizeof(val))
281 return got_ferror(outfile, GOT_ERR_IO);
282 return NULL;
285 static const struct got_error *
286 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
288 size_t n;
290 val = htobe16(val);
291 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
292 n = fwrite(&val, 1, sizeof(val), outfile);
293 if (n != sizeof(val))
294 return got_ferror(outfile, GOT_ERR_IO);
295 return NULL;
298 static const struct got_error *
299 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
301 size_t n, len, pad = 0;
302 static const uint8_t zero[8] = { 0 };
304 len = strlen(path);
305 while ((len + pad) % 8 != 0)
306 pad++;
307 if (pad == 0)
308 pad = 8; /* NUL-terminate */
310 SHA1Update(ctx, path, len);
311 n = fwrite(path, 1, len, outfile);
312 if (n != len)
313 return got_ferror(outfile, GOT_ERR_IO);
314 SHA1Update(ctx, zero, pad);
315 n = fwrite(zero, 1, pad, outfile);
316 if (n != pad)
317 return got_ferror(outfile, GOT_ERR_IO);
318 return NULL;
321 static const struct got_error *
322 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
323 FILE *outfile)
325 const struct got_error *err;
326 size_t n;
328 err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
329 if (err)
330 return err;
331 err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
332 if (err)
333 return err;
334 err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
335 if (err)
336 return err;
337 err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
338 if (err)
339 return err;
341 err = write_fileindex_val32(ctx, ie->uid, outfile);
342 if (err)
343 return err;
344 err = write_fileindex_val32(ctx, ie->gid, outfile);
345 if (err)
346 return err;
347 err = write_fileindex_val32(ctx, ie->size, outfile);
348 if (err)
349 return err;
351 err = write_fileindex_val16(ctx, ie->mode, outfile);
352 if (err)
353 return err;
355 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
356 n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
357 if (n != SHA1_DIGEST_LENGTH)
358 return got_ferror(outfile, GOT_ERR_IO);
360 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
361 n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
362 if (n != SHA1_DIGEST_LENGTH)
363 return got_ferror(outfile, GOT_ERR_IO);
365 err = write_fileindex_val32(ctx, ie->flags, outfile);
366 if (err)
367 return err;
369 err = write_fileindex_path(ctx, ie->path, outfile);
370 return err;
373 const struct got_error *
374 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
376 const struct got_error *err = NULL;
377 struct got_fileindex_hdr hdr;
378 SHA1_CTX ctx;
379 uint8_t sha1[SHA1_DIGEST_LENGTH];
380 size_t n;
381 struct got_fileindex_entry *ie;
383 SHA1Init(&ctx);
385 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
386 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
387 hdr.nentries = htobe32(fileindex->nentries);
389 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
390 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
391 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
392 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
393 if (n != sizeof(hdr.signature))
394 return got_ferror(outfile, GOT_ERR_IO);
395 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
396 if (n != sizeof(hdr.version))
397 return got_ferror(outfile, GOT_ERR_IO);
398 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
399 if (n != sizeof(hdr.nentries))
400 return got_ferror(outfile, GOT_ERR_IO);
402 RB_FOREACH(ie, got_fileindex_tree, &fileindex->entries) {
403 ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
404 err = write_fileindex_entry(&ctx, ie, outfile);
405 if (err)
406 return err;
409 SHA1Final(sha1, &ctx);
410 n = fwrite(sha1, 1, sizeof(sha1), outfile);
411 if (n != sizeof(sha1))
412 return got_ferror(outfile, GOT_ERR_IO);
414 if (fflush(outfile) != 0)
415 return got_error_from_errno("fflush");
417 return NULL;
420 static const struct got_error *
421 read_fileindex_val64(uint64_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 = be64toh(*val);
430 return NULL;
433 static const struct got_error *
434 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
436 size_t n;
438 n = fread(val, 1, sizeof(*val), infile);
439 if (n != sizeof(*val))
440 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
441 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
442 *val = be32toh(*val);
443 return NULL;
446 static const struct got_error *
447 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
449 size_t n;
451 n = fread(val, 1, sizeof(*val), infile);
452 if (n != sizeof(*val))
453 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
454 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
455 *val = be16toh(*val);
456 return NULL;
459 static const struct got_error *
460 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
462 const struct got_error *err = NULL;
463 const size_t chunk_size = 8;
464 size_t n, len = 0, totlen = chunk_size;
466 *path = malloc(totlen);
467 if (*path == NULL)
468 return got_error_from_errno("malloc");
470 do {
471 if (len + chunk_size > totlen) {
472 char *p = reallocarray(*path, totlen + chunk_size, 1);
473 if (p == NULL) {
474 err = got_error_from_errno("reallocarray");
475 break;
477 totlen += chunk_size;
478 *path = p;
480 n = fread(*path + len, 1, chunk_size, infile);
481 if (n != chunk_size) {
482 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
483 break;
485 SHA1Update(ctx, *path + len, chunk_size);
486 len += chunk_size;
487 } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
489 if (err) {
490 free(*path);
491 *path = NULL;
493 return err;
496 static const struct got_error *
497 read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
498 FILE *infile)
500 const struct got_error *err;
501 struct got_fileindex_entry *ie;
502 size_t n;
504 *iep = NULL;
506 ie = calloc(1, sizeof(*ie));
507 if (ie == NULL)
508 return got_error_from_errno("calloc");
510 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
511 if (err)
512 goto done;
513 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
514 if (err)
515 goto done;
516 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
517 if (err)
518 goto done;
519 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
520 if (err)
521 goto done;
523 err = read_fileindex_val32(&ie->uid, ctx, infile);
524 if (err)
525 goto done;
526 err = read_fileindex_val32(&ie->gid, ctx, infile);
527 if (err)
528 goto done;
529 err = read_fileindex_val32(&ie->size, ctx, infile);
530 if (err)
531 goto done;
533 err = read_fileindex_val16(&ie->mode, ctx, infile);
534 if (err)
535 goto done;
537 n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
538 if (n != SHA1_DIGEST_LENGTH) {
539 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
540 goto done;
542 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
544 n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
545 if (n != SHA1_DIGEST_LENGTH) {
546 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
547 goto done;
549 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
551 err = read_fileindex_val32(&ie->flags, ctx, infile);
552 if (err)
553 goto done;
555 err = read_fileindex_path(&ie->path, ctx, infile);
556 done:
557 if (err)
558 got_fileindex_entry_free(ie);
559 else
560 *iep = ie;
561 return err;
564 const struct got_error *
565 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
567 const struct got_error *err = NULL;
568 struct got_fileindex_hdr hdr;
569 SHA1_CTX ctx;
570 struct got_fileindex_entry *ie;
571 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
572 uint8_t sha1[SHA1_DIGEST_LENGTH];
573 size_t n;
574 int i;
576 SHA1Init(&ctx);
578 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
579 if (n != sizeof(hdr.signature)) {
580 if (n == 0) /* EOF */
581 return NULL;
582 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
584 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
585 if (n != sizeof(hdr.version)) {
586 if (n == 0) /* EOF */
587 return NULL;
588 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
590 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
591 if (n != sizeof(hdr.nentries)) {
592 if (n == 0) /* EOF */
593 return NULL;
594 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
597 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
598 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
599 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
601 hdr.signature = be32toh(hdr.signature);
602 hdr.version = be32toh(hdr.version);
603 hdr.nentries = be32toh(hdr.nentries);
605 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
606 return got_error(GOT_ERR_FILEIDX_SIG);
607 if (hdr.version != GOT_FILE_INDEX_VERSION)
608 return got_error(GOT_ERR_FILEIDX_VER);
610 for (i = 0; i < hdr.nentries; i++) {
611 err = read_fileindex_entry(&ie, &ctx, infile);
612 if (err)
613 return err;
614 err = add_entry(fileindex, ie);
615 if (err)
616 return err;
619 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
620 if (n != sizeof(sha1_expected))
621 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
622 SHA1Final(sha1, &ctx);
623 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
624 return got_error(GOT_ERR_FILEIDX_CSUM);
626 return NULL;
629 static struct got_fileindex_entry *
630 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
632 struct got_fileindex_entry *next;
634 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
636 /* Skip entries which were newly added by diff callbacks. */
637 while (next && (next->flags & GOT_FILEIDX_F_NOT_FLUSHED))
638 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
640 return next;
643 static const struct got_error *
644 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
645 const struct got_tree_entries *, const char *, const char *,
646 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
648 static const struct got_error *
649 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
650 struct got_fileindex_entry **ie, struct got_tree_entry *te,
651 const char *path, const char *entry_name, struct got_repository *repo,
652 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
654 const struct got_error *err = NULL;
656 if (S_ISDIR(te->mode)) {
657 char *subpath;
658 struct got_tree_object *subtree;
660 if (asprintf(&subpath, "%s%s%s", path,
661 path[0] == '\0' ? "" : "/", te->name) == -1)
662 return got_error_from_errno("asprintf");
664 err = got_object_open_as_tree(&subtree, repo, te->id);
665 if (err) {
666 free(subpath);
667 return err;
670 err = diff_fileindex_tree(fileindex, ie,
671 got_object_tree_get_entries(subtree), subpath, entry_name,
672 repo, cb, cb_arg);
673 free(subpath);
674 got_object_tree_close(subtree);
675 if (err)
676 return err;
679 *next = SIMPLEQ_NEXT(te, entry);
680 return NULL;
683 static const struct got_error *
684 diff_fileindex_tree(struct got_fileindex *fileindex,
685 struct got_fileindex_entry **ie, const struct got_tree_entries *entries,
686 const char *path, const char *entry_name, struct got_repository *repo,
687 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
689 const struct got_error *err = NULL;
690 struct got_tree_entry *te = NULL;
691 size_t path_len = strlen(path);
692 struct got_fileindex_entry *next;
694 te = SIMPLEQ_FIRST(&entries->head);
695 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
696 if (te && *ie) {
697 char *te_path;
698 int cmp;
699 if (asprintf(&te_path, "%s/%s", path, te->name) == -1) {
700 err = got_error_from_errno("asprintf");
701 break;
703 cmp = got_path_cmp((*ie)->path, te_path,
704 got_fileindex_entry_path_len(*ie), strlen(te_path));
705 free(te_path);
706 if (cmp == 0) {
707 if (got_path_is_child((*ie)->path, path,
708 path_len) && (entry_name == NULL ||
709 strcmp(te->name, entry_name) == 0)) {
710 err = cb->diff_old_new(cb_arg, *ie, te,
711 path);
712 if (err || entry_name)
713 break;
715 *ie = walk_fileindex(fileindex, *ie);
716 err = walk_tree(&te, fileindex, ie, te,
717 path, entry_name, repo, cb, cb_arg);
718 } else if (cmp < 0) {
719 next = walk_fileindex(fileindex, *ie);
720 if (got_path_is_child((*ie)->path, path,
721 path_len) && (entry_name == NULL ||
722 strcmp(te->name, entry_name) == 0)) {
723 err = cb->diff_old(cb_arg, *ie, path);
724 if (err || entry_name)
725 break;
727 *ie = next;
728 } else {
729 if ((entry_name == NULL ||
730 strcmp(te->name, entry_name) == 0)) {
731 err = cb->diff_new(cb_arg, te, path);
732 if (err || entry_name)
733 break;
735 err = walk_tree(&te, fileindex, ie, te,
736 path, entry_name, repo, cb, cb_arg);
738 if (err)
739 break;
740 } else if (*ie) {
741 next = walk_fileindex(fileindex, *ie);
742 if (got_path_is_child((*ie)->path, path, path_len) &&
743 (entry_name == NULL ||
744 strcmp(te->name, entry_name) == 0)) {
745 err = cb->diff_old(cb_arg, *ie, path);
746 if (err || entry_name)
747 break;
749 *ie = next;
750 } else if (te) {
751 if (entry_name == NULL ||
752 strcmp(te->name, entry_name) == 0) {
753 err = cb->diff_new(cb_arg, te, path);
754 if (err || entry_name)
755 break;
757 err = walk_tree(&te, fileindex, ie, te, path,
758 entry_name, repo, cb, cb_arg);
759 if (err)
760 break;
764 return err;
767 const struct got_error *
768 got_fileindex_diff_tree(struct got_fileindex *fileindex,
769 struct got_tree_object *tree, const char *path, const char *entry_name,
770 struct got_repository *repo,
771 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
773 struct got_fileindex_entry *ie;
774 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
775 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
776 ie = walk_fileindex(fileindex, ie);
777 return diff_fileindex_tree(fileindex, &ie,
778 got_object_tree_get_entries(tree), path, entry_name, repo,
779 cb, cb_arg);
782 static const struct got_error *
783 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
784 struct got_pathlist_head *, const char *, const char *,
785 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
787 static const struct got_error *
788 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
790 const struct got_error *err = NULL;
791 struct got_pathlist_entry *new = NULL;
792 struct dirent *dep = NULL;
793 struct dirent *de = NULL;
795 for (;;) {
796 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
797 if (de == NULL) {
798 err = got_error_from_errno("malloc");
799 break;
802 if (readdir_r(dir, de, &dep) != 0) {
803 err = got_error_from_errno("readdir_r");
804 free(de);
805 break;
807 if (dep == NULL) {
808 free(de);
809 break;
812 if (strcmp(de->d_name, ".") == 0 ||
813 strcmp(de->d_name, "..") == 0 ||
814 (path[0] == '\0' &&
815 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
816 free(de);
817 continue;
820 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
821 if (err) {
822 free(de);
823 break;
825 if (new == NULL) {
826 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
827 free(de);
828 break;
832 return err;
835 void
836 free_dirlist(struct got_pathlist_head *dirlist)
838 struct got_pathlist_entry *dle;
840 TAILQ_FOREACH(dle, dirlist, entry)
841 free(dle->data);
842 got_pathlist_free(dirlist);
845 static const struct got_error *
846 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
847 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle,
848 const char *path, const char *rootpath, struct got_repository *repo,
849 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
851 const struct got_error *err = NULL;
852 struct dirent *de = dle->data;
854 *next = NULL;
856 if (de->d_type == DT_DIR) {
857 char *subpath;
858 char *subdirpath;
859 DIR *subdir;
860 struct got_pathlist_head subdirlist;
862 TAILQ_INIT(&subdirlist);
864 if (asprintf(&subpath, "%s%s%s", path,
865 path[0] == '\0' ? "" : "/", de->d_name) == -1)
866 return got_error_from_errno("asprintf");
868 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
869 free(subpath);
870 return got_error_from_errno("asprintf");
873 subdir = opendir(subdirpath);
874 if (subdir == NULL) {
875 free(subpath);
876 free(subdirpath);
877 return got_error_from_errno2("opendir", subdirpath);
880 err = read_dirlist(&subdirlist, subdir, subdirpath);
881 if (err) {
882 free(subpath);
883 free(subdirpath);
884 closedir(subdir);
885 return err;
887 err = diff_fileindex_dir(fileindex, ie, &subdirlist, rootpath,
888 subpath, repo, cb, cb_arg);
889 free(subpath);
890 free(subdirpath);
891 closedir(subdir);
892 free_dirlist(&subdirlist);
893 if (err)
894 return err;
897 *next = TAILQ_NEXT(dle, entry);
898 return NULL;
901 static const struct got_error *
902 diff_fileindex_dir(struct got_fileindex *fileindex,
903 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
904 const char *rootpath, const char *path, struct got_repository *repo,
905 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
907 const struct got_error *err = NULL;
908 struct dirent *de = NULL;
909 size_t path_len = strlen(path);
910 struct got_pathlist_entry *dle;
912 dle = TAILQ_FIRST(dirlist);
913 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
914 if (dle && *ie) {
915 char *de_path;
916 int cmp;
917 de = dle->data;
918 if (asprintf(&de_path, "%s/%s", path,
919 de->d_name) == -1) {
920 err = got_error_from_errno("asprintf");
921 break;
923 cmp = got_path_cmp((*ie)->path, de_path,
924 got_fileindex_entry_path_len(*ie),
925 strlen(path) + 1 + de->d_namlen);
926 free(de_path);
927 if (cmp == 0) {
928 err = cb->diff_old_new(cb_arg, *ie, de, path);
929 if (err)
930 break;
931 *ie = walk_fileindex(fileindex, *ie);
932 err = walk_dir(&dle, fileindex, ie, dle, path,
933 rootpath, repo, cb, cb_arg);
934 } else if (cmp < 0 ) {
935 err = cb->diff_old(cb_arg, *ie, path);
936 if (err)
937 break;
938 *ie = walk_fileindex(fileindex, *ie);
939 } else {
940 err = cb->diff_new(cb_arg, de, path);
941 if (err)
942 break;
943 err = walk_dir(&dle, fileindex, ie, dle, path,
944 rootpath, repo, cb, cb_arg);
946 if (err)
947 break;
948 } else if (*ie) {
949 err = cb->diff_old(cb_arg, *ie, path);
950 if (err)
951 break;
952 *ie = walk_fileindex(fileindex, *ie);
953 } else if (dle) {
954 de = dle->data;
955 err = cb->diff_new(cb_arg, de, path);
956 if (err)
957 break;
958 err = walk_dir(&dle, fileindex, ie, dle, path,
959 rootpath, repo, cb, cb_arg);
960 if (err)
961 break;
965 return err;
968 const struct got_error *
969 got_fileindex_diff_dir(struct got_fileindex *fileindex, DIR *rootdir,
970 const char *rootpath, const char *path, struct got_repository *repo,
971 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
973 const struct got_error *err;
974 struct got_fileindex_entry *ie;
975 struct got_pathlist_head dirlist;
977 TAILQ_INIT(&dirlist);
978 err = read_dirlist(&dirlist, rootdir, path);
979 if (err)
980 return err;
981 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
982 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
983 ie = walk_fileindex(fileindex, ie);
984 err = diff_fileindex_dir(fileindex, &ie, &dirlist, rootpath, path,
985 repo, cb, cb_arg);
986 free_dirlist(&dirlist);
987 return err;
990 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);