]>
git.cameronkatri.com Git - trustcache.git/blob - trustcache.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
37 #include "trustcache.h"
40 main(int argc
, char **argv
)
44 fprintf(stderr
, "Usage: trustcache append [-f flags] [-u uuid | 0] infile file ...\n"
45 " trustcache create [-u uuid] [-v version] outfile file ...\n"
46 " trustcache info [-c] [-h] [-e entrynum] file\n"
47 " trustcache remove [-k] file hash ...\n\n"
48 "See trustcache(1) for more information\n");
54 if (strcmp(argv
[1], "info") == 0)
55 ret
= tcinfo(argc
- 1, argv
+ 1);
56 else if (strcmp(argv
[1], "create") == 0)
57 ret
= tccreate(argc
- 1, argv
+ 1);
58 else if (strcmp(argv
[1], "append") == 0)
59 ret
= tcappend(argc
- 1, argv
+ 1);
60 else if (strcmp(argv
[1], "remove") == 0)
61 ret
= tcremove(argc
- 1, argv
+ 1);
63 fprintf(stderr
, "Unknown subcommand %s\n", argv
[1]);
72 opentrustcache(const char *path
)
75 struct trust_cache cache
;
77 if ((f
= fopen(path
, "r")) == NULL
) {
78 fprintf(stderr
, "%s: %s\n", path
, strerror(errno
));
82 fread(&cache
, sizeof(struct trust_cache
) - sizeof(struct trust_cache_entry1
*), 1, f
);
83 cache
.version
= le32toh(cache
.version
);
84 cache
.num_entries
= le32toh(cache
.num_entries
);
86 if (cache
.version
== 0) {
87 if ((cache
.hashes
= calloc(cache
.num_entries
, sizeof(trust_cache_hash0
))) == NULL
)
89 fread(cache
.hashes
, sizeof(trust_cache_hash0
), cache
.num_entries
, f
);
90 } else if (cache
.version
== 1) {
91 if ((cache
.entries
= calloc(cache
.num_entries
, sizeof(struct trust_cache_entry1
))) == NULL
)
93 fread(cache
.entries
, sizeof(struct trust_cache_entry1
), cache
.num_entries
, f
);
95 fprintf(stderr
, "%s: Unsupported version %i\n", path
, cache
.version
);
104 writetrustcache(struct trust_cache cache
, const char *path
)
107 if ((f
= fopen(path
, "wb")) == NULL
) {
108 fprintf(stderr
, "%s: %s\n", path
, strerror(errno
));
112 cache
.version
= htole32(cache
.version
);
113 cache
.num_entries
= htole32(cache
.num_entries
);
114 fwrite(&cache
, sizeof(struct trust_cache
) - sizeof(struct trust_cache_entry1
*), 1, f
);
115 cache
.version
= le32toh(cache
.version
);
116 cache
.num_entries
= le32toh(cache
.num_entries
);
118 for (uint32_t i
= 0; i
< cache
.num_entries
; i
++) {
119 if (cache
.version
== 0)
120 fwrite(&cache
.hashes
[i
], sizeof(trust_cache_hash0
), 1, f
);
121 else if (cache
.version
== 1)
122 fwrite(&cache
.entries
[i
], sizeof(struct trust_cache_entry1
), 1, f
);