aboutsummaryrefslogtreecommitdiffstats
path: root/cache_from_tree.c
diff options
context:
space:
mode:
authorCameron Katri <me@cameronkatri.com>2022-05-20 10:42:38 -0400
committerCameron Katri <me@cameronkatri.com>2022-05-20 10:42:38 -0400
commitaa035f73ce081b3f07247bd15860d72355a096b2 (patch)
tree170a83f5aa816f936df5ae906f20da3fb6b9404e /cache_from_tree.c
downloadtrustcache-aa035f73ce081b3f07247bd15860d72355a096b2.tar.gz
trustcache-aa035f73ce081b3f07247bd15860d72355a096b2.tar.zst
trustcache-aa035f73ce081b3f07247bd15860d72355a096b2.zip
Initial import
Diffstat (limited to 'cache_from_tree.c')
-rw-r--r--cache_from_tree.c86
1 files changed, 86 insertions, 0 deletions
diff --git a/cache_from_tree.c b/cache_from_tree.c
new file mode 100644
index 0000000..92a2210
--- /dev/null
+++ b/cache_from_tree.c
@@ -0,0 +1,86 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2022 Cameron Katri. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY CAMERON KATRI AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL CAMERON KATRI OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <ftw.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "trustcache.h"
+#include "machoparse/cdhash.h"
+
+static struct trust_cache cache = {};
+
+int
+tccallback(const char *path, const struct stat *sb, int typeflag, struct FTW *ftw)
+{
+ if (!S_ISREG(sb->st_mode))
+ return 0;
+
+ struct cdhashes c = {};
+ c.count = 0;
+ find_cdhash(path, sb, &c);
+
+ if (c.count == 0)
+ return 0;
+
+ for (int i = 0; i < c.count; i++) {
+ if (cache.version == 0) {
+ if ((cache.hashes = realloc(cache.hashes, sizeof(trust_cache_hash0) * (cache.num_entries + 1))) == NULL)
+ exit(1);
+ memcpy(cache.hashes[cache.num_entries], c.h[i].cdhash, CS_CDHASH_LEN);
+ } else if (cache.version == 1) {
+ if ((cache.entries = realloc(cache.entries, sizeof(struct trust_cache_entry1) * (cache.num_entries + 1))) == NULL)
+ exit(1);
+ cache.entries[cache.num_entries].hash_type = c.h[i].hash_type;
+ cache.entries[cache.num_entries].flags = 0;
+ memcpy(cache.entries[cache.num_entries].cdhash, c.h[i].cdhash, CS_CDHASH_LEN);
+ }
+ cache.num_entries++;
+ }
+
+ free(c.h);
+
+ return 0;
+}
+
+struct trust_cache
+cache_from_tree(const char *path, uint32_t version)
+{
+ struct trust_cache ret = {};
+ cache.version = version;
+ cache.num_entries = 0;
+ ret.version = version;
+
+ if (nftw(path, tccallback, 20, 0) == -1) {
+ perror("nftw");
+ return ret;
+ }
+
+ ret.num_entries = cache.num_entries;
+ ret.hashes = cache.hashes;
+ return ret;
+}