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 int wt_fd, const char *ondisk_path, uint8_t *blob_sha1,
90 uint8_t *commit_sha1, int update_timestamps)
91 {
92 struct stat sb;
94 if (fstatat(wt_fd, ondisk_path, &sb, AT_SYMLINK_NOFOLLOW) != 0) {
95 if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
96 errno == ENOENT))
97 return got_error_from_errno2("fstatat", ondisk_path);
98 sb.st_mode = GOT_DEFAULT_FILE_MODE;
99 } else {
100 if (sb.st_mode & S_IFDIR)
101 return got_error_set_errno(EISDIR, ondisk_path);
102 ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
106 if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
107 if (update_timestamps) {
108 ie->ctime_sec = sb.st_ctim.tv_sec;
109 ie->ctime_nsec = sb.st_ctim.tv_nsec;
110 ie->mtime_sec = sb.st_mtim.tv_sec;
111 ie->mtime_nsec = sb.st_mtim.tv_nsec;
113 ie->uid = sb.st_uid;
114 ie->gid = sb.st_gid;
115 ie->size = (sb.st_size & 0xffffffff);
116 if (S_ISLNK(sb.st_mode)) {
117 got_fileindex_entry_filetype_set(ie,
118 GOT_FILEIDX_MODE_SYMLINK);
119 fileindex_entry_perms_set(ie, 0);
120 } else {
121 got_fileindex_entry_filetype_set(ie,
122 GOT_FILEIDX_MODE_REGULAR_FILE);
123 fileindex_entry_perms_set(ie,
124 sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
128 if (blob_sha1) {
129 memcpy(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
130 ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
131 } else
132 ie->flags |= GOT_FILEIDX_F_NO_BLOB;
134 if (commit_sha1) {
135 memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
136 ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
137 } else
138 ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
140 return NULL;
143 void
144 got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
146 ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
149 const struct got_error *
150 got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
151 const char *relpath)
153 size_t len;
155 *ie = calloc(1, sizeof(**ie));
156 if (*ie == NULL)
157 return got_error_from_errno("calloc");
159 (*ie)->path = strdup(relpath);
160 if ((*ie)->path == NULL) {
161 const struct got_error *err = got_error_from_errno("strdup");
162 free(*ie);
163 *ie = NULL;
164 return err;
167 len = strlen(relpath);
168 if (len > GOT_FILEIDX_F_PATH_LEN)
169 len = GOT_FILEIDX_F_PATH_LEN;
170 (*ie)->flags |= len;
172 return NULL;
175 void
176 got_fileindex_entry_free(struct got_fileindex_entry *ie)
178 free(ie->path);
179 free(ie);
182 size_t
183 got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
185 return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
188 uint32_t
189 got_fileindex_entry_stage_get(const struct got_fileindex_entry *ie)
191 return ((ie->flags & GOT_FILEIDX_F_STAGE) >> GOT_FILEIDX_F_STAGE_SHIFT);
194 void
195 got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage)
197 ie->flags &= ~GOT_FILEIDX_F_STAGE;
198 ie->flags |= ((stage << GOT_FILEIDX_F_STAGE_SHIFT) &
199 GOT_FILEIDX_F_STAGE);
202 int
203 got_fileindex_entry_filetype_get(struct got_fileindex_entry *ie)
205 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
208 void
209 got_fileindex_entry_filetype_set(struct got_fileindex_entry *ie, int type)
211 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_ONDISK;
212 ie->mode |= (type & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
215 void
216 got_fileindex_entry_staged_filetype_set(struct got_fileindex_entry *ie, int type)
218 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_STAGED;
219 ie->mode |= ((type << GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT) &
220 GOT_FILEIDX_MODE_FILE_TYPE_STAGED);
223 int
224 got_fileindex_entry_staged_filetype_get(struct got_fileindex_entry *ie)
226 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_STAGED) >>
227 GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT;
230 int
231 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
233 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
236 int
237 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
239 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
242 int
243 got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
245 return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
248 static const struct got_error *
249 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
251 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
252 return got_error(GOT_ERR_NO_SPACE);
254 RB_INSERT(got_fileindex_tree, &fileindex->entries, ie);
255 fileindex->nentries++;
256 return NULL;
259 const struct got_error *
260 got_fileindex_entry_add(struct got_fileindex *fileindex,
261 struct got_fileindex_entry *ie)
263 /* Flag this entry until it gets written out to disk. */
264 ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
266 return add_entry(fileindex, ie);
269 void
270 got_fileindex_entry_remove(struct got_fileindex *fileindex,
271 struct got_fileindex_entry *ie)
273 /*
274 * Removing an entry from the RB tree immediately breaks
275 * in-progress iterations over file index entries.
276 * So flag this entry for removal and remove it once the index
277 * is written out to disk. Meanwhile, pretend this entry no longer
278 * exists if we get queried for it again before then.
279 */
280 ie->flags |= GOT_FILEIDX_F_REMOVE_ON_FLUSH;
281 fileindex->nentries--;
284 struct got_fileindex_entry *
285 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
286 size_t path_len)
288 struct got_fileindex_entry *ie;
289 struct got_fileindex_entry key;
290 memset(&key, 0, sizeof(key));
291 key.path = (char *)path;
292 key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
293 ie = RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
294 if (ie && (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH))
295 return NULL;
296 return ie;
299 const struct got_error *
300 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
301 got_fileindex_cb cb, void *cb_arg)
303 const struct got_error *err;
304 struct got_fileindex_entry *ie, *tmp;
306 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
307 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
308 continue;
309 err = (*cb)(cb_arg, ie);
310 if (err)
311 return err;
313 return NULL;
316 struct got_fileindex *
317 got_fileindex_alloc(void)
319 struct got_fileindex *fileindex;
321 fileindex = calloc(1, sizeof(*fileindex));
322 if (fileindex == NULL)
323 return NULL;
325 RB_INIT(&fileindex->entries);
326 return fileindex;
329 void
330 got_fileindex_free(struct got_fileindex *fileindex)
332 struct got_fileindex_entry *ie;
334 while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
335 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
336 got_fileindex_entry_free(ie);
338 free(fileindex);
341 static const struct got_error *
342 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
344 size_t n;
346 val = htobe64(val);
347 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
348 n = fwrite(&val, 1, sizeof(val), outfile);
349 if (n != sizeof(val))
350 return got_ferror(outfile, GOT_ERR_IO);
351 return NULL;
354 static const struct got_error *
355 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
357 size_t n;
359 val = htobe32(val);
360 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
361 n = fwrite(&val, 1, sizeof(val), outfile);
362 if (n != sizeof(val))
363 return got_ferror(outfile, GOT_ERR_IO);
364 return NULL;
367 static const struct got_error *
368 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
370 size_t n;
372 val = htobe16(val);
373 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
374 n = fwrite(&val, 1, sizeof(val), outfile);
375 if (n != sizeof(val))
376 return got_ferror(outfile, GOT_ERR_IO);
377 return NULL;
380 static const struct got_error *
381 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
383 size_t n, len, pad = 0;
384 static const uint8_t zero[8] = { 0 };
386 len = strlen(path);
387 while ((len + pad) % 8 != 0)
388 pad++;
389 if (pad == 0)
390 pad = 8; /* NUL-terminate */
392 SHA1Update(ctx, path, len);
393 n = fwrite(path, 1, len, outfile);
394 if (n != len)
395 return got_ferror(outfile, GOT_ERR_IO);
396 SHA1Update(ctx, zero, pad);
397 n = fwrite(zero, 1, pad, outfile);
398 if (n != pad)
399 return got_ferror(outfile, GOT_ERR_IO);
400 return NULL;
403 static const struct got_error *
404 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
405 FILE *outfile)
407 const struct got_error *err;
408 size_t n;
409 uint32_t stage;
411 err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
412 if (err)
413 return err;
414 err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
415 if (err)
416 return err;
417 err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
418 if (err)
419 return err;
420 err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
421 if (err)
422 return err;
424 err = write_fileindex_val32(ctx, ie->uid, outfile);
425 if (err)
426 return err;
427 err = write_fileindex_val32(ctx, ie->gid, outfile);
428 if (err)
429 return err;
430 err = write_fileindex_val32(ctx, ie->size, outfile);
431 if (err)
432 return err;
434 err = write_fileindex_val16(ctx, ie->mode, outfile);
435 if (err)
436 return err;
438 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
439 n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
440 if (n != SHA1_DIGEST_LENGTH)
441 return got_ferror(outfile, GOT_ERR_IO);
443 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
444 n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
445 if (n != SHA1_DIGEST_LENGTH)
446 return got_ferror(outfile, GOT_ERR_IO);
448 err = write_fileindex_val32(ctx, ie->flags, outfile);
449 if (err)
450 return err;
452 err = write_fileindex_path(ctx, ie->path, outfile);
453 if (err)
454 return err;
456 stage = got_fileindex_entry_stage_get(ie);
457 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
458 stage == GOT_FILEIDX_STAGE_ADD) {
459 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
460 n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
461 outfile);
462 if (n != SHA1_DIGEST_LENGTH)
463 return got_ferror(outfile, GOT_ERR_IO);
466 return NULL;
469 const struct got_error *
470 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
472 const struct got_error *err = NULL;
473 struct got_fileindex_hdr hdr;
474 SHA1_CTX ctx;
475 uint8_t sha1[SHA1_DIGEST_LENGTH];
476 size_t n;
477 struct got_fileindex_entry *ie, *tmp;
479 SHA1Init(&ctx);
481 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
482 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
483 hdr.nentries = htobe32(fileindex->nentries);
485 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
486 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
487 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
488 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
489 if (n != sizeof(hdr.signature))
490 return got_ferror(outfile, GOT_ERR_IO);
491 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
492 if (n != sizeof(hdr.version))
493 return got_ferror(outfile, GOT_ERR_IO);
494 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
495 if (n != sizeof(hdr.nentries))
496 return got_ferror(outfile, GOT_ERR_IO);
498 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
499 ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
500 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH) {
501 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
502 got_fileindex_entry_free(ie);
503 continue;
505 err = write_fileindex_entry(&ctx, ie, outfile);
506 if (err)
507 return err;
510 SHA1Final(sha1, &ctx);
511 n = fwrite(sha1, 1, sizeof(sha1), outfile);
512 if (n != sizeof(sha1))
513 return got_ferror(outfile, GOT_ERR_IO);
515 if (fflush(outfile) != 0)
516 return got_error_from_errno("fflush");
518 return NULL;
521 static const struct got_error *
522 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
524 size_t n;
526 n = fread(val, 1, sizeof(*val), infile);
527 if (n != sizeof(*val))
528 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
529 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
530 *val = be64toh(*val);
531 return NULL;
534 static const struct got_error *
535 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
537 size_t n;
539 n = fread(val, 1, sizeof(*val), infile);
540 if (n != sizeof(*val))
541 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
542 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
543 *val = be32toh(*val);
544 return NULL;
547 static const struct got_error *
548 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
550 size_t n;
552 n = fread(val, 1, sizeof(*val), infile);
553 if (n != sizeof(*val))
554 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
555 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
556 *val = be16toh(*val);
557 return NULL;
560 static const struct got_error *
561 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
563 const struct got_error *err = NULL;
564 const size_t chunk_size = 8;
565 size_t n, len = 0, totlen = chunk_size;
567 *path = malloc(totlen);
568 if (*path == NULL)
569 return got_error_from_errno("malloc");
571 do {
572 if (len + chunk_size > totlen) {
573 char *p = reallocarray(*path, totlen + chunk_size, 1);
574 if (p == NULL) {
575 err = got_error_from_errno("reallocarray");
576 break;
578 totlen += chunk_size;
579 *path = p;
581 n = fread(*path + len, 1, chunk_size, infile);
582 if (n != chunk_size) {
583 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
584 break;
586 SHA1Update(ctx, *path + len, chunk_size);
587 len += chunk_size;
588 } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
590 if (err) {
591 free(*path);
592 *path = NULL;
594 return err;
597 static const struct got_error *
598 read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
599 FILE *infile, uint32_t version)
601 const struct got_error *err;
602 struct got_fileindex_entry *ie;
603 size_t n;
605 *iep = NULL;
607 ie = calloc(1, sizeof(*ie));
608 if (ie == NULL)
609 return got_error_from_errno("calloc");
611 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
612 if (err)
613 goto done;
614 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
615 if (err)
616 goto done;
617 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
618 if (err)
619 goto done;
620 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
621 if (err)
622 goto done;
624 err = read_fileindex_val32(&ie->uid, ctx, infile);
625 if (err)
626 goto done;
627 err = read_fileindex_val32(&ie->gid, ctx, infile);
628 if (err)
629 goto done;
630 err = read_fileindex_val32(&ie->size, ctx, infile);
631 if (err)
632 goto done;
634 err = read_fileindex_val16(&ie->mode, ctx, infile);
635 if (err)
636 goto done;
638 n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
639 if (n != SHA1_DIGEST_LENGTH) {
640 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
641 goto done;
643 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
645 n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
646 if (n != SHA1_DIGEST_LENGTH) {
647 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
648 goto done;
650 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
652 err = read_fileindex_val32(&ie->flags, ctx, infile);
653 if (err)
654 goto done;
656 err = read_fileindex_path(&ie->path, ctx, infile);
657 if (err)
658 goto done;
660 if (version >= 2) {
661 uint32_t stage = got_fileindex_entry_stage_get(ie);
662 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
663 stage == GOT_FILEIDX_STAGE_ADD) {
664 n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
665 infile);
666 if (n != SHA1_DIGEST_LENGTH) {
667 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
668 goto done;
670 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
672 } else {
673 /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
674 ie->flags &= ~GOT_FILEIDX_F_STAGE;
677 done:
678 if (err)
679 got_fileindex_entry_free(ie);
680 else
681 *iep = ie;
682 return err;
685 const struct got_error *
686 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
688 const struct got_error *err = NULL;
689 struct got_fileindex_hdr hdr;
690 SHA1_CTX ctx;
691 struct got_fileindex_entry *ie;
692 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
693 uint8_t sha1[SHA1_DIGEST_LENGTH];
694 size_t n;
695 int i;
697 SHA1Init(&ctx);
699 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
700 if (n != sizeof(hdr.signature)) {
701 if (n == 0) /* EOF */
702 return NULL;
703 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
705 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
706 if (n != sizeof(hdr.version)) {
707 if (n == 0) /* EOF */
708 return NULL;
709 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
711 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
712 if (n != sizeof(hdr.nentries)) {
713 if (n == 0) /* EOF */
714 return NULL;
715 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
718 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
719 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
720 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
722 hdr.signature = be32toh(hdr.signature);
723 hdr.version = be32toh(hdr.version);
724 hdr.nentries = be32toh(hdr.nentries);
726 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
727 return got_error(GOT_ERR_FILEIDX_SIG);
728 if (hdr.version > GOT_FILE_INDEX_VERSION)
729 return got_error(GOT_ERR_FILEIDX_VER);
731 for (i = 0; i < hdr.nentries; i++) {
732 err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
733 if (err)
734 return err;
735 err = add_entry(fileindex, ie);
736 if (err)
737 return err;
740 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
741 if (n != sizeof(sha1_expected))
742 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
743 SHA1Final(sha1, &ctx);
744 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
745 return got_error(GOT_ERR_FILEIDX_CSUM);
747 return NULL;
750 static struct got_fileindex_entry *
751 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
753 struct got_fileindex_entry *next;
755 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
757 /* Skip entries which were added or removed by diff callbacks. */
758 while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
759 GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
760 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
762 return next;
765 static const struct got_error *
766 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
767 struct got_tree_object *tree, const char *, const char *,
768 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
770 static const struct got_error *
771 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
772 struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
773 const char *path, const char *entry_name, struct got_repository *repo,
774 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
776 const struct got_error *err = NULL;
777 struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
779 if (!got_object_tree_entry_is_submodule(te) &&
780 S_ISDIR(got_tree_entry_get_mode(te))) {
781 char *subpath;
782 struct got_tree_object *subtree;
784 if (asprintf(&subpath, "%s%s%s", path,
785 path[0] == '\0' ? "" : "/",
786 got_tree_entry_get_name(te)) == -1)
787 return got_error_from_errno("asprintf");
789 err = got_object_open_as_tree(&subtree, repo,
790 got_tree_entry_get_id(te));
791 if (err) {
792 free(subpath);
793 return err;
796 err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
797 entry_name, repo, cb, cb_arg);
798 free(subpath);
799 got_object_tree_close(subtree);
800 if (err)
801 return err;
804 (*tidx)++;
805 *next = got_object_tree_get_entry(tree, *tidx);
806 return NULL;
809 static const struct got_error *
810 diff_fileindex_tree(struct got_fileindex *fileindex,
811 struct got_fileindex_entry **ie, struct got_tree_object *tree,
812 const char *path, const char *entry_name, struct got_repository *repo,
813 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
815 const struct got_error *err = NULL;
816 struct got_tree_entry *te = NULL;
817 size_t path_len = strlen(path);
818 struct got_fileindex_entry *next;
819 int tidx = 0;
821 te = got_object_tree_get_entry(tree, tidx);
822 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
823 if (te && *ie) {
824 char *te_path;
825 const char *te_name = got_tree_entry_get_name(te);
826 int cmp;
827 if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
828 err = got_error_from_errno("asprintf");
829 break;
831 cmp = got_path_cmp((*ie)->path, te_path,
832 got_fileindex_entry_path_len(*ie), strlen(te_path));
833 free(te_path);
834 if (cmp == 0) {
835 if (got_path_is_child((*ie)->path, path,
836 path_len) &&
837 !got_object_tree_entry_is_submodule(te) &&
838 (entry_name == NULL ||
839 strcmp(te_name, entry_name) == 0)) {
840 err = cb->diff_old_new(cb_arg, *ie, te,
841 path);
842 if (err || entry_name)
843 break;
845 *ie = walk_fileindex(fileindex, *ie);
846 err = walk_tree(&te, fileindex, ie, tree, &tidx,
847 path, entry_name, repo, cb, cb_arg);
848 } else if (cmp < 0) {
849 next = walk_fileindex(fileindex, *ie);
850 if (got_path_is_child((*ie)->path, path,
851 path_len) && entry_name == NULL) {
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);