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
48 #define GOT_FILEIDX_F_REMOVE_ON_FLUSH 0x00100000
50 struct got_fileindex {
51 struct got_fileindex_tree entries;
52 int nentries; /* Does not include entries marked for removal. */
53 #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
54 };
56 uint16_t
57 got_fileindex_perms_from_st(struct stat *sb)
58 {
59 uint16_t perms = (sb->st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
60 return (perms << GOT_FILEIDX_MODE_PERMS_SHIFT);
61 }
63 mode_t
64 got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
65 {
66 mode_t perms = (ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT);
67 return (S_IFREG | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
68 }
70 const struct got_error *
71 got_fileindex_entry_update(struct got_fileindex_entry *ie,
72 const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
73 int update_timestamps)
74 {
75 struct stat sb;
77 if (lstat(ondisk_path, &sb) != 0) {
78 if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
79 errno == ENOENT))
80 return got_error_from_errno2("lstat", ondisk_path);
81 } else {
82 if (sb.st_mode & S_IFDIR)
83 return got_error_set_errno(EISDIR, ondisk_path);
84 ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
85 }
88 if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
89 if (update_timestamps) {
90 ie->ctime_sec = sb.st_ctime;
91 ie->ctime_nsec = sb.st_ctimensec;
92 ie->mtime_sec = sb.st_mtime;
93 ie->mtime_nsec = sb.st_mtimensec;
94 }
95 ie->uid = sb.st_uid;
96 ie->gid = sb.st_gid;
97 ie->size = (sb.st_size & 0xffffffff);
98 if (S_ISLNK(sb.st_mode))
99 ie->mode = GOT_FILEIDX_MODE_SYMLINK;
100 else {
101 ie->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
102 ie->mode |= got_fileindex_perms_from_st(&sb);
106 if (blob_sha1) {
107 memcpy(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
108 ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
109 } else
110 ie->flags |= GOT_FILEIDX_F_NO_BLOB;
112 if (commit_sha1) {
113 memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
114 ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
115 } else
116 ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
118 return NULL;
121 void
122 got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
124 ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
127 const struct got_error *
128 got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
129 const char *relpath)
131 size_t len;
133 *ie = calloc(1, sizeof(**ie));
134 if (*ie == NULL)
135 return got_error_from_errno("calloc");
137 (*ie)->path = strdup(relpath);
138 if ((*ie)->path == NULL) {
139 const struct got_error *err = got_error_from_errno("strdup");
140 free(*ie);
141 *ie = NULL;
142 return err;
145 len = strlen(relpath);
146 if (len > GOT_FILEIDX_F_PATH_LEN)
147 len = GOT_FILEIDX_F_PATH_LEN;
148 (*ie)->flags |= len;
150 return NULL;
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 /*
224 * Removing an entry from the RB tree immediately breaks
225 * in-progress iterations over file index entries.
226 * So flag this entry for removal and remove it once the index
227 * is written out to disk. Meanwhile, pretend this entry no longer
228 * exists if we get queried for it again before then.
229 */
230 ie->flags |= GOT_FILEIDX_F_REMOVE_ON_FLUSH;
231 fileindex->nentries--;
234 struct got_fileindex_entry *
235 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
236 size_t path_len)
238 struct got_fileindex_entry *ie;
239 struct got_fileindex_entry key;
240 memset(&key, 0, sizeof(key));
241 key.path = (char *)path;
242 key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
243 ie = RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
244 if (ie && (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH))
245 return NULL;
246 return ie;
249 const struct got_error *
250 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
251 got_fileindex_cb cb, void *cb_arg)
253 const struct got_error *err;
254 struct got_fileindex_entry *ie, *tmp;
256 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
257 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
258 continue;
259 err = (*cb)(cb_arg, ie);
260 if (err)
261 return err;
263 return NULL;
266 struct got_fileindex *
267 got_fileindex_alloc(void)
269 struct got_fileindex *fileindex;
271 fileindex = calloc(1, sizeof(*fileindex));
272 if (fileindex == NULL)
273 return NULL;
275 RB_INIT(&fileindex->entries);
276 return fileindex;
279 void
280 got_fileindex_free(struct got_fileindex *fileindex)
282 struct got_fileindex_entry *ie;
284 while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
285 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
286 got_fileindex_entry_free(ie);
288 free(fileindex);
291 static const struct got_error *
292 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
294 size_t n;
296 val = htobe64(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_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
307 size_t n;
309 val = htobe32(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_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
320 size_t n;
322 val = htobe16(val);
323 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
324 n = fwrite(&val, 1, sizeof(val), outfile);
325 if (n != sizeof(val))
326 return got_ferror(outfile, GOT_ERR_IO);
327 return NULL;
330 static const struct got_error *
331 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
333 size_t n, len, pad = 0;
334 static const uint8_t zero[8] = { 0 };
336 len = strlen(path);
337 while ((len + pad) % 8 != 0)
338 pad++;
339 if (pad == 0)
340 pad = 8; /* NUL-terminate */
342 SHA1Update(ctx, path, len);
343 n = fwrite(path, 1, len, outfile);
344 if (n != len)
345 return got_ferror(outfile, GOT_ERR_IO);
346 SHA1Update(ctx, zero, pad);
347 n = fwrite(zero, 1, pad, outfile);
348 if (n != pad)
349 return got_ferror(outfile, GOT_ERR_IO);
350 return NULL;
353 static const struct got_error *
354 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
355 FILE *outfile)
357 const struct got_error *err;
358 size_t n;
359 uint32_t stage;
361 err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
362 if (err)
363 return err;
364 err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
365 if (err)
366 return err;
367 err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
368 if (err)
369 return err;
370 err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
371 if (err)
372 return err;
374 err = write_fileindex_val32(ctx, ie->uid, outfile);
375 if (err)
376 return err;
377 err = write_fileindex_val32(ctx, ie->gid, outfile);
378 if (err)
379 return err;
380 err = write_fileindex_val32(ctx, ie->size, outfile);
381 if (err)
382 return err;
384 err = write_fileindex_val16(ctx, ie->mode, outfile);
385 if (err)
386 return err;
388 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
389 n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
390 if (n != SHA1_DIGEST_LENGTH)
391 return got_ferror(outfile, GOT_ERR_IO);
393 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
394 n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
395 if (n != SHA1_DIGEST_LENGTH)
396 return got_ferror(outfile, GOT_ERR_IO);
398 err = write_fileindex_val32(ctx, ie->flags, outfile);
399 if (err)
400 return err;
402 err = write_fileindex_path(ctx, ie->path, outfile);
403 if (err)
404 return err;
406 stage = got_fileindex_entry_stage_get(ie);
407 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
408 stage == GOT_FILEIDX_STAGE_ADD) {
409 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
410 n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
411 outfile);
412 if (n != SHA1_DIGEST_LENGTH)
413 return got_ferror(outfile, GOT_ERR_IO);
416 return NULL;
419 const struct got_error *
420 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
422 const struct got_error *err = NULL;
423 struct got_fileindex_hdr hdr;
424 SHA1_CTX ctx;
425 uint8_t sha1[SHA1_DIGEST_LENGTH];
426 size_t n;
427 struct got_fileindex_entry *ie, *tmp;
429 SHA1Init(&ctx);
431 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
432 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
433 hdr.nentries = htobe32(fileindex->nentries);
435 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
436 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
437 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
438 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
439 if (n != sizeof(hdr.signature))
440 return got_ferror(outfile, GOT_ERR_IO);
441 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
442 if (n != sizeof(hdr.version))
443 return got_ferror(outfile, GOT_ERR_IO);
444 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
445 if (n != sizeof(hdr.nentries))
446 return got_ferror(outfile, GOT_ERR_IO);
448 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
449 ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
450 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH) {
451 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
452 got_fileindex_entry_free(ie);
453 continue;
455 err = write_fileindex_entry(&ctx, ie, outfile);
456 if (err)
457 return err;
460 SHA1Final(sha1, &ctx);
461 n = fwrite(sha1, 1, sizeof(sha1), outfile);
462 if (n != sizeof(sha1))
463 return got_ferror(outfile, GOT_ERR_IO);
465 if (fflush(outfile) != 0)
466 return got_error_from_errno("fflush");
468 return NULL;
471 static const struct got_error *
472 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
474 size_t n;
476 n = fread(val, 1, sizeof(*val), infile);
477 if (n != sizeof(*val))
478 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
479 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
480 *val = be64toh(*val);
481 return NULL;
484 static const struct got_error *
485 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
487 size_t n;
489 n = fread(val, 1, sizeof(*val), infile);
490 if (n != sizeof(*val))
491 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
492 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
493 *val = be32toh(*val);
494 return NULL;
497 static const struct got_error *
498 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
500 size_t n;
502 n = fread(val, 1, sizeof(*val), infile);
503 if (n != sizeof(*val))
504 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
505 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
506 *val = be16toh(*val);
507 return NULL;
510 static const struct got_error *
511 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
513 const struct got_error *err = NULL;
514 const size_t chunk_size = 8;
515 size_t n, len = 0, totlen = chunk_size;
517 *path = malloc(totlen);
518 if (*path == NULL)
519 return got_error_from_errno("malloc");
521 do {
522 if (len + chunk_size > totlen) {
523 char *p = reallocarray(*path, totlen + chunk_size, 1);
524 if (p == NULL) {
525 err = got_error_from_errno("reallocarray");
526 break;
528 totlen += chunk_size;
529 *path = p;
531 n = fread(*path + len, 1, chunk_size, infile);
532 if (n != chunk_size) {
533 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
534 break;
536 SHA1Update(ctx, *path + len, chunk_size);
537 len += chunk_size;
538 } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
540 if (err) {
541 free(*path);
542 *path = NULL;
544 return err;
547 static const struct got_error *
548 read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
549 FILE *infile, uint32_t version)
551 const struct got_error *err;
552 struct got_fileindex_entry *ie;
553 size_t n;
555 *iep = NULL;
557 ie = calloc(1, sizeof(*ie));
558 if (ie == NULL)
559 return got_error_from_errno("calloc");
561 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
562 if (err)
563 goto done;
564 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
565 if (err)
566 goto done;
567 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
568 if (err)
569 goto done;
570 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
571 if (err)
572 goto done;
574 err = read_fileindex_val32(&ie->uid, ctx, infile);
575 if (err)
576 goto done;
577 err = read_fileindex_val32(&ie->gid, ctx, infile);
578 if (err)
579 goto done;
580 err = read_fileindex_val32(&ie->size, ctx, infile);
581 if (err)
582 goto done;
584 err = read_fileindex_val16(&ie->mode, ctx, infile);
585 if (err)
586 goto done;
588 n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
589 if (n != SHA1_DIGEST_LENGTH) {
590 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
591 goto done;
593 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
595 n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
596 if (n != SHA1_DIGEST_LENGTH) {
597 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
598 goto done;
600 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
602 err = read_fileindex_val32(&ie->flags, ctx, infile);
603 if (err)
604 goto done;
606 err = read_fileindex_path(&ie->path, ctx, infile);
607 if (err)
608 goto done;
610 if (version >= 2) {
611 uint32_t stage = got_fileindex_entry_stage_get(ie);
612 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
613 stage == GOT_FILEIDX_STAGE_ADD) {
614 n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
615 infile);
616 if (n != SHA1_DIGEST_LENGTH) {
617 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
618 goto done;
620 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
622 } else {
623 /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
624 ie->flags &= ~GOT_FILEIDX_F_STAGE;
627 done:
628 if (err)
629 got_fileindex_entry_free(ie);
630 else
631 *iep = ie;
632 return err;
635 const struct got_error *
636 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
638 const struct got_error *err = NULL;
639 struct got_fileindex_hdr hdr;
640 SHA1_CTX ctx;
641 struct got_fileindex_entry *ie;
642 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
643 uint8_t sha1[SHA1_DIGEST_LENGTH];
644 size_t n;
645 int i;
647 SHA1Init(&ctx);
649 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
650 if (n != sizeof(hdr.signature)) {
651 if (n == 0) /* EOF */
652 return NULL;
653 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
655 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
656 if (n != sizeof(hdr.version)) {
657 if (n == 0) /* EOF */
658 return NULL;
659 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
661 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
662 if (n != sizeof(hdr.nentries)) {
663 if (n == 0) /* EOF */
664 return NULL;
665 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
668 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
669 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
670 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
672 hdr.signature = be32toh(hdr.signature);
673 hdr.version = be32toh(hdr.version);
674 hdr.nentries = be32toh(hdr.nentries);
676 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
677 return got_error(GOT_ERR_FILEIDX_SIG);
678 if (hdr.version > GOT_FILE_INDEX_VERSION)
679 return got_error(GOT_ERR_FILEIDX_VER);
681 for (i = 0; i < hdr.nentries; i++) {
682 err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
683 if (err)
684 return err;
685 err = add_entry(fileindex, ie);
686 if (err)
687 return err;
690 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
691 if (n != sizeof(sha1_expected))
692 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
693 SHA1Final(sha1, &ctx);
694 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
695 return got_error(GOT_ERR_FILEIDX_CSUM);
697 return NULL;
700 static struct got_fileindex_entry *
701 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
703 struct got_fileindex_entry *next;
705 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
707 /* Skip entries which were added or removed by diff callbacks. */
708 while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
709 GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
710 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
712 return next;
715 static const struct got_error *
716 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
717 struct got_tree_object *tree, const char *, const char *,
718 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
720 static const struct got_error *
721 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
722 struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
723 const char *path, const char *entry_name, struct got_repository *repo,
724 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
726 const struct got_error *err = NULL;
727 struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
729 if (!got_object_tree_entry_is_submodule(te) &&
730 S_ISDIR(got_tree_entry_get_mode(te))) {
731 char *subpath;
732 struct got_tree_object *subtree;
734 if (asprintf(&subpath, "%s%s%s", path,
735 path[0] == '\0' ? "" : "/",
736 got_tree_entry_get_name(te)) == -1)
737 return got_error_from_errno("asprintf");
739 err = got_object_open_as_tree(&subtree, repo,
740 got_tree_entry_get_id(te));
741 if (err) {
742 free(subpath);
743 return err;
746 err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
747 entry_name, repo, cb, cb_arg);
748 free(subpath);
749 got_object_tree_close(subtree);
750 if (err)
751 return err;
754 (*tidx)++;
755 *next = got_object_tree_get_entry(tree, *tidx);
756 return NULL;
759 static const struct got_error *
760 diff_fileindex_tree(struct got_fileindex *fileindex,
761 struct got_fileindex_entry **ie, struct got_tree_object *tree,
762 const char *path, const char *entry_name, struct got_repository *repo,
763 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
765 const struct got_error *err = NULL;
766 struct got_tree_entry *te = NULL;
767 size_t path_len = strlen(path);
768 struct got_fileindex_entry *next;
769 int tidx = 0;
771 te = got_object_tree_get_entry(tree, tidx);
772 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
773 if (te && *ie) {
774 char *te_path;
775 const char *te_name = got_tree_entry_get_name(te);
776 int cmp;
777 if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
778 err = got_error_from_errno("asprintf");
779 break;
781 cmp = got_path_cmp((*ie)->path, te_path,
782 got_fileindex_entry_path_len(*ie), strlen(te_path));
783 free(te_path);
784 if (cmp == 0) {
785 if (got_path_is_child((*ie)->path, path,
786 path_len) &&
787 !got_object_tree_entry_is_submodule(te) &&
788 (entry_name == NULL ||
789 strcmp(te_name, entry_name) == 0)) {
790 err = cb->diff_old_new(cb_arg, *ie, te,
791 path);
792 if (err || entry_name)
793 break;
795 *ie = walk_fileindex(fileindex, *ie);
796 err = walk_tree(&te, fileindex, ie, tree, &tidx,
797 path, entry_name, repo, cb, cb_arg);
798 } else if (cmp < 0) {
799 next = walk_fileindex(fileindex, *ie);
800 if (got_path_is_child((*ie)->path, path,
801 path_len) && (entry_name == NULL ||
802 strcmp(te_name, entry_name) == 0)) {
803 err = cb->diff_old(cb_arg, *ie, path);
804 if (err || entry_name)
805 break;
807 *ie = next;
808 } else {
809 if ((entry_name == NULL ||
810 strcmp(te_name, entry_name) == 0)) {
811 err = cb->diff_new(cb_arg, te, path);
812 if (err || entry_name)
813 break;
815 err = walk_tree(&te, fileindex, ie, tree, &tidx,
816 path, entry_name, repo, cb, cb_arg);
818 if (err)
819 break;
820 } else if (*ie) {
821 next = walk_fileindex(fileindex, *ie);
822 if (got_path_is_child((*ie)->path, path, path_len) &&
823 (entry_name == NULL ||
824 (te && strcmp(got_tree_entry_get_name(te),
825 entry_name) == 0))) {
826 err = cb->diff_old(cb_arg, *ie, path);
827 if (err || entry_name)
828 break;
830 *ie = next;
831 } else if (te) {
832 if (!got_object_tree_entry_is_submodule(te) &&
833 (entry_name == NULL ||
834 strcmp(got_tree_entry_get_name(te), entry_name)
835 == 0)) {
836 err = cb->diff_new(cb_arg, te, path);
837 if (err || entry_name)
838 break;
840 err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
841 entry_name, repo, cb, cb_arg);
842 if (err)
843 break;
847 return err;
850 const struct got_error *
851 got_fileindex_diff_tree(struct got_fileindex *fileindex,
852 struct got_tree_object *tree, const char *path, const char *entry_name,
853 struct got_repository *repo,
854 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
856 struct got_fileindex_entry *ie;
857 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
858 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
859 ie = walk_fileindex(fileindex, ie);
860 return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
861 cb, cb_arg);
864 static const struct got_error *
865 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
866 struct got_pathlist_head *, int, const char *, const char *,
867 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
869 static const struct got_error *
870 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
872 const struct got_error *err = NULL;
873 struct got_pathlist_entry *new = NULL;
874 struct dirent *dep = NULL;
875 struct dirent *de = NULL;
877 for (;;) {
878 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
879 if (de == NULL) {
880 err = got_error_from_errno("malloc");
881 break;
884 if (readdir_r(dir, de, &dep) != 0) {
885 err = got_error_from_errno("readdir_r");
886 free(de);
887 break;
889 if (dep == NULL) {
890 free(de);
891 break;
894 if (strcmp(de->d_name, ".") == 0 ||
895 strcmp(de->d_name, "..") == 0 ||
896 (path[0] == '\0' &&
897 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
898 free(de);
899 continue;
902 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
903 if (err) {
904 free(de);
905 break;
907 if (new == NULL) {
908 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
909 free(de);
910 break;
914 return err;
917 void
918 free_dirlist(struct got_pathlist_head *dirlist)
920 struct got_pathlist_entry *dle;
922 TAILQ_FOREACH(dle, dirlist, entry)
923 free(dle->data);
924 got_pathlist_free(dirlist);
927 static const struct got_error *
928 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
929 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
930 const char *path, const char *rootpath, struct got_repository *repo,
931 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
933 const struct got_error *err = NULL;
934 struct dirent *de = dle->data;
935 DIR *subdir = NULL;
936 int subdirfd = -1;
937 int type;
939 *next = NULL;
941 if (de->d_type == DT_UNKNOWN) {
942 /* Occurs on NFS mounts without "readdir plus" RPC. */
943 char *dir_path;
944 if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
945 return got_error_from_errno("asprintf");
946 err = got_path_dirent_type(&type, dir_path, de);
947 free(dir_path);
948 if (err)
949 return err;
950 } else
951 type = de->d_type;
953 if (type == DT_DIR) {
954 char *subpath;
955 char *subdirpath;
956 struct got_pathlist_head subdirlist;
958 TAILQ_INIT(&subdirlist);
960 if (asprintf(&subpath, "%s%s%s", path,
961 path[0] == '\0' ? "" : "/", de->d_name) == -1)
962 return got_error_from_errno("asprintf");
964 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
965 free(subpath);
966 return got_error_from_errno("asprintf");
969 subdirfd = openat(fd, de->d_name,
970 O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
971 if (subdirfd == -1) {
972 if (errno == EACCES) {
973 *next = TAILQ_NEXT(dle, entry);
974 return NULL;
976 err = got_error_from_errno2("openat", subdirpath);
977 free(subpath);
978 free(subdirpath);
979 return err;
982 subdir = fdopendir(subdirfd);
983 if (subdir == NULL)
984 return got_error_from_errno2("fdopendir", path);
985 subdirfd = -1;
986 err = read_dirlist(&subdirlist, subdir, subdirpath);
987 if (err) {
988 free(subpath);
989 free(subdirpath);
990 closedir(subdir);
991 return err;
993 err = diff_fileindex_dir(fileindex, ie, &subdirlist,
994 dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
995 if (subdir && closedir(subdir) == -1 && err == NULL)
996 err = got_error_from_errno2("closedir", subdirpath);
997 free(subpath);
998 free(subdirpath);
999 free_dirlist(&subdirlist);
1000 if (err)
1001 return err;
1004 *next = TAILQ_NEXT(dle, entry);
1005 return NULL;
1008 static const struct got_error *
1009 diff_fileindex_dir(struct got_fileindex *fileindex,
1010 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1011 int dirfd, const char *rootpath, const char *path,
1012 struct got_repository *repo,
1013 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1015 const struct got_error *err = NULL;
1016 struct dirent *de = NULL;
1017 size_t path_len = strlen(path);
1018 struct got_pathlist_entry *dle;
1020 if (cb->diff_traverse) {
1021 err = cb->diff_traverse(cb_arg, path, dirfd);
1022 if (err)
1023 return err;
1026 dle = TAILQ_FIRST(dirlist);
1027 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1028 if (dle && *ie) {
1029 char *de_path;
1030 int cmp;
1031 de = dle->data;
1032 if (asprintf(&de_path, "%s/%s", path,
1033 de->d_name) == -1) {
1034 err = got_error_from_errno("asprintf");
1035 break;
1037 cmp = got_path_cmp((*ie)->path, de_path,
1038 got_fileindex_entry_path_len(*ie),
1039 strlen(path) + 1 + de->d_namlen);
1040 free(de_path);
1041 if (cmp == 0) {
1042 err = cb->diff_old_new(cb_arg, *ie, de, path,
1043 dirfd);
1044 if (err)
1045 break;
1046 *ie = walk_fileindex(fileindex, *ie);
1047 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1048 path, rootpath, repo, cb, cb_arg);
1049 } else if (cmp < 0 ) {
1050 err = cb->diff_old(cb_arg, *ie, path);
1051 if (err)
1052 break;
1053 *ie = walk_fileindex(fileindex, *ie);
1054 } else {
1055 err = cb->diff_new(cb_arg, de, path, dirfd);
1056 if (err)
1057 break;
1058 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1059 path, rootpath, repo, cb, cb_arg);
1061 if (err)
1062 break;
1063 } else if (*ie) {
1064 err = cb->diff_old(cb_arg, *ie, path);
1065 if (err)
1066 break;
1067 *ie = walk_fileindex(fileindex, *ie);
1068 } else if (dle) {
1069 de = dle->data;
1070 err = cb->diff_new(cb_arg, de, path, dirfd);
1071 if (err)
1072 break;
1073 err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1074 rootpath, repo, cb, cb_arg);
1075 if (err)
1076 break;
1080 return err;
1083 const struct got_error *
1084 got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1085 const char *rootpath, const char *path, struct got_repository *repo,
1086 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1088 const struct got_error *err;
1089 struct got_fileindex_entry *ie;
1090 struct got_pathlist_head dirlist;
1091 int fd2;
1092 DIR *dir;
1094 TAILQ_INIT(&dirlist);
1097 * Duplicate the file descriptor so we can call closedir() below
1098 * without closing the file descriptor passed in by our caller.
1100 fd2 = dup(fd);
1101 if (fd2 == -1)
1102 return got_error_from_errno2("dup", path);
1103 if (lseek(fd2, 0, SEEK_SET) == -1) {
1104 err = got_error_from_errno2("lseek", path);
1105 close(fd2);
1106 return err;
1108 dir = fdopendir(fd2);
1109 if (dir == NULL) {
1110 err = got_error_from_errno2("fdopendir", path);
1111 close(fd2);
1112 return err;
1114 err = read_dirlist(&dirlist, dir, path);
1115 if (err) {
1116 closedir(dir);
1117 return err;
1120 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1121 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1122 ie = walk_fileindex(fileindex, ie);
1123 err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1124 rootpath, path, repo, cb, cb_arg);
1126 if (closedir(dir) == -1 && err == NULL)
1127 err = got_error_from_errno2("closedir", path);
1128 free_dirlist(&dirlist);
1129 return err;
1132 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);