]>
git.cameronkatri.com Git - trustcache.git/blob - cache_from_tree.c
2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2022 Cameron Katri. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #define _XOPEN_SOURCE 500
34 #include "trustcache.h"
35 #include "machoparse/cdhash.h"
37 static struct trust_cache cache
= {};
40 tccallback(const char *path
, const struct stat
*sb
, __attribute__((unused
)) int typeflag
, __attribute__((unused
)) struct FTW
*ftw
)
42 if (!S_ISREG(sb
->st_mode
))
45 struct cdhashes c
= {};
47 find_cdhash(path
, sb
, &c
);
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
)
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
)
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 } else if (cache
.version
== 2) {
64 if ((cache
.entries2
= realloc(cache
.entries
, sizeof(struct trust_cache_entry2
) * (cache
.num_entries
+ 1))) == NULL
)
66 cache
.entries2
[cache
.num_entries
].hash_type
= c
.h
[i
].hash_type
;
67 cache
.entries2
[cache
.num_entries
].category
= 0;
68 memcpy(cache
.entries2
[cache
.num_entries
].cdhash
, c
.h
[i
].cdhash
, CS_CDHASH_LEN
);
79 cache_from_tree(const char *path
, uint32_t version
)
81 struct trust_cache ret
= {};
82 cache
.version
= version
;
83 cache
.num_entries
= 0;
84 ret
.version
= version
;
86 if (nftw(path
, tccallback
, 20, 0) == -1) {
87 // on macOS, nftw(3) will fail if the path is not a directory, but we don't want that
88 if (errno
== ENOTDIR
) {
90 if (stat(path
, &sb
) == 0 && tccallback(path
, &sb
, 0, NULL
) == 0)
98 ret
.num_entries
= cache
.num_entries
;
99 ret
.hashes
= cache
.hashes
;