librazor/util.c
author J. Ali Harlow <ali@juiblex.co.uk>
Mon Jul 06 18:19:13 2009 +0100 (2009-07-06)
changeset 375 c903635ae422
parent 359 c9c90315ea24
child 377 5549419824b4
permissions -rw-r--r--
Set RPM_INSTALL_PREFIX{n} in preun as well
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@322
     4
 * Copyright (C) 2009  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
ali@322
    21
#include "config.h"
ali@322
    22
rhughes@241
    23
#include <limits.h>
rhughes@241
    24
#include <string.h>
ali@322
    25
#include <sys/types.h>
rhughes@241
    26
#include <sys/stat.h>
rhughes@241
    27
#include <stdlib.h>
rhughes@241
    28
#include <stdio.h>
rhughes@241
    29
#include <stdint.h>
ali@339
    30
#include <errno.h>
rhughes@241
    31
#include <unistd.h>
ali@322
    32
#include <fcntl.h>
ali@359
    33
#ifdef MSWIN_API
ali@359
    34
#include <direct.h>
ali@359
    35
#endif
ali@322
    36
#if HAVE_SYS_MMAN_H
ali@322
    37
#include <sys/mman.h>
ali@322
    38
#endif
ali@372
    39
#include <assert.h>
rhughes@241
    40
ali@359
    41
#include "razor.h"
rhughes@241
    42
#include "razor-internal.h"
rhughes@241
    43
ali@322
    44
#ifndef O_BINARY
ali@322
    45
#define O_BINARY	0
ali@322
    46
#endif
ali@322
    47
ali@359
    48
#define RAZOR_ASCII_ISALPHA(c)	\
ali@359
    49
			((c) >= 'A' && (c) <= 'Z' || (c) >= 'a' && (c) <= 'z')
ali@359
    50
ali@338
    51
/* Required by gnulib on non-libc platforms */
ali@338
    52
char *program_name = "librazor";
ali@338
    53
ali@359
    54
static int allow_all_root_names = 0;
ali@359
    55
ali@359
    56
/*
ali@359
    57
 * Primarily intended for testing named roots under UNIX platforms.
ali@359
    58
 */
ali@359
    59
RAZOR_EXPORT void razor_disable_root_name_checks(int disable)
ali@359
    60
{
ali@359
    61
	allow_all_root_names = disable;
ali@359
    62
}
ali@359
    63
ali@359
    64
static int razor_valid_root_name(const char *name)
ali@359
    65
{
ali@359
    66
	if (allow_all_root_names)
ali@359
    67
		return !strchr(name,'/');
ali@359
    68
ali@359
    69
#ifdef MSWIN_API
ali@359
    70
	return RAZOR_ASCII_ISALPHA(name[0]) && name[1] == ':' &&
ali@359
    71
	       name[2] == '\0';
ali@359
    72
#else
ali@359
    73
	return name[0] == '\0';
ali@359
    74
#endif
ali@359
    75
}
ali@359
    76
rhughes@241
    77
int
rhughes@241
    78
