librazor/razor-internal.h
author J. Ali Harlow <ali@juiblex.co.uk>
Wed Apr 29 17:00:01 2009 +0100 (2009-04-29)
changeset 361 2523d03a840e
parent 352 4866573c6944
child 364 66ec30bde5e5
permissions -rw-r--r--
Add support for preloading lua modules. This is useful both when
providing lua bindings to applications based on librazor and when
producing static binaries using librazor (where otherwise the lua
POSIX library would need to be included as an additional dynamic
object).
     1 /*
     2  * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
     3  * Copyright (C) 2008  Red Hat, Inc
     4  *
     5  * This program is free software; you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation; either version 2 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License along
    16  * with this program; if not, write to the Free Software Foundation, Inc.,
    17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    18  */
    19 
    20 #ifndef _RAZOR_INTERNAL_H_
    21 #define _RAZOR_INTERNAL_H_
    22 
    23 #include <stdlib.h>
    24 #include <stdint.h>
    25 #include <stdarg.h>
    26 #include <unistd.h>
    27 
    28 #include "razor.h"
    29 
    30 /* GCC visibility */
    31 #if defined(__GNUC__) && __GNUC__ >= 4
    32 #define RAZOR_EXPORT __attribute__ ((visibility("default")))
    33 #else
    34 #define RAZOR_EXPORT
    35 #endif
    36 
    37 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
    38 #define ALIGN(value, base) (((value) + (base - 1)) & ~((base) - 1))
    39 
    40 void *zalloc(size_t size);
    41 
    42 struct array {
    43 	void *data;
    44 	int size, alloc;
    45 };
    46 
    47 void array_init(struct array *array);
    48 void array_release(struct array *array);
    49 void *array_add(struct array *array, int size);
    50 
    51 
    52 struct list_head {
    53 	uint32_t list_ptr : 24;
    54 	uint32_t flags    : 8;
    55 };
    56 
    57 struct list {
    58 	uint32_t data  : 24;
    59 	uint32_t flags : 8;
    60 };
    61 
    62 void list_set_empty(struct list_head *head);
    63 void list_set_ptr(struct list_head *head, uint32_t ptr);
    64 void list_set_array(struct list_head *head, struct array *pool, struct array *items, int force_indirect);
    65 
    66 struct list *list_first(struct list_head *head, struct array *pool);
    67 struct list *list_next(struct list *list);
    68 
    69 void list_remap_pool(struct array *pool, uint32_t *map);
    70 void list_remap_head(struct list_head *list, uint32_t *map);
    71 
    72 
    73 struct hashtable {
    74 	struct array buckets;
    75 	struct array *pool;
    76 };
    77 
    78 void hashtable_init(struct hashtable *table, struct array *pool);
    79 void hashtable_release(struct hashtable *table);
    80 uint32_t hashtable_insert(struct hashtable *table, const char *key);
    81 uint32_t hashtable_lookup(struct hashtable *table, const char *key);
    82 uint32_t hashtable_tokenize(struct hashtable *table, const char *string);
    83 
    84 
    85 struct razor_set_section {
    86 	uint32_t name;
    87 	uint32_t offset;
    88 	uint32_t size;
    89 };
    90 
    91 struct razor_set_header {
    92 	uint32_t magic;
    93 	uint32_t version;
    94 	uint32_t num_sections;
    95 };
    96 
    97 #define RAZOR_MAGIC 	0x525a4442
    98 #define RAZOR_VERSION	1
    99 
   100 #define RAZOR_STRING_POOL		"string_pool"
   101 #define RAZOR_PACKAGES			"packages"
   102 #define RAZOR_PROPERTIES		"properties"
   103 #define RAZOR_PACKAGE_POOL		"package_pool"
   104 #define RAZOR_PROPERTY_POOL		"property_pool"
   105 
   106 #define RAZOR_DETAILS_STRING_POOL	"details_string_pool"
   107 
   108 #define RAZOR_FILES			"files"
   109 #define RAZOR_FILE_POOL			"file_pool"
   110 #define RAZOR_FILE_STRING_POOL		"file_string_pool"
   111 
   112 struct razor_package {
   113 	unsigned int name  : 24;
   114 	unsigned int flags : 8;
   115 	uint32_t version;
   116 	uint32_t arch;
   117 	uint32_t summary;
   118 	uint32_t description;
   119 	uint32_t url;
   120 	uint32_t license;
   121 	struct list_head properties;
   122 	struct list_head files;
   123 };
   124 
   125 
   126 struct razor_property {
   127 	uint32_t name;
   128 	uint32_t flags;
   129 	uint32_t version;
   130 	struct list_head packages;
   131 };
   132 
   133 struct razor_entry {
   134 	uint32_t name  : 24;
   135 	uint32_t flags : 8;
   136 	uint32_t start;
   137 	struct list_head packages;
   138 };
   139 
   140 #define RAZOR_ENTRY_LAST	0x80
   141 
   142 struct razor_set {
   143 	struct array string_pool;
   144  	struct array packages;
   145  	struct array properties;
   146  	struct array files;
   147 	struct array package_pool;
   148  	struct array property_pool;
   149  	struct array file_pool;
   150 	struct array file_string_pool;
   151 	struct array details_string_pool;
   152 
   153 	struct razor_set_header *header;
   154 	size_t header_size;
   155 
   156 	struct razor_set_header *details_header;
   157 	size_t details_header_size;
   158 
   159 	struct razor_set_header *files_header;
   160 	size_t files_header_size;
   161 };
   162 
   163 struct import_entry {
   164 	uint32_t package;
   165 	char *name;
   166 };
   167 
   168 struct import_directory {
   169 	uint32_t name, count;
   170 	struct array files;
   171 	struct array packages;
   172 	struct import_directory *last;
   173 };
   174 
   175 struct razor_importer {
   176 	struct razor_set *set;
   177 	struct hashtable table;
   178 	struct hashtable file_table;
   179 	struct hashtable details_table;
   180 	struct razor_package *package;
   181 	struct array properties;
   182 	struct array files;
   183 	struct array file_requires;
   184 };
   185 
   186 struct razor_package_iterator {
   187 	struct razor_set *set;
   188 	struct razor_package *package, *end;
   189 	struct list *index;
   190 	int free_index;
   191 };
   192 
   193 void
   194 razor_package_iterator_init_for_property(struct razor_package_iterator *pi,
   195 					 struct razor_set *set,
   196 					 struct razor_property *property);
   197 
   198 struct razor_property_iterator {
   199 	struct razor_set *set;
   200 	struct razor_property *property, *end;
   201 	struct list *index;
   202 };
   203 
   204 struct razor_file_iterator {
   205 	struct razor_set *set;
   206 	struct array path;
   207 	struct list *index;
   208 };
   209 
   210 struct razor_entry *
   211 razor_set_find_entry(struct razor_set *set,
   212 		     struct razor_entry *dir, const char *pattern);
   213 
   214 struct razor_merger *
   215 razor_merger_create(struct razor_set *set1, struct razor_set *set2);
   216 void
   217 razor_merger_add_package(struct razor_merger *merger,
   218 			 struct razor_package *package);
   219 struct razor_set *
   220 razor_merger_finish(struct razor_merger *merger);
   221 
   222 int run_lua_script(const char *root, const char *name, const char *body,
   223 		   ssize_t len);
   224 
   225 /* Utility functions */
   226 
   227 void
   228 razor_package_get_details_varg(struct razor_set *set,
   229 			       struct razor_package *package,
   230 			       va_list args);
   231 
   232 int razor_create_dir(const char *root, const char *path);
   233 int razor_write(int fd, const void *data, size_t size);
   234 
   235 void *razor_file_get_contents(const char *filename, size_t *length);
   236 int razor_file_free_contents(void *addr, size_t length);
   237 
   238 
   239 typedef int (*razor_compare_with_data_func_t)(const void *p1,
   240 					      const void *p,
   241 					      void *data);
   242 uint32_t *
   243 razor_qsort_with_data(void *base, size_t nelem, size_t size,
   244 		      razor_compare_with_data_func_t compare, void *data);
   245 
   246 #endif /* _RAZOR_INTERNAL_H_ */