2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/queue.h>
33 #include "got_error.h"
34 #include "got_object.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
57 got_fileindex_entry_perms_get(struct got_fileindex_entry *ie)
59 return ((ie->mode & GOT_FILEIDX_MODE_PERMS) >>
60 GOT_FILEIDX_MODE_PERMS_SHIFT);
64 fileindex_entry_perms_set(struct got_fileindex_entry *ie, mode_t mode)
66 ie->mode &= ~GOT_FILEIDX_MODE_PERMS;
67 ie->mode |= ((mode << GOT_FILEIDX_MODE_PERMS_SHIFT) &
68 GOT_FILEIDX_MODE_PERMS);
72 got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
74 mode_t perms = got_fileindex_entry_perms_get(ie);
75 int type = got_fileindex_entry_filetype_get(ie);
78 if (type == GOT_FILEIDX_MODE_REGULAR_FILE ||
79 type == GOT_FILEIDX_MODE_BAD_SYMLINK)
84 return (ftype | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
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)
94 if (lstat(ondisk_path, &sb) != 0) {
95 if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
97 return got_error_from_errno2("lstat", ondisk_path);
98 sb.st_mode = GOT_DEFAULT_FILE_MODE;
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;
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);
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));
129 memcpy(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
130 ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
132 ie->flags |= GOT_FILEIDX_F_NO_BLOB;
135 memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
136 ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
138 ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
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,
155 *ie = calloc(1, sizeof(**ie));
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");
167 len = strlen(relpath);
168 if (len > GOT_FILEIDX_F_PATH_LEN)
169 len = GOT_FILEIDX_F_PATH_LEN;
176 got_fileindex_entry_free(struct got_fileindex_entry *ie)
183 got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
185 return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
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);
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);
203 got_fileindex_entry_filetype_get(struct got_fileindex_entry *ie)
205 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
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);
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);
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;
231 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
233 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
237 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
239 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
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++;
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);
270 got_fileindex_entry_remove(struct got_fileindex *fileindex,
271 struct got_fileindex_entry *ie)
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.
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,
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))
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)
309 err = (*cb)(cb_arg, ie);
316 struct got_fileindex *
317 got_fileindex_alloc(void)
319 struct got_fileindex *fileindex;
321 fileindex = calloc(1, sizeof(*fileindex));
322 if (fileindex == NULL)
325 RB_INIT(&fileindex->entries);
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);
341 static const struct got_error *
342 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
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);
354 static const struct got_error *
355 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
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);
367 static const struct got_error *
368 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
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);
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 };
387 while ((len + pad) % 8 != 0)
390 pad = 8; /* NUL-terminate */
392 SHA1Update(ctx, path, len);
393 n = fwrite(path, 1, len, outfile);
395 return got_ferror(outfile, GOT_ERR_IO);
396 SHA1Update(ctx, zero, pad);
397 n = fwrite(zero, 1, pad, outfile);
399 return got_ferror(outfile, GOT_ERR_IO);
403 static const struct got_error *
404 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
407 const struct got_error *err;
411 err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
414 err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
417 err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
420 err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
424 err = write_fileindex_val32(ctx, ie->uid, outfile);
427 err = write_fileindex_val32(ctx, ie->gid, outfile);
430 err = write_fileindex_val32(ctx, ie->size, outfile);
434 err = write_fileindex_val16(ctx, ie->mode, outfile);
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);
452 err = write_fileindex_path(ctx, ie->path, outfile);
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,
462 if (n != SHA1_DIGEST_LENGTH)
463 return got_ferror(outfile, GOT_ERR_IO);
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;
475 uint8_t sha1[SHA1_DIGEST_LENGTH];
477 struct got_fileindex_entry *ie, *tmp;
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);
505 err = write_fileindex_entry(&ctx, ie, outfile);
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");
521 static const struct got_error *
522 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
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);
534 static const struct got_error *
535 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
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);
547 static const struct got_error *
548 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
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);
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);
569 return got_error_from_errno("malloc");
572 if (len + chunk_size > totlen) {
573 char *p = reallocarray(*path, totlen + chunk_size, 1);
575 err = got_error_from_errno("reallocarray");
578 totlen += chunk_size;
581 n = fread(*path + len, 1, chunk_size, infile);
582 if (n != chunk_size) {
583 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
586 SHA1Update(ctx, *path + len, chunk_size);
588 } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
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;
607 ie = calloc(1, sizeof(*ie));
609 return got_error_from_errno("calloc");
611 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
614 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
617 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
620 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
624 err = read_fileindex_val32(&ie->uid, ctx, infile);
627 err = read_fileindex_val32(&ie->gid, ctx, infile);
630 err = read_fileindex_val32(&ie->size, ctx, infile);
634 err = read_fileindex_val16(&ie->mode, ctx, infile);
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);
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);
650 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
652 err = read_fileindex_val32(&ie->flags, ctx, infile);
656 err = read_fileindex_path(&ie->path, ctx, infile);
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,
666 if (n != SHA1_DIGEST_LENGTH) {
667 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
670 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
673 /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
674 ie->flags &= ~GOT_FILEIDX_F_STAGE;
679 got_fileindex_entry_free(ie);
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;
691 struct got_fileindex_entry *ie;
692 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
693 uint8_t sha1[SHA1_DIGEST_LENGTH];
699 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
700 if (n != sizeof(hdr.signature)) {
701 if (n == 0) /* EOF */
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 */
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 */
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);
735 err = add_entry(fileindex, ie);
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);
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);
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))) {
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));
796 err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
797 entry_name, repo, cb, cb_arg);
799 got_object_tree_close(subtree);
805 *next = got_object_tree_get_entry(tree, *tidx);
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;
821 te = got_object_tree_get_entry(tree, tidx);
822 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
825 const char *te_name = got_tree_entry_get_name(te);
827 if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
828 err = got_error_from_errno("asprintf");
831 cmp = got_path_cmp((*ie)->path, te_path,
832 got_fileindex_entry_path_len(*ie), strlen(te_path));
835 if (got_path_is_child((*ie)->path, path,
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,
842 if (err || entry_name)
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 strcmp(te_name, entry_name) == 0)) {
853 err = cb->diff_old(cb_arg, *ie, path);
854 if (err || entry_name)
859 if ((entry_name == NULL ||
860 strcmp(te_name, entry_name) == 0)) {
861 err = cb->diff_new(cb_arg, te, path);
862 if (err || entry_name)
865 err = walk_tree(&te, fileindex, ie, tree, &tidx,
866 path, entry_name, repo, cb, cb_arg);
871 next = walk_fileindex(fileindex, *ie);
872 if (got_path_is_child((*ie)->path, path, path_len) &&
873 (entry_name == NULL ||
874 (te && strcmp(got_tree_entry_get_name(te),
875 entry_name) == 0))) {
876 err = cb->diff_old(cb_arg, *ie, path);
877 if (err || entry_name)
882 if (!got_object_tree_entry_is_submodule(te) &&
883 (entry_name == NULL ||
884 strcmp(got_tree_entry_get_name(te), entry_name)
886 err = cb->diff_new(cb_arg, te, path);
887 if (err || entry_name)
890 err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
891 entry_name, repo, cb, cb_arg);
900 const struct got_error *
901 got_fileindex_diff_tree(struct got_fileindex *fileindex,
902 struct got_tree_object *tree, const char *path, const char *entry_name,
903 struct got_repository *repo,
904 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
906 struct got_fileindex_entry *ie;
907 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
908 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
909 ie = walk_fileindex(fileindex, ie);
910 return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
914 static const struct got_error *
915 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
916 struct got_pathlist_head *, int, const char *, const char *,
917 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
919 static const struct got_error *
920 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
922 const struct got_error *err = NULL;
923 struct got_pathlist_entry *new = NULL;
924 struct dirent *dep = NULL;
925 struct dirent *de = NULL;
928 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
930 err = got_error_from_errno("malloc");
934 if (readdir_r(dir, de, &dep) != 0) {
935 err = got_error_from_errno("readdir_r");
944 if (strcmp(de->d_name, ".") == 0 ||
945 strcmp(de->d_name, "..") == 0 ||
947 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
952 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
958 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
968 free_dirlist(struct got_pathlist_head *dirlist)
970 struct got_pathlist_entry *dle;
972 TAILQ_FOREACH(dle, dirlist, entry)
974 got_pathlist_free(dirlist);
977 static const struct got_error *
978 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
979 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
980 const char *path, const char *rootpath, struct got_repository *repo,
981 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
983 const struct got_error *err = NULL;
984 struct dirent *de = dle->data;
991 if (de->d_type == DT_UNKNOWN) {
992 /* Occurs on NFS mounts without "readdir plus" RPC. */
994 if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
995 return got_error_from_errno("asprintf");
996 err = got_path_dirent_type(&type, dir_path, de);
1003 if (type == DT_DIR) {
1006 struct got_pathlist_head subdirlist;
1008 TAILQ_INIT(&subdirlist);
1010 if (asprintf(&subpath, "%s%s%s", path,
1011 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1012 return got_error_from_errno("asprintf");
1014 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
1016 return got_error_from_errno("asprintf");
1019 subdirfd = openat(fd, de->d_name,
1020 O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
1021 if (subdirfd == -1) {
1022 if (errno == EACCES) {
1023 *next = TAILQ_NEXT(dle, entry);
1026 err = got_error_from_errno2("openat", subdirpath);
1032 subdir = fdopendir(subdirfd);
1034 return got_error_from_errno2("fdopendir", path);
1036 err = read_dirlist(&subdirlist, subdir, subdirpath);
1043 err = diff_fileindex_dir(fileindex, ie, &subdirlist,
1044 dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
1045 if (subdir && closedir(subdir) == -1 && err == NULL)
1046 err = got_error_from_errno2("closedir", subdirpath);
1049 free_dirlist(&subdirlist);
1054 *next = TAILQ_NEXT(dle, entry);
1058 static const struct got_error *
1059 diff_fileindex_dir(struct got_fileindex *fileindex,
1060 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1061 int dirfd, const char *rootpath, const char *path,
1062 struct got_repository *repo,
1063 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1065 const struct got_error *err = NULL;
1066 struct dirent *de = NULL;
1067 size_t path_len = strlen(path);
1068 struct got_pathlist_entry *dle;
1070 if (cb->diff_traverse) {
1071 err = cb->diff_traverse(cb_arg, path, dirfd);
1076 dle = TAILQ_FIRST(dirlist);
1077 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1082 if (asprintf(&de_path, "%s/%s", path,
1083 de->d_name) == -1) {
1084 err = got_error_from_errno("asprintf");
1087 cmp = got_path_cmp((*ie)->path, de_path,
1088 got_fileindex_entry_path_len(*ie),
1089 strlen(path) + 1 + de->d_namlen);
1092 err = cb->diff_old_new(cb_arg, *ie, de, path,
1096 *ie = walk_fileindex(fileindex, *ie);
1097 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1098 path, rootpath, repo, cb, cb_arg);
1099 } else if (cmp < 0 ) {
1100 err = cb->diff_old(cb_arg, *ie, path);
1103 *ie = walk_fileindex(fileindex, *ie);
1105 err = cb->diff_new(cb_arg, de, path, dirfd);
1108 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1109 path, rootpath, repo, cb, cb_arg);
1114 err = cb->diff_old(cb_arg, *ie, path);
1117 *ie = walk_fileindex(fileindex, *ie);
1120 err = cb->diff_new(cb_arg, de, path, dirfd);
1123 err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1124 rootpath, repo, cb, cb_arg);
1133 const struct got_error *
1134 got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1135 const char *rootpath, const char *path, struct got_repository *repo,
1136 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1138 const struct got_error *err;
1139 struct got_fileindex_entry *ie;
1140 struct got_pathlist_head dirlist;
1144 TAILQ_INIT(&dirlist);
1147 * Duplicate the file descriptor so we can call closedir() below
1148 * without closing the file descriptor passed in by our caller.
1152 return got_error_from_errno2("dup", path);
1153 if (lseek(fd2, 0, SEEK_SET) == -1) {
1154 err = got_error_from_errno2("lseek", path);
1158 dir = fdopendir(fd2);
1160 err = got_error_from_errno2("fdopendir", path);
1164 err = read_dirlist(&dirlist, dir, path);
1170 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1171 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1172 ie = walk_fileindex(fileindex, ie);
1173 err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1174 rootpath, path, repo, cb, cb_arg);
1176 if (closedir(dir) == -1 && err == NULL)
1177 err = got_error_from_errno2("closedir", path);
1178 free_dirlist(&dirlist);
1182 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);