librazor/razor-internal.h
author ali <j.a.harlow@letterboxes.org>
Thu Apr 14 11:59:56 2016 +0100 (2016-04-14)
changeset 467 4b7270fd3201
parent 458 3f841a46eab5
child 475 008c75a5e08d
permissions -rw-r--r--
Release version 0.6.1
     1 /*
     2  * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
     3  * Copyright (C) 2008  Red Hat, Inc
     4  * Copyright (C) 2009, 2011-2012, 2014  J. Ali Harlow <ali@juiblex.co.uk>
     5  *
     6  * This program is free software; you can redistribute it and/or modify
     7  * it under the terms of the GNU General Public License as published by
     8  * the Free Software Foundation; either version 2 of the License, or
     9  * (at your option) any later version.
    10  *
    11  * This program is distributed in the hope that it will be useful,
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  * GNU General Public License for more details.
    15  *
    16  * You should have received a copy of the GNU General Public License along
    17  * with this program; if not, write to the Free Software Foundation, Inc.,
    18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    19  */
    20 
    21 #ifndef _RAZOR_INTERNAL_H_
    22 #define _RAZOR_INTERNAL_H_
    23 
    24 #ifdef MSWIN_API
    25 #include <windows.h>
    26 #endif
    27 #include <stdlib.h>
    28 #include <stdint.h>
    29 #include <stdarg.h>
    30 #include <unistd.h>
    31 
    32 #include "razor.h"
    33 #include "types/types.h"
    34 
    35 /* GCC extensions */
    36 #if defined(__GNUC__) && __GNUC__ >= 4
    37 #define RAZOR_EXPORT __attribute__ ((visibility("default")))
    38 #else
    39 #define RAZOR_EXPORT
    40 #endif
    41 
    42 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
    43 #define PADDING(value, base) (-(value) & (base - 1))
    44 #define ALIGN(value, base) ((value) + PADDING(value, base))
    45 void *zalloc(size_t size);
    46 
    47 struct razor_set_section {
    48 	uint32_t name;
    49 	uint32_t offset;
    50 	uint32_t size;
    51 };
    52 
    53 struct razor_set_header {
    54 	uint32_t magic;
    55 	uint32_t version;
    56 	uint32_t num_sections;
    57 };
    58 
    59 #define RAZOR_MAGIC 	0x525a4442
    60 
    61 #define RAZOR_STRING_POOL		"string_pool"
    62 #define RAZOR_PACKAGES			"packages"
    63 #define RAZOR_PROPERTIES		"properties"
    64 #define RAZOR_PACKAGE_POOL		"package_pool"
    65 #define RAZOR_PROPERTY_POOL		"property_pool"
    66 #define RAZOR_PREFIX_POOL		"prefix_pool"
    67 
    68 #define RAZOR_DETAILS_STRING_POOL	"details_string_pool"
    69 
    70 #define RAZOR_FILES			"files"
    71 #define RAZOR_FILE_POOL			"file_pool"
    72 #define RAZOR_FILE_STRING_POOL		"file_string_pool"
    73 
    74 struct razor_script {
    75 	uint32_t program;
    76 	uint32_t body;
    77 };
    78 
    79 struct razor_package {
    80 	unsigned int name  : 24;
    81 	unsigned int flags : 8;
    82 	uint32_t version;
    83 	uint32_t arch;
    84 	uint32_t summary;
    85 	uint32_t description;
    86 	uint32_t url;
    87 	uint32_t license;
    88 	struct list_head properties;
    89 	struct list_head files;
    90 	struct list_head install_prefixes;
    91 	struct razor_script preun;
    92 	struct razor_script postun;
    93 };
    94 
    95 
    96 struct razor_property {
    97 	uint32_t name;
    98 	uint32_t flags;
    99 	uint32_t version;
   100 	struct list_head packages;
   101 };
   102 
   103 struct razor_entry {
   104 	uint32_t name  : 24;
   105 	uint32_t flags : 8;
   106 	uint32_t start;
   107 	struct list_head packages;
   108 };
   109 
   110 #define RAZOR_ENTRY_LAST	0x80
   111 
   112 struct razor_set {
   113 	uint32_t header_version;
   114 	struct array string_pool;
   115  	struct array packages;
   116  	struct array properties;
   117  	struct array files;
   118 	struct array package_pool;
   119  	struct array property_pool;
   120  	struct array file_pool;
   121  	struct array prefix_pool;
   122 	struct array file_string_pool;
   123 	struct array details_string_pool;
   124 	struct razor_mapped_file *mapped_files;
   125 	int lock_fd, ref_count;
   126 	enum razor_set_flags flags;
   127 };
   128 
   129 struct import_entry {
   130 	uint32_t package;
   131 	char *name;
   132 };
   133 
   134 struct import_directory {
   135 	uint32_t name, count;
   136 	struct array files;
   137 	struct array packages;
   138 	struct import_directory *last;
   139 };
   140 
   141 struct razor_importer {
   142 	struct razor_set *set;
   143 	struct hashtable table;
   144 	struct hashtable file_table;
   145 	struct hashtable details_table;
   146 	struct razor_package *package;
   147 	struct array properties;
   148 	struct array files;
   149 	struct array file_requires;
   150 	struct array install_prefixes;
   151 };
   152 
   153 struct razor_package_iterator {
   154 	struct razor_set *set;
   155 	struct razor_package *package, *end;
   156 	struct list *index;
   157 	void *alloced_index;
   158 };
   159 
   160 void
   161 razor_package_iterator_init_for_property(struct razor_package_iterator *pi,
   162 					 struct razor_set *set,
   163 					 struct razor_property *property);
   164 
   165 struct razor_property_iterator {
   166 	struct razor_set *set;
   167 	struct razor_property *property, *end;
   168 	struct list *index;
   169 };
   170 
   171 struct razor_file_iterator {
   172 	struct razor_set *set;
   173 	struct array path;
   174 	struct list *index;
   175 	int post_order;
   176 };
   177 
   178 struct razor_string_iterator {
   179 	struct razor_set *set;
   180 	struct list *index;
   181 };
   182 
   183 int
   184 razor_set_aquire_lock(struct razor_set *set, const char *path, int exclusive);
   185 
   186 struct razor_entry *
   187 razor_set_find_entry(struct razor_set *set,
   188 		     struct razor_entry *dir, const char *pattern);
   189 
   190 struct razor_merger *
   191 razor_merger_create(struct razor_set *set1, struct razor_set *set2);
   192 void
   193 razor_merger_add_package(struct razor_merger *merger,
   194 			 struct razor_package *package);
   195 struct razor_set *
   196 razor_merger_commit(struct razor_merger *merger);
   197 void
   198 razor_merger_package_add_script(struct razor_merger *merger,
   199 				struct razor_package *package,
   200 				enum razor_property_flags script,
   201 				const char *program, const char *body);
   202 void
   203 razor_merger_destroy(struct razor_merger *merger);
   204 
   205 int run_lua_script(const char *root, const char *name, const char *body,
   206 		   ssize_t len, int arg1);
   207 
   208 int razor_run_script(const char *root, enum razor_property_flags script,
   209 		     const char *program, const char *body, int arg1);
   210 
   211 /* Utility functions */
   212 
   213 void
   214 razor_package_get_details_varg(struct razor_set *set,
   215 			       struct razor_package *package,
   216 			       va_list args);
   217 void razor_rpm_get_details_varg(struct razor_rpm *rpm, va_list args);
   218 
   219 int razor_create_dir(const char *root, const char *path);
   220 int razor_remove(const char *path);
   221 int razor_write(int fd, const void *data, size_t size);
   222 
   223 void *
   224 razor_file_get_contents(const char *filename, size_t *length, int private,
   225 			struct razor_error **error);
   226 int razor_file_free_contents(void *addr, size_t length);
   227 
   228 
   229 typedef int (*razor_compare_with_data_func_t)(const void *p1,
   230 					      const void *p,
   231 					      void *data);
   232 uint32_t *
   233 razor_qsort_with_data(void *base, size_t nelem, size_t size,
   234 		      razor_compare_with_data_func_t compare, void *data);
   235 
   236 struct environment {
   237 	int is_set;
   238 	struct array vars, string_pool;
   239 };
   240 
   241 void environment_init(struct environment *env);
   242 void environment_add_variable(struct environment *env,
   243 			      const char *variable, const char *value);
   244 void environment_set(struct environment *env);
   245 void environment_unset(struct environment *env);
   246 void environment_release(struct environment *env);
   247 
   248 #ifdef MSWIN_API
   249 char *razor_utf16_to_utf8(const wchar_t *utf16, int len);
   250 wchar_t *razor_utf8_to_utf16(const char *utf8, int len);
   251 #endif
   252 
   253 /* Error functions */
   254 struct razor_error {
   255 	int domain;
   256 	int code;
   257 	char *object;
   258 	char *str;
   259 	char *obj_str;
   260 	char *summary;
   261 	char *msg;
   262 };
   263 
   264 #define razor_error_new_posix(object) \
   265 	razor_error_new_str(RAZOR_POSIX_ERROR, errno, object, strerror(errno))
   266 #define razor_set_error_posix(error, object) \
   267 	if (error) \
   268 		*(error) = razor_error_new_posix(object); \
   269 	else
   270 
   271 #ifdef MSWIN_API
   272 struct razor_error *razor_error_new_mswin(const wchar_t *object, DWORD error);
   273 struct razor_error *razor_error_new_str2(int domain, int code,
   274 					 const wchar_t *object,
   275 					 const char *str);
   276 
   277 #define razor_set_error_mswin(error, object, err) \
   278 	if (error) \
   279 		*(error) = razor_error_new_mswin(object, err); \
   280 	else
   281 #define razor_set_error2(error, domain, code, object, str) \
   282 	if (error) \
   283 		*(error) = razor_error_new_str2(domain, code, object, str); \
   284 	else
   285 #endif	/* MSWIN_API */
   286 
   287 /* Atomic functions */
   288 
   289 #if HAVE_WINDOWS_KTM
   290 struct razor_atomic {
   291 	HANDLE transaction;
   292 	int n_files;
   293 	struct razor_atomic_file {
   294 		wchar_t *path;
   295 		HANDLE h;
   296 	} *files;
   297 	int in_undo;
   298 	struct razor_error *error;
   299 };
   300 #elif ENABLE_ATOMIC
   301 struct atomic_action {
   302 	struct atomic_action *next;
   303 	enum atomic_action_type {
   304 		/* Complex actions */
   305 		ACTION_MAKE_DIRS,
   306 		ACTION_REMOVE,
   307 		/* Primitive actions */
   308 		ACTION_CREATE_DIR,
   309 #if HAVE_SYMLINK
   310 		ACTION_CREATE_SYMLINK,
   311 #endif
   312 		ACTION_MOVE,
   313 	} type;
   314 	struct {
   315 		char *path;
   316 		union atomic_action_args {
   317 			struct {
   318 				char *root;
   319 			} make_dirs;
   320 			struct {
   321 				mode_t mode;
   322 			} create_dir;
   323 #if HAVE_SYMLINK
   324 			struct {
   325 				char *target;
   326 			} create_symlink;
   327 #endif
   328 			struct {
   329 				char *dest;
   330 			} move;
   331 		} u;
   332 	} args;
   333 };
   334 
   335 struct razor_atomic {
   336 	struct atomic_action *actions;
   337 	char *description;
   338 	char *toplevel;
   339 	unsigned next_file_tag;
   340 	int in_undo;
   341 	struct razor_error *error;
   342 };
   343 
   344 char *atomic_action_attic_tmpnam(struct razor_atomic *atomic);
   345 struct atomic_action *
   346 atomic_action_list_prepend(struct atomic_action *list,
   347 			   struct atomic_action *action);
   348 struct atomic_action *atomic_action_new(enum atomic_action_type type);
   349 void atomic_action_free(struct atomic_action *action);
   350 struct atomic_action *atomic_action_list_reverse(struct atomic_action *list);
   351 struct atomic_action *
   352 atomic_action_do(struct razor_atomic *atomic, struct atomic_action *action);
   353 void
   354 atomic_action_undo(struct razor_atomic *atomic, struct atomic_action *action);
   355 #else	/* !HAVE_WINDOWS_KTM && !ENABLE_ATOMIC */
   356 struct razor_atomic {
   357 	int in_undo;
   358 	struct razor_error *error;
   359 };
   360 #endif
   361 
   362 int razor_allow_all_root_names(void);
   363 int razor_valid_root_name(const char *name);
   364 
   365 #define RAZOR_ASCII_ISALPHA(c) \
   366 	((c) >= 'A' && (c) <= 'Z' || (c) >= 'a' && (c) <= 'z')
   367 
   368 #ifdef MSWIN_API
   369 #define SKIP_DRIVE_LETTER(path) \
   370 	((RAZOR_ASCII_ISALPHA(path[0]) && (path)[1] == ':') ? \
   371 	   (path) + 2 : (path))
   372 #else
   373 #define SKIP_DRIVE_LETTER(path) (path)
   374 #endif /* MSWIN_API */
   375 
   376 #endif /* _RAZOR_INTERNAL_H_ */