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 const struct got_error *
929 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
931 const struct got_error *err = NULL;
932 struct got_pathlist_entry *new = NULL;
933 struct dirent *dep = NULL;
934 struct dirent *de = NULL;
936 for (;;) {
937 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
938 if (de == NULL) {
939 err = got_error_from_errno("malloc");
940 break;
943 if (readdir_r(dir, de, &dep) != 0) {
944 err = got_error_from_errno("readdir_r");
945 free(de);
946 break;
948 if (dep == NULL) {
949 free(de);
950 break;
953 if (strcmp(de->d_name, ".") == 0 ||
954 strcmp(de->d_name, "..") == 0 ||
955 (path[0] == '\0' &&
956 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0) ||
957 (path[0] == '\0' &&
958 strcmp(de->d_name, GOT_WORKTREE_CVG_DIR) == 0)) {
959 free(de);
960 continue;
963 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
964 if (err) {
965 free(de);
966 break;
968 if (new == NULL) {
969 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
970 free(de);
971 break;
975 return err;
978 static int
979 have_tracked_file_in_dir(struct got_fileindex *fileindex, const char *path)
981 struct got_fileindex_entry *ie;
982 size_t path_len = strlen(path);
983 int cmp;
985 ie = RB_ROOT(&fileindex->entries);
986 while (ie) {
987 if (got_path_is_child(ie->path, path, path_len))
988 return 1;
989 cmp = got_path_cmp(path, ie->path, path_len,
990 got_fileindex_entry_path_len(ie));
991 if (cmp < 0)
992 ie = RB_LEFT(ie, entry);
993 else if (cmp > 0)
994 ie = RB_RIGHT(ie, entry);
995 else
996 break;
999 return 0;
1002 static const struct got_error *
1003 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
1004 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
1005 const char *path, const char *rootpath, struct got_repository *repo,
1006 int ignore, struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1008 const struct got_error *err = NULL;
1009 struct dirent *de = dle->data;
1010 DIR *subdir = NULL;
1011 int subdirfd = -1;
1013 *next = NULL;
1015 /* Must traverse ignored directories if they contain tracked files. */
1016 if (de->d_type == DT_DIR && ignore &&
1017 have_tracked_file_in_dir(fileindex, path))
1018 ignore = 0;
1020 if (de->d_type == DT_DIR && !ignore) {
1021 char *subpath;
1022 char *subdirpath;
1023 struct got_pathlist_head subdirlist;
1025 TAILQ_INIT(&subdirlist);
1027 if (asprintf(&subpath, "%s%s%s", path,
1028 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1029 return got_error_from_errno("asprintf");
1031 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
1032 free(subpath);
1033 return got_error_from_errno("asprintf");
1036 subdirfd = openat(fd, de->d_name,
1037 O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
1038 if (subdirfd == -1) {
1039 if (errno == EACCES) {
1040 *next = TAILQ_NEXT(dle, entry);
1041 return NULL;
1043 err = got_error_from_errno2("openat", subdirpath);
1044 free(subpath);
1045 free(subdirpath);
1046 return err;
1049 subdir = fdopendir(subdirfd);
1050 if (subdir == NULL) {
1051 err = got_error_from_errno2("fdopendir", path);
1052 close(subdirfd);
1053 free(subpath);
1054 free(subdirpath);
1055 return err;
1057 subdirfd = -1;
1058 err = read_dirlist(&subdirlist, subdir, subdirpath);
1059 if (err) {
1060 free(subpath);
1061 free(subdirpath);
1062 closedir(subdir);
1063 return err;
1065 err = diff_fileindex_dir(fileindex, ie, &subdirlist,
1066 dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
1067 if (subdir && closedir(subdir) == -1 && err == NULL)
1068 err = got_error_from_errno2("closedir", subdirpath);
1069 free(subpath);
1070 free(subdirpath);
1071 got_pathlist_free(&subdirlist, GOT_PATHLIST_FREE_DATA);
1072 if (err)
1073 return err;
1076 *next = TAILQ_NEXT(dle, entry);
1077 return NULL;
1080 static const struct got_error *
1081 dirent_type_fixup(struct dirent *de, const char *rootpath, const char *path)
1083 const struct got_error *err;
1084 char *dir_path;
1085 int type;
1087 if (de->d_type != DT_UNKNOWN)
1088 return NULL;
1090 /* DT_UNKNOWN occurs on NFS mounts without "readdir plus" RPC. */
1091 if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
1092 return got_error_from_errno("asprintf");
1093 err = got_path_dirent_type(&type, dir_path, de);
1094 free(dir_path);
1095 if (err)
1096 return err;
1098 de->d_type = type;
1099 return NULL;
1102 static const struct got_error *
1103 diff_fileindex_dir(struct got_fileindex *fileindex,
1104 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1105 int dirfd, const char *rootpath, const char *path,
1106 struct got_repository *repo,
1107 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1109 const struct got_error *err = NULL;
1110 struct dirent *de = NULL;
1111 size_t path_len = strlen(path);
1112 struct got_pathlist_entry *dle;
1113 int ignore;
1115 if (cb->diff_traverse) {
1116 err = cb->diff_traverse(cb_arg, path, dirfd);
1117 if (err)
1118 return err;
1121 dle = TAILQ_FIRST(dirlist);
1122 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1123 if (dle && *ie) {
1124 char *de_path;
1125 int cmp;
1126 de = dle->data;
1127 err = dirent_type_fixup(de, rootpath, path);
1128 if (err)
1129 break;
1130 if (asprintf(&de_path, "%s/%s", path,
1131 de->d_name) == -1) {
1132 err = got_error_from_errno("asprintf");
1133 break;
1135 cmp = got_path_cmp((*ie)->path, de_path,
1136 got_fileindex_entry_path_len(*ie),
1137 strlen(path) + 1 + de->d_namlen);
1138 free(de_path);
1139 if (cmp == 0) {
1140 err = cb->diff_old_new(cb_arg, *ie, de, path,
1141 dirfd);
1142 if (err)
1143 break;
1144 *ie = walk_fileindex(fileindex, *ie);
1145 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1146 path, rootpath, repo, 0, cb, cb_arg);
1147 } else if (cmp < 0 ) {
1148 err = cb->diff_old(cb_arg, *ie, path);
1149 if (err)
1150 break;
1151 *ie = walk_fileindex(fileindex, *ie);
1152 } else {
1153 err = cb->diff_new(&ignore, cb_arg, de, path,
1154 dirfd);
1155 if (err)
1156 break;
1157 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1158 path, rootpath, repo, ignore, cb, cb_arg);
1160 if (err)
1161 break;
1162 } else if (*ie) {
1163 err = cb->diff_old(cb_arg, *ie, path);
1164 if (err)
1165 break;
1166 *ie = walk_fileindex(fileindex, *ie);
1167 } else if (dle) {
1168 de = dle->data;
1169 err = dirent_type_fixup(de, rootpath, path);
1170 if (err)
1171 break;
1172 err = cb->diff_new(&ignore, cb_arg, de, path, dirfd);
1173 if (err)
1174 break;
1175 err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1176 rootpath, repo, ignore, cb, cb_arg);
1177 if (err)
1178 break;
1182 return err;
1185 const struct got_error *
1186 got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1187 const char *rootpath, const char *path, struct got_repository *repo,
1188 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1190 const struct got_error *err;
1191 struct got_fileindex_entry *ie;
1192 struct got_pathlist_head dirlist;
1193 int fd2;
1194 DIR *dir;
1196 TAILQ_INIT(&dirlist);
1199 * Duplicate the file descriptor so we can call closedir() below
1200 * without closing the file descriptor passed in by our caller.
1202 fd2 = dup(fd);
1203 if (fd2 == -1)
1204 return got_error_from_errno2("dup", path);
1205 if (lseek(fd2, 0, SEEK_SET) == -1) {
1206 err = got_error_from_errno2("lseek", path);
1207 close(fd2);
1208 return err;
1210 dir = fdopendir(fd2);
1211 if (dir == NULL) {
1212 err = got_error_from_errno2("fdopendir", path);
1213 close(fd2);
1214 return err;
1216 err = read_dirlist(&dirlist, dir, path);
1217 if (err) {
1218 closedir(dir);
1219 return err;
1222 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1223 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1224 ie = walk_fileindex(fileindex, ie);
1225 err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1226 rootpath, path, repo, cb, cb_arg);
1228 if (closedir(dir) == -1 && err == NULL)
1229 err = got_error_from_errno2("closedir", path);
1230 got_pathlist_free(&dirlist, GOT_PATHLIST_FREE_DATA);
1231 return err;
1234 struct got_object_id *
1235 got_fileindex_entry_get_staged_blob_id(struct got_object_id *id,
1236 struct got_fileindex_entry *ie)
1238 memset(id, 0, sizeof(*id));
1239 memcpy(id->sha1, ie->staged_blob_sha1, sizeof(ie->staged_blob_sha1));
1240 return id;
1243 struct got_object_id *
1244 got_fileindex_entry_get_blob_id(struct got_object_id *id,
1245 struct got_fileindex_entry *ie)
1247 memset(id, 0, sizeof(*id));
1248 memcpy(id->sha1, ie->blob_sha1, sizeof(ie->blob_sha1));
1249 return id;
1252 struct got_object_id *
1253 got_fileindex_entry_get_commit_id(struct got_object_id *id,
1254 struct got_fileindex_entry *ie)
1256 memset(id, 0, sizeof(*id));
1257 memcpy(id->sha1, ie->commit_sha1, sizeof(ie->commit_sha1));
1258 return id;
1261 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);