librazor/razor-internal.h
author J. Ali Harlow <ali@juiblex.co.uk>
Thu Oct 09 17:27:41 2014 +0100 (2014-10-09)
changeset 455 df914f383f5c
parent 441 cf499fd51df7
child 458 3f841a46eab5
permissions -rw-r--r--
Support downloading from local repository even without libcurl

Using the --url option of the razor executable, it is possible
to specify a yum repository on the local machine (eg., on installation
media) and import from there, eg.,:

C> razor --url file:///d:/ import-yum

This will be handled by libcurl if available but if not, an internal
copy routine will be used.

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