razor_create_dir(const char *root, const char *path)
rhughes@241
    79
{
rhughes@241
    80
	char buffer[PATH_MAX], *p;
rhughes@241
    81
	const char *slash, *next;
rhughes@241
    82
	struct stat buf;
rhughes@241
    83
rhughes@241
    84
	/* Create all sub-directories in dir. We know root exists and
ali@359
    85
	 * is a dir, root does not end in a '/', and path either has a
ali@359
    86
	 * leading '/' or (on MS-Windows only) root is the empty string
ali@359
    87
	 * and path starts with drive (eg., "c:/windows"). */
rhughes@241
    88
rhughes@241
    89
	strcpy(buffer, root);
rhughes@241
    90
	p = buffer + strlen(buffer);
rhughes@241
    91
	slash = path;
rhughes@241
    92
	for (slash = path; *slash != '\0'; slash = next) {
rhughes@241
    93
		next = strchr(slash + 1, '/');
rhughes@241
    94
		if (next == NULL)
rhughes@241
    95
			break;
rhughes@241
    96
rhughes@241
    97
		memcpy(p, slash, next - slash);
rhughes@241
    98
		p += next - slash;
rhughes@241
    99
		*p = '\0';
rhughes@241
   100
ali@359
   101
		if (razor_valid_root_name(buffer))
ali@359
   102
			continue;
ali@359
   103
rhughes@241
   104
		if (stat(buffer, &buf) == 0) {
rhughes@241
   105
			if (!S_ISDIR(buf.st_mode)) {
rhughes@241
   106
				fprintf(stderr,
rhughes@241
   107
					"%s exists but is not a directory\n",
rhughes@241
   108
					buffer);
rhughes@241
   109
				return -1;
rhughes@241
   110
			}
rhughes@241
   111
		} else if (mkdir(buffer, 0777) < 0) {
ali@339
   112
			fprintf(stderr, "failed to make directory %s: %s\n",
ali@339
   113
				buffer, strerror(errno));
rhughes@241
   114
			return -1;
rhughes@241
   115
		}
rhughes@241
   116
rhughes@241
   117
		/* FIXME: What to do about permissions for dirs we
rhughes@241
   118
		 * have to create but are not in the cpio archive? */
rhughes@241
   119
	}
rhughes@241
   120
rhughes@241
   121
	return 0;
rhughes@241
   122
}
rhughes@241
   123
rhughes@241
   124
int
rhughes@241
   125
razor_write(int fd, const void *data, size_t size)
rhughes@241
   126
{
rhughes@241
   127
	size_t rest;
rhughes@241
   128
	ssize_t written;
rhughes@241
   129
	const unsigned char *p;
rhughes@241
   130
rhughes@241
   131
	rest = size;
rhughes@241
   132
	p = data;
rhughes@241
   133
	while (rest > 0) {
rhughes@241
   134
		written = write(fd, p, rest);
rhughes@241
   135
		if (written < 0) {
ali@339
   136
			perror("write error");
rhughes@241
   137
			return -1;
rhughes@241
   138
		}
rhughes@241
   139
		rest -= written;
rhughes@241
   140
		p += written;
rhughes@241
   141
	}
rhughes@241
   142
rhughes@241
   143
	return 0;
rhughes@241
   144
}
rhughes@241
   145
ali@322
   146
void *
ali@322
   147
razor_file_get_contents(const char *filename, size_t *length)
ali@322
   148
{
ali@322
   149
	int fd;
ali@322
   150
	struct stat st;
ali@322
   151
	void *addr;
ali@322
   152
#if !HAVE_SYS_MMAN_H
ali@322
   153
	size_t nb;
ali@322
   154
	ssize_t res;
ali@322
   155
#endif
ali@322
   156
ali@322
   157
	fd = open(filename, O_RDONLY | O_BINARY);
ali@322
   158
	if (fd < 0)
ali@322
   159
		return NULL;
ali@322
   160
ali@322
   161
	if (fstat(fd, &st) < 0) {
ali@322
   162
		close(fd);
ali@322
   163
		return NULL;
ali@322
   164
	}
ali@322
   165
ali@322
   166
	*length = st.st_size;
ali@322
   167
#if HAVE_SYS_MMAN_H
ali@322
   168
	addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
ali@322
   169
#else
ali@322
   170
	addr = malloc(st.st_size);
ali@322
   171
	if (addr) {
ali@322
   172
		nb = 0;
ali@322
   173
		while(nb < st.st_size) {
ali@322
   174
			res = read(fd, addr + nb, st.st_size - nb);
ali@322
   175
			if (res <= 0) {
ali@322
   176
				free(addr);
ali@322
   177
				addr = NULL;
ali@322
   178
				break;
ali@322
   179
			}
ali@322
   180
			nb += res;
ali@322
   181
		}
ali@322
   182
	}
ali@322
   183
#endif
ali@322
   184
	close(fd);
ali@322
   185
ali@322
   186
#if HAVE_SYS_MMAN_H
ali@322
   187
	if (addr == MAP_FAILED)
ali@322
   188
		addr = NULL;
ali@322
   189
#endif
ali@322
   190
ali@322
   191
	return addr;
ali@322
   192
}
ali@322
   193
