librazor/razor-internal.h
author J. Ali Harlow <ali@juiblex.co.uk>
Thu Jun 07 18:36:20 2018 +0100 (2018-06-07)
changeset 499 c89e5edb8eae
parent 498 5a49f274ab2d
permissions -rw-r--r--
Improve error on failure to lock database
     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, 2016  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 
    46 #define str_has_prefix(s, prefix)	(!strncmp(s, prefix, strlen(prefix)))
    47 
    48 void *zalloc(size_t size);
    49 
    50 struct razor_set_section {
    51 	uint32_t name;
    52 	uint32_t offset;
    53 	uint32_t size;
    54 };
    55 
    56 struct razor_set_header {
    57 	uint32_t magic;
    58 	uint32_t version;
    59 	uint32_t num_sections;
    60 };
    61 
    62 #define RAZOR_MAGIC 	0x525a4442
    63 
    64 #define RAZOR_STRING_POOL		"string_pool"
    65 #define RAZOR_PACKAGES			"packages"
    66 #define RAZOR_PROPERTIES		"properties"
    67 #define RAZOR_PACKAGE_POOL		"package_pool"
    68 #define RAZOR_PROPERTY_POOL		"property_pool"
    69 #define RAZOR_PREFIX_POOL		"prefix_pool"
    70 
    71 #define RAZOR_DETAILS_STRING_POOL	"details_string_pool"
    72 
    73 #define RAZOR_FILES			"files"
    74 #define RAZOR_FILE_POOL			"file_pool"
    75 #define RAZOR_FILE_STRING_POOL		"file_string_pool"
    76 
    77 struct razor_script {
    78 	uint32_t program;
    79 	uint32_t body;
    80 };
    81 
    82 struct razor_package {
    83 	unsigned int name  : 24;
    84 	unsigned int flags : 8;
    85 	uint32_t version;
    86 	uint32_t arch;
    87 	uint32_t summary;
    88 	uint32_t description;
    89 	uint32_t url;
    90 	uint32_t license;
    91 	struct list_head properties;
    92 	struct list_head files;
    93 	struct list_head install_prefixes;
    94 	struct razor_script preun;
    95 	struct razor_script postun;
    96 };
    97 
    98 
    99 struct razor_property {
   100 	uint32_t name;
   101 	uint32_t flags;
   102 	uint32_t version;
   103 	struct list_head packages;
   104 };
   105 
   106 struct razor_entry {
   107 	uint32_t name  : 24;
   108 	uint32_t flags : 8;
   109 	uint32_t start;
   110 	struct list_head packages;
   111 };
   112 
   113 #define RAZOR_ENTRY_LAST	0x80
   114 
   115 struct razor_set {
   116 	uint32_t header_version;
   117 	struct array string_pool;
   118  	struct array packages;
   119  	struct array properties;
   120  	struct array files;
   121 	struct array package_pool;
   122  	struct array property_pool;
   123  	struct array file_pool;
   124  	struct array prefix_pool;
   125 	struct array file_string_pool;
   126 	struct array details_string_pool;
   127 	struct razor_mapped_file *mapped_files;
   128 	int lock, ref_count;
   129 	enum razor_set_flags flags;
   130 };
   131 
   132 struct import_entry {
   133 	uint32_t package;
   134 	char *name;
   135 };
   136 
   137 struct import_directory {
   138 	uint32_t name, count;
   139 	struct array files;
   140 	struct array packages;
   141 	struct import_directory *last;
   142 };
   143 
   144 struct razor_importer {
   145 	struct razor_set *set;
   146 	struct hashtable table;
   147 	struct hashtable file_table;
   148 	struct hashtable details_table;
   149 	struct razor_package *package;
   150 	struct array properties;
   151 	struct array files;
   152 	struct array file_requires;
   153 	struct array install_prefixes;
   154 };
   155 
   156 struct razor_package_iterator {
   157 	struct razor_set *set;
   158 	struct razor_package *package, *end;
   159 	struct list *index;
   160 	void *alloced_index;
   161 };
   162 
   163 void
   164 razor_package_iterator_init_for_property(struct razor_package_iterator *pi,
   165 					 struct razor_set *set,
   166 					 struct razor_property *property);
   167 
   168 struct razor_property_iterator {
   169 	struct razor_set *set;
   170 	struct razor_property *property, *end;
   171 	struct list *index;
   172 };
   173 
   174 struct razor_file_iterator {
   175 	struct razor_set *set;
   176 	struct array path;
   177 	struct list *index;
   178 	int post_order;
   179 };
   180 
   181 struct razor_string_iterator {
   182 	struct razor_set *set;
   183 	struct list *index;
   184 };
   185 
   186 int
   187 razor_set_acquire_lock(struct razor_set *set, const char *path, int exclusive,
   188 		       struct razor_error **error);
   189 
   190 struct razor_entry *
   191 razor_set_find_entry(struct razor_set *set,
   192 		     struct razor_entry *dir, const char *pattern);
   193 
   194 struct razor_merger *
   195 razor_merger_create(struct razor_set *set1, struct razor_set *set2);
   196 void
   197 razor_merger_add_package(struct razor_merger *merger,
   198 			 struct razor_package *package);
   199 struct razor_set *
   200 razor_merger_commit(struct razor_merger *merger);
   201 void
   202 razor_merger_package_add_script(struct razor_merger *merger,
   203 				struct razor_package *package,
   204 				enum razor_property_flags script,
   205 				const char *program, const char *body);
   206 void
   207 razor_merger_destroy(struct razor_merger *merger);
   208 
   209 int run_lua_script(const char *root, const char *name, const char *body,
   210 		   ssize_t len, int arg1);
   211 
   212 int razor_run_script(const char *root, enum razor_property_flags script,
   213 		     const char *program, const char *body, int arg1,
   214 		     struct razor_error **error);
   215 
   216 /* URI functions */
   217 
   218 struct razor_uri {
   219 	char *scheme;
   220 	char *userinfo;
   221 	char *host;
   222 	char *port;
   223 	char *path;
   224 	char *query;
   225 	char *fragment;
   226 };
   227 
   228 void razor_uri_destroy(struct razor_uri *ru);
   229 int razor_uri_parse_uri(struct razor_uri *ru, const char *uri, int absolute,
   230 			struct razor_error **error);
   231 int razor_uri_parse_relative_ref(struct razor_uri *ru, const char *uri,
   232 				 struct razor_error **error);
   233 int razor_uri_parse(struct razor_uri *ru, const char *uri,
   234 		    struct razor_error **error);
   235 void razor_uri_normalize(struct razor_uri *ru);
   236 char *razor_uri_get_authority(const struct razor_uri *ru) RAZOR_MALLOC;
   237 char *razor_uri_recompose(const struct razor_uri *ru) RAZOR_MALLOC;
   238 void razor_uri_resolve(struct razor_uri *T, const struct razor_uri *base,
   239 		       const struct razor_uri *R);
   240 
   241 char *razor_resolve_uri_root(const char *root_uri, const char *relative_uri,
   242 			     int is_relative, struct razor_error **error)
   243       RAZOR_MALLOC;
   244 
   245 int razor_file_mkdir(const char *path, mode_t mode, struct razor_error **error);
   246 int razor_file_unlink(const char *path, struct razor_error **error);
   247 int razor_file_open(const char *path, int flags, mode_t mode,
   248 		    struct razor_error **error);
   249 int razor_file_move(const char *path, const char *dest,
   250 		    struct razor_error **error);
   251 void *razor_file_get_contents(const char *path, size_t *length, int _private,
   252 			      struct razor_error **error);
   253 int razor_file_free_contents(void *addr, size_t length);
   254 int razor_file_is_directory(const char *path, struct razor_error **error);
   255 char *razor_file_mkdtemp_near(const char *path, const char *_template,
   256 			      struct razor_error **error);
   257 void *razor_file_opendir(const char *path, struct razor_error **error);
   258 char *razor_file_readdir(void *dir, struct razor_error **error);
   259 int razor_file_closedir(void *dir, struct razor_error **error);
   260 
   261 char *razor_path_from_parsed_uri(const struct razor_uri *ru,
   262   struct razor_error **error);
   263 
   264 /* Utility functions */
   265 
   266 void
   267 razor_package_get_details_varg(struct razor_set *set,
   268 			       struct razor_package *package,
   269 			       va_list args);
   270 void razor_rpm_get_details_varg(struct razor_rpm *rpm, va_list args);
   271 
   272 typedef int (*razor_compare_with_data_func_t)(const void *p1,
   273 					      const void *p,
   274 					      void *data);
   275 uint32_t *
   276 razor_qsort_with_data(void *base, size_t nelem, size_t size,
   277 		      razor_compare_with_data_func_t compare, void *data);
   278 
   279 struct environment {
   280 	int is_set;
   281 	struct array vars, string_pool;
   282 };
   283 
   284 void environment_init(struct environment *env);
   285 void environment_add_variable(struct environment *env,
   286 			      const char *variable, const char *value);
   287 void environment_set(struct environment *env);
   288 void environment_unset(struct environment *env);
   289 void environment_release(struct environment *env);
   290 
   291 #ifdef MSWIN_API
   292 char *razor_utf16_to_utf8(const wchar_t *utf16, int len);
   293 wchar_t *razor_utf8_to_utf16(const char *utf8, int len);
   294 #endif
   295 
   296 char *razor_abspath(const char *path);
   297 
   298 /* Error functions */
   299 struct razor_error {
   300 	int domain;
   301 	int code;
   302 	char *object;
   303 	char *str;
   304 	char *obj_str;
   305 	char *summary;
   306 	char *msg;
   307 };
   308 
   309 #define razor_error_new_posix(object) \
   310 	razor_error_new_str(RAZOR_POSIX_ERROR, errno, object, strerror(errno))
   311 #define razor_set_error_posix(error, object) \
   312 	if (error) \
   313 		*(error) = razor_error_new_posix(object); \
   314 	else
   315 #define razor_set_error(error, domain, code, object, str) \
   316 	if (error) \
   317 		*(error) = razor_error_new_str(domain, code, object, str); \
   318 	else
   319 void razor_error_set_object(struct razor_error *error, const char *object);
   320 
   321 #ifdef MSWIN_API
   322 struct razor_error *razor_error_new_mswin(const wchar_t *object, DWORD error);
   323 struct razor_error *razor_error_new_str2(int domain, int code,
   324 					 const wchar_t *object,
   325 					 const char *str);
   326 
   327 #define razor_set_error_mswin(error, object, err) \
   328 	if (error) \
   329 		*(error) = razor_error_new_mswin(object, err); \
   330 	else
   331 #define razor_set_error2(error, domain, code, object, str) \
   332 	if (error) \
   333 		*(error) = razor_error_new_str2(domain, code, object, str); \
   334 	else
   335 #endif	/* MSWIN_API */
   336 
   337 /* Atomic functions */
   338 
   339 #if HAVE_WINDOWS_KTM
   340 struct razor_atomic {
   341 	HANDLE transaction;
   342 	int n_files;
   343 	struct razor_atomic_file {
   344 		wchar_t *path;
   345 		HANDLE h;
   346 	} *files;
   347 	int in_undo;
   348 	struct razor_error *error;
   349 };
   350 #elif ENABLE_ATOMIC
   351 struct atomic_action {
   352 	struct atomic_action *next;
   353 	enum atomic_action_type {
   354 		/* Complex actions */
   355 		ACTION_MAKE_DIRS,
   356 		ACTION_REMOVE,
   357 		/* Primitive actions */
   358 		ACTION_CREATE_DIR,
   359 #if HAVE_SYMLINK
   360 		ACTION_CREATE_SYMLINK,
   361 #endif
   362 		ACTION_MOVE,
   363 	} type;
   364 	struct {
   365 		char *uri;
   366 		union atomic_action_args {
   367 			struct {
   368 				char *root;
   369 			} make_dirs;
   370 			struct {
   371 				mode_t mode;
   372 			} create_dir;
   373 #if HAVE_SYMLINK
   374 			struct {
   375 				char *target;
   376 			} create_symlink;
   377 #endif
   378 			struct {
   379 				char *dest;
   380 			} move;
   381 		} u;
   382 	} args;
   383 };
   384 
   385 struct razor_atomic {
   386 	struct atomic_action *actions;
   387 	char *description;
   388 	char *toplevel;
   389 	unsigned next_file_tag;
   390 	int in_undo;
   391 	struct razor_error *error;
   392 };
   393 
   394 char *atomic_action_attic_tmpnam(struct razor_atomic *atomic);
   395 struct atomic_action *
   396 atomic_action_list_prepend(struct atomic_action *list,
   397 			   struct atomic_action *action);
   398 struct atomic_action *atomic_action_new(enum atomic_action_type type);
   399 void atomic_action_free(struct atomic_action *action);
   400 struct atomic_action *atomic_action_list_reverse(struct atomic_action *list);
   401 struct atomic_action *
   402 atomic_action_do(struct razor_atomic *atomic, struct atomic_action *action);
   403 void
   404 atomic_action_undo(struct razor_atomic *atomic, struct atomic_action *action);
   405 #else	/* !HAVE_WINDOWS_KTM && !ENABLE_ATOMIC */
   406 struct razor_atomic {
   407 	int in_undo;
   408 	struct razor_error *error;
   409 };
   410 #endif
   411 
   412 char *razor_resolve_database_file(const char *root_uri, const char *filename,
   413 				  struct razor_error **error);
   414 
   415 int razor_allow_all_root_names(void);
   416 int razor_valid_root_name(const char *name);
   417 
   418 #define RAZOR_ASCII_ISALPHA(c) \
   419 	((c) >= 'A' && (c) <= 'Z' || (c) >= 'a' && (c) <= 'z')
   420 
   421 #ifdef MSWIN_API
   422 #define SKIP_DRIVE_LETTER(path) \
   423 	((RAZOR_ASCII_ISALPHA((path)[0]) && (path)[1] == ':') ? \
   424 	   (path) + 2 : (path))
   425 #else
   426 #define SKIP_DRIVE_LETTER(path) (path)
   427 #endif /* MSWIN_API */
   428 
   429 #endif /* _RAZOR_INTERNAL_H_ */