Blob


1 /*
2 * Copyright (c) 2018 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_INDEX_ENTRY_MODE_SYMLINK;
58 else
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);
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_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,
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_INDEX_ENTRY_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 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
304 sizeof(hdr.nentries);
305 uint8_t buf[len];
306 struct got_fileindex_entry *entry;
308 SHA1Init(&ctx);
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);
317 if (n != len)
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);
323 if (err)
324 return err;
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);
332 return NULL;
335 static const struct got_error *
336 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
338 size_t n;
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);
345 return NULL;
348 static const struct got_error *
349 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
351 size_t n;
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);
358 return NULL;
361 static const struct got_error *
362 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
364 size_t n;
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);
371 return NULL;
374 static const struct got_error *
375 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
377 const struct got_error *err = NULL;
378 uint8_t buf[8];
379 size_t n, len = 0, totlen = sizeof(buf);
381 *path = malloc(totlen);
382 if (*path == NULL)
383 return got_error_from_errno();
385 do {
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);
391 if (p == NULL) {
392 err = got_error_from_errno();
393 break;
395 totlen += sizeof(buf);
396 *path = p;
398 SHA1Update(ctx, buf, sizeof(buf));
399 memcpy(*path + len, buf, sizeof(buf));
400 len += sizeof(buf);
401 } while (memchr(buf, '\0', sizeof(buf)) == NULL);
403 if (err) {
404 free(*path);
405 *path = NULL;
407 return err;
410 static const struct got_error *
411 read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
412 FILE *infile)
414 const struct got_error *err;
415 struct got_fileindex_entry *entry;
416 size_t n;
418 *entryp = NULL;
420 entry = calloc(1, sizeof(*entry));
421 if (entry == NULL)
422 return got_error_from_errno();
424 err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
425 if (err)
426 goto done;
427 err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
428 if (err)
429 goto done;
430 err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
431 if (err)
432 goto done;
433 err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
434 if (err)
435 goto done;
437 err = read_fileindex_val32(&entry->uid, ctx, infile);
438 if (err)
439 goto done;
440 err = read_fileindex_val32(&entry->gid, ctx, infile);
441 if (err)
442 goto done;
443 err = read_fileindex_val32(&entry->size, ctx, infile);
444 if (err)
445 goto done;
447 err = read_fileindex_val16(&entry->mode, ctx, infile);
448 if (err)
449 goto done;
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);
454 goto done;
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);
461 goto done;
463 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
465 err = read_fileindex_val32(&entry->flags, ctx, infile);
466 if (err)
467 goto done;
469 err = read_fileindex_path(&entry->path, ctx, infile);
470 done:
471 if (err)
472 free(entry);
473 else
474 *entryp = entry;
475 return err;
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;
483 SHA1_CTX ctx;
484 struct got_fileindex_entry *entry;
485 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
486 uint8_t sha1[SHA1_DIGEST_LENGTH];
487 size_t n;
488 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
489 sizeof(hdr.nentries);
490 uint8_t buf[len];
491 int i;
493 SHA1Init(&ctx);
495 n = fread(buf, 1, len, infile);
496 if (n != len) {
497 if (n == 0) /* EOF */
498 return NULL;
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);
516 if (err)
517 return err;
518 err = add_entry(fileindex, entry);
519 if (err)
520 return err;
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);
530 return NULL;
533 static int
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);
538 char *ie_name;
540 if (!got_path_is_child(ie->path, parent_path, parent_len))
541 return 0;
543 ie_name = ie->path + parent_len;
544 while (ie_name[0] == '/')
545 ie_name++;
547 return strchr(ie_name, '/') == NULL;
550 static int
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);
555 char *ie_name;
557 if (!in_same_subdir(ie, parent_path, te)) {
558 if (parent_path[0])
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] == '/')
565 ie_name++;
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);
586 return 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);
599 return NULL;
602 while (te && S_ISDIR(te->mode)) {
603 char *subpath;
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);
611 if (err) {
612 free(subpath);
613 return err;
616 if (*ie == NULL || !in_same_subdir(*ie, path, te)) {
617 err = cb->diff_new(cb_arg, te, path);
618 if (err)
619 return err;
622 err = diff_fileindex_tree(fileindex, ie, subtree,
623 subpath, repo, cb, cb_arg);
624 free(subpath);
625 got_object_tree_close(subtree);
626 if (err)
627 return err;
628 te = SIMPLEQ_NEXT(te, entry);
631 *next = te;
632 return NULL;
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);
649 do {
650 if (te && *ie) {
651 int cmp = cmp_entries(*ie, path, te);
652 if (cmp == 0) {
653 err = cb->diff_old_new(cb_arg, *ie, te,
654 path);
655 if (err)
656 break;
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);
663 if (err)
664 break;
665 *ie = next;
666 } else {
667 err = cb->diff_new(cb_arg, te, path);
668 if (err)
669 break;
670 err = walk_tree(&te, fileindex, ie, te,
671 path, repo, cb, cb_arg);
673 if (err)
674 break;
675 } else if (*ie) {
676 next = walk_fileindex(fileindex, *ie);
677 err = cb->diff_old(cb_arg, *ie, path);
678 if (err)
679 break;
680 *ie = next;
681 } else if (te) {
682 err = cb->diff_new(cb_arg, te, path);
683 if (err)
684 break;
685 err = walk_tree(&te, fileindex, ie, te, path, repo, cb,
686 cb_arg);
687 if (err)
688 break;
690 } while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te);
692 return err;
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);