1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016 RehiveTech. All rights reserved.
10 #include <rte_debug.h>
14 struct resource_list resource_list = TAILQ_HEAD_INITIALIZER(resource_list);
16 size_t resource_size(const struct resource *r)
18 return r->end - r->begin;
21 const struct resource *resource_find(const char *name)
25 TAILQ_FOREACH(r, &resource_list, next) {
28 if (!strcmp(r->name, name))
35 int resource_fwrite(const struct resource *r, FILE *f)
37 const size_t goal = resource_size(r);
40 while (total < goal) {
41 size_t wlen = fwrite(r->begin + total, 1, goal - total, f);
53 int resource_fwrite_file(const struct resource *r, const char *fname)
58 f = fopen(fname, "w");
64 ret = resource_fwrite(r, f);
69 #ifdef RTE_APP_TEST_RESOURCE_TAR
71 #include <archive_entry.h>
73 static int do_copy(struct archive *r, struct archive *w)
77 #if ARCHIVE_VERSION_NUMBER >= 3000000
85 ret = archive_read_data_block(r, &buf, &len, &off);
86 if (ret == ARCHIVE_RETRY)
89 if (ret == ARCHIVE_EOF)
92 if (ret != ARCHIVE_OK)
96 ret = archive_write_data_block(w, buf, len, off);
97 if (ret != ARCHIVE_OK && ret != ARCHIVE_RETRY)
99 } while (ret != ARCHIVE_OK);
103 int resource_untar(const struct resource *res)
107 struct archive_entry *e;
112 p = malloc(resource_size(res));
114 rte_panic("Failed to malloc %zu B\n", resource_size(res));
116 memcpy(p, res->begin, resource_size(res));
118 r = archive_read_new();
124 archive_read_support_format_all(r);
125 archive_read_support_filter_all(r);
127 w = archive_write_disk_new();
129 archive_read_free(r);
134 flags |= ARCHIVE_EXTRACT_PERM;
135 flags |= ARCHIVE_EXTRACT_FFLAGS;
136 archive_write_disk_set_options(w, flags);
137 archive_write_disk_set_standard_lookup(w);
139 ret = archive_read_open_memory(r, p, resource_size(res));
140 if (ret != ARCHIVE_OK)
144 ret = archive_read_next_header(r, &e);
145 if (ret == ARCHIVE_EOF)
147 if (ret != ARCHIVE_OK)
150 ret = archive_write_header(w, e);
151 if (ret == ARCHIVE_EOF)
153 if (ret != ARCHIVE_OK)
156 if (archive_entry_size(e) == 0)
160 if (ret != ARCHIVE_OK)
163 ret = archive_write_finish_entry(w);
164 if (ret != ARCHIVE_OK)
168 archive_write_free(w);
169 archive_read_free(r);
174 archive_write_free(w);
175 archive_read_free(r);
177 rte_panic("Failed: %s\n", archive_error_string(r));
181 int resource_rm_by_tar(const struct resource *res)
184 struct archive_entry *e;
190 p = malloc(resource_size(res));
192 rte_panic("Failed to malloc %zu B\n", resource_size(res));
194 memcpy(p, res->begin, resource_size(res));
197 * If somebody creates a file somewhere inside the extracted TAR
198 * hierarchy during a test the resource_rm_by_tar might loop
199 * infinitely. We prevent this by adding the attempts counter there.
200 * In normal case, max N iteration is done where N is the depth of
201 * the file-hierarchy.
203 while (try_again && attempts < 10000) {
204 r = archive_read_new();
210 archive_read_support_format_all(r);
211 archive_read_support_filter_all(r);
213 ret = archive_read_open_memory(r, p, resource_size(res));
214 if (ret != ARCHIVE_OK) {
215 fprintf(stderr, "Failed: %s\n",
216 archive_error_string(r));
223 ret = archive_read_next_header(r, &e);
224 if (ret == ARCHIVE_EOF)
226 if (ret != ARCHIVE_OK)
229 ret = remove(archive_entry_pathname(e));
237 /* should not usually happen: */
244 perror("Failed to remove file");
250 archive_read_free(r);
254 if (attempts >= 10000) {
255 fprintf(stderr, "Failed to remove archive\n");
264 archive_read_free(r);
267 rte_panic("Failed: %s\n", archive_error_string(r));
271 #endif /* RTE_APP_TEST_RESOURCE_TAR */
273 void resource_register(struct resource *r)
275 TAILQ_INSERT_TAIL(&resource_list, r, next);