2 * Copyright (c) 2018 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>
28 #include "got_error.h"
29 #include "got_object.h"
31 #include "got_lib_path.h"
32 #include "got_lib_fileindex.h"
34 struct got_fileindex {
35 struct got_fileindex_tree entries;
37 #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
40 const struct got_error *
41 got_fileindex_entry_update(struct got_fileindex_entry *entry,
42 const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1)
46 if (lstat(ondisk_path, &sb) != 0)
47 return got_error_from_errno();
49 entry->ctime_sec = sb.st_ctime;
50 entry->ctime_nsec = sb.st_ctimensec;
51 entry->mtime_sec = sb.st_mtime;
52 entry->mtime_nsec = sb.st_mtimensec;
53 entry->uid = sb.st_uid;
54 entry->gid = sb.st_gid;
55 entry->size = (sb.st_size & 0xffffffff);
56 if (sb.st_mode & S_IFLNK)
57 entry->mode = GOT_INDEX_ENTRY_MODE_SYMLINK;
59 entry->mode = GOT_INDEX_ENTRY_MODE_REGULAR_FILE;
60 entry->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
61 GOT_INDEX_ENTRY_MODE_PERMS_SHIFT);
62 memcpy(entry->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
63 memcpy(entry->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
68 const struct got_error *
69 got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
70 const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
75 *entry = calloc(1, sizeof(**entry));
77 return got_error_from_errno();
79 (*entry)->path = strdup(relpath);
80 if ((*entry)->path == NULL) {
81 const struct got_error *err = got_error_from_errno();
87 len = strlen(relpath);
88 if (len > GOT_INDEX_ENTRY_F_PATH_LEN)
89 len = GOT_INDEX_ENTRY_F_PATH_LEN;
90 (*entry)->flags |= len;
92 return got_fileindex_entry_update(*entry, ondisk_path, blob_sha1,
97 got_fileindex_entry_free(struct got_fileindex_entry *entry)
103 static const struct got_error *
104 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *entry)
106 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
107 return got_error(GOT_ERR_NO_SPACE);
109 RB_INSERT(got_fileindex_tree, &fileindex->entries, entry);
110 fileindex->nentries++;
114 const struct got_error *
115 got_fileindex_entry_add(struct got_fileindex *fileindex,
116 struct got_fileindex_entry *entry)
118 /* Flag this entry until it gets written out to disk. */
119 entry->flags |= GOT_INDEX_ENTRY_F_INTENT_TO_ADD;
121 return add_entry(fileindex, entry);
125 got_fileindex_entry_remove(struct got_fileindex *fileindex,
126 struct got_fileindex_entry *entry)
128 RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
129 fileindex->nentries--;
132 struct got_fileindex_entry *
133 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
135 struct got_fileindex_entry key;
136 memset(&key, 0, sizeof(key));
137 key.path = (char *)path;
138 return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
141 const struct got_error *
142 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
143 got_fileindex_cb cb, void *cb_arg)
145 const struct got_error *err;
146 struct got_fileindex_entry *entry, *tmp;
148 RB_FOREACH_SAFE(entry, got_fileindex_tree, &fileindex->entries, tmp) {
149 err = (*cb)(cb_arg, entry);
156 struct got_fileindex *
157 got_fileindex_alloc(void)
159 struct got_fileindex *fileindex;
161 fileindex = calloc(1, sizeof(*fileindex));
162 if (fileindex == NULL)
165 RB_INIT(&fileindex->entries);
170 got_fileindex_free(struct got_fileindex *fileindex)
172 struct got_fileindex_entry *entry;
174 while ((entry = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
175 RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
176 got_fileindex_entry_free(entry);
181 static const struct got_error *
182 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
187 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
188 n = fwrite(&val, 1, sizeof(val), outfile);
189 if (n != sizeof(val))
190 return got_ferror(outfile, GOT_ERR_IO);
194 static const struct got_error *
195 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
200 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
201 n = fwrite(&val, 1, sizeof(val), outfile);
202 if (n != sizeof(val))
203 return got_ferror(outfile, GOT_ERR_IO);
207 static const struct got_error *
208 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
213 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
214 n = fwrite(&val, 1, sizeof(val), outfile);
215 if (n != sizeof(val))
216 return got_ferror(outfile, GOT_ERR_IO);
220 static const struct got_error *
221 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
223 size_t n, len, pad = 0;
224 static const uint8_t zero[8] = { 0 };
227 while ((len + pad) % 8 != 0)
230 pad = 8; /* NUL-terminate */
232 SHA1Update(ctx, path, len);
233 n = fwrite(path, 1, len, outfile);
235 return got_ferror(outfile, GOT_ERR_IO);
236 SHA1Update(ctx, zero, pad);
237 n = fwrite(zero, 1, pad, outfile);
239 return got_ferror(outfile, GOT_ERR_IO);
243 static const struct got_error *
244 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
247 const struct got_error *err;
250 err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
253 err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
256 err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
259 err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
263 err = write_fileindex_val32(ctx, entry->uid, outfile);
266 err = write_fileindex_val32(ctx, entry->gid, outfile);
269 err = write_fileindex_val32(ctx, entry->size, outfile);
273 err = write_fileindex_val16(ctx, entry->mode, outfile);
277 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
278 n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
279 if (n != SHA1_DIGEST_LENGTH)
280 return got_ferror(outfile, GOT_ERR_IO);
282 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
283 n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
284 if (n != SHA1_DIGEST_LENGTH)
285 return got_ferror(outfile, GOT_ERR_IO);
287 err = write_fileindex_val32(ctx, entry->flags, outfile);
291 err = write_fileindex_path(ctx, entry->path, outfile);
295 const struct got_error *
296 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
298 const struct got_error *err = NULL;
299 struct got_fileindex_hdr hdr;
301 uint8_t sha1[SHA1_DIGEST_LENGTH];
303 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
304 sizeof(hdr.nentries);
306 struct got_fileindex_entry *entry;
310 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
311 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
312 hdr.nentries = htobe32(fileindex->nentries);
314 memcpy(buf, &hdr, len);
315 SHA1Update(&ctx, buf, len);
316 n = fwrite(buf, 1, len, outfile);
318 return got_ferror(outfile, GOT_ERR_IO);
320 RB_FOREACH(entry, got_fileindex_tree, &fileindex->entries) {
321 entry->flags &= ~GOT_INDEX_ENTRY_F_INTENT_TO_ADD;
322 err = write_fileindex_entry(&ctx, entry, outfile);
327 SHA1Final(sha1, &ctx);
328 n = fwrite(sha1, 1, sizeof(sha1), outfile);
329 if (n != sizeof(sha1))
330 return got_ferror(outfile, GOT_ERR_IO);
335 static const struct got_error *
336 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
340 n = fread(val, 1, sizeof(*val), infile);
341 if (n != sizeof(*val))
342 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
343 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
344 *val = be64toh(*val);
348 static const struct got_error *
349 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
353 n = fread(val, 1, sizeof(*val), infile);
354 if (n != sizeof(*val))
355 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
356 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
357 *val = be32toh(*val);
361 static const struct got_error *
362 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
366 n = fread(val, 1, sizeof(*val), infile);
367 if (n != sizeof(*val))
368 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
369 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
370 *val = be16toh(*val);
374 static const struct got_error *
375 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
377 const struct got_error *err = NULL;
379 size_t n, len = 0, totlen = sizeof(buf);
381 *path = malloc(totlen);
383 return got_error_from_errno();
386 n = fread(buf, 1, sizeof(buf), infile);
387 if (n != sizeof(buf))
388 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
389 if (len + sizeof(buf) > totlen) {
390 char *p = reallocarray(*path, totlen + sizeof(buf), 1);
392 err = got_error_from_errno();
395 totlen += sizeof(buf);
398 SHA1Update(ctx, buf, sizeof(buf));
399 memcpy(*path + len, buf, sizeof(buf));
401 } while (memchr(buf, '\0', sizeof(buf)) == NULL);
410 static const struct got_error *
411 read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
414 const struct got_error *err;
415 struct got_fileindex_entry *entry;
420 entry = calloc(1, sizeof(*entry));
422 return got_error_from_errno();
424 err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
427 err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
430 err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
433 err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
437 err = read_fileindex_val32(&entry->uid, ctx, infile);
440 err = read_fileindex_val32(&entry->gid, ctx, infile);
443 err = read_fileindex_val32(&entry->size, ctx, infile);
447 err = read_fileindex_val16(&entry->mode, ctx, infile);
451 n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
452 if (n != SHA1_DIGEST_LENGTH) {
453 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
456 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
458 n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
459 if (n != SHA1_DIGEST_LENGTH) {
460 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
463 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
465 err = read_fileindex_val32(&entry->flags, ctx, infile);
469 err = read_fileindex_path(&entry->path, ctx, infile);
478 const struct got_error *
479 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
481 const struct got_error *err = NULL;
482 struct got_fileindex_hdr hdr;
484 struct got_fileindex_entry *entry;
485 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
486 uint8_t sha1[SHA1_DIGEST_LENGTH];
488 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
489 sizeof(hdr.nentries);
495 n = fread(buf, 1, len, infile);
497 if (n == 0) /* EOF */
499 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
502 SHA1Update(&ctx, buf, len);
504 memcpy(&hdr, buf, len);
505 hdr.signature = be32toh(hdr.signature);
506 hdr.version = be32toh(hdr.version);
507 hdr.nentries = be32toh(hdr.nentries);
509 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
510 return got_error(GOT_ERR_FILEIDX_SIG);
511 if (hdr.version != GOT_FILE_INDEX_VERSION)
512 return got_error(GOT_ERR_FILEIDX_VER);
514 for (i = 0; i < hdr.nentries; i++) {
515 err = read_fileindex_entry(&entry, &ctx, infile);
518 err = add_entry(fileindex, entry);
523 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
524 if (n != sizeof(sha1_expected))
525 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
526 SHA1Final(sha1, &ctx);
527 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
528 return got_error(GOT_ERR_FILEIDX_CSUM);
534 in_same_subdir(struct got_fileindex_entry *ie, const char *parent_path,
535 struct got_tree_entry *te)
537 size_t parent_len = strlen(parent_path);
540 if (!got_path_is_child(ie->path, parent_path, parent_len))
543 ie_name = ie->path + parent_len;
544 while (ie_name[0] == '/')
547 return strchr(ie_name, '/') == NULL;
551 cmp_entries(struct got_fileindex_entry *ie, const char *parent_path,
552 struct got_tree_entry *te)
554 size_t parent_len = strlen(parent_path);
557 if (!in_same_subdir(ie, parent_path, te)) {
559 return strcmp(ie->path, parent_path);
560 return strcmp(ie->path, te->name);
563 ie_name = ie->path + parent_len;
564 while (ie_name[0] == '/')
567 return strcmp(ie_name, te->name);
570 static const struct got_error *
571 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
572 struct got_tree_object *, const char *, struct got_repository *,
573 struct got_fileindex_diff_cb *, void *);
575 struct got_fileindex_entry *
576 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
578 struct got_fileindex_entry *next;
580 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
582 /* Skip entries which were newly added by diff callbacks. */
583 while (next && (next->flags & GOT_INDEX_ENTRY_F_INTENT_TO_ADD))
584 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
589 static const struct got_error *
590 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
591 struct got_fileindex_entry **ie, struct got_tree_entry *te,
592 const char *path, struct got_repository *repo,
593 struct got_fileindex_diff_cb *cb, void *cb_arg)
595 const struct got_error *err = NULL;
597 if (te && S_ISREG(te->mode)) {
598 *next = SIMPLEQ_NEXT(te, entry);
602 while (te && S_ISDIR(te->mode)) {
604 struct got_tree_object *subtree;
606 if (asprintf(&subpath, "%s%s%s", path,
607 path[0] == '\0' ? "" : "/", te->name) == -1)
608 return got_error_from_errno();
610 err = got_object_open_as_tree(&subtree, repo, te->id);
616 if (*ie == NULL || !in_same_subdir(*ie, path, te)) {
617 err = cb->diff_new(cb_arg, te, path);
622 err = diff_fileindex_tree(fileindex, ie, subtree,
623 subpath, repo, cb, cb_arg);
625 got_object_tree_close(subtree);
628 te = SIMPLEQ_NEXT(te, entry);
635 static const struct got_error *
636 diff_fileindex_tree(struct got_fileindex *fileindex,
637 struct got_fileindex_entry **ie, struct got_tree_object *tree,
638 const char *path, struct got_repository *repo,
639 struct got_fileindex_diff_cb *cb, void *cb_arg)
641 const struct got_error *err = NULL;
642 struct got_tree_entry *te = NULL;
643 size_t path_len = strlen(path);
644 const struct got_tree_entries *entries;
645 struct got_fileindex_entry *next;
647 entries = got_object_tree_get_entries(tree);
648 te = SIMPLEQ_FIRST(&entries->head);
651 int cmp = cmp_entries(*ie, path, te);
653 err = cb->diff_old_new(cb_arg, *ie, te,
657 *ie = walk_fileindex(fileindex, *ie);
658 err = walk_tree(&te, fileindex, ie, te,
659 path, repo, cb, cb_arg);
660 } else if (cmp < 0 ) {
661 next = walk_fileindex(fileindex, *ie);
662 err = cb->diff_old(cb_arg, *ie, path);
667 err = cb->diff_new(cb_arg, te, path);
670 err = walk_tree(&te, fileindex, ie, te,
671 path, repo, cb, cb_arg);
676 next = walk_fileindex(fileindex, *ie);
677 err = cb->diff_old(cb_arg, *ie, path);
682 err = cb->diff_new(cb_arg, te, path);
685 err = walk_tree(&te, fileindex, ie, te, path, repo, cb,
690 } while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te);
695 const struct got_error *
696 got_fileindex_diff_tree(struct got_fileindex *fileindex,
697 struct got_tree_object *tree, struct got_repository *repo,
698 struct got_fileindex_diff_cb *cb, void *cb_arg)
700 struct got_fileindex_entry *min;
701 min = RB_MIN(got_fileindex_tree, &fileindex->entries);
702 return diff_fileindex_tree(fileindex, &min, tree, "", repo, cb, cb_arg);
705 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);