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;
107 if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
108 if (update_timestamps) {
109 ie->ctime_sec = sb.st_ctim.tv_sec;
110 ie->ctime_nsec = sb.st_ctim.tv_nsec;
111 ie->mtime_sec = sb.st_mtim.tv_sec;
112 ie->mtime_nsec = sb.st_mtim.tv_nsec;
114 ie->uid = sb.st_uid;
115 ie->gid = sb.st_gid;
116 ie->size = (sb.st_size & 0xffffffff);
117 if (S_ISLNK(sb.st_mode)) {
118 got_fileindex_entry_filetype_set(ie,
119 GOT_FILEIDX_MODE_SYMLINK);
120 fileindex_entry_perms_set(ie, 0);
121 } else {
122 got_fileindex_entry_filetype_set(ie,
123 GOT_FILEIDX_MODE_REGULAR_FILE);
124 fileindex_entry_perms_set(ie,
125 sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
129 if (blob_sha1) {
130 memcpy(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
131 ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
132 } else
133 ie->flags |= GOT_FILEIDX_F_NO_BLOB;
135 if (commit_sha1) {
136 memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
137 ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
138 } else
139 ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
141 return NULL;
144 void
145 got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
147 ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
150 void
151 got_fileindex_entry_mark_skipped(struct got_fileindex_entry *ie)
153 ie->flags |= GOT_FILEIDX_F_SKIPPED;
156 const struct got_error *
157 got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
158 const char *relpath)
160 size_t len;
162 *ie = calloc(1, sizeof(**ie));
163 if (*ie == NULL)
164 return got_error_from_errno("calloc");
166 (*ie)->path = strdup(relpath);
167 if ((*ie)->path == NULL) {
168 const struct got_error *err = got_error_from_errno("strdup");
169 free(*ie);
170 *ie = NULL;
171 return err;
174 len = strlen(relpath);
175 if (len > GOT_FILEIDX_F_PATH_LEN)
176 len = GOT_FILEIDX_F_PATH_LEN;
177 (*ie)->flags |= len;
179 return NULL;
182 void
183 got_fileindex_entry_free(struct got_fileindex_entry *ie)
185 free(ie->path);
186 free(ie);
189 size_t
190 got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
192 return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
195 uint32_t
196 got_fileindex_entry_stage_get(const struct got_fileindex_entry *ie)
198 return ((ie->flags & GOT_FILEIDX_F_STAGE) >> GOT_FILEIDX_F_STAGE_SHIFT);
201 void
202 got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage)
204 ie->flags &= ~GOT_FILEIDX_F_STAGE;
205 ie->flags |= ((stage << GOT_FILEIDX_F_STAGE_SHIFT) &
206 GOT_FILEIDX_F_STAGE);
209 int
210 got_fileindex_entry_filetype_get(struct got_fileindex_entry *ie)
212 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
215 void
216 got_fileindex_entry_filetype_set(struct got_fileindex_entry *ie, int type)
218 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_ONDISK;
219 ie->mode |= (type & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
222 void
223 got_fileindex_entry_staged_filetype_set(struct got_fileindex_entry *ie,
224 int type)
226 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_STAGED;
227 ie->mode |= ((type << GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT) &
228 GOT_FILEIDX_MODE_FILE_TYPE_STAGED);
231 int
232 got_fileindex_entry_staged_filetype_get(struct got_fileindex_entry *ie)
234 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_STAGED) >>
235 GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT;
238 int
239 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
241 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
244 int
245 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
247 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
250 int
251 got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
253 return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
256 int
257 got_fileindex_entry_was_skipped(struct got_fileindex_entry *ie)
259 return (ie->flags & GOT_FILEIDX_F_SKIPPED) != 0;
262 static const struct got_error *
263 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
265 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
266 return got_error(GOT_ERR_NO_SPACE);
268 if (RB_INSERT(got_fileindex_tree, &fileindex->entries, ie) != NULL)
269 return got_error_path(ie->path, GOT_ERR_FILEIDX_DUP_ENTRY);
271 fileindex->nentries++;
272 return NULL;
275 const struct got_error *
276 got_fileindex_entry_add(struct got_fileindex *fileindex,
277 struct got_fileindex_entry *ie)
279 /* Flag this entry until it gets written out to disk. */
280 ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
282 return add_entry(fileindex, ie);
285 void
286 got_fileindex_entry_remove(struct got_fileindex *fileindex,
287 struct got_fileindex_entry *ie)
289 /*
290 * Removing an entry from the RB tree immediately breaks
291 * in-progress iterations over file index entries.
292 * So flag this entry for removal and remove it once the index
293 * is written out to disk. Meanwhile, pretend this entry no longer
294 * exists if we get queried for it again before then.
295 */
296 ie->flags |= GOT_FILEIDX_F_REMOVE_ON_FLUSH;
297 fileindex->nentries--;
300 struct got_fileindex_entry *
301 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
302 size_t path_len)
304 struct got_fileindex_entry *ie;
305 struct got_fileindex_entry key;
306 memset(&key, 0, sizeof(key));
307 key.path = (char *)path;
308 key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
309 ie = RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
310 if (ie && (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH))
311 return NULL;
312 return ie;
315 const struct got_error *
316 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
317 got_fileindex_cb cb, void *cb_arg)
319 const struct got_error *err;
320 struct got_fileindex_entry *ie, *tmp;
322 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
323 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
324 continue;
325 err = (*cb)(cb_arg, ie);
326 if (err)
327 return err;
329 return NULL;
332 struct got_fileindex *
333 got_fileindex_alloc(void)
335 struct got_fileindex *fileindex;
337 fileindex = calloc(1, sizeof(*fileindex));
338 if (fileindex == NULL)
339 return NULL;
341 RB_INIT(&fileindex->entries);
342 return fileindex;
345 void
346 got_fileindex_free(struct got_fileindex *fileindex)
348 struct got_fileindex_entry *ie;
350 while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
351 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
352 got_fileindex_entry_free(ie);
354 free(fileindex);
357 static const struct got_error *
358 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
360 size_t n;
362 val = htobe64(val);
363 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
364 n = fwrite(&val, 1, sizeof(val), outfile);
365 if (n != sizeof(val))
366 return got_ferror(outfile, GOT_ERR_IO);
367 return NULL;
370 static const struct got_error *
371 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
373 size_t n;
375 val = htobe32(val);
376 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
377 n = fwrite(&val, 1, sizeof(val), outfile);
378 if (n != sizeof(val))
379 return got_ferror(outfile, GOT_ERR_IO);
380 return NULL;
383 static const struct got_error *
384 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
386 size_t n;
388 val = htobe16(val);
389 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
390 n = fwrite(&val, 1, sizeof(val), outfile);
391 if (n != sizeof(val))
392 return got_ferror(outfile, GOT_ERR_IO);
393 return NULL;
396 static const struct got_error *
397 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
399 size_t n, len, pad = 0;
400 static const uint8_t zero[8] = { 0 };
402 len = strlen(path);
403 while ((len + pad) % 8 != 0)
404 pad++;
405 if (pad == 0)
406 pad = 8; /* NUL-terminate */
408 SHA1Update(ctx, path, len);
409 n = fwrite(path, 1, len, outfile);
410 if (n != len)
411 return got_ferror(outfile, GOT_ERR_IO);
412 SHA1Update(ctx, zero, pad);
413 n = fwrite(zero, 1, pad, outfile);
414 if (n != pad)
415 return got_ferror(outfile, GOT_ERR_IO);
416 return NULL;
419 static const struct got_error *
420 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
421 FILE *outfile)
423 const struct got_error *err;
424 size_t n;
425 uint32_t stage;
427 err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
428 if (err)
429 return err;
430 err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
431 if (err)
432 return err;
433 err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
434 if (err)
435 return err;
436 err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
437 if (err)
438 return err;
440 err = write_fileindex_val32(ctx, ie->uid, outfile);
441 if (err)
442 return err;
443 err = write_fileindex_val32(ctx, ie->gid, outfile);
444 if (err)
445 return err;
446 err = write_fileindex_val32(ctx, ie->size, outfile);
447 if (err)
448 return err;
450 err = write_fileindex_val16(ctx, ie->mode, outfile);
451 if (err)
452 return err;
454 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
455 n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
456 if (n != SHA1_DIGEST_LENGTH)
457 return got_ferror(outfile, GOT_ERR_IO);
459 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
460 n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
461 if (n != SHA1_DIGEST_LENGTH)
462 return got_ferror(outfile, GOT_ERR_IO);
464 err = write_fileindex_val32(ctx, ie->flags, outfile);
465 if (err)
466 return err;
468 err = write_fileindex_path(ctx, ie->path, outfile);
469 if (err)
470 return err;
472 stage = got_fileindex_entry_stage_get(ie);
473 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
474 stage == GOT_FILEIDX_STAGE_ADD) {
475 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
476 n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
477 outfile);
478 if (n != SHA1_DIGEST_LENGTH)
479 return got_ferror(outfile, GOT_ERR_IO);
482 return NULL;
485 const struct got_error *
486 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
488 const struct got_error *err = NULL;
489 struct got_fileindex_hdr hdr;
490 SHA1_CTX ctx;
491 uint8_t sha1[SHA1_DIGEST_LENGTH];
492 size_t n;
493 struct got_fileindex_entry *ie, *tmp;
495 SHA1Init(&ctx);
497 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
498 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
499 hdr.nentries = htobe32(fileindex->nentries);
501 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
502 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
503 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
504 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
505 if (n != sizeof(hdr.signature))
506 return got_ferror(outfile, GOT_ERR_IO);
507 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
508 if (n != sizeof(hdr.version))
509 return got_ferror(outfile, GOT_ERR_IO);
510 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
511 if (n != sizeof(hdr.nentries))
512 return got_ferror(outfile, GOT_ERR_IO);
514 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
515 ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
516 ie->flags &= ~GOT_FILEIDX_F_SKIPPED;
517 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH) {
518 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
519 got_fileindex_entry_free(ie);
520 continue;
522 err = write_fileindex_entry(&ctx, ie, outfile);
523 if (err)
524 return err;
527 SHA1Final(sha1, &ctx);
528 n = fwrite(sha1, 1, sizeof(sha1), outfile);
529 if (n != sizeof(sha1))
530 return got_ferror(outfile, GOT_ERR_IO);
532 if (fflush(outfile) != 0)
533 return got_error_from_errno("fflush");
535 return NULL;
538 static const struct got_error *
539 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
541 size_t n;
543 n = fread(val, 1, sizeof(*val), infile);
544 if (n != sizeof(*val))
545 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
546 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
547 *val = be64toh(*val);
548 return NULL;
551 static const struct got_error *
552 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
554 size_t n;
556 n = fread(val, 1, sizeof(*val), infile);
557 if (n != sizeof(*val))
558 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
559 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
560 *val = be32toh(*val);
561 return NULL;
564 static const struct got_error *
565 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
567 size_t n;
569 n = fread(val, 1, sizeof(*val), infile);
570 if (n != sizeof(*val))
571 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
572 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
573 *val = be16toh(*val);
574 return NULL;
577 static const struct got_error *
578 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
580 const struct got_error *err = NULL;
581 const size_t chunk_size = 8;
582 size_t n, len = 0, totlen = chunk_size;
584 *path = malloc(totlen);
585 if (*path == NULL)
586 return got_error_from_errno("malloc");
588 do {
589 if (len + chunk_size > totlen) {
590 char *p = reallocarray(*path, totlen + chunk_size, 1);
591 if (p == NULL) {
592 err = got_error_from_errno("reallocarray");
593 break;
595 totlen += chunk_size;
596 *path = p;
598 n = fread(*path + len, 1, chunk_size, infile);
599 if (n != chunk_size) {
600 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
601 break;
603 SHA1Update(ctx, *path + len, chunk_size);
604 len += chunk_size;
605 } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
607 if (err) {
608 free(*path);
609 *path = NULL;
611 return err;
614 static const struct got_error *
615 read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
616 FILE *infile, uint32_t version)
618 const struct got_error *err;
619 struct got_fileindex_entry *ie;
620 size_t n;
622 *iep = NULL;
624 ie = calloc(1, sizeof(*ie));
625 if (ie == NULL)
626 return got_error_from_errno("calloc");
628 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
629 if (err)
630 goto done;
631 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
632 if (err)
633 goto done;
634 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
635 if (err)
636 goto done;
637 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
638 if (err)
639 goto done;
641 err = read_fileindex_val32(&ie->uid, ctx, infile);
642 if (err)
643 goto done;
644 err = read_fileindex_val32(&ie->gid, ctx, infile);
645 if (err)
646 goto done;
647 err = read_fileindex_val32(&ie->size, ctx, infile);
648 if (err)
649 goto done;
651 err = read_fileindex_val16(&ie->mode, ctx, infile);
652 if (err)
653 goto done;
655 n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
656 if (n != SHA1_DIGEST_LENGTH) {
657 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
658 goto done;
660 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
662 n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
663 if (n != SHA1_DIGEST_LENGTH) {
664 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
665 goto done;
667 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
669 err = read_fileindex_val32(&ie->flags, ctx, infile);
670 if (err)
671 goto done;
673 err = read_fileindex_path(&ie->path, ctx, infile);
674 if (err)
675 goto done;
677 if (version >= 2) {
678 uint32_t stage = got_fileindex_entry_stage_get(ie);
679 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
680 stage == GOT_FILEIDX_STAGE_ADD) {
681 n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
682 infile);
683 if (n != SHA1_DIGEST_LENGTH) {
684 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
685 goto done;
687 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
689 } else {
690 /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
691 ie->flags &= ~GOT_FILEIDX_F_STAGE;
694 done:
695 if (err)
696 got_fileindex_entry_free(ie);
697 else
698 *iep = ie;
699 return err;
702 const struct got_error *
703 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
705 const struct got_error *err = NULL;
706 struct got_fileindex_hdr hdr;
707 SHA1_CTX ctx;
708 struct got_fileindex_entry *ie;
709 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
710 uint8_t sha1[SHA1_DIGEST_LENGTH];
711 size_t n;
712 int i;
714 SHA1Init(&ctx);
716 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
717 if (n != sizeof(hdr.signature)) {
718 if (n == 0) /* EOF */
719 return NULL;
720 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
722 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
723 if (n != sizeof(hdr.version)) {
724 if (n == 0) /* EOF */
725 return NULL;
726 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
728 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
729 if (n != sizeof(hdr.nentries)) {
730 if (n == 0) /* EOF */
731 return NULL;
732 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
735 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
736 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
737 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
739 hdr.signature = be32toh(hdr.signature);
740 hdr.version = be32toh(hdr.version);
741 hdr.nentries = be32toh(hdr.nentries);
743 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
744 return got_error(GOT_ERR_FILEIDX_SIG);
745 if (hdr.version > GOT_FILE_INDEX_VERSION)
746 return got_error(GOT_ERR_FILEIDX_VER);
748 for (i = 0; i < hdr.nentries; i++) {
749 err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
750 if (err)
751 return err;
752 err = add_entry(fileindex, ie);
753 if (err)
754 return err;
757 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
758 if (n != sizeof(sha1_expected))
759 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
760 SHA1Final(sha1, &ctx);
761 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
762 return got_error(GOT_ERR_FILEIDX_CSUM);
764 return NULL;
767 static struct got_fileindex_entry *
768 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
770 struct got_fileindex_entry *next;
772 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
774 /* Skip entries which were added or removed by diff callbacks. */
775 while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
776 GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
777 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
779 return next;
782 static const struct got_error *
783 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
784 struct got_tree_object *tree, const char *, const char *,
785 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
787 static const struct got_error *
788 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
789 struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
790 const char *path, const char *entry_name, struct got_repository *repo,
791 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
793 const struct got_error *err = NULL;
794 struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
796 if (!got_object_tree_entry_is_submodule(te) &&
797 S_ISDIR(got_tree_entry_get_mode(te))) {
798 char *subpath;
799 struct got_tree_object *subtree;
801 if (asprintf(&subpath, "%s%s%s", path,
802 path[0] == '\0' ? "" : "/",
803 got_tree_entry_get_name(te)) == -1)
804 return got_error_from_errno("asprintf");
806 err = got_object_open_as_tree(&subtree, repo,
807 got_tree_entry_get_id(te));
808 if (err) {
809 free(subpath);
810 return err;
813 err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
814 entry_name, repo, cb, cb_arg);
815 free(subpath);
816 got_object_tree_close(subtree);
817 if (err)
818 return err;
821 (*tidx)++;
822 *next = got_object_tree_get_entry(tree, *tidx);
823 return NULL;
826 static const struct got_error *
827 diff_fileindex_tree(struct got_fileindex *fileindex,
828 struct got_fileindex_entry **ie, struct got_tree_object *tree,
829 const char *path, const char *entry_name, struct got_repository *repo,
830 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
832 const struct got_error *err = NULL;
833 struct got_tree_entry *te = NULL;
834 size_t path_len = strlen(path);
835 struct got_fileindex_entry *next;
836 int tidx = 0;
838 te = got_object_tree_get_entry(tree, tidx);
839 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
840 if (te && *ie) {
841 char *te_path;
842 const char *te_name = got_tree_entry_get_name(te);
843 int cmp;
844 if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
845 err = got_error_from_errno("asprintf");
846 break;
848 cmp = got_path_cmp((*ie)->path, te_path,
849 got_fileindex_entry_path_len(*ie), strlen(te_path));
850 free(te_path);
851 if (cmp == 0) {
852 if (got_path_is_child((*ie)->path, path,
853 path_len) &&
854 !got_object_tree_entry_is_submodule(te) &&
855 (entry_name == NULL ||
856 strcmp(te_name, entry_name) == 0)) {
857 err = cb->diff_old_new(cb_arg, *ie, te,
858 path);
859 if (err || entry_name)
860 break;
862 *ie = walk_fileindex(fileindex, *ie);
863 err = walk_tree(&te, fileindex, ie, tree, &tidx,
864 path, entry_name, repo, cb, cb_arg);
865 } else if (cmp < 0) {
866 next = walk_fileindex(fileindex, *ie);
867 if (got_path_is_child((*ie)->path, path,
868 path_len) && entry_name == NULL) {
869 err = cb->diff_old(cb_arg, *ie, path);
870 if (err || entry_name)
871 break;
873 *ie = next;
874 } else {
875 if ((entry_name == NULL ||
876 strcmp(te_name, entry_name) == 0)) {
877 err = cb->diff_new(cb_arg, te, path);
878 if (err || entry_name)
879 break;
881 err = walk_tree(&te, fileindex, ie, tree, &tidx,
882 path, entry_name, repo, cb, cb_arg);
884 if (err)
885 break;
886 } else if (*ie) {
887 next = walk_fileindex(fileindex, *ie);
888 if (got_path_is_child((*ie)->path, path, path_len) &&
889 (entry_name == NULL ||
890 (te && strcmp(got_tree_entry_get_name(te),
891 entry_name) == 0))) {
892 err = cb->diff_old(cb_arg, *ie, path);
893 if (err || entry_name)
894 break;
896 *ie = next;
897 } else if (te) {
898 if (!got_object_tree_entry_is_submodule(te) &&
899 (entry_name == NULL ||
900 strcmp(got_tree_entry_get_name(te), entry_name)
901 == 0)) {
902 err = cb->diff_new(cb_arg, te, path);
903 if (err || entry_name)
904 break;
906 err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
907 entry_name, repo, cb, cb_arg);
908 if (err)
909 break;
913 return err;
916 const struct got_error *
917 got_fileindex_diff_tree(struct got_fileindex *fileindex,
918 struct got_tree_object *tree, const char *path, const char *entry_name,
919 struct got_repository *repo,
920 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
922 struct got_fileindex_entry *ie;
923 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
924 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
925 ie = walk_fileindex(fileindex, ie);
926 return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
927 cb, cb_arg);
930 static const struct got_error *
931 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
932 struct got_pathlist_head *, int, const char *, const char *,
933 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
935 static const struct got_error *
936 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
938 const struct got_error *err = NULL;
939 struct got_pathlist_entry *new = NULL;
940 struct dirent *dep = NULL;
941 struct dirent *de = NULL;
943 for (;;) {
944 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
945 if (de == NULL) {
946 err = got_error_from_errno("malloc");
947 break;
950 if (readdir_r(dir, de, &dep) != 0) {
951 err = got_error_from_errno("readdir_r");
952 free(de);
953 break;
955 if (dep == NULL) {
956 free(de);
957 break;
960 if (strcmp(de->d_name, ".") == 0 ||
961 strcmp(de->d_name, "..") == 0 ||
962 (path[0] == '\0' &&
963 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
964 free(de);
965 continue;
968 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
969 if (err) {
970 free(de);
971 break;
973 if (new == NULL) {
974 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
975 free(de);
976 break;
980 return err;
983 static void
984 free_dirlist(struct got_pathlist_head *dirlist)
986 struct got_pathlist_entry *dle;
988 TAILQ_FOREACH(dle, dirlist, entry)
989 free(dle->data);
990 got_pathlist_free(dirlist);
993 static int
994 have_tracked_file_in_dir(struct got_fileindex *fileindex, const char *path)
996 struct got_fileindex_entry *ie;
997 size_t path_len = strlen(path);
998 int cmp;
1000 ie = RB_ROOT(&fileindex->entries);
1001 while (ie) {
1002 if (got_path_is_child(ie->path, path, path_len))
1003 return 1;
1004 cmp = got_path_cmp(path, ie->path, path_len,
1005 got_fileindex_entry_path_len(ie));
1006 if (cmp < 0)
1007 ie = RB_LEFT(ie, entry);
1008 else if (cmp > 0)
1009 ie = RB_RIGHT(ie, entry);
1010 else
1011 break;
1014 return 0;
1017 static const struct got_error *
1018 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
1019 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
1020 const char *path, const char *rootpath, struct got_repository *repo,
1021 int ignore, struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1023 const struct got_error *err = NULL;
1024 struct dirent *de = dle->data;
1025 DIR *subdir = NULL;
1026 int subdirfd = -1;
1028 *next = NULL;
1030 /* Must traverse ignored directories if they contain tracked files. */
1031 if (de->d_type == DT_DIR && ignore &&
1032 have_tracked_file_in_dir(fileindex, path))
1033 ignore = 0;
1035 if (de->d_type == DT_DIR && !ignore) {
1036 char *subpath;
1037 char *subdirpath;
1038 struct got_pathlist_head subdirlist;
1040 TAILQ_INIT(&subdirlist);
1042 if (asprintf(&subpath, "%s%s%s", path,
1043 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1044 return got_error_from_errno("asprintf");
1046 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
1047 free(subpath);
1048 return got_error_from_errno("asprintf");
1051 subdirfd = openat(fd, de->d_name,
1052 O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
1053 if (subdirfd == -1) {
1054 if (errno == EACCES) {
1055 *next = TAILQ_NEXT(dle, entry);
1056 return NULL;
1058 err = got_error_from_errno2("openat", subdirpath);
1059 free(subpath);
1060 free(subdirpath);
1061 return err;
1064 subdir = fdopendir(subdirfd);
1065 if (subdir == NULL)
1066 return got_error_from_errno2("fdopendir", path);
1067 subdirfd = -1;
1068 err = read_dirlist(&subdirlist, subdir, subdirpath);
1069 if (err) {
1070 free(subpath);
1071 free(subdirpath);
1072 closedir(subdir);
1073 return err;
1075 err = diff_fileindex_dir(fileindex, ie, &subdirlist,
1076 dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
1077 if (subdir && closedir(subdir) == -1 && err == NULL)
1078 err = got_error_from_errno2("closedir", subdirpath);
1079 free(subpath);
1080 free(subdirpath);
1081 free_dirlist(&subdirlist);
1082 if (err)
1083 return err;
1086 *next = TAILQ_NEXT(dle, entry);
1087 return NULL;
1090 static const struct got_error *
1091 dirent_type_fixup(struct dirent *de, const char *rootpath, const char *path)
1093 const struct got_error *err;
1094 char *dir_path;
1095 int type;
1097 if (de->d_type != DT_UNKNOWN)
1098 return NULL;
1100 /* DT_UNKNOWN occurs on NFS mounts without "readdir plus" RPC. */
1101 if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
1102 return got_error_from_errno("asprintf");
1103 err = got_path_dirent_type(&type, dir_path, de);
1104 free(dir_path);
1105 if (err)
1106 return err;
1108 de->d_type = type;
1109 return NULL;
1112 static const struct got_error *
1113 diff_fileindex_dir(struct got_fileindex *fileindex,
1114 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1115 int dirfd, const char *rootpath, const char *path,
1116 struct got_repository *repo,
1117 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1119 const struct got_error *err = NULL;
1120 struct dirent *de = NULL;
1121 size_t path_len = strlen(path);
1122 struct got_pathlist_entry *dle;
1123 int ignore;
1125 if (cb->diff_traverse) {
1126 err = cb->diff_traverse(cb_arg, path, dirfd);
1127 if (err)
1128 return err;
1131 dle = TAILQ_FIRST(dirlist);
1132 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1133 if (dle && *ie) {
1134 char *de_path;
1135 int cmp;
1136 de = dle->data;
1137 err = dirent_type_fixup(de, rootpath, path);
1138 if (err)
1139 break;
1140 if (asprintf(&de_path, "%s/%s", path,
1141 de->d_name) == -1) {
1142 err = got_error_from_errno("asprintf");
1143 break;
1145 cmp = got_path_cmp((*ie)->path, de_path,
1146 got_fileindex_entry_path_len(*ie),
1147 strlen(path) + 1 + de->d_namlen);
1148 free(de_path);
1149 if (cmp == 0) {
1150 err = cb->diff_old_new(cb_arg, *ie, de, path,
1151 dirfd);
1152 if (err)
1153 break;
1154 *ie = walk_fileindex(fileindex, *ie);
1155 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1156 path, rootpath, repo, 0, cb, cb_arg);
1157 } else if (cmp < 0 ) {
1158 err = cb->diff_old(cb_arg, *ie, path);
1159 if (err)
1160 break;
1161 *ie = walk_fileindex(fileindex, *ie);
1162 } else {
1163 err = cb->diff_new(&ignore, cb_arg, de, path,
1164 dirfd);
1165 if (err)
1166 break;
1167 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1168 path, rootpath, repo, ignore, cb, cb_arg);
1170 if (err)
1171 break;
1172 } else if (*ie) {
1173 err = cb->diff_old(cb_arg, *ie, path);
1174 if (err)
1175 break;
1176 *ie = walk_fileindex(fileindex, *ie);
1177 } else if (dle) {
1178 de = dle->data;
1179 err = dirent_type_fixup(de, rootpath, path);
1180 if (err)
1181 break;
1182 err = cb->diff_new(&ignore, cb_arg, de, path, dirfd);
1183 if (err)
1184 break;
1185 err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1186 rootpath, repo, ignore, cb, cb_arg);
1187 if (err)
1188 break;
1192 return err;
1195 const struct got_error *
1196 got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1197 const char *rootpath, const char *path, struct got_repository *repo,
1198 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1200 const struct got_error *err;
1201 struct got_fileindex_entry *ie;
1202 struct got_pathlist_head dirlist;
1203 int fd2;
1204 DIR *dir;
1206 TAILQ_INIT(&dirlist);
1209 * Duplicate the file descriptor so we can call closedir() below
1210 * without closing the file descriptor passed in by our caller.
1212 fd2 = dup(fd);
1213 if (fd2 == -1)
1214 return got_error_from_errno2("dup", path);
1215 if (lseek(fd2, 0, SEEK_SET) == -1) {
1216 err = got_error_from_errno2("lseek", path);
1217 close(fd2);
1218 return err;
1220 dir = fdopendir(fd2);
1221 if (dir == NULL) {
1222 err = got_error_from_errno2("fdopendir", path);
1223 close(fd2);
1224 return err;
1226 err = read_dirlist(&dirlist, dir, path);
1227 if (err) {
1228 closedir(dir);
1229 return err;
1232 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1233 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1234 ie = walk_fileindex(fileindex, ie);
1235 err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1236 rootpath, path, repo, cb, cb_arg);
1238 if (closedir(dir) == -1 && err == NULL)
1239 err = got_error_from_errno2("closedir", path);
1240 free_dirlist(&dirlist);
1241 return err;
1244 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);