librazor/razor.c
author J. Ali Harlow <ali@juiblex.co.uk>
Thu Nov 13 11:30:27 2014 +0000 (2014-11-13)
changeset 463 1ca7a49838e9
parent 458 3f841a46eab5
child 475 008c75a5e08d
permissions -rw-r--r--
Switch from 0.5.7 to 0.6 and update libtool versioning to note incompatibilty
     1 /*
     2  * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
     3  * Copyright (C) 2008  Red Hat, Inc
     4  * Copyright (C) 2009-2012  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 #define _GNU_SOURCE
    22 
    23 #include "config.h"
    24 
    25 #include <stdlib.h>
    26 #include <stddef.h>
    27 #include <stdint.h>
    28 #include <stdio.h>
    29 #include <stdarg.h>
    30 #include <string.h>
    31 #include <sys/types.h>
    32 #include <sys/stat.h>
    33 #include <unistd.h>
    34 #include <fcntl.h>
    35 #include <errno.h>
    36 #include <ctype.h>
    37 #include <fnmatch.h>
    38 #include <limits.h>
    39 #include <assert.h>
    40 #ifdef MSWIN_API
    41 #include <windows.h>
    42 #endif
    43 
    44 #include "razor-internal.h"
    45 #include "razor.h"
    46 
    47 #ifndef O_BINARY
    48 #define O_BINARY	0
    49 #endif
    50 
    51 struct razor_set_section_index {
    52 	const char *name;
    53 	uint32_t offset;
    54 	uint32_t flags;
    55 };
    56 
    57 #define MAIN(type, field) \
    58 	{ type, offsetof(struct razor_set, field), RAZOR_SECTION_MAIN }
    59 #define FILES(type, field) \
    60 	{ type, offsetof(struct razor_set, field), RAZOR_SECTION_FILES }
    61 #define DETAILS(type, field) \
    62 	{ type, offsetof(struct razor_set, field), RAZOR_SECTION_DETAILS }
    63 
    64 struct razor_set_section_index razor_sections[] = {
    65 	MAIN(RAZOR_STRING_POOL, string_pool),
    66 	MAIN(RAZOR_PACKAGES, packages),
    67 	MAIN(RAZOR_PROPERTIES, properties),
    68 	MAIN(RAZOR_PACKAGE_POOL, package_pool),
    69 	MAIN(RAZOR_PROPERTY_POOL, property_pool),
    70 	MAIN(RAZOR_PREFIX_POOL, prefix_pool),
    71 	FILES(RAZOR_FILES, files),
    72 	FILES(RAZOR_FILE_POOL, file_pool),
    73 	FILES(RAZOR_FILE_STRING_POOL, file_string_pool),
    74 	DETAILS(RAZOR_DETAILS_STRING_POOL, details_string_pool)
    75 };
    76 
    77 RAZOR_EXPORT struct razor_set *
    78 razor_set_create_without_root(void)
    79 {
    80 	struct razor_set *set;
    81 	char *empty;
    82 
    83 	set = zalloc(sizeof *set);
    84 
    85 	if (set) {
    86 		empty = array_add(&set->string_pool, 1);
    87 		*empty = '\0';
    88 
    89 		set->lock_fd = -1;
    90 
    91 		set->ref_count = 1;
    92 
    93 		set->header_version = RAZOR_HEADER_VERSION;
    94 
    95 		set->flags = RAZOR_SET_PRIVATE;
    96 	}
    97 
    98 	return set;
    99 }
   100 
   101 RAZOR_EXPORT struct razor_set *
   102 razor_set_create(void)
   103 {
   104 	struct razor_set *set;
   105 	struct razor_entry *e;
   106 
   107 	set = razor_set_create_without_root();
   108 
   109 	e = array_add(&set->files, sizeof *e);
   110 	e->name = 0;
   111 	e->flags = RAZOR_ENTRY_LAST;
   112 	e->start = 0;
   113 	list_set_empty(&e->packages);
   114 
   115 	return set;
   116 }
   117 
   118 RAZOR_EXPORT uint32_t
   119 razor_set_get_header_version(struct razor_set *set)
   120 {
   121 	return set->header_version;
   122 }
   123 
   124 RAZOR_EXPORT int
   125 razor_set_set_header_version(struct razor_set *set, uint32_t header_version)
   126 {
   127 	if (header_version<RAZOR_HEADER_VERSION_MIN ||
   128 	    header_version>RAZOR_HEADER_VERSION)
   129 		return -1;
   130 	else {
   131 		set->header_version = header_version;
   132 		return 0;
   133 	}
   134 }
   135 
   136 struct razor_mapped_file {
   137 	struct razor_set_header *header;
   138 	size_t size;
   139 	struct razor_mapped_file *next;
   140 };
   141 
   142 RAZOR_EXPORT int
   143 razor_set_bind_sections(struct razor_set *set, const char *filename,
   144 			enum razor_set_flags flags, struct razor_error **error)
   145 {
   146 	struct razor_set_section *s, *sections;
   147 	struct razor_mapped_file *file;
   148 	const char *pool, *reason;
   149 	struct array *array;
   150 	int i, j, code;
   151 
   152 	file = zalloc(sizeof *file);
   153 	if (file == NULL) {
   154 		razor_set_error(error, RAZOR_POSIX_ERROR, ENOMEM, NULL,
   155 				"Not enough memory");
   156 		return -1;
   157 	}
   158 
   159 	file->header = razor_file_get_contents(filename, &file->size,
   160 					       flags & RAZOR_SET_PRIVATE,
   161 					       error);
   162 	if (!file->header) {
   163 		free(file);
   164 		return -1;
   165 	}
   166 
   167 	if (file->size < sizeof *file->header) {
   168 		code = RAZOR_GENERAL_ERROR_DATABASE_CORRUPTED;
   169 		reason = "Premature EOF";
   170 	} else if (file->header->magic != RAZOR_MAGIC) {
   171 		code = RAZOR_GENERAL_ERROR_DATABASE_CORRUPTED;
   172 		reason = "Bad magic number";
   173 	} else if (file->header->version < RAZOR_HEADER_VERSION_MIN ||
   174 		   file->header->version > RAZOR_HEADER_VERSION) {
   175 		code = RAZOR_GENERAL_ERROR_DATABASE_INCOMPATIBLE;
   176 		reason = "Incompatible file version";
   177 	} else
   178 		reason = NULL;
   179 
   180 	if (reason) {
   181 		razor_set_error(error, RAZOR_GENERAL_ERROR, code, filename,
   182 				reason);
   183 		razor_file_free_contents(file->header, file->size);
   184 		free(file);
   185 		return -1;
   186 	}
   187 
   188 	set->flags = flags & RAZOR_SET_PRIVATE;
   189 
   190 	set->header_version = file->header->version;
   191 
   192 	if (set->mapped_files == NULL) {
   193 		for (i = 0; i < ARRAY_SIZE(razor_sections); i++) {
   194 			array = (void *) set + razor_sections[i].offset;
   195 			array_release(array);
   196 		}
   197 	}
   198 
   199 	file->next = set->mapped_files;
   200 	set->mapped_files = file;
   201 
   202 	sections = (void *) file->header + sizeof *file->header;
   203 	pool = (void *) sections +
   204 		file->header->num_sections * sizeof *sections;
   205 
   206 	for (i = 0; i < file->header->num_sections; i++) {
   207 		s = sections + i;
   208 		for (j = 0; j < ARRAY_SIZE(razor_sections); j++)
   209 			if (!strcmp(razor_sections[j].name, &pool[s->name]))
   210 				break;
   211 		if (j == ARRAY_SIZE(razor_sections))
   212 			continue;
   213 		array = (void *) set + razor_sections[j].offset;
   214 		array->data = (void *) file->header + s->offset;
   215 		array->size = s->size;
   216 		array->alloc = s->size;
   217 	}
   218 
   219 	return 0;
   220 }
   221 
   222 RAZOR_EXPORT struct razor_set *
   223 razor_set_open(const char *filename, enum razor_set_flags flags,
   224 	       struct razor_error **error)
   225 {
   226 	struct razor_set *set;
   227 
   228 	set = zalloc(sizeof *set);
   229 	if (!set) {
   230 		razor_set_error(error, RAZOR_POSIX_ERROR, ENOMEM, NULL,
   231 				"Not enough memory");
   232 		return NULL;
   233 	}
   234 
   235 	set->lock_fd = -1;
   236 	set->ref_count = 1;
   237 	if (razor_set_bind_sections(set, filename, flags, error)) {
   238 		free(set);
   239 		return NULL;
   240 	}
   241 	return set;
   242 }
   243 
   244 int
   245 razor_set_aquire_lock(struct razor_set *set, const char *path, int exclusive)
   246 {
   247 	int fd;
   248 	assert(set != NULL);
   249 
   250 	if (path) {
   251 		fd = open(path, O_CREAT | O_RDWR | O_TRUNC | O_BINARY, 0666);
   252 		if (fd < 0)
   253 			return -1;
   254 	} else {
   255 		fd = -1;
   256 	}
   257 
   258 #ifdef MSWIN_API
   259 	DWORD flags = LOCKFILE_FAIL_IMMEDIATELY;
   260 	OVERLAPPED lock = {0};
   261 
   262 	if (exclusive)
   263 		flags |= LOCKFILE_EXCLUSIVE_LOCK;
   264 	if (fd >= 0 && !LockFileEx((HANDLE)_get_osfhandle(fd), flags, 0, 1, 0,
   265 				   &lock)) {
   266 		close(fd);
   267 		return -1;
   268 	}
   269 	if (set->lock_fd >= 0)
   270 		(void)UnlockFile((HANDLE)_get_osfhandle(set->lock_fd), 0, 0, 1,
   271 				 0);
   272 #else
   273 	struct flock lock = {0};
   274 
   275 	lock.l_type = exclusive ? F_WRLCK : F_RDLCK;
   276 	lock.l_whence = SEEK_SET;
   277 	lock.l_start = 0;
   278 	lock.l_len = 0;
   279 	if (fd >= 0 && fcntl(fd, F_SETLK, &lock) < 0) {
   280 		close(fd);
   281 		return -1;
   282 	}
   283 	if (set->lock_fd >= 0) {
   284 		lock.l_type = F_UNLCK;
   285 		(void)fcntl(set->lock_fd, F_SETLK, &lock);
   286 	}
   287 #endif
   288 
   289 	if (set->lock_fd >= 0)
   290 		close(set->lock_fd);
   291 	set->lock_fd = fd;
   292 
   293 	return 0;
   294 }
   295 
   296 static void
   297 razor_set_destroy(struct razor_set *set)
   298 {
   299 	struct razor_mapped_file *file, *next;
   300 	struct array *array;
   301 	int i;
   302 
   303 	assert (set != NULL);
   304 
   305 	if (set->mapped_files == NULL) {
   306 		for (i = 0; i < ARRAY_SIZE(razor_sections); i++) {
   307 			array = (void *) set + razor_sections[i].offset;
   308 			array_release(array);
   309 		}
   310 	} else {
   311 		for (file = set->mapped_files; file != NULL; file = next) {
   312 			next = file->next;
   313 			razor_file_free_contents(file->header, file->size);
   314 			free(file);
   315 		}
   316 	}
   317 
   318 	razor_set_aquire_lock(set, NULL, 0);
   319 	free(set);
   320 }
   321 
   322 RAZOR_EXPORT void
   323 razor_set_unref(struct razor_set *set)
   324 {
   325 	if (set && !--set->ref_count)
   326 		razor_set_destroy(set);
   327 }
   328 
   329 RAZOR_EXPORT struct razor_set *
   330 razor_set_ref(struct razor_set *set)
   331 {
   332 	if (set)
   333 		set->ref_count++;
   334 	return set;
   335 }
   336 
   337 RAZOR_EXPORT void
   338 razor_set_write_to_handle(struct razor_set *set, struct razor_atomic *atomic,
   339 			  int handle, uint32_t section_mask)
   340 {
   341 	struct razor_set_header header;
   342 	struct razor_set_section sections[ARRAY_SIZE(razor_sections)];
   343 	struct hashtable table;
   344 	struct array pool, *arrays[ARRAY_SIZE(razor_sections)];
   345 	uint32_t offset;
   346 	int count, i, j;
   347 	static const char padding[4];
   348 
   349 	array_init(&pool);
   350 	hashtable_init(&table, &pool);
   351 
   352 	j = 0;
   353 	for (i = 0; i < ARRAY_SIZE(razor_sections); i++) {
   354 		if ((razor_sections[i].flags & section_mask) == 0)
   355 			continue;
   356 
   357 		arrays[j] = (void *) set + razor_sections[i].offset;
   358 		sections[j].name =
   359 			hashtable_tokenize(&table, razor_sections[i].name);
   360 		j++;
   361 	}
   362 
   363 	count = j;
   364 	header.magic = RAZOR_MAGIC;
   365 	header.version = set->header_version;
   366 	header.num_sections = count;
   367 	offset = sizeof header + count * sizeof *sections + ALIGN(pool.size, 4);
   368 
   369 	for (i = 0; i < count; i++) {
   370 		sections[i].offset = offset;
   371 		sections[i].size = arrays[i]->size;
   372 		offset += ALIGN(arrays[i]->size, 4);
   373 	}
   374 
   375 	razor_atomic_write(atomic, handle, &header, sizeof header);
   376 	razor_atomic_write(atomic, handle, sections, count * sizeof *sections);
   377 	razor_atomic_write(atomic, handle, pool.data, pool.size);
   378 	razor_atomic_write(atomic, handle, padding, PADDING(pool.size, 4));
   379 
   380 	for (i = 0; i < count; i++) {
   381 		razor_atomic_write(atomic, handle, arrays[i]->data,
   382 				   arrays[i]->size);
   383 		razor_atomic_write(atomic, handle, padding,
   384 				   PADDING(arrays[i]->size, 4));
   385 	}
   386 
   387 	array_release(&pool);
   388 	hashtable_release(&table);
   389 }
   390 
   391 RAZOR_EXPORT int
   392 razor_set_write(struct razor_set *set, struct razor_atomic *atomic,
   393 		const char *filename, uint32_t sections)
   394 {
   395 	int h;
   396 
   397 	h = razor_atomic_create_file(atomic, filename,
   398 				     S_IRWXU | S_IRWXG | S_IRWXO);
   399 	if (h < 0)
   400 		return -1;
   401 
   402 	razor_set_write_to_handle(set, atomic, h, sections);
   403 
   404 	return razor_atomic_close(atomic, h);
   405 }
   406 
   407 RAZOR_EXPORT void
   408 razor_build_evr(char *evr_buf, int size, const char *epoch,
   409 		const char *version, const char *release)
   410 {
   411 	int len;
   412 
   413 	if (!version || !*version) {
   414 		*evr_buf = '\0';
   415 		return;
   416 	}
   417 
   418 	if (epoch && *epoch && strcmp(epoch, "0") != 0) {
   419 		len = snprintf(evr_buf, size, "%s:", epoch);
   420 		evr_buf += len;
   421 		size -= len;
   422 	}
   423 	len = snprintf(evr_buf, size, "%s", version);
   424 	evr_buf += len;
   425 	size -= len;
   426 	if (release && *release)
   427 		snprintf(evr_buf, size, "-%s", release);
   428 }
   429 
   430 RAZOR_EXPORT int
   431 razor_versioncmp(const char *s1, const char *s2)
   432 {
   433 	const char *p1, *p2;
   434 	long n1, n2;
   435 	int res;
   436 
   437 	assert (s1 != NULL);
   438 	assert (s2 != NULL);
   439 
   440 	n1 = strtol(s1, (char **) &p1, 10);
   441 	n2 = strtol(s2, (char **) &p2, 10);
   442 
   443 	/* Epoch; if one but not the other has an epoch set, default
   444 	 * the epoch-less version to 0. */
   445 	res = (*p1 == ':') - (*p2 == ':');
   446 	if (res < 0) {
   447 		n1 = 0;
   448 		p1 = s1;
   449 		p2++;
   450 	} else if (res > 0) {
   451 		p1++;
   452 		n2 = 0;
   453 		p2 = s2;
   454 	}
   455 
   456 	if (n1 != n2)
   457 		return n1 - n2;
   458 	while (*p1 && *p2) {
   459 		if (*p1 != *p2)
   460 			return *p1 - *p2;
   461 		p1++;
   462 		p2++;
   463 		if (isdigit(*p1) && isdigit(*p2))
   464 			return razor_versioncmp(p1, p2);
   465 	}
   466 
   467 	return *p1 - *p2;
   468 }
   469 
   470 static const char *
   471 razor_package_get_details_string(struct razor_set *set,
   472 				 struct razor_package *package,
   473 				 enum razor_detail_type type)
   474 {
   475 	const char *pool;
   476 
   477 	switch (type) {
   478 	case RAZOR_DETAIL_NAME:
   479 		pool = set->string_pool.data;
   480 		return &pool[package->name];
   481 
   482 	case RAZOR_DETAIL_VERSION:
   483 		pool = set->string_pool.data;
   484 		return &pool[package->version];
   485 
   486 	case RAZOR_DETAIL_ARCH:
   487 		pool = set->string_pool.data;
   488 		return &pool[package->arch];
   489 
   490 	case RAZOR_DETAIL_SUMMARY:
   491 		if (!set->details_string_pool.size)
   492 			return "";
   493 		pool = set->details_string_pool.data;
   494 		return &pool[package->summary];
   495 
   496 	case RAZOR_DETAIL_DESCRIPTION:
   497 		if (!set->details_string_pool.size)
   498 			return "";
   499 		pool = set->details_string_pool.data;
   500 		return &pool[package->description];
   501 
   502 	case RAZOR_DETAIL_URL:
   503 		if (!set->details_string_pool.size)
   504 			return "";
   505 		pool = set->details_string_pool.data;
   506 		return &pool[package->url];
   507 
   508 	case RAZOR_DETAIL_LICENSE:
   509 		if (!set->details_string_pool.size)
   510 			return "";
   511 		pool = set->details_string_pool.data;
   512 		return &pool[package->license];
   513 
   514 	case RAZOR_DETAIL_PREUNPROG:
   515 		pool = set->string_pool.data;
   516 		return &pool[package->preun.program];
   517 
   518 	case RAZOR_DETAIL_PREUN:
   519 		pool = set->string_pool.data;
   520 		return &pool[package->preun.body];
   521 
   522 	case RAZOR_DETAIL_POSTUNPROG:
   523 		pool = set->string_pool.data;
   524 		return &pool[package->postun.program];
   525 
   526 	case RAZOR_DETAIL_POSTUN:
   527 		pool = set->string_pool.data;
   528 		return &pool[package->postun.body];
   529 
   530 	default:
   531 		fprintf(stderr, "type %u not found\n", type);
   532 		return NULL;
   533 	}
   534 }
   535 
   536 static const char *const *
   537 razor_package_get_details_array(struct razor_set *set,
   538 				struct razor_package *package,
   539 				enum razor_detail_type type)
   540 {
   541 	switch (type) {
   542 	case RAZOR_DETAIL_PREFIXES:
   543 		/* We don't track prefixes in packages. Install
   544 		 * prefixes are tracked, and made available via
   545 		 * razor_install_prefix_iterator_create().
   546 		 */
   547 		return NULL;
   548 
   549 	default:
   550 		fprintf(stderr, "type %u not found\n", type);
   551 		return NULL;
   552 	}
   553 }
   554 
   555 /**
   556  * razor_package_get_details_varg:
   557  * @set: a %razor_set
   558  * @package: a %razor_package
   559  * @args: a va_list of arguments to set
   560  **/
   561 void
   562 razor_package_get_details_varg(struct razor_set *set,
   563 			       struct razor_package *package,
   564 			       va_list args)
   565 {
   566 	int i;
   567 	enum razor_detail_type type;
   568 	const char **string;
   569 	const char *const **array;
   570 
   571 	for (i = 0;; i += 2) {
   572 		type = va_arg(args, enum razor_detail_type);
   573 		if (type == RAZOR_DETAIL_LAST)
   574 			break;
   575 		if (type == RAZOR_DETAIL_PREFIXES) {
   576 			array = va_arg(args, const char *const **);
   577 			*array = razor_package_get_details_array(set, package,
   578 								 type);
   579 		} else {
   580 			string = va_arg(args, const char **);
   581 			*string = razor_package_get_details_string(set, package,
   582 								   type);
   583 		}
   584 	}
   585 
   586 }
   587 
   588 /**
   589  * razor_package_get_details:
   590  * @set: a %razor_set
   591  * @package: a %razor_package
   592  *
   593  * Gets details about a package using a varg interface
   594  * The vararg must be terminated with %RAZOR_DETAIL_LAST.
   595  *
   596  * Example: razor_package_get_details (set, package,
   597  *				       RAZOR_DETAIL_URL, &url,
   598  *				       RAZOR_DETAIL_LAST);
   599  **/
   600 RAZOR_EXPORT void
   601 razor_package_get_details(struct razor_set *set, struct razor_package *package, ...)
   602 {
   603 	va_list args;
   604 
   605 	assert (set != NULL);
   606 	assert (package != NULL);
   607 
   608 	va_start(args, NULL);
   609 	razor_package_get_details_varg (set, package, args);
   610 	va_end (args);
   611 }
   612 
   613 /**
   614  * razor_package_remove:
   615  * @prev: The %razor_set before the current transaction
   616  * @next: The %razor_set after the current transaction is applied
   617  * @package: a %razor_package
   618  * @root: the root into which the package is currently installed
   619  * @install_count: the value to pass to uninstall scripts
   620  * @stage: Limit the removal to just the scripts or the files
   621  *
   622  * Removes an installed package.
   623  **/
   624 RAZOR_EXPORT int
   625 razor_package_remove(struct razor_set *prev, struct razor_set *next,
   626 		     struct razor_atomic *atomic, struct razor_package *package,
   627 		     const char *root, int install_count,
   628 		     enum razor_stage_type stage)
   629 {
   630 	struct razor_file_iterator *fi;
   631 	struct razor_package_iterator *pi;
   632 	struct razor_package *p;
   633 	char *buffer, buf[32];
   634 	const char *name, *program, *script;
   635 	int i, count, retval = 0;
   636 	struct environment env;
   637 	struct list *link;
   638 	const char *prefix;
   639 
   640 	if (stage & RAZOR_STAGE_SCRIPTS) {
   641 		environment_init(&env);
   642 		link = list_first(&package->install_prefixes,
   643 				  &prev->prefix_pool);
   644 		for (i = 0; link; i++) {
   645 			prefix = (const char *)prev->string_pool.data +
   646 				 link->data;
   647 			sprintf(buf, "RPM_INSTALL_PREFIX%d", i);
   648 			environment_add_variable(&env, buf, prefix);
   649 			link = list_next(link);
   650 		}
   651 		environment_set(&env);
   652 	}
   653 
   654 	if (stage & RAZOR_STAGE_SCRIPTS_PRE) {
   655 		razor_package_get_details(prev, package,
   656 					  RAZOR_DETAIL_PREUNPROG, &program,
   657 					  RAZOR_DETAIL_PREUN, &script,
   658 					  RAZOR_DETAIL_LAST);
   659 
   660 		retval = razor_run_script(root, RAZOR_PROPERTY_PREUN, program,
   661 					  script, install_count);
   662 	}
   663 
   664 	if (!retval && (stage & RAZOR_STAGE_FILES)) {
   665 		fi = razor_file_iterator_create(prev, package, 1);
   666 
   667 		while (razor_file_iterator_next(fi, &name)) {
   668 			pi = razor_package_iterator_create_for_file(next, name);
   669 			count = 0;
   670 			while (razor_package_iterator_next(pi, &p,
   671 							   RAZOR_DETAIL_LAST))
   672 				count++;
   673 			razor_package_iterator_destroy(pi);
   674 			if (count <= 0) {
   675 				buffer = razor_concat(root, name, NULL);
   676 				razor_atomic_remove(atomic, buffer);
   677 				free(buffer);
   678 			}
   679 		}
   680 
   681 		razor_file_iterator_destroy(fi);
   682 
   683 		retval = razor_atomic_in_error_state(atomic);
   684 	}
   685 
   686 	if (!retval && (stage & RAZOR_STAGE_SCRIPTS_POST)) {
   687 		razor_package_get_details(prev, package,
   688 					  RAZOR_DETAIL_POSTUNPROG, &program,
   689 					  RAZOR_DETAIL_POSTUN, &script,
   690 					  RAZOR_DETAIL_LAST);
   691 
   692 		retval |= razor_run_script(root, RAZOR_PROPERTY_POSTUN, program,
   693 					   script, install_count);
   694 	}
   695 
   696 	if (stage & RAZOR_STAGE_SCRIPTS) {
   697 		environment_unset(&env);
   698 		environment_release(&env);
   699 	}
   700 
   701 	return retval;
   702 }
   703 
   704 RAZOR_EXPORT const char *
   705 razor_property_relation_to_string(struct razor_property *p)
   706 {
   707 	assert (p != NULL);
   708 
   709 	switch (p->flags & RAZOR_PROPERTY_RELATION_MASK) {
   710 	case RAZOR_PROPERTY_LESS:
   711 		return "<";
   712 
   713 	case RAZOR_PROPERTY_LESS | RAZOR_PROPERTY_EQUAL:
   714 		return "<=";
   715 
   716 	case RAZOR_PROPERTY_EQUAL:
   717 		return "=";
   718 
   719 	case RAZOR_PROPERTY_GREATER | RAZOR_PROPERTY_EQUAL:
   720 		return ">=";
   721 
   722 	case RAZOR_PROPERTY_GREATER:
   723 		return ">";
   724 
   725 	default:
   726 		return "?";
   727 	}
   728 }
   729 
   730 RAZOR_EXPORT const char *
   731 razor_property_type_to_string(struct razor_property *p)
   732 {
   733 	assert (p != NULL);
   734 
   735 	switch (p->flags & RAZOR_PROPERTY_TYPE_MASK) {
   736 	case RAZOR_PROPERTY_REQUIRES:
   737 		return "requires";
   738 	case RAZOR_PROPERTY_PROVIDES:
   739 		return "provides";
   740 	case RAZOR_PROPERTY_CONFLICTS:
   741 		return "conflicts";
   742 	case RAZOR_PROPERTY_OBSOLETES:
   743 		return "obsoletes";
   744 	default:
   745 		return NULL;
   746 	}
   747 }
   748 
   749 RAZOR_EXPORT struct razor_entry *
   750 razor_set_find_entry(struct razor_set *set,
   751 		     struct razor_entry *dir, const char *pattern)
   752 {
   753 	struct razor_entry *e, *subdir;
   754 	const char *n, *pool = set->file_string_pool.data;
   755 	int len;
   756 
   757 	assert (set != NULL);
   758 	assert (pattern != NULL);
   759 
   760 	if (dir == NULL)
   761 		return NULL;
   762 
   763 	e = dir;
   764 	do {
   765 		n = pool + e->name;
   766 		if (strcmp(pattern, n) == 0)
   767 			return e;
   768 		len = strlen(n);
   769 		if (e->start != 0 && strncmp(pattern, n, len) == 0 &&
   770 		    pattern[len] == '/') {
   771 			subdir = (struct razor_entry *) set->files.data +
   772 				 e->start;
   773 			return razor_set_find_entry(set, subdir,
   774 						    pattern + len + 1);
   775 		}
   776 	} while (!((e++)->flags & RAZOR_ENTRY_LAST));
   777 
   778 	return NULL;
   779 }
   780 
   781 static void
   782 list_dir(struct razor_set *set, struct razor_entry *dir,
   783 	 char *prefix, const char *pattern)
   784 {
   785 	struct razor_entry *e, *subdir;
   786 	const char *n, *pool = set->file_string_pool.data;
   787 
   788 	e = dir;
   789 	do {
   790 		n = pool + e->name;
   791 		if (pattern && pattern[0] && fnmatch(pattern, n, 0) != 0)
   792 			continue;
   793 		printf("%s/%s\n", prefix, n);
   794 		if (e->start) {
   795 			char *sub = prefix + strlen (prefix);
   796 			*sub = '/';
   797 			strcpy (sub + 1, n);
   798 			subdir = (struct razor_entry *) set->files.data +
   799 				 e->start;
   800 			list_dir(set, subdir, prefix, pattern);
   801 			*sub = '\0';
   802 		}
   803 	} while (!((e++)->flags & RAZOR_ENTRY_LAST));
   804 }
   805 
   806 RAZOR_EXPORT void
   807 razor_set_list_files(struct razor_set *set, const char *pattern)
   808 {
   809 	struct razor_entry *root, *e;
   810 	char buffer[512], *p, *base;
   811 
   812 	assert (set != NULL);
   813 
   814 	root = (struct razor_entry *) set->files.data;
   815 
   816 	if (pattern == NULL) {
   817 		p = set->file_string_pool.data;
   818 		e = root;
   819 		do {
   820 			if (e->start) {
   821 				strcpy(buffer, p + e->name);
   822 				list_dir(set, root + e->start, buffer, NULL);
   823 			}
   824 		} while (!((e++)->flags & RAZOR_ENTRY_LAST));
   825 		return;
   826 	}
   827 
   828 	strcpy(buffer, pattern);
   829 	e = razor_set_find_entry(set, root, buffer);
   830 	if (e && e->start) {
   831 		base = NULL;
   832 	} else {
   833 		p = strrchr(buffer, '/');
   834 		if (p) {
   835 			*p = '\0';
   836 			base = p + 1;
   837 		} else {
   838 			base = NULL;
   839 		}
   840 	}
   841 	e = razor_set_find_entry(set, root, buffer);
   842 	if (e && e->start)
   843 		list_dir(set, root + e->start, buffer, base);
   844 }
   845 
   846 RAZOR_EXPORT void
   847 razor_set_list_package_files(struct razor_set *set,
   848 			     struct razor_package *package)
   849 {
   850 	struct razor_file_iterator *fi;
   851 	const char *name;
   852 
   853 	assert (set != NULL);
   854 	assert (package != NULL);
   855 
   856 	fi = razor_file_iterator_create(set, package, 0);
   857 
   858 	while (razor_file_iterator_next(fi, &name))
   859 		printf("%s\n", name);
   860 
   861 	razor_file_iterator_destroy(fi);
   862 }
   863 
   864 /*
   865  * Package data can potentially come from two places. The so-called
   866  * metadata (eg., from comps.xml) and from an RPM file. We consider
   867  * a package which has additional data from an RPM file as "fixed".
   868  * If a package needs fixing, then razor_transaction_fixup_package()
   869  * will do so. When considering what packages to add and to remove
   870  * we need to take this into account since we always want to add
   871  * unfixed packages (otherwise we have a potential conflict between
   872  * the existing package data and that present in the RPM).
   873  */
   874 static int
   875 razor_package_is_fixed(struct razor_set *set, struct razor_package *p)
   876 {
   877 	const char *preunprog, *preun, *postunprog, *postun;
   878 
   879 	if (!p)
   880 		return 0;
   881 	razor_package_get_details(set, p,
   882 				  RAZOR_DETAIL_PREUNPROG, &preunprog,
   883 				  RAZOR_DETAIL_PREUN, &preun,
   884 				  RAZOR_DETAIL_POSTUNPROG, &postunprog,
   885 				  RAZOR_DETAIL_POSTUN, &postun,
   886 				  RAZOR_DETAIL_LAST);
   887 	return *preunprog || *preun || *postunprog || *postun;
   888 }
   889 
   890 RAZOR_EXPORT void
   891 razor_set_diff(struct razor_set *set, struct razor_set *upstream,
   892 	       razor_diff_callback_t callback, void *data)
   893 {
   894  	struct razor_package_iterator *pi1, *pi2;
   895  	struct razor_package *p1, *p2;
   896 	const char *name1, *name2, *version1, *version2, *arch1, *arch2;
   897 	int res, is_fixed1, is_fixed2;
   898 
   899 	assert (set != NULL);
   900 	assert (upstream != NULL);
   901 
   902 	pi1 = razor_package_iterator_create(set);
   903 	pi2 = razor_package_iterator_create(upstream);
   904 
   905 	razor_package_iterator_next(pi1, &p1,
   906 				    RAZOR_DETAIL_NAME, &name1,
   907 				    RAZOR_DETAIL_VERSION, &version1,
   908 				    RAZOR_DETAIL_ARCH, &arch1,
   909 				    RAZOR_DETAIL_LAST);
   910 	is_fixed1 = razor_package_is_fixed(set, p1);
   911 	razor_package_iterator_next(pi2, &p2,
   912 				    RAZOR_DETAIL_NAME, &name2,
   913 				    RAZOR_DETAIL_VERSION, &version2,
   914 				    RAZOR_DETAIL_ARCH, &arch2,
   915 				    RAZOR_DETAIL_LAST);
   916 	is_fixed2 = razor_package_is_fixed(upstream, p2);
   917 
   918 	while (p1 || p2) {
   919 		if (p1 && p2) {
   920 			res = strcmp(name1, name2);
   921 			if (res == 0)
   922 				res = razor_versioncmp(version1, version2);
   923 			if (res == 0)
   924 				res = is_fixed1 - is_fixed2;
   925 		} else {
   926 			res = 0;
   927 		}
   928 
   929 		if (p2 == NULL || res < 0)
   930 			callback(RAZOR_DIFF_ACTION_REMOVE,
   931 				 p1, name1, version1, arch1, data);
   932 		else if (p1 == NULL || res > 0)
   933 			callback(RAZOR_DIFF_ACTION_ADD,
   934 				 p2, name2, version2, arch2, data);
   935 
   936 		if (p1 != NULL && res <= 0) {
   937 			razor_package_iterator_next(pi1, &p1,
   938 						    RAZOR_DETAIL_NAME, &name1,
   939 						    RAZOR_DETAIL_VERSION, &version1,
   940 						    RAZOR_DETAIL_ARCH, &arch1,
   941 						    RAZOR_DETAIL_LAST);
   942 			is_fixed1 = razor_package_is_fixed(set, p1);
   943 		}
   944 		if (p2 != NULL && res >= 0) {
   945 			razor_package_iterator_next(pi2, &p2,
   946 						    RAZOR_DETAIL_NAME, &name2,
   947 						    RAZOR_DETAIL_VERSION, &version2,
   948 						    RAZOR_DETAIL_ARCH, &arch2,
   949 						    RAZOR_DETAIL_LAST);
   950 			is_fixed2 = razor_package_is_fixed(upstream, p2);
   951 		}
   952 	}
   953 
   954 	razor_package_iterator_destroy(pi1);
   955 	razor_package_iterator_destroy(pi2);
   956 }
   957 
   958 struct install_action {
   959 	enum razor_install_action action;
   960 	struct razor_package *package;
   961 };
   962 
   963 struct razor_install_iterator {
   964 	struct razor_set *set;
   965 	struct razor_set *next;
   966 	struct array actions;
   967 	struct deque *order, *left;
   968 };
   969 
   970 static void
   971 add_action(enum razor_diff_action action,
   972 	   struct razor_package *package,
   973 	   const char *name,
   974 	   const char *version,
   975 	   const char *arch,
   976 	   void *data)
   977 {
   978 	struct razor_install_iterator *ii = data;
   979 	struct install_action *a;
   980 
   981 	a = array_add(&ii->actions, sizeof *a);
   982 	a->package = package;
   983 
   984 	switch (action) {
   985 	case RAZOR_DIFF_ACTION_ADD:
   986 		a->action = RAZOR_INSTALL_ACTION_ADD;
   987 		break;
   988 	case RAZOR_DIFF_ACTION_REMOVE:
   989 		a->action = RAZOR_INSTALL_ACTION_REMOVE;
   990 		break;
   991 	}
   992 }
   993 
   994 /*
   995  * Does <package> have a requirement for <script> which is
   996  * satisfied by <provider> ?
   997  * Note: We already know that <provider> is to be added as part of this install
   998  * so there is no need to check the relation.
   999  */
  1000 static int
  1001 package_script_requires(struct razor_set *set, struct razor_package *package,
  1002 			enum razor_property_flags script,
  1003 			struct razor_package *provider)
  1004 {
  1005 	struct list *link;
  1006 	struct razor_property *prop;
  1007 
  1008 	link = list_first(&package->properties, &set->property_pool);
  1009 	for(; link; link = list_next(link)) {
  1010 		prop = set->properties.data;
  1011 		prop += link->data;
  1012 		if ((prop->flags & RAZOR_PROPERTY_SCRIPT_MASK) & script &&
  1013 		    (prop->flags & RAZOR_PROPERTY_TYPE_MASK) == RAZOR_PROPERTY_REQUIRES &&
  1014 		    provider->name == prop->name)
  1015 			return 1;
  1016 	}
  1017 	return 0;
  1018 }
  1019 
  1020 RAZOR_EXPORT struct razor_install_iterator *
  1021 razor_set_create_install_iterator(struct razor_set *set,
  1022 				  struct razor_set *next)
  1023 {
  1024 	struct razor_install_iterator *ii;
  1025 	struct razor_property *prop;
  1026 	/* A graph of the actions to be perfomed where
  1027 	 * A->B means action A should follow action B.
  1028 	 */
  1029 	struct graph follows;
  1030 	struct install_action *actions, *ai, *aj, *an;
  1031 	int i, j, count, vertex_added;
  1032 	struct list *link;
  1033 	struct razor_set *rs;
  1034 	struct deque *order;
  1035 	int barrier_needed;
  1036 
  1037 	assert (set != NULL);
  1038 	assert (next != NULL);
  1039 
  1040 	ii = zalloc(sizeof *ii);
  1041 	ii->set = set;
  1042 	ii->next = next;
  1043 	
  1044 	razor_set_diff(set, next, add_action, ii);
  1045 
  1046 	actions = ii->actions.data;
  1047 	count = ii->actions.size / sizeof (struct install_action);
  1048 
  1049 	graph_init(&follows);
  1050 
  1051 	for(i = 0; i < count; i++) {
  1052 		ai = actions + i;
  1053 		rs = ai->action == RAZOR_INSTALL_ACTION_ADD ? next : set;
  1054 		vertex_added = 0;
  1055 		link = list_first(&ai->package->properties, &rs->property_pool);
  1056 		for(; link; link = list_next(link)) {
  1057 			prop = rs->properties.data;
  1058 			prop += link->data;
  1059 			switch(prop->flags & RAZOR_PROPERTY_TYPE_MASK) {
  1060 			case RAZOR_PROPERTY_REQUIRES:
  1061 			case RAZOR_PROPERTY_CONFLICTS:
  1062 				for(j = 0; j < count; j++) {
  1063 					if (j == i)
  1064 						continue;
  1065 					aj = actions + j;
  1066 					if (aj->package->name == prop->name) {
  1067 						if (ai->action ==
  1068 						    RAZOR_INSTALL_ACTION_ADD)
  1069 							graph_add_edge(&follows,
  1070 								       i, j);
  1071 						else
  1072 							graph_add_edge(&follows,
  1073 								       j, i);
  1074 						vertex_added++;
  1075 					}
  1076 				}
  1077 				break;
  1078 			}
  1079 		}
  1080 		if (ai->action == RAZOR_INSTALL_ACTION_ADD) {
  1081 			for(j = 0; j < count; j++) {
  1082 				if (j == i)
  1083 					continue;
  1084 				aj = actions + j;
  1085 				if (aj->package == ai->package &&
  1086 				    aj->action == RAZOR_INSTALL_ACTION_REMOVE) {
  1087 					graph_add_edge(&follows, i, j);
  1088 					vertex_added++;
  1089 				}
  1090 			}
  1091 		}
  1092 		if (!vertex_added)
  1093 			graph_add_edge(&follows, i, i);
  1094 	}
  1095 
  1096 	/*
  1097 	 * Because files are installed and removed using razor_atomic,
  1098 	 * but scripts are run with no regard for these, we need some
  1099 	 * means for synchronisation between the two. We support this
  1100 	 * via transaction barriers which are points where
  1101 	 * razor_atomic_commit() should be called so that scripts can
  1102 	 * rely on the files they need being present.
  1103 	 *
  1104 	 * Rules:
  1105 	 *   1)	Transaction barriers never occur within a dependency
  1106 	 *	loop. Since we can't guarantee any ordering of actions
  1107 	 *	within such a loop, they make no sense.
  1108 	 *   2) Otherwise the following table applies:
  1109 	 *	Action I    Action J	Barrier between I and J iff
  1110 	 *	ADD P	    ADD Q	%pre(Q) requires P
  1111 	 *	ADD P	    REMOVE Q	%preun(Q) requires P
  1112 	 *	REMOVE P    ADD Q	never
  1113 	 *	REMOVE P    REMOVE Q	%postun(P) requires Q
  1114 	 *
  1115 	 * FIXME:
  1116 	 *	This should take account of conflicts somehow.
  1117 	 */
  1118 	order = graph_sort(&follows);
  1119 	ii->order = deque_new(0);
  1120 	if (!deque_empty(order)) {
  1121 		j = deque_pop(order);
  1122 		deque_unshift(ii->order, j);
  1123 	}
  1124 	while (!deque_empty(order)) {
  1125 		i = j;
  1126 		j = deque_pop(order);
  1127 		ai = actions + i;
  1128 		aj = actions + j;
  1129 		if (graph_is_connected(&follows, i, j) &&
  1130 		    graph_is_connected(&follows, j, i))
  1131 			barrier_needed = 0;
  1132 		else if (ai->action == RAZOR_INSTALL_ACTION_ADD &&
  1133 			 aj->action == RAZOR_INSTALL_ACTION_ADD &&
  1134 			 package_script_requires(next, aj->package,
  1135 						 RAZOR_PROPERTY_PRE,
  1136 						 ai->package))
  1137 			barrier_needed = 1;
  1138 		else if (ai->action == RAZOR_INSTALL_ACTION_ADD &&
  1139 			 aj->action == RAZOR_INSTALL_ACTION_REMOVE &&
  1140 			 package_script_requires(set, aj->package,
  1141 						 RAZOR_PROPERTY_PREUN,
  1142 						 ai->package))
  1143 			barrier_needed = 1;
  1144 		else if (ai->action == RAZOR_INSTALL_ACTION_REMOVE &&
  1145 			 aj->action == RAZOR_INSTALL_ACTION_REMOVE &&
  1146 			 package_script_requires(set, ai->package,
  1147 						 RAZOR_PROPERTY_POSTUN,
  1148 						 aj->package))
  1149 			barrier_needed = 1;
  1150 		else
  1151 			barrier_needed = 0;
  1152 		if (barrier_needed) {
  1153 			an = array_add(&ii->actions, sizeof *an);
  1154 			actions = ii->actions.data;
  1155 			an->package = NULL;
  1156 			an->action = RAZOR_INSTALL_ACTION_COMMIT;
  1157 			deque_unshift(ii->order, an - actions);
  1158 		}
  1159 		deque_unshift(ii->order, j);
  1160 	}
  1161 	deque_free(order);
  1162 
  1163 	ii->left = deque_dup(ii->order);
  1164 	graph_release(&follows);
  1165 
  1166 	return ii;
  1167 }
  1168 
  1169 RAZOR_EXPORT int
  1170 razor_install_iterator_next(struct razor_install_iterator *ii,
  1171 			    struct razor_package **package,
  1172 			    enum razor_install_action *action,
  1173 			    int *count)
  1174 {
  1175 	struct install_action *a;
  1176 	struct razor_package_iterator *pi;
  1177 	struct razor_package *pkg;
  1178 	const char *removing, *name;
  1179 
  1180 	if (deque_empty(ii->left))
  1181 		return 0;
  1182 
  1183 	a = (struct install_action *)ii->actions.data + deque_pop(ii->left);
  1184 	*package = a->package;
  1185 	*action = a->action;
  1186 	*count = 0;
  1187 
  1188 	if (a->action == RAZOR_INSTALL_ACTION_REMOVE) {
  1189 		razor_package_get_details(ii->set, a->package,
  1190 					  RAZOR_DETAIL_NAME, &removing,
  1191 					  RAZOR_DETAIL_LAST);
  1192 
  1193 		pi = razor_package_iterator_create(ii->next);
  1194 		while (razor_package_iterator_next(pi, &pkg,
  1195 						   RAZOR_DETAIL_NAME, &name,
  1196 						   RAZOR_DETAIL_LAST)) {
  1197 			if (!strcmp(name, removing))
  1198 				(*count)++;
  1199 		}
  1200 		razor_package_iterator_destroy(pi);
  1201 	} else if (a->action == RAZOR_INSTALL_ACTION_ADD)
  1202 		*count = 1;
  1203 
  1204 	return 1;
  1205 }
  1206 
  1207 static int
  1208 action_is_included(struct razor_install_iterator *ii, struct deque *done,
  1209 		   struct razor_package *package,
  1210 		   enum razor_install_action action)
  1211 {
  1212 	struct deque *t;
  1213 	struct install_action *a;
  1214 	int retval;
  1215 
  1216 	t = deque_dup(done);
  1217 
  1218 	while(!deque_empty(t)) {
  1219 		a = (struct install_action *)ii->actions.data + deque_pop(t);
  1220 		if (a->package == package && a->action == action)
  1221 			break;
  1222 	}
  1223 	retval = !deque_empty(t);
  1224 
  1225 	deque_free(t);
  1226 
  1227 	return retval;
  1228 }
  1229 
  1230 RAZOR_EXPORT struct razor_set *
  1231 razor_install_iterator_commit_set(struct razor_install_iterator *ii)
  1232 {
  1233 	struct razor_merger *merger;
  1234 	struct razor_set *set;
  1235 	struct razor_package *n, *nend, *s, *send;
  1236 	struct deque *done;
  1237 	size_t pos;
  1238 	char *npool, *spool;
  1239 	int cmp;
  1240 
  1241 	done = deque_dup(ii->order);
  1242 	pos = razor_install_iterator_tell(ii);
  1243 	while(deque_length(done) > pos)
  1244 		deque_shift(done);
  1245 
  1246 	s = ii->set->packages.data;
  1247 	send = ii->set->packages.data + ii->set->packages.size;
  1248 	spool = ii->set->string_pool.data;
  1249 
  1250 	n = ii->next->packages.data;
  1251 	nend = ii->next->packages.data + ii->next->packages.size;
  1252 	npool = ii->next->string_pool.data;
  1253 
  1254 	merger = razor_merger_create(ii->set, ii->next);
  1255 	while (s < send || n < nend) {
  1256 		if (s < send && n < nend)
  1257 			cmp = strcmp(&spool[s->name], &npool[n->name]);
  1258 		else if (s < send)
  1259 			cmp = -1;
  1260 		else
  1261 			cmp = 1;
  1262 
  1263 		if (cmp < 0) {
  1264 			if (!action_is_included(ii, done, s,
  1265 						RAZOR_INSTALL_ACTION_REMOVE))
  1266 				razor_merger_add_package(merger, s);
  1267 			s++;
  1268 		} else if (cmp == 0) {
  1269 			if (!action_is_included(ii, done, s,
  1270 						RAZOR_INSTALL_ACTION_REMOVE))
  1271 				razor_merger_add_package(merger, s);
  1272 			if (action_is_included(ii, done, n,
  1273 					       RAZOR_INSTALL_ACTION_ADD))
  1274 				razor_merger_add_package(merger, n);
  1275 
  1276 			s++;
  1277 			n++;
  1278 		} else {
  1279 			if (action_is_included(ii, done, n,
  1280 					       RAZOR_INSTALL_ACTION_ADD))
  1281 				razor_merger_add_package(merger, n);
  1282 			n++;
  1283 		}
  1284 	}
  1285 
  1286 	set = razor_merger_commit(merger);
  1287 	razor_merger_destroy(merger);
  1288 	deque_free(done);
  1289 
  1290 	return set;
  1291 }
  1292 
  1293 RAZOR_EXPORT void
  1294 razor_install_iterator_rewind(struct razor_install_iterator *ii)
  1295 {
  1296 	deque_free(ii->left);
  1297 	ii->left=deque_dup(ii->order);
  1298 }
  1299 
  1300 RAZOR_EXPORT size_t
  1301 razor_install_iterator_tell(struct razor_install_iterator *ii)
  1302 {
  1303 	return deque_length(ii->order) - deque_length(ii->left);
  1304 }
  1305 
  1306 RAZOR_EXPORT size_t
  1307 razor_install_iterator_seek(struct razor_install_iterator *ii, size_t pos)
  1308 {
  1309 	size_t current_pos;
  1310 
  1311 	if (pos > deque_length(ii->order))
  1312 		pos = deque_length(ii->order);
  1313 
  1314 	current_pos = razor_install_iterator_tell(ii);
  1315 
  1316 	if (pos < current_pos) {
  1317 		razor_install_iterator_rewind(ii);
  1318 		current_pos = 0;
  1319 	}
  1320 
  1321 	while(current_pos < pos) {
  1322 		(void) deque_pop(ii->left);
  1323 		current_pos++;
  1324 	}
  1325 
  1326 	return current_pos;
  1327 }
  1328 
  1329 RAZOR_EXPORT void
  1330 razor_install_iterator_destroy(struct razor_install_iterator *ii)
  1331 {
  1332 	array_release(&ii->actions);
  1333 	deque_free(ii->order);
  1334 	deque_free(ii->left);
  1335 	free(ii);
  1336 }