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/stat.h>
19 #include <errno.h>
20 #include <dirent.h>
21 #include <fcntl.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <limits.h>
26 #include <unistd.h>
27 #include <uuid.h>
29 #include "got_compat.h"
31 #include "got_error.h"
32 #include "got_object.h"
33 #include "got_path.h"
35 #include "got_lib_fileindex.h"
36 #include "got_lib_worktree.h"
38 /* got_fileindex_entry flags */
39 #define GOT_FILEIDX_F_PATH_LEN 0x00000fff
40 #define GOT_FILEIDX_F_STAGE 0x0000f000
41 #define GOT_FILEIDX_F_STAGE_SHIFT 12
42 #define GOT_FILEIDX_F_NOT_FLUSHED 0x00010000
43 #define GOT_FILEIDX_F_NO_BLOB 0x00020000
44 #define GOT_FILEIDX_F_NO_COMMIT 0x00040000
45 #define GOT_FILEIDX_F_NO_FILE_ON_DISK 0x00080000
46 #define GOT_FILEIDX_F_REMOVE_ON_FLUSH 0x00100000
47 #define GOT_FILEIDX_F_SKIPPED 0x00200000
49 struct got_fileindex {
50 struct got_fileindex_tree entries;
51 int nentries; /* Does not include entries marked for removal. */
52 #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
53 };
55 mode_t
56 got_fileindex_entry_perms_get(struct got_fileindex_entry *ie)
57 {
58 return ((ie->mode & GOT_FILEIDX_MODE_PERMS) >>
59 GOT_FILEIDX_MODE_PERMS_SHIFT);
60 }
62 static void
63 fileindex_entry_perms_set(struct got_fileindex_entry *ie, mode_t mode)
64 {
65 ie->mode &= ~GOT_FILEIDX_MODE_PERMS;
66 ie->mode |= ((mode << GOT_FILEIDX_MODE_PERMS_SHIFT) &
67 GOT_FILEIDX_MODE_PERMS);
68 }
70 mode_t
71 got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
72 {
73 mode_t perms = got_fileindex_entry_perms_get(ie);
74 int type = got_fileindex_entry_filetype_get(ie);
75 uint32_t ftype;
77 if (type == GOT_FILEIDX_MODE_REGULAR_FILE ||
78 type == GOT_FILEIDX_MODE_BAD_SYMLINK)
79 ftype = S_IFREG;
80 else
81 ftype = S_IFLNK;
83 return (ftype | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
84 }
86 const struct got_error *
87 got_fileindex_entry_update(struct got_fileindex_entry *ie,
88 int wt_fd, const char *ondisk_path, uint8_t *blob_sha1,
89 uint8_t *commit_sha1, int update_timestamps)
90 {
91 struct stat sb;
93 if (fstatat(wt_fd, ondisk_path, &sb, AT_SYMLINK_NOFOLLOW) != 0) {
94 if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
95 errno == ENOENT))
96 return got_error_from_errno2("fstatat", ondisk_path);
97 sb.st_mode = GOT_DEFAULT_FILE_MODE;
98 } else {
99 if (sb.st_mode & S_IFDIR)
100 return got_error_set_errno(EISDIR, ondisk_path);
101 ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
105 if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
106 if (update_timestamps) {
107 ie->ctime_sec = sb.st_ctim.tv_sec;
108 ie->ctime_nsec = sb.st_ctim.tv_nsec;
109 ie->mtime_sec = sb.st_mtim.tv_sec;
110 ie->mtime_nsec = sb.st_mtim.tv_nsec;
112 ie->uid = sb.st_uid;
113 ie->gid = sb.st_gid;
114 ie->size = (sb.st_size & 0xffffffff);
115 if (S_ISLNK(sb.st_mode)) {
116 got_fileindex_entry_filetype_set(ie,
117 GOT_FILEIDX_MODE_SYMLINK);
118 fileindex_entry_perms_set(ie, 0);
119 } else {
120 got_fileindex_entry_filetype_set(ie,
121 GOT_FILEIDX_MODE_REGULAR_FILE);
122 fileindex_entry_perms_set(ie,
123 sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
127 if (blob_sha1) {
128 memcpy(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
129 ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
130 } else
131 ie->flags |= GOT_FILEIDX_F_NO_BLOB;
133 if (commit_sha1) {
134 memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
135 ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
136 } else
137 ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
139 return NULL;
142 void
143 got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
145 ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
148 void
149 got_fileindex_entry_mark_skipped(struct got_fileindex_entry *ie)
151 ie->flags |= GOT_FILEIDX_F_SKIPPED;
154 const struct got_error *
155 got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
156 const char *relpath)
158 size_t len;
160 *ie = calloc(1, sizeof(**ie));
161 if (*ie == NULL)
162 return got_error_from_errno("calloc");
164 (*ie)->path = strdup(relpath);
165 if ((*ie)->path == NULL) {
166 const struct got_error *err = got_error_from_errno("strdup");
167 free(*ie);
168 *ie = NULL;
169 return err;
172 len = strlen(relpath);
173 if (len > GOT_FILEIDX_F_PATH_LEN)
174 len = GOT_FILEIDX_F_PATH_LEN;
175 (*ie)->flags |= len;
177 return NULL;
180 void
181 got_fileindex_entry_free(struct got_fileindex_entry *ie)
183 free(ie->path);
184 free(ie);
187 size_t
188 got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
190 return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
193 uint32_t
194 got_fileindex_entry_stage_get(const struct got_fileindex_entry *ie)
196 return ((ie->flags & GOT_FILEIDX_F_STAGE) >> GOT_FILEIDX_F_STAGE_SHIFT);
199 void
200 got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage)
202 ie->flags &= ~GOT_FILEIDX_F_STAGE;
203 ie->flags |= ((stage << GOT_FILEIDX_F_STAGE_SHIFT) &
204 GOT_FILEIDX_F_STAGE);
207 int
208 got_fileindex_entry_filetype_get(struct got_fileindex_entry *ie)
210 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
213 void
214 got_fileindex_entry_filetype_set(struct got_fileindex_entry *ie, int type)
216 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_ONDISK;
217 ie->mode |= (type & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
220 void
221 got_fileindex_entry_staged_filetype_set(struct got_fileindex_entry *ie, int type)
223 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_STAGED;
224 ie->mode |= ((type << GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT) &
225 GOT_FILEIDX_MODE_FILE_TYPE_STAGED);
228 int
229 got_fileindex_entry_staged_filetype_get(struct got_fileindex_entry *ie)
231 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_STAGED) >>
232 GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT;
235 int
236 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
238 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
241 int
242 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
244 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
247 int
248 got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
250 return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
253 int
254 got_fileindex_entry_was_skipped(struct got_fileindex_entry *ie)
256 return (ie->flags & GOT_FILEIDX_F_SKIPPED) != 0;
259 static const struct got_error *
260 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
262 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
263 return got_error(GOT_ERR_NO_SPACE);
265 RB_INSERT(got_fileindex_tree, &fileindex->entries, ie);
266 fileindex->nentries++;
267 return NULL;
270 const struct got_error *
271 got_fileindex_entry_add(struct got_fileindex *fileindex,
272 struct got_fileindex_entry *ie)
274 /* Flag this entry until it gets written out to disk. */
275 ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
277 return add_entry(fileindex, ie);
280 void
281 got_fileindex_entry_remove(struct got_fileindex *fileindex,
282 struct got_fileindex_entry *ie)
284 /*
285 * Removing an entry from the RB tree immediately breaks
286 * in-progress iterations over file index entries.
287 * So flag this entry for removal and remove it once the index
288 * is written out to disk. Meanwhile, pretend this entry no longer
289 * exists if we get queried for it again before then.
290 */
291 ie->flags |= GOT_FILEIDX_F_REMOVE_ON_FLUSH;
292 fileindex->nentries--;
295 struct got_fileindex_entry *
296 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
297 size_t path_len)
299 struct got_fileindex_entry *ie;
300 struct got_fileindex_entry key;
301 memset(&key, 0, sizeof(key));
302 key.path = (char *)path;
303 key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
304 ie = RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
305 if (ie && (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH))
306 return NULL;
307 return ie;
310 const struct got_error *
311 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
312 got_fileindex_cb cb, void *cb_arg)
314 const struct got_error *err;
315 struct got_fileindex_entry *ie, *tmp;
317 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
318 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
319 continue;
320 err = (*cb)(cb_arg, ie);
321 if (err)
322 return err;
324 return NULL;
327 struct got_fileindex *
328 got_fileindex_alloc(void)
330 struct got_fileindex *fileindex;
332 fileindex = calloc(1, sizeof(*fileindex));
333 if (fileindex == NULL)
334 return NULL;
336 RB_INIT(&fileindex->entries);
337 return fileindex;
340 void
341 got_fileindex_free(struct got_fileindex *fileindex)
343 struct got_fileindex_entry *ie;
345 while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
346 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
347 got_fileindex_entry_free(ie);
349 free(fileindex);
352 static const struct got_error *
353 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
355 size_t n;
357 val = htobe64(val);
358 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
359 n = fwrite(&val, 1, sizeof(val), outfile);
360 if (n != sizeof(val))
361 return got_ferror(outfile, GOT_ERR_IO);
362 return NULL;
365 static const struct got_error *
366 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
368 size_t n;
370 val = htobe32(val);
371 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
372 n = fwrite(&val, 1, sizeof(val), outfile);
373 if (n != sizeof(val))
374 return got_ferror(outfile, GOT_ERR_IO);
375 return NULL;
378 static const struct got_error *
379 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
381 size_t n;
383 val = htobe16(val);
384 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
385 n = fwrite(&val, 1, sizeof(val), outfile);
386 if (n != sizeof(val))
387 return got_ferror(outfile, GOT_ERR_IO);
388 return NULL;
391 static const struct got_error *
392 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
394 size_t n, len, pad = 0;
395 static const uint8_t zero[8] = { 0 };
397 len = strlen(path);
398 while ((len + pad) % 8 != 0)
399 pad++;
400 if (pad == 0)
401 pad = 8; /* NUL-terminate */
403 SHA1Update(ctx, path, len);
404 n = fwrite(path, 1, len, outfile);
405 if (n != len)
406 return got_ferror(outfile, GOT_ERR_IO);
407 SHA1Update(ctx, zero, pad);
408 n = fwrite(zero, 1, pad, outfile);
409 if (n != pad)
410 return got_ferror(outfile, GOT_ERR_IO);
411 return NULL;
414 static const struct got_error *
415 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
416 FILE *outfile)
418 const struct got_error *err;
419 size_t n;
420 uint32_t stage;
422 err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
423 if (err)
424 return err;
425 err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
426 if (err)
427 return err;
428 err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
429 if (err)
430 return err;
431 err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
432 if (err)
433 return err;
435 err = write_fileindex_val32(ctx, ie->uid, outfile);
436 if (err)
437 return err;
438 err = write_fileindex_val32(ctx, ie->gid, outfile);
439 if (err)
440 return err;
441 err = write_fileindex_val32(ctx, ie->size, outfile);
442 if (err)
443 return err;
445 err = write_fileindex_val16(ctx, ie->mode, outfile);
446 if (err)
447 return err;
449 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
450 n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
451 if (n != SHA1_DIGEST_LENGTH)
452 return got_ferror(outfile, GOT_ERR_IO);
454 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
455 n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
456 if (n != SHA1_DIGEST_LENGTH)
457 return got_ferror(outfile, GOT_ERR_IO);
459 err = write_fileindex_val32(ctx, ie->flags, outfile);
460 if (err)
461 return err;
463 err = write_fileindex_path(ctx, ie->path, outfile);
464 if (err)
465 return err;
467 stage = got_fileindex_entry_stage_get(ie);
468 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
469 stage == GOT_FILEIDX_STAGE_ADD) {
470 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
471 n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
472 outfile);
473 if (n != SHA1_DIGEST_LENGTH)
474 return got_ferror(outfile, GOT_ERR_IO);
477 return NULL;
480 const struct got_error *
481 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
483 const struct got_error *err = NULL;
484 struct got_fileindex_hdr hdr;
485 SHA1_CTX ctx;
486 uint8_t sha1[SHA1_DIGEST_LENGTH];
487 size_t n;
488 struct got_fileindex_entry *ie, *tmp;
490 SHA1Init(&ctx);
492 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
493 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
494 hdr.nentries = htobe32(fileindex->nentries);
496 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
497 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
498 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
499 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
500 if (n != sizeof(hdr.signature))
501 return got_ferror(outfile, GOT_ERR_IO);
502 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
503 if (n != sizeof(hdr.version))
504 return got_ferror(outfile, GOT_ERR_IO);
505 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
506 if (n != sizeof(hdr.nentries))
507 return got_ferror(outfile, GOT_ERR_IO);
509 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
510 ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
511 ie->flags &= ~GOT_FILEIDX_F_SKIPPED;
512 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH) {
513 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
514 got_fileindex_entry_free(ie);
515 continue;
517 err = write_fileindex_entry(&ctx, ie, outfile);
518 if (err)
519 return err;
522 SHA1Final(sha1, &ctx);
523 n = fwrite(sha1, 1, sizeof(sha1), outfile);
524 if (n != sizeof(sha1))
525 return got_ferror(outfile, GOT_ERR_IO);
527 if (fflush(outfile) != 0)
528 return got_error_from_errno("fflush");
530 return NULL;
533 static const struct got_error *
534 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
536 size_t n;
538 n = fread(val, 1, sizeof(*val), infile);
539 if (n != sizeof(*val))
540 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
541 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
542 *val = be64toh(*val);
543 return NULL;
546 static const struct got_error *
547 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
549 size_t n;
551 n = fread(val, 1, sizeof(*val), infile);
552 if (n != sizeof(*val))
553 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
554 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
555 *val = be32toh(*val);
556 return NULL;
559 static const struct got_error *
560 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
562 size_t n;
564 n = fread(val, 1, sizeof(*val), infile);
565 if (n != sizeof(*val))
566 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
567 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
568 *val = be16toh(*val);
569 return NULL;
572 static const struct got_error *
573 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
575 const struct got_error *err = NULL;
576 const size_t chunk_size = 8;
577 size_t n, len = 0, totlen = chunk_size;
579 *path = malloc(totlen);
580 if (*path == NULL)
581 return got_error_from_errno("malloc");
583 do {
584 if (len + chunk_size > totlen) {
585 char *p = reallocarray(*path, totlen + chunk_size, 1);
586 if (p == NULL) {
587 err = got_error_from_errno("reallocarray");
588 break;
590 totlen += chunk_size;
591 *path = p;
593 n = fread(*path + len, 1, chunk_size, infile);
594 if (n != chunk_size) {
595 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
596 break;
598 SHA1Update(ctx, *path + len, chunk_size);
599 len += chunk_size;
600 } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
602 if (err) {
603 free(*path);
604 *path = NULL;
606 return err;
609 static const struct got_error *
610 read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
611 FILE *infile, uint32_t version)
613 const struct got_error *err;
614 struct got_fileindex_entry *ie;
615 size_t n;
617 *iep = NULL;
619 ie = calloc(1, sizeof(*ie));
620 if (ie == NULL)
621 return got_error_from_errno("calloc");
623 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
624 if (err)
625 goto done;
626 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
627 if (err)
628 goto done;
629 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
630 if (err)
631 goto done;
632 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
633 if (err)
634 goto done;
636 err = read_fileindex_val32(&ie->uid, ctx, infile);
637 if (err)
638 goto done;
639 err = read_fileindex_val32(&ie->gid, ctx, infile);
640 if (err)
641 goto done;
642 err = read_fileindex_val32(&ie->size, ctx, infile);
643 if (err)
644 goto done;
646 err = read_fileindex_val16(&ie->mode, ctx, infile);
647 if (err)
648 goto done;
650 n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
651 if (n != SHA1_DIGEST_LENGTH) {
652 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
653 goto done;
655 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
657 n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
658 if (n != SHA1_DIGEST_LENGTH) {
659 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
660 goto done;
662 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
664 err = read_fileindex_val32(&ie->flags, ctx, infile);
665 if (err)
666 goto done;
668 err = read_fileindex_path(&ie->path, ctx, infile);
669 if (err)
670 goto done;
672 if (version >= 2) {
673 uint32_t stage = got_fileindex_entry_stage_get(ie);
674 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
675 stage == GOT_FILEIDX_STAGE_ADD) {
676 n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
677 infile);
678 if (n != SHA1_DIGEST_LENGTH) {
679 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
680 goto done;
682 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
684 } else {
685 /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
686 ie->flags &= ~GOT_FILEIDX_F_STAGE;
689 done:
690 if (err)
691 got_fileindex_entry_free(ie);
692 else
693 *iep = ie;
694 return err;
697 const struct got_error *
698 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
700 const struct got_error *err = NULL;
701 struct got_fileindex_hdr hdr;
702 SHA1_CTX ctx;
703 struct got_fileindex_entry *ie;
704 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
705 uint8_t sha1[SHA1_DIGEST_LENGTH];
706 size_t n;
707 int i;
709 SHA1Init(&ctx);
711 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
712 if (n != sizeof(hdr.signature)) {
713 if (n == 0) /* EOF */
714 return NULL;
715 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
717 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
718 if (n != sizeof(hdr.version)) {
719 if (n == 0) /* EOF */
720 return NULL;
721 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
723 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
724 if (n != sizeof(hdr.nentries)) {
725 if (n == 0) /* EOF */
726 return NULL;
727 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
730 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
731 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
732 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
734 hdr.signature = be32toh(hdr.signature);
735 hdr.version = be32toh(hdr.version);
736 hdr.nentries = be32toh(hdr.nentries);
738 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
739 return got_error(GOT_ERR_FILEIDX_SIG);
740 if (hdr.version > GOT_FILE_INDEX_VERSION)
741 return got_error(GOT_ERR_FILEIDX_VER);
743 for (i = 0; i < hdr.nentries; i++) {
744 err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
745 if (err)
746 return err;
747 err = add_entry(fileindex, ie);
748 if (err)
749 return err;
752 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
753 if (n != sizeof(sha1_expected))
754 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
755 SHA1Final(sha1, &ctx);
756 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
757 return got_error(GOT_ERR_FILEIDX_CSUM);
759 return NULL;
762 static struct got_fileindex_entry *
763 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
765 struct got_fileindex_entry *next;
767 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
769 /* Skip entries which were added or removed by diff callbacks. */
770 while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
771 GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
772 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
774 return next;
777 static const struct got_error *
778 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
779 struct got_tree_object *tree, const char *, const char *,
780 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
782 static const struct got_error *
783 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
784 struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
785 const char *path, const char *entry_name, struct got_repository *repo,
786 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
788 const struct got_error *err = NULL;
789 struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
791 if (!got_object_tree_entry_is_submodule(te) &&
792 S_ISDIR(got_tree_entry_get_mode(te))) {
793 char *subpath;
794 struct got_tree_object *subtree;
796 if (asprintf(&subpath, "%s%s%s", path,
797 path[0] == '\0' ? "" : "/",
798 got_tree_entry_get_name(te)) == -1)
799 return got_error_from_errno("asprintf");
801 err = got_object_open_as_tree(&subtree, repo,
802 got_tree_entry_get_id(te));
803 if (err) {
804 free(subpath);
805 return err;
808 err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
809 entry_name, repo, cb, cb_arg);
810 free(subpath);
811 got_object_tree_close(subtree);
812 if (err)
813 return err;
816 (*tidx)++;
817 *next = got_object_tree_get_entry(tree, *tidx);
818 return NULL;
821 static const struct got_error *
822 diff_fileindex_tree(struct got_fileindex *fileindex,
823 struct got_fileindex_entry **ie, struct got_tree_object *tree,
824 const char *path, const char *entry_name, struct got_repository *repo,
825 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
827 const struct got_error *err = NULL;
828 struct got_tree_entry *te = NULL;
829 size_t path_len = strlen(path);
830 struct got_fileindex_entry *next;
831 int tidx = 0;
833 te = got_object_tree_get_entry(tree, tidx);
834 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
835 if (te && *ie) {
836 char *te_path;
837 const char *te_name = got_tree_entry_get_name(te);
838 int cmp;
839 if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
840 err = got_error_from_errno("asprintf");
841 break;
843 cmp = got_path_cmp((*ie)->path, te_path,
844 got_fileindex_entry_path_len(*ie), strlen(te_path));
845 free(te_path);
846 if (cmp == 0) {
847 if (got_path_is_child((*ie)->path, path,
848 path_len) &&
849 !got_object_tree_entry_is_submodule(te) &&
850 (entry_name == NULL ||
851 strcmp(te_name, entry_name) == 0)) {
852 err = cb->diff_old_new(cb_arg, *ie, te,
853 path);
854 if (err || entry_name)
855 break;
857 *ie = walk_fileindex(fileindex, *ie);
858 err = walk_tree(&te, fileindex, ie, tree, &tidx,
859 path, entry_name, repo, cb, cb_arg);
860 } else if (cmp < 0) {
861 next = walk_fileindex(fileindex, *ie);
862 if (got_path_is_child((*ie)->path, path,
863 path_len) && entry_name == NULL) {
864 err = cb->diff_old(cb_arg, *ie, path);
865 if (err || entry_name)
866 break;
868 *ie = next;
869 } else {
870 if ((entry_name == NULL ||
871 strcmp(te_name, entry_name) == 0)) {
872 err = cb->diff_new(cb_arg, te, path);
873 if (err || entry_name)
874 break;
876 err = walk_tree(&te, fileindex, ie, tree, &tidx,
877 path, entry_name, repo, cb, cb_arg);
879 if (err)
880 break;
881 } else if (*ie) {
882 next = walk_fileindex(fileindex, *ie);
883 if (got_path_is_child((*ie)->path, path, path_len) &&
884 (entry_name == NULL ||
885 (te && strcmp(got_tree_entry_get_name(te),
886 entry_name) == 0))) {
887 err = cb->diff_old(cb_arg, *ie, path);
888 if (err || entry_name)
889 break;
891 *ie = next;
892 } else if (te) {
893 if (!got_object_tree_entry_is_submodule(te) &&
894 (entry_name == NULL ||
895 strcmp(got_tree_entry_get_name(te), entry_name)
896 == 0)) {
897 err = cb->diff_new(cb_arg, te, path);
898 if (err || entry_name)
899 break;
901 err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
902 entry_name, repo, cb, cb_arg);
903 if (err)
904 break;
908 return err;
911 const struct got_error *
912 got_fileindex_diff_tree(struct got_fileindex *fileindex,
913 struct got_tree_object *tree, const char *path, const char *entry_name,
914 struct got_repository *repo,
915 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
917 struct got_fileindex_entry *ie;
918 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
919 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
920 ie = walk_fileindex(fileindex, ie);
921 return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
922 cb, cb_arg);
925 static const struct got_error *
926 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
927 struct got_pathlist_head *, int, const char *, const char *,
928 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
930 static const struct got_error *
931 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
933 const struct got_error *err = NULL;
934 struct got_pathlist_entry *new = NULL;
935 struct dirent *dep = NULL;
936 struct dirent *de = NULL;
938 for (;;) {
939 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
940 if (de == NULL) {
941 err = got_error_from_errno("malloc");
942 break;
945 if (readdir_r(dir, de, &dep) != 0) {
946 err = got_error_from_errno("readdir_r");
947 free(de);
948 break;
950 if (dep == NULL) {
951 free(de);
952 break;
955 if (strcmp(de->d_name, ".") == 0 ||
956 strcmp(de->d_name, "..") == 0 ||
957 (path[0] == '\0' &&
958 strcmp(de->d_name, GOT_WORKTREE_GOT_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 void
979 free_dirlist(struct got_pathlist_head *dirlist)
981 struct got_pathlist_entry *dle;
983 TAILQ_FOREACH(dle, dirlist, entry)
984 free(dle->data);
985 got_pathlist_free(dirlist);
988 static int
989 have_tracked_file_in_dir(struct got_fileindex *fileindex, const char *path)
991 struct got_fileindex_entry *ie;
992 size_t path_len = strlen(path);
993 int cmp;
995 ie = RB_ROOT(&fileindex->entries);
996 while (ie) {
997 if (got_path_is_child(ie->path, path, path_len))
998 return 1;
999 cmp = got_path_cmp(path, ie->path, path_len,
1000 got_fileindex_entry_path_len(ie));
1001 if (cmp < 0)
1002 ie = RB_LEFT(ie, entry);
1003 else if (cmp > 0)
1004 ie = RB_RIGHT(ie, entry);
1005 else
1006 break;
1009 return 0;
1012 static const struct got_error *
1013 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
1014 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
1015 const char *path, const char *rootpath, struct got_repository *repo,
1016 int ignore, struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1018 const struct got_error *err = NULL;
1019 struct dirent *de = dle->data;
1020 DIR *subdir = NULL;
1021 int subdirfd = -1;
1022 int type;
1024 *next = NULL;
1026 if (de->d_type == DT_UNKNOWN) {
1027 /* Occurs on NFS mounts without "readdir plus" RPC. */
1028 char *dir_path;
1029 if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
1030 return got_error_from_errno("asprintf");
1031 err = got_path_dirent_type(&type, dir_path, de);
1032 free(dir_path);
1033 if (err)
1034 return err;
1035 } else
1036 type = de->d_type;
1038 /* Must traverse ignored directories if they contain tracked files. */
1039 if (type == DT_DIR && ignore &&
1040 have_tracked_file_in_dir(fileindex, path))
1041 ignore = 0;
1043 if (type == DT_DIR && !ignore) {
1044 char *subpath;
1045 char *subdirpath;
1046 struct got_pathlist_head subdirlist;
1048 TAILQ_INIT(&subdirlist);
1050 if (asprintf(&subpath, "%s%s%s", path,
1051 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1052 return got_error_from_errno("asprintf");
1054 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
1055 free(subpath);
1056 return got_error_from_errno("asprintf");
1059 subdirfd = openat(fd, de->d_name,
1060 O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
1061 if (subdirfd == -1) {
1062 if (errno == EACCES) {
1063 *next = TAILQ_NEXT(dle, entry);
1064 return NULL;
1066 err = got_error_from_errno2("openat", subdirpath);
1067 free(subpath);
1068 free(subdirpath);
1069 return err;
1072 subdir = fdopendir(subdirfd);
1073 if (subdir == NULL)
1074 return got_error_from_errno2("fdopendir", path);
1075 subdirfd = -1;
1076 err = read_dirlist(&subdirlist, subdir, subdirpath);
1077 if (err) {
1078 free(subpath);
1079 free(subdirpath);
1080 closedir(subdir);
1081 return err;
1083 err = diff_fileindex_dir(fileindex, ie, &subdirlist,
1084 dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
1085 if (subdir && closedir(subdir) == -1 && err == NULL)
1086 err = got_error_from_errno2("closedir", subdirpath);
1087 free(subpath);
1088 free(subdirpath);
1089 free_dirlist(&subdirlist);
1090 if (err)
1091 return err;
1094 *next = TAILQ_NEXT(dle, entry);
1095 return NULL;
1098 static const struct got_error *
1099 diff_fileindex_dir(struct got_fileindex *fileindex,
1100 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1101 int dirfd, const char *rootpath, const char *path,
1102 struct got_repository *repo,
1103 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1105 const struct got_error *err = NULL;
1106 struct dirent *de = NULL;
1107 size_t path_len = strlen(path);
1108 struct got_pathlist_entry *dle;
1109 int ignore;
1111 if (cb->diff_traverse) {
1112 err = cb->diff_traverse(cb_arg, path, dirfd);
1113 if (err)
1114 return err;
1117 dle = TAILQ_FIRST(dirlist);
1118 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1119 if (dle && *ie) {
1120 char *de_path;
1121 int cmp;
1122 de = dle->data;
1123 if (asprintf(&de_path, "%s/%s", path,
1124 de->d_name) == -1) {
1125 err = got_error_from_errno("asprintf");
1126 break;
1128 cmp = got_path_cmp((*ie)->path, de_path,
1129 got_fileindex_entry_path_len(*ie),
1130 strlen(path) + 1 + strlen(de->d_name));
1131 free(de_path);
1132 if (cmp == 0) {
1133 err = cb->diff_old_new(cb_arg, *ie, de, path,
1134 dirfd);
1135 if (err)
1136 break;
1137 *ie = walk_fileindex(fileindex, *ie);
1138 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1139 path, rootpath, repo, 0, cb, cb_arg);
1140 } else if (cmp < 0 ) {
1141 err = cb->diff_old(cb_arg, *ie, path);
1142 if (err)
1143 break;
1144 *ie = walk_fileindex(fileindex, *ie);
1145 } else {
1146 err = cb->diff_new(&ignore, cb_arg, de, path,
1147 dirfd);
1148 if (err)
1149 break;
1150 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1151 path, rootpath, repo, ignore, cb, cb_arg);
1153 if (err)
1154 break;
1155 } else if (*ie) {
1156 err = cb->diff_old(cb_arg, *ie, path);
1157 if (err)
1158 break;
1159 *ie = walk_fileindex(fileindex, *ie);
1160 } else if (dle) {
1161 de = dle->data;
1162 err = cb->diff_new(&ignore, cb_arg, de, path, dirfd);
1163 if (err)
1164 break;
1165 err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1166 rootpath, repo, ignore, cb, cb_arg);
1167 if (err)
1168 break;
1172 return err;
1175 const struct got_error *
1176 got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1177 const char *rootpath, const char *path, struct got_repository *repo,
1178 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1180 const struct got_error *err;
1181 struct got_fileindex_entry *ie;
1182 struct got_pathlist_head dirlist;
1183 int fd2;
1184 DIR *dir;
1186 TAILQ_INIT(&dirlist);
1189 * Duplicate the file descriptor so we can call closedir() below
1190 * without closing the file descriptor passed in by our caller.
1192 fd2 = dup(fd);
1193 if (fd2 == -1)
1194 return got_error_from_errno2("dup", path);
1195 if (lseek(fd2, 0, SEEK_SET) == -1) {
1196 err = got_error_from_errno2("lseek", path);
1197 close(fd2);
1198 return err;
1200 dir = fdopendir(fd2);
1201 if (dir == NULL) {
1202 err = got_error_from_errno2("fdopendir", path);
1203 close(fd2);
1204 return err;
1206 err = read_dirlist(&dirlist, dir, path);
1207 if (err) {
1208 closedir(dir);
1209 return err;
1212 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1213 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1214 ie = walk_fileindex(fileindex, ie);
1215 err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1216 rootpath, path, repo, cb, cb_arg);
1218 if (closedir(dir) == -1 && err == NULL)
1219 err = got_error_from_errno2("closedir", path);
1220 free_dirlist(&dirlist);
1221 return err;
1224 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);