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 <stdint.h>
28 #include <string.h>
29 #include <limits.h>
30 #include <unistd.h>
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_path.h"
36 #include "got_lib_hash.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 INT32_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;
106 if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
107 if (update_timestamps) {
108 ie->ctime_sec = sb.st_ctim.tv_sec;
109 ie->ctime_nsec = sb.st_ctim.tv_nsec;
110 ie->mtime_sec = sb.st_mtim.tv_sec;
111 ie->mtime_nsec = sb.st_mtim.tv_nsec;
113 ie->uid = sb.st_uid;
114 ie->gid = sb.st_gid;
115 ie->size = (sb.st_size & 0xffffffff);
116 if (S_ISLNK(sb.st_mode)) {
117 got_fileindex_entry_filetype_set(ie,
118 GOT_FILEIDX_MODE_SYMLINK);
119 fileindex_entry_perms_set(ie, 0);
120 } else {
121 got_fileindex_entry_filetype_set(ie,
122 GOT_FILEIDX_MODE_REGULAR_FILE);
123 fileindex_entry_perms_set(ie,
124 sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
128 if (blob_sha1) {
129 memmove(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
130 ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
131 } else
132 ie->flags |= GOT_FILEIDX_F_NO_BLOB;
134 if (commit_sha1) {
135 memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
136 ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
137 } else
138 ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
140 return NULL;
143 void
144 got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
146 ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
149 void
150 got_fileindex_entry_mark_skipped(struct got_fileindex_entry *ie)
152 ie->flags |= GOT_FILEIDX_F_SKIPPED;
155 const struct got_error *
156 got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
157 const char *relpath)
159 size_t len;
161 *ie = calloc(1, sizeof(**ie));
162 if (*ie == NULL)
163 return got_error_from_errno("calloc");
165 (*ie)->path = strdup(relpath);
166 if ((*ie)->path == NULL) {
167 const struct got_error *err = got_error_from_errno("strdup");
168 free(*ie);
169 *ie = NULL;
170 return err;
173 len = strlen(relpath);
174 if (len > GOT_FILEIDX_F_PATH_LEN)
175 len = GOT_FILEIDX_F_PATH_LEN;
176 (*ie)->flags |= len;
178 return NULL;
181 void
182 got_fileindex_entry_free(struct got_fileindex_entry *ie)
184 free(ie->path);
185 free(ie);
188 size_t
189 got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
191 return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
194 uint32_t
195 got_fileindex_entry_stage_get(const struct got_fileindex_entry *ie)
197 return ((ie->flags & GOT_FILEIDX_F_STAGE) >> GOT_FILEIDX_F_STAGE_SHIFT);
200 void
201 got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage)
203 ie->flags &= ~GOT_FILEIDX_F_STAGE;
204 ie->flags |= ((stage << GOT_FILEIDX_F_STAGE_SHIFT) &
205 GOT_FILEIDX_F_STAGE);
208 int
209 got_fileindex_entry_filetype_get(struct got_fileindex_entry *ie)
211 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
214 void
215 got_fileindex_entry_filetype_set(struct got_fileindex_entry *ie, int type)
217 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_ONDISK;
218 ie->mode |= (type & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
221 void
222 got_fileindex_entry_staged_filetype_set(struct got_fileindex_entry *ie,
223 int type)
225 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_STAGED;
226 ie->mode |= ((type << GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT) &
227 GOT_FILEIDX_MODE_FILE_TYPE_STAGED);
230 int
231 got_fileindex_entry_staged_filetype_get(struct got_fileindex_entry *ie)
233 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_STAGED) >>
234 GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT;
237 int
238 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
240 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
243 int
244 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
246 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
249 int
250 got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
252 return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
255 int
256 got_fileindex_entry_was_skipped(struct got_fileindex_entry *ie)
258 return (ie->flags & GOT_FILEIDX_F_SKIPPED) != 0;
261 static const struct got_error *
262 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
264 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
265 return got_error(GOT_ERR_NO_SPACE);
267 if (RB_INSERT(got_fileindex_tree, &fileindex->entries, ie) != NULL)
268 return got_error_path(ie->path, GOT_ERR_FILEIDX_DUP_ENTRY);
270 fileindex->nentries++;
271 return NULL;
274 const struct got_error *
275 got_fileindex_entry_add(struct got_fileindex *fileindex,
276 struct got_fileindex_entry *ie)
278 /* Flag this entry until it gets written out to disk. */
279 ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
281 return add_entry(fileindex, ie);
284 void
285 got_fileindex_entry_remove(struct got_fileindex *fileindex,
286 struct got_fileindex_entry *ie)
288 /*
289 * Removing an entry from the RB tree immediately breaks
290 * in-progress iterations over file index entries.
291 * So flag this entry for removal and remove it once the index
292 * is written out to disk. Meanwhile, pretend this entry no longer
293 * exists if we get queried for it again before then.
294 */
295 ie->flags |= GOT_FILEIDX_F_REMOVE_ON_FLUSH;
296 fileindex->nentries--;
299 struct got_fileindex_entry *
300 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
301 size_t path_len)
303 struct got_fileindex_entry *ie;
304 struct got_fileindex_entry key;
305 memset(&key, 0, sizeof(key));
306 key.path = (char *)path;
307 key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
308 ie = RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
309 if (ie && (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH))
310 return NULL;
311 return ie;
314 const struct got_error *
315 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
316 got_fileindex_cb cb, void *cb_arg)
318 const struct got_error *err;
319 struct got_fileindex_entry *ie, *tmp;
321 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
322 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
323 continue;
324 err = (*cb)(cb_arg, ie);
325 if (err)
326 return err;
328 return NULL;
331 struct got_fileindex *
332 got_fileindex_alloc(void)
334 struct got_fileindex *fileindex;
336 fileindex = calloc(1, sizeof(*fileindex));
337 if (fileindex == NULL)
338 return NULL;
340 RB_INIT(&fileindex->entries);
341 return fileindex;
344 void
345 got_fileindex_free(struct got_fileindex *fileindex)
347 struct got_fileindex_entry *ie;
349 while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
350 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
351 got_fileindex_entry_free(ie);
353 free(fileindex);
356 static const struct got_error *
357 write_fileindex_val64(struct got_hash *ctx, uint64_t val, FILE *outfile)
359 size_t n;
361 val = htobe64(val);
362 got_hash_update(ctx, &val, sizeof(val));
363 n = fwrite(&val, 1, sizeof(val), outfile);
364 if (n != sizeof(val))
365 return got_ferror(outfile, GOT_ERR_IO);
366 return NULL;
369 static const struct got_error *
370 write_fileindex_val32(struct got_hash *ctx, uint32_t val, FILE *outfile)
372 size_t n;
374 val = htobe32(val);
375 got_hash_update(ctx, &val, sizeof(val));
376 n = fwrite(&val, 1, sizeof(val), outfile);
377 if (n != sizeof(val))
378 return got_ferror(outfile, GOT_ERR_IO);
379 return NULL;
382 static const struct got_error *
383 write_fileindex_val16(struct got_hash *ctx, uint16_t val, FILE *outfile)
385 size_t n;
387 val = htobe16(val);
388 got_hash_update(ctx, &val, sizeof(val));
389 n = fwrite(&val, 1, sizeof(val), outfile);
390 if (n != sizeof(val))
391 return got_ferror(outfile, GOT_ERR_IO);
392 return NULL;
395 static const struct got_error *
396 write_fileindex_path(struct got_hash *ctx, const char *path, FILE *outfile)
398 size_t n, len, pad = 0;
399 static const uint8_t zero[8] = { 0 };
401 len = strlen(path);
402 while ((len + pad) % 8 != 0)
403 pad++;
404 if (pad == 0)
405 pad = 8; /* NUL-terminate */
407 got_hash_update(ctx, path, len);
408 n = fwrite(path, 1, len, outfile);
409 if (n != len)
410 return got_ferror(outfile, GOT_ERR_IO);
411 got_hash_update(ctx, zero, pad);
412 n = fwrite(zero, 1, pad, outfile);
413 if (n != pad)
414 return got_ferror(outfile, GOT_ERR_IO);
415 return NULL;
418 static const struct got_error *
419 write_fileindex_entry(struct got_hash *ctx, struct got_fileindex_entry *ie,
420 FILE *outfile)
422 const struct got_error *err;
423 size_t n;
424 uint32_t stage;
426 err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
427 if (err)
428 return err;
429 err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
430 if (err)
431 return err;
432 err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
433 if (err)
434 return err;
435 err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
436 if (err)
437 return err;
439 err = write_fileindex_val32(ctx, ie->uid, outfile);
440 if (err)
441 return err;
442 err = write_fileindex_val32(ctx, ie->gid, outfile);
443 if (err)
444 return err;
445 err = write_fileindex_val32(ctx, ie->size, outfile);
446 if (err)
447 return err;
449 err = write_fileindex_val16(ctx, ie->mode, outfile);
450 if (err)
451 return err;
453 got_hash_update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
454 n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
455 if (n != SHA1_DIGEST_LENGTH)
456 return got_ferror(outfile, GOT_ERR_IO);
458 got_hash_update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
459 n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
460 if (n != SHA1_DIGEST_LENGTH)
461 return got_ferror(outfile, GOT_ERR_IO);
463 err = write_fileindex_val32(ctx, ie->flags, outfile);
464 if (err)
465 return err;
467 err = write_fileindex_path(ctx, ie->path, outfile);
468 if (err)
469 return err;
471 stage = got_fileindex_entry_stage_get(ie);
472 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
473 stage == GOT_FILEIDX_STAGE_ADD) {
474 got_hash_update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
475 n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
476 outfile);
477 if (n != SHA1_DIGEST_LENGTH)
478 return got_ferror(outfile, GOT_ERR_IO);
481 return NULL;
484 const struct got_error *
485 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
487 const struct got_error *err = NULL;
488 struct got_fileindex_hdr hdr;
489 struct got_hash ctx;
490 uint8_t hash[GOT_HASH_DIGEST_MAXLEN];
491 size_t n;
492 struct got_fileindex_entry *ie, *tmp;
494 got_hash_init(&ctx, GOT_HASH_SHA1);
496 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
497 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
498 hdr.nentries = htobe32(fileindex->nentries);
500 got_hash_update(&ctx, &hdr.signature, sizeof(hdr.signature));
501 got_hash_update(&ctx, &hdr.version, sizeof(hdr.version));
502 got_hash_update(&ctx, &hdr.nentries, sizeof(hdr.nentries));
503 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
504 if (n != sizeof(hdr.signature))
505 return got_ferror(outfile, GOT_ERR_IO);
506 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
507 if (n != sizeof(hdr.version))
508 return got_ferror(outfile, GOT_ERR_IO);
509 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
510 if (n != sizeof(hdr.nentries))
511 return got_ferror(outfile, GOT_ERR_IO);
513 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
514 ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
515 ie->flags &= ~GOT_FILEIDX_F_SKIPPED;
516 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH) {
517 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
518 got_fileindex_entry_free(ie);
519 continue;
521 err = write_fileindex_entry(&ctx, ie, outfile);
522 if (err)
523 return err;
526 got_hash_final(&ctx, hash);
527 n = fwrite(hash, 1, SHA1_DIGEST_LENGTH, outfile);
528 if (n != SHA1_DIGEST_LENGTH)
529 return got_ferror(outfile, GOT_ERR_IO);
531 if (fflush(outfile) != 0)
532 return got_error_from_errno("fflush");
534 return NULL;
537 static const struct got_error *
538 read_fileindex_val64(uint64_t *val, struct got_hash *ctx, FILE *infile)
540 size_t n;
542 n = fread(val, 1, sizeof(*val), infile);
543 if (n != sizeof(*val))
544 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
545 got_hash_update(ctx, val, sizeof(*val));
546 *val = be64toh(*val);
547 return NULL;
550 static const struct got_error *
551 read_fileindex_val32(uint32_t *val, struct got_hash *ctx, FILE *infile)
553 size_t n;
555 n = fread(val, 1, sizeof(*val), infile);
556 if (n != sizeof(*val))
557 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
558 got_hash_update(ctx, val, sizeof(*val));
559 *val = be32toh(*val);
560 return NULL;
563 static const struct got_error *
564 read_fileindex_val16(uint16_t *val, struct got_hash *ctx, FILE *infile)
566 size_t n;
568 n = fread(val, 1, sizeof(*val), infile);
569 if (n != sizeof(*val))
570 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
571 got_hash_update(ctx, val, sizeof(*val));
572 *val = be16toh(*val);
573 return NULL;
576 static const struct got_error *
577 read_fileindex_path(char **path, struct got_hash *ctx, FILE *infile)
579 const size_t chunk_size = 8;
580 char p[PATH_MAX];
581 size_t n, len = 0;
583 do {
584 if (len + chunk_size > sizeof(p))
585 return got_error(GOT_ERR_FILEIDX_BAD);
587 n = fread(&p[len], 1, chunk_size, infile);
588 if (n != chunk_size)
589 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
591 got_hash_update(ctx, &p[len], chunk_size);
592 len += chunk_size;
593 } while (memchr(&p[len - chunk_size], '\0', chunk_size) == NULL);
595 *path = strdup(p);
596 if (*path == NULL)
597 return got_error_from_errno("strdup");
598 return NULL;
601 static const struct got_error *
602 read_fileindex_entry(struct got_fileindex_entry **iep, struct got_hash *ctx,
603 FILE *infile, uint32_t version)
605 const struct got_error *err;
606 struct got_fileindex_entry *ie;
607 size_t n;
609 *iep = NULL;
611 ie = calloc(1, sizeof(*ie));
612 if (ie == NULL)
613 return got_error_from_errno("calloc");
615 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
616 if (err)
617 goto done;
618 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
619 if (err)
620 goto done;
621 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
622 if (err)
623 goto done;
624 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
625 if (err)
626 goto done;
628 err = read_fileindex_val32(&ie->uid, ctx, infile);
629 if (err)
630 goto done;
631 err = read_fileindex_val32(&ie->gid, ctx, infile);
632 if (err)
633 goto done;
634 err = read_fileindex_val32(&ie->size, ctx, infile);
635 if (err)
636 goto done;
638 err = read_fileindex_val16(&ie->mode, ctx, infile);
639 if (err)
640 goto done;
642 n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
643 if (n != SHA1_DIGEST_LENGTH) {
644 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
645 goto done;
647 got_hash_update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
649 n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
650 if (n != SHA1_DIGEST_LENGTH) {
651 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
652 goto done;
654 got_hash_update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
656 err = read_fileindex_val32(&ie->flags, ctx, infile);
657 if (err)
658 goto done;
660 err = read_fileindex_path(&ie->path, ctx, infile);
661 if (err)
662 goto done;
664 if (version >= 2) {
665 uint32_t stage = got_fileindex_entry_stage_get(ie);
666 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
667 stage == GOT_FILEIDX_STAGE_ADD) {
668 n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
669 infile);
670 if (n != SHA1_DIGEST_LENGTH) {
671 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
672 goto done;
674 got_hash_update(ctx, ie->staged_blob_sha1,
675 SHA1_DIGEST_LENGTH);
677 } else {
678 /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
679 ie->flags &= ~GOT_FILEIDX_F_STAGE;
682 done:
683 if (err)
684 got_fileindex_entry_free(ie);
685 else
686 *iep = ie;
687 return err;
690 const struct got_error *
691 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
693 const struct got_error *err = NULL;
694 struct got_fileindex_hdr hdr;
695 struct got_hash ctx;
696 struct got_fileindex_entry *ie;
697 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
698 uint8_t sha1[SHA1_DIGEST_LENGTH];
699 size_t n;
700 int i;
702 got_hash_init(&ctx, GOT_HASH_SHA1);
704 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
705 if (n != sizeof(hdr.signature)) {
706 if (n == 0) /* EOF */
707 return NULL;
708 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
710 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
711 if (n != sizeof(hdr.version)) {
712 if (n == 0) /* EOF */
713 return NULL;
714 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
716 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
717 if (n != sizeof(hdr.nentries)) {
718 if (n == 0) /* EOF */
719 return NULL;
720 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
723 got_hash_update(&ctx, &hdr.signature, sizeof(hdr.signature));
724 got_hash_update(&ctx, &hdr.version, sizeof(hdr.version));
725 got_hash_update(&ctx, &hdr.nentries, sizeof(hdr.nentries));
727 hdr.signature = be32toh(hdr.signature);
728 hdr.version = be32toh(hdr.version);
729 hdr.nentries = be32toh(hdr.nentries);
731 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
732 return got_error(GOT_ERR_FILEIDX_SIG);
733 if (hdr.version > GOT_FILE_INDEX_VERSION)
734 return got_error(GOT_ERR_FILEIDX_VER);
736 for (i = 0; i < hdr.nentries; i++) {
737 err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
738 if (err)
739 return err;
740 err = add_entry(fileindex, ie);
741 if (err) {
742 got_fileindex_entry_free(ie);
743 return err;
747 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
748 if (n != sizeof(sha1_expected))
749 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
750 got_hash_final(&ctx, sha1);
751 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
752 return got_error(GOT_ERR_FILEIDX_CSUM);
754 return NULL;
757 static struct got_fileindex_entry *
758 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
760 struct got_fileindex_entry *next;
762 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
764 /* Skip entries which were added or removed by diff callbacks. */
765 while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
766 GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
767 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
769 return next;
772 static const struct got_error *
773 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
774 struct got_tree_object *tree, const char *, const char *,
775 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
777 static const struct got_error *
778 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
779 struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
780 const char *path, const char *entry_name, struct got_repository *repo,
781 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
783 const struct got_error *err = NULL;
784 struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
786 if (!got_object_tree_entry_is_submodule(te) &&
787 S_ISDIR(got_tree_entry_get_mode(te))) {
788 char *subpath;
789 struct got_tree_object *subtree;
791 if (asprintf(&subpath, "%s%s%s", path,
792 path[0] == '\0' ? "" : "/",
793 got_tree_entry_get_name(te)) == -1)
794 return got_error_from_errno("asprintf");
796 err = got_object_open_as_tree(&subtree, repo,
797 got_tree_entry_get_id(te));
798 if (err) {
799 free(subpath);
800 return err;
803 err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
804 entry_name, repo, cb, cb_arg);
805 free(subpath);
806 got_object_tree_close(subtree);
807 if (err)
808 return err;
811 (*tidx)++;
812 *next = got_object_tree_get_entry(tree, *tidx);
813 return NULL;
816 static const struct got_error *
817 diff_fileindex_tree(struct got_fileindex *fileindex,
818 struct got_fileindex_entry **ie, struct got_tree_object *tree,
819 const char *path, const char *entry_name, struct got_repository *repo,
820 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
822 const struct got_error *err = NULL;
823 struct got_tree_entry *te = NULL;
824 size_t path_len = strlen(path);
825 struct got_fileindex_entry *next;
826 int tidx = 0;
828 te = got_object_tree_get_entry(tree, tidx);
829 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
830 if (te && *ie) {
831 char *te_path;
832 const char *te_name = got_tree_entry_get_name(te);
833 int cmp;
834 if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
835 err = got_error_from_errno("asprintf");
836 break;
838 cmp = got_path_cmp((*ie)->path, te_path,
839 got_fileindex_entry_path_len(*ie), strlen(te_path));
840 free(te_path);
841 if (cmp == 0) {
842 if (got_path_is_child((*ie)->path, path,
843 path_len) &&
844 !got_object_tree_entry_is_submodule(te) &&
845 (entry_name == NULL ||
846 strcmp(te_name, entry_name) == 0)) {
847 err = cb->diff_old_new(cb_arg, *ie, te,
848 path);
849 if (err || entry_name)
850 break;
852 *ie = walk_fileindex(fileindex, *ie);
853 err = walk_tree(&te, fileindex, ie, tree, &tidx,
854 path, entry_name, repo, cb, cb_arg);
855 } else if (cmp < 0) {
856 next = walk_fileindex(fileindex, *ie);
857 if (got_path_is_child((*ie)->path, path,
858 path_len) && entry_name == NULL) {
859 err = cb->diff_old(cb_arg, *ie, path);
860 if (err || entry_name)
861 break;
863 *ie = next;
864 } else {
865 if ((entry_name == NULL ||
866 strcmp(te_name, entry_name) == 0)) {
867 err = cb->diff_new(cb_arg, te, path);
868 if (err || entry_name)
869 break;
871 err = walk_tree(&te, fileindex, ie, tree, &tidx,
872 path, entry_name, repo, cb, cb_arg);
874 if (err)
875 break;
876 } else if (*ie) {
877 next = walk_fileindex(fileindex, *ie);
878 if (got_path_is_child((*ie)->path, path, path_len) &&
879 (entry_name == NULL ||
880 (te && strcmp(got_tree_entry_get_name(te),
881 entry_name) == 0))) {
882 err = cb->diff_old(cb_arg, *ie, path);
883 if (err || entry_name)
884 break;
886 *ie = next;
887 } else if (te) {
888 if (!got_object_tree_entry_is_submodule(te) &&
889 (entry_name == NULL ||
890 strcmp(got_tree_entry_get_name(te), entry_name)
891 == 0)) {
892 err = cb->diff_new(cb_arg, te, path);
893 if (err || entry_name)
894 break;
896 err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
897 entry_name, repo, cb, cb_arg);
898 if (err)
899 break;
903 return err;
906 const struct got_error *
907 got_fileindex_diff_tree(struct got_fileindex *fileindex,
908 struct got_tree_object *tree, const char *path, const char *entry_name,
909 struct got_repository *repo,
910 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
912 struct got_fileindex_entry *ie;
913 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
914 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
915 ie = walk_fileindex(fileindex, ie);
916 return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
917 cb, cb_arg);
920 static const struct got_error *
921 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
922 struct got_pathlist_head *, int, const char *, const char *,
923 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
925 static const struct got_error *
926 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
928 const struct got_error *err = NULL;
929 struct got_pathlist_entry *new = NULL;
930 struct dirent *dep = NULL;
931 struct dirent *de = NULL;
933 for (;;) {
934 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
935 if (de == NULL) {
936 err = got_error_from_errno("malloc");
937 break;
940 if (readdir_r(dir, de, &dep) != 0) {
941 err = got_error_from_errno("readdir_r");
942 free(de);
943 break;
945 if (dep == NULL) {
946 free(de);
947 break;
950 if (strcmp(de->d_name, ".") == 0 ||
951 strcmp(de->d_name, "..") == 0 ||
952 (path[0] == '\0' &&
953 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0) ||
954 (path[0] == '\0' &&
955 strcmp(de->d_name, GOT_WORKTREE_CVG_DIR) == 0)) {
956 free(de);
957 continue;
960 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
961 if (err) {
962 free(de);
963 break;
965 if (new == NULL) {
966 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
967 free(de);
968 break;
972 return err;
975 static int
976 have_tracked_file_in_dir(struct got_fileindex *fileindex, const char *path)
978 struct got_fileindex_entry *ie;
979 size_t path_len = strlen(path);
980 int cmp;
982 ie = RB_ROOT(&fileindex->entries);
983 while (ie) {
984 if (got_path_is_child(ie->path, path, path_len))
985 return 1;
986 cmp = got_path_cmp(path, ie->path, path_len,
987 got_fileindex_entry_path_len(ie));
988 if (cmp < 0)
989 ie = RB_LEFT(ie, entry);
990 else if (cmp > 0)
991 ie = RB_RIGHT(ie, entry);
992 else
993 break;
996 return 0;
999 static const struct got_error *
1000 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
1001 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
1002 const char *path, const char *rootpath, struct got_repository *repo,
1003 int ignore, struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1005 const struct got_error *err = NULL;
1006 struct dirent *de = dle->data;
1007 DIR *subdir = NULL;
1008 int subdirfd = -1;
1010 *next = NULL;
1012 /* Must traverse ignored directories if they contain tracked files. */
1013 if (de->d_type == DT_DIR && ignore &&
1014 have_tracked_file_in_dir(fileindex, path))
1015 ignore = 0;
1017 if (de->d_type == DT_DIR && !ignore) {
1018 char *subpath;
1019 char *subdirpath;
1020 struct got_pathlist_head subdirlist;
1022 TAILQ_INIT(&subdirlist);
1024 if (asprintf(&subpath, "%s%s%s", path,
1025 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1026 return got_error_from_errno("asprintf");
1028 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
1029 free(subpath);
1030 return got_error_from_errno("asprintf");
1033 subdirfd = openat(fd, de->d_name,
1034 O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
1035 if (subdirfd == -1) {
1036 if (errno == EACCES) {
1037 *next = TAILQ_NEXT(dle, entry);
1038 return NULL;
1040 err = got_error_from_errno2("openat", subdirpath);
1041 free(subpath);
1042 free(subdirpath);
1043 return err;
1046 subdir = fdopendir(subdirfd);
1047 if (subdir == NULL) {
1048 err = got_error_from_errno2("fdopendir", path);
1049 close(subdirfd);
1050 free(subpath);
1051 free(subdirpath);
1052 return err;
1054 subdirfd = -1;
1055 err = read_dirlist(&subdirlist, subdir, subdirpath);
1056 if (err) {
1057 free(subpath);
1058 free(subdirpath);
1059 closedir(subdir);
1060 return err;
1062 err = diff_fileindex_dir(fileindex, ie, &subdirlist,
1063 dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
1064 if (subdir && closedir(subdir) == -1 && err == NULL)
1065 err = got_error_from_errno2("closedir", subdirpath);
1066 free(subpath);
1067 free(subdirpath);
1068 got_pathlist_free(&subdirlist, GOT_PATHLIST_FREE_DATA);
1069 if (err)
1070 return err;
1073 *next = TAILQ_NEXT(dle, entry);
1074 return NULL;
1077 static const struct got_error *
1078 dirent_type_fixup(struct dirent *de, const char *rootpath, const char *path)
1080 const struct got_error *err;
1081 char *dir_path;
1082 int type;
1084 if (de->d_type != DT_UNKNOWN)
1085 return NULL;
1087 /* DT_UNKNOWN occurs on NFS mounts without "readdir plus" RPC. */
1088 if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
1089 return got_error_from_errno("asprintf");
1090 err = got_path_dirent_type(&type, dir_path, de);
1091 free(dir_path);
1092 if (err)
1093 return err;
1095 de->d_type = type;
1096 return NULL;
1099 static const struct got_error *
1100 diff_fileindex_dir(struct got_fileindex *fileindex,
1101 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1102 int dirfd, const char *rootpath, const char *path,
1103 struct got_repository *repo,
1104 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1106 const struct got_error *err = NULL;
1107 struct dirent *de = NULL;
1108 size_t path_len = strlen(path);
1109 struct got_pathlist_entry *dle;
1110 int ignore;
1112 if (cb->diff_traverse) {
1113 err = cb->diff_traverse(cb_arg, path, dirfd);
1114 if (err)
1115 return err;
1118 dle = TAILQ_FIRST(dirlist);
1119 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1120 if (dle && *ie) {
1121 char *de_path;
1122 int cmp;
1123 de = dle->data;
1124 err = dirent_type_fixup(de, rootpath, path);
1125 if (err)
1126 break;
1127 if (asprintf(&de_path, "%s/%s", path,
1128 de->d_name) == -1) {
1129 err = got_error_from_errno("asprintf");
1130 break;
1132 cmp = got_path_cmp((*ie)->path, de_path,
1133 got_fileindex_entry_path_len(*ie),
1134 strlen(path) + 1 + strlen(de->d_name));
1135 free(de_path);
1136 if (cmp == 0) {
1137 err = cb->diff_old_new(cb_arg, *ie, de, path,
1138 dirfd);
1139 if (err)
1140 break;
1141 *ie = walk_fileindex(fileindex, *ie);
1142 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1143 path, rootpath, repo, 0, cb, cb_arg);
1144 } else if (cmp < 0 ) {
1145 err = cb->diff_old(cb_arg, *ie, path);
1146 if (err)
1147 break;
1148 *ie = walk_fileindex(fileindex, *ie);
1149 } else {
1150 err = cb->diff_new(&ignore, cb_arg, de, path,
1151 dirfd);
1152 if (err)
1153 break;
1154 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1155 path, rootpath, repo, ignore, cb, cb_arg);
1157 if (err)
1158 break;
1159 } else if (*ie) {
1160 err = cb->diff_old(cb_arg, *ie, path);
1161 if (err)
1162 break;
1163 *ie = walk_fileindex(fileindex, *ie);
1164 } else if (dle) {
1165 de = dle->data;
1166 err = dirent_type_fixup(de, rootpath, path);
1167 if (err)
1168 break;
1169 err = cb->diff_new(&ignore, cb_arg, de, path, dirfd);
1170 if (err)
1171 break;
1172 err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1173 rootpath, repo, ignore, cb, cb_arg);
1174 if (err)
1175 break;
1179 return err;
1182 const struct got_error *
1183 got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1184 const char *rootpath, const char *path, struct got_repository *repo,
1185 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1187 const struct got_error *err;
1188 struct got_fileindex_entry *ie;
1189 struct got_pathlist_head dirlist;
1190 int fd2;
1191 DIR *dir;
1193 TAILQ_INIT(&dirlist);
1196 * Duplicate the file descriptor so we can call closedir() below
1197 * without closing the file descriptor passed in by our caller.
1199 fd2 = dup(fd);
1200 if (fd2 == -1)
1201 return got_error_from_errno2("dup", path);
1202 if (lseek(fd2, 0, SEEK_SET) == -1) {
1203 err = got_error_from_errno2("lseek", path);
1204 close(fd2);
1205 return err;
1207 dir = fdopendir(fd2);
1208 if (dir == NULL) {
1209 err = got_error_from_errno2("fdopendir", path);
1210 close(fd2);
1211 return err;
1213 err = read_dirlist(&dirlist, dir, path);
1214 if (err) {
1215 closedir(dir);
1216 return err;
1219 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1220 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1221 ie = walk_fileindex(fileindex, ie);
1222 err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1223 rootpath, path, repo, cb, cb_arg);
1225 if (closedir(dir) == -1 && err == NULL)
1226 err = got_error_from_errno2("closedir", path);
1227 got_pathlist_free(&dirlist, GOT_PATHLIST_FREE_DATA);
1228 return err;
1231 struct got_object_id *
1232 got_fileindex_entry_get_staged_blob_id(struct got_object_id *id,
1233 struct got_fileindex_entry *ie)
1235 memset(id, 0, sizeof(*id));
1236 memcpy(id->sha1, ie->staged_blob_sha1, sizeof(ie->staged_blob_sha1));
1237 return id;
1240 struct got_object_id *
1241 got_fileindex_entry_get_blob_id(struct got_object_id *id,
1242 struct got_fileindex_entry *ie)
1244 memset(id, 0, sizeof(*id));
1245 memcpy(id->sha1, ie->blob_sha1, sizeof(ie->blob_sha1));
1246 return id;
1249 struct got_object_id *
1250 got_fileindex_entry_get_commit_id(struct got_object_id *id,
1251 struct got_fileindex_entry *ie)
1253 memset(id, 0, sizeof(*id));
1254 memcpy(id->sha1, ie->commit_sha1, sizeof(ie->commit_sha1));
1255 return id;
1258 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);