1*4418919fSjohnjiang /* SPDX-License-Identifier: BSD-3-Clause 2*4418919fSjohnjiang * Copyright(c) 2016 RehiveTech. All rights reserved. 3*4418919fSjohnjiang */ 4*4418919fSjohnjiang 5*4418919fSjohnjiang #ifndef _RESOURCE_H_ 6*4418919fSjohnjiang #define _RESOURCE_H_ 7*4418919fSjohnjiang 8*4418919fSjohnjiang /** 9*4418919fSjohnjiang * @file 10*4418919fSjohnjiang * 11*4418919fSjohnjiang * Test Resource API 12*4418919fSjohnjiang * 13*4418919fSjohnjiang * Each test can require and use some external resources. Usually, an external 14*4418919fSjohnjiang * resource is a file or a filesystem sub-hierarchy. A resource is included 15*4418919fSjohnjiang * inside the test executable. 16*4418919fSjohnjiang */ 17*4418919fSjohnjiang 18*4418919fSjohnjiang #include <sys/queue.h> 19*4418919fSjohnjiang #include <stdio.h> 20*4418919fSjohnjiang #include <stddef.h> 21*4418919fSjohnjiang 22*4418919fSjohnjiang #include <rte_eal.h> 23*4418919fSjohnjiang #include <rte_common.h> 24*4418919fSjohnjiang 25*4418919fSjohnjiang TAILQ_HEAD(resource_list, resource); 26*4418919fSjohnjiang extern struct resource_list resource_list; 27*4418919fSjohnjiang 28*4418919fSjohnjiang /** 29*4418919fSjohnjiang * Representation of a resource. It points to the resource's binary data. 30*4418919fSjohnjiang * The semantics of the binary data are defined by the target test. 31*4418919fSjohnjiang */ 32*4418919fSjohnjiang struct resource { 33*4418919fSjohnjiang const char *name; /**< Unique name of the resource */ 34*4418919fSjohnjiang const char *begin; /**< Start of resource data */ 35*4418919fSjohnjiang const char *end; /**< End of resource data */ 36*4418919fSjohnjiang TAILQ_ENTRY(resource) next; 37*4418919fSjohnjiang }; 38*4418919fSjohnjiang 39*4418919fSjohnjiang /** 40*4418919fSjohnjiang * @return size of the given resource 41*4418919fSjohnjiang */ 42*4418919fSjohnjiang size_t resource_size(const struct resource *r); 43*4418919fSjohnjiang 44*4418919fSjohnjiang /** 45*4418919fSjohnjiang * Find a resource by name in the global list of resources. 46*4418919fSjohnjiang */ 47*4418919fSjohnjiang const struct resource *resource_find(const char *name); 48*4418919fSjohnjiang 49*4418919fSjohnjiang /** 50*4418919fSjohnjiang * Write the raw data of the resource to the given file. 51*4418919fSjohnjiang * @return 0 on success 52*4418919fSjohnjiang */ 53*4418919fSjohnjiang int resource_fwrite(const struct resource *r, FILE *f); 54*4418919fSjohnjiang 55*4418919fSjohnjiang /** 56*4418919fSjohnjiang * Write the raw data of the resource to the given file given by name. 57*4418919fSjohnjiang * The name is relative to the current working directory. 58*4418919fSjohnjiang * @return 0 on success 59*4418919fSjohnjiang */ 60*4418919fSjohnjiang int resource_fwrite_file(const struct resource *r, const char *fname); 61*4418919fSjohnjiang 62*4418919fSjohnjiang /** 63*4418919fSjohnjiang * Treat the given resource as a tar archive. Extract 64*4418919fSjohnjiang * the archive to the current directory. 65*4418919fSjohnjiang */ 66*4418919fSjohnjiang int resource_untar(const struct resource *res); 67*4418919fSjohnjiang 68*4418919fSjohnjiang /** 69*4418919fSjohnjiang * Treat the given resource as a tar archive. Remove 70*4418919fSjohnjiang * all files (related to the current directory) listed 71*4418919fSjohnjiang * in the tar archive. 72*4418919fSjohnjiang */ 73*4418919fSjohnjiang int resource_rm_by_tar(const struct resource *res); 74*4418919fSjohnjiang 75*4418919fSjohnjiang /** 76*4418919fSjohnjiang * Register a resource in the global list of resources. 77*4418919fSjohnjiang * Not intended for direct use, please check the REGISTER_RESOURCE 78*4418919fSjohnjiang * macro. 79*4418919fSjohnjiang */ 80*4418919fSjohnjiang void resource_register(struct resource *r); 81*4418919fSjohnjiang 82*4418919fSjohnjiang /** 83*4418919fSjohnjiang * Definition of a resource linked externally (by means of the used toolchain). 84*4418919fSjohnjiang * Only the base name of the resource is expected. The name refers to the 85*4418919fSjohnjiang * linked pointers beg_<name> and end_<name> provided externally. 86*4418919fSjohnjiang */ 87*4418919fSjohnjiang #define REGISTER_LINKED_RESOURCE(n) \ 88*4418919fSjohnjiang extern const char beg_ ##n; \ 89*4418919fSjohnjiang extern const char end_ ##n; \ 90*4418919fSjohnjiang REGISTER_RESOURCE(n, &beg_ ##n, &end_ ##n) \ 91*4418919fSjohnjiang 92*4418919fSjohnjiang /** 93*4418919fSjohnjiang * Definition of a resource described by its name, and pointers begin, end. 94*4418919fSjohnjiang */ 95*4418919fSjohnjiang #define REGISTER_RESOURCE(n, b, e) \ 96*4418919fSjohnjiang static struct resource linkres_ ##n = { \ 97*4418919fSjohnjiang .name = RTE_STR(n), \ 98*4418919fSjohnjiang .begin = b, \ 99*4418919fSjohnjiang .end = e, \ 100*4418919fSjohnjiang }; \ 101*4418919fSjohnjiang RTE_INIT(resinitfn_ ##n) \ 102*4418919fSjohnjiang { \ 103*4418919fSjohnjiang resource_register(&linkres_ ##n); \ 104*4418919fSjohnjiang } 105*4418919fSjohnjiang 106*4418919fSjohnjiang #endif 107