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>
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.
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.
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.
25 #include <sys/types.h>
36 #include "razor-internal.h"
42 /* Required by gnulib on non-libc platforms */
43 char *program_name = "librazor";
46 razor_create_dir(const char *root, const char *path)
48 char buffer[PATH_MAX], *p;
49 const char *slash, *next;
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
57 p = buffer + strlen(buffer);
59 for (slash = path; *slash != '\0'; slash = next) {
60 next = strchr(slash + 1, '/');
64 memcpy(p, slash, next - slash);
68 if (stat(buffer, &buf) == 0) {
69 if (!S_ISDIR(buf.st_mode)) {
71 "%s exists but is not a directory\n",
75 } else if (mkdir(buffer, 0777) < 0) {
76 fprintf(stderr, "failed to make directory %s: %m\n",
81 /* FIXME: What to do about permissions for dirs we
82 * have to create but are not in the cpio archive? */
89 razor_write(int fd, const void *data, size_t size)
93 const unsigned char *p;
98 written = write(fd, p, rest);
100 fprintf(stderr, "write error: %m\n");
111 razor_file_get_contents(const char *filename, size_t *length)
121 fd = open(filename, O_RDONLY | O_BINARY);
125 if (fstat(fd, &st) < 0) {
130 *length = st.st_size;
132 addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
134 addr = malloc(st.st_size);
137 while(nb < st.st_size) {
138 res = read(fd, addr + nb, st.st_size - nb);
151 if (addr == MAP_FAILED)
159 razor_file_free_contents(void *addr, size_t length)
162 return munmap(addr, length);
169 struct qsort_context {
171 razor_compare_with_data_func_t compare;
176 qsort_swap(void *p1, void *p2, size_t size)
180 memcpy(buffer, p1, size);
181 memcpy(p1, p2, size);
182 memcpy(p2, buffer, size);
186 __qsort_with_data(void *base, size_t nelem, uint32_t *map,
187 struct qsort_context *ctx)
189 void *p, *start, *end, *pivot;
190 uint32_t *mp, *mstart, *mend, tmp;
191 int left, right, result;
192 size_t size = ctx->size;
196 end = base + nelem * size;
200 pivot = base + (rand() % nelem) * size;
203 result = ctx->compare(p, pivot, ctx->data);
205 qsort_swap(p, start, size);
215 } else if (result == 0) {
221 qsort_swap(p, end, size);
230 left = (start - base) / size;
231 right = (base + nelem * size - end) / size;
233 __qsort_with_data(base, left, map, ctx);
235 __qsort_with_data(end, right, mend, ctx);
239 razor_qsort_with_data(void *base, size_t nelem, size_t size,
240 razor_compare_with_data_func_t compare, void *data)
242 struct qsort_context ctx;
250 ctx.compare = compare;
253 map = malloc(nelem * sizeof (uint32_t));
254 for (i = 0; i < nelem; i++)
257 __qsort_with_data(base, nelem, map, &ctx);