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 mode_t
57 got_fileindex_entry_perms_get(struct got_fileindex_entry *ie)
58 {
59 return ((ie->mode & GOT_FILEIDX_MODE_PERMS) >>
60 GOT_FILEIDX_MODE_PERMS_SHIFT);
61 }
63 static void
64 fileindex_entry_perms_set(struct got_fileindex_entry *ie, mode_t mode)
65 {
66 ie->mode &= ~GOT_FILEIDX_MODE_PERMS;
67 ie->mode |= ((mode << GOT_FILEIDX_MODE_PERMS_SHIFT) &
68 GOT_FILEIDX_MODE_PERMS);
69 }
71 mode_t
72 got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
73 {
74 mode_t perms = got_fileindex_entry_perms_get(ie);
75 int type = got_fileindex_entry_filetype_get(ie);
76 uint32_t ftype;
78 if (type == GOT_FILEIDX_MODE_REGULAR_FILE ||
79 type == GOT_FILEIDX_MODE_BAD_SYMLINK)
80 ftype = S_IFREG;
81 else
82 ftype = S_IFLNK;
84 return (ftype | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
85 }
87 const struct got_error *
88 got_fileindex_entry_update(struct got_fileindex_entry *ie,
89 const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
90 int update_timestamps)
91 {
92 struct stat sb;
94 if (lstat(ondisk_path, &sb) != 0) {
95 if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
96 errno == ENOENT))
97 return got_error_from_errno2("lstat", ondisk_path);
98 } else {
99 if (sb.st_mode & S_IFDIR)
100 return got_error_set_errno(EISDIR, ondisk_path);
101 ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
105 if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
106 if (update_timestamps) {
107 ie->ctime_sec = sb.st_ctime;
108 ie->ctime_nsec = sb.st_ctimensec;
109 ie->mtime_sec = sb.st_mtime;
110 ie->mtime_nsec = sb.st_mtimensec;
112 ie->uid = sb.st_uid;
113 ie->gid = sb.st_gid;
114 ie->size = (sb.st_size & 0xffffffff);
115 if (S_ISLNK(sb.st_mode)) {
116 got_fileindex_entry_filetype_set(ie,
117 GOT_FILEIDX_MODE_SYMLINK);
118 fileindex_entry_perms_set(ie, 0);
119 } else {
120 got_fileindex_entry_filetype_set(ie,
121 GOT_FILEIDX_MODE_REGULAR_FILE);
122 fileindex_entry_perms_set(ie,
123 sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
127 if (blob_sha1) {
128 memcpy(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
129 ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
130 } else
131 ie->flags |= GOT_FILEIDX_F_NO_BLOB;
133 if (commit_sha1) {
134 memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
135 ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
136 } else
137 ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
139 return NULL;
142 void
143 got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
145 ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
148 const struct got_error *
149 got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
150 const char *relpath)
152 size_t len;
154 *ie = calloc(1, sizeof(**ie));
155 if (*ie == NULL)
156 return got_error_from_errno("calloc");
158 (*ie)->path = strdup(relpath);
159 if ((*ie)->path == NULL) {
160 const struct got_error *err = got_error_from_errno("strdup");
161 free(*ie);
162 *ie = NULL;
163 return err;
166 len = strlen(relpath);
167 if (len > GOT_FILEIDX_F_PATH_LEN)
168 len = GOT_FILEIDX_F_PATH_LEN;
169 (*ie)->flags |= len;
171 return NULL;
174 void
175 got_fileindex_entry_free(struct got_fileindex_entry *ie)
177 free(ie->path);
178 free(ie);
181 size_t
182 got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
184 return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
187 uint32_t
188 got_fileindex_entry_stage_get(const struct got_fileindex_entry *ie)
190 return ((ie->flags & GOT_FILEIDX_F_STAGE) >> GOT_FILEIDX_F_STAGE_SHIFT);
193 void
194 got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage)
196 ie->flags &= ~GOT_FILEIDX_F_STAGE;
197 ie->flags |= ((stage << GOT_FILEIDX_F_STAGE_SHIFT) &
198 GOT_FILEIDX_F_STAGE);
201 int
202 got_fileindex_entry_filetype_get(struct got_fileindex_entry *ie)
204 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
207 void
208 got_fileindex_entry_filetype_set(struct got_fileindex_entry *ie, int type)
210 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_ONDISK;
211 ie->mode |= (type & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
214 void
215 got_fileindex_entry_staged_filetype_set(struct got_fileindex_entry *ie, int type)
217 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_STAGED;
218 ie->mode |= ((type << GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT) &
219 GOT_FILEIDX_MODE_FILE_TYPE_STAGED);
222 int
223 got_fileindex_entry_staged_filetype_get(struct got_fileindex_entry *ie)
225 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_STAGED) >>
226 GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT;
229 int
230 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
232 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
235 int
236 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
238 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
241 int
242 got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
244 return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
247 static const struct got_error *
248 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
250 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
251 return got_error(GOT_ERR_NO_SPACE);
253 RB_INSERT(got_fileindex_tree, &fileindex->entries, ie);
254 fileindex->nentries++;
255 return NULL;
258 const struct got_error *
259 got_fileindex_entry_add(struct got_fileindex *fileindex,
260 struct got_fileindex_entry *ie)
262 /* Flag this entry until it gets written out to disk. */
263 ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
265 return add_entry(fileindex, ie);
268 void
269 got_fileindex_entry_remove(struct got_fileindex *fileindex,
270 struct got_fileindex_entry *ie)
272 /*
273 * Removing an entry from the RB tree immediately breaks
274 * in-progress iterations over file index entries.
275 * So flag this entry for removal and remove it once the index
276 * is written out to disk. Meanwhile, pretend this entry no longer
277 * exists if we get queried for it again before then.
278 */
279 ie->flags |= GOT_FILEIDX_F_REMOVE_ON_FLUSH;
280 fileindex->nentries--;
283 struct got_fileindex_entry *
284 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
285 size_t path_len)
287 struct got_fileindex_entry *ie;
288 struct got_fileindex_entry key;
289 memset(&key, 0, sizeof(key));
290 key.path = (char *)path;
291 key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
292 ie = RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
293 if (ie && (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH))
294 return NULL;
295 return ie;
298 const struct got_error *
299 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
300 got_fileindex_cb cb, void *cb_arg)
302 const struct got_error *err;
303 struct got_fileindex_entry *ie, *tmp;
305 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
306 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
307 continue;
308 err = (*cb)(cb_arg, ie);
309 if (err)
310 return err;
312 return NULL;
315 struct got_fileindex *
316 got_fileindex_alloc(void)
318 struct got_fileindex *fileindex;
320 fileindex = calloc(1, sizeof(*fileindex));
321 if (fileindex == NULL)
322 return NULL;
324 RB_INIT(&fileindex->entries);
325 return fileindex;
328 void
329 got_fileindex_free(struct got_fileindex *fileindex)
331 struct got_fileindex_entry *ie;
333 while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
334 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
335 got_fileindex_entry_free(ie);
337 free(fileindex);
340 static const struct got_error *
341 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
343 size_t n;
345 val = htobe64(val);
346 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
347 n = fwrite(&val, 1, sizeof(val), outfile);
348 if (n != sizeof(val))
349 return got_ferror(outfile, GOT_ERR_IO);
350 return NULL;
353 static const struct got_error *
354 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
356 size_t n;
358 val = htobe32(val);
359 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
360 n = fwrite(&val, 1, sizeof(val), outfile);
361 if (n != sizeof(val))
362 return got_ferror(outfile, GOT_ERR_IO);
363 return NULL;
366 static const struct got_error *
367 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
369 size_t n;
371 val = htobe16(val);
372 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
373 n = fwrite(&val, 1, sizeof(val), outfile);
374 if (n != sizeof(val))
375 return got_ferror(outfile, GOT_ERR_IO);
376 return NULL;
379 static const struct got_error *
380 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
382 size_t n, len, pad = 0;
383 static const uint8_t zero[8] = { 0 };
385 len = strlen(path);
386 while ((len + pad) % 8 != 0)
387 pad++;
388 if (pad == 0)
389 pad = 8; /* NUL-terminate */
391 SHA1Update(ctx, path, len);
392 n = fwrite(path, 1, len, outfile);
393 if (n != len)
394 return got_ferror(outfile, GOT_ERR_IO);
395 SHA1Update(ctx, zero, pad);
396 n = fwrite(zero, 1, pad, outfile);
397 if (n != pad)
398 return got_ferror(outfile, GOT_ERR_IO);
399 return NULL;
402 static const struct got_error *
403 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
404 FILE *outfile)
406 const struct got_error *err;
407 size_t n;
408 uint32_t stage;
410 err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
411 if (err)
412 return err;
413 err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
414 if (err)
415 return err;
416 err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
417 if (err)
418 return err;
419 err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
420 if (err)
421 return err;
423 err = write_fileindex_val32(ctx, ie->uid, outfile);
424 if (err)
425 return err;
426 err = write_fileindex_val32(ctx, ie->gid, outfile);
427 if (err)
428 return err;
429 err = write_fileindex_val32(ctx, ie->size, outfile);
430 if (err)
431 return err;
433 err = write_fileindex_val16(ctx, ie->mode, outfile);
434 if (err)
435 return err;
437 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
438 n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
439 if (n != SHA1_DIGEST_LENGTH)
440 return got_ferror(outfile, GOT_ERR_IO);
442 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
443 n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
444 if (n != SHA1_DIGEST_LENGTH)
445 return got_ferror(outfile, GOT_ERR_IO);
447 err = write_fileindex_val32(ctx, ie->flags, outfile);
448 if (err)
449 return err;
451 err = write_fileindex_path(ctx, ie->path, outfile);
452 if (err)
453 return err;
455 stage = got_fileindex_entry_stage_get(ie);
456 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
457 stage == GOT_FILEIDX_STAGE_ADD) {
458 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
459 n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
460 outfile);
461 if (n != SHA1_DIGEST_LENGTH)
462 return got_ferror(outfile, GOT_ERR_IO);
465 return NULL;
468 const struct got_error *
469 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
471 const struct got_error *err = NULL;
472 struct got_fileindex_hdr hdr;
473 SHA1_CTX ctx;
474 uint8_t sha1[SHA1_DIGEST_LENGTH];
475 size_t n;
476 struct got_fileindex_entry *ie, *tmp;
478 SHA1Init(&ctx);
480 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
481 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
482 hdr.nentries = htobe32(fileindex->nentries);
484 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
485 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
486 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
487 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
488 if (n != sizeof(hdr.signature))
489 return got_ferror(outfile, GOT_ERR_IO);
490 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
491 if (n != sizeof(hdr.version))
492 return got_ferror(outfile, GOT_ERR_IO);
493 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
494 if (n != sizeof(hdr.nentries))
495 return got_ferror(outfile, GOT_ERR_IO);
497 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
498 ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
499 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH) {
500 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
501 got_fileindex_entry_free(ie);
502 continue;
504 err = write_fileindex_entry(&ctx, ie, outfile);
505 if (err)
506 return err;
509 SHA1Final(sha1, &ctx);
510 n = fwrite(sha1, 1, sizeof(sha1), outfile);
511 if (n != sizeof(sha1))
512 return got_ferror(outfile, GOT_ERR_IO);
514 if (fflush(outfile) != 0)
515 return got_error_from_errno("fflush");
517 return NULL;
520 static const struct got_error *
521 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
523 size_t n;
525 n = fread(val, 1, sizeof(*val), infile);
526 if (n != sizeof(*val))
527 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
528 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
529 *val = be64toh(*val);
530 return NULL;
533 static const struct got_error *
534 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
536 size_t n;
538 n = fread(val, 1, sizeof(*val), infile);
539 if (n != sizeof(*val))
540 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
541 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
542 *val = be32toh(*val);
543 return NULL;
546 static const struct got_error *
547 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
549 size_t n;
551 n = fread(val, 1, sizeof(*val), infile);
552 if (n != sizeof(*val))
553 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
554 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
555 *val = be16toh(*val);
556 return NULL;
559 static const struct got_error *
560 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
562 const struct got_error *err = NULL;
563 const size_t chunk_size = 8;
564 size_t n, len = 0, totlen = chunk_size;
566 *path = malloc(totlen);
567 if (*path == NULL)
568 return got_error_from_errno("malloc");
570 do {
571 if (len + chunk_size > totlen) {
572 char *p = reallocarray(*path, totlen + chunk_size, 1);
573 if (p == NULL) {
574 err = got_error_from_errno("reallocarray");
575 break;
577 totlen += chunk_size;
578 *path = p;
580 n = fread(*path + len, 1, chunk_size, infile);
581 if (n != chunk_size) {
582 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
583 break;
585 SHA1Update(ctx, *path + len, chunk_size);
586 len += chunk_size;
587 } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
589 if (err) {
590 free(*path);
591 *path = NULL;
593 return err;
596 static const struct got_error *
597 read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
598 FILE *infile, uint32_t version)
600 const struct got_error *err;
601 struct got_fileindex_entry *ie;
602 size_t n;
604 *iep = NULL;
606 ie = calloc(1, sizeof(*ie));
607 if (ie == NULL)
608 return got_error_from_errno("calloc");
610 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
611 if (err)
612 goto done;
613 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
614 if (err)
615 goto done;
616 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
617 if (err)
618 goto done;
619 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
620 if (err)
621 goto done;
623 err = read_fileindex_val32(&ie->uid, ctx, infile);
624 if (err)
625 goto done;
626 err = read_fileindex_val32(&ie->gid, ctx, infile);
627 if (err)
628 goto done;
629 err = read_fileindex_val32(&ie->size, ctx, infile);
630 if (err)
631 goto done;
633 err = read_fileindex_val16(&ie->mode, ctx, infile);
634 if (err)
635 goto done;
637 n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
638 if (n != SHA1_DIGEST_LENGTH) {
639 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
640 goto done;
642 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
644 n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
645 if (n != SHA1_DIGEST_LENGTH) {
646 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
647 goto done;
649 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
651 err = read_fileindex_val32(&ie->flags, ctx, infile);
652 if (err)
653 goto done;
655 err = read_fileindex_path(&ie->path, ctx, infile);
656 if (err)
657 goto done;
659 if (version >= 2) {
660 uint32_t stage = got_fileindex_entry_stage_get(ie);
661 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
662 stage == GOT_FILEIDX_STAGE_ADD) {
663 n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
664 infile);
665 if (n != SHA1_DIGEST_LENGTH) {
666 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
667 goto done;
669 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
671 } else {
672 /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
673 ie->flags &= ~GOT_FILEIDX_F_STAGE;
676 done:
677 if (err)
678 got_fileindex_entry_free(ie);
679 else
680 *iep = ie;
681 return err;
684 const struct got_error *
685 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
687 const struct got_error *err = NULL;
688 struct got_fileindex_hdr hdr;
689 SHA1_CTX ctx;
690 struct got_fileindex_entry *ie;
691 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
692 uint8_t sha1[SHA1_DIGEST_LENGTH];
693 size_t n;
694 int i;
696 SHA1Init(&ctx);
698 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
699 if (n != sizeof(hdr.signature)) {
700 if (n == 0) /* EOF */
701 return NULL;
702 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
704 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
705 if (n != sizeof(hdr.version)) {
706 if (n == 0) /* EOF */
707 return NULL;
708 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
710 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
711 if (n != sizeof(hdr.nentries)) {
712 if (n == 0) /* EOF */
713 return NULL;
714 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
717 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
718 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
719 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
721 hdr.signature = be32toh(hdr.signature);
722 hdr.version = be32toh(hdr.version);
723 hdr.nentries = be32toh(hdr.nentries);
725 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
726 return got_error(GOT_ERR_FILEIDX_SIG);
727 if (hdr.version > GOT_FILE_INDEX_VERSION)
728 return got_error(GOT_ERR_FILEIDX_VER);
730 for (i = 0; i < hdr.nentries; i++) {
731 err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
732 if (err)
733 return err;
734 err = add_entry(fileindex, ie);
735 if (err)
736 return err;
739 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
740 if (n != sizeof(sha1_expected))
741 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
742 SHA1Final(sha1, &ctx);
743 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
744 return got_error(GOT_ERR_FILEIDX_CSUM);
746 return NULL;
749 static struct got_fileindex_entry *
750 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
752 struct got_fileindex_entry *next;
754 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
756 /* Skip entries which were added or removed by diff callbacks. */
757 while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
758 GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
759 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
761 return next;
764 static const struct got_error *
765 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
766 struct got_tree_object *tree, const char *, const char *,
767 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
769 static const struct got_error *
770 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
771 struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
772 const char *path, const char *entry_name, struct got_repository *repo,
773 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
775 const struct got_error *err = NULL;
776 struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
778 if (!got_object_tree_entry_is_submodule(te) &&
779 S_ISDIR(got_tree_entry_get_mode(te))) {
780 char *subpath;
781 struct got_tree_object *subtree;
783 if (asprintf(&subpath, "%s%s%s", path,
784 path[0] == '\0' ? "" : "/",
785 got_tree_entry_get_name(te)) == -1)
786 return got_error_from_errno("asprintf");
788 err = got_object_open_as_tree(&subtree, repo,
789 got_tree_entry_get_id(te));
790 if (err) {
791 free(subpath);
792 return err;
795 err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
796 entry_name, repo, cb, cb_arg);
797 free(subpath);
798 got_object_tree_close(subtree);
799 if (err)
800 return err;
803 (*tidx)++;
804 *next = got_object_tree_get_entry(tree, *tidx);
805 return NULL;
808 static const struct got_error *
809 diff_fileindex_tree(struct got_fileindex *fileindex,
810 struct got_fileindex_entry **ie, struct got_tree_object *tree,
811 const char *path, const char *entry_name, struct got_repository *repo,
812 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
814 const struct got_error *err = NULL;
815 struct got_tree_entry *te = NULL;
816 size_t path_len = strlen(path);
817 struct got_fileindex_entry *next;
818 int tidx = 0;
820 te = got_object_tree_get_entry(tree, tidx);
821 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
822 if (te && *ie) {
823 char *te_path;
824 const char *te_name = got_tree_entry_get_name(te);
825 int cmp;
826 if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
827 err = got_error_from_errno("asprintf");
828 break;
830 cmp = got_path_cmp((*ie)->path, te_path,
831 got_fileindex_entry_path_len(*ie), strlen(te_path));
832 free(te_path);
833 if (cmp == 0) {
834 if (got_path_is_child((*ie)->path, path,
835 path_len) &&
836 !got_object_tree_entry_is_submodule(te) &&
837 (entry_name == NULL ||
838 strcmp(te_name, entry_name) == 0)) {
839 err = cb->diff_old_new(cb_arg, *ie, te,
840 path);
841 if (err || entry_name)
842 break;
844 *ie = walk_fileindex(fileindex, *ie);
845 err = walk_tree(&te, fileindex, ie, tree, &tidx,
846 path, entry_name, repo, cb, cb_arg);
847 } else if (cmp < 0) {
848 next = walk_fileindex(fileindex, *ie);
849 if (got_path_is_child((*ie)->path, path,
850 path_len) && (entry_name == NULL ||
851 strcmp(te_name, entry_name) == 0)) {
852 err = cb->diff_old(cb_arg, *ie, path);
853 if (err || entry_name)
854 break;
856 *ie = next;
857 } else {
858 if ((entry_name == NULL ||
859 strcmp(te_name, entry_name) == 0)) {
860 err = cb->diff_new(cb_arg, te, path);
861 if (err || entry_name)
862 break;
864 err = walk_tree(&te, fileindex, ie, tree, &tidx,
865 path, entry_name, repo, cb, cb_arg);
867 if (err)
868 break;
869 } else if (*ie) {
870 next = walk_fileindex(fileindex, *ie);
871 if (got_path_is_child((*ie)->path, path, path_len) &&
872 (entry_name == NULL ||
873 (te && strcmp(got_tree_entry_get_name(te),
874 entry_name) == 0))) {
875 err = cb->diff_old(cb_arg, *ie, path);
876 if (err || entry_name)
877 break;
879 *ie = next;
880 } else if (te) {
881 if (!got_object_tree_entry_is_submodule(te) &&
882 (entry_name == NULL ||
883 strcmp(got_tree_entry_get_name(te), entry_name)
884 == 0)) {
885 err = cb->diff_new(cb_arg, te, path);
886 if (err || entry_name)
887 break;
889 err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
890 entry_name, repo, cb, cb_arg);
891 if (err)
892 break;
896 return err;
899 const struct got_error *
900 got_fileindex_diff_tree(struct got_fileindex *fileindex,
901 struct got_tree_object *tree, const char *path, const char *entry_name,
902 struct got_repository *repo,
903 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
905 struct got_fileindex_entry *ie;
906 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
907 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
908 ie = walk_fileindex(fileindex, ie);
909 return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
910 cb, cb_arg);
913 static const struct got_error *
914 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
915 struct got_pathlist_head *, int, const char *, const char *,
916 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
918 static const struct got_error *
919 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
921 const struct got_error *err = NULL;
922 struct got_pathlist_entry *new = NULL;
923 struct dirent *dep = NULL;
924 struct dirent *de = NULL;
926 for (;;) {
927 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
928 if (de == NULL) {
929 err = got_error_from_errno("malloc");
930 break;
933 if (readdir_r(dir, de, &dep) != 0) {
934 err = got_error_from_errno("readdir_r");
935 free(de);
936 break;
938 if (dep == NULL) {
939 free(de);
940 break;
943 if (strcmp(de->d_name, ".") == 0 ||
944 strcmp(de->d_name, "..") == 0 ||
945 (path[0] == '\0' &&
946 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
947 free(de);
948 continue;
951 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
952 if (err) {
953 free(de);
954 break;
956 if (new == NULL) {
957 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
958 free(de);
959 break;
963 return err;
966 void
967 free_dirlist(struct got_pathlist_head *dirlist)
969 struct got_pathlist_entry *dle;
971 TAILQ_FOREACH(dle, dirlist, entry)
972 free(dle->data);
973 got_pathlist_free(dirlist);
976 static const struct got_error *
977 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
978 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
979 const char *path, const char *rootpath, struct got_repository *repo,
980 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
982 const struct got_error *err = NULL;
983 struct dirent *de = dle->data;
984 DIR *subdir = NULL;
985 int subdirfd = -1;
986 int type;
988 *next = NULL;
990 if (de->d_type == DT_UNKNOWN) {
991 /* Occurs on NFS mounts without "readdir plus" RPC. */
992 char *dir_path;
993 if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
994 return got_error_from_errno("asprintf");
995 err = got_path_dirent_type(&type, dir_path, de);
996 free(dir_path);
997 if (err)
998 return err;
999 } else
1000 type = de->d_type;
1002 if (type == DT_DIR) {
1003 char *subpath;
1004 char *subdirpath;
1005 struct got_pathlist_head subdirlist;
1007 TAILQ_INIT(&subdirlist);
1009 if (asprintf(&subpath, "%s%s%s", path,
1010 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1011 return got_error_from_errno("asprintf");
1013 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
1014 free(subpath);
1015 return got_error_from_errno("asprintf");
1018 subdirfd = openat(fd, de->d_name,
1019 O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
1020 if (subdirfd == -1) {
1021 if (errno == EACCES) {
1022 *next = TAILQ_NEXT(dle, entry);
1023 return NULL;
1025 err = got_error_from_errno2("openat", subdirpath);
1026 free(subpath);
1027 free(subdirpath);
1028 return err;
1031 subdir = fdopendir(subdirfd);
1032 if (subdir == NULL)
1033 return got_error_from_errno2("fdopendir", path);
1034 subdirfd = -1;
1035 err = read_dirlist(&subdirlist, subdir, subdirpath);
1036 if (err) {
1037 free(subpath);
1038 free(subdirpath);
1039 closedir(subdir);
1040 return err;
1042 err = diff_fileindex_dir(fileindex, ie, &subdirlist,
1043 dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
1044 if (subdir && closedir(subdir) == -1 && err == NULL)
1045 err = got_error_from_errno2("closedir", subdirpath);
1046 free(subpath);
1047 free(subdirpath);
1048 free_dirlist(&subdirlist);
1049 if (err)
1050 return err;
1053 *next = TAILQ_NEXT(dle, entry);
1054 return NULL;
1057 static const struct got_error *
1058 diff_fileindex_dir(struct got_fileindex *fileindex,
1059 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1060 int dirfd, const char *rootpath, const char *path,
1061 struct got_repository *repo,
1062 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1064 const struct got_error *err = NULL;
1065 struct dirent *de = NULL;
1066 size_t path_len = strlen(path);
1067 struct got_pathlist_entry *dle;
1069 if (cb->diff_traverse) {
1070 err = cb->diff_traverse(cb_arg, path, dirfd);
1071 if (err)
1072 return err;
1075 dle = TAILQ_FIRST(dirlist);
1076 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1077 if (dle && *ie) {
1078 char *de_path;
1079 int cmp;
1080 de = dle->data;
1081 if (asprintf(&de_path, "%s/%s", path,
1082 de->d_name) == -1) {
1083 err = got_error_from_errno("asprintf");
1084 break;
1086 cmp = got_path_cmp((*ie)->path, de_path,
1087 got_fileindex_entry_path_len(*ie),
1088 strlen(path) + 1 + de->d_namlen);
1089 free(de_path);
1090 if (cmp == 0) {
1091 err = cb->diff_old_new(cb_arg, *ie, de, path,
1092 dirfd);
1093 if (err)
1094 break;
1095 *ie = walk_fileindex(fileindex, *ie);
1096 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1097 path, rootpath, repo, cb, cb_arg);
1098 } else if (cmp < 0 ) {
1099 err = cb->diff_old(cb_arg, *ie, path);
1100 if (err)
1101 break;
1102 *ie = walk_fileindex(fileindex, *ie);
1103 } else {
1104 err = cb->diff_new(cb_arg, de, path, dirfd);
1105 if (err)
1106 break;
1107 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1108 path, rootpath, repo, cb, cb_arg);
1110 if (err)
1111 break;
1112 } else if (*ie) {
1113 err = cb->diff_old(cb_arg, *ie, path);
1114 if (err)
1115 break;
1116 *ie = walk_fileindex(fileindex, *ie);
1117 } else if (dle) {
1118 de = dle->data;
1119 err = cb->diff_new(cb_arg, de, path, dirfd);
1120 if (err)
1121 break;
1122 err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1123 rootpath, repo, cb, cb_arg);
1124 if (err)
1125 break;
1129 return err;
1132 const struct got_error *
1133 got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1134 const char *rootpath, const char *path, struct got_repository *repo,
1135 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1137 const struct got_error *err;
1138 struct got_fileindex_entry *ie;
1139 struct got_pathlist_head dirlist;
1140 int fd2;
1141 DIR *dir;
1143 TAILQ_INIT(&dirlist);
1146 * Duplicate the file descriptor so we can call closedir() below
1147 * without closing the file descriptor passed in by our caller.
1149 fd2 = dup(fd);
1150 if (fd2 == -1)
1151 return got_error_from_errno2("dup", path);
1152 if (lseek(fd2, 0, SEEK_SET) == -1) {
1153 err = got_error_from_errno2("lseek", path);
1154 close(fd2);
1155 return err;
1157 dir = fdopendir(fd2);
1158 if (dir == NULL) {
1159 err = got_error_from_errno2("fdopendir", path);
1160 close(fd2);
1161 return err;
1163 err = read_dirlist(&dirlist, dir, path);
1164 if (err) {
1165 closedir(dir);
1166 return err;
1169 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1170 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1171 ie = walk_fileindex(fileindex, ie);
1172 err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1173 rootpath, path, repo, cb, cb_arg);
1175 if (closedir(dir) == -1 && err == NULL)
1176 err = got_error_from_errno2("closedir", path);
1177 free_dirlist(&dirlist);
1178 return err;
1181 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);