Fix bug causing "razor list-files" to fail to see any files.
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>
37 #include "razor-internal.h"
43 /* Required by gnulib on non-libc platforms */
44 char *program_name = "librazor";
47 razor_create_dir(const char *root, const char *path)
49 char buffer[PATH_MAX], *p;
50 const char *slash, *next;
53 /* Create all sub-directories in dir. We know root exists and
54 * is a dir, root does not end in a '/', and path has a
58 p = buffer + strlen(buffer);
60 for (slash = path; *slash != '\0'; slash = next) {
61 next = strchr(slash + 1, '/');
65 memcpy(p, slash, next - slash);
69 if (stat(buffer, &buf) == 0) {
70 if (!S_ISDIR(buf.st_mode)) {
72 "%s exists but is not a directory\n",
76 } else if (mkdir(buffer, 0777) < 0) {
77 fprintf(stderr, "failed to make directory %s: %s\n",
78 buffer, strerror(errno));
82 /* FIXME: What to do about permissions for dirs we
83 * have to create but are not in the cpio archive? */
90 razor_write(int fd, const void *data, size_t size)
94 const unsigned char *p;
99 written = write(fd, p, rest);
101 perror("write error");
112 razor_file_get_contents(const char *filename, size_t *length)
122 fd = open(filename, O_RDONLY | O_BINARY);
126 if (fstat(fd, &st) < 0) {
131 *length = st.st_size;
133 addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
135 addr = malloc(st.st_size);
138 while(nb < st.st_size) {
139 res = read(fd, addr + nb, st.st_size - nb);
152 if (addr == MAP_FAILED)
160 razor_file_free_contents(void *addr, size_t length)
163 return munmap(addr, length);
170 struct qsort_context {
172 razor_compare_with_data_func_t compare;
177 qsort_swap(void *p1, void *p2, size_t size)
181 memcpy(buffer, p1, size);
182 memcpy(p1, p2, size);
183 memcpy(p2, buffer, size);
187 __qsort_with_data(void *base, size_t nelem, uint32_t *map,
188 struct qsort_context *ctx)
190 void *p, *start, *end, *pivot;
191 uint32_t *mp, *mstart, *mend, tmp;
192 int left, right, result;
193 size_t size = ctx->size;
197 end = base + nelem * size;
201 pivot = base + (rand() % nelem) * size;
204 result = ctx->compare(p, pivot, ctx->data);
206 qsort_swap(p, start, size);
216 } else if (result == 0) {
222 qsort_swap(p, end, size);
231 left = (start - base) / size;
232 right = (base + nelem * size - end) / size;
234 __qsort_with_data(base, left, map, ctx);
236 __qsort_with_data(end, right, mend, ctx);
240 razor_qsort_with_data(void *base, size_t nelem, size_t size,
241 razor_compare_with_data_func_t compare, void *data)
243 struct qsort_context ctx;
251 ctx.compare = compare;
254 map = malloc(nelem * sizeof (uint32_t));
255 for (i = 0; i < nelem; i++)
258 __qsort_with_data(base, nelem, map, &ctx);