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);
186 const struct got_error *
187 got_fileindex_entry_filetype_set(struct got_fileindex_entry *ie, int type)
189 switch (type) {
190 case GOT_FILEIDX_MODE_REGULAR_FILE:
191 case GOT_FILEIDX_MODE_SYMLINK:
192 case GOT_FILEIDX_MODE_BAD_SYMLINK:
193 break;
194 default:
195 return got_error(GOT_ERR_BAD_FILETYPE);
198 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE;
199 ie->mode |= type;
200 return NULL;
203 int
204 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
206 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
209 int
210 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
212 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
215 int
216 got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
218 return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
221 static const struct got_error *
222 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
224 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
225 return got_error(GOT_ERR_NO_SPACE);
227 RB_INSERT(got_fileindex_tree, &fileindex->entries, ie);
228 fileindex->nentries++;
229 return NULL;
232 const struct got_error *
233 got_fileindex_entry_add(struct got_fileindex *fileindex,
234 struct got_fileindex_entry *ie)
236 /* Flag this entry until it gets written out to disk. */
237 ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
239 return add_entry(fileindex, ie);
242 void
243 got_fileindex_entry_remove(struct got_fileindex *fileindex,
244 struct got_fileindex_entry *ie)
246 /*
247 * Removing an entry from the RB tree immediately breaks
248 * in-progress iterations over file index entries.
249 * So flag this entry for removal and remove it once the index
250 * is written out to disk. Meanwhile, pretend this entry no longer
251 * exists if we get queried for it again before then.
252 */
253 ie->flags |= GOT_FILEIDX_F_REMOVE_ON_FLUSH;
254 fileindex->nentries--;
257 struct got_fileindex_entry *
258 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
259 size_t path_len)
261 struct got_fileindex_entry *ie;
262 struct got_fileindex_entry key;
263 memset(&key, 0, sizeof(key));
264 key.path = (char *)path;
265 key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
266 ie = RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
267 if (ie && (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH))
268 return NULL;
269 return ie;
272 const struct got_error *
273 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
274 got_fileindex_cb cb, void *cb_arg)
276 const struct got_error *err;
277 struct got_fileindex_entry *ie, *tmp;
279 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
280 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
281 continue;
282 err = (*cb)(cb_arg, ie);
283 if (err)
284 return err;
286 return NULL;
289 struct got_fileindex *
290 got_fileindex_alloc(void)
292 struct got_fileindex *fileindex;
294 fileindex = calloc(1, sizeof(*fileindex));
295 if (fileindex == NULL)
296 return NULL;
298 RB_INIT(&fileindex->entries);
299 return fileindex;
302 void
303 got_fileindex_free(struct got_fileindex *fileindex)
305 struct got_fileindex_entry *ie;
307 while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
308 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
309 got_fileindex_entry_free(ie);
311 free(fileindex);
314 static const struct got_error *
315 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
317 size_t n;
319 val = htobe64(val);
320 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
321 n = fwrite(&val, 1, sizeof(val), outfile);
322 if (n != sizeof(val))
323 return got_ferror(outfile, GOT_ERR_IO);
324 return NULL;
327 static const struct got_error *
328 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
330 size_t n;
332 val = htobe32(val);
333 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
334 n = fwrite(&val, 1, sizeof(val), outfile);
335 if (n != sizeof(val))
336 return got_ferror(outfile, GOT_ERR_IO);
337 return NULL;
340 static const struct got_error *
341 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
343 size_t n;
345 val = htobe16(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_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
356 size_t n, len, pad = 0;
357 static const uint8_t zero[8] = { 0 };
359 len = strlen(path);
360 while ((len + pad) % 8 != 0)
361 pad++;
362 if (pad == 0)
363 pad = 8; /* NUL-terminate */
365 SHA1Update(ctx, path, len);
366 n = fwrite(path, 1, len, outfile);
367 if (n != len)
368 return got_ferror(outfile, GOT_ERR_IO);
369 SHA1Update(ctx, zero, pad);
370 n = fwrite(zero, 1, pad, outfile);
371 if (n != pad)
372 return got_ferror(outfile, GOT_ERR_IO);
373 return NULL;
376 static const struct got_error *
377 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
378 FILE *outfile)
380 const struct got_error *err;
381 size_t n;
382 uint32_t stage;
384 err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
385 if (err)
386 return err;
387 err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
388 if (err)
389 return err;
390 err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
391 if (err)
392 return err;
393 err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
394 if (err)
395 return err;
397 err = write_fileindex_val32(ctx, ie->uid, outfile);
398 if (err)
399 return err;
400 err = write_fileindex_val32(ctx, ie->gid, outfile);
401 if (err)
402 return err;
403 err = write_fileindex_val32(ctx, ie->size, outfile);
404 if (err)
405 return err;
407 err = write_fileindex_val16(ctx, ie->mode, outfile);
408 if (err)
409 return err;
411 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
412 n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
413 if (n != SHA1_DIGEST_LENGTH)
414 return got_ferror(outfile, GOT_ERR_IO);
416 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
417 n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
418 if (n != SHA1_DIGEST_LENGTH)
419 return got_ferror(outfile, GOT_ERR_IO);
421 err = write_fileindex_val32(ctx, ie->flags, outfile);
422 if (err)
423 return err;
425 err = write_fileindex_path(ctx, ie->path, outfile);
426 if (err)
427 return err;
429 stage = got_fileindex_entry_stage_get(ie);
430 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
431 stage == GOT_FILEIDX_STAGE_ADD) {
432 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
433 n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
434 outfile);
435 if (n != SHA1_DIGEST_LENGTH)
436 return got_ferror(outfile, GOT_ERR_IO);
439 return NULL;
442 const struct got_error *
443 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
445 const struct got_error *err = NULL;
446 struct got_fileindex_hdr hdr;
447 SHA1_CTX ctx;
448 uint8_t sha1[SHA1_DIGEST_LENGTH];
449 size_t n;
450 struct got_fileindex_entry *ie, *tmp;
452 SHA1Init(&ctx);
454 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
455 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
456 hdr.nentries = htobe32(fileindex->nentries);
458 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
459 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
460 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
461 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
462 if (n != sizeof(hdr.signature))
463 return got_ferror(outfile, GOT_ERR_IO);
464 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
465 if (n != sizeof(hdr.version))
466 return got_ferror(outfile, GOT_ERR_IO);
467 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
468 if (n != sizeof(hdr.nentries))
469 return got_ferror(outfile, GOT_ERR_IO);
471 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
472 ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
473 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH) {
474 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
475 got_fileindex_entry_free(ie);
476 continue;
478 err = write_fileindex_entry(&ctx, ie, outfile);
479 if (err)
480 return err;
483 SHA1Final(sha1, &ctx);
484 n = fwrite(sha1, 1, sizeof(sha1), outfile);
485 if (n != sizeof(sha1))
486 return got_ferror(outfile, GOT_ERR_IO);
488 if (fflush(outfile) != 0)
489 return got_error_from_errno("fflush");
491 return NULL;
494 static const struct got_error *
495 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
497 size_t n;
499 n = fread(val, 1, sizeof(*val), infile);
500 if (n != sizeof(*val))
501 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
502 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
503 *val = be64toh(*val);
504 return NULL;
507 static const struct got_error *
508 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
510 size_t n;
512 n = fread(val, 1, sizeof(*val), infile);
513 if (n != sizeof(*val))
514 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
515 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
516 *val = be32toh(*val);
517 return NULL;
520 static const struct got_error *
521 read_fileindex_val16(uint16_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 = be16toh(*val);
530 return NULL;
533 static const struct got_error *
534 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
536 const struct got_error *err = NULL;
537 const size_t chunk_size = 8;
538 size_t n, len = 0, totlen = chunk_size;
540 *path = malloc(totlen);
541 if (*path == NULL)
542 return got_error_from_errno("malloc");
544 do {
545 if (len + chunk_size > totlen) {
546 char *p = reallocarray(*path, totlen + chunk_size, 1);
547 if (p == NULL) {
548 err = got_error_from_errno("reallocarray");
549 break;
551 totlen += chunk_size;
552 *path = p;
554 n = fread(*path + len, 1, chunk_size, infile);
555 if (n != chunk_size) {
556 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
557 break;
559 SHA1Update(ctx, *path + len, chunk_size);
560 len += chunk_size;
561 } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
563 if (err) {
564 free(*path);
565 *path = NULL;
567 return err;
570 static const struct got_error *
571 read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
572 FILE *infile, uint32_t version)
574 const struct got_error *err;
575 struct got_fileindex_entry *ie;
576 size_t n;
578 *iep = NULL;
580 ie = calloc(1, sizeof(*ie));
581 if (ie == NULL)
582 return got_error_from_errno("calloc");
584 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
585 if (err)
586 goto done;
587 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
588 if (err)
589 goto done;
590 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
591 if (err)
592 goto done;
593 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
594 if (err)
595 goto done;
597 err = read_fileindex_val32(&ie->uid, ctx, infile);
598 if (err)
599 goto done;
600 err = read_fileindex_val32(&ie->gid, ctx, infile);
601 if (err)
602 goto done;
603 err = read_fileindex_val32(&ie->size, ctx, infile);
604 if (err)
605 goto done;
607 err = read_fileindex_val16(&ie->mode, ctx, infile);
608 if (err)
609 goto done;
611 n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
612 if (n != SHA1_DIGEST_LENGTH) {
613 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
614 goto done;
616 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
618 n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
619 if (n != SHA1_DIGEST_LENGTH) {
620 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
621 goto done;
623 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
625 err = read_fileindex_val32(&ie->flags, ctx, infile);
626 if (err)
627 goto done;
629 err = read_fileindex_path(&ie->path, ctx, infile);
630 if (err)
631 goto done;
633 if (version >= 2) {
634 uint32_t stage = got_fileindex_entry_stage_get(ie);
635 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
636 stage == GOT_FILEIDX_STAGE_ADD) {
637 n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
638 infile);
639 if (n != SHA1_DIGEST_LENGTH) {
640 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
641 goto done;
643 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
645 } else {
646 /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
647 ie->flags &= ~GOT_FILEIDX_F_STAGE;
650 done:
651 if (err)
652 got_fileindex_entry_free(ie);
653 else
654 *iep = ie;
655 return err;
658 const struct got_error *
659 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
661 const struct got_error *err = NULL;
662 struct got_fileindex_hdr hdr;
663 SHA1_CTX ctx;
664 struct got_fileindex_entry *ie;
665 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
666 uint8_t sha1[SHA1_DIGEST_LENGTH];
667 size_t n;
668 int i;
670 SHA1Init(&ctx);
672 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
673 if (n != sizeof(hdr.signature)) {
674 if (n == 0) /* EOF */
675 return NULL;
676 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
678 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
679 if (n != sizeof(hdr.version)) {
680 if (n == 0) /* EOF */
681 return NULL;
682 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
684 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
685 if (n != sizeof(hdr.nentries)) {
686 if (n == 0) /* EOF */
687 return NULL;
688 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
691 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
692 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
693 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
695 hdr.signature = be32toh(hdr.signature);
696 hdr.version = be32toh(hdr.version);
697 hdr.nentries = be32toh(hdr.nentries);
699 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
700 return got_error(GOT_ERR_FILEIDX_SIG);
701 if (hdr.version > GOT_FILE_INDEX_VERSION)
702 return got_error(GOT_ERR_FILEIDX_VER);
704 for (i = 0; i < hdr.nentries; i++) {
705 err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
706 if (err)
707 return err;
708 err = add_entry(fileindex, ie);
709 if (err)
710 return err;
713 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
714 if (n != sizeof(sha1_expected))
715 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
716 SHA1Final(sha1, &ctx);
717 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
718 return got_error(GOT_ERR_FILEIDX_CSUM);
720 return NULL;
723 static struct got_fileindex_entry *
724 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
726 struct got_fileindex_entry *next;
728 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
730 /* Skip entries which were added or removed by diff callbacks. */
731 while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
732 GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
733 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
735 return next;
738 static const struct got_error *
739 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
740 struct got_tree_object *tree, const char *, const char *,
741 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
743 static const struct got_error *
744 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
745 struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
746 const char *path, const char *entry_name, struct got_repository *repo,
747 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
749 const struct got_error *err = NULL;
750 struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
752 if (!got_object_tree_entry_is_submodule(te) &&
753 S_ISDIR(got_tree_entry_get_mode(te))) {
754 char *subpath;
755 struct got_tree_object *subtree;
757 if (asprintf(&subpath, "%s%s%s", path,
758 path[0] == '\0' ? "" : "/",
759 got_tree_entry_get_name(te)) == -1)
760 return got_error_from_errno("asprintf");
762 err = got_object_open_as_tree(&subtree, repo,
763 got_tree_entry_get_id(te));
764 if (err) {
765 free(subpath);
766 return err;
769 err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
770 entry_name, repo, cb, cb_arg);
771 free(subpath);
772 got_object_tree_close(subtree);
773 if (err)
774 return err;
777 (*tidx)++;
778 *next = got_object_tree_get_entry(tree, *tidx);
779 return NULL;
782 static const struct got_error *
783 diff_fileindex_tree(struct got_fileindex *fileindex,
784 struct got_fileindex_entry **ie, struct got_tree_object *tree,
785 const char *path, const char *entry_name, struct got_repository *repo,
786 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
788 const struct got_error *err = NULL;
789 struct got_tree_entry *te = NULL;
790 size_t path_len = strlen(path);
791 struct got_fileindex_entry *next;
792 int tidx = 0;
794 te = got_object_tree_get_entry(tree, tidx);
795 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
796 if (te && *ie) {
797 char *te_path;
798 const char *te_name = got_tree_entry_get_name(te);
799 int cmp;
800 if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
801 err = got_error_from_errno("asprintf");
802 break;
804 cmp = got_path_cmp((*ie)->path, te_path,
805 got_fileindex_entry_path_len(*ie), strlen(te_path));
806 free(te_path);
807 if (cmp == 0) {
808 if (got_path_is_child((*ie)->path, path,
809 path_len) &&
810 !got_object_tree_entry_is_submodule(te) &&
811 (entry_name == NULL ||
812 strcmp(te_name, entry_name) == 0)) {
813 err = cb->diff_old_new(cb_arg, *ie, te,
814 path);
815 if (err || entry_name)
816 break;
818 *ie = walk_fileindex(fileindex, *ie);
819 err = walk_tree(&te, fileindex, ie, tree, &tidx,
820 path, entry_name, repo, cb, cb_arg);
821 } else if (cmp < 0) {
822 next = walk_fileindex(fileindex, *ie);
823 if (got_path_is_child((*ie)->path, path,
824 path_len) && (entry_name == NULL ||
825 strcmp(te_name, entry_name) == 0)) {
826 err = cb->diff_old(cb_arg, *ie, path);
827 if (err || entry_name)
828 break;
830 *ie = next;
831 } else {
832 if ((entry_name == NULL ||
833 strcmp(te_name, entry_name) == 0)) {
834 err = cb->diff_new(cb_arg, te, path);
835 if (err || entry_name)
836 break;
838 err = walk_tree(&te, fileindex, ie, tree, &tidx,
839 path, entry_name, repo, cb, cb_arg);
841 if (err)
842 break;
843 } else if (*ie) {
844 next = walk_fileindex(fileindex, *ie);
845 if (got_path_is_child((*ie)->path, path, path_len) &&
846 (entry_name == NULL ||
847 (te && strcmp(got_tree_entry_get_name(te),
848 entry_name) == 0))) {
849 err = cb->diff_old(cb_arg, *ie, path);
850 if (err || entry_name)
851 break;
853 *ie = next;
854 } else if (te) {
855 if (!got_object_tree_entry_is_submodule(te) &&
856 (entry_name == NULL ||
857 strcmp(got_tree_entry_get_name(te), entry_name)
858 == 0)) {
859 err = cb->diff_new(cb_arg, te, path);
860 if (err || entry_name)
861 break;
863 err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
864 entry_name, repo, cb, cb_arg);
865 if (err)
866 break;
870 return err;
873 const struct got_error *
874 got_fileindex_diff_tree(struct got_fileindex *fileindex,
875 struct got_tree_object *tree, const char *path, const char *entry_name,
876 struct got_repository *repo,
877 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
879 struct got_fileindex_entry *ie;
880 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
881 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
882 ie = walk_fileindex(fileindex, ie);
883 return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
884 cb, cb_arg);
887 static const struct got_error *
888 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
889 struct got_pathlist_head *, int, const char *, const char *,
890 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
892 static const struct got_error *
893 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
895 const struct got_error *err = NULL;
896 struct got_pathlist_entry *new = NULL;
897 struct dirent *dep = NULL;
898 struct dirent *de = NULL;
900 for (;;) {
901 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
902 if (de == NULL) {
903 err = got_error_from_errno("malloc");
904 break;
907 if (readdir_r(dir, de, &dep) != 0) {
908 err = got_error_from_errno("readdir_r");
909 free(de);
910 break;
912 if (dep == NULL) {
913 free(de);
914 break;
917 if (strcmp(de->d_name, ".") == 0 ||
918 strcmp(de->d_name, "..") == 0 ||
919 (path[0] == '\0' &&
920 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
921 free(de);
922 continue;
925 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
926 if (err) {
927 free(de);
928 break;
930 if (new == NULL) {
931 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
932 free(de);
933 break;
937 return err;
940 void
941 free_dirlist(struct got_pathlist_head *dirlist)
943 struct got_pathlist_entry *dle;
945 TAILQ_FOREACH(dle, dirlist, entry)
946 free(dle->data);
947 got_pathlist_free(dirlist);
950 static const struct got_error *
951 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
952 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
953 const char *path, const char *rootpath, struct got_repository *repo,
954 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
956 const struct got_error *err = NULL;
957 struct dirent *de = dle->data;
958 DIR *subdir = NULL;
959 int subdirfd = -1;
960 int type;
962 *next = NULL;
964 if (de->d_type == DT_UNKNOWN) {
965 /* Occurs on NFS mounts without "readdir plus" RPC. */
966 char *dir_path;
967 if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
968 return got_error_from_errno("asprintf");
969 err = got_path_dirent_type(&type, dir_path, de);
970 free(dir_path);
971 if (err)
972 return err;
973 } else
974 type = de->d_type;
976 if (type == DT_DIR) {
977 char *subpath;
978 char *subdirpath;
979 struct got_pathlist_head subdirlist;
981 TAILQ_INIT(&subdirlist);
983 if (asprintf(&subpath, "%s%s%s", path,
984 path[0] == '\0' ? "" : "/", de->d_name) == -1)
985 return got_error_from_errno("asprintf");
987 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
988 free(subpath);
989 return got_error_from_errno("asprintf");
992 subdirfd = openat(fd, de->d_name,
993 O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
994 if (subdirfd == -1) {
995 if (errno == EACCES) {
996 *next = TAILQ_NEXT(dle, entry);
997 return NULL;
999 err = got_error_from_errno2("openat", subdirpath);
1000 free(subpath);
1001 free(subdirpath);
1002 return err;
1005 subdir = fdopendir(subdirfd);
1006 if (subdir == NULL)
1007 return got_error_from_errno2("fdopendir", path);
1008 subdirfd = -1;
1009 err = read_dirlist(&subdirlist, subdir, subdirpath);
1010 if (err) {
1011 free(subpath);
1012 free(subdirpath);
1013 closedir(subdir);
1014 return err;
1016 err = diff_fileindex_dir(fileindex, ie, &subdirlist,
1017 dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
1018 if (subdir && closedir(subdir) == -1 && err == NULL)
1019 err = got_error_from_errno2("closedir", subdirpath);
1020 free(subpath);
1021 free(subdirpath);
1022 free_dirlist(&subdirlist);
1023 if (err)
1024 return err;
1027 *next = TAILQ_NEXT(dle, entry);
1028 return NULL;
1031 static const struct got_error *
1032 diff_fileindex_dir(struct got_fileindex *fileindex,
1033 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1034 int dirfd, const char *rootpath, const char *path,
1035 struct got_repository *repo,
1036 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1038 const struct got_error *err = NULL;
1039 struct dirent *de = NULL;
1040 size_t path_len = strlen(path);
1041 struct got_pathlist_entry *dle;
1043 if (cb->diff_traverse) {
1044 err = cb->diff_traverse(cb_arg, path, dirfd);
1045 if (err)
1046 return err;
1049 dle = TAILQ_FIRST(dirlist);
1050 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1051 if (dle && *ie) {
1052 char *de_path;
1053 int cmp;
1054 de = dle->data;
1055 if (asprintf(&de_path, "%s/%s", path,
1056 de->d_name) == -1) {
1057 err = got_error_from_errno("asprintf");
1058 break;
1060 cmp = got_path_cmp((*ie)->path, de_path,
1061 got_fileindex_entry_path_len(*ie),
1062 strlen(path) + 1 + de->d_namlen);
1063 free(de_path);
1064 if (cmp == 0) {
1065 err = cb->diff_old_new(cb_arg, *ie, de, path,
1066 dirfd);
1067 if (err)
1068 break;
1069 *ie = walk_fileindex(fileindex, *ie);
1070 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1071 path, rootpath, repo, cb, cb_arg);
1072 } else if (cmp < 0 ) {
1073 err = cb->diff_old(cb_arg, *ie, path);
1074 if (err)
1075 break;
1076 *ie = walk_fileindex(fileindex, *ie);
1077 } else {
1078 err = cb->diff_new(cb_arg, de, path, dirfd);
1079 if (err)
1080 break;
1081 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1082 path, rootpath, repo, cb, cb_arg);
1084 if (err)
1085 break;
1086 } else if (*ie) {
1087 err = cb->diff_old(cb_arg, *ie, path);
1088 if (err)
1089 break;
1090 *ie = walk_fileindex(fileindex, *ie);
1091 } else if (dle) {
1092 de = dle->data;
1093 err = cb->diff_new(cb_arg, de, path, dirfd);
1094 if (err)
1095 break;
1096 err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1097 rootpath, repo, cb, cb_arg);
1098 if (err)
1099 break;
1103 return err;
1106 const struct got_error *
1107 got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1108 const char *rootpath, const char *path, struct got_repository *repo,
1109 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1111 const struct got_error *err;
1112 struct got_fileindex_entry *ie;
1113 struct got_pathlist_head dirlist;
1114 int fd2;
1115 DIR *dir;
1117 TAILQ_INIT(&dirlist);
1120 * Duplicate the file descriptor so we can call closedir() below
1121 * without closing the file descriptor passed in by our caller.
1123 fd2 = dup(fd);
1124 if (fd2 == -1)
1125 return got_error_from_errno2("dup", path);
1126 if (lseek(fd2, 0, SEEK_SET) == -1) {
1127 err = got_error_from_errno2("lseek", path);
1128 close(fd2);
1129 return err;
1131 dir = fdopendir(fd2);
1132 if (dir == NULL) {
1133 err = got_error_from_errno2("fdopendir", path);
1134 close(fd2);
1135 return err;
1137 err = read_dirlist(&dirlist, dir, path);
1138 if (err) {
1139 closedir(dir);
1140 return err;
1143 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1144 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1145 ie = walk_fileindex(fileindex, ie);
1146 err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1147 rootpath, path, repo, cb, cb_arg);
1149 if (closedir(dir) == -1 && err == NULL)
1150 err = got_error_from_errno2("closedir", path);
1151 free_dirlist(&dirlist);
1152 return err;
1155 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);