ali@322
   194
int
ali@322
   195
razor_file_free_contents(void *addr, size_t length)
ali@322
   196
{
ali@322
   197
#if HAVE_SYS_MMAN_H
ali@322
   198
	return munmap(addr, length);
ali@322
   199
#else
ali@322
   200
	free(addr);
ali@322
   201
	return 0;
ali@322
   202
#endif
ali@322
   203
}
ali@322
   204
rhughes@241
   205
struct qsort_context {
rhughes@241
   206
	size_t size;
rhughes@241
   207
	razor_compare_with_data_func_t compare;
rhughes@241
   208
	void *data;
rhughes@241
   209
};
rhughes@241
   210
rhughes@241
   211
static void
rhughes@241
   212
qsort_swap(void *p1, void *p2, size_t size)
rhughes@241
   213
{
rhughes@241
   214
	char buffer[size];
rhughes@241
   215
rhughes@241
   216
	memcpy(buffer, p1, size);
rhughes@241
   217
	memcpy(p1, p2, size);
rhughes@241
   218
	memcpy(p2, buffer, size);
rhughes@241
   219
}
rhughes@241
   220
rhughes@241
   221
static void
rhughes@241
   222
__qsort_with_data(void *base, size_t nelem, uint32_t *map,
rhughes@241
   223
		  struct qsort_context *ctx)
rhughes@241
   224
{
rhughes@241
   225
	void *p, *start, *end, *pivot;
rhughes@241
   226
	uint32_t *mp, *mstart, *mend, tmp;
rhughes@241
   227
	int left, right, result;
rhughes@241
   228
	size_t size = ctx->size;
rhughes@241
   229
rhughes@241
   230
	p = base;
rhughes@241
   231
	start = base;
rhughes@241
   232
	end = base + nelem * size;
rhughes@241
   233
	mp = map;
rhughes@241
   234
	mstart = map;
rhughes@241
   235
	mend = map + nelem;
ali@337
   236
	pivot = base + (rand() % nelem) * size;
rhughes@241
   237
rhughes@241
   238
	while (p < end) {
rhughes@241
   239
		result = ctx->compare(p, pivot, ctx->data);
rhughes@241
   240
		if (result < 0) {
rhughes@241
   241
			qsort_swap(p, start, size);
rhughes@241
   242
			tmp = *mp;
rhughes@241
   243
			*mp = *mstart;
rhughes@241
   244
			*mstart = tmp;
rhughes@241
   245
			if (start == pivot)
rhughes@241
   246
				pivot = p;
rhughes@241
   247
			start += size;
rhughes@241
   248
			mstart++;
rhughes@241
   249
			p += size;
rhughes@241
   250
			mp++;
rhughes@241
   251
		} else if (result == 0) {
rhughes@241
   252
			p += size;
rhughes@241
   253
			mp++;
rhughes@241
   254
		} else {
rhughes@241
   255
 			end -= size;
rhughes@241
   256
			mend--;
rhughes@241
   257
			qsort_swap(p, end, size);
rhughes@241
   258
			tmp = *mp;
rhughes@241
   259
			*mp = *mend;
rhughes@241
   260
			*mend = tmp;
rhughes@241
   261
			if (end == pivot)
rhughes@241
   262
				pivot = p;
rhughes@241
   263
		}
rhughes@241
   264
	}
rhughes@241
   265
rhughes@241
   266
	left = (start - base) / size;
rhughes@241
   267
	right = (base + nelem * size - end) / size;
rhughes@241
   268
	if (left > 1)
rhughes@241
   269
		__qsort_with_data(base, left, map, ctx);
rhughes@241
   270
	if (right > 1)
rhughes@241
   271
		__qsort_with_data(end, right, mend, ctx);
rhughes@241
   272
}
rhughes@241
   273
rhughes@241
   274
