librazor/util.c
author J. Ali Harlow <ali@juiblex.co.uk>
Thu Jan 08 17:50:04 2009 +0000 (2009-01-08)
changeset 338 47f3e27cb978
parent 337 c89695ed29f0
child 339 159067260aad
permissions -rw-r--r--
Provide a program_name global variable as required by gnulib to
avoid the need for all programs that use librazor to do so.
     1 /*
     2  * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
     3  * Copyright (C) 2008  Red Hat, Inc
     4  * Copyright (C) 2009  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 #include "config.h"
    22 
    23 #include <limits.h>
    24 #include <string.h>
    25 #include <sys/types.h>
    26 #include <sys/stat.h>
    27 #include <stdlib.h>
    28 #include <stdio.h>
    29 #include <stdint.h>
    30 #include <unistd.h>
    31 #include <fcntl.h>
    32 #if HAVE_SYS_MMAN_H
    33 #include <sys/mman.h>
    34 #endif
    35 
    36 #include "razor-internal.h"
    37 
    38 #ifndef O_BINARY
    39 #define O_BINARY	0
    40 #endif
    41 
    42 /* Required by gnulib on non-libc platforms */
    43 char *program_name = "librazor";
    44 
    45 int
    46 razor_create_dir(const char *root, const char *path)
    47 {
    48 	char buffer[PATH_MAX], *p;
    49 	const char *slash, *next;
    50 	struct stat buf;
    51 
    52 	/* Create all sub-directories in dir. We know root exists and
    53 	 * is a dir, root does not end in a '/', and path has a
    54 	 * leading '/'. */
    55 
    56 	strcpy(buffer, root);
    57 	p = buffer + strlen(buffer);
    58 	slash = path;
    59 	for (slash = path; *slash != '\0'; slash = next) {
    60 		next = strchr(slash + 1, '/');
    61 		if (next == NULL)
    62 			break;
    63 
    64 		memcpy(p, slash, next - slash);
    65 		p += next - slash;
    66 		*p = '\0';
    67 
    68 		if (stat(buffer, &buf) == 0) {
    69 			if (!S_ISDIR(buf.st_mode)) {
    70 				fprintf(stderr,
    71 					"%s exists but is not a directory\n",
    72 					buffer);
    73 				return -1;
    74 			}
    75 		} else if (mkdir(buffer, 0777) < 0) {
    76 			fprintf(stderr, "failed to make directory %s: %m\n",
    77 				buffer);
    78 			return -1;
    79 		}
    80 
    81 		/* FIXME: What to do about permissions for dirs we
    82 		 * have to create but are not in the cpio archive? */
    83 	}
    84 
    85 	return 0;
    86 }
    87 
    88 int
    89 razor_write(int fd, const void *data, size_t size)
    90 {
    91 	size_t rest;
    92 	ssize_t written;
    93 	const unsigned char *p;
    94 
    95 	rest = size;
    96 	p = data;
    97 	while (rest > 0) {
    98 		written = write(fd, p, rest);
    99 		if (written < 0) {
   100 			fprintf(stderr, "write error: %m\n");
   101 			return -1;
   102 		}
   103 		rest -= written;
   104 		p += written;
   105 	}
   106 
   107 	return 0;
   108 }
   109 
   110 void *
   111 razor_file_get_contents(const char *filename, size_t *length)
   112 {
   113 	int fd;
   114 	struct stat st;
   115 	void *addr;
   116 #if !HAVE_SYS_MMAN_H
   117 	size_t nb;
   118 	ssize_t res;
   119 #endif
   120 
   121 	fd = open(filename, O_RDONLY | O_BINARY);
   122 	if (fd < 0)
   123 		return NULL;
   124 
   125 	if (fstat(fd, &st) < 0) {
   126 		close(fd);
   127 		return NULL;
   128 	}
   129 
   130 	*length = st.st_size;
   131 #if HAVE_SYS_MMAN_H
   132 	addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
   133 #else
   134 	addr = malloc(st.st_size);
   135 	if (addr) {
   136 		nb = 0;
   137 		while(nb < st.st_size) {
   138 			res = read(fd, addr + nb, st.st_size - nb);
   139 			if (res <= 0) {
   140 				free(addr);
   141 				addr = NULL;
   142 				break;
   143 			}
   144 			nb += res;
   145 		}
   146 	}
   147 #endif
   148 	close(fd);
   149 
   150 #if HAVE_SYS_MMAN_H
   151 	if (addr == MAP_FAILED)
   152 		addr = NULL;
   153 #endif
   154 
   155 	return addr;
   156 }
   157 
   158 int
   159 razor_file_free_contents(void *addr, size_t length)
   160 {
   161 #if HAVE_SYS_MMAN_H
   162 	return munmap(addr, length);
   163 #else
   164 	free(addr);
   165 	return 0;
   166 #endif
   167 }
   168 
   169 struct qsort_context {
   170 	size_t size;
   171 	razor_compare_with_data_func_t compare;
   172 	void *data;
   173 };
   174 
   175 static void
   176 qsort_swap(void *p1, void *p2, size_t size)
   177 {
   178 	char buffer[size];
   179 
   180 	memcpy(buffer, p1, size);
   181 	memcpy(p1, p2, size);
   182 	memcpy(p2, buffer, size);
   183 }
   184 
   185 static void
   186 __qsort_with_data(void *base, size_t nelem, uint32_t *map,
   187 		  struct qsort_context *ctx)
   188 {
   189 	void *p, *start, *end, *pivot;
   190 	uint32_t *mp, *mstart, *mend, tmp;
   191 	int left, right, result;
   192 	size_t size = ctx->size;
   193 
   194 	p = base;
   195 	start = base;
   196 	end = base + nelem * size;
   197 	mp = map;
   198 	mstart = map;
   199 	mend = map + nelem;
   200 	pivot = base + (rand() % nelem) * size;
   201 
   202 	while (p < end) {
   203 		result = ctx->compare(p, pivot, ctx->data);
   204 		if (result < 0) {
   205 			qsort_swap(p, start, size);
   206 			tmp = *mp;
   207 			*mp = *mstart;
   208 			*mstart = tmp;
   209 			if (start == pivot)
   210 				pivot = p;
   211 			start += size;
   212 			mstart++;
   213 			p += size;
   214 			mp++;
   215 		} else if (result == 0) {
   216 			p += size;
   217 			mp++;
   218 		} else {
   219  			end -= size;
   220 			mend--;
   221 			qsort_swap(p, end, size);
   222 			tmp = *mp;
   223 			*mp = *mend;
   224 			*mend = tmp;
   225 			if (end == pivot)
   226 				pivot = p;
   227 		}
   228 	}
   229 
   230 	left = (start - base) / size;
   231 	right = (base + nelem * size - end) / size;
   232 	if (left > 1)
   233 		__qsort_with_data(base, left, map, ctx);
   234 	if (right > 1)
   235 		__qsort_with_data(end, right, mend, ctx);
   236 }
   237 
   238 uint32_t *
   239 razor_qsort_with_data(void *base, size_t nelem, size_t size,
   240 		      razor_compare_with_data_func_t compare, void *data)
   241 {
   242 	struct qsort_context ctx;
   243 	uint32_t *map;
   244 	int i;
   245 
   246 	if (nelem == 0)
   247 		return NULL;
   248 
   249 	ctx.size = size;
   250 	ctx.compare = compare;
   251 	ctx.data = data;
   252 
   253 	map = malloc(nelem * sizeof (uint32_t));
   254 	for (i = 0; i < nelem; i++)
   255 		map[i] = i;
   256 
   257 	__qsort_with_data(base, nelem, map, &ctx);
   258 
   259 	return map;
   260 }