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
50 struct got_fileindex {
51 struct got_fileindex_tree entries;
52 int nentries; /* Does not include entries marked for removal. */
53 #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
54 };
56 uint16_t
57 got_fileindex_perms_from_st(struct stat *sb)
58 {
59 uint16_t perms = (sb->st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
60 return (perms << GOT_FILEIDX_MODE_PERMS_SHIFT);
61 }
63 mode_t
64 got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
65 {
66 mode_t perms = (ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT);
67 return (S_IFREG | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
68 }
70 const struct got_error *
71 got_fileindex_entry_update(struct got_fileindex_entry *ie,
72 const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
73 int update_timestamps)
74 {
75 struct stat sb;
77 if (lstat(ondisk_path, &sb) != 0) {
78 if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
79 errno == ENOENT))
80 return got_error_from_errno2("lstat", ondisk_path);
81 } else {
82 if (sb.st_mode & S_IFDIR)
83 return got_error_set_errno(EISDIR, ondisk_path);
84 ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
85 }
88 if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
89 if (update_timestamps) {
90 ie->ctime_sec = sb.st_ctime;
91 ie->ctime_nsec = sb.st_ctimensec;
92 ie->mtime_sec = sb.st_mtime;
93 ie->mtime_nsec = sb.st_mtimensec;
94 }
95 ie->uid = sb.st_uid;
96 ie->gid = sb.st_gid;
97 ie->size = (sb.st_size & 0xffffffff);
98 if (S_ISLNK(sb.st_mode))
99 ie->mode = GOT_FILEIDX_MODE_SYMLINK;
100 else {
101 ie->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
102 ie->mode |= got_fileindex_perms_from_st(&sb);
106 if (blob_sha1) {
107 memcpy(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
108 ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
109 } else
110 ie->flags |= GOT_FILEIDX_F_NO_BLOB;
112 if (commit_sha1) {
113 memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
114 ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
115 } else
116 ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
118 return NULL;
121 void
122 got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
124 ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
127 const struct got_error *
128 got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
129 const char *relpath)
131 size_t len;
133 *ie = calloc(1, sizeof(**ie));
134 if (*ie == NULL)
135 return got_error_from_errno("calloc");
137 (*ie)->path = strdup(relpath);
138 if ((*ie)->path == NULL) {
139 const struct got_error *err = got_error_from_errno("strdup");
140 free(*ie);
141 *ie = NULL;
142 return err;
145 len = strlen(relpath);
146 if (len > GOT_FILEIDX_F_PATH_LEN)
147 len = GOT_FILEIDX_F_PATH_LEN;
148 (*ie)->flags |= len;
150 return NULL;
153 void
154 got_fileindex_entry_free(struct got_fileindex_entry *ie)
156 free(ie->path);
157 free(ie);
160 size_t
161 got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
163 return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
166 uint32_t
167 got_fileindex_entry_stage_get(const struct got_fileindex_entry *ie)
169 return ((ie->flags & GOT_FILEIDX_F_STAGE) >> GOT_FILEIDX_F_STAGE_SHIFT);
172 void
173 got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage)
175 ie->flags &= ~GOT_FILEIDX_F_STAGE;
176 ie->flags |= ((stage << GOT_FILEIDX_F_STAGE_SHIFT) &
177 GOT_FILEIDX_F_STAGE);
180 int
181 got_fileindex_entry_filetype_get(struct got_fileindex_entry *ie)
183 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
186 void
187 got_fileindex_entry_filetype_set(struct got_fileindex_entry *ie, int type)
189 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_ONDISK;
190 ie->mode |= (type & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
193 void
194 got_fileindex_entry_staged_filetype_set(struct got_fileindex_entry *ie, int type)
196 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_STAGED;
197 ie->mode |= ((type << GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT) &
198 GOT_FILEIDX_MODE_FILE_TYPE_STAGED);
201 int
202 got_fileindex_entry_staged_filetype_get(struct got_fileindex_entry *ie)
204 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_STAGED) >>
205 GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT;
208 int
209 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
211 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
214 int
215 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
217 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
220 int
221 got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
223 return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
226 static const struct got_error *
227 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
229 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
230 return got_error(GOT_ERR_NO_SPACE);
232 RB_INSERT(got_fileindex_tree, &fileindex->entries, ie);
233 fileindex->nentries++;
234 return NULL;
237 const struct got_error *
238 got_fileindex_entry_add(struct got_fileindex *fileindex,
239 struct got_fileindex_entry *ie)
241 /* Flag this entry until it gets written out to disk. */
242 ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
244 return add_entry(fileindex, ie);
247 void
248 got_fileindex_entry_remove(struct got_fileindex *fileindex,
249 struct got_fileindex_entry *ie)
251 /*
252 * Removing an entry from the RB tree immediately breaks
253 * in-progress iterations over file index entries.
254 * So flag this entry for removal and remove it once the index
255 * is written out to disk. Meanwhile, pretend this entry no longer
256 * exists if we get queried for it again before then.
257 */
258 ie->flags |= GOT_FILEIDX_F_REMOVE_ON_FLUSH;
259 fileindex->nentries--;
262 struct got_fileindex_entry *
263 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
264 size_t path_len)
266 struct got_fileindex_entry *ie;
267 struct got_fileindex_entry key;
268 memset(&key, 0, sizeof(key));
269 key.path = (char *)path;
270 key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
271 ie = RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
272 if (ie && (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH))
273 return NULL;
274 return ie;
277 const struct got_error *
278 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
279 got_fileindex_cb cb, void *cb_arg)
281 const struct got_error *err;
282 struct got_fileindex_entry *ie, *tmp;
284 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
285 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
286 continue;
287 err = (*cb)(cb_arg, ie);
288 if (err)
289 return err;
291 return NULL;
294 struct got_fileindex *
295 got_fileindex_alloc(void)
297 struct got_fileindex *fileindex;
299 fileindex = calloc(1, sizeof(*fileindex));
300 if (fileindex == NULL)
301 return NULL;
303 RB_INIT(&fileindex->entries);
304 return fileindex;
307 void
308 got_fileindex_free(struct got_fileindex *fileindex)
310 struct got_fileindex_entry *ie;
312 while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
313 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
314 got_fileindex_entry_free(ie);
316 free(fileindex);
319 static const struct got_error *
320 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
322 size_t n;
324 val = htobe64(val);
325 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
326 n = fwrite(&val, 1, sizeof(val), outfile);
327 if (n != sizeof(val))
328 return got_ferror(outfile, GOT_ERR_IO);
329 return NULL;
332 static const struct got_error *
333 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
335 size_t n;
337 val = htobe32(val);
338 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
339 n = fwrite(&val, 1, sizeof(val), outfile);
340 if (n != sizeof(val))
341 return got_ferror(outfile, GOT_ERR_IO);
342 return NULL;
345 static const struct got_error *
346 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
348 size_t n;
350 val = htobe16(val);
351 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
352 n = fwrite(&val, 1, sizeof(val), outfile);
353 if (n != sizeof(val))
354 return got_ferror(outfile, GOT_ERR_IO);
355 return NULL;
358 static const struct got_error *
359 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
361 size_t n, len, pad = 0;
362 static const uint8_t zero[8] = { 0 };
364 len = strlen(path);
365 while ((len + pad) % 8 != 0)
366 pad++;
367 if (pad == 0)
368 pad = 8; /* NUL-terminate */
370 SHA1Update(ctx, path, len);
371 n = fwrite(path, 1, len, outfile);
372 if (n != len)
373 return got_ferror(outfile, GOT_ERR_IO);
374 SHA1Update(ctx, zero, pad);
375 n = fwrite(zero, 1, pad, outfile);
376 if (n != pad)
377 return got_ferror(outfile, GOT_ERR_IO);
378 return NULL;
381 static const struct got_error *
382 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
383 FILE *outfile)
385 const struct got_error *err;
386 size_t n;
387 uint32_t stage;
389 err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
390 if (err)
391 return err;
392 err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
393 if (err)
394 return err;
395 err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
396 if (err)
397 return err;
398 err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
399 if (err)
400 return err;
402 err = write_fileindex_val32(ctx, ie->uid, outfile);
403 if (err)
404 return err;
405 err = write_fileindex_val32(ctx, ie->gid, outfile);
406 if (err)
407 return err;
408 err = write_fileindex_val32(ctx, ie->size, outfile);
409 if (err)
410 return err;
412 err = write_fileindex_val16(ctx, ie->mode, outfile);
413 if (err)
414 return err;
416 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
417 n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
418 if (n != SHA1_DIGEST_LENGTH)
419 return got_ferror(outfile, GOT_ERR_IO);
421 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
422 n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
423 if (n != SHA1_DIGEST_LENGTH)
424 return got_ferror(outfile, GOT_ERR_IO);
426 err = write_fileindex_val32(ctx, ie->flags, outfile);
427 if (err)
428 return err;
430 err = write_fileindex_path(ctx, ie->path, outfile);
431 if (err)
432 return err;
434 stage = got_fileindex_entry_stage_get(ie);
435 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
436 stage == GOT_FILEIDX_STAGE_ADD) {
437 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
438 n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
439 outfile);
440 if (n != SHA1_DIGEST_LENGTH)
441 return got_ferror(outfile, GOT_ERR_IO);
444 return NULL;
447 const struct got_error *
448 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
450 const struct got_error *err = NULL;
451 struct got_fileindex_hdr hdr;
452 SHA1_CTX ctx;
453 uint8_t sha1[SHA1_DIGEST_LENGTH];
454 size_t n;
455 struct got_fileindex_entry *ie, *tmp;
457 SHA1Init(&ctx);
459 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
460 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
461 hdr.nentries = htobe32(fileindex->nentries);
463 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
464 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
465 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
466 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
467 if (n != sizeof(hdr.signature))
468 return got_ferror(outfile, GOT_ERR_IO);
469 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
470 if (n != sizeof(hdr.version))
471 return got_ferror(outfile, GOT_ERR_IO);
472 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
473 if (n != sizeof(hdr.nentries))
474 return got_ferror(outfile, GOT_ERR_IO);
476 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
477 ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
478 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH) {
479 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
480 got_fileindex_entry_free(ie);
481 continue;
483 err = write_fileindex_entry(&ctx, ie, outfile);
484 if (err)
485 return err;
488 SHA1Final(sha1, &ctx);
489 n = fwrite(sha1, 1, sizeof(sha1), outfile);
490 if (n != sizeof(sha1))
491 return got_ferror(outfile, GOT_ERR_IO);
493 if (fflush(outfile) != 0)
494 return got_error_from_errno("fflush");
496 return NULL;
499 static const struct got_error *
500 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
502 size_t n;
504 n = fread(val, 1, sizeof(*val), infile);
505 if (n != sizeof(*val))
506 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
507 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
508 *val = be64toh(*val);
509 return NULL;
512 static const struct got_error *
513 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
515 size_t n;
517 n = fread(val, 1, sizeof(*val), infile);
518 if (n != sizeof(*val))
519 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
520 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
521 *val = be32toh(*val);
522 return NULL;
525 static const struct got_error *
526 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
528 size_t n;
530 n = fread(val, 1, sizeof(*val), infile);
531 if (n != sizeof(*val))
532 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
533 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
534 *val = be16toh(*val);
535 return NULL;
538 static const struct got_error *
539 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
541 const struct got_error *err = NULL;
542 const size_t chunk_size = 8;
543 size_t n, len = 0, totlen = chunk_size;
545 *path = malloc(totlen);
546 if (*path == NULL)
547 return got_error_from_errno("malloc");
549 do {
550 if (len + chunk_size > totlen) {
551 char *p = reallocarray(*path, totlen + chunk_size, 1);
552 if (p == NULL) {
553 err = got_error_from_errno("reallocarray");
554 break;
556 totlen += chunk_size;
557 *path = p;
559 n = fread(*path + len, 1, chunk_size, infile);
560 if (n != chunk_size) {
561 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
562 break;
564 SHA1Update(ctx, *path + len, chunk_size);
565 len += chunk_size;
566 } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
568 if (err) {
569 free(*path);
570 *path = NULL;
572 return err;
575 static const struct got_error *
576 read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
577 FILE *infile, uint32_t version)
579 const struct got_error *err;
580 struct got_fileindex_entry *ie;
581 size_t n;
583 *iep = NULL;
585 ie = calloc(1, sizeof(*ie));
586 if (ie == NULL)
587 return got_error_from_errno("calloc");
589 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
590 if (err)
591 goto done;
592 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
593 if (err)
594 goto done;
595 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
596 if (err)
597 goto done;
598 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
599 if (err)
600 goto done;
602 err = read_fileindex_val32(&ie->uid, ctx, infile);
603 if (err)
604 goto done;
605 err = read_fileindex_val32(&ie->gid, ctx, infile);
606 if (err)
607 goto done;
608 err = read_fileindex_val32(&ie->size, ctx, infile);
609 if (err)
610 goto done;
612 err = read_fileindex_val16(&ie->mode, ctx, infile);
613 if (err)
614 goto done;
616 n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
617 if (n != SHA1_DIGEST_LENGTH) {
618 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
619 goto done;
621 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
623 n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
624 if (n != SHA1_DIGEST_LENGTH) {
625 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
626 goto done;
628 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
630 err = read_fileindex_val32(&ie->flags, ctx, infile);
631 if (err)
632 goto done;
634 err = read_fileindex_path(&ie->path, ctx, infile);
635 if (err)
636 goto done;
638 if (version >= 2) {
639 uint32_t stage = got_fileindex_entry_stage_get(ie);
640 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
641 stage == GOT_FILEIDX_STAGE_ADD) {
642 n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
643 infile);
644 if (n != SHA1_DIGEST_LENGTH) {
645 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
646 goto done;
648 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
650 } else {
651 /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
652 ie->flags &= ~GOT_FILEIDX_F_STAGE;
655 done:
656 if (err)
657 got_fileindex_entry_free(ie);
658 else
659 *iep = ie;
660 return err;
663 const struct got_error *
664 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
666 const struct got_error *err = NULL;
667 struct got_fileindex_hdr hdr;
668 SHA1_CTX ctx;
669 struct got_fileindex_entry *ie;
670 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
671 uint8_t sha1[SHA1_DIGEST_LENGTH];
672 size_t n;
673 int i;
675 SHA1Init(&ctx);
677 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
678 if (n != sizeof(hdr.signature)) {
679 if (n == 0) /* EOF */
680 return NULL;
681 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
683 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
684 if (n != sizeof(hdr.version)) {
685 if (n == 0) /* EOF */
686 return NULL;
687 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
689 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
690 if (n != sizeof(hdr.nentries)) {
691 if (n == 0) /* EOF */
692 return NULL;
693 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
696 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
697 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
698 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
700 hdr.signature = be32toh(hdr.signature);
701 hdr.version = be32toh(hdr.version);
702 hdr.nentries = be32toh(hdr.nentries);
704 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
705 return got_error(GOT_ERR_FILEIDX_SIG);
706 if (hdr.version > GOT_FILE_INDEX_VERSION)
707 return got_error(GOT_ERR_FILEIDX_VER);
709 for (i = 0; i < hdr.nentries; i++) {
710 err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
711 if (err)
712 return err;
713 err = add_entry(fileindex, ie);
714 if (err)
715 return err;
718 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
719 if (n != sizeof(sha1_expected))
720 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
721 SHA1Final(sha1, &ctx);
722 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
723 return got_error(GOT_ERR_FILEIDX_CSUM);
725 return NULL;
728 static struct got_fileindex_entry *
729 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
731 struct got_fileindex_entry *next;
733 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
735 /* Skip entries which were added or removed by diff callbacks. */
736 while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
737 GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
738 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
740 return next;
743 static const struct got_error *
744 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
745 struct got_tree_object *tree, const char *, const char *,
746 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
748 static const struct got_error *
749 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
750 struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
751 const char *path, const char *entry_name, struct got_repository *repo,
752 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
754 const struct got_error *err = NULL;
755 struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
757 if (!got_object_tree_entry_is_submodule(te) &&
758 S_ISDIR(got_tree_entry_get_mode(te))) {
759 char *subpath;
760 struct got_tree_object *subtree;
762 if (asprintf(&subpath, "%s%s%s", path,
763 path[0] == '\0' ? "" : "/",
764 got_tree_entry_get_name(te)) == -1)
765 return got_error_from_errno("asprintf");
767 err = got_object_open_as_tree(&subtree, repo,
768 got_tree_entry_get_id(te));
769 if (err) {
770 free(subpath);
771 return err;
774 err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
775 entry_name, repo, cb, cb_arg);
776 free(subpath);
777 got_object_tree_close(subtree);
778 if (err)
779 return err;
782 (*tidx)++;
783 *next = got_object_tree_get_entry(tree, *tidx);
784 return NULL;
787 static const struct got_error *
788 diff_fileindex_tree(struct got_fileindex *fileindex,
789 struct got_fileindex_entry **ie, struct got_tree_object *tree,
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 = NULL;
795 size_t path_len = strlen(path);
796 struct got_fileindex_entry *next;
797 int tidx = 0;
799 te = got_object_tree_get_entry(tree, tidx);
800 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
801 if (te && *ie) {
802 char *te_path;
803 const char *te_name = got_tree_entry_get_name(te);
804 int cmp;
805 if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
806 err = got_error_from_errno("asprintf");
807 break;
809 cmp = got_path_cmp((*ie)->path, te_path,
810 got_fileindex_entry_path_len(*ie), strlen(te_path));
811 free(te_path);
812 if (cmp == 0) {
813 if (got_path_is_child((*ie)->path, path,
814 path_len) &&
815 !got_object_tree_entry_is_submodule(te) &&
816 (entry_name == NULL ||
817 strcmp(te_name, entry_name) == 0)) {
818 err = cb->diff_old_new(cb_arg, *ie, te,
819 path);
820 if (err || entry_name)
821 break;
823 *ie = walk_fileindex(fileindex, *ie);
824 err = walk_tree(&te, fileindex, ie, tree, &tidx,
825 path, entry_name, repo, cb, cb_arg);
826 } else if (cmp < 0) {
827 next = walk_fileindex(fileindex, *ie);
828 if (got_path_is_child((*ie)->path, path,
829 path_len) && (entry_name == NULL ||
830 strcmp(te_name, entry_name) == 0)) {
831 err = cb->diff_old(cb_arg, *ie, path);
832 if (err || entry_name)
833 break;
835 *ie = next;
836 } else {
837 if ((entry_name == NULL ||
838 strcmp(te_name, entry_name) == 0)) {
839 err = cb->diff_new(cb_arg, te, path);
840 if (err || entry_name)
841 break;
843 err = walk_tree(&te, fileindex, ie, tree, &tidx,
844 path, entry_name, repo, cb, cb_arg);
846 if (err)
847 break;
848 } else if (*ie) {
849 next = walk_fileindex(fileindex, *ie);
850 if (got_path_is_child((*ie)->path, path, path_len) &&
851 (entry_name == NULL ||
852 (te && strcmp(got_tree_entry_get_name(te),
853 entry_name) == 0))) {
854 err = cb->diff_old(cb_arg, *ie, path);
855 if (err || entry_name)
856 break;
858 *ie = next;
859 } else if (te) {
860 if (!got_object_tree_entry_is_submodule(te) &&
861 (entry_name == NULL ||
862 strcmp(got_tree_entry_get_name(te), entry_name)
863 == 0)) {
864 err = cb->diff_new(cb_arg, te, path);
865 if (err || entry_name)
866 break;
868 err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
869 entry_name, repo, cb, cb_arg);
870 if (err)
871 break;
875 return err;
878 const struct got_error *
879 got_fileindex_diff_tree(struct got_fileindex *fileindex,
880 struct got_tree_object *tree, const char *path, const char *entry_name,
881 struct got_repository *repo,
882 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
884 struct got_fileindex_entry *ie;
885 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
886 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
887 ie = walk_fileindex(fileindex, ie);
888 return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
889 cb, cb_arg);
892 static const struct got_error *
893 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
894 struct got_pathlist_head *, int, const char *, const char *,
895 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
897 static const struct got_error *
898 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
900 const struct got_error *err = NULL;
901 struct got_pathlist_entry *new = NULL;
902 struct dirent *dep = NULL;
903 struct dirent *de = NULL;
905 for (;;) {
906 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
907 if (de == NULL) {
908 err = got_error_from_errno("malloc");
909 break;
912 if (readdir_r(dir, de, &dep) != 0) {
913 err = got_error_from_errno("readdir_r");
914 free(de);
915 break;
917 if (dep == NULL) {
918 free(de);
919 break;
922 if (strcmp(de->d_name, ".") == 0 ||
923 strcmp(de->d_name, "..") == 0 ||
924 (path[0] == '\0' &&
925 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
926 free(de);
927 continue;
930 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
931 if (err) {
932 free(de);
933 break;
935 if (new == NULL) {
936 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
937 free(de);
938 break;
942 return err;
945 void
946 free_dirlist(struct got_pathlist_head *dirlist)
948 struct got_pathlist_entry *dle;
950 TAILQ_FOREACH(dle, dirlist, entry)
951 free(dle->data);
952 got_pathlist_free(dirlist);
955 static const struct got_error *
956 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
957 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
958 const char *path, const char *rootpath, struct got_repository *repo,
959 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
961 const struct got_error *err = NULL;
962 struct dirent *de = dle->data;
963 DIR *subdir = NULL;
964 int subdirfd = -1;
965 int type;
967 *next = NULL;
969 if (de->d_type == DT_UNKNOWN) {
970 /* Occurs on NFS mounts without "readdir plus" RPC. */
971 char *dir_path;
972 if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
973 return got_error_from_errno("asprintf");
974 err = got_path_dirent_type(&type, dir_path, de);
975 free(dir_path);
976 if (err)
977 return err;
978 } else
979 type = de->d_type;
981 if (type == DT_DIR) {
982 char *subpath;
983 char *subdirpath;
984 struct got_pathlist_head subdirlist;
986 TAILQ_INIT(&subdirlist);
988 if (asprintf(&subpath, "%s%s%s", path,
989 path[0] == '\0' ? "" : "/", de->d_name) == -1)
990 return got_error_from_errno("asprintf");
992 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
993 free(subpath);
994 return got_error_from_errno("asprintf");
997 subdirfd = openat(fd, de->d_name,
998 O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
999 if (subdirfd == -1) {
1000 if (errno == EACCES) {
1001 *next = TAILQ_NEXT(dle, entry);
1002 return NULL;
1004 err = got_error_from_errno2("openat", subdirpath);
1005 free(subpath);
1006 free(subdirpath);
1007 return err;
1010 subdir = fdopendir(subdirfd);
1011 if (subdir == NULL)
1012 return got_error_from_errno2("fdopendir", path);
1013 subdirfd = -1;
1014 err = read_dirlist(&subdirlist, subdir, subdirpath);
1015 if (err) {
1016 free(subpath);
1017 free(subdirpath);
1018 closedir(subdir);
1019 return err;
1021 err = diff_fileindex_dir(fileindex, ie, &subdirlist,
1022 dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
1023 if (subdir && closedir(subdir) == -1 && err == NULL)
1024 err = got_error_from_errno2("closedir", subdirpath);
1025 free(subpath);
1026 free(subdirpath);
1027 free_dirlist(&subdirlist);
1028 if (err)
1029 return err;
1032 *next = TAILQ_NEXT(dle, entry);
1033 return NULL;
1036 static const struct got_error *
1037 diff_fileindex_dir(struct got_fileindex *fileindex,
1038 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1039 int dirfd, const char *rootpath, const char *path,
1040 struct got_repository *repo,
1041 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1043 const struct got_error *err = NULL;
1044 struct dirent *de = NULL;
1045 size_t path_len = strlen(path);
1046 struct got_pathlist_entry *dle;
1048 if (cb->diff_traverse) {
1049 err = cb->diff_traverse(cb_arg, path, dirfd);
1050 if (err)
1051 return err;
1054 dle = TAILQ_FIRST(dirlist);
1055 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1056 if (dle && *ie) {
1057 char *de_path;
1058 int cmp;
1059 de = dle->data;
1060 if (asprintf(&de_path, "%s/%s", path,
1061 de->d_name) == -1) {
1062 err = got_error_from_errno("asprintf");
1063 break;
1065 cmp = got_path_cmp((*ie)->path, de_path,
1066 got_fileindex_entry_path_len(*ie),
1067 strlen(path) + 1 + de->d_namlen);
1068 free(de_path);
1069 if (cmp == 0) {
1070 err = cb->diff_old_new(cb_arg, *ie, de, path,
1071 dirfd);
1072 if (err)
1073 break;
1074 *ie = walk_fileindex(fileindex, *ie);
1075 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1076 path, rootpath, repo, cb, cb_arg);
1077 } else if (cmp < 0 ) {
1078 err = cb->diff_old(cb_arg, *ie, path);
1079 if (err)
1080 break;
1081 *ie = walk_fileindex(fileindex, *ie);
1082 } else {
1083 err = cb->diff_new(cb_arg, de, path, dirfd);
1084 if (err)
1085 break;
1086 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1087 path, rootpath, repo, cb, cb_arg);
1089 if (err)
1090 break;
1091 } else if (*ie) {
1092 err = cb->diff_old(cb_arg, *ie, path);
1093 if (err)
1094 break;
1095 *ie = walk_fileindex(fileindex, *ie);
1096 } else if (dle) {
1097 de = dle->data;
1098 err = cb->diff_new(cb_arg, de, path, dirfd);
1099 if (err)
1100 break;
1101 err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1102 rootpath, repo, cb, cb_arg);
1103 if (err)
1104 break;
1108 return err;
1111 const struct got_error *
1112 got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1113 const char *rootpath, const char *path, struct got_repository *repo,
1114 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1116 const struct got_error *err;
1117 struct got_fileindex_entry *ie;
1118 struct got_pathlist_head dirlist;
1119 int fd2;
1120 DIR *dir;
1122 TAILQ_INIT(&dirlist);
1125 * Duplicate the file descriptor so we can call closedir() below
1126 * without closing the file descriptor passed in by our caller.
1128 fd2 = dup(fd);
1129 if (fd2 == -1)
1130 return got_error_from_errno2("dup", path);
1131 if (lseek(fd2, 0, SEEK_SET) == -1) {
1132 err = got_error_from_errno2("lseek", path);
1133 close(fd2);
1134 return err;
1136 dir = fdopendir(fd2);
1137 if (dir == NULL) {
1138 err = got_error_from_errno2("fdopendir", path);
1139 close(fd2);
1140 return err;
1142 err = read_dirlist(&dirlist, dir, path);
1143 if (err) {
1144 closedir(dir);
1145 return err;
1148 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1149 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1150 ie = walk_fileindex(fileindex, ie);
1151 err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1152 rootpath, path, repo, cb, cb_arg);
1154 if (closedir(dir) == -1 && err == NULL)
1155 err = got_error_from_errno2("closedir", path);
1156 free_dirlist(&dirlist);
1157 return err;
1160 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);