uint32_t *
rhughes@241
   275
razor_qsort_with_data(void *base, size_t nelem, size_t size,
rhughes@241
   276
		      razor_compare_with_data_func_t compare, void *data)
rhughes@241
   277
{
rhughes@241
   278
	struct qsort_context ctx;
rhughes@241
   279
	uint32_t *map;
rhughes@241
   280
	int i;
rhughes@241
   281
rhughes@241
   282
	if (nelem == 0)
rhughes@241
   283
		return NULL;
rhughes@241
   284
rhughes@241
   285
	ctx.size = size;
rhughes@241
   286
	ctx.compare = compare;
rhughes@241
   287
	ctx.data = data;
rhughes@241
   288
rhughes@241
   289
	map = malloc(nelem * sizeof (uint32_t));
rhughes@241
   290
	for (i = 0; i < nelem; i++)
rhughes@241
   291
		map[i] = i;
rhughes@241
   292
rhughes@241
   293
	__qsort_with_data(base, nelem, map, &ctx);
rhughes@241
   294
rhughes@241
   295
	return map;
rhughes@241
   296
}
ali@372
   297
ali@372
   298
void environment_init(struct environment *env)
ali@372
   299
{
ali@372
   300
	env->is_set = 0;
ali@372
   301
	array_init(&env->string_pool);
ali@372
   302
	array_init(&env->vars);
ali@372
   303
}
ali@372
   304
ali@372
   305
void environment_add_variable(struct environment *env,
ali@372
   306
			      const char *variable, const char *value)
ali@372
   307
{
ali@372
   308
	char *s;
ali@372
   309
	uint32_t *r;
ali@372
   310
	assert(!env->is_set);
ali@372
   311
ali@372
   312
	s = array_add(&env->string_pool,
ali@372
   313
		      strlen(variable) + strlen(value) + 2);
ali@372
   314
	sprintf(s, "%s=%s", variable, value);
ali@372
   315
	r = array_add(&env->vars, sizeof *r);
ali@372
   316
	*r = s - (char *)env->string_pool.data;
ali@372
   317
}
ali@372
   318
ali@372
   319
void environment_set(struct environment *env)
ali@372
   320
{
ali@372
   321
	int i, count;
ali@372
   322
	char *s;
ali@372
   323
        uint32_t *r;
ali@372
   324
ali@372
   325
	if (!env->is_set) {
ali@372
   326
		count = env->vars.size / sizeof(uint32_t);
ali@372
   327
		r = (uint32_t *)env->vars.data;
ali@372
   328
		for (i = 0; i < count; i++) {
ali@372
   329
			s = env->string_pool.data + *r++;
ali@372
   330
			putenv(s);
ali@372
   331
		}
ali@372
   332
ali@372
   333
		env->is_set = 1;
ali@372
   334
	}
ali@372
   335
}
ali@372
   336
ali@372
   337
void environment_unset(struct environment *env)
ali@372
   338
{
ali@372
   339
	int i, count;
ali@372
   340
	char c, *s, *t;
ali@372
   341
        uint32_t *r;
ali@372
   342
ali@372
   343
	if (env->is_set) {
ali@372
   344
		count = env->vars.size / sizeof(uint32_t);
ali@372
   345
		r = (uint32_t *)env->vars.data;
ali@372
   346
		for (i = 0; i < count; i++) {
ali@372
   347
			s = env->string_pool.data + *r++;
ali@372
   348
			t = strchr(s, '=') + 1;
ali@372
   349
			c = *t;
ali@372
   350
			*t = '\0';
ali@372
   351
			putenv(s);
ali@372
   352
			*t = c;
ali@372
   353
		}
ali@372
   354
ali@372
   355
		env->is_set = 0;
ali@372
   356
	}
ali@372
   357
}
ali@372
   358
ali@372
   359
void environment_release(struct environment *env)
ali@372
   360
{
ali@372
   361
	environment_unset(env);
ali@372
   362
	array_release(&env->string_pool);
ali@372
   363
	array_release(&env->vars);
ali@372
   364
}