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 <fcntl.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sha1.h>
28 #include <endian.h>
29 #include <limits.h>
30 #include <unistd.h>
31 #include <uuid.h>
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_path.h"
37 #include "got_lib_fileindex.h"
38 #include "got_lib_worktree.h"
40 /* got_fileindex_entry flags */
41 #define GOT_FILEIDX_F_PATH_LEN 0x00000fff
42 #define GOT_FILEIDX_F_STAGE 0x0000f000
43 #define GOT_FILEIDX_F_STAGE_SHIFT 12
44 #define GOT_FILEIDX_F_NOT_FLUSHED 0x00010000
45 #define GOT_FILEIDX_F_NO_BLOB 0x00020000
46 #define GOT_FILEIDX_F_NO_COMMIT 0x00040000
47 #define GOT_FILEIDX_F_NO_FILE_ON_DISK 0x00080000
49 struct got_fileindex {
50 struct got_fileindex_tree entries;
51 int nentries;
52 #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
53 };
55 uint16_t
56 got_fileindex_perms_from_st(struct stat *sb)
57 {
58 uint16_t perms = (sb->st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
59 return (perms << GOT_FILEIDX_MODE_PERMS_SHIFT);
60 }
62 mode_t
63 got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
64 {
65 mode_t perms = (ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT);
66 return (S_IFREG | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
67 }
69 const struct got_error *
70 got_fileindex_entry_update(struct got_fileindex_entry *ie,
71 const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
72 int update_timestamps)
73 {
74 struct stat sb;
76 if (lstat(ondisk_path, &sb) != 0) {
77 if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
78 errno == ENOENT))
79 return got_error_from_errno2("lstat", ondisk_path);
80 } else {
81 if (sb.st_mode & S_IFDIR)
82 return got_error_set_errno(EISDIR, ondisk_path);
83 ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
84 }
87 if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
88 if (update_timestamps) {
89 ie->ctime_sec = sb.st_ctime;
90 ie->ctime_nsec = sb.st_ctimensec;
91 ie->mtime_sec = sb.st_mtime;
92 ie->mtime_nsec = sb.st_mtimensec;
93 }
94 ie->uid = sb.st_uid;
95 ie->gid = sb.st_gid;
96 ie->size = (sb.st_size & 0xffffffff);
97 if (sb.st_mode & S_IFLNK)
98 ie->mode = GOT_FILEIDX_MODE_SYMLINK;
99 else
100 ie->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
101 ie->mode |= got_fileindex_perms_from_st(&sb);
104 if (blob_sha1) {
105 memcpy(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
106 ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
107 } else
108 ie->flags |= GOT_FILEIDX_F_NO_BLOB;
110 if (commit_sha1) {
111 memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
112 ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
113 } else
114 ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
116 return NULL;
119 void
120 got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
122 ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
125 const struct got_error *
126 got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
127 const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
128 uint8_t *commit_sha1)
130 size_t len;
132 *ie = calloc(1, sizeof(**ie));
133 if (*ie == NULL)
134 return got_error_from_errno("calloc");
136 (*ie)->path = strdup(relpath);
137 if ((*ie)->path == NULL) {
138 const struct got_error *err = got_error_from_errno("strdup");
139 free(*ie);
140 *ie = NULL;
141 return err;
144 len = strlen(relpath);
145 if (len > GOT_FILEIDX_F_PATH_LEN)
146 len = GOT_FILEIDX_F_PATH_LEN;
147 (*ie)->flags |= len;
149 return got_fileindex_entry_update(*ie, ondisk_path, blob_sha1,
150 commit_sha1, 1);
153 void
154 got_fileindex_entry_free(struct got_fileindex_entry *ie)
156 free(ie->path);
157 free(ie);
160 size_t
161 got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
163 return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
166 uint32_t
167 got_fileindex_entry_stage_get(const struct got_fileindex_entry *ie)
169 return ((ie->flags & GOT_FILEIDX_F_STAGE) >> GOT_FILEIDX_F_STAGE_SHIFT);
172 void
173 got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage)
175 ie->flags &= ~GOT_FILEIDX_F_STAGE;
176 ie->flags |= ((stage << GOT_FILEIDX_F_STAGE_SHIFT) &
177 GOT_FILEIDX_F_STAGE);
180 int
181 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
183 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
186 int
187 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
189 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
192 int
193 got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
195 return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
198 static const struct got_error *
199 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
201 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
202 return got_error(GOT_ERR_NO_SPACE);
204 RB_INSERT(got_fileindex_tree, &fileindex->entries, ie);
205 fileindex->nentries++;
206 return NULL;
209 const struct got_error *
210 got_fileindex_entry_add(struct got_fileindex *fileindex,
211 struct got_fileindex_entry *ie)
213 /* Flag this entry until it gets written out to disk. */
214 ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
216 return add_entry(fileindex, ie);
219 void
220 got_fileindex_entry_remove(struct got_fileindex *fileindex,
221 struct got_fileindex_entry *ie)
223 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
224 fileindex->nentries--;
227 struct got_fileindex_entry *
228 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
229 size_t path_len)
231 struct got_fileindex_entry key;
232 memset(&key, 0, sizeof(key));
233 key.path = (char *)path;
234 key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
235 return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
238 const struct got_error *
239 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
240 got_fileindex_cb cb, void *cb_arg)
242 const struct got_error *err;
243 struct got_fileindex_entry *ie, *tmp;
245 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
246 err = (*cb)(cb_arg, ie);
247 if (err)
248 return err;
250 return NULL;
253 struct got_fileindex *
254 got_fileindex_alloc(void)
256 struct got_fileindex *fileindex;
258 fileindex = calloc(1, sizeof(*fileindex));
259 if (fileindex == NULL)
260 return NULL;
262 RB_INIT(&fileindex->entries);
263 return fileindex;
266 void
267 got_fileindex_free(struct got_fileindex *fileindex)
269 struct got_fileindex_entry *ie;
271 while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
272 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
273 got_fileindex_entry_free(ie);
275 free(fileindex);
278 static const struct got_error *
279 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
281 size_t n;
283 val = htobe64(val);
284 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
285 n = fwrite(&val, 1, sizeof(val), outfile);
286 if (n != sizeof(val))
287 return got_ferror(outfile, GOT_ERR_IO);
288 return NULL;
291 static const struct got_error *
292 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
294 size_t n;
296 val = htobe32(val);
297 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
298 n = fwrite(&val, 1, sizeof(val), outfile);
299 if (n != sizeof(val))
300 return got_ferror(outfile, GOT_ERR_IO);
301 return NULL;
304 static const struct got_error *
305 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
307 size_t n;
309 val = htobe16(val);
310 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
311 n = fwrite(&val, 1, sizeof(val), outfile);
312 if (n != sizeof(val))
313 return got_ferror(outfile, GOT_ERR_IO);
314 return NULL;
317 static const struct got_error *
318 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
320 size_t n, len, pad = 0;
321 static const uint8_t zero[8] = { 0 };
323 len = strlen(path);
324 while ((len + pad) % 8 != 0)
325 pad++;
326 if (pad == 0)
327 pad = 8; /* NUL-terminate */
329 SHA1Update(ctx, path, len);
330 n = fwrite(path, 1, len, outfile);
331 if (n != len)
332 return got_ferror(outfile, GOT_ERR_IO);
333 SHA1Update(ctx, zero, pad);
334 n = fwrite(zero, 1, pad, outfile);
335 if (n != pad)
336 return got_ferror(outfile, GOT_ERR_IO);
337 return NULL;
340 static const struct got_error *
341 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
342 FILE *outfile)
344 const struct got_error *err;
345 size_t n;
346 uint32_t stage;
348 err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
349 if (err)
350 return err;
351 err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
352 if (err)
353 return err;
354 err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
355 if (err)
356 return err;
357 err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
358 if (err)
359 return err;
361 err = write_fileindex_val32(ctx, ie->uid, outfile);
362 if (err)
363 return err;
364 err = write_fileindex_val32(ctx, ie->gid, outfile);
365 if (err)
366 return err;
367 err = write_fileindex_val32(ctx, ie->size, outfile);
368 if (err)
369 return err;
371 err = write_fileindex_val16(ctx, ie->mode, outfile);
372 if (err)
373 return err;
375 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
376 n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
377 if (n != SHA1_DIGEST_LENGTH)
378 return got_ferror(outfile, GOT_ERR_IO);
380 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
381 n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
382 if (n != SHA1_DIGEST_LENGTH)
383 return got_ferror(outfile, GOT_ERR_IO);
385 err = write_fileindex_val32(ctx, ie->flags, outfile);
386 if (err)
387 return err;
389 err = write_fileindex_path(ctx, ie->path, outfile);
390 if (err)
391 return err;
393 stage = got_fileindex_entry_stage_get(ie);
394 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
395 stage == GOT_FILEIDX_STAGE_ADD) {
396 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
397 n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
398 outfile);
399 if (n != SHA1_DIGEST_LENGTH)
400 return got_ferror(outfile, GOT_ERR_IO);
403 return NULL;
406 const struct got_error *
407 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
409 const struct got_error *err = NULL;
410 struct got_fileindex_hdr hdr;
411 SHA1_CTX ctx;
412 uint8_t sha1[SHA1_DIGEST_LENGTH];
413 size_t n;
414 struct got_fileindex_entry *ie;
416 SHA1Init(&ctx);
418 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
419 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
420 hdr.nentries = htobe32(fileindex->nentries);
422 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
423 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
424 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
425 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
426 if (n != sizeof(hdr.signature))
427 return got_ferror(outfile, GOT_ERR_IO);
428 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
429 if (n != sizeof(hdr.version))
430 return got_ferror(outfile, GOT_ERR_IO);
431 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
432 if (n != sizeof(hdr.nentries))
433 return got_ferror(outfile, GOT_ERR_IO);
435 RB_FOREACH(ie, got_fileindex_tree, &fileindex->entries) {
436 ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
437 err = write_fileindex_entry(&ctx, ie, outfile);
438 if (err)
439 return err;
442 SHA1Final(sha1, &ctx);
443 n = fwrite(sha1, 1, sizeof(sha1), outfile);
444 if (n != sizeof(sha1))
445 return got_ferror(outfile, GOT_ERR_IO);
447 if (fflush(outfile) != 0)
448 return got_error_from_errno("fflush");
450 return NULL;
453 static const struct got_error *
454 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
456 size_t n;
458 n = fread(val, 1, sizeof(*val), infile);
459 if (n != sizeof(*val))
460 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
461 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
462 *val = be64toh(*val);
463 return NULL;
466 static const struct got_error *
467 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
469 size_t n;
471 n = fread(val, 1, sizeof(*val), infile);
472 if (n != sizeof(*val))
473 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
474 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
475 *val = be32toh(*val);
476 return NULL;
479 static const struct got_error *
480 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
482 size_t n;
484 n = fread(val, 1, sizeof(*val), infile);
485 if (n != sizeof(*val))
486 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
487 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
488 *val = be16toh(*val);
489 return NULL;
492 static const struct got_error *
493 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
495 const struct got_error *err = NULL;
496 const size_t chunk_size = 8;
497 size_t n, len = 0, totlen = chunk_size;
499 *path = malloc(totlen);
500 if (*path == NULL)
501 return got_error_from_errno("malloc");
503 do {
504 if (len + chunk_size > totlen) {
505 char *p = reallocarray(*path, totlen + chunk_size, 1);
506 if (p == NULL) {
507 err = got_error_from_errno("reallocarray");
508 break;
510 totlen += chunk_size;
511 *path = p;
513 n = fread(*path + len, 1, chunk_size, infile);
514 if (n != chunk_size) {
515 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
516 break;
518 SHA1Update(ctx, *path + len, chunk_size);
519 len += chunk_size;
520 } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
522 if (err) {
523 free(*path);
524 *path = NULL;
526 return err;
529 static const struct got_error *
530 read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
531 FILE *infile, uint32_t version)
533 const struct got_error *err;
534 struct got_fileindex_entry *ie;
535 size_t n;
537 *iep = NULL;
539 ie = calloc(1, sizeof(*ie));
540 if (ie == NULL)
541 return got_error_from_errno("calloc");
543 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
544 if (err)
545 goto done;
546 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
547 if (err)
548 goto done;
549 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
550 if (err)
551 goto done;
552 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
553 if (err)
554 goto done;
556 err = read_fileindex_val32(&ie->uid, ctx, infile);
557 if (err)
558 goto done;
559 err = read_fileindex_val32(&ie->gid, ctx, infile);
560 if (err)
561 goto done;
562 err = read_fileindex_val32(&ie->size, ctx, infile);
563 if (err)
564 goto done;
566 err = read_fileindex_val16(&ie->mode, ctx, infile);
567 if (err)
568 goto done;
570 n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
571 if (n != SHA1_DIGEST_LENGTH) {
572 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
573 goto done;
575 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
577 n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
578 if (n != SHA1_DIGEST_LENGTH) {
579 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
580 goto done;
582 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
584 err = read_fileindex_val32(&ie->flags, ctx, infile);
585 if (err)
586 goto done;
588 err = read_fileindex_path(&ie->path, ctx, infile);
589 if (err)
590 goto done;
592 if (version >= 2) {
593 uint32_t stage = got_fileindex_entry_stage_get(ie);
594 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
595 stage == GOT_FILEIDX_STAGE_ADD) {
596 n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
597 infile);
598 if (n != SHA1_DIGEST_LENGTH) {
599 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
600 goto done;
602 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
604 } else {
605 /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
606 ie->flags &= ~GOT_FILEIDX_F_STAGE;
609 done:
610 if (err)
611 got_fileindex_entry_free(ie);
612 else
613 *iep = ie;
614 return err;
617 const struct got_error *
618 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
620 const struct got_error *err = NULL;
621 struct got_fileindex_hdr hdr;
622 SHA1_CTX ctx;
623 struct got_fileindex_entry *ie;
624 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
625 uint8_t sha1[SHA1_DIGEST_LENGTH];
626 size_t n;
627 int i;
629 SHA1Init(&ctx);
631 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
632 if (n != sizeof(hdr.signature)) {
633 if (n == 0) /* EOF */
634 return NULL;
635 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
637 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
638 if (n != sizeof(hdr.version)) {
639 if (n == 0) /* EOF */
640 return NULL;
641 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
643 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
644 if (n != sizeof(hdr.nentries)) {
645 if (n == 0) /* EOF */
646 return NULL;
647 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
650 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
651 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
652 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
654 hdr.signature = be32toh(hdr.signature);
655 hdr.version = be32toh(hdr.version);
656 hdr.nentries = be32toh(hdr.nentries);
658 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
659 return got_error(GOT_ERR_FILEIDX_SIG);
660 if (hdr.version > GOT_FILE_INDEX_VERSION)
661 return got_error(GOT_ERR_FILEIDX_VER);
663 for (i = 0; i < hdr.nentries; i++) {
664 err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
665 if (err)
666 return err;
667 err = add_entry(fileindex, ie);
668 if (err)
669 return err;
672 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
673 if (n != sizeof(sha1_expected))
674 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
675 SHA1Final(sha1, &ctx);
676 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
677 return got_error(GOT_ERR_FILEIDX_CSUM);
679 return NULL;
682 static struct got_fileindex_entry *
683 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
685 struct got_fileindex_entry *next;
687 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
689 /* Skip entries which were newly added by diff callbacks. */
690 while (next && (next->flags & GOT_FILEIDX_F_NOT_FLUSHED))
691 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
693 return next;
696 static const struct got_error *
697 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
698 struct got_tree_object *tree, const char *, const char *,
699 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
701 static const struct got_error *
702 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
703 struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
704 const char *path, const char *entry_name, struct got_repository *repo,
705 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
707 const struct got_error *err = NULL;
708 struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
710 if (!got_object_tree_entry_is_submodule(te) &&
711 S_ISDIR(got_tree_entry_get_mode(te))) {
712 char *subpath;
713 struct got_tree_object *subtree;
715 if (asprintf(&subpath, "%s%s%s", path,
716 path[0] == '\0' ? "" : "/",
717 got_tree_entry_get_name(te)) == -1)
718 return got_error_from_errno("asprintf");
720 err = got_object_open_as_tree(&subtree, repo,
721 got_tree_entry_get_id(te));
722 if (err) {
723 free(subpath);
724 return err;
727 err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
728 entry_name, repo, cb, cb_arg);
729 free(subpath);
730 got_object_tree_close(subtree);
731 if (err)
732 return err;
735 (*tidx)++;
736 *next = got_object_tree_get_entry(tree, *tidx);
737 return NULL;
740 static const struct got_error *
741 diff_fileindex_tree(struct got_fileindex *fileindex,
742 struct got_fileindex_entry **ie, struct got_tree_object *tree,
743 const char *path, const char *entry_name, struct got_repository *repo,
744 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
746 const struct got_error *err = NULL;
747 struct got_tree_entry *te = NULL;
748 size_t path_len = strlen(path);
749 struct got_fileindex_entry *next;
750 int tidx = 0;
752 te = got_object_tree_get_entry(tree, tidx);
753 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
754 if (te && *ie) {
755 char *te_path;
756 const char *te_name = got_tree_entry_get_name(te);
757 int cmp;
758 if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
759 err = got_error_from_errno("asprintf");
760 break;
762 cmp = got_path_cmp((*ie)->path, te_path,
763 got_fileindex_entry_path_len(*ie), strlen(te_path));
764 free(te_path);
765 if (cmp == 0) {
766 if (got_path_is_child((*ie)->path, path,
767 path_len) &&
768 !got_object_tree_entry_is_submodule(te) &&
769 (entry_name == NULL ||
770 strcmp(te_name, entry_name) == 0)) {
771 err = cb->diff_old_new(cb_arg, *ie, te,
772 path);
773 if (err || entry_name)
774 break;
776 *ie = walk_fileindex(fileindex, *ie);
777 err = walk_tree(&te, fileindex, ie, tree, &tidx,
778 path, entry_name, repo, cb, cb_arg);
779 } else if (cmp < 0) {
780 next = walk_fileindex(fileindex, *ie);
781 if (got_path_is_child((*ie)->path, path,
782 path_len) && (entry_name == NULL ||
783 strcmp(te_name, entry_name) == 0)) {
784 err = cb->diff_old(cb_arg, *ie, path);
785 if (err || entry_name)
786 break;
788 *ie = next;
789 } else {
790 if ((entry_name == NULL ||
791 strcmp(te_name, entry_name) == 0)) {
792 err = cb->diff_new(cb_arg, te, path);
793 if (err || entry_name)
794 break;
796 err = walk_tree(&te, fileindex, ie, tree, &tidx,
797 path, entry_name, repo, cb, cb_arg);
799 if (err)
800 break;
801 } else if (*ie) {
802 next = walk_fileindex(fileindex, *ie);
803 if (got_path_is_child((*ie)->path, path, path_len) &&
804 (entry_name == NULL ||
805 (te && strcmp(got_tree_entry_get_name(te),
806 entry_name) == 0))) {
807 err = cb->diff_old(cb_arg, *ie, path);
808 if (err || entry_name)
809 break;
811 *ie = next;
812 } else if (te) {
813 if (!got_object_tree_entry_is_submodule(te) &&
814 (entry_name == NULL ||
815 strcmp(got_tree_entry_get_name(te), entry_name)
816 == 0)) {
817 err = cb->diff_new(cb_arg, te, path);
818 if (err || entry_name)
819 break;
821 err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
822 entry_name, repo, cb, cb_arg);
823 if (err)
824 break;
828 return err;
831 const struct got_error *
832 got_fileindex_diff_tree(struct got_fileindex *fileindex,
833 struct got_tree_object *tree, const char *path, const char *entry_name,
834 struct got_repository *repo,
835 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
837 struct got_fileindex_entry *ie;
838 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
839 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
840 ie = walk_fileindex(fileindex, ie);
841 return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
842 cb, cb_arg);
845 static const struct got_error *
846 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
847 struct got_pathlist_head *, int, const char *, const char *,
848 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
850 static const struct got_error *
851 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
853 const struct got_error *err = NULL;
854 struct got_pathlist_entry *new = NULL;
855 struct dirent *dep = NULL;
856 struct dirent *de = NULL;
858 for (;;) {
859 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
860 if (de == NULL) {
861 err = got_error_from_errno("malloc");
862 break;
865 if (readdir_r(dir, de, &dep) != 0) {
866 err = got_error_from_errno("readdir_r");
867 free(de);
868 break;
870 if (dep == NULL) {
871 free(de);
872 break;
875 if (strcmp(de->d_name, ".") == 0 ||
876 strcmp(de->d_name, "..") == 0 ||
877 (path[0] == '\0' &&
878 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
879 free(de);
880 continue;
883 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
884 if (err) {
885 free(de);
886 break;
888 if (new == NULL) {
889 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
890 free(de);
891 break;
895 return err;
898 void
899 free_dirlist(struct got_pathlist_head *dirlist)
901 struct got_pathlist_entry *dle;
903 TAILQ_FOREACH(dle, dirlist, entry)
904 free(dle->data);
905 got_pathlist_free(dirlist);
908 static const struct got_error *
909 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
910 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
911 const char *path, const char *rootpath, struct got_repository *repo,
912 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
914 const struct got_error *err = NULL;
915 struct dirent *de = dle->data;
916 DIR *subdir = NULL;
917 int subdirfd = -1;
919 *next = NULL;
921 if (de->d_type == DT_DIR) {
922 char *subpath;
923 char *subdirpath;
924 struct got_pathlist_head subdirlist;
926 TAILQ_INIT(&subdirlist);
928 if (asprintf(&subpath, "%s%s%s", path,
929 path[0] == '\0' ? "" : "/", de->d_name) == -1)
930 return got_error_from_errno("asprintf");
932 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
933 free(subpath);
934 return got_error_from_errno("asprintf");
937 subdirfd = openat(fd, de->d_name,
938 O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
939 if (subdirfd == -1) {
940 if (errno == EACCES) {
941 *next = TAILQ_NEXT(dle, entry);
942 return NULL;
944 err = got_error_from_errno2("openat", subdirpath);
945 free(subpath);
946 free(subdirpath);
947 return err;
950 subdir = fdopendir(subdirfd);
951 if (subdir == NULL)
952 return got_error_from_errno2("fdopendir", path);
953 subdirfd = -1;
954 err = read_dirlist(&subdirlist, subdir, subdirpath);
955 if (err) {
956 free(subpath);
957 free(subdirpath);
958 closedir(subdir);
959 return err;
961 err = diff_fileindex_dir(fileindex, ie, &subdirlist,
962 dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
963 if (subdir && closedir(subdir) == -1 && err == NULL)
964 err = got_error_from_errno2("closedir", subdirpath);
965 free(subpath);
966 free(subdirpath);
967 free_dirlist(&subdirlist);
968 if (err)
969 return err;
972 *next = TAILQ_NEXT(dle, entry);
973 return NULL;
976 static const struct got_error *
977 diff_fileindex_dir(struct got_fileindex *fileindex,
978 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
979 int dirfd, const char *rootpath, const char *path,
980 struct got_repository *repo,
981 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
983 const struct got_error *err = NULL;
984 struct dirent *de = NULL;
985 size_t path_len = strlen(path);
986 struct got_pathlist_entry *dle;
988 dle = TAILQ_FIRST(dirlist);
989 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
990 if (dle && *ie) {
991 char *de_path;
992 int cmp;
993 de = dle->data;
994 if (asprintf(&de_path, "%s/%s", path,
995 de->d_name) == -1) {
996 err = got_error_from_errno("asprintf");
997 break;
999 cmp = got_path_cmp((*ie)->path, de_path,
1000 got_fileindex_entry_path_len(*ie),
1001 strlen(path) + 1 + de->d_namlen);
1002 free(de_path);
1003 if (cmp == 0) {
1004 err = cb->diff_old_new(cb_arg, *ie, de, path,
1005 dirfd);
1006 if (err)
1007 break;
1008 *ie = walk_fileindex(fileindex, *ie);
1009 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1010 path, rootpath, repo, cb, cb_arg);
1011 } else if (cmp < 0 ) {
1012 err = cb->diff_old(cb_arg, *ie, path);
1013 if (err)
1014 break;
1015 *ie = walk_fileindex(fileindex, *ie);
1016 } else {
1017 err = cb->diff_new(cb_arg, de, path, dirfd);
1018 if (err)
1019 break;
1020 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1021 path, rootpath, repo, cb, cb_arg);
1023 if (err)
1024 break;
1025 } else if (*ie) {
1026 err = cb->diff_old(cb_arg, *ie, path);
1027 if (err)
1028 break;
1029 *ie = walk_fileindex(fileindex, *ie);
1030 } else if (dle) {
1031 de = dle->data;
1032 err = cb->diff_new(cb_arg, de, path, dirfd);
1033 if (err)
1034 break;
1035 err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1036 rootpath, repo, cb, cb_arg);
1037 if (err)
1038 break;
1042 return err;
1045 const struct got_error *
1046 got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1047 const char *rootpath, const char *path, struct got_repository *repo,
1048 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1050 const struct got_error *err;
1051 struct got_fileindex_entry *ie;
1052 struct got_pathlist_head dirlist;
1053 int fd2;
1054 DIR *dir;
1056 TAILQ_INIT(&dirlist);
1059 * Duplicate the file descriptor so we can call closedir() below
1060 * without closing the file descriptor passed in by our caller.
1062 fd2 = dup(fd);
1063 if (fd2 == -1)
1064 return got_error_from_errno2("dup", path);
1065 dir = fdopendir(fd2);
1066 if (dir == NULL)
1067 return got_error_from_errno2("fdopendir", path);
1068 err = read_dirlist(&dirlist, dir, path);
1069 if (err) {
1070 closedir(dir);
1071 return err;
1074 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1075 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1076 ie = walk_fileindex(fileindex, ie);
1077 err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1078 rootpath, path, repo, cb, cb_arg);
1080 if (closedir(dir) == -1 && err == NULL)
1081 err = got_error_from_errno2("closedir", path);
1082 free_dirlist(&dirlist);
1083 return err;
1086 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);