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 <endian.h>
29 #include <limits.h>
30 #include <unistd.h>
31 #include <uuid.h>
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_path.h"
37 #include "got_lib_fileindex.h"
38 #include "got_lib_worktree.h"
40 /* got_fileindex_entry flags */
41 #define GOT_FILEIDX_F_PATH_LEN 0x00000fff
42 #define GOT_FILEIDX_F_STAGE 0x0000f000
43 #define GOT_FILEIDX_F_STAGE_SHIFT 12
44 #define GOT_FILEIDX_F_NOT_FLUSHED 0x00010000
45 #define GOT_FILEIDX_F_NO_BLOB 0x00020000
46 #define GOT_FILEIDX_F_NO_COMMIT 0x00040000
47 #define GOT_FILEIDX_F_NO_FILE_ON_DISK 0x00080000
48 #define GOT_FILEIDX_F_REMOVE_ON_FLUSH 0x00100000
49 #define GOT_FILEIDX_F_SKIPPED 0x00200000
51 struct got_fileindex {
52 struct got_fileindex_tree entries;
53 int nentries; /* Does not include entries marked for removal. */
54 #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
55 };
57 mode_t
58 got_fileindex_entry_perms_get(struct got_fileindex_entry *ie)
59 {
60 return ((ie->mode & GOT_FILEIDX_MODE_PERMS) >>
61 GOT_FILEIDX_MODE_PERMS_SHIFT);
62 }
64 static void
65 fileindex_entry_perms_set(struct got_fileindex_entry *ie, mode_t mode)
66 {
67 ie->mode &= ~GOT_FILEIDX_MODE_PERMS;
68 ie->mode |= ((mode << GOT_FILEIDX_MODE_PERMS_SHIFT) &
69 GOT_FILEIDX_MODE_PERMS);
70 }
72 mode_t
73 got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
74 {
75 mode_t perms = got_fileindex_entry_perms_get(ie);
76 int type = got_fileindex_entry_filetype_get(ie);
77 uint32_t ftype;
79 if (type == GOT_FILEIDX_MODE_REGULAR_FILE ||
80 type == GOT_FILEIDX_MODE_BAD_SYMLINK)
81 ftype = S_IFREG;
82 else
83 ftype = S_IFLNK;
85 return (ftype | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
86 }
88 const struct got_error *
89 got_fileindex_entry_update(struct got_fileindex_entry *ie,
90 int wt_fd, const char *ondisk_path, uint8_t *blob_sha1,
91 uint8_t *commit_sha1, int update_timestamps)
92 {
93 struct stat sb;
95 if (fstatat(wt_fd, ondisk_path, &sb, AT_SYMLINK_NOFOLLOW) != 0) {
96 if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
97 errno == ENOENT))
98 return got_error_from_errno2("fstatat", ondisk_path);
99 sb.st_mode = GOT_DEFAULT_FILE_MODE;
100 } else {
101 if (sb.st_mode & S_IFDIR)
102 return got_error_set_errno(EISDIR, ondisk_path);
103 ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
106 if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
107 if (update_timestamps) {
108 ie->ctime_sec = sb.st_ctim.tv_sec;
109 ie->ctime_nsec = sb.st_ctim.tv_nsec;
110 ie->mtime_sec = sb.st_mtim.tv_sec;
111 ie->mtime_nsec = sb.st_mtim.tv_nsec;
113 ie->uid = sb.st_uid;
114 ie->gid = sb.st_gid;
115 ie->size = (sb.st_size & 0xffffffff);
116 if (S_ISLNK(sb.st_mode)) {
117 got_fileindex_entry_filetype_set(ie,
118 GOT_FILEIDX_MODE_SYMLINK);
119 fileindex_entry_perms_set(ie, 0);
120 } else {
121 got_fileindex_entry_filetype_set(ie,
122 GOT_FILEIDX_MODE_REGULAR_FILE);
123 fileindex_entry_perms_set(ie,
124 sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
128 if (blob_sha1) {
129 memcpy(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
130 ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
131 } else
132 ie->flags |= GOT_FILEIDX_F_NO_BLOB;
134 if (commit_sha1) {
135 memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
136 ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
137 } else
138 ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
140 return NULL;
143 void
144 got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
146 ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
149 void
150 got_fileindex_entry_mark_skipped(struct got_fileindex_entry *ie)
152 ie->flags |= GOT_FILEIDX_F_SKIPPED;
155 const struct got_error *
156 got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
157 const char *relpath)
159 size_t len;
161 *ie = calloc(1, sizeof(**ie));
162 if (*ie == NULL)
163 return got_error_from_errno("calloc");
165 (*ie)->path = strdup(relpath);
166 if ((*ie)->path == NULL) {
167 const struct got_error *err = got_error_from_errno("strdup");
168 free(*ie);
169 *ie = NULL;
170 return err;
173 len = strlen(relpath);
174 if (len > GOT_FILEIDX_F_PATH_LEN)
175 len = GOT_FILEIDX_F_PATH_LEN;
176 (*ie)->flags |= len;
178 return NULL;
181 void
182 got_fileindex_entry_free(struct got_fileindex_entry *ie)
184 free(ie->path);
185 free(ie);
188 size_t
189 got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
191 return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
194 uint32_t
195 got_fileindex_entry_stage_get(const struct got_fileindex_entry *ie)
197 return ((ie->flags & GOT_FILEIDX_F_STAGE) >> GOT_FILEIDX_F_STAGE_SHIFT);
200 void
201 got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage)
203 ie->flags &= ~GOT_FILEIDX_F_STAGE;
204 ie->flags |= ((stage << GOT_FILEIDX_F_STAGE_SHIFT) &
205 GOT_FILEIDX_F_STAGE);
208 int
209 got_fileindex_entry_filetype_get(struct got_fileindex_entry *ie)
211 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
214 void
215 got_fileindex_entry_filetype_set(struct got_fileindex_entry *ie, int type)
217 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_ONDISK;
218 ie->mode |= (type & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
221 void
222 got_fileindex_entry_staged_filetype_set(struct got_fileindex_entry *ie,
223 int type)
225 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_STAGED;
226 ie->mode |= ((type << GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT) &
227 GOT_FILEIDX_MODE_FILE_TYPE_STAGED);
230 int
231 got_fileindex_entry_staged_filetype_get(struct got_fileindex_entry *ie)
233 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_STAGED) >>
234 GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT;
237 int
238 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
240 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
243 int
244 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
246 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
249 int
250 got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
252 return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
255 int
256 got_fileindex_entry_was_skipped(struct got_fileindex_entry *ie)
258 return (ie->flags & GOT_FILEIDX_F_SKIPPED) != 0;
261 static const struct got_error *
262 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
264 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
265 return got_error(GOT_ERR_NO_SPACE);
267 if (RB_INSERT(got_fileindex_tree, &fileindex->entries, ie) != NULL)
268 return got_error_path(ie->path, GOT_ERR_FILEIDX_DUP_ENTRY);
270 fileindex->nentries++;
271 return NULL;
274 const struct got_error *
275 got_fileindex_entry_add(struct got_fileindex *fileindex,
276 struct got_fileindex_entry *ie)
278 /* Flag this entry until it gets written out to disk. */
279 ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
281 return add_entry(fileindex, ie);
284 void
285 got_fileindex_entry_remove(struct got_fileindex *fileindex,
286 struct got_fileindex_entry *ie)
288 /*
289 * Removing an entry from the RB tree immediately breaks
290 * in-progress iterations over file index entries.
291 * So flag this entry for removal and remove it once the index
292 * is written out to disk. Meanwhile, pretend this entry no longer
293 * exists if we get queried for it again before then.
294 */
295 ie->flags |= GOT_FILEIDX_F_REMOVE_ON_FLUSH;
296 fileindex->nentries--;
299 struct got_fileindex_entry *
300 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
301 size_t path_len)
303 struct got_fileindex_entry *ie;
304 struct got_fileindex_entry key;
305 memset(&key, 0, sizeof(key));
306 key.path = (char *)path;
307 key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
308 ie = RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
309 if (ie && (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH))
310 return NULL;
311 return ie;
314 const struct got_error *
315 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
316 got_fileindex_cb cb, void *cb_arg)
318 const struct got_error *err;
319 struct got_fileindex_entry *ie, *tmp;
321 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
322 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
323 continue;
324 err = (*cb)(cb_arg, ie);
325 if (err)
326 return err;
328 return NULL;
331 struct got_fileindex *
332 got_fileindex_alloc(void)
334 struct got_fileindex *fileindex;
336 fileindex = calloc(1, sizeof(*fileindex));
337 if (fileindex == NULL)
338 return NULL;
340 RB_INIT(&fileindex->entries);
341 return fileindex;
344 void
345 got_fileindex_free(struct got_fileindex *fileindex)
347 struct got_fileindex_entry *ie;
349 while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
350 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
351 got_fileindex_entry_free(ie);
353 free(fileindex);
356 static const struct got_error *
357 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
359 size_t n;
361 val = htobe64(val);
362 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
363 n = fwrite(&val, 1, sizeof(val), outfile);
364 if (n != sizeof(val))
365 return got_ferror(outfile, GOT_ERR_IO);
366 return NULL;
369 static const struct got_error *
370 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
372 size_t n;
374 val = htobe32(val);
375 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
376 n = fwrite(&val, 1, sizeof(val), outfile);
377 if (n != sizeof(val))
378 return got_ferror(outfile, GOT_ERR_IO);
379 return NULL;
382 static const struct got_error *
383 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
385 size_t n;
387 val = htobe16(val);
388 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
389 n = fwrite(&val, 1, sizeof(val), outfile);
390 if (n != sizeof(val))
391 return got_ferror(outfile, GOT_ERR_IO);
392 return NULL;
395 static const struct got_error *
396 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
398 size_t n, len, pad = 0;
399 static const uint8_t zero[8] = { 0 };
401 len = strlen(path);
402 while ((len + pad) % 8 != 0)
403 pad++;
404 if (pad == 0)
405 pad = 8; /* NUL-terminate */
407 SHA1Update(ctx, path, len);
408 n = fwrite(path, 1, len, outfile);
409 if (n != len)
410 return got_ferror(outfile, GOT_ERR_IO);
411 SHA1Update(ctx, zero, pad);
412 n = fwrite(zero, 1, pad, outfile);
413 if (n != pad)
414 return got_ferror(outfile, GOT_ERR_IO);
415 return NULL;
418 static const struct got_error *
419 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
420 FILE *outfile)
422 const struct got_error *err;
423 size_t n;
424 uint32_t stage;
426 err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
427 if (err)
428 return err;
429 err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
430 if (err)
431 return err;
432 err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
433 if (err)
434 return err;
435 err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
436 if (err)
437 return err;
439 err = write_fileindex_val32(ctx, ie->uid, outfile);
440 if (err)
441 return err;
442 err = write_fileindex_val32(ctx, ie->gid, outfile);
443 if (err)
444 return err;
445 err = write_fileindex_val32(ctx, ie->size, outfile);
446 if (err)
447 return err;
449 err = write_fileindex_val16(ctx, ie->mode, outfile);
450 if (err)
451 return err;
453 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
454 n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
455 if (n != SHA1_DIGEST_LENGTH)
456 return got_ferror(outfile, GOT_ERR_IO);
458 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
459 n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
460 if (n != SHA1_DIGEST_LENGTH)
461 return got_ferror(outfile, GOT_ERR_IO);
463 err = write_fileindex_val32(ctx, ie->flags, outfile);
464 if (err)
465 return err;
467 err = write_fileindex_path(ctx, ie->path, outfile);
468 if (err)
469 return err;
471 stage = got_fileindex_entry_stage_get(ie);
472 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
473 stage == GOT_FILEIDX_STAGE_ADD) {
474 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
475 n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
476 outfile);
477 if (n != SHA1_DIGEST_LENGTH)
478 return got_ferror(outfile, GOT_ERR_IO);
481 return NULL;
484 const struct got_error *
485 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
487 const struct got_error *err = NULL;
488 struct got_fileindex_hdr hdr;
489 SHA1_CTX ctx;
490 uint8_t sha1[SHA1_DIGEST_LENGTH];
491 size_t n;
492 struct got_fileindex_entry *ie, *tmp;
494 SHA1Init(&ctx);
496 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
497 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
498 hdr.nentries = htobe32(fileindex->nentries);
500 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
501 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
502 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
503 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
504 if (n != sizeof(hdr.signature))
505 return got_ferror(outfile, GOT_ERR_IO);
506 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
507 if (n != sizeof(hdr.version))
508 return got_ferror(outfile, GOT_ERR_IO);
509 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
510 if (n != sizeof(hdr.nentries))
511 return got_ferror(outfile, GOT_ERR_IO);
513 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
514 ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
515 ie->flags &= ~GOT_FILEIDX_F_SKIPPED;
516 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH) {
517 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
518 got_fileindex_entry_free(ie);
519 continue;
521 err = write_fileindex_entry(&ctx, ie, outfile);
522 if (err)
523 return err;
526 SHA1Final(sha1, &ctx);
527 n = fwrite(sha1, 1, sizeof(sha1), outfile);
528 if (n != sizeof(sha1))
529 return got_ferror(outfile, GOT_ERR_IO);
531 if (fflush(outfile) != 0)
532 return got_error_from_errno("fflush");
534 return NULL;
537 static const struct got_error *
538 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
540 size_t n;
542 n = fread(val, 1, sizeof(*val), infile);
543 if (n != sizeof(*val))
544 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
545 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
546 *val = be64toh(*val);
547 return NULL;
550 static const struct got_error *
551 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
553 size_t n;
555 n = fread(val, 1, sizeof(*val), infile);
556 if (n != sizeof(*val))
557 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
558 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
559 *val = be32toh(*val);
560 return NULL;
563 static const struct got_error *
564 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
566 size_t n;
568 n = fread(val, 1, sizeof(*val), infile);
569 if (n != sizeof(*val))
570 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
571 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
572 *val = be16toh(*val);
573 return NULL;
576 static const struct got_error *
577 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
579 const struct got_error *err = NULL;
580 const size_t chunk_size = 8;
581 size_t n, len = 0, totlen = chunk_size;
583 *path = malloc(totlen);
584 if (*path == NULL)
585 return got_error_from_errno("malloc");
587 do {
588 if (len + chunk_size > totlen) {
589 char *p = reallocarray(*path, totlen + chunk_size, 1);
590 if (p == NULL) {
591 err = got_error_from_errno("reallocarray");
592 break;
594 totlen += chunk_size;
595 *path = p;
597 n = fread(*path + len, 1, chunk_size, infile);
598 if (n != chunk_size) {
599 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
600 break;
602 SHA1Update(ctx, *path + len, chunk_size);
603 len += chunk_size;
604 } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
606 if (err) {
607 free(*path);
608 *path = NULL;
610 return err;
613 static const struct got_error *
614 read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
615 FILE *infile, uint32_t version)
617 const struct got_error *err;
618 struct got_fileindex_entry *ie;
619 size_t n;
621 *iep = NULL;
623 ie = calloc(1, sizeof(*ie));
624 if (ie == NULL)
625 return got_error_from_errno("calloc");
627 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
628 if (err)
629 goto done;
630 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
631 if (err)
632 goto done;
633 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
634 if (err)
635 goto done;
636 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
637 if (err)
638 goto done;
640 err = read_fileindex_val32(&ie->uid, ctx, infile);
641 if (err)
642 goto done;
643 err = read_fileindex_val32(&ie->gid, ctx, infile);
644 if (err)
645 goto done;
646 err = read_fileindex_val32(&ie->size, ctx, infile);
647 if (err)
648 goto done;
650 err = read_fileindex_val16(&ie->mode, ctx, infile);
651 if (err)
652 goto done;
654 n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
655 if (n != SHA1_DIGEST_LENGTH) {
656 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
657 goto done;
659 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
661 n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
662 if (n != SHA1_DIGEST_LENGTH) {
663 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
664 goto done;
666 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
668 err = read_fileindex_val32(&ie->flags, ctx, infile);
669 if (err)
670 goto done;
672 err = read_fileindex_path(&ie->path, ctx, infile);
673 if (err)
674 goto done;
676 if (version >= 2) {
677 uint32_t stage = got_fileindex_entry_stage_get(ie);
678 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
679 stage == GOT_FILEIDX_STAGE_ADD) {
680 n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
681 infile);
682 if (n != SHA1_DIGEST_LENGTH) {
683 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
684 goto done;
686 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
688 } else {
689 /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
690 ie->flags &= ~GOT_FILEIDX_F_STAGE;
693 done:
694 if (err)
695 got_fileindex_entry_free(ie);
696 else
697 *iep = ie;
698 return err;
701 const struct got_error *
702 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
704 const struct got_error *err = NULL;
705 struct got_fileindex_hdr hdr;
706 SHA1_CTX ctx;
707 struct got_fileindex_entry *ie;
708 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
709 uint8_t sha1[SHA1_DIGEST_LENGTH];
710 size_t n;
711 int i;
713 SHA1Init(&ctx);
715 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
716 if (n != sizeof(hdr.signature)) {
717 if (n == 0) /* EOF */
718 return NULL;
719 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
721 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
722 if (n != sizeof(hdr.version)) {
723 if (n == 0) /* EOF */
724 return NULL;
725 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
727 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
728 if (n != sizeof(hdr.nentries)) {
729 if (n == 0) /* EOF */
730 return NULL;
731 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
734 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
735 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
736 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
738 hdr.signature = be32toh(hdr.signature);
739 hdr.version = be32toh(hdr.version);
740 hdr.nentries = be32toh(hdr.nentries);
742 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
743 return got_error(GOT_ERR_FILEIDX_SIG);
744 if (hdr.version > GOT_FILE_INDEX_VERSION)
745 return got_error(GOT_ERR_FILEIDX_VER);
747 for (i = 0; i < hdr.nentries; i++) {
748 err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
749 if (err)
750 return err;
751 err = add_entry(fileindex, ie);
752 if (err)
753 return err;
756 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
757 if (n != sizeof(sha1_expected))
758 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
759 SHA1Final(sha1, &ctx);
760 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
761 return got_error(GOT_ERR_FILEIDX_CSUM);
763 return NULL;
766 static struct got_fileindex_entry *
767 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
769 struct got_fileindex_entry *next;
771 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
773 /* Skip entries which were added or removed by diff callbacks. */
774 while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
775 GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
776 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
778 return next;
781 static const struct got_error *
782 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
783 struct got_tree_object *tree, const char *, const char *,
784 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
786 static const struct got_error *
787 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
788 struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
789 const char *path, const char *entry_name, struct got_repository *repo,
790 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
792 const struct got_error *err = NULL;
793 struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
795 if (!got_object_tree_entry_is_submodule(te) &&
796 S_ISDIR(got_tree_entry_get_mode(te))) {
797 char *subpath;
798 struct got_tree_object *subtree;
800 if (asprintf(&subpath, "%s%s%s", path,
801 path[0] == '\0' ? "" : "/",
802 got_tree_entry_get_name(te)) == -1)
803 return got_error_from_errno("asprintf");
805 err = got_object_open_as_tree(&subtree, repo,
806 got_tree_entry_get_id(te));
807 if (err) {
808 free(subpath);
809 return err;
812 err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
813 entry_name, repo, cb, cb_arg);
814 free(subpath);
815 got_object_tree_close(subtree);
816 if (err)
817 return err;
820 (*tidx)++;
821 *next = got_object_tree_get_entry(tree, *tidx);
822 return NULL;
825 static const struct got_error *
826 diff_fileindex_tree(struct got_fileindex *fileindex,
827 struct got_fileindex_entry **ie, struct got_tree_object *tree,
828 const char *path, const char *entry_name, struct got_repository *repo,
829 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
831 const struct got_error *err = NULL;
832 struct got_tree_entry *te = NULL;
833 size_t path_len = strlen(path);
834 struct got_fileindex_entry *next;
835 int tidx = 0;
837 te = got_object_tree_get_entry(tree, tidx);
838 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
839 if (te && *ie) {
840 char *te_path;
841 const char *te_name = got_tree_entry_get_name(te);
842 int cmp;
843 if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
844 err = got_error_from_errno("asprintf");
845 break;
847 cmp = got_path_cmp((*ie)->path, te_path,
848 got_fileindex_entry_path_len(*ie), strlen(te_path));
849 free(te_path);
850 if (cmp == 0) {
851 if (got_path_is_child((*ie)->path, path,
852 path_len) &&
853 !got_object_tree_entry_is_submodule(te) &&
854 (entry_name == NULL ||
855 strcmp(te_name, entry_name) == 0)) {
856 err = cb->diff_old_new(cb_arg, *ie, te,
857 path);
858 if (err || entry_name)
859 break;
861 *ie = walk_fileindex(fileindex, *ie);
862 err = walk_tree(&te, fileindex, ie, tree, &tidx,
863 path, entry_name, repo, cb, cb_arg);
864 } else if (cmp < 0) {
865 next = walk_fileindex(fileindex, *ie);
866 if (got_path_is_child((*ie)->path, path,
867 path_len) && entry_name == NULL) {
868 err = cb->diff_old(cb_arg, *ie, path);
869 if (err || entry_name)
870 break;
872 *ie = next;
873 } else {
874 if ((entry_name == NULL ||
875 strcmp(te_name, entry_name) == 0)) {
876 err = cb->diff_new(cb_arg, te, path);
877 if (err || entry_name)
878 break;
880 err = walk_tree(&te, fileindex, ie, tree, &tidx,
881 path, entry_name, repo, cb, cb_arg);
883 if (err)
884 break;
885 } else if (*ie) {
886 next = walk_fileindex(fileindex, *ie);
887 if (got_path_is_child((*ie)->path, path, path_len) &&
888 (entry_name == NULL ||
889 (te && strcmp(got_tree_entry_get_name(te),
890 entry_name) == 0))) {
891 err = cb->diff_old(cb_arg, *ie, path);
892 if (err || entry_name)
893 break;
895 *ie = next;
896 } else if (te) {
897 if (!got_object_tree_entry_is_submodule(te) &&
898 (entry_name == NULL ||
899 strcmp(got_tree_entry_get_name(te), entry_name)
900 == 0)) {
901 err = cb->diff_new(cb_arg, te, path);
902 if (err || entry_name)
903 break;
905 err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
906 entry_name, repo, cb, cb_arg);
907 if (err)
908 break;
912 return err;
915 const struct got_error *
916 got_fileindex_diff_tree(struct got_fileindex *fileindex,
917 struct got_tree_object *tree, const char *path, const char *entry_name,
918 struct got_repository *repo,
919 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
921 struct got_fileindex_entry *ie;
922 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
923 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
924 ie = walk_fileindex(fileindex, ie);
925 return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
926 cb, cb_arg);
929 static const struct got_error *
930 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
931 struct got_pathlist_head *, int, const char *, const char *,
932 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
934 static const struct got_error *
935 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
937 const struct got_error *err = NULL;
938 struct got_pathlist_entry *new = NULL;
939 struct dirent *dep = NULL;
940 struct dirent *de = NULL;
942 for (;;) {
943 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
944 if (de == NULL) {
945 err = got_error_from_errno("malloc");
946 break;
949 if (readdir_r(dir, de, &dep) != 0) {
950 err = got_error_from_errno("readdir_r");
951 free(de);
952 break;
954 if (dep == NULL) {
955 free(de);
956 break;
959 if (strcmp(de->d_name, ".") == 0 ||
960 strcmp(de->d_name, "..") == 0 ||
961 (path[0] == '\0' &&
962 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
963 free(de);
964 continue;
967 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
968 if (err) {
969 free(de);
970 break;
972 if (new == NULL) {
973 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
974 free(de);
975 break;
979 return err;
982 static int
983 have_tracked_file_in_dir(struct got_fileindex *fileindex, const char *path)
985 struct got_fileindex_entry *ie;
986 size_t path_len = strlen(path);
987 int cmp;
989 ie = RB_ROOT(&fileindex->entries);
990 while (ie) {
991 if (got_path_is_child(ie->path, path, path_len))
992 return 1;
993 cmp = got_path_cmp(path, ie->path, path_len,
994 got_fileindex_entry_path_len(ie));
995 if (cmp < 0)
996 ie = RB_LEFT(ie, entry);
997 else if (cmp > 0)
998 ie = RB_RIGHT(ie, entry);
999 else
1000 break;
1003 return 0;
1006 static const struct got_error *
1007 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
1008 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
1009 const char *path, const char *rootpath, struct got_repository *repo,
1010 int ignore, struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1012 const struct got_error *err = NULL;
1013 struct dirent *de = dle->data;
1014 DIR *subdir = NULL;
1015 int subdirfd = -1;
1017 *next = NULL;
1019 /* Must traverse ignored directories if they contain tracked files. */
1020 if (de->d_type == DT_DIR && ignore &&
1021 have_tracked_file_in_dir(fileindex, path))
1022 ignore = 0;
1024 if (de->d_type == DT_DIR && !ignore) {
1025 char *subpath;
1026 char *subdirpath;
1027 struct got_pathlist_head subdirlist;
1029 TAILQ_INIT(&subdirlist);
1031 if (asprintf(&subpath, "%s%s%s", path,
1032 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1033 return got_error_from_errno("asprintf");
1035 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
1036 free(subpath);
1037 return got_error_from_errno("asprintf");
1040 subdirfd = openat(fd, de->d_name,
1041 O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
1042 if (subdirfd == -1) {
1043 if (errno == EACCES) {
1044 *next = TAILQ_NEXT(dle, entry);
1045 return NULL;
1047 err = got_error_from_errno2("openat", subdirpath);
1048 free(subpath);
1049 free(subdirpath);
1050 return err;
1053 subdir = fdopendir(subdirfd);
1054 if (subdir == NULL)
1055 return got_error_from_errno2("fdopendir", path);
1056 subdirfd = -1;
1057 err = read_dirlist(&subdirlist, subdir, subdirpath);
1058 if (err) {
1059 free(subpath);
1060 free(subdirpath);
1061 closedir(subdir);
1062 return err;
1064 err = diff_fileindex_dir(fileindex, ie, &subdirlist,
1065 dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
1066 if (subdir && closedir(subdir) == -1 && err == NULL)
1067 err = got_error_from_errno2("closedir", subdirpath);
1068 free(subpath);
1069 free(subdirpath);
1070 got_pathlist_free(&subdirlist, GOT_PATHLIST_FREE_DATA);
1071 if (err)
1072 return err;
1075 *next = TAILQ_NEXT(dle, entry);
1076 return NULL;
1079 static const struct got_error *
1080 dirent_type_fixup(struct dirent *de, const char *rootpath, const char *path)
1082 const struct got_error *err;
1083 char *dir_path;
1084 int type;
1086 if (de->d_type != DT_UNKNOWN)
1087 return NULL;
1089 /* DT_UNKNOWN occurs on NFS mounts without "readdir plus" RPC. */
1090 if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
1091 return got_error_from_errno("asprintf");
1092 err = got_path_dirent_type(&type, dir_path, de);
1093 free(dir_path);
1094 if (err)
1095 return err;
1097 de->d_type = type;
1098 return NULL;
1101 static const struct got_error *
1102 diff_fileindex_dir(struct got_fileindex *fileindex,
1103 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1104 int dirfd, const char *rootpath, const char *path,
1105 struct got_repository *repo,
1106 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1108 const struct got_error *err = NULL;
1109 struct dirent *de = NULL;
1110 size_t path_len = strlen(path);
1111 struct got_pathlist_entry *dle;
1112 int ignore;
1114 if (cb->diff_traverse) {
1115 err = cb->diff_traverse(cb_arg, path, dirfd);
1116 if (err)
1117 return err;
1120 dle = TAILQ_FIRST(dirlist);
1121 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1122 if (dle && *ie) {
1123 char *de_path;
1124 int cmp;
1125 de = dle->data;
1126 err = dirent_type_fixup(de, rootpath, path);
1127 if (err)
1128 break;
1129 if (asprintf(&de_path, "%s/%s", path,
1130 de->d_name) == -1) {
1131 err = got_error_from_errno("asprintf");
1132 break;
1134 cmp = got_path_cmp((*ie)->path, de_path,
1135 got_fileindex_entry_path_len(*ie),
1136 strlen(path) + 1 + de->d_namlen);
1137 free(de_path);
1138 if (cmp == 0) {
1139 err = cb->diff_old_new(cb_arg, *ie, de, path,
1140 dirfd);
1141 if (err)
1142 break;
1143 *ie = walk_fileindex(fileindex, *ie);
1144 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1145 path, rootpath, repo, 0, cb, cb_arg);
1146 } else if (cmp < 0 ) {
1147 err = cb->diff_old(cb_arg, *ie, path);
1148 if (err)
1149 break;
1150 *ie = walk_fileindex(fileindex, *ie);
1151 } else {
1152 err = cb->diff_new(&ignore, cb_arg, de, path,
1153 dirfd);
1154 if (err)
1155 break;
1156 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1157 path, rootpath, repo, ignore, cb, cb_arg);
1159 if (err)
1160 break;
1161 } else if (*ie) {
1162 err = cb->diff_old(cb_arg, *ie, path);
1163 if (err)
1164 break;
1165 *ie = walk_fileindex(fileindex, *ie);
1166 } else if (dle) {
1167 de = dle->data;
1168 err = dirent_type_fixup(de, rootpath, path);
1169 if (err)
1170 break;
1171 err = cb->diff_new(&ignore, cb_arg, de, path, dirfd);
1172 if (err)
1173 break;
1174 err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1175 rootpath, repo, ignore, cb, cb_arg);
1176 if (err)
1177 break;
1181 return err;
1184 const struct got_error *
1185 got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1186 const char *rootpath, const char *path, struct got_repository *repo,
1187 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1189 const struct got_error *err;
1190 struct got_fileindex_entry *ie;
1191 struct got_pathlist_head dirlist;
1192 int fd2;
1193 DIR *dir;
1195 TAILQ_INIT(&dirlist);
1198 * Duplicate the file descriptor so we can call closedir() below
1199 * without closing the file descriptor passed in by our caller.
1201 fd2 = dup(fd);
1202 if (fd2 == -1)
1203 return got_error_from_errno2("dup", path);
1204 if (lseek(fd2, 0, SEEK_SET) == -1) {
1205 err = got_error_from_errno2("lseek", path);
1206 close(fd2);
1207 return err;
1209 dir = fdopendir(fd2);
1210 if (dir == NULL) {
1211 err = got_error_from_errno2("fdopendir", path);
1212 close(fd2);
1213 return err;
1215 err = read_dirlist(&dirlist, dir, path);
1216 if (err) {
1217 closedir(dir);
1218 return err;
1221 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1222 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1223 ie = walk_fileindex(fileindex, ie);
1224 err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1225 rootpath, path, repo, cb, cb_arg);
1227 if (closedir(dir) == -1 && err == NULL)
1228 err = got_error_from_errno2("closedir", path);
1229 got_pathlist_free(&dirlist, GOT_PATHLIST_FREE_DATA);
1230 return err;
1233 struct got_object_id *
1234 got_fileindex_entry_get_staged_blob_id(struct got_object_id *id,
1235 struct got_fileindex_entry *ie)
1237 memset(id, 0, sizeof(*id));
1238 memcpy(id->sha1, ie->staged_blob_sha1, sizeof(ie->staged_blob_sha1));
1239 return id;
1242 struct got_object_id *
1243 got_fileindex_entry_get_blob_id(struct got_object_id *id,
1244 struct got_fileindex_entry *ie)
1246 memset(id, 0, sizeof(*id));
1247 memcpy(id->sha1, ie->blob_sha1, sizeof(ie->blob_sha1));
1248 return id;
1251 struct got_object_id *
1252 got_fileindex_entry_get_commit_id(struct got_object_id *id,
1253 struct got_fileindex_entry *ie)
1255 memset(id, 0, sizeof(*id));
1256 memcpy(id->sha1, ie->commit_sha1, sizeof(ie->commit_sha1));
1257 return id;
1260 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);