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 <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sha1.h>
25 #include <endian.h>
26 #include <limits.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;
36 int nentries;
37 #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
38 };
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)
43 {
44 struct stat sb;
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_FILEIDX_MODE_SYMLINK;
58 else
59 entry->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
60 entry->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
61 GOT_FILEIDX_MODE_PERMS_SHIFT);
62 memcpy(entry->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
63 memcpy(entry->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
65 return NULL;
66 }
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,
71 uint8_t *commit_sha1)
72 {
73 size_t len;
75 *entry = calloc(1, sizeof(**entry));
76 if (*entry == NULL)
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();
82 free(*entry);
83 *entry = NULL;
84 return err;
85 }
87 len = strlen(relpath);
88 if (len > GOT_FILEIDX_F_PATH_LEN)
89 len = GOT_FILEIDX_F_PATH_LEN;
90 (*entry)->flags |= len;
92 return got_fileindex_entry_update(*entry, ondisk_path, blob_sha1,
93 commit_sha1);
94 }
96 void
97 got_fileindex_entry_free(struct got_fileindex_entry *entry)
98 {
99 free(entry->path);
100 free(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++;
111 return NULL;
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_FILEIDX_F_INTENT_TO_ADD;
121 return add_entry(fileindex, entry);
124 void
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);
150 if (err)
151 return err;
153 return NULL;
156 struct got_fileindex *
157 got_fileindex_alloc(void)
159 struct got_fileindex *fileindex;
161 fileindex = calloc(1, sizeof(*fileindex));
162 if (fileindex == NULL)
163 return NULL;
165 RB_INIT(&fileindex->entries);
166 return fileindex;
169 void
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);
178 free(fileindex);
181 static const struct got_error *
182 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
184 size_t n;
186 val = htobe64(val);
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);
191 return NULL;
194 static const struct got_error *
195 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
197 size_t n;
199 val = htobe32(val);
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);
204 return NULL;
207 static const struct got_error *
208 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
210 size_t n;
212 val = htobe16(val);
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);
217 return NULL;
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 };
226 len = strlen(path);
227 while ((len + pad) % 8 != 0)
228 pad++;
229 if (pad == 0)
230 pad = 8; /* NUL-terminate */
232 SHA1Update(ctx, path, len);
233 n = fwrite(path, 1, len, outfile);
234 if (n != len)
235 return got_ferror(outfile, GOT_ERR_IO);
236 SHA1Update(ctx, zero, pad);
237 n = fwrite(zero, 1, pad, outfile);
238 if (n != pad)
239 return got_ferror(outfile, GOT_ERR_IO);
240 return NULL;
243 static const struct got_error *
244 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
245 FILE *outfile)
247 const struct got_error *err;
248 size_t n;
250 err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
251 if (err)
252 return err;
253 err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
254 if (err)
255 return err;
256 err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
257 if (err)
258 return err;
259 err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
260 if (err)
261 return err;
263 err = write_fileindex_val32(ctx, entry->uid, outfile);
264 if (err)
265 return err;
266 err = write_fileindex_val32(ctx, entry->gid, outfile);
267 if (err)
268 return err;
269 err = write_fileindex_val32(ctx, entry->size, outfile);
270 if (err)
271 return err;
273 err = write_fileindex_val16(ctx, entry->mode, outfile);
274 if (err)
275 return err;
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);
288 if (err)
289 return err;
291 err = write_fileindex_path(ctx, entry->path, outfile);
292 return err;
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;
300 SHA1_CTX ctx;
301 uint8_t sha1[SHA1_DIGEST_LENGTH];
302 size_t n;
303 struct got_fileindex_entry *entry;
305 SHA1Init(&ctx);
307 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
308 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
309 hdr.nentries = htobe32(fileindex->nentries);
311 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
312 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
313 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
314 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
315 if (n != sizeof(hdr.signature))
316 return got_ferror(outfile, GOT_ERR_IO);
317 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
318 if (n != sizeof(hdr.version))
319 return got_ferror(outfile, GOT_ERR_IO);
320 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
321 if (n != sizeof(hdr.nentries))
322 return got_ferror(outfile, GOT_ERR_IO);
324 RB_FOREACH(entry, got_fileindex_tree, &fileindex->entries) {
325 entry->flags &= ~GOT_FILEIDX_F_INTENT_TO_ADD;
326 err = write_fileindex_entry(&ctx, entry, outfile);
327 if (err)
328 return err;
331 SHA1Final(sha1, &ctx);
332 n = fwrite(sha1, 1, sizeof(sha1), outfile);
333 if (n != sizeof(sha1))
334 return got_ferror(outfile, GOT_ERR_IO);
336 if (fflush(outfile) != 0)
337 return got_error_from_errno();
339 return NULL;
342 static const struct got_error *
343 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
345 size_t n;
347 n = fread(val, 1, sizeof(*val), infile);
348 if (n != sizeof(*val))
349 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
350 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
351 *val = be64toh(*val);
352 return NULL;
355 static const struct got_error *
356 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
358 size_t n;
360 n = fread(val, 1, sizeof(*val), infile);
361 if (n != sizeof(*val))
362 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
363 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
364 *val = be32toh(*val);
365 return NULL;
368 static const struct got_error *
369 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
371 size_t n;
373 n = fread(val, 1, sizeof(*val), infile);
374 if (n != sizeof(*val))
375 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
376 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
377 *val = be16toh(*val);
378 return NULL;
381 static const struct got_error *
382 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
384 const struct got_error *err = NULL;
385 uint8_t buf[8];
386 size_t n, len = 0, totlen = sizeof(buf);
388 *path = malloc(totlen);
389 if (*path == NULL)
390 return got_error_from_errno();
392 do {
393 n = fread(buf, 1, sizeof(buf), infile);
394 if (n != sizeof(buf))
395 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
396 if (len + sizeof(buf) > totlen) {
397 char *p = reallocarray(*path, totlen + sizeof(buf), 1);
398 if (p == NULL) {
399 err = got_error_from_errno();
400 break;
402 totlen += sizeof(buf);
403 *path = p;
405 SHA1Update(ctx, buf, sizeof(buf));
406 memcpy(*path + len, buf, sizeof(buf));
407 len += sizeof(buf);
408 } while (memchr(buf, '\0', sizeof(buf)) == NULL);
410 if (err) {
411 free(*path);
412 *path = NULL;
414 return err;
417 static const struct got_error *
418 read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
419 FILE *infile)
421 const struct got_error *err;
422 struct got_fileindex_entry *entry;
423 size_t n;
425 *entryp = NULL;
427 entry = calloc(1, sizeof(*entry));
428 if (entry == NULL)
429 return got_error_from_errno();
431 err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
432 if (err)
433 goto done;
434 err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
435 if (err)
436 goto done;
437 err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
438 if (err)
439 goto done;
440 err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
441 if (err)
442 goto done;
444 err = read_fileindex_val32(&entry->uid, ctx, infile);
445 if (err)
446 goto done;
447 err = read_fileindex_val32(&entry->gid, ctx, infile);
448 if (err)
449 goto done;
450 err = read_fileindex_val32(&entry->size, ctx, infile);
451 if (err)
452 goto done;
454 err = read_fileindex_val16(&entry->mode, ctx, infile);
455 if (err)
456 goto done;
458 n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
459 if (n != SHA1_DIGEST_LENGTH) {
460 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
461 goto done;
463 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
465 n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
466 if (n != SHA1_DIGEST_LENGTH) {
467 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
468 goto done;
470 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
472 err = read_fileindex_val32(&entry->flags, ctx, infile);
473 if (err)
474 goto done;
476 err = read_fileindex_path(&entry->path, ctx, infile);
477 done:
478 if (err)
479 free(entry);
480 else
481 *entryp = entry;
482 return err;
485 const struct got_error *
486 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
488 const struct got_error *err = NULL;
489 struct got_fileindex_hdr hdr;
490 SHA1_CTX ctx;
491 struct got_fileindex_entry *entry;
492 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
493 uint8_t sha1[SHA1_DIGEST_LENGTH];
494 size_t n;
495 int i;
497 SHA1Init(&ctx);
499 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
500 if (n != sizeof(hdr.signature)) {
501 if (n == 0) /* EOF */
502 return NULL;
503 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
505 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
506 if (n != sizeof(hdr.version)) {
507 if (n == 0) /* EOF */
508 return NULL;
509 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
511 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
512 if (n != sizeof(hdr.nentries)) {
513 if (n == 0) /* EOF */
514 return NULL;
515 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
518 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
519 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
520 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
522 hdr.signature = be32toh(hdr.signature);
523 hdr.version = be32toh(hdr.version);
524 hdr.nentries = be32toh(hdr.nentries);
526 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
527 return got_error(GOT_ERR_FILEIDX_SIG);
528 if (hdr.version != GOT_FILE_INDEX_VERSION)
529 return got_error(GOT_ERR_FILEIDX_VER);
531 for (i = 0; i < hdr.nentries; i++) {
532 err = read_fileindex_entry(&entry, &ctx, infile);
533 if (err)
534 return err;
535 err = add_entry(fileindex, entry);
536 if (err)
537 return err;
540 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
541 if (n != sizeof(sha1_expected))
542 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
543 SHA1Final(sha1, &ctx);
544 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
545 return got_error(GOT_ERR_FILEIDX_CSUM);
547 return NULL;
550 struct got_fileindex_entry *
551 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
553 struct got_fileindex_entry *next;
555 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
557 /* Skip entries which were newly added by diff callbacks. */
558 while (next && (next->flags & GOT_FILEIDX_F_INTENT_TO_ADD))
559 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
561 return next;
564 static const struct got_error *
565 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
566 struct got_tree_object *, const char *, struct got_repository *,
567 struct got_fileindex_diff_cb *, void *);
569 static const struct got_error *
570 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
571 struct got_fileindex_entry **ie, struct got_tree_entry *te,
572 const char *path, struct got_repository *repo,
573 struct got_fileindex_diff_cb *cb, void *cb_arg)
575 const struct got_error *err = NULL;
577 if (S_ISDIR(te->mode)) {
578 char *subpath;
579 struct got_tree_object *subtree;
581 if (asprintf(&subpath, "%s%s%s", path,
582 path[0] == '\0' ? "" : "/", te->name) == -1)
583 return got_error_from_errno();
585 err = got_object_open_as_tree(&subtree, repo, te->id);
586 if (err) {
587 free(subpath);
588 return err;
591 err = diff_fileindex_tree(fileindex, ie, subtree,
592 subpath, repo, cb, cb_arg);
593 free(subpath);
594 got_object_tree_close(subtree);
595 if (err)
596 return err;
599 *next = SIMPLEQ_NEXT(te, entry);
600 return NULL;
603 /*
604 * Decide whether a fileindex entry path is equivalent to a tree entry path,
605 * and if it is not, then decide which of the two should be processed first.
606 */
607 static int
608 cmp_entries(const char *ie_path, const char *parent_path,
609 size_t parent_len, const char *te_name)
611 int cmp = strncmp(ie_path, parent_path, parent_len);
612 if (cmp == 0) {
613 const char *ie_name = ie_path + parent_len;
614 while (ie_name[0] == '/')
615 ie_name++;
616 cmp = strcmp(ie_name, te_name);
618 return cmp;
622 static const struct got_error *
623 diff_fileindex_tree(struct got_fileindex *fileindex,
624 struct got_fileindex_entry **ie, struct got_tree_object *tree,
625 const char *path, struct got_repository *repo,
626 struct got_fileindex_diff_cb *cb, void *cb_arg)
628 const struct got_error *err = NULL;
629 struct got_tree_entry *te = NULL;
630 size_t path_len = strlen(path);
631 const struct got_tree_entries *entries;
632 struct got_fileindex_entry *next;
634 entries = got_object_tree_get_entries(tree);
635 te = SIMPLEQ_FIRST(&entries->head);
636 do {
637 if (te && *ie) {
638 int cmp = cmp_entries((*ie)->path, path, path_len,
639 te->name);
640 if (cmp == 0) {
641 err = cb->diff_old_new(cb_arg, *ie, te,
642 path);
643 if (err)
644 break;
645 *ie = walk_fileindex(fileindex, *ie);
646 err = walk_tree(&te, fileindex, ie, te,
647 path, repo, cb, cb_arg);
648 } else if (cmp < 0 ) {
649 next = walk_fileindex(fileindex, *ie);
650 err = cb->diff_old(cb_arg, *ie, path);
651 if (err)
652 break;
653 *ie = next;
654 } else {
655 err = cb->diff_new(cb_arg, te, path);
656 if (err)
657 break;
658 err = walk_tree(&te, fileindex, ie, te,
659 path, repo, cb, cb_arg);
661 if (err)
662 break;
663 } else if (*ie) {
664 next = walk_fileindex(fileindex, *ie);
665 err = cb->diff_old(cb_arg, *ie, path);
666 if (err)
667 break;
668 *ie = next;
669 } else if (te) {
670 err = cb->diff_new(cb_arg, te, path);
671 if (err)
672 break;
673 err = walk_tree(&te, fileindex, ie, te, path, repo, cb,
674 cb_arg);
675 if (err)
676 break;
678 } while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te);
680 return err;
683 const struct got_error *
684 got_fileindex_diff_tree(struct got_fileindex *fileindex,
685 struct got_tree_object *tree, struct got_repository *repo,
686 struct got_fileindex_diff_cb *cb, void *cb_arg)
688 struct got_fileindex_entry *min;
689 min = RB_MIN(got_fileindex_tree, &fileindex->entries);
690 return diff_fileindex_tree(fileindex, &min, tree, "", repo, cb, cb_arg);
693 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);