]> git.cameronkatri.com Git - trustcache.git/blob - cache_from_tree.c
610653c7743c4e0fe4e992ec0e03e2399709c6d0
[trustcache.git] / cache_from_tree.c
1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2022 Cameron Katri. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY CAMERON KATRI AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL CAMERON KATRI OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #define _XOPEN_SOURCE 500
29 #include <errno.h>
30 #include <ftw.h>
31 #include <stdio.h>
32 #include <string.h>
33
34 #include "trustcache.h"
35 #include "machoparse/cdhash.h"
36
37 static struct trust_cache cache = {};
38
39 static int
40 tccallback(const char *path, const struct stat *sb, __attribute__((unused)) int typeflag, __attribute__((unused)) struct FTW *ftw)
41 {
42 if (!S_ISREG(sb->st_mode))
43 return 0;
44
45 struct cdhashes c = {};
46 c.count = 0;
47 find_cdhash(path, sb, &c);
48
49 if (c.count == 0)
50 return 0;
51
52 for (int i = 0; i < c.count; i++) {
53 if (cache.version == 0) {
54 if ((cache.hashes = realloc(cache.hashes, sizeof(trust_cache_hash0) * (cache.num_entries + 1))) == NULL)
55 exit(1);
56 memcpy(cache.hashes[cache.num_entries], c.h[i].cdhash, CS_CDHASH_LEN);
57 } else if (cache.version == 1) {
58 if ((cache.entries = realloc(cache.entries, sizeof(struct trust_cache_entry1) * (cache.num_entries + 1))) == NULL)
59 exit(1);
60 cache.entries[cache.num_entries].hash_type = c.h[i].hash_type;
61 cache.entries[cache.num_entries].flags = 0;
62 memcpy(cache.entries[cache.num_entries].cdhash, c.h[i].cdhash, CS_CDHASH_LEN);
63 }
64 cache.num_entries++;
65 }
66
67 free(c.h);
68
69 return 0;
70 }
71
72 struct trust_cache
73 cache_from_tree(const char *path, uint32_t version)
74 {
75 struct trust_cache ret = {};
76 cache.version = version;
77 cache.num_entries = 0;
78 ret.version = version;
79
80 if (nftw(path, tccallback, 20, 0) == -1) {
81 // on macOS, nftw(3) will fail if the path is not a directory, but we don't want that
82 if (errno == ENOTDIR) {
83 struct stat sb;
84 if (stat(path, &sb) == 0 && tccallback(path, &sb, 0, NULL) == 0)
85 goto done;
86 }
87 perror("nftw");
88 return ret;
89 }
90
91 done:
92 ret.num_entries = cache.num_entries;
93 ret.hashes = cache.hashes;
94 return ret;
95 }