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 0x0000f000
41 #define GOT_FILEIDX_F_STAGE_SHIFT 12
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 uint16_t
54 got_fileindex_perms_from_st(struct stat *sb)
55 {
56 uint16_t perms = (sb->st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
57 return (perms << GOT_FILEIDX_MODE_PERMS_SHIFT);
58 }
60 mode_t
61 got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
62 {
63 mode_t perms = (ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT);
64 return (S_IFREG | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
65 }
67 const struct got_error *
68 got_fileindex_entry_update(struct got_fileindex_entry *ie,
69 const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
70 int update_timestamps)
71 {
72 struct stat sb;
74 if (lstat(ondisk_path, &sb) != 0) {
75 if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
76 errno == ENOENT))
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 ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
82 }
85 if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
86 if (update_timestamps) {
87 ie->ctime_sec = sb.st_ctime;
88 ie->ctime_nsec = sb.st_ctimensec;
89 ie->mtime_sec = sb.st_mtime;
90 ie->mtime_nsec = sb.st_mtimensec;
91 }
92 ie->uid = sb.st_uid;
93 ie->gid = sb.st_gid;
94 ie->size = (sb.st_size & 0xffffffff);
95 if (sb.st_mode & S_IFLNK)
96 ie->mode = GOT_FILEIDX_MODE_SYMLINK;
97 else
98 ie->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
99 ie->mode |= got_fileindex_perms_from_st(&sb);
102 if (blob_sha1) {
103 memcpy(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
104 ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
105 } else
106 ie->flags |= GOT_FILEIDX_F_NO_BLOB;
108 if (commit_sha1) {
109 memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
110 ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
111 } else
112 ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
114 return NULL;
117 void
118 got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
120 ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
123 const struct got_error *
124 got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
125 const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
126 uint8_t *commit_sha1)
128 size_t len;
130 *ie = calloc(1, sizeof(**ie));
131 if (*ie == NULL)
132 return got_error_from_errno("calloc");
134 (*ie)->path = strdup(relpath);
135 if ((*ie)->path == NULL) {
136 const struct got_error *err = got_error_from_errno("strdup");
137 free(*ie);
138 *ie = NULL;
139 return err;
142 len = strlen(relpath);
143 if (len > GOT_FILEIDX_F_PATH_LEN)
144 len = GOT_FILEIDX_F_PATH_LEN;
145 (*ie)->flags |= len;
147 return got_fileindex_entry_update(*ie, ondisk_path, blob_sha1,
148 commit_sha1, 1);
151 void
152 got_fileindex_entry_free(struct got_fileindex_entry *ie)
154 free(ie->path);
155 free(ie);
158 size_t
159 got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
161 return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
164 uint32_t
165 got_fileindex_entry_stage_get(const struct got_fileindex_entry *ie)
167 return ((ie->flags & GOT_FILEIDX_F_STAGE) >> GOT_FILEIDX_F_STAGE_SHIFT);
170 void
171 got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage)
173 ie->flags &= ~GOT_FILEIDX_F_STAGE;
174 ie->flags |= ((stage << GOT_FILEIDX_F_STAGE_SHIFT) &
175 GOT_FILEIDX_F_STAGE);
178 int
179 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
181 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
184 int
185 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
187 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
190 int
191 got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
193 return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
196 static const struct got_error *
197 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
199 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
200 return got_error(GOT_ERR_NO_SPACE);
202 RB_INSERT(got_fileindex_tree, &fileindex->entries, ie);
203 fileindex->nentries++;
204 return NULL;
207 const struct got_error *
208 got_fileindex_entry_add(struct got_fileindex *fileindex,
209 struct got_fileindex_entry *ie)
211 /* Flag this entry until it gets written out to disk. */
212 ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
214 return add_entry(fileindex, ie);
217 void
218 got_fileindex_entry_remove(struct got_fileindex *fileindex,
219 struct got_fileindex_entry *ie)
221 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
222 fileindex->nentries--;
225 struct got_fileindex_entry *
226 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
227 size_t path_len)
229 struct got_fileindex_entry key;
230 memset(&key, 0, sizeof(key));
231 key.path = (char *)path;
232 key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
233 return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
236 const struct got_error *
237 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
238 got_fileindex_cb cb, void *cb_arg)
240 const struct got_error *err;
241 struct got_fileindex_entry *ie, *tmp;
243 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
244 err = (*cb)(cb_arg, ie);
245 if (err)
246 return err;
248 return NULL;
251 struct got_fileindex *
252 got_fileindex_alloc(void)
254 struct got_fileindex *fileindex;
256 fileindex = calloc(1, sizeof(*fileindex));
257 if (fileindex == NULL)
258 return NULL;
260 RB_INIT(&fileindex->entries);
261 return fileindex;
264 void
265 got_fileindex_free(struct got_fileindex *fileindex)
267 struct got_fileindex_entry *ie;
269 while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
270 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
271 got_fileindex_entry_free(ie);
273 free(fileindex);
276 static const struct got_error *
277 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
279 size_t n;
281 val = htobe64(val);
282 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
283 n = fwrite(&val, 1, sizeof(val), outfile);
284 if (n != sizeof(val))
285 return got_ferror(outfile, GOT_ERR_IO);
286 return NULL;
289 static const struct got_error *
290 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
292 size_t n;
294 val = htobe32(val);
295 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
296 n = fwrite(&val, 1, sizeof(val), outfile);
297 if (n != sizeof(val))
298 return got_ferror(outfile, GOT_ERR_IO);
299 return NULL;
302 static const struct got_error *
303 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
305 size_t n;
307 val = htobe16(val);
308 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
309 n = fwrite(&val, 1, sizeof(val), outfile);
310 if (n != sizeof(val))
311 return got_ferror(outfile, GOT_ERR_IO);
312 return NULL;
315 static const struct got_error *
316 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
318 size_t n, len, pad = 0;
319 static const uint8_t zero[8] = { 0 };
321 len = strlen(path);
322 while ((len + pad) % 8 != 0)
323 pad++;
324 if (pad == 0)
325 pad = 8; /* NUL-terminate */
327 SHA1Update(ctx, path, len);
328 n = fwrite(path, 1, len, outfile);
329 if (n != len)
330 return got_ferror(outfile, GOT_ERR_IO);
331 SHA1Update(ctx, zero, pad);
332 n = fwrite(zero, 1, pad, outfile);
333 if (n != pad)
334 return got_ferror(outfile, GOT_ERR_IO);
335 return NULL;
338 static const struct got_error *
339 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
340 FILE *outfile)
342 const struct got_error *err;
343 size_t n;
344 uint32_t stage;
346 err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
347 if (err)
348 return err;
349 err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
350 if (err)
351 return err;
352 err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
353 if (err)
354 return err;
355 err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
356 if (err)
357 return err;
359 err = write_fileindex_val32(ctx, ie->uid, outfile);
360 if (err)
361 return err;
362 err = write_fileindex_val32(ctx, ie->gid, outfile);
363 if (err)
364 return err;
365 err = write_fileindex_val32(ctx, ie->size, outfile);
366 if (err)
367 return err;
369 err = write_fileindex_val16(ctx, ie->mode, outfile);
370 if (err)
371 return err;
373 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
374 n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
375 if (n != SHA1_DIGEST_LENGTH)
376 return got_ferror(outfile, GOT_ERR_IO);
378 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
379 n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
380 if (n != SHA1_DIGEST_LENGTH)
381 return got_ferror(outfile, GOT_ERR_IO);
383 err = write_fileindex_val32(ctx, ie->flags, outfile);
384 if (err)
385 return err;
387 err = write_fileindex_path(ctx, ie->path, outfile);
388 if (err)
389 return err;
391 stage = got_fileindex_entry_stage_get(ie);
392 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
393 stage == GOT_FILEIDX_STAGE_ADD) {
394 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
395 n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
396 outfile);
397 if (n != SHA1_DIGEST_LENGTH)
398 return got_ferror(outfile, GOT_ERR_IO);
401 return NULL;
404 const struct got_error *
405 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
407 const struct got_error *err = NULL;
408 struct got_fileindex_hdr hdr;
409 SHA1_CTX ctx;
410 uint8_t sha1[SHA1_DIGEST_LENGTH];
411 size_t n;
412 struct got_fileindex_entry *ie;
414 SHA1Init(&ctx);
416 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
417 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
418 hdr.nentries = htobe32(fileindex->nentries);
420 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
421 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
422 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
423 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
424 if (n != sizeof(hdr.signature))
425 return got_ferror(outfile, GOT_ERR_IO);
426 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
427 if (n != sizeof(hdr.version))
428 return got_ferror(outfile, GOT_ERR_IO);
429 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
430 if (n != sizeof(hdr.nentries))
431 return got_ferror(outfile, GOT_ERR_IO);
433 RB_FOREACH(ie, got_fileindex_tree, &fileindex->entries) {
434 ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
435 err = write_fileindex_entry(&ctx, ie, outfile);
436 if (err)
437 return err;
440 SHA1Final(sha1, &ctx);
441 n = fwrite(sha1, 1, sizeof(sha1), outfile);
442 if (n != sizeof(sha1))
443 return got_ferror(outfile, GOT_ERR_IO);
445 if (fflush(outfile) != 0)
446 return got_error_from_errno("fflush");
448 return NULL;
451 static const struct got_error *
452 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
454 size_t n;
456 n = fread(val, 1, sizeof(*val), infile);
457 if (n != sizeof(*val))
458 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
459 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
460 *val = be64toh(*val);
461 return NULL;
464 static const struct got_error *
465 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
467 size_t n;
469 n = fread(val, 1, sizeof(*val), infile);
470 if (n != sizeof(*val))
471 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
472 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
473 *val = be32toh(*val);
474 return NULL;
477 static const struct got_error *
478 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
480 size_t n;
482 n = fread(val, 1, sizeof(*val), infile);
483 if (n != sizeof(*val))
484 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
485 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
486 *val = be16toh(*val);
487 return NULL;
490 static const struct got_error *
491 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
493 const struct got_error *err = NULL;
494 const size_t chunk_size = 8;
495 size_t n, len = 0, totlen = chunk_size;
497 *path = malloc(totlen);
498 if (*path == NULL)
499 return got_error_from_errno("malloc");
501 do {
502 if (len + chunk_size > totlen) {
503 char *p = reallocarray(*path, totlen + chunk_size, 1);
504 if (p == NULL) {
505 err = got_error_from_errno("reallocarray");
506 break;
508 totlen += chunk_size;
509 *path = p;
511 n = fread(*path + len, 1, chunk_size, infile);
512 if (n != chunk_size) {
513 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
514 break;
516 SHA1Update(ctx, *path + len, chunk_size);
517 len += chunk_size;
518 } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
520 if (err) {
521 free(*path);
522 *path = NULL;
524 return err;
527 static const struct got_error *
528 read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
529 FILE *infile, uint32_t version)
531 const struct got_error *err;
532 struct got_fileindex_entry *ie;
533 size_t n;
535 *iep = NULL;
537 ie = calloc(1, sizeof(*ie));
538 if (ie == NULL)
539 return got_error_from_errno("calloc");
541 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
542 if (err)
543 goto done;
544 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
545 if (err)
546 goto done;
547 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
548 if (err)
549 goto done;
550 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
551 if (err)
552 goto done;
554 err = read_fileindex_val32(&ie->uid, ctx, infile);
555 if (err)
556 goto done;
557 err = read_fileindex_val32(&ie->gid, ctx, infile);
558 if (err)
559 goto done;
560 err = read_fileindex_val32(&ie->size, ctx, infile);
561 if (err)
562 goto done;
564 err = read_fileindex_val16(&ie->mode, ctx, infile);
565 if (err)
566 goto done;
568 n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
569 if (n != SHA1_DIGEST_LENGTH) {
570 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
571 goto done;
573 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
575 n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
576 if (n != SHA1_DIGEST_LENGTH) {
577 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
578 goto done;
580 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
582 err = read_fileindex_val32(&ie->flags, ctx, infile);
583 if (err)
584 goto done;
586 err = read_fileindex_path(&ie->path, ctx, infile);
587 if (err)
588 goto done;
590 if (version >= 2) {
591 uint32_t stage = got_fileindex_entry_stage_get(ie);
592 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
593 stage == GOT_FILEIDX_STAGE_ADD) {
594 n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
595 infile);
596 if (n != SHA1_DIGEST_LENGTH) {
597 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
598 goto done;
600 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
602 } else {
603 /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
604 ie->flags &= ~GOT_FILEIDX_F_STAGE;
607 done:
608 if (err)
609 got_fileindex_entry_free(ie);
610 else
611 *iep = ie;
612 return err;
615 const struct got_error *
616 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
618 const struct got_error *err = NULL;
619 struct got_fileindex_hdr hdr;
620 SHA1_CTX ctx;
621 struct got_fileindex_entry *ie;
622 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
623 uint8_t sha1[SHA1_DIGEST_LENGTH];
624 size_t n;
625 int i;
627 SHA1Init(&ctx);
629 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
630 if (n != sizeof(hdr.signature)) {
631 if (n == 0) /* EOF */
632 return NULL;
633 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
635 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
636 if (n != sizeof(hdr.version)) {
637 if (n == 0) /* EOF */
638 return NULL;
639 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
641 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
642 if (n != sizeof(hdr.nentries)) {
643 if (n == 0) /* EOF */
644 return NULL;
645 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
648 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
649 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
650 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
652 hdr.signature = be32toh(hdr.signature);
653 hdr.version = be32toh(hdr.version);
654 hdr.nentries = be32toh(hdr.nentries);
656 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
657 return got_error(GOT_ERR_FILEIDX_SIG);
658 if (hdr.version > GOT_FILE_INDEX_VERSION)
659 return got_error(GOT_ERR_FILEIDX_VER);
661 for (i = 0; i < hdr.nentries; i++) {
662 err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
663 if (err)
664 return err;
665 err = add_entry(fileindex, ie);
666 if (err)
667 return err;
670 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
671 if (n != sizeof(sha1_expected))
672 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
673 SHA1Final(sha1, &ctx);
674 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
675 return got_error(GOT_ERR_FILEIDX_CSUM);
677 return NULL;
680 static struct got_fileindex_entry *
681 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
683 struct got_fileindex_entry *next;
685 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
687 /* Skip entries which were newly added by diff callbacks. */
688 while (next && (next->flags & GOT_FILEIDX_F_NOT_FLUSHED))
689 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
691 return next;
694 static const struct got_error *
695 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
696 const struct got_tree_entries *, const char *, const char *,
697 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
699 static const struct got_error *
700 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
701 struct got_fileindex_entry **ie, struct got_tree_entry *te,
702 const char *path, const char *entry_name, struct got_repository *repo,
703 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
705 const struct got_error *err = NULL;
707 if (!got_object_tree_entry_is_submodule(te) && S_ISDIR(te->mode)) {
708 char *subpath;
709 struct got_tree_object *subtree;
711 if (asprintf(&subpath, "%s%s%s", path,
712 path[0] == '\0' ? "" : "/", te->name) == -1)
713 return got_error_from_errno("asprintf");
715 err = got_object_open_as_tree(&subtree, repo, te->id);
716 if (err) {
717 free(subpath);
718 return err;
721 err = diff_fileindex_tree(fileindex, ie,
722 got_object_tree_get_entries(subtree), subpath, entry_name,
723 repo, cb, cb_arg);
724 free(subpath);
725 got_object_tree_close(subtree);
726 if (err)
727 return err;
730 *next = SIMPLEQ_NEXT(te, entry);
731 return NULL;
734 static const struct got_error *
735 diff_fileindex_tree(struct got_fileindex *fileindex,
736 struct got_fileindex_entry **ie, const struct got_tree_entries *entries,
737 const char *path, const char *entry_name, struct got_repository *repo,
738 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
740 const struct got_error *err = NULL;
741 struct got_tree_entry *te = NULL;
742 size_t path_len = strlen(path);
743 struct got_fileindex_entry *next;
745 te = SIMPLEQ_FIRST(&entries->head);
746 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
747 if (te && *ie) {
748 char *te_path;
749 int cmp;
750 if (asprintf(&te_path, "%s/%s", path, te->name) == -1) {
751 err = got_error_from_errno("asprintf");
752 break;
754 cmp = got_path_cmp((*ie)->path, te_path,
755 got_fileindex_entry_path_len(*ie), strlen(te_path));
756 free(te_path);
757 if (cmp == 0) {
758 if (got_path_is_child((*ie)->path, path,
759 path_len) &&
760 !got_object_tree_entry_is_submodule(te) &&
761 (entry_name == NULL ||
762 strcmp(te->name, entry_name) == 0)) {
763 err = cb->diff_old_new(cb_arg, *ie, te,
764 path);
765 if (err || entry_name)
766 break;
768 *ie = walk_fileindex(fileindex, *ie);
769 err = walk_tree(&te, fileindex, ie, te,
770 path, entry_name, repo, cb, cb_arg);
771 } else if (cmp < 0) {
772 next = walk_fileindex(fileindex, *ie);
773 if (got_path_is_child((*ie)->path, path,
774 path_len) && (entry_name == NULL ||
775 strcmp(te->name, entry_name) == 0)) {
776 err = cb->diff_old(cb_arg, *ie, path);
777 if (err || entry_name)
778 break;
780 *ie = next;
781 } else {
782 if ((entry_name == NULL ||
783 strcmp(te->name, entry_name) == 0)) {
784 err = cb->diff_new(cb_arg, te, path);
785 if (err || entry_name)
786 break;
788 err = walk_tree(&te, fileindex, ie, te,
789 path, entry_name, repo, cb, cb_arg);
791 if (err)
792 break;
793 } else if (*ie) {
794 next = walk_fileindex(fileindex, *ie);
795 if (got_path_is_child((*ie)->path, path, path_len) &&
796 (entry_name == NULL ||
797 (te && strcmp(te->name, entry_name) == 0))) {
798 err = cb->diff_old(cb_arg, *ie, path);
799 if (err || entry_name)
800 break;
802 *ie = next;
803 } else if (te) {
804 if (!got_object_tree_entry_is_submodule(te) &&
805 (entry_name == NULL ||
806 strcmp(te->name, entry_name) == 0)) {
807 err = cb->diff_new(cb_arg, te, path);
808 if (err || entry_name)
809 break;
811 err = walk_tree(&te, fileindex, ie, te, path,
812 entry_name, repo, cb, cb_arg);
813 if (err)
814 break;
818 return err;
821 const struct got_error *
822 got_fileindex_diff_tree(struct got_fileindex *fileindex,
823 struct got_tree_object *tree, const char *path, const char *entry_name,
824 struct got_repository *repo,
825 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
827 struct got_fileindex_entry *ie;
828 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
829 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
830 ie = walk_fileindex(fileindex, ie);
831 return diff_fileindex_tree(fileindex, &ie,
832 got_object_tree_get_entries(tree), path, entry_name, repo,
833 cb, cb_arg);
836 static const struct got_error *
837 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
838 struct got_pathlist_head *, const char *, const char *,
839 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
841 static const struct got_error *
842 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
844 const struct got_error *err = NULL;
845 struct got_pathlist_entry *new = NULL;
846 struct dirent *dep = NULL;
847 struct dirent *de = NULL;
849 for (;;) {
850 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
851 if (de == NULL) {
852 err = got_error_from_errno("malloc");
853 break;
856 if (readdir_r(dir, de, &dep) != 0) {
857 err = got_error_from_errno("readdir_r");
858 free(de);
859 break;
861 if (dep == NULL) {
862 free(de);
863 break;
866 if (strcmp(de->d_name, ".") == 0 ||
867 strcmp(de->d_name, "..") == 0 ||
868 (path[0] == '\0' &&
869 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
870 free(de);
871 continue;
874 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
875 if (err) {
876 free(de);
877 break;
879 if (new == NULL) {
880 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
881 free(de);
882 break;
886 return err;
889 void
890 free_dirlist(struct got_pathlist_head *dirlist)
892 struct got_pathlist_entry *dle;
894 TAILQ_FOREACH(dle, dirlist, entry)
895 free(dle->data);
896 got_pathlist_free(dirlist);
899 static const struct got_error *
900 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
901 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle,
902 const char *path, const char *rootpath, struct got_repository *repo,
903 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
905 const struct got_error *err = NULL;
906 struct dirent *de = dle->data;
908 *next = NULL;
910 if (de->d_type == DT_DIR) {
911 char *subpath;
912 char *subdirpath;
913 DIR *subdir;
914 struct got_pathlist_head subdirlist;
916 TAILQ_INIT(&subdirlist);
918 if (asprintf(&subpath, "%s%s%s", path,
919 path[0] == '\0' ? "" : "/", de->d_name) == -1)
920 return got_error_from_errno("asprintf");
922 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
923 free(subpath);
924 return got_error_from_errno("asprintf");
927 subdir = opendir(subdirpath);
928 if (subdir == NULL) {
929 if (errno == EACCES) {
930 *next = TAILQ_NEXT(dle, entry);
931 return NULL;
933 err = got_error_from_errno2("opendir", subdirpath);
934 free(subpath);
935 free(subdirpath);
936 return err;
939 err = read_dirlist(&subdirlist, subdir, subdirpath);
940 if (err) {
941 free(subpath);
942 free(subdirpath);
943 closedir(subdir);
944 return err;
946 err = diff_fileindex_dir(fileindex, ie, &subdirlist, rootpath,
947 subpath, repo, cb, cb_arg);
948 free(subpath);
949 free(subdirpath);
950 closedir(subdir);
951 free_dirlist(&subdirlist);
952 if (err)
953 return err;
956 *next = TAILQ_NEXT(dle, entry);
957 return NULL;
960 static const struct got_error *
961 diff_fileindex_dir(struct got_fileindex *fileindex,
962 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
963 const char *rootpath, const char *path, struct got_repository *repo,
964 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
966 const struct got_error *err = NULL;
967 struct dirent *de = NULL;
968 size_t path_len = strlen(path);
969 struct got_pathlist_entry *dle;
971 dle = TAILQ_FIRST(dirlist);
972 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
973 if (dle && *ie) {
974 char *de_path;
975 int cmp;
976 de = dle->data;
977 if (asprintf(&de_path, "%s/%s", path,
978 de->d_name) == -1) {
979 err = got_error_from_errno("asprintf");
980 break;
982 cmp = got_path_cmp((*ie)->path, de_path,
983 got_fileindex_entry_path_len(*ie),
984 strlen(path) + 1 + de->d_namlen);
985 free(de_path);
986 if (cmp == 0) {
987 err = cb->diff_old_new(cb_arg, *ie, de, path);
988 if (err)
989 break;
990 *ie = walk_fileindex(fileindex, *ie);
991 err = walk_dir(&dle, fileindex, ie, dle, path,
992 rootpath, repo, cb, cb_arg);
993 } else if (cmp < 0 ) {
994 err = cb->diff_old(cb_arg, *ie, path);
995 if (err)
996 break;
997 *ie = walk_fileindex(fileindex, *ie);
998 } else {
999 err = cb->diff_new(cb_arg, de, path);
1000 if (err)
1001 break;
1002 err = walk_dir(&dle, fileindex, ie, dle, path,
1003 rootpath, repo, cb, cb_arg);
1005 if (err)
1006 break;
1007 } else if (*ie) {
1008 err = cb->diff_old(cb_arg, *ie, path);
1009 if (err)
1010 break;
1011 *ie = walk_fileindex(fileindex, *ie);
1012 } else if (dle) {
1013 de = dle->data;
1014 err = cb->diff_new(cb_arg, de, path);
1015 if (err)
1016 break;
1017 err = walk_dir(&dle, fileindex, ie, dle, path,
1018 rootpath, repo, cb, cb_arg);
1019 if (err)
1020 break;
1024 return err;
1027 const struct got_error *
1028 got_fileindex_diff_dir(struct got_fileindex *fileindex, DIR *rootdir,
1029 const char *rootpath, const char *path, struct got_repository *repo,
1030 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1032 const struct got_error *err;
1033 struct got_fileindex_entry *ie;
1034 struct got_pathlist_head dirlist;
1036 TAILQ_INIT(&dirlist);
1037 err = read_dirlist(&dirlist, rootdir, path);
1038 if (err)
1039 return err;
1040 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1041 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1042 ie = walk_fileindex(fileindex, ie);
1043 err = diff_fileindex_dir(fileindex, &ie, &dirlist, rootpath, path,
1044 repo, cb, cb_arg);
1045 free_dirlist(&dirlist);
1046 return err;
1049 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);