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
49 #define GOT_FILEIDX_F_SKIPPED 0x00200000
51 struct got_fileindex {
52 struct got_fileindex_tree entries;
53 int nentries; /* Does not include entries marked for removal. */
54 #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
55 };
57 mode_t
58 got_fileindex_entry_perms_get(struct got_fileindex_entry *ie)
59 {
60 return ((ie->mode & GOT_FILEIDX_MODE_PERMS) >>
61 GOT_FILEIDX_MODE_PERMS_SHIFT);
62 }
64 static void
65 fileindex_entry_perms_set(struct got_fileindex_entry *ie, mode_t mode)
66 {
67 ie->mode &= ~GOT_FILEIDX_MODE_PERMS;
68 ie->mode |= ((mode << GOT_FILEIDX_MODE_PERMS_SHIFT) &
69 GOT_FILEIDX_MODE_PERMS);
70 }
72 mode_t
73 got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
74 {
75 mode_t perms = got_fileindex_entry_perms_get(ie);
76 int type = got_fileindex_entry_filetype_get(ie);
77 uint32_t ftype;
79 if (type == GOT_FILEIDX_MODE_REGULAR_FILE ||
80 type == GOT_FILEIDX_MODE_BAD_SYMLINK)
81 ftype = S_IFREG;
82 else
83 ftype = S_IFLNK;
85 return (ftype | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
86 }
88 const struct got_error *
89 got_fileindex_entry_update(struct got_fileindex_entry *ie,
90 int wt_fd, const char *ondisk_path, uint8_t *blob_sha1,
91 uint8_t *commit_sha1, int update_timestamps)
92 {
93 struct stat sb;
95 if (fstatat(wt_fd, ondisk_path, &sb, AT_SYMLINK_NOFOLLOW) != 0) {
96 if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
97 errno == ENOENT))
98 return got_error_from_errno2("fstatat", ondisk_path);
99 sb.st_mode = GOT_DEFAULT_FILE_MODE;
100 } else {
101 if (sb.st_mode & S_IFDIR)
102 return got_error_set_errno(EISDIR, ondisk_path);
103 ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
107 if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
108 if (update_timestamps) {
109 ie->ctime_sec = sb.st_ctim.tv_sec;
110 ie->ctime_nsec = sb.st_ctim.tv_nsec;
111 ie->mtime_sec = sb.st_mtim.tv_sec;
112 ie->mtime_nsec = sb.st_mtim.tv_nsec;
114 ie->uid = sb.st_uid;
115 ie->gid = sb.st_gid;
116 ie->size = (sb.st_size & 0xffffffff);
117 if (S_ISLNK(sb.st_mode)) {
118 got_fileindex_entry_filetype_set(ie,
119 GOT_FILEIDX_MODE_SYMLINK);
120 fileindex_entry_perms_set(ie, 0);
121 } else {
122 got_fileindex_entry_filetype_set(ie,
123 GOT_FILEIDX_MODE_REGULAR_FILE);
124 fileindex_entry_perms_set(ie,
125 sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
129 if (blob_sha1) {
130 memcpy(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
131 ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
132 } else
133 ie->flags |= GOT_FILEIDX_F_NO_BLOB;
135 if (commit_sha1) {
136 memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
137 ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
138 } else
139 ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
141 return NULL;
144 void
145 got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
147 ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
150 void
151 got_fileindex_entry_mark_skipped(struct got_fileindex_entry *ie)
153 ie->flags |= GOT_FILEIDX_F_SKIPPED;
156 const struct got_error *
157 got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
158 const char *relpath)
160 size_t len;
162 *ie = calloc(1, sizeof(**ie));
163 if (*ie == NULL)
164 return got_error_from_errno("calloc");
166 (*ie)->path = strdup(relpath);
167 if ((*ie)->path == NULL) {
168 const struct got_error *err = got_error_from_errno("strdup");
169 free(*ie);
170 *ie = NULL;
171 return err;
174 len = strlen(relpath);
175 if (len > GOT_FILEIDX_F_PATH_LEN)
176 len = GOT_FILEIDX_F_PATH_LEN;
177 (*ie)->flags |= len;
179 return NULL;
182 void
183 got_fileindex_entry_free(struct got_fileindex_entry *ie)
185 free(ie->path);
186 free(ie);
189 size_t
190 got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
192 return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
195 uint32_t
196 got_fileindex_entry_stage_get(const struct got_fileindex_entry *ie)
198 return ((ie->flags & GOT_FILEIDX_F_STAGE) >> GOT_FILEIDX_F_STAGE_SHIFT);
201 void
202 got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage)
204 ie->flags &= ~GOT_FILEIDX_F_STAGE;
205 ie->flags |= ((stage << GOT_FILEIDX_F_STAGE_SHIFT) &
206 GOT_FILEIDX_F_STAGE);
209 int
210 got_fileindex_entry_filetype_get(struct got_fileindex_entry *ie)
212 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
215 void
216 got_fileindex_entry_filetype_set(struct got_fileindex_entry *ie, int type)
218 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_ONDISK;
219 ie->mode |= (type & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
222 void
223 got_fileindex_entry_staged_filetype_set(struct got_fileindex_entry *ie,
224 int type)
226 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_STAGED;
227 ie->mode |= ((type << GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT) &
228 GOT_FILEIDX_MODE_FILE_TYPE_STAGED);
231 int
232 got_fileindex_entry_staged_filetype_get(struct got_fileindex_entry *ie)
234 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_STAGED) >>
235 GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT;
238 int
239 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
241 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
244 int
245 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
247 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
250 int
251 got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
253 return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
256 int
257 got_fileindex_entry_was_skipped(struct got_fileindex_entry *ie)
259 return (ie->flags & GOT_FILEIDX_F_SKIPPED) != 0;
262 static const struct got_error *
263 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
265 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
266 return got_error(GOT_ERR_NO_SPACE);
268 RB_INSERT(got_fileindex_tree, &fileindex->entries, ie);
269 fileindex->nentries++;
270 return NULL;
273 const struct got_error *
274 got_fileindex_entry_add(struct got_fileindex *fileindex,
275 struct got_fileindex_entry *ie)
277 /* Flag this entry until it gets written out to disk. */
278 ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
280 return add_entry(fileindex, ie);
283 void
284 got_fileindex_entry_remove(struct got_fileindex *fileindex,
285 struct got_fileindex_entry *ie)
287 /*
288 * Removing an entry from the RB tree immediately breaks
289 * in-progress iterations over file index entries.
290 * So flag this entry for removal and remove it once the index
291 * is written out to disk. Meanwhile, pretend this entry no longer
292 * exists if we get queried for it again before then.
293 */
294 ie->flags |= GOT_FILEIDX_F_REMOVE_ON_FLUSH;
295 fileindex->nentries--;
298 struct got_fileindex_entry *
299 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
300 size_t path_len)
302 struct got_fileindex_entry *ie;
303 struct got_fileindex_entry key;
304 memset(&key, 0, sizeof(key));
305 key.path = (char *)path;
306 key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
307 ie = RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
308 if (ie && (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH))
309 return NULL;
310 return ie;
313 const struct got_error *
314 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
315 got_fileindex_cb cb, void *cb_arg)
317 const struct got_error *err;
318 struct got_fileindex_entry *ie, *tmp;
320 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
321 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
322 continue;
323 err = (*cb)(cb_arg, ie);
324 if (err)
325 return err;
327 return NULL;
330 struct got_fileindex *
331 got_fileindex_alloc(void)
333 struct got_fileindex *fileindex;
335 fileindex = calloc(1, sizeof(*fileindex));
336 if (fileindex == NULL)
337 return NULL;
339 RB_INIT(&fileindex->entries);
340 return fileindex;
343 void
344 got_fileindex_free(struct got_fileindex *fileindex)
346 struct got_fileindex_entry *ie;
348 while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
349 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
350 got_fileindex_entry_free(ie);
352 free(fileindex);
355 static const struct got_error *
356 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
358 size_t n;
360 val = htobe64(val);
361 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
362 n = fwrite(&val, 1, sizeof(val), outfile);
363 if (n != sizeof(val))
364 return got_ferror(outfile, GOT_ERR_IO);
365 return NULL;
368 static const struct got_error *
369 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
371 size_t n;
373 val = htobe32(val);
374 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
375 n = fwrite(&val, 1, sizeof(val), outfile);
376 if (n != sizeof(val))
377 return got_ferror(outfile, GOT_ERR_IO);
378 return NULL;
381 static const struct got_error *
382 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
384 size_t n;
386 val = htobe16(val);
387 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
388 n = fwrite(&val, 1, sizeof(val), outfile);
389 if (n != sizeof(val))
390 return got_ferror(outfile, GOT_ERR_IO);
391 return NULL;
394 static const struct got_error *
395 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
397 size_t n, len, pad = 0;
398 static const uint8_t zero[8] = { 0 };
400 len = strlen(path);
401 while ((len + pad) % 8 != 0)
402 pad++;
403 if (pad == 0)
404 pad = 8; /* NUL-terminate */
406 SHA1Update(ctx, path, len);
407 n = fwrite(path, 1, len, outfile);
408 if (n != len)
409 return got_ferror(outfile, GOT_ERR_IO);
410 SHA1Update(ctx, zero, pad);
411 n = fwrite(zero, 1, pad, outfile);
412 if (n != pad)
413 return got_ferror(outfile, GOT_ERR_IO);
414 return NULL;
417 static const struct got_error *
418 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
419 FILE *outfile)
421 const struct got_error *err;
422 size_t n;
423 uint32_t stage;
425 err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
426 if (err)
427 return err;
428 err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
429 if (err)
430 return err;
431 err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
432 if (err)
433 return err;
434 err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
435 if (err)
436 return err;
438 err = write_fileindex_val32(ctx, ie->uid, outfile);
439 if (err)
440 return err;
441 err = write_fileindex_val32(ctx, ie->gid, outfile);
442 if (err)
443 return err;
444 err = write_fileindex_val32(ctx, ie->size, outfile);
445 if (err)
446 return err;
448 err = write_fileindex_val16(ctx, ie->mode, outfile);
449 if (err)
450 return err;
452 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
453 n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
454 if (n != SHA1_DIGEST_LENGTH)
455 return got_ferror(outfile, GOT_ERR_IO);
457 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
458 n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
459 if (n != SHA1_DIGEST_LENGTH)
460 return got_ferror(outfile, GOT_ERR_IO);
462 err = write_fileindex_val32(ctx, ie->flags, outfile);
463 if (err)
464 return err;
466 err = write_fileindex_path(ctx, ie->path, outfile);
467 if (err)
468 return err;
470 stage = got_fileindex_entry_stage_get(ie);
471 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
472 stage == GOT_FILEIDX_STAGE_ADD) {
473 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
474 n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
475 outfile);
476 if (n != SHA1_DIGEST_LENGTH)
477 return got_ferror(outfile, GOT_ERR_IO);
480 return NULL;
483 const struct got_error *
484 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
486 const struct got_error *err = NULL;
487 struct got_fileindex_hdr hdr;
488 SHA1_CTX ctx;
489 uint8_t sha1[SHA1_DIGEST_LENGTH];
490 size_t n;
491 struct got_fileindex_entry *ie, *tmp;
493 SHA1Init(&ctx);
495 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
496 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
497 hdr.nentries = htobe32(fileindex->nentries);
499 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
500 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
501 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
502 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
503 if (n != sizeof(hdr.signature))
504 return got_ferror(outfile, GOT_ERR_IO);
505 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
506 if (n != sizeof(hdr.version))
507 return got_ferror(outfile, GOT_ERR_IO);
508 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
509 if (n != sizeof(hdr.nentries))
510 return got_ferror(outfile, GOT_ERR_IO);
512 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
513 ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
514 ie->flags &= ~GOT_FILEIDX_F_SKIPPED;
515 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH) {
516 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
517 got_fileindex_entry_free(ie);
518 continue;
520 err = write_fileindex_entry(&ctx, ie, outfile);
521 if (err)
522 return err;
525 SHA1Final(sha1, &ctx);
526 n = fwrite(sha1, 1, sizeof(sha1), outfile);
527 if (n != sizeof(sha1))
528 return got_ferror(outfile, GOT_ERR_IO);
530 if (fflush(outfile) != 0)
531 return got_error_from_errno("fflush");
533 return NULL;
536 static const struct got_error *
537 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
539 size_t n;
541 n = fread(val, 1, sizeof(*val), infile);
542 if (n != sizeof(*val))
543 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
544 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
545 *val = be64toh(*val);
546 return NULL;
549 static const struct got_error *
550 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
552 size_t n;
554 n = fread(val, 1, sizeof(*val), infile);
555 if (n != sizeof(*val))
556 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
557 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
558 *val = be32toh(*val);
559 return NULL;
562 static const struct got_error *
563 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
565 size_t n;
567 n = fread(val, 1, sizeof(*val), infile);
568 if (n != sizeof(*val))
569 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
570 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
571 *val = be16toh(*val);
572 return NULL;
575 static const struct got_error *
576 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
578 const struct got_error *err = NULL;
579 const size_t chunk_size = 8;
580 size_t n, len = 0, totlen = chunk_size;
582 *path = malloc(totlen);
583 if (*path == NULL)
584 return got_error_from_errno("malloc");
586 do {
587 if (len + chunk_size > totlen) {
588 char *p = reallocarray(*path, totlen + chunk_size, 1);
589 if (p == NULL) {
590 err = got_error_from_errno("reallocarray");
591 break;
593 totlen += chunk_size;
594 *path = p;
596 n = fread(*path + len, 1, chunk_size, infile);
597 if (n != chunk_size) {
598 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
599 break;
601 SHA1Update(ctx, *path + len, chunk_size);
602 len += chunk_size;
603 } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
605 if (err) {
606 free(*path);
607 *path = NULL;
609 return err;
612 static const struct got_error *
613 read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
614 FILE *infile, uint32_t version)
616 const struct got_error *err;
617 struct got_fileindex_entry *ie;
618 size_t n;
620 *iep = NULL;
622 ie = calloc(1, sizeof(*ie));
623 if (ie == NULL)
624 return got_error_from_errno("calloc");
626 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
627 if (err)
628 goto done;
629 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
630 if (err)
631 goto done;
632 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
633 if (err)
634 goto done;
635 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
636 if (err)
637 goto done;
639 err = read_fileindex_val32(&ie->uid, ctx, infile);
640 if (err)
641 goto done;
642 err = read_fileindex_val32(&ie->gid, ctx, infile);
643 if (err)
644 goto done;
645 err = read_fileindex_val32(&ie->size, ctx, infile);
646 if (err)
647 goto done;
649 err = read_fileindex_val16(&ie->mode, ctx, infile);
650 if (err)
651 goto done;
653 n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
654 if (n != SHA1_DIGEST_LENGTH) {
655 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
656 goto done;
658 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
660 n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
661 if (n != SHA1_DIGEST_LENGTH) {
662 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
663 goto done;
665 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
667 err = read_fileindex_val32(&ie->flags, ctx, infile);
668 if (err)
669 goto done;
671 err = read_fileindex_path(&ie->path, ctx, infile);
672 if (err)
673 goto done;
675 if (version >= 2) {
676 uint32_t stage = got_fileindex_entry_stage_get(ie);
677 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
678 stage == GOT_FILEIDX_STAGE_ADD) {
679 n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
680 infile);
681 if (n != SHA1_DIGEST_LENGTH) {
682 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
683 goto done;
685 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
687 } else {
688 /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
689 ie->flags &= ~GOT_FILEIDX_F_STAGE;
692 done:
693 if (err)
694 got_fileindex_entry_free(ie);
695 else
696 *iep = ie;
697 return err;
700 const struct got_error *
701 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
703 const struct got_error *err = NULL;
704 struct got_fileindex_hdr hdr;
705 SHA1_CTX ctx;
706 struct got_fileindex_entry *ie;
707 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
708 uint8_t sha1[SHA1_DIGEST_LENGTH];
709 size_t n;
710 int i;
712 SHA1Init(&ctx);
714 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
715 if (n != sizeof(hdr.signature)) {
716 if (n == 0) /* EOF */
717 return NULL;
718 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
720 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
721 if (n != sizeof(hdr.version)) {
722 if (n == 0) /* EOF */
723 return NULL;
724 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
726 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
727 if (n != sizeof(hdr.nentries)) {
728 if (n == 0) /* EOF */
729 return NULL;
730 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
733 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
734 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
735 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
737 hdr.signature = be32toh(hdr.signature);
738 hdr.version = be32toh(hdr.version);
739 hdr.nentries = be32toh(hdr.nentries);
741 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
742 return got_error(GOT_ERR_FILEIDX_SIG);
743 if (hdr.version > GOT_FILE_INDEX_VERSION)
744 return got_error(GOT_ERR_FILEIDX_VER);
746 for (i = 0; i < hdr.nentries; i++) {
747 err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
748 if (err)
749 return err;
750 err = add_entry(fileindex, ie);
751 if (err)
752 return err;
755 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
756 if (n != sizeof(sha1_expected))
757 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
758 SHA1Final(sha1, &ctx);
759 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
760 return got_error(GOT_ERR_FILEIDX_CSUM);
762 return NULL;
765 static struct got_fileindex_entry *
766 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
768 struct got_fileindex_entry *next;
770 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
772 /* Skip entries which were added or removed by diff callbacks. */
773 while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
774 GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
775 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
777 return next;
780 static const struct got_error *
781 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
782 struct got_tree_object *tree, const char *, const char *,
783 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
785 static const struct got_error *
786 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
787 struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
788 const char *path, const char *entry_name, struct got_repository *repo,
789 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
791 const struct got_error *err = NULL;
792 struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
794 if (!got_object_tree_entry_is_submodule(te) &&
795 S_ISDIR(got_tree_entry_get_mode(te))) {
796 char *subpath;
797 struct got_tree_object *subtree;
799 if (asprintf(&subpath, "%s%s%s", path,
800 path[0] == '\0' ? "" : "/",
801 got_tree_entry_get_name(te)) == -1)
802 return got_error_from_errno("asprintf");
804 err = got_object_open_as_tree(&subtree, repo,
805 got_tree_entry_get_id(te));
806 if (err) {
807 free(subpath);
808 return err;
811 err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
812 entry_name, repo, cb, cb_arg);
813 free(subpath);
814 got_object_tree_close(subtree);
815 if (err)
816 return err;
819 (*tidx)++;
820 *next = got_object_tree_get_entry(tree, *tidx);
821 return NULL;
824 static const struct got_error *
825 diff_fileindex_tree(struct got_fileindex *fileindex,
826 struct got_fileindex_entry **ie, struct got_tree_object *tree,
827 const char *path, const char *entry_name, struct got_repository *repo,
828 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
830 const struct got_error *err = NULL;
831 struct got_tree_entry *te = NULL;
832 size_t path_len = strlen(path);
833 struct got_fileindex_entry *next;
834 int tidx = 0;
836 te = got_object_tree_get_entry(tree, tidx);
837 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
838 if (te && *ie) {
839 char *te_path;
840 const char *te_name = got_tree_entry_get_name(te);
841 int cmp;
842 if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
843 err = got_error_from_errno("asprintf");
844 break;
846 cmp = got_path_cmp((*ie)->path, te_path,
847 got_fileindex_entry_path_len(*ie), strlen(te_path));
848 free(te_path);
849 if (cmp == 0) {
850 if (got_path_is_child((*ie)->path, path,
851 path_len) &&
852 !got_object_tree_entry_is_submodule(te) &&
853 (entry_name == NULL ||
854 strcmp(te_name, entry_name) == 0)) {
855 err = cb->diff_old_new(cb_arg, *ie, te,
856 path);
857 if (err || entry_name)
858 break;
860 *ie = walk_fileindex(fileindex, *ie);
861 err = walk_tree(&te, fileindex, ie, tree, &tidx,
862 path, entry_name, repo, cb, cb_arg);
863 } else if (cmp < 0) {
864 next = walk_fileindex(fileindex, *ie);
865 if (got_path_is_child((*ie)->path, path,
866 path_len) && entry_name == NULL) {
867 err = cb->diff_old(cb_arg, *ie, path);
868 if (err || entry_name)
869 break;
871 *ie = next;
872 } else {
873 if ((entry_name == NULL ||
874 strcmp(te_name, entry_name) == 0)) {
875 err = cb->diff_new(cb_arg, te, path);
876 if (err || entry_name)
877 break;
879 err = walk_tree(&te, fileindex, ie, tree, &tidx,
880 path, entry_name, repo, cb, cb_arg);
882 if (err)
883 break;
884 } else if (*ie) {
885 next = walk_fileindex(fileindex, *ie);
886 if (got_path_is_child((*ie)->path, path, path_len) &&
887 (entry_name == NULL ||
888 (te && strcmp(got_tree_entry_get_name(te),
889 entry_name) == 0))) {
890 err = cb->diff_old(cb_arg, *ie, path);
891 if (err || entry_name)
892 break;
894 *ie = next;
895 } else if (te) {
896 if (!got_object_tree_entry_is_submodule(te) &&
897 (entry_name == NULL ||
898 strcmp(got_tree_entry_get_name(te), entry_name)
899 == 0)) {
900 err = cb->diff_new(cb_arg, te, path);
901 if (err || entry_name)
902 break;
904 err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
905 entry_name, repo, cb, cb_arg);
906 if (err)
907 break;
911 return err;
914 const struct got_error *
915 got_fileindex_diff_tree(struct got_fileindex *fileindex,
916 struct got_tree_object *tree, const char *path, const char *entry_name,
917 struct got_repository *repo,
918 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
920 struct got_fileindex_entry *ie;
921 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
922 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
923 ie = walk_fileindex(fileindex, ie);
924 return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
925 cb, cb_arg);
928 static const struct got_error *
929 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
930 struct got_pathlist_head *, int, const char *, const char *,
931 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
933 static const struct got_error *
934 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
936 const struct got_error *err = NULL;
937 struct got_pathlist_entry *new = NULL;
938 struct dirent *dep = NULL;
939 struct dirent *de = NULL;
941 for (;;) {
942 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
943 if (de == NULL) {
944 err = got_error_from_errno("malloc");
945 break;
948 if (readdir_r(dir, de, &dep) != 0) {
949 err = got_error_from_errno("readdir_r");
950 free(de);
951 break;
953 if (dep == NULL) {
954 free(de);
955 break;
958 if (strcmp(de->d_name, ".") == 0 ||
959 strcmp(de->d_name, "..") == 0 ||
960 (path[0] == '\0' &&
961 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
962 free(de);
963 continue;
966 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
967 if (err) {
968 free(de);
969 break;
971 if (new == NULL) {
972 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
973 free(de);
974 break;
978 return err;
981 void
982 free_dirlist(struct got_pathlist_head *dirlist)
984 struct got_pathlist_entry *dle;
986 TAILQ_FOREACH(dle, dirlist, entry)
987 free(dle->data);
988 got_pathlist_free(dirlist);
991 static int
992 have_tracked_file_in_dir(struct got_fileindex *fileindex, const char *path)
994 struct got_fileindex_entry *ie;
995 size_t path_len = strlen(path);
996 int cmp;
998 ie = RB_ROOT(&fileindex->entries);
999 while (ie) {
1000 if (got_path_is_child(ie->path, path, path_len))
1001 return 1;
1002 cmp = got_path_cmp(path, ie->path, path_len,
1003 got_fileindex_entry_path_len(ie));
1004 if (cmp < 0)
1005 ie = RB_LEFT(ie, entry);
1006 else if (cmp > 0)
1007 ie = RB_RIGHT(ie, entry);
1008 else
1009 break;
1012 return 0;
1015 static const struct got_error *
1016 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
1017 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
1018 const char *path, const char *rootpath, struct got_repository *repo,
1019 int ignore, struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1021 const struct got_error *err = NULL;
1022 struct dirent *de = dle->data;
1023 DIR *subdir = NULL;
1024 int subdirfd = -1;
1025 int type;
1027 *next = NULL;
1029 if (de->d_type == DT_UNKNOWN) {
1030 /* Occurs on NFS mounts without "readdir plus" RPC. */
1031 char *dir_path;
1032 if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
1033 return got_error_from_errno("asprintf");
1034 err = got_path_dirent_type(&type, dir_path, de);
1035 free(dir_path);
1036 if (err)
1037 return err;
1038 } else
1039 type = de->d_type;
1041 /* Must traverse ignored directories if they contain tracked files. */
1042 if (type == DT_DIR && ignore &&
1043 have_tracked_file_in_dir(fileindex, path))
1044 ignore = 0;
1046 if (type == DT_DIR && !ignore) {
1047 char *subpath;
1048 char *subdirpath;
1049 struct got_pathlist_head subdirlist;
1051 TAILQ_INIT(&subdirlist);
1053 if (asprintf(&subpath, "%s%s%s", path,
1054 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1055 return got_error_from_errno("asprintf");
1057 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
1058 free(subpath);
1059 return got_error_from_errno("asprintf");
1062 subdirfd = openat(fd, de->d_name,
1063 O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
1064 if (subdirfd == -1) {
1065 if (errno == EACCES) {
1066 *next = TAILQ_NEXT(dle, entry);
1067 return NULL;
1069 err = got_error_from_errno2("openat", subdirpath);
1070 free(subpath);
1071 free(subdirpath);
1072 return err;
1075 subdir = fdopendir(subdirfd);
1076 if (subdir == NULL)
1077 return got_error_from_errno2("fdopendir", path);
1078 subdirfd = -1;
1079 err = read_dirlist(&subdirlist, subdir, subdirpath);
1080 if (err) {
1081 free(subpath);
1082 free(subdirpath);
1083 closedir(subdir);
1084 return err;
1086 err = diff_fileindex_dir(fileindex, ie, &subdirlist,
1087 dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
1088 if (subdir && closedir(subdir) == -1 && err == NULL)
1089 err = got_error_from_errno2("closedir", subdirpath);
1090 free(subpath);
1091 free(subdirpath);
1092 free_dirlist(&subdirlist);
1093 if (err)
1094 return err;
1097 *next = TAILQ_NEXT(dle, entry);
1098 return NULL;
1101 static const struct got_error *
1102 diff_fileindex_dir(struct got_fileindex *fileindex,
1103 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1104 int dirfd, const char *rootpath, const char *path,
1105 struct got_repository *repo,
1106 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1108 const struct got_error *err = NULL;
1109 struct dirent *de = NULL;
1110 size_t path_len = strlen(path);
1111 struct got_pathlist_entry *dle;
1112 int ignore;
1114 if (cb->diff_traverse) {
1115 err = cb->diff_traverse(cb_arg, path, dirfd);
1116 if (err)
1117 return err;
1120 dle = TAILQ_FIRST(dirlist);
1121 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1122 if (dle && *ie) {
1123 char *de_path;
1124 int cmp;
1125 de = dle->data;
1126 if (asprintf(&de_path, "%s/%s", path,
1127 de->d_name) == -1) {
1128 err = got_error_from_errno("asprintf");
1129 break;
1131 cmp = got_path_cmp((*ie)->path, de_path,
1132 got_fileindex_entry_path_len(*ie),
1133 strlen(path) + 1 + de->d_namlen);
1134 free(de_path);
1135 if (cmp == 0) {
1136 err = cb->diff_old_new(cb_arg, *ie, de, path,
1137 dirfd);
1138 if (err)
1139 break;
1140 *ie = walk_fileindex(fileindex, *ie);
1141 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1142 path, rootpath, repo, 0, cb, cb_arg);
1143 } else if (cmp < 0 ) {
1144 err = cb->diff_old(cb_arg, *ie, path);
1145 if (err)
1146 break;
1147 *ie = walk_fileindex(fileindex, *ie);
1148 } else {
1149 err = cb->diff_new(&ignore, cb_arg, de, path,
1150 dirfd);
1151 if (err)
1152 break;
1153 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1154 path, rootpath, repo, ignore, cb, cb_arg);
1156 if (err)
1157 break;
1158 } else if (*ie) {
1159 err = cb->diff_old(cb_arg, *ie, path);
1160 if (err)
1161 break;
1162 *ie = walk_fileindex(fileindex, *ie);
1163 } else if (dle) {
1164 de = dle->data;
1165 err = cb->diff_new(&ignore, cb_arg, de, path, dirfd);
1166 if (err)
1167 break;
1168 err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1169 rootpath, repo, ignore, cb, cb_arg);
1170 if (err)
1171 break;
1175 return err;
1178 const struct got_error *
1179 got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1180 const char *rootpath, const char *path, struct got_repository *repo,
1181 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1183 const struct got_error *err;
1184 struct got_fileindex_entry *ie;
1185 struct got_pathlist_head dirlist;
1186 int fd2;
1187 DIR *dir;
1189 TAILQ_INIT(&dirlist);
1192 * Duplicate the file descriptor so we can call closedir() below
1193 * without closing the file descriptor passed in by our caller.
1195 fd2 = dup(fd);
1196 if (fd2 == -1)
1197 return got_error_from_errno2("dup", path);
1198 if (lseek(fd2, 0, SEEK_SET) == -1) {
1199 err = got_error_from_errno2("lseek", path);
1200 close(fd2);
1201 return err;
1203 dir = fdopendir(fd2);
1204 if (dir == NULL) {
1205 err = got_error_from_errno2("fdopendir", path);
1206 close(fd2);
1207 return err;
1209 err = read_dirlist(&dirlist, dir, path);
1210 if (err) {
1211 closedir(dir);
1212 return err;
1215 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1216 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1217 ie = walk_fileindex(fileindex, ie);
1218 err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1219 rootpath, path, repo, cb, cb_arg);
1221 if (closedir(dir) == -1 && err == NULL)
1222 err = got_error_from_errno2("closedir", path);
1223 free_dirlist(&dirlist);
1224 return err;
1227 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);