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"
43 razor_create_dir(const char *root, const char *path)
45 char buffer[PATH_MAX], *p;
46 const char *slash, *next;
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
54 p = buffer + strlen(buffer);
56 for (slash = path; *slash != '\0'; slash = next) {
57 next = strchr(slash + 1, '/');
61 memcpy(p, slash, next - slash);
65 if (stat(buffer, &buf) == 0) {
66 if (!S_ISDIR(buf.st_mode)) {
68 "%s exists but is not a directory\n",
72 } else if (mkdir(buffer, 0777) < 0) {
73 fprintf(stderr, "failed to make directory %s: %m\n",
78 /* FIXME: What to do about permissions for dirs we
79 * have to create but are not in the cpio archive? */
86 razor_write(int fd, const void *data, size_t size)
90 const unsigned char *p;
95 written = write(fd, p, rest);
97 fprintf(stderr, "write error: %m\n");
108 razor_file_get_contents(const char *filename, size_t *length)
118 fd = open(filename, O_RDONLY | O_BINARY);
122 if (fstat(fd, &st) < 0) {
127 *length = st.st_size;
129 addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
131 addr = malloc(st.st_size);
134 while(nb < st.st_size) {
135 res = read(fd, addr + nb, st.st_size - nb);
148 if (addr == MAP_FAILED)
156 razor_file_free_contents(void *addr, size_t length)
159 return munmap(addr, length);
166 struct qsort_context {
168 razor_compare_with_data_func_t compare;
173 qsort_swap(void *p1, void *p2, size_t size)
177 memcpy(buffer, p1, size);
178 memcpy(p1, p2, size);
179 memcpy(p2, buffer, size);
183 __qsort_with_data(void *base, size_t nelem, uint32_t *map,
184 struct qsort_context *ctx)
186 void *p, *start, *end, *pivot;
187 uint32_t *mp, *mstart, *mend, tmp;
188 int left, right, result;
189 size_t size = ctx->size;
193 end = base + nelem * size;
197 pivot = base + (random() % nelem) * size;
200 result = ctx->compare(p, pivot, ctx->data);
202 qsort_swap(p, start, size);
212 } else if (result == 0) {
218 qsort_swap(p, end, size);
227 left = (start - base) / size;
228 right = (base + nelem * size - end) / size;
230 __qsort_with_data(base, left, map, ctx);
232 __qsort_with_data(end, right, mend, ctx);
236 razor_qsort_with_data(void *base, size_t nelem, size_t size,
237 razor_compare_with_data_func_t compare, void *data)
239 struct qsort_context ctx;
247 ctx.compare = compare;
250 map = malloc(nelem * sizeof (uint32_t));
251 for (i = 0; i < nelem; i++)
254 __qsort_with_data(base, nelem, map, &ctx);