]>
git.cameronkatri.com Git - trustcache.git/blob - remove.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
36 #include "trustcache.h"
37 #include "uuid/uuid.h"
40 tcremove(int argc
, char **argv
)
42 bool keepuuid
= false;
46 while ((ch
= getopt(argc
, argv
, "k")) != -1) {
61 struct trust_cache cache
= opentrustcache(argv
[0]);
64 uuid_generate(cache
.uuid
);
66 uint8_t hash
[CS_CDHASH_LEN
] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
68 for (int i
= 1; i
< argc
; i
++) {
69 if (strlen(argv
[i
]) != 40) {
70 fprintf(stderr
, "%s is not a valid CDHash\n", argv
[i
]);
73 for (size_t j
= 0; j
< CS_CDHASH_LEN
; j
++)
74 sscanf(argv
[i
] + 2 * j
, "%02hhx", &hash
[j
]);
77 while (j
< cache
.num_entries
) {
78 if (cache
.version
== 0) {
79 if (memcmp(cache
.hashes
[j
], hash
, CS_CDHASH_LEN
) == 0) {
80 memmove(&cache
.hashes
[j
], &cache
.hashes
[j
+ 1], (cache
.num_entries
- j
- 1) * sizeof(trust_cache_hash0
));
85 } else if (cache
.version
== 1) {
86 if (memcmp(cache
.entries
[j
].cdhash
, hash
, CS_CDHASH_LEN
) == 0) {
87 memmove(&cache
.entries
[j
], &cache
.entries
[j
+ 1], (cache
.num_entries
- j
- 1) * sizeof(struct trust_cache_entry1
));
95 for (size_t j
= 0; j
< CS_CDHASH_LEN
; j
++)
99 if ((f
= fopen(argv
[0], "wb")) == NULL
) {
100 fprintf(stderr
, "%s: %s\n", argv
[0], strerror(errno
));
104 cache
.version
= htole32(cache
.version
);
105 cache
.num_entries
= htole32(cache
.num_entries
);
106 fwrite(&cache
, sizeof(struct trust_cache
) - sizeof(struct trust_cache_entry1
*), 1, f
);
107 cache
.version
= le32toh(cache
.version
);
108 cache
.num_entries
= le32toh(cache
.num_entries
);
110 for (uint32_t i
= 0; i
< cache
.num_entries
; i
++) {
111 if (cache
.version
== 0)
112 fwrite(&cache
.hashes
[i
], sizeof(trust_cache_hash0
), 1, f
);
113 else if (cache
.version
== 1)
114 fwrite(&cache
.entries
[i
], sizeof(struct trust_cache_entry1
), 1, f
);
117 printf("Removed %i %s\n", numremoved
, numremoved
== 1 ? "entry" : "entries");