Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/tree.h>
19 #include <sys/stat.h>
21 #include <errno.h>
22 #include <dirent.h>
23 #include <fcntl.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <string.h>
28 #include <sha1.h>
29 #include <sha2.h>
30 #include <endian.h>
31 #include <limits.h>
32 #include <unistd.h>
33 #include <uuid.h>
35 #include "got_error.h"
36 #include "got_object.h"
37 #include "got_path.h"
39 #include "got_lib_hash.h"
40 #include "got_lib_fileindex.h"
41 #include "got_lib_worktree.h"
43 /* got_fileindex_entry flags */
44 #define GOT_FILEIDX_F_PATH_LEN 0x00000fff
45 #define GOT_FILEIDX_F_STAGE 0x0000f000
46 #define GOT_FILEIDX_F_STAGE_SHIFT 12
47 #define GOT_FILEIDX_F_NOT_FLUSHED 0x00010000
48 #define GOT_FILEIDX_F_NO_BLOB 0x00020000
49 #define GOT_FILEIDX_F_NO_COMMIT 0x00040000
50 #define GOT_FILEIDX_F_NO_FILE_ON_DISK 0x00080000
51 #define GOT_FILEIDX_F_REMOVE_ON_FLUSH 0x00100000
52 #define GOT_FILEIDX_F_SKIPPED 0x00200000
54 struct got_fileindex {
55 struct got_fileindex_tree entries;
56 int nentries; /* Does not include entries marked for removal. */
57 #define GOT_FILEIDX_MAX_ENTRIES INT32_MAX
58 };
60 mode_t
61 got_fileindex_entry_perms_get(struct got_fileindex_entry *ie)
62 {
63 return ((ie->mode & GOT_FILEIDX_MODE_PERMS) >>
64 GOT_FILEIDX_MODE_PERMS_SHIFT);
65 }
67 static void
68 fileindex_entry_perms_set(struct got_fileindex_entry *ie, mode_t mode)
69 {
70 ie->mode &= ~GOT_FILEIDX_MODE_PERMS;
71 ie->mode |= ((mode << GOT_FILEIDX_MODE_PERMS_SHIFT) &
72 GOT_FILEIDX_MODE_PERMS);
73 }
75 mode_t
76 got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
77 {
78 mode_t perms = got_fileindex_entry_perms_get(ie);
79 int type = got_fileindex_entry_filetype_get(ie);
80 uint32_t ftype;
82 if (type == GOT_FILEIDX_MODE_REGULAR_FILE ||
83 type == GOT_FILEIDX_MODE_BAD_SYMLINK)
84 ftype = S_IFREG;
85 else
86 ftype = S_IFLNK;
88 return (ftype | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
89 }
91 const struct got_error *
92 got_fileindex_entry_update(struct got_fileindex_entry *ie,
93 int wt_fd, const char *ondisk_path, uint8_t *blob_sha1,
94 uint8_t *commit_sha1, int update_timestamps)
95 {
96 struct stat sb;
98 if (fstatat(wt_fd, ondisk_path, &sb, AT_SYMLINK_NOFOLLOW) != 0) {
99 if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
100 errno == ENOENT))
101 return got_error_from_errno2("fstatat", ondisk_path);
102 sb.st_mode = GOT_DEFAULT_FILE_MODE;
103 } else {
104 if (sb.st_mode & S_IFDIR)
105 return got_error_set_errno(EISDIR, ondisk_path);
106 ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
109 if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
110 if (update_timestamps) {
111 ie->ctime_sec = sb.st_ctim.tv_sec;
112 ie->ctime_nsec = sb.st_ctim.tv_nsec;
113 ie->mtime_sec = sb.st_mtim.tv_sec;
114 ie->mtime_nsec = sb.st_mtim.tv_nsec;
116 ie->uid = sb.st_uid;
117 ie->gid = sb.st_gid;
118 ie->size = (sb.st_size & 0xffffffff);
119 if (S_ISLNK(sb.st_mode)) {
120 got_fileindex_entry_filetype_set(ie,
121 GOT_FILEIDX_MODE_SYMLINK);
122 fileindex_entry_perms_set(ie, 0);
123 } else {
124 got_fileindex_entry_filetype_set(ie,
125 GOT_FILEIDX_MODE_REGULAR_FILE);
126 fileindex_entry_perms_set(ie,
127 sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
131 if (blob_sha1) {
132 memmove(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
133 ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
134 } else
135 ie->flags |= GOT_FILEIDX_F_NO_BLOB;
137 if (commit_sha1) {
138 memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
139 ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
140 } else
141 ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
143 return NULL;
146 void
147 got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
149 ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
152 void
153 got_fileindex_entry_mark_skipped(struct got_fileindex_entry *ie)
155 ie->flags |= GOT_FILEIDX_F_SKIPPED;
158 const struct got_error *
159 got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
160 const char *relpath)
162 size_t len;
164 *ie = calloc(1, sizeof(**ie));
165 if (*ie == NULL)
166 return got_error_from_errno("calloc");
168 (*ie)->path = strdup(relpath);
169 if ((*ie)->path == NULL) {
170 const struct got_error *err = got_error_from_errno("strdup");
171 free(*ie);
172 *ie = NULL;
173 return err;
176 len = strlen(relpath);
177 if (len > GOT_FILEIDX_F_PATH_LEN)
178 len = GOT_FILEIDX_F_PATH_LEN;
179 (*ie)->flags |= len;
181 return NULL;
184 void
185 got_fileindex_entry_free(struct got_fileindex_entry *ie)
187 free(ie->path);
188 free(ie);
191 size_t
192 got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
194 return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
197 uint32_t
198 got_fileindex_entry_stage_get(const struct got_fileindex_entry *ie)
200 return ((ie->flags & GOT_FILEIDX_F_STAGE) >> GOT_FILEIDX_F_STAGE_SHIFT);
203 void
204 got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage)
206 ie->flags &= ~GOT_FILEIDX_F_STAGE;
207 ie->flags |= ((stage << GOT_FILEIDX_F_STAGE_SHIFT) &
208 GOT_FILEIDX_F_STAGE);
211 int
212 got_fileindex_entry_filetype_get(struct got_fileindex_entry *ie)
214 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
217 void
218 got_fileindex_entry_filetype_set(struct got_fileindex_entry *ie, int type)
220 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_ONDISK;
221 ie->mode |= (type & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
224 void
225 got_fileindex_entry_staged_filetype_set(struct got_fileindex_entry *ie,
226 int type)
228 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_STAGED;
229 ie->mode |= ((type << GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT) &
230 GOT_FILEIDX_MODE_FILE_TYPE_STAGED);
233 int
234 got_fileindex_entry_staged_filetype_get(struct got_fileindex_entry *ie)
236 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_STAGED) >>
237 GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT;
240 int
241 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
243 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
246 int
247 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
249 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
252 int
253 got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
255 return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
258 int
259 got_fileindex_entry_was_skipped(struct got_fileindex_entry *ie)
261 return (ie->flags & GOT_FILEIDX_F_SKIPPED) != 0;
264 static const struct got_error *
265 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
267 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
268 return got_error(GOT_ERR_NO_SPACE);
270 if (RB_INSERT(got_fileindex_tree, &fileindex->entries, ie) != NULL)
271 return got_error_path(ie->path, GOT_ERR_FILEIDX_DUP_ENTRY);
273 fileindex->nentries++;
274 return NULL;
277 const struct got_error *
278 got_fileindex_entry_add(struct got_fileindex *fileindex,
279 struct got_fileindex_entry *ie)
281 /* Flag this entry until it gets written out to disk. */
282 ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
284 return add_entry(fileindex, ie);
287 void
288 got_fileindex_entry_remove(struct got_fileindex *fileindex,
289 struct got_fileindex_entry *ie)
291 /*
292 * Removing an entry from the RB tree immediately breaks
293 * in-progress iterations over file index entries.
294 * So flag this entry for removal and remove it once the index
295 * is written out to disk. Meanwhile, pretend this entry no longer
296 * exists if we get queried for it again before then.
297 */
298 ie->flags |= GOT_FILEIDX_F_REMOVE_ON_FLUSH;
299 fileindex->nentries--;
302 struct got_fileindex_entry *
303 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
304 size_t path_len)
306 struct got_fileindex_entry *ie;
307 struct got_fileindex_entry key;
308 memset(&key, 0, sizeof(key));
309 key.path = (char *)path;
310 key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
311 ie = RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
312 if (ie && (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH))
313 return NULL;
314 return ie;
317 const struct got_error *
318 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
319 got_fileindex_cb cb, void *cb_arg)
321 const struct got_error *err;
322 struct got_fileindex_entry *ie, *tmp;
324 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
325 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
326 continue;
327 err = (*cb)(cb_arg, ie);
328 if (err)
329 return err;
331 return NULL;
334 struct got_fileindex *
335 got_fileindex_alloc(void)
337 struct got_fileindex *fileindex;
339 fileindex = calloc(1, sizeof(*fileindex));
340 if (fileindex == NULL)
341 return NULL;
343 RB_INIT(&fileindex->entries);
344 return fileindex;
347 void
348 got_fileindex_free(struct got_fileindex *fileindex)
350 struct got_fileindex_entry *ie;
352 while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
353 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
354 got_fileindex_entry_free(ie);
356 free(fileindex);
359 static const struct got_error *
360 write_fileindex_val64(struct got_hash *ctx, uint64_t val, FILE *outfile)
362 size_t n;
364 val = htobe64(val);
365 got_hash_update(ctx, &val, sizeof(val));
366 n = fwrite(&val, 1, sizeof(val), outfile);
367 if (n != sizeof(val))
368 return got_ferror(outfile, GOT_ERR_IO);
369 return NULL;
372 static const struct got_error *
373 write_fileindex_val32(struct got_hash *ctx, uint32_t val, FILE *outfile)
375 size_t n;
377 val = htobe32(val);
378 got_hash_update(ctx, &val, sizeof(val));
379 n = fwrite(&val, 1, sizeof(val), outfile);
380 if (n != sizeof(val))
381 return got_ferror(outfile, GOT_ERR_IO);
382 return NULL;
385 static const struct got_error *
386 write_fileindex_val16(struct got_hash *ctx, uint16_t val, FILE *outfile)
388 size_t n;
390 val = htobe16(val);
391 got_hash_update(ctx, &val, sizeof(val));
392 n = fwrite(&val, 1, sizeof(val), outfile);
393 if (n != sizeof(val))
394 return got_ferror(outfile, GOT_ERR_IO);
395 return NULL;
398 static const struct got_error *
399 write_fileindex_path(struct got_hash *ctx, const char *path, FILE *outfile)
401 size_t n, len, pad = 0;
402 static const uint8_t zero[8] = { 0 };
404 len = strlen(path);
405 while ((len + pad) % 8 != 0)
406 pad++;
407 if (pad == 0)
408 pad = 8; /* NUL-terminate */
410 got_hash_update(ctx, path, len);
411 n = fwrite(path, 1, len, outfile);
412 if (n != len)
413 return got_ferror(outfile, GOT_ERR_IO);
414 got_hash_update(ctx, zero, pad);
415 n = fwrite(zero, 1, pad, outfile);
416 if (n != pad)
417 return got_ferror(outfile, GOT_ERR_IO);
418 return NULL;
421 static const struct got_error *
422 write_fileindex_entry(struct got_hash *ctx, struct got_fileindex_entry *ie,
423 FILE *outfile)
425 const struct got_error *err;
426 size_t n;
427 uint32_t stage;
429 err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
430 if (err)
431 return err;
432 err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
433 if (err)
434 return err;
435 err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
436 if (err)
437 return err;
438 err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
439 if (err)
440 return err;
442 err = write_fileindex_val32(ctx, ie->uid, outfile);
443 if (err)
444 return err;
445 err = write_fileindex_val32(ctx, ie->gid, outfile);
446 if (err)
447 return err;
448 err = write_fileindex_val32(ctx, ie->size, outfile);
449 if (err)
450 return err;
452 err = write_fileindex_val16(ctx, ie->mode, outfile);
453 if (err)
454 return err;
456 got_hash_update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
457 n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
458 if (n != SHA1_DIGEST_LENGTH)
459 return got_ferror(outfile, GOT_ERR_IO);
461 got_hash_update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
462 n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
463 if (n != SHA1_DIGEST_LENGTH)
464 return got_ferror(outfile, GOT_ERR_IO);
466 err = write_fileindex_val32(ctx, ie->flags, outfile);
467 if (err)
468 return err;
470 err = write_fileindex_path(ctx, ie->path, outfile);
471 if (err)
472 return err;
474 stage = got_fileindex_entry_stage_get(ie);
475 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
476 stage == GOT_FILEIDX_STAGE_ADD) {
477 got_hash_update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
478 n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
479 outfile);
480 if (n != SHA1_DIGEST_LENGTH)
481 return got_ferror(outfile, GOT_ERR_IO);
484 return NULL;
487 const struct got_error *
488 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
490 const struct got_error *err = NULL;
491 struct got_fileindex_hdr hdr;
492 struct got_hash ctx;
493 uint8_t hash[GOT_HASH_DIGEST_MAXLEN];
494 size_t n;
495 struct got_fileindex_entry *ie, *tmp;
497 got_hash_init(&ctx, GOT_HASH_SHA1);
499 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
500 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
501 hdr.nentries = htobe32(fileindex->nentries);
503 got_hash_update(&ctx, &hdr.signature, sizeof(hdr.signature));
504 got_hash_update(&ctx, &hdr.version, sizeof(hdr.version));
505 got_hash_update(&ctx, &hdr.nentries, sizeof(hdr.nentries));
506 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
507 if (n != sizeof(hdr.signature))
508 return got_ferror(outfile, GOT_ERR_IO);
509 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
510 if (n != sizeof(hdr.version))
511 return got_ferror(outfile, GOT_ERR_IO);
512 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
513 if (n != sizeof(hdr.nentries))
514 return got_ferror(outfile, GOT_ERR_IO);
516 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
517 ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
518 ie->flags &= ~GOT_FILEIDX_F_SKIPPED;
519 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH) {
520 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
521 got_fileindex_entry_free(ie);
522 continue;
524 err = write_fileindex_entry(&ctx, ie, outfile);
525 if (err)
526 return err;
529 got_hash_final(&ctx, hash);
530 n = fwrite(hash, 1, SHA1_DIGEST_LENGTH, outfile);
531 if (n != SHA1_DIGEST_LENGTH)
532 return got_ferror(outfile, GOT_ERR_IO);
534 if (fflush(outfile) != 0)
535 return got_error_from_errno("fflush");
537 return NULL;
540 static const struct got_error *
541 read_fileindex_val64(uint64_t *val, struct got_hash *ctx, FILE *infile)
543 size_t n;
545 n = fread(val, 1, sizeof(*val), infile);
546 if (n != sizeof(*val))
547 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
548 got_hash_update(ctx, val, sizeof(*val));
549 *val = be64toh(*val);
550 return NULL;
553 static const struct got_error *
554 read_fileindex_val32(uint32_t *val, struct got_hash *ctx, FILE *infile)
556 size_t n;
558 n = fread(val, 1, sizeof(*val), infile);
559 if (n != sizeof(*val))
560 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
561 got_hash_update(ctx, val, sizeof(*val));
562 *val = be32toh(*val);
563 return NULL;
566 static const struct got_error *
567 read_fileindex_val16(uint16_t *val, struct got_hash *ctx, FILE *infile)
569 size_t n;
571 n = fread(val, 1, sizeof(*val), infile);
572 if (n != sizeof(*val))
573 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
574 got_hash_update(ctx, val, sizeof(*val));
575 *val = be16toh(*val);
576 return NULL;
579 static const struct got_error *
580 read_fileindex_path(char **path, struct got_hash *ctx, FILE *infile)
582 const size_t chunk_size = 8;
583 char p[PATH_MAX];
584 size_t n, len = 0;
586 do {
587 if (len + chunk_size > sizeof(p))
588 return got_error(GOT_ERR_FILEIDX_BAD);
590 n = fread(&p[len], 1, chunk_size, infile);
591 if (n != chunk_size)
592 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
594 got_hash_update(ctx, &p[len], chunk_size);
595 len += chunk_size;
596 } while (memchr(&p[len - chunk_size], '\0', chunk_size) == NULL);
598 *path = strdup(p);
599 if (*path == NULL)
600 return got_error_from_errno("strdup");
601 return NULL;
604 static const struct got_error *
605 read_fileindex_entry(struct got_fileindex_entry **iep, struct got_hash *ctx,
606 FILE *infile, uint32_t version)
608 const struct got_error *err;
609 struct got_fileindex_entry *ie;
610 size_t n;
612 *iep = NULL;
614 ie = calloc(1, sizeof(*ie));
615 if (ie == NULL)
616 return got_error_from_errno("calloc");
618 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
619 if (err)
620 goto done;
621 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
622 if (err)
623 goto done;
624 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
625 if (err)
626 goto done;
627 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
628 if (err)
629 goto done;
631 err = read_fileindex_val32(&ie->uid, ctx, infile);
632 if (err)
633 goto done;
634 err = read_fileindex_val32(&ie->gid, ctx, infile);
635 if (err)
636 goto done;
637 err = read_fileindex_val32(&ie->size, ctx, infile);
638 if (err)
639 goto done;
641 err = read_fileindex_val16(&ie->mode, ctx, infile);
642 if (err)
643 goto done;
645 n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
646 if (n != SHA1_DIGEST_LENGTH) {
647 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
648 goto done;
650 got_hash_update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
652 n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
653 if (n != SHA1_DIGEST_LENGTH) {
654 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
655 goto done;
657 got_hash_update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
659 err = read_fileindex_val32(&ie->flags, ctx, infile);
660 if (err)
661 goto done;
663 err = read_fileindex_path(&ie->path, ctx, infile);
664 if (err)
665 goto done;
667 if (version >= 2) {
668 uint32_t stage = got_fileindex_entry_stage_get(ie);
669 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
670 stage == GOT_FILEIDX_STAGE_ADD) {
671 n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
672 infile);
673 if (n != SHA1_DIGEST_LENGTH) {
674 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
675 goto done;
677 got_hash_update(ctx, ie->staged_blob_sha1,
678 SHA1_DIGEST_LENGTH);
680 } else {
681 /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
682 ie->flags &= ~GOT_FILEIDX_F_STAGE;
685 done:
686 if (err)
687 got_fileindex_entry_free(ie);
688 else
689 *iep = ie;
690 return err;
693 const struct got_error *
694 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
696 const struct got_error *err = NULL;
697 struct got_fileindex_hdr hdr;
698 struct got_hash ctx;
699 struct got_fileindex_entry *ie;
700 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
701 uint8_t sha1[SHA1_DIGEST_LENGTH];
702 size_t n;
703 int i;
705 got_hash_init(&ctx, GOT_HASH_SHA1);
707 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
708 if (n != sizeof(hdr.signature)) {
709 if (n == 0) /* EOF */
710 return NULL;
711 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
713 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
714 if (n != sizeof(hdr.version)) {
715 if (n == 0) /* EOF */
716 return NULL;
717 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
719 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
720 if (n != sizeof(hdr.nentries)) {
721 if (n == 0) /* EOF */
722 return NULL;
723 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
726 got_hash_update(&ctx, &hdr.signature, sizeof(hdr.signature));
727 got_hash_update(&ctx, &hdr.version, sizeof(hdr.version));
728 got_hash_update(&ctx, &hdr.nentries, sizeof(hdr.nentries));
730 hdr.signature = be32toh(hdr.signature);
731 hdr.version = be32toh(hdr.version);
732 hdr.nentries = be32toh(hdr.nentries);
734 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
735 return got_error(GOT_ERR_FILEIDX_SIG);
736 if (hdr.version > GOT_FILE_INDEX_VERSION)
737 return got_error(GOT_ERR_FILEIDX_VER);
739 for (i = 0; i < hdr.nentries; i++) {
740 err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
741 if (err)
742 return err;
743 err = add_entry(fileindex, ie);
744 if (err) {
745 got_fileindex_entry_free(ie);
746 return err;
750 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
751 if (n != sizeof(sha1_expected))
752 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
753 got_hash_final(&ctx, sha1);
754 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
755 return got_error(GOT_ERR_FILEIDX_CSUM);
757 return NULL;
760 static struct got_fileindex_entry *
761 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
763 struct got_fileindex_entry *next;
765 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
767 /* Skip entries which were added or removed by diff callbacks. */
768 while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
769 GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
770 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
772 return next;
775 static const struct got_error *
776 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
777 struct got_tree_object *tree, const char *, const char *,
778 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
780 static const struct got_error *
781 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
782 struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
783 const char *path, const char *entry_name, struct got_repository *repo,
784 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
786 const struct got_error *err = NULL;
787 struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
789 if (!got_object_tree_entry_is_submodule(te) &&
790 S_ISDIR(got_tree_entry_get_mode(te))) {
791 char *subpath;
792 struct got_tree_object *subtree;
794 if (asprintf(&subpath, "%s%s%s", path,
795 path[0] == '\0' ? "" : "/",
796 got_tree_entry_get_name(te)) == -1)
797 return got_error_from_errno("asprintf");
799 err = got_object_open_as_tree(&subtree, repo,
800 got_tree_entry_get_id(te));
801 if (err) {
802 free(subpath);
803 return err;
806 err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
807 entry_name, repo, cb, cb_arg);
808 free(subpath);
809 got_object_tree_close(subtree);
810 if (err)
811 return err;
814 (*tidx)++;
815 *next = got_object_tree_get_entry(tree, *tidx);
816 return NULL;
819 static const struct got_error *
820 diff_fileindex_tree(struct got_fileindex *fileindex,
821 struct got_fileindex_entry **ie, struct got_tree_object *tree,
822 const char *path, const char *entry_name, struct got_repository *repo,
823 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
825 const struct got_error *err = NULL;
826 struct got_tree_entry *te = NULL;
827 size_t path_len = strlen(path);
828 struct got_fileindex_entry *next;
829 int tidx = 0;
831 te = got_object_tree_get_entry(tree, tidx);
832 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
833 if (te && *ie) {
834 char *te_path;
835 const char *te_name = got_tree_entry_get_name(te);
836 int cmp;
837 if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
838 err = got_error_from_errno("asprintf");
839 break;
841 cmp = got_path_cmp((*ie)->path, te_path,
842 got_fileindex_entry_path_len(*ie), strlen(te_path));
843 free(te_path);
844 if (cmp == 0) {
845 if (got_path_is_child((*ie)->path, path,
846 path_len) &&
847 !got_object_tree_entry_is_submodule(te) &&
848 (entry_name == NULL ||
849 strcmp(te_name, entry_name) == 0)) {
850 err = cb->diff_old_new(cb_arg, *ie, te,
851 path);
852 if (err || entry_name)
853 break;
855 *ie = walk_fileindex(fileindex, *ie);
856 err = walk_tree(&te, fileindex, ie, tree, &tidx,
857 path, entry_name, repo, cb, cb_arg);
858 } else if (cmp < 0) {
859 next = walk_fileindex(fileindex, *ie);
860 if (got_path_is_child((*ie)->path, path,
861 path_len) && entry_name == NULL) {
862 err = cb->diff_old(cb_arg, *ie, path);
863 if (err || entry_name)
864 break;
866 *ie = next;
867 } else {
868 if ((entry_name == NULL ||
869 strcmp(te_name, entry_name) == 0)) {
870 err = cb->diff_new(cb_arg, te, path);
871 if (err || entry_name)
872 break;
874 err = walk_tree(&te, fileindex, ie, tree, &tidx,
875 path, entry_name, repo, cb, cb_arg);
877 if (err)
878 break;
879 } else if (*ie) {
880 next = walk_fileindex(fileindex, *ie);
881 if (got_path_is_child((*ie)->path, path, path_len) &&
882 (entry_name == NULL ||
883 (te && strcmp(got_tree_entry_get_name(te),
884 entry_name) == 0))) {
885 err = cb->diff_old(cb_arg, *ie, path);
886 if (err || entry_name)
887 break;
889 *ie = next;
890 } else if (te) {
891 if (!got_object_tree_entry_is_submodule(te) &&
892 (entry_name == NULL ||
893 strcmp(got_tree_entry_get_name(te), entry_name)
894 == 0)) {
895 err = cb->diff_new(cb_arg, te, path);
896 if (err || entry_name)
897 break;
899 err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
900 entry_name, repo, cb, cb_arg);
901 if (err)
902 break;
906 return err;
909 const struct got_error *
910 got_fileindex_diff_tree(struct got_fileindex *fileindex,
911 struct got_tree_object *tree, const char *path, const char *entry_name,
912 struct got_repository *repo,
913 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
915 struct got_fileindex_entry *ie;
916 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
917 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
918 ie = walk_fileindex(fileindex, ie);
919 return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
920 cb, cb_arg);
923 static const struct got_error *
924 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
925 struct got_pathlist_head *, int, const char *, const char *,
926 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
928 static struct dirent *
929 copy_dirent(const struct dirent *de)
931 size_t amt = de->d_reclen;
932 struct dirent *copy;
934 copy = malloc(amt);
935 if (copy != NULL) {
936 memcpy(copy, de, amt);
938 return copy;
941 static const struct got_error *
942 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
944 const struct got_error *err = NULL;
945 struct got_pathlist_entry *new = NULL;
946 struct dirent *de = NULL;
948 for (;;) {
949 errno = 0;
950 if ((de = readdir(dir)) == NULL) {
951 if (errno != 0) {
952 err = got_error_from_errno("readdir");
954 break;
957 if (strcmp(de->d_name, ".") == 0 ||
958 strcmp(de->d_name, "..") == 0 ||
959 (path[0] == '\0' &&
960 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0) ||
961 (path[0] == '\0' &&
962 strcmp(de->d_name, GOT_WORKTREE_CVG_DIR) == 0)) {
963 continue;
966 de = copy_dirent(de);
967 if (de == NULL) {
968 err = got_error_from_errno("malloc");
969 break;
971 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
972 if (err) {
973 free(de);
974 break;
976 if (new == NULL) {
977 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
978 free(de);
979 break;
983 return err;
986 static int
987 have_tracked_file_in_dir(struct got_fileindex *fileindex, const char *path)
989 struct got_fileindex_entry *ie;
990 size_t path_len = strlen(path);
991 int cmp;
993 ie = RB_ROOT(&fileindex->entries);
994 while (ie) {
995 if (got_path_is_child(ie->path, path, path_len))
996 return 1;
997 cmp = got_path_cmp(path, ie->path, path_len,
998 got_fileindex_entry_path_len(ie));
999 if (cmp < 0)
1000 ie = RB_LEFT(ie, entry);
1001 else if (cmp > 0)
1002 ie = RB_RIGHT(ie, entry);
1003 else
1004 break;
1007 return 0;
1010 static const struct got_error *
1011 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
1012 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
1013 const char *path, const char *rootpath, struct got_repository *repo,
1014 int ignore, struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1016 const struct got_error *err = NULL;
1017 struct dirent *de = dle->data;
1018 DIR *subdir = NULL;
1019 int subdirfd = -1;
1021 *next = NULL;
1023 /* Must traverse ignored directories if they contain tracked files. */
1024 if (de->d_type == DT_DIR && ignore &&
1025 have_tracked_file_in_dir(fileindex, path))
1026 ignore = 0;
1028 if (de->d_type == DT_DIR && !ignore) {
1029 char *subpath;
1030 char *subdirpath;
1031 struct got_pathlist_head subdirlist;
1033 TAILQ_INIT(&subdirlist);
1035 if (asprintf(&subpath, "%s%s%s", path,
1036 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1037 return got_error_from_errno("asprintf");
1039 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
1040 free(subpath);
1041 return got_error_from_errno("asprintf");
1044 subdirfd = openat(fd, de->d_name,
1045 O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
1046 if (subdirfd == -1) {
1047 if (errno == EACCES) {
1048 *next = TAILQ_NEXT(dle, entry);
1049 return NULL;
1051 err = got_error_from_errno2("openat", subdirpath);
1052 free(subpath);
1053 free(subdirpath);
1054 return err;
1057 subdir = fdopendir(subdirfd);
1058 if (subdir == NULL) {
1059 err = got_error_from_errno2("fdopendir", path);
1060 close(subdirfd);
1061 free(subpath);
1062 free(subdirpath);
1063 return err;
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 got_pathlist_free(&subdirlist, GOT_PATHLIST_FREE_DATA);
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 + de->d_namlen);
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 got_pathlist_free(&dirlist, GOT_PATHLIST_FREE_DATA);
1239 return err;
1242 struct got_object_id *
1243 got_fileindex_entry_get_staged_blob_id(struct got_object_id *id,
1244 struct got_fileindex_entry *ie)
1246 memset(id, 0, sizeof(*id));
1247 memcpy(id->sha1, ie->staged_blob_sha1, sizeof(ie->staged_blob_sha1));
1248 return id;
1251 struct got_object_id *
1252 got_fileindex_entry_get_blob_id(struct got_object_id *id,
1253 struct got_fileindex_entry *ie)
1255 memset(id, 0, sizeof(*id));
1256 memcpy(id->sha1, ie->blob_sha1, sizeof(ie->blob_sha1));
1257 return id;
1260 struct got_object_id *
1261 got_fileindex_entry_get_commit_id(struct got_object_id *id,
1262 struct got_fileindex_entry *ie)
1264 memset(id, 0, sizeof(*id));
1265 memcpy(id->sha1, ie->commit_sha1, sizeof(ie->commit_sha1));
1266 return id;
1269 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);