resource.h revision 8b25d1ad
1/*-
2 *   BSD LICENSE
3 *
4 *   Copyright(c) 2016 RehiveTech. All rights reserved.
5 *   All rights reserved.
6 *
7 *   Redistribution and use in source and binary forms, with or without
8 *   modification, are permitted provided that the following conditions
9 *   are met:
10 *
11 *     * Redistributions of source code must retain the above copyright
12 *       notice, this list of conditions and the following disclaimer.
13 *     * Redistributions in binary form must reproduce the above copyright
14 *       notice, this list of conditions and the following disclaimer in
15 *       the documentation and/or other materials provided with the
16 *       distribution.
17 *     * Neither the name of RehiveTech nor the names of its
18 *       contributors may be used to endorse or promote products derived
19 *       from this software without specific prior written permission.
20 *
21 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#ifndef _RESOURCE_H_
35#define _RESOURCE_H_
36
37/**
38 * @file
39 *
40 * Test Resource API
41 *
42 * Each test can require and use some external resources. Usually, an external
43 * resource is a file or a filesystem sub-hierarchy. A resource is included
44 * inside the test executable.
45 */
46
47#include <sys/queue.h>
48#include <stdio.h>
49#include <stddef.h>
50
51#include <rte_eal.h>
52#include <rte_common.h>
53
54TAILQ_HEAD(resource_list, resource);
55extern struct resource_list resource_list;
56
57/**
58 * Representation of a resource. It points to the resource's binary data.
59 * The semantics of the binary data are defined by the target test.
60 */
61struct resource {
62	const char *name;  /**< Unique name of the resource */
63	const char *begin; /**< Start of resource data */
64	const char *end;   /**< End of resource data */
65	TAILQ_ENTRY(resource) next;
66};
67
68/**
69 * @return size of the given resource
70 */
71size_t resource_size(const struct resource *r);
72
73/**
74 * Find a resource by name in the global list of resources.
75 */
76const struct resource *resource_find(const char *name);
77
78/**
79 * Write the raw data of the resource to the given file.
80 * @return 0 on success
81 */
82int resource_fwrite(const struct resource *r, FILE *f);
83
84/**
85 * Write the raw data of the resource to the given file given by name.
86 * The name is relative to the current working directory.
87 * @return 0 on success
88 */
89int resource_fwrite_file(const struct resource *r, const char *fname);
90
91/**
92 * Treat the given resource as a tar archive. Extract
93 * the archive to the current directory.
94 */
95int resource_untar(const struct resource *res);
96
97/**
98 * Treat the given resource as a tar archive. Remove
99 * all files (related to the current directory) listed
100 * in the tar archive.
101 */
102int resource_rm_by_tar(const struct resource *res);
103
104/**
105 * Register a resource in the global list of resources.
106 * Not intended for direct use, please check the REGISTER_RESOURCE
107 * macro.
108 */
109void resource_register(struct resource *r);
110
111/**
112 * Definition of a resource linked externally (by means of the used toolchain).
113 * Only the base name of the resource is expected. The name refers to the
114 * linked pointers beg_<name> and end_<name> provided externally.
115 */
116#define REGISTER_LINKED_RESOURCE(n) \
117extern const char beg_ ##n;         \
118extern const char end_ ##n;         \
119REGISTER_RESOURCE(n, &beg_ ##n, &end_ ##n) \
120
121/**
122 * Definition of a resource described by its name, and pointers begin, end.
123 */
124#define REGISTER_RESOURCE(n, b, e) \
125static struct resource linkres_ ##n = {       \
126	.name = RTE_STR(n),     \
127	.begin = b,             \
128	.end = e,               \
129};                              \
130static void __attribute__((constructor, used)) resinitfn_ ##n(void) \
131{                               \
132	resource_register(&linkres_ ##n);  \
133}
134
135#endif
136