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 "got_compat.h"
19 #include <sys/queue.h>
20 #include <sys/stat.h>
22 #include <errno.h>
23 #include <dirent.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <limits.h>
29 #include <unistd.h>
31 #include "got_error.h"
32 #include "got_object.h"
33 #include "got_path.h"
35 #include "got_lib_hash.h"
36 #include "got_lib_fileindex.h"
37 #include "got_lib_worktree.h"
39 /* got_fileindex_entry flags */
40 #define GOT_FILEIDX_F_PATH_LEN 0x00000fff
41 #define GOT_FILEIDX_F_STAGE 0x0000f000
42 #define GOT_FILEIDX_F_STAGE_SHIFT 12
43 #define GOT_FILEIDX_F_NOT_FLUSHED 0x00010000
44 #define GOT_FILEIDX_F_NO_BLOB 0x00020000
45 #define GOT_FILEIDX_F_NO_COMMIT 0x00040000
46 #define GOT_FILEIDX_F_NO_FILE_ON_DISK 0x00080000
47 #define GOT_FILEIDX_F_REMOVE_ON_FLUSH 0x00100000
48 #define GOT_FILEIDX_F_SKIPPED 0x00200000
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 mode_t
57 got_fileindex_entry_perms_get(struct got_fileindex_entry *ie)
58 {
59 return ((ie->mode & GOT_FILEIDX_MODE_PERMS) >>
60 GOT_FILEIDX_MODE_PERMS_SHIFT);
61 }
63 static void
64 fileindex_entry_perms_set(struct got_fileindex_entry *ie, mode_t mode)
65 {
66 ie->mode &= ~GOT_FILEIDX_MODE_PERMS;
67 ie->mode |= ((mode << GOT_FILEIDX_MODE_PERMS_SHIFT) &
68 GOT_FILEIDX_MODE_PERMS);
69 }
71 mode_t
72 got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
73 {
74 mode_t perms = got_fileindex_entry_perms_get(ie);
75 int type = got_fileindex_entry_filetype_get(ie);
76 uint32_t ftype;
78 if (type == GOT_FILEIDX_MODE_REGULAR_FILE ||
79 type == GOT_FILEIDX_MODE_BAD_SYMLINK)
80 ftype = S_IFREG;
81 else
82 ftype = S_IFLNK;
84 return (ftype | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
85 }
87 const struct got_error *
88 got_fileindex_entry_update(struct got_fileindex_entry *ie,
89 int wt_fd, const char *ondisk_path, uint8_t *blob_sha1,
90 uint8_t *commit_sha1, int update_timestamps)
91 {
92 struct stat sb;
94 if (fstatat(wt_fd, ondisk_path, &sb, AT_SYMLINK_NOFOLLOW) != 0) {
95 if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
96 errno == ENOENT))
97 return got_error_from_errno2("fstatat", ondisk_path);
98 sb.st_mode = GOT_DEFAULT_FILE_MODE;
99 } else {
100 if (sb.st_mode & S_IFDIR)
101 return got_error_set_errno(EISDIR, ondisk_path);
102 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 memmove(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(struct got_hash *ctx, uint64_t val, FILE *outfile)
358 size_t n;
360 val = htobe64(val);
361 got_hash_update(ctx, &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(struct got_hash *ctx, uint32_t val, FILE *outfile)
371 size_t n;
373 val = htobe32(val);
374 got_hash_update(ctx, &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(struct got_hash *ctx, uint16_t val, FILE *outfile)
384 size_t n;
386 val = htobe16(val);
387 got_hash_update(ctx, &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(struct got_hash *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 got_hash_update(ctx, path, len);
407 n = fwrite(path, 1, len, outfile);
408 if (n != len)
409 return got_ferror(outfile, GOT_ERR_IO);
410 got_hash_update(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(struct got_hash *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 got_hash_update(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 got_hash_update(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 got_hash_update(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 struct got_hash ctx;
489 uint8_t hash[GOT_HASH_DIGEST_MAXLEN];
490 size_t n;
491 struct got_fileindex_entry *ie, *tmp;
493 got_hash_init(&ctx, GOT_HASH_SHA1);
495 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
496 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
497 hdr.nentries = htobe32(fileindex->nentries);
499 got_hash_update(&ctx, &hdr.signature, sizeof(hdr.signature));
500 got_hash_update(&ctx, &hdr.version, sizeof(hdr.version));
501 got_hash_update(&ctx, &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 got_hash_final(&ctx, hash);
526 n = fwrite(hash, 1, SHA1_DIGEST_LENGTH, outfile);
527 if (n != SHA1_DIGEST_LENGTH)
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, struct got_hash *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 got_hash_update(ctx, val, sizeof(*val));
545 *val = be64toh(*val);
546 return NULL;
549 static const struct got_error *
550 read_fileindex_val32(uint32_t *val, struct got_hash *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 got_hash_update(ctx, val, sizeof(*val));
558 *val = be32toh(*val);
559 return NULL;
562 static const struct got_error *
563 read_fileindex_val16(uint16_t *val, struct got_hash *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 got_hash_update(ctx, val, sizeof(*val));
571 *val = be16toh(*val);
572 return NULL;
575 static const struct got_error *
576 read_fileindex_path(char **path, struct got_hash *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 got_hash_update(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, struct got_hash *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 got_hash_update(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 got_hash_update(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 got_hash_update(ctx, ie->staged_blob_sha1,
686 SHA1_DIGEST_LENGTH);
688 } else {
689 /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
690 ie->flags &= ~GOT_FILEIDX_F_STAGE;
693 done:
694 if (err)
695 got_fileindex_entry_free(ie);
696 else
697 *iep = ie;
698 return err;
701 const struct got_error *
702 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
704 const struct got_error *err = NULL;
705 struct got_fileindex_hdr hdr;
706 struct got_hash ctx;
707 struct got_fileindex_entry *ie;
708 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
709 uint8_t sha1[SHA1_DIGEST_LENGTH];
710 size_t n;
711 int i;
713 got_hash_init(&ctx, GOT_HASH_SHA1);
715 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
716 if (n != sizeof(hdr.signature)) {
717 if (n == 0) /* EOF */
718 return NULL;
719 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
721 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
722 if (n != sizeof(hdr.version)) {
723 if (n == 0) /* EOF */
724 return NULL;
725 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
727 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
728 if (n != sizeof(hdr.nentries)) {
729 if (n == 0) /* EOF */
730 return NULL;
731 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
734 got_hash_update(&ctx, &hdr.signature, sizeof(hdr.signature));
735 got_hash_update(&ctx, &hdr.version, sizeof(hdr.version));
736 got_hash_update(&ctx, &hdr.nentries, sizeof(hdr.nentries));
738 hdr.signature = be32toh(hdr.signature);
739 hdr.version = be32toh(hdr.version);
740 hdr.nentries = be32toh(hdr.nentries);
742 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
743 return got_error(GOT_ERR_FILEIDX_SIG);
744 if (hdr.version > GOT_FILE_INDEX_VERSION)
745 return got_error(GOT_ERR_FILEIDX_VER);
747 for (i = 0; i < hdr.nentries; i++) {
748 err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
749 if (err)
750 return err;
751 err = add_entry(fileindex, ie);
752 if (err) {
753 got_fileindex_entry_free(ie);
754 return err;
758 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
759 if (n != sizeof(sha1_expected))
760 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
761 got_hash_final(&ctx, sha1);
762 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
763 return got_error(GOT_ERR_FILEIDX_CSUM);
765 return NULL;
768 static struct got_fileindex_entry *
769 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
771 struct got_fileindex_entry *next;
773 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
775 /* Skip entries which were added or removed by diff callbacks. */
776 while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
777 GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
778 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
780 return next;
783 static const struct got_error *
784 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
785 struct got_tree_object *tree, const char *, const char *,
786 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
788 static const struct got_error *
789 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
790 struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
791 const char *path, const char *entry_name, struct got_repository *repo,
792 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
794 const struct got_error *err = NULL;
795 struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
797 if (!got_object_tree_entry_is_submodule(te) &&
798 S_ISDIR(got_tree_entry_get_mode(te))) {
799 char *subpath;
800 struct got_tree_object *subtree;
802 if (asprintf(&subpath, "%s%s%s", path,
803 path[0] == '\0' ? "" : "/",
804 got_tree_entry_get_name(te)) == -1)
805 return got_error_from_errno("asprintf");
807 err = got_object_open_as_tree(&subtree, repo,
808 got_tree_entry_get_id(te));
809 if (err) {
810 free(subpath);
811 return err;
814 err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
815 entry_name, repo, cb, cb_arg);
816 free(subpath);
817 got_object_tree_close(subtree);
818 if (err)
819 return err;
822 (*tidx)++;
823 *next = got_object_tree_get_entry(tree, *tidx);
824 return NULL;
827 static const struct got_error *
828 diff_fileindex_tree(struct got_fileindex *fileindex,
829 struct got_fileindex_entry **ie, struct got_tree_object *tree,
830 const char *path, const char *entry_name, struct got_repository *repo,
831 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
833 const struct got_error *err = NULL;
834 struct got_tree_entry *te = NULL;
835 size_t path_len = strlen(path);
836 struct got_fileindex_entry *next;
837 int tidx = 0;
839 te = got_object_tree_get_entry(tree, tidx);
840 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
841 if (te && *ie) {
842 char *te_path;
843 const char *te_name = got_tree_entry_get_name(te);
844 int cmp;
845 if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
846 err = got_error_from_errno("asprintf");
847 break;
849 cmp = got_path_cmp((*ie)->path, te_path,
850 got_fileindex_entry_path_len(*ie), strlen(te_path));
851 free(te_path);
852 if (cmp == 0) {
853 if (got_path_is_child((*ie)->path, path,
854 path_len) &&
855 !got_object_tree_entry_is_submodule(te) &&
856 (entry_name == NULL ||
857 strcmp(te_name, entry_name) == 0)) {
858 err = cb->diff_old_new(cb_arg, *ie, te,
859 path);
860 if (err || entry_name)
861 break;
863 *ie = walk_fileindex(fileindex, *ie);
864 err = walk_tree(&te, fileindex, ie, tree, &tidx,
865 path, entry_name, repo, cb, cb_arg);
866 } else if (cmp < 0) {
867 next = walk_fileindex(fileindex, *ie);
868 if (got_path_is_child((*ie)->path, path,
869 path_len) && entry_name == NULL) {
870 err = cb->diff_old(cb_arg, *ie, path);
871 if (err || entry_name)
872 break;
874 *ie = next;
875 } else {
876 if ((entry_name == NULL ||
877 strcmp(te_name, entry_name) == 0)) {
878 err = cb->diff_new(cb_arg, te, path);
879 if (err || entry_name)
880 break;
882 err = walk_tree(&te, fileindex, ie, tree, &tidx,
883 path, entry_name, repo, cb, cb_arg);
885 if (err)
886 break;
887 } else if (*ie) {
888 next = walk_fileindex(fileindex, *ie);
889 if (got_path_is_child((*ie)->path, path, path_len) &&
890 (entry_name == NULL ||
891 (te && strcmp(got_tree_entry_get_name(te),
892 entry_name) == 0))) {
893 err = cb->diff_old(cb_arg, *ie, path);
894 if (err || entry_name)
895 break;
897 *ie = next;
898 } else if (te) {
899 if (!got_object_tree_entry_is_submodule(te) &&
900 (entry_name == NULL ||
901 strcmp(got_tree_entry_get_name(te), entry_name)
902 == 0)) {
903 err = cb->diff_new(cb_arg, te, path);
904 if (err || entry_name)
905 break;
907 err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
908 entry_name, repo, cb, cb_arg);
909 if (err)
910 break;
914 return err;
917 const struct got_error *
918 got_fileindex_diff_tree(struct got_fileindex *fileindex,
919 struct got_tree_object *tree, const char *path, const char *entry_name,
920 struct got_repository *repo,
921 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
923 struct got_fileindex_entry *ie;
924 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
925 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
926 ie = walk_fileindex(fileindex, ie);
927 return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
928 cb, cb_arg);
931 static const struct got_error *
932 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
933 struct got_pathlist_head *, int, const char *, const char *,
934 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
936 static const struct got_error *
937 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
939 const struct got_error *err = NULL;
940 struct got_pathlist_entry *new = NULL;
941 struct dirent *dep = NULL;
942 struct dirent *de = NULL;
944 for (;;) {
945 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
946 if (de == NULL) {
947 err = got_error_from_errno("malloc");
948 break;
951 if (readdir_r(dir, de, &dep) != 0) {
952 err = got_error_from_errno("readdir_r");
953 free(de);
954 break;
956 if (dep == NULL) {
957 free(de);
958 break;
961 if (strcmp(de->d_name, ".") == 0 ||
962 strcmp(de->d_name, "..") == 0 ||
963 (path[0] == '\0' &&
964 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
965 free(de);
966 continue;
969 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
970 if (err) {
971 free(de);
972 break;
974 if (new == NULL) {
975 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
976 free(de);
977 break;
981 return err;
984 static int
985 have_tracked_file_in_dir(struct got_fileindex *fileindex, const char *path)
987 struct got_fileindex_entry *ie;
988 size_t path_len = strlen(path);
989 int cmp;
991 ie = RB_ROOT(&fileindex->entries);
992 while (ie) {
993 if (got_path_is_child(ie->path, path, path_len))
994 return 1;
995 cmp = got_path_cmp(path, ie->path, path_len,
996 got_fileindex_entry_path_len(ie));
997 if (cmp < 0)
998 ie = RB_LEFT(ie, entry);
999 else if (cmp > 0)
1000 ie = RB_RIGHT(ie, entry);
1001 else
1002 break;
1005 return 0;
1008 static const struct got_error *
1009 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
1010 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
1011 const char *path, const char *rootpath, struct got_repository *repo,
1012 int ignore, struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1014 const struct got_error *err = NULL;
1015 struct dirent *de = dle->data;
1016 DIR *subdir = NULL;
1017 int subdirfd = -1;
1019 *next = NULL;
1021 /* Must traverse ignored directories if they contain tracked files. */
1022 if (de->d_type == DT_DIR && ignore &&
1023 have_tracked_file_in_dir(fileindex, path))
1024 ignore = 0;
1026 if (de->d_type == DT_DIR && !ignore) {
1027 char *subpath;
1028 char *subdirpath;
1029 struct got_pathlist_head subdirlist;
1031 TAILQ_INIT(&subdirlist);
1033 if (asprintf(&subpath, "%s%s%s", path,
1034 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1035 return got_error_from_errno("asprintf");
1037 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
1038 free(subpath);
1039 return got_error_from_errno("asprintf");
1042 subdirfd = openat(fd, de->d_name,
1043 O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
1044 if (subdirfd == -1) {
1045 if (errno == EACCES) {
1046 *next = TAILQ_NEXT(dle, entry);
1047 return NULL;
1049 err = got_error_from_errno2("openat", subdirpath);
1050 free(subpath);
1051 free(subdirpath);
1052 return err;
1055 subdir = fdopendir(subdirfd);
1056 if (subdir == NULL)
1057 return got_error_from_errno2("fdopendir", path);
1058 subdirfd = -1;
1059 err = read_dirlist(&subdirlist, subdir, subdirpath);
1060 if (err) {
1061 free(subpath);
1062 free(subdirpath);
1063 closedir(subdir);
1064 return err;
1066 err = diff_fileindex_dir(fileindex, ie, &subdirlist,
1067 dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
1068 if (subdir && closedir(subdir) == -1 && err == NULL)
1069 err = got_error_from_errno2("closedir", subdirpath);
1070 free(subpath);
1071 free(subdirpath);
1072 got_pathlist_free(&subdirlist, GOT_PATHLIST_FREE_DATA);
1073 if (err)
1074 return err;
1077 *next = TAILQ_NEXT(dle, entry);
1078 return NULL;
1081 static const struct got_error *
1082 dirent_type_fixup(struct dirent *de, const char *rootpath, const char *path)
1084 const struct got_error *err;
1085 char *dir_path;
1086 int type;
1088 if (de->d_type != DT_UNKNOWN)
1089 return NULL;
1091 /* DT_UNKNOWN occurs on NFS mounts without "readdir plus" RPC. */
1092 if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
1093 return got_error_from_errno("asprintf");
1094 err = got_path_dirent_type(&type, dir_path, de);
1095 free(dir_path);
1096 if (err)
1097 return err;
1099 de->d_type = type;
1100 return NULL;
1103 static const struct got_error *
1104 diff_fileindex_dir(struct got_fileindex *fileindex,
1105 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1106 int dirfd, const char *rootpath, const char *path,
1107 struct got_repository *repo,
1108 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1110 const struct got_error *err = NULL;
1111 struct dirent *de = NULL;
1112 size_t path_len = strlen(path);
1113 struct got_pathlist_entry *dle;
1114 int ignore;
1116 if (cb->diff_traverse) {
1117 err = cb->diff_traverse(cb_arg, path, dirfd);
1118 if (err)
1119 return err;
1122 dle = TAILQ_FIRST(dirlist);
1123 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1124 if (dle && *ie) {
1125 char *de_path;
1126 int cmp;
1127 de = dle->data;
1128 err = dirent_type_fixup(de, rootpath, path);
1129 if (err)
1130 break;
1131 if (asprintf(&de_path, "%s/%s", path,
1132 de->d_name) == -1) {
1133 err = got_error_from_errno("asprintf");
1134 break;
1136 cmp = got_path_cmp((*ie)->path, de_path,
1137 got_fileindex_entry_path_len(*ie),
1138 strlen(path) + 1 + strlen(de->d_name));
1139 free(de_path);
1140 if (cmp == 0) {
1141 err = cb->diff_old_new(cb_arg, *ie, de, path,
1142 dirfd);
1143 if (err)
1144 break;
1145 *ie = walk_fileindex(fileindex, *ie);
1146 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1147 path, rootpath, repo, 0, cb, cb_arg);
1148 } else if (cmp < 0 ) {
1149 err = cb->diff_old(cb_arg, *ie, path);
1150 if (err)
1151 break;
1152 *ie = walk_fileindex(fileindex, *ie);
1153 } else {
1154 err = cb->diff_new(&ignore, cb_arg, de, path,
1155 dirfd);
1156 if (err)
1157 break;
1158 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1159 path, rootpath, repo, ignore, cb, cb_arg);
1161 if (err)
1162 break;
1163 } else if (*ie) {
1164 err = cb->diff_old(cb_arg, *ie, path);
1165 if (err)
1166 break;
1167 *ie = walk_fileindex(fileindex, *ie);
1168 } else if (dle) {
1169 de = dle->data;
1170 err = dirent_type_fixup(de, rootpath, path);
1171 if (err)
1172 break;
1173 err = cb->diff_new(&ignore, cb_arg, de, path, dirfd);
1174 if (err)
1175 break;
1176 err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1177 rootpath, repo, ignore, cb, cb_arg);
1178 if (err)
1179 break;
1183 return err;
1186 const struct got_error *
1187 got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1188 const char *rootpath, const char *path, struct got_repository *repo,
1189 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1191 const struct got_error *err;
1192 struct got_fileindex_entry *ie;
1193 struct got_pathlist_head dirlist;
1194 int fd2;
1195 DIR *dir;
1197 TAILQ_INIT(&dirlist);
1200 * Duplicate the file descriptor so we can call closedir() below
1201 * without closing the file descriptor passed in by our caller.
1203 fd2 = dup(fd);
1204 if (fd2 == -1)
1205 return got_error_from_errno2("dup", path);
1206 if (lseek(fd2, 0, SEEK_SET) == -1) {
1207 err = got_error_from_errno2("lseek", path);
1208 close(fd2);
1209 return err;
1211 dir = fdopendir(fd2);
1212 if (dir == NULL) {
1213 err = got_error_from_errno2("fdopendir", path);
1214 close(fd2);
1215 return err;
1217 err = read_dirlist(&dirlist, dir, path);
1218 if (err) {
1219 closedir(dir);
1220 return err;
1223 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1224 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1225 ie = walk_fileindex(fileindex, ie);
1226 err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1227 rootpath, path, repo, cb, cb_arg);
1229 if (closedir(dir) == -1 && err == NULL)
1230 err = got_error_from_errno2("closedir", path);
1231 got_pathlist_free(&dirlist, GOT_PATHLIST_FREE_DATA);
1232 return err;
1235 struct got_object_id *
1236 got_fileindex_entry_get_staged_blob_id(struct got_object_id *id,
1237 struct got_fileindex_entry *ie)
1239 memset(id, 0, sizeof(*id));
1240 memcpy(id->sha1, ie->staged_blob_sha1, sizeof(ie->staged_blob_sha1));
1241 return id;
1244 struct got_object_id *
1245 got_fileindex_entry_get_blob_id(struct got_object_id *id,
1246 struct got_fileindex_entry *ie)
1248 memset(id, 0, sizeof(*id));
1249 memcpy(id->sha1, ie->blob_sha1, sizeof(ie->blob_sha1));
1250 return id;
1253 struct got_object_id *
1254 got_fileindex_entry_get_commit_id(struct got_object_id *id,
1255 struct got_fileindex_entry *ie)
1257 memset(id, 0, sizeof(*id));
1258 memcpy(id->sha1, ie->commit_sha1, sizeof(ie->commit_sha1));
1259 return id;
1262 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);