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/stat.h>
20 #include <errno.h>
21 #include <dirent.h>
22 #include <fcntl.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <limits.h>
27 #include <unistd.h>
29 #include "got_compat.h"
31 #include "got_error.h"
32 #include "got_object.h"
33 #include "got_path.h"
35 #include "got_lib_fileindex.h"
36 #include "got_lib_worktree.h"
38 /* got_fileindex_entry flags */
39 #define GOT_FILEIDX_F_PATH_LEN 0x00000fff
40 #define GOT_FILEIDX_F_STAGE 0x0000f000
41 #define GOT_FILEIDX_F_STAGE_SHIFT 12
42 #define GOT_FILEIDX_F_NOT_FLUSHED 0x00010000
43 #define GOT_FILEIDX_F_NO_BLOB 0x00020000
44 #define GOT_FILEIDX_F_NO_COMMIT 0x00040000
45 #define GOT_FILEIDX_F_NO_FILE_ON_DISK 0x00080000
46 #define GOT_FILEIDX_F_REMOVE_ON_FLUSH 0x00100000
47 #define GOT_FILEIDX_F_SKIPPED 0x00200000
49 struct got_fileindex {
50 struct got_fileindex_tree entries;
51 int nentries; /* Does not include entries marked for removal. */
52 #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
53 };
55 mode_t
56 got_fileindex_entry_perms_get(struct got_fileindex_entry *ie)
57 {
58 return ((ie->mode & GOT_FILEIDX_MODE_PERMS) >>
59 GOT_FILEIDX_MODE_PERMS_SHIFT);
60 }
62 static void
63 fileindex_entry_perms_set(struct got_fileindex_entry *ie, mode_t mode)
64 {
65 ie->mode &= ~GOT_FILEIDX_MODE_PERMS;
66 ie->mode |= ((mode << GOT_FILEIDX_MODE_PERMS_SHIFT) &
67 GOT_FILEIDX_MODE_PERMS);
68 }
70 mode_t
71 got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
72 {
73 mode_t perms = got_fileindex_entry_perms_get(ie);
74 int type = got_fileindex_entry_filetype_get(ie);
75 uint32_t ftype;
77 if (type == GOT_FILEIDX_MODE_REGULAR_FILE ||
78 type == GOT_FILEIDX_MODE_BAD_SYMLINK)
79 ftype = S_IFREG;
80 else
81 ftype = S_IFLNK;
83 return (ftype | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
84 }
86 const struct got_error *
87 got_fileindex_entry_update(struct got_fileindex_entry *ie,
88 int wt_fd, const char *ondisk_path, uint8_t *blob_sha1,
89 uint8_t *commit_sha1, int update_timestamps)
90 {
91 struct stat sb;
93 if (fstatat(wt_fd, ondisk_path, &sb, AT_SYMLINK_NOFOLLOW) != 0) {
94 if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
95 errno == ENOENT))
96 return got_error_from_errno2("fstatat", ondisk_path);
97 sb.st_mode = GOT_DEFAULT_FILE_MODE;
98 } else {
99 if (sb.st_mode & S_IFDIR)
100 return got_error_set_errno(EISDIR, ondisk_path);
101 ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
105 if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
106 if (update_timestamps) {
107 ie->ctime_sec = sb.st_ctim.tv_sec;
108 ie->ctime_nsec = sb.st_ctim.tv_nsec;
109 ie->mtime_sec = sb.st_mtim.tv_sec;
110 ie->mtime_nsec = sb.st_mtim.tv_nsec;
112 ie->uid = sb.st_uid;
113 ie->gid = sb.st_gid;
114 ie->size = (sb.st_size & 0xffffffff);
115 if (S_ISLNK(sb.st_mode)) {
116 got_fileindex_entry_filetype_set(ie,
117 GOT_FILEIDX_MODE_SYMLINK);
118 fileindex_entry_perms_set(ie, 0);
119 } else {
120 got_fileindex_entry_filetype_set(ie,
121 GOT_FILEIDX_MODE_REGULAR_FILE);
122 fileindex_entry_perms_set(ie,
123 sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
127 if (blob_sha1) {
128 memcpy(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
129 ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
130 } else
131 ie->flags |= GOT_FILEIDX_F_NO_BLOB;
133 if (commit_sha1) {
134 memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
135 ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
136 } else
137 ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
139 return NULL;
142 void
143 got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
145 ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
148 void
149 got_fileindex_entry_mark_skipped(struct got_fileindex_entry *ie)
151 ie->flags |= GOT_FILEIDX_F_SKIPPED;
154 const struct got_error *
155 got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
156 const char *relpath)
158 size_t len;
160 *ie = calloc(1, sizeof(**ie));
161 if (*ie == NULL)
162 return got_error_from_errno("calloc");
164 (*ie)->path = strdup(relpath);
165 if ((*ie)->path == NULL) {
166 const struct got_error *err = got_error_from_errno("strdup");
167 free(*ie);
168 *ie = NULL;
169 return err;
172 len = strlen(relpath);
173 if (len > GOT_FILEIDX_F_PATH_LEN)
174 len = GOT_FILEIDX_F_PATH_LEN;
175 (*ie)->flags |= len;
177 return NULL;
180 void
181 got_fileindex_entry_free(struct got_fileindex_entry *ie)
183 free(ie->path);
184 free(ie);
187 size_t
188 got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
190 return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
193 uint32_t
194 got_fileindex_entry_stage_get(const struct got_fileindex_entry *ie)
196 return ((ie->flags & GOT_FILEIDX_F_STAGE) >> GOT_FILEIDX_F_STAGE_SHIFT);
199 void
200 got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage)
202 ie->flags &= ~GOT_FILEIDX_F_STAGE;
203 ie->flags |= ((stage << GOT_FILEIDX_F_STAGE_SHIFT) &
204 GOT_FILEIDX_F_STAGE);
207 int
208 got_fileindex_entry_filetype_get(struct got_fileindex_entry *ie)
210 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
213 void
214 got_fileindex_entry_filetype_set(struct got_fileindex_entry *ie, int type)
216 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_ONDISK;
217 ie->mode |= (type & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
220 void
221 got_fileindex_entry_staged_filetype_set(struct got_fileindex_entry *ie,
222 int type)
224 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_STAGED;
225 ie->mode |= ((type << GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT) &
226 GOT_FILEIDX_MODE_FILE_TYPE_STAGED);
229 int
230 got_fileindex_entry_staged_filetype_get(struct got_fileindex_entry *ie)
232 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_STAGED) >>
233 GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT;
236 int
237 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
239 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
242 int
243 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
245 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
248 int
249 got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
251 return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
254 int
255 got_fileindex_entry_was_skipped(struct got_fileindex_entry *ie)
257 return (ie->flags & GOT_FILEIDX_F_SKIPPED) != 0;
260 static const struct got_error *
261 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
263 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
264 return got_error(GOT_ERR_NO_SPACE);
266 if (RB_INSERT(got_fileindex_tree, &fileindex->entries, ie) != NULL)
267 return got_error_path(ie->path, GOT_ERR_FILEIDX_DUP_ENTRY);
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 static 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;
1026 *next = NULL;
1028 /* Must traverse ignored directories if they contain tracked files. */
1029 if (de->d_type == DT_DIR && ignore &&
1030 have_tracked_file_in_dir(fileindex, path))
1031 ignore = 0;
1033 if (de->d_type == DT_DIR && !ignore) {
1034 char *subpath;
1035 char *subdirpath;
1036 struct got_pathlist_head subdirlist;
1038 TAILQ_INIT(&subdirlist);
1040 if (asprintf(&subpath, "%s%s%s", path,
1041 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1042 return got_error_from_errno("asprintf");
1044 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
1045 free(subpath);
1046 return got_error_from_errno("asprintf");
1049 subdirfd = openat(fd, de->d_name,
1050 O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
1051 if (subdirfd == -1) {
1052 if (errno == EACCES) {
1053 *next = TAILQ_NEXT(dle, entry);
1054 return NULL;
1056 err = got_error_from_errno2("openat", subdirpath);
1057 free(subpath);
1058 free(subdirpath);
1059 return err;
1062 subdir = fdopendir(subdirfd);
1063 if (subdir == NULL)
1064 return got_error_from_errno2("fdopendir", path);
1065 subdirfd = -1;
1066 err = read_dirlist(&subdirlist, subdir, subdirpath);
1067 if (err) {
1068 free(subpath);
1069 free(subdirpath);
1070 closedir(subdir);
1071 return err;
1073 err = diff_fileindex_dir(fileindex, ie, &subdirlist,
1074 dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
1075 if (subdir && closedir(subdir) == -1 && err == NULL)
1076 err = got_error_from_errno2("closedir", subdirpath);
1077 free(subpath);
1078 free(subdirpath);
1079 free_dirlist(&subdirlist);
1080 if (err)
1081 return err;
1084 *next = TAILQ_NEXT(dle, entry);
1085 return NULL;
1088 static const struct got_error *
1089 dirent_type_fixup(struct dirent *de, const char *rootpath, const char *path)
1091 const struct got_error *err;
1092 char *dir_path;
1093 int type;
1095 if (de->d_type != DT_UNKNOWN)
1096 return NULL;
1098 /* DT_UNKNOWN occurs on NFS mounts without "readdir plus" RPC. */
1099 if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
1100 return got_error_from_errno("asprintf");
1101 err = got_path_dirent_type(&type, dir_path, de);
1102 free(dir_path);
1103 if (err)
1104 return err;
1106 de->d_type = type;
1107 return NULL;
1110 static const struct got_error *
1111 diff_fileindex_dir(struct got_fileindex *fileindex,
1112 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1113 int dirfd, const char *rootpath, const char *path,
1114 struct got_repository *repo,
1115 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1117 const struct got_error *err = NULL;
1118 struct dirent *de = NULL;
1119 size_t path_len = strlen(path);
1120 struct got_pathlist_entry *dle;
1121 int ignore;
1123 if (cb->diff_traverse) {
1124 err = cb->diff_traverse(cb_arg, path, dirfd);
1125 if (err)
1126 return err;
1129 dle = TAILQ_FIRST(dirlist);
1130 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1131 if (dle && *ie) {
1132 char *de_path;
1133 int cmp;
1134 de = dle->data;
1135 err = dirent_type_fixup(de, rootpath, path);
1136 if (err)
1137 break;
1138 if (asprintf(&de_path, "%s/%s", path,
1139 de->d_name) == -1) {
1140 err = got_error_from_errno("asprintf");
1141 break;
1143 cmp = got_path_cmp((*ie)->path, de_path,
1144 got_fileindex_entry_path_len(*ie),
1145 strlen(path) + 1 + strlen(de->d_name));
1146 free(de_path);
1147 if (cmp == 0) {
1148 err = cb->diff_old_new(cb_arg, *ie, de, path,
1149 dirfd);
1150 if (err)
1151 break;
1152 *ie = walk_fileindex(fileindex, *ie);
1153 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1154 path, rootpath, repo, 0, cb, cb_arg);
1155 } else if (cmp < 0 ) {
1156 err = cb->diff_old(cb_arg, *ie, path);
1157 if (err)
1158 break;
1159 *ie = walk_fileindex(fileindex, *ie);
1160 } else {
1161 err = cb->diff_new(&ignore, cb_arg, de, path,
1162 dirfd);
1163 if (err)
1164 break;
1165 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1166 path, rootpath, repo, ignore, cb, cb_arg);
1168 if (err)
1169 break;
1170 } else if (*ie) {
1171 err = cb->diff_old(cb_arg, *ie, path);
1172 if (err)
1173 break;
1174 *ie = walk_fileindex(fileindex, *ie);
1175 } else if (dle) {
1176 de = dle->data;
1177 err = dirent_type_fixup(de, rootpath, path);
1178 if (err)
1179 break;
1180 err = cb->diff_new(&ignore, cb_arg, de, path, dirfd);
1181 if (err)
1182 break;
1183 err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1184 rootpath, repo, ignore, cb, cb_arg);
1185 if (err)
1186 break;
1190 return err;
1193 const struct got_error *
1194 got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1195 const char *rootpath, const char *path, struct got_repository *repo,
1196 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1198 const struct got_error *err;
1199 struct got_fileindex_entry *ie;
1200 struct got_pathlist_head dirlist;
1201 int fd2;
1202 DIR *dir;
1204 TAILQ_INIT(&dirlist);
1207 * Duplicate the file descriptor so we can call closedir() below
1208 * without closing the file descriptor passed in by our caller.
1210 fd2 = dup(fd);
1211 if (fd2 == -1)
1212 return got_error_from_errno2("dup", path);
1213 if (lseek(fd2, 0, SEEK_SET) == -1) {
1214 err = got_error_from_errno2("lseek", path);
1215 close(fd2);
1216 return err;
1218 dir = fdopendir(fd2);
1219 if (dir == NULL) {
1220 err = got_error_from_errno2("fdopendir", path);
1221 close(fd2);
1222 return err;
1224 err = read_dirlist(&dirlist, dir, path);
1225 if (err) {
1226 closedir(dir);
1227 return err;
1230 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1231 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1232 ie = walk_fileindex(fileindex, ie);
1233 err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1234 rootpath, path, repo, cb, cb_arg);
1236 if (closedir(dir) == -1 && err == NULL)
1237 err = got_error_from_errno2("closedir", path);
1238 free_dirlist(&dirlist);
1239 return err;
1242 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);