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