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 <string.h>
27 #include <sha1.h>
28 #include <sha2.h>
29 #include <endian.h>
30 #include <limits.h>
31 #include <unistd.h>
32 #include <uuid.h>
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_path.h"
38 #include "got_lib_hash.h"
39 #include "got_lib_fileindex.h"
40 #include "got_lib_worktree.h"
42 /* got_fileindex_entry flags */
43 #define GOT_FILEIDX_F_PATH_LEN 0x00000fff
44 #define GOT_FILEIDX_F_STAGE 0x0000f000
45 #define GOT_FILEIDX_F_STAGE_SHIFT 12
46 #define GOT_FILEIDX_F_NOT_FLUSHED 0x00010000
47 #define GOT_FILEIDX_F_NO_BLOB 0x00020000
48 #define GOT_FILEIDX_F_NO_COMMIT 0x00040000
49 #define GOT_FILEIDX_F_NO_FILE_ON_DISK 0x00080000
50 #define GOT_FILEIDX_F_REMOVE_ON_FLUSH 0x00100000
51 #define GOT_FILEIDX_F_SKIPPED 0x00200000
53 struct got_fileindex {
54 struct got_fileindex_tree entries;
55 int nentries; /* Does not include entries marked for removal. */
56 #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
57 };
59 mode_t
60 got_fileindex_entry_perms_get(struct got_fileindex_entry *ie)
61 {
62 return ((ie->mode & GOT_FILEIDX_MODE_PERMS) >>
63 GOT_FILEIDX_MODE_PERMS_SHIFT);
64 }
66 static void
67 fileindex_entry_perms_set(struct got_fileindex_entry *ie, mode_t mode)
68 {
69 ie->mode &= ~GOT_FILEIDX_MODE_PERMS;
70 ie->mode |= ((mode << GOT_FILEIDX_MODE_PERMS_SHIFT) &
71 GOT_FILEIDX_MODE_PERMS);
72 }
74 mode_t
75 got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
76 {
77 mode_t perms = got_fileindex_entry_perms_get(ie);
78 int type = got_fileindex_entry_filetype_get(ie);
79 uint32_t ftype;
81 if (type == GOT_FILEIDX_MODE_REGULAR_FILE ||
82 type == GOT_FILEIDX_MODE_BAD_SYMLINK)
83 ftype = S_IFREG;
84 else
85 ftype = S_IFLNK;
87 return (ftype | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
88 }
90 const struct got_error *
91 got_fileindex_entry_update(struct got_fileindex_entry *ie,
92 int wt_fd, const char *ondisk_path, uint8_t *blob_sha1,
93 uint8_t *commit_sha1, int update_timestamps)
94 {
95 struct stat sb;
97 if (fstatat(wt_fd, ondisk_path, &sb, AT_SYMLINK_NOFOLLOW) != 0) {
98 if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
99 errno == ENOENT))
100 return got_error_from_errno2("fstatat", ondisk_path);
101 sb.st_mode = GOT_DEFAULT_FILE_MODE;
102 } else {
103 if (sb.st_mode & S_IFDIR)
104 return got_error_set_errno(EISDIR, ondisk_path);
105 ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
108 if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
109 if (update_timestamps) {
110 ie->ctime_sec = sb.st_ctim.tv_sec;
111 ie->ctime_nsec = sb.st_ctim.tv_nsec;
112 ie->mtime_sec = sb.st_mtim.tv_sec;
113 ie->mtime_nsec = sb.st_mtim.tv_nsec;
115 ie->uid = sb.st_uid;
116 ie->gid = sb.st_gid;
117 ie->size = (sb.st_size & 0xffffffff);
118 if (S_ISLNK(sb.st_mode)) {
119 got_fileindex_entry_filetype_set(ie,
120 GOT_FILEIDX_MODE_SYMLINK);
121 fileindex_entry_perms_set(ie, 0);
122 } else {
123 got_fileindex_entry_filetype_set(ie,
124 GOT_FILEIDX_MODE_REGULAR_FILE);
125 fileindex_entry_perms_set(ie,
126 sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
130 if (blob_sha1) {
131 memmove(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
132 ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
133 } else
134 ie->flags |= GOT_FILEIDX_F_NO_BLOB;
136 if (commit_sha1) {
137 memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
138 ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
139 } else
140 ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
142 return NULL;
145 void
146 got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
148 ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
151 void
152 got_fileindex_entry_mark_skipped(struct got_fileindex_entry *ie)
154 ie->flags |= GOT_FILEIDX_F_SKIPPED;
157 const struct got_error *
158 got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
159 const char *relpath)
161 size_t len;
163 *ie = calloc(1, sizeof(**ie));
164 if (*ie == NULL)
165 return got_error_from_errno("calloc");
167 (*ie)->path = strdup(relpath);
168 if ((*ie)->path == NULL) {
169 const struct got_error *err = got_error_from_errno("strdup");
170 free(*ie);
171 *ie = NULL;
172 return err;
175 len = strlen(relpath);
176 if (len > GOT_FILEIDX_F_PATH_LEN)
177 len = GOT_FILEIDX_F_PATH_LEN;
178 (*ie)->flags |= len;
180 return NULL;
183 void
184 got_fileindex_entry_free(struct got_fileindex_entry *ie)
186 free(ie->path);
187 free(ie);
190 size_t
191 got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
193 return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
196 uint32_t
197 got_fileindex_entry_stage_get(const struct got_fileindex_entry *ie)
199 return ((ie->flags & GOT_FILEIDX_F_STAGE) >> GOT_FILEIDX_F_STAGE_SHIFT);
202 void
203 got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage)
205 ie->flags &= ~GOT_FILEIDX_F_STAGE;
206 ie->flags |= ((stage << GOT_FILEIDX_F_STAGE_SHIFT) &
207 GOT_FILEIDX_F_STAGE);
210 int
211 got_fileindex_entry_filetype_get(struct got_fileindex_entry *ie)
213 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
216 void
217 got_fileindex_entry_filetype_set(struct got_fileindex_entry *ie, int type)
219 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_ONDISK;
220 ie->mode |= (type & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
223 void
224 got_fileindex_entry_staged_filetype_set(struct got_fileindex_entry *ie,
225 int type)
227 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_STAGED;
228 ie->mode |= ((type << GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT) &
229 GOT_FILEIDX_MODE_FILE_TYPE_STAGED);
232 int
233 got_fileindex_entry_staged_filetype_get(struct got_fileindex_entry *ie)
235 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_STAGED) >>
236 GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT;
239 int
240 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
242 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
245 int
246 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
248 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
251 int
252 got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
254 return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
257 int
258 got_fileindex_entry_was_skipped(struct got_fileindex_entry *ie)
260 return (ie->flags & GOT_FILEIDX_F_SKIPPED) != 0;
263 static const struct got_error *
264 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
266 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
267 return got_error(GOT_ERR_NO_SPACE);
269 if (RB_INSERT(got_fileindex_tree, &fileindex->entries, ie) != NULL)
270 return got_error_path(ie->path, GOT_ERR_FILEIDX_DUP_ENTRY);
272 fileindex->nentries++;
273 return NULL;
276 const struct got_error *
277 got_fileindex_entry_add(struct got_fileindex *fileindex,
278 struct got_fileindex_entry *ie)
280 /* Flag this entry until it gets written out to disk. */
281 ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
283 return add_entry(fileindex, ie);
286 void
287 got_fileindex_entry_remove(struct got_fileindex *fileindex,
288 struct got_fileindex_entry *ie)
290 /*
291 * Removing an entry from the RB tree immediately breaks
292 * in-progress iterations over file index entries.
293 * So flag this entry for removal and remove it once the index
294 * is written out to disk. Meanwhile, pretend this entry no longer
295 * exists if we get queried for it again before then.
296 */
297 ie->flags |= GOT_FILEIDX_F_REMOVE_ON_FLUSH;
298 fileindex->nentries--;
301 struct got_fileindex_entry *
302 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
303 size_t path_len)
305 struct got_fileindex_entry *ie;
306 struct got_fileindex_entry key;
307 memset(&key, 0, sizeof(key));
308 key.path = (char *)path;
309 key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
310 ie = RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
311 if (ie && (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH))
312 return NULL;
313 return ie;
316 const struct got_error *
317 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
318 got_fileindex_cb cb, void *cb_arg)
320 const struct got_error *err;
321 struct got_fileindex_entry *ie, *tmp;
323 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
324 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
325 continue;
326 err = (*cb)(cb_arg, ie);
327 if (err)
328 return err;
330 return NULL;
333 struct got_fileindex *
334 got_fileindex_alloc(void)
336 struct got_fileindex *fileindex;
338 fileindex = calloc(1, sizeof(*fileindex));
339 if (fileindex == NULL)
340 return NULL;
342 RB_INIT(&fileindex->entries);
343 return fileindex;
346 void
347 got_fileindex_free(struct got_fileindex *fileindex)
349 struct got_fileindex_entry *ie;
351 while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
352 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
353 got_fileindex_entry_free(ie);
355 free(fileindex);
358 static const struct got_error *
359 write_fileindex_val64(struct got_hash *ctx, uint64_t val, FILE *outfile)
361 size_t n;
363 val = htobe64(val);
364 got_hash_update(ctx, &val, sizeof(val));
365 n = fwrite(&val, 1, sizeof(val), outfile);
366 if (n != sizeof(val))
367 return got_ferror(outfile, GOT_ERR_IO);
368 return NULL;
371 static const struct got_error *
372 write_fileindex_val32(struct got_hash *ctx, uint32_t val, FILE *outfile)
374 size_t n;
376 val = htobe32(val);
377 got_hash_update(ctx, &val, sizeof(val));
378 n = fwrite(&val, 1, sizeof(val), outfile);
379 if (n != sizeof(val))
380 return got_ferror(outfile, GOT_ERR_IO);
381 return NULL;
384 static const struct got_error *
385 write_fileindex_val16(struct got_hash *ctx, uint16_t val, FILE *outfile)
387 size_t n;
389 val = htobe16(val);
390 got_hash_update(ctx, &val, sizeof(val));
391 n = fwrite(&val, 1, sizeof(val), outfile);
392 if (n != sizeof(val))
393 return got_ferror(outfile, GOT_ERR_IO);
394 return NULL;
397 static const struct got_error *
398 write_fileindex_path(struct got_hash *ctx, const char *path, FILE *outfile)
400 size_t n, len, pad = 0;
401 static const uint8_t zero[8] = { 0 };
403 len = strlen(path);
404 while ((len + pad) % 8 != 0)
405 pad++;
406 if (pad == 0)
407 pad = 8; /* NUL-terminate */
409 got_hash_update(ctx, path, len);
410 n = fwrite(path, 1, len, outfile);
411 if (n != len)
412 return got_ferror(outfile, GOT_ERR_IO);
413 got_hash_update(ctx, zero, pad);
414 n = fwrite(zero, 1, pad, outfile);
415 if (n != pad)
416 return got_ferror(outfile, GOT_ERR_IO);
417 return NULL;
420 static const struct got_error *
421 write_fileindex_entry(struct got_hash *ctx, struct got_fileindex_entry *ie,
422 FILE *outfile)
424 const struct got_error *err;
425 size_t n;
426 uint32_t stage;
428 err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
429 if (err)
430 return err;
431 err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
432 if (err)
433 return err;
434 err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
435 if (err)
436 return err;
437 err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
438 if (err)
439 return err;
441 err = write_fileindex_val32(ctx, ie->uid, outfile);
442 if (err)
443 return err;
444 err = write_fileindex_val32(ctx, ie->gid, outfile);
445 if (err)
446 return err;
447 err = write_fileindex_val32(ctx, ie->size, outfile);
448 if (err)
449 return err;
451 err = write_fileindex_val16(ctx, ie->mode, outfile);
452 if (err)
453 return err;
455 got_hash_update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
456 n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
457 if (n != SHA1_DIGEST_LENGTH)
458 return got_ferror(outfile, GOT_ERR_IO);
460 got_hash_update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
461 n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
462 if (n != SHA1_DIGEST_LENGTH)
463 return got_ferror(outfile, GOT_ERR_IO);
465 err = write_fileindex_val32(ctx, ie->flags, outfile);
466 if (err)
467 return err;
469 err = write_fileindex_path(ctx, ie->path, outfile);
470 if (err)
471 return err;
473 stage = got_fileindex_entry_stage_get(ie);
474 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
475 stage == GOT_FILEIDX_STAGE_ADD) {
476 got_hash_update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
477 n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
478 outfile);
479 if (n != SHA1_DIGEST_LENGTH)
480 return got_ferror(outfile, GOT_ERR_IO);
483 return NULL;
486 const struct got_error *
487 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
489 const struct got_error *err = NULL;
490 struct got_fileindex_hdr hdr;
491 struct got_hash ctx;
492 uint8_t hash[GOT_HASH_DIGEST_MAXLEN];
493 size_t n;
494 struct got_fileindex_entry *ie, *tmp;
496 got_hash_init(&ctx, GOT_HASH_SHA1);
498 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
499 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
500 hdr.nentries = htobe32(fileindex->nentries);
502 got_hash_update(&ctx, &hdr.signature, sizeof(hdr.signature));
503 got_hash_update(&ctx, &hdr.version, sizeof(hdr.version));
504 got_hash_update(&ctx, &hdr.nentries, sizeof(hdr.nentries));
505 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
506 if (n != sizeof(hdr.signature))
507 return got_ferror(outfile, GOT_ERR_IO);
508 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
509 if (n != sizeof(hdr.version))
510 return got_ferror(outfile, GOT_ERR_IO);
511 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
512 if (n != sizeof(hdr.nentries))
513 return got_ferror(outfile, GOT_ERR_IO);
515 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
516 ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
517 ie->flags &= ~GOT_FILEIDX_F_SKIPPED;
518 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH) {
519 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
520 got_fileindex_entry_free(ie);
521 continue;
523 err = write_fileindex_entry(&ctx, ie, outfile);
524 if (err)
525 return err;
528 got_hash_final(&ctx, hash);
529 n = fwrite(hash, 1, SHA1_DIGEST_LENGTH, outfile);
530 if (n != SHA1_DIGEST_LENGTH)
531 return got_ferror(outfile, GOT_ERR_IO);
533 if (fflush(outfile) != 0)
534 return got_error_from_errno("fflush");
536 return NULL;
539 static const struct got_error *
540 read_fileindex_val64(uint64_t *val, struct got_hash *ctx, FILE *infile)
542 size_t n;
544 n = fread(val, 1, sizeof(*val), infile);
545 if (n != sizeof(*val))
546 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
547 got_hash_update(ctx, val, sizeof(*val));
548 *val = be64toh(*val);
549 return NULL;
552 static const struct got_error *
553 read_fileindex_val32(uint32_t *val, struct got_hash *ctx, FILE *infile)
555 size_t n;
557 n = fread(val, 1, sizeof(*val), infile);
558 if (n != sizeof(*val))
559 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
560 got_hash_update(ctx, val, sizeof(*val));
561 *val = be32toh(*val);
562 return NULL;
565 static const struct got_error *
566 read_fileindex_val16(uint16_t *val, struct got_hash *ctx, FILE *infile)
568 size_t n;
570 n = fread(val, 1, sizeof(*val), infile);
571 if (n != sizeof(*val))
572 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
573 got_hash_update(ctx, val, sizeof(*val));
574 *val = be16toh(*val);
575 return NULL;
578 static const struct got_error *
579 read_fileindex_path(char **path, struct got_hash *ctx, FILE *infile)
581 const struct got_error *err = NULL;
582 const size_t chunk_size = 8;
583 size_t n, len = 0, totlen = chunk_size;
585 *path = malloc(totlen);
586 if (*path == NULL)
587 return got_error_from_errno("malloc");
589 do {
590 if (len + chunk_size > totlen) {
591 char *p = reallocarray(*path, totlen + chunk_size, 1);
592 if (p == NULL) {
593 err = got_error_from_errno("reallocarray");
594 break;
596 totlen += chunk_size;
597 *path = p;
599 n = fread(*path + len, 1, chunk_size, infile);
600 if (n != chunk_size) {
601 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
602 break;
604 got_hash_update(ctx, *path + len, chunk_size);
605 len += chunk_size;
606 } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
608 if (err) {
609 free(*path);
610 *path = NULL;
612 return err;
615 static const struct got_error *
616 read_fileindex_entry(struct got_fileindex_entry **iep, struct got_hash *ctx,
617 FILE *infile, uint32_t version)
619 const struct got_error *err;
620 struct got_fileindex_entry *ie;
621 size_t n;
623 *iep = NULL;
625 ie = calloc(1, sizeof(*ie));
626 if (ie == NULL)
627 return got_error_from_errno("calloc");
629 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
630 if (err)
631 goto done;
632 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
633 if (err)
634 goto done;
635 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
636 if (err)
637 goto done;
638 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
639 if (err)
640 goto done;
642 err = read_fileindex_val32(&ie->uid, ctx, infile);
643 if (err)
644 goto done;
645 err = read_fileindex_val32(&ie->gid, ctx, infile);
646 if (err)
647 goto done;
648 err = read_fileindex_val32(&ie->size, ctx, infile);
649 if (err)
650 goto done;
652 err = read_fileindex_val16(&ie->mode, ctx, infile);
653 if (err)
654 goto done;
656 n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
657 if (n != SHA1_DIGEST_LENGTH) {
658 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
659 goto done;
661 got_hash_update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
663 n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
664 if (n != SHA1_DIGEST_LENGTH) {
665 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
666 goto done;
668 got_hash_update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
670 err = read_fileindex_val32(&ie->flags, ctx, infile);
671 if (err)
672 goto done;
674 err = read_fileindex_path(&ie->path, ctx, infile);
675 if (err)
676 goto done;
678 if (version >= 2) {
679 uint32_t stage = got_fileindex_entry_stage_get(ie);
680 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
681 stage == GOT_FILEIDX_STAGE_ADD) {
682 n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
683 infile);
684 if (n != SHA1_DIGEST_LENGTH) {
685 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
686 goto done;
688 got_hash_update(ctx, ie->staged_blob_sha1,
689 SHA1_DIGEST_LENGTH);
691 } else {
692 /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
693 ie->flags &= ~GOT_FILEIDX_F_STAGE;
696 done:
697 if (err)
698 got_fileindex_entry_free(ie);
699 else
700 *iep = ie;
701 return err;
704 const struct got_error *
705 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
707 const struct got_error *err = NULL;
708 struct got_fileindex_hdr hdr;
709 struct got_hash ctx;
710 struct got_fileindex_entry *ie;
711 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
712 uint8_t sha1[SHA1_DIGEST_LENGTH];
713 size_t n;
714 int i;
716 got_hash_init(&ctx, GOT_HASH_SHA1);
718 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
719 if (n != sizeof(hdr.signature)) {
720 if (n == 0) /* EOF */
721 return NULL;
722 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
724 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
725 if (n != sizeof(hdr.version)) {
726 if (n == 0) /* EOF */
727 return NULL;
728 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
730 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
731 if (n != sizeof(hdr.nentries)) {
732 if (n == 0) /* EOF */
733 return NULL;
734 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
737 got_hash_update(&ctx, &hdr.signature, sizeof(hdr.signature));
738 got_hash_update(&ctx, &hdr.version, sizeof(hdr.version));
739 got_hash_update(&ctx, &hdr.nentries, sizeof(hdr.nentries));
741 hdr.signature = be32toh(hdr.signature);
742 hdr.version = be32toh(hdr.version);
743 hdr.nentries = be32toh(hdr.nentries);
745 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
746 return got_error(GOT_ERR_FILEIDX_SIG);
747 if (hdr.version > GOT_FILE_INDEX_VERSION)
748 return got_error(GOT_ERR_FILEIDX_VER);
750 for (i = 0; i < hdr.nentries; i++) {
751 err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
752 if (err)
753 return err;
754 err = add_entry(fileindex, ie);
755 if (err) {
756 got_fileindex_entry_free(ie);
757 return err;
761 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
762 if (n != sizeof(sha1_expected))
763 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
764 got_hash_final(&ctx, sha1);
765 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
766 return got_error(GOT_ERR_FILEIDX_CSUM);
768 return NULL;
771 static struct got_fileindex_entry *
772 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
774 struct got_fileindex_entry *next;
776 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
778 /* Skip entries which were added or removed by diff callbacks. */
779 while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
780 GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
781 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
783 return next;
786 static const struct got_error *
787 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
788 struct got_tree_object *tree, const char *, const char *,
789 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
791 static const struct got_error *
792 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
793 struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
794 const char *path, const char *entry_name, struct got_repository *repo,
795 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
797 const struct got_error *err = NULL;
798 struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
800 if (!got_object_tree_entry_is_submodule(te) &&
801 S_ISDIR(got_tree_entry_get_mode(te))) {
802 char *subpath;
803 struct got_tree_object *subtree;
805 if (asprintf(&subpath, "%s%s%s", path,
806 path[0] == '\0' ? "" : "/",
807 got_tree_entry_get_name(te)) == -1)
808 return got_error_from_errno("asprintf");
810 err = got_object_open_as_tree(&subtree, repo,
811 got_tree_entry_get_id(te));
812 if (err) {
813 free(subpath);
814 return err;
817 err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
818 entry_name, repo, cb, cb_arg);
819 free(subpath);
820 got_object_tree_close(subtree);
821 if (err)
822 return err;
825 (*tidx)++;
826 *next = got_object_tree_get_entry(tree, *tidx);
827 return NULL;
830 static const struct got_error *
831 diff_fileindex_tree(struct got_fileindex *fileindex,
832 struct got_fileindex_entry **ie, struct got_tree_object *tree,
833 const char *path, const char *entry_name, struct got_repository *repo,
834 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
836 const struct got_error *err = NULL;
837 struct got_tree_entry *te = NULL;
838 size_t path_len = strlen(path);
839 struct got_fileindex_entry *next;
840 int tidx = 0;
842 te = got_object_tree_get_entry(tree, tidx);
843 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
844 if (te && *ie) {
845 char *te_path;
846 const char *te_name = got_tree_entry_get_name(te);
847 int cmp;
848 if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
849 err = got_error_from_errno("asprintf");
850 break;
852 cmp = got_path_cmp((*ie)->path, te_path,
853 got_fileindex_entry_path_len(*ie), strlen(te_path));
854 free(te_path);
855 if (cmp == 0) {
856 if (got_path_is_child((*ie)->path, path,
857 path_len) &&
858 !got_object_tree_entry_is_submodule(te) &&
859 (entry_name == NULL ||
860 strcmp(te_name, entry_name) == 0)) {
861 err = cb->diff_old_new(cb_arg, *ie, te,
862 path);
863 if (err || entry_name)
864 break;
866 *ie = walk_fileindex(fileindex, *ie);
867 err = walk_tree(&te, fileindex, ie, tree, &tidx,
868 path, entry_name, repo, cb, cb_arg);
869 } else if (cmp < 0) {
870 next = walk_fileindex(fileindex, *ie);
871 if (got_path_is_child((*ie)->path, path,
872 path_len) && entry_name == NULL) {
873 err = cb->diff_old(cb_arg, *ie, path);
874 if (err || entry_name)
875 break;
877 *ie = next;
878 } else {
879 if ((entry_name == NULL ||
880 strcmp(te_name, entry_name) == 0)) {
881 err = cb->diff_new(cb_arg, te, path);
882 if (err || entry_name)
883 break;
885 err = walk_tree(&te, fileindex, ie, tree, &tidx,
886 path, entry_name, repo, cb, cb_arg);
888 if (err)
889 break;
890 } else if (*ie) {
891 next = walk_fileindex(fileindex, *ie);
892 if (got_path_is_child((*ie)->path, path, path_len) &&
893 (entry_name == NULL ||
894 (te && strcmp(got_tree_entry_get_name(te),
895 entry_name) == 0))) {
896 err = cb->diff_old(cb_arg, *ie, path);
897 if (err || entry_name)
898 break;
900 *ie = next;
901 } else if (te) {
902 if (!got_object_tree_entry_is_submodule(te) &&
903 (entry_name == NULL ||
904 strcmp(got_tree_entry_get_name(te), entry_name)
905 == 0)) {
906 err = cb->diff_new(cb_arg, te, path);
907 if (err || entry_name)
908 break;
910 err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
911 entry_name, repo, cb, cb_arg);
912 if (err)
913 break;
917 return err;
920 const struct got_error *
921 got_fileindex_diff_tree(struct got_fileindex *fileindex,
922 struct got_tree_object *tree, const char *path, const char *entry_name,
923 struct got_repository *repo,
924 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
926 struct got_fileindex_entry *ie;
927 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
928 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
929 ie = walk_fileindex(fileindex, ie);
930 return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
931 cb, cb_arg);
934 static const struct got_error *
935 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
936 struct got_pathlist_head *, int, const char *, const char *,
937 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
939 static const struct got_error *
940 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
942 const struct got_error *err = NULL;
943 struct got_pathlist_entry *new = NULL;
944 struct dirent *dep = NULL;
945 struct dirent *de = NULL;
947 for (;;) {
948 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
949 if (de == NULL) {
950 err = got_error_from_errno("malloc");
951 break;
954 if (readdir_r(dir, de, &dep) != 0) {
955 err = got_error_from_errno("readdir_r");
956 free(de);
957 break;
959 if (dep == NULL) {
960 free(de);
961 break;
964 if (strcmp(de->d_name, ".") == 0 ||
965 strcmp(de->d_name, "..") == 0 ||
966 (path[0] == '\0' &&
967 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
968 free(de);
969 continue;
972 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
973 if (err) {
974 free(de);
975 break;
977 if (new == NULL) {
978 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
979 free(de);
980 break;
984 return err;
987 static int
988 have_tracked_file_in_dir(struct got_fileindex *fileindex, const char *path)
990 struct got_fileindex_entry *ie;
991 size_t path_len = strlen(path);
992 int cmp;
994 ie = RB_ROOT(&fileindex->entries);
995 while (ie) {
996 if (got_path_is_child(ie->path, path, path_len))
997 return 1;
998 cmp = got_path_cmp(path, ie->path, path_len,
999 got_fileindex_entry_path_len(ie));
1000 if (cmp < 0)
1001 ie = RB_LEFT(ie, entry);
1002 else if (cmp > 0)
1003 ie = RB_RIGHT(ie, entry);
1004 else
1005 break;
1008 return 0;
1011 static const struct got_error *
1012 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
1013 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
1014 const char *path, const char *rootpath, struct got_repository *repo,
1015 int ignore, struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1017 const struct got_error *err = NULL;
1018 struct dirent *de = dle->data;
1019 DIR *subdir = NULL;
1020 int subdirfd = -1;
1022 *next = NULL;
1024 /* Must traverse ignored directories if they contain tracked files. */
1025 if (de->d_type == DT_DIR && ignore &&
1026 have_tracked_file_in_dir(fileindex, path))
1027 ignore = 0;
1029 if (de->d_type == DT_DIR && !ignore) {
1030 char *subpath;
1031 char *subdirpath;
1032 struct got_pathlist_head subdirlist;
1034 TAILQ_INIT(&subdirlist);
1036 if (asprintf(&subpath, "%s%s%s", path,
1037 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1038 return got_error_from_errno("asprintf");
1040 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
1041 free(subpath);
1042 return got_error_from_errno("asprintf");
1045 subdirfd = openat(fd, de->d_name,
1046 O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
1047 if (subdirfd == -1) {
1048 if (errno == EACCES) {
1049 *next = TAILQ_NEXT(dle, entry);
1050 return NULL;
1052 err = got_error_from_errno2("openat", subdirpath);
1053 free(subpath);
1054 free(subdirpath);
1055 return err;
1058 subdir = fdopendir(subdirfd);
1059 if (subdir == NULL)
1060 return got_error_from_errno2("fdopendir", path);
1061 subdirfd = -1;
1062 err = read_dirlist(&subdirlist, subdir, subdirpath);
1063 if (err) {
1064 free(subpath);
1065 free(subdirpath);
1066 closedir(subdir);
1067 return err;
1069 err = diff_fileindex_dir(fileindex, ie, &subdirlist,
1070 dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
1071 if (subdir && closedir(subdir) == -1 && err == NULL)
1072 err = got_error_from_errno2("closedir", subdirpath);
1073 free(subpath);
1074 free(subdirpath);
1075 got_pathlist_free(&subdirlist, GOT_PATHLIST_FREE_DATA);
1076 if (err)
1077 return err;
1080 *next = TAILQ_NEXT(dle, entry);
1081 return NULL;
1084 static const struct got_error *
1085 dirent_type_fixup(struct dirent *de, const char *rootpath, const char *path)
1087 const struct got_error *err;
1088 char *dir_path;
1089 int type;
1091 if (de->d_type != DT_UNKNOWN)
1092 return NULL;
1094 /* DT_UNKNOWN occurs on NFS mounts without "readdir plus" RPC. */
1095 if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
1096 return got_error_from_errno("asprintf");
1097 err = got_path_dirent_type(&type, dir_path, de);
1098 free(dir_path);
1099 if (err)
1100 return err;
1102 de->d_type = type;
1103 return NULL;
1106 static const struct got_error *
1107 diff_fileindex_dir(struct got_fileindex *fileindex,
1108 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1109 int dirfd, const char *rootpath, const char *path,
1110 struct got_repository *repo,
1111 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1113 const struct got_error *err = NULL;
1114 struct dirent *de = NULL;
1115 size_t path_len = strlen(path);
1116 struct got_pathlist_entry *dle;
1117 int ignore;
1119 if (cb->diff_traverse) {
1120 err = cb->diff_traverse(cb_arg, path, dirfd);
1121 if (err)
1122 return err;
1125 dle = TAILQ_FIRST(dirlist);
1126 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1127 if (dle && *ie) {
1128 char *de_path;
1129 int cmp;
1130 de = dle->data;
1131 err = dirent_type_fixup(de, rootpath, path);
1132 if (err)
1133 break;
1134 if (asprintf(&de_path, "%s/%s", path,
1135 de->d_name) == -1) {
1136 err = got_error_from_errno("asprintf");
1137 break;
1139 cmp = got_path_cmp((*ie)->path, de_path,
1140 got_fileindex_entry_path_len(*ie),
1141 strlen(path) + 1 + de->d_namlen);
1142 free(de_path);
1143 if (cmp == 0) {
1144 err = cb->diff_old_new(cb_arg, *ie, de, path,
1145 dirfd);
1146 if (err)
1147 break;
1148 *ie = walk_fileindex(fileindex, *ie);
1149 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1150 path, rootpath, repo, 0, cb, cb_arg);
1151 } else if (cmp < 0 ) {
1152 err = cb->diff_old(cb_arg, *ie, path);
1153 if (err)
1154 break;
1155 *ie = walk_fileindex(fileindex, *ie);
1156 } else {
1157 err = cb->diff_new(&ignore, cb_arg, de, path,
1158 dirfd);
1159 if (err)
1160 break;
1161 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1162 path, rootpath, repo, ignore, cb, cb_arg);
1164 if (err)
1165 break;
1166 } else if (*ie) {
1167 err = cb->diff_old(cb_arg, *ie, path);
1168 if (err)
1169 break;
1170 *ie = walk_fileindex(fileindex, *ie);
1171 } else if (dle) {
1172 de = dle->data;
1173 err = dirent_type_fixup(de, rootpath, path);
1174 if (err)
1175 break;
1176 err = cb->diff_new(&ignore, cb_arg, de, path, dirfd);
1177 if (err)
1178 break;
1179 err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1180 rootpath, repo, ignore, cb, cb_arg);
1181 if (err)
1182 break;
1186 return err;
1189 const struct got_error *
1190 got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1191 const char *rootpath, const char *path, struct got_repository *repo,
1192 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1194 const struct got_error *err;
1195 struct got_fileindex_entry *ie;
1196 struct got_pathlist_head dirlist;
1197 int fd2;
1198 DIR *dir;
1200 TAILQ_INIT(&dirlist);
1203 * Duplicate the file descriptor so we can call closedir() below
1204 * without closing the file descriptor passed in by our caller.
1206 fd2 = dup(fd);
1207 if (fd2 == -1)
1208 return got_error_from_errno2("dup", path);
1209 if (lseek(fd2, 0, SEEK_SET) == -1) {
1210 err = got_error_from_errno2("lseek", path);
1211 close(fd2);
1212 return err;
1214 dir = fdopendir(fd2);
1215 if (dir == NULL) {
1216 err = got_error_from_errno2("fdopendir", path);
1217 close(fd2);
1218 return err;
1220 err = read_dirlist(&dirlist, dir, path);
1221 if (err) {
1222 closedir(dir);
1223 return err;
1226 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1227 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1228 ie = walk_fileindex(fileindex, ie);
1229 err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1230 rootpath, path, repo, cb, cb_arg);
1232 if (closedir(dir) == -1 && err == NULL)
1233 err = got_error_from_errno2("closedir", path);
1234 got_pathlist_free(&dirlist, GOT_PATHLIST_FREE_DATA);
1235 return err;
1238 struct got_object_id *
1239 got_fileindex_entry_get_staged_blob_id(struct got_object_id *id,
1240 struct got_fileindex_entry *ie)
1242 memset(id, 0, sizeof(*id));
1243 memcpy(id->sha1, ie->staged_blob_sha1, sizeof(ie->staged_blob_sha1));
1244 return id;
1247 struct got_object_id *
1248 got_fileindex_entry_get_blob_id(struct got_object_id *id,
1249 struct got_fileindex_entry *ie)
1251 memset(id, 0, sizeof(*id));
1252 memcpy(id->sha1, ie->blob_sha1, sizeof(ie->blob_sha1));
1253 return id;
1256 struct got_object_id *
1257 got_fileindex_entry_get_commit_id(struct got_object_id *id,
1258 struct got_fileindex_entry *ie)
1260 memset(id, 0, sizeof(*id));
1261 memcpy(id->sha1, ie->commit_sha1, sizeof(ie->commit_sha1));
1262 return id;
1265 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);