diff -r 3f841a46eab5 -r 008c75a5e08d librazor/util.c --- a/librazor/util.c Fri Oct 17 10:10:57 2014 +0100 +++ b/librazor/util.c Mon Jul 04 10:48:18 2016 +0100 @@ -1,7 +1,7 @@ /* * Copyright (C) 2008 Kristian Høgsberg * Copyright (C) 2008 Red Hat, Inc - * Copyright (C) 2009, 2011, 2012, 2014 J. Ali Harlow + * Copyright (C) 2009, 2011, 2012, 2014, 2016 J. Ali Harlow * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -22,32 +22,20 @@ #include #include -#include -#include #include #include #include #include #include -#include -#ifdef MSWIN_API -#include -#include -#else +#ifndef MSWIN_API #include #endif -#if HAVE_SYS_MMAN_H -#include -#endif #include #include "razor.h" +#include "types/types.h" #include "razor-internal.h" -#ifndef O_BINARY -#define O_BINARY 0 -#endif - /* Required by gnulib on non-libc platforms */ char *program_name = "librazor"; @@ -62,130 +50,6 @@ return p; } -#if HAVE_SYS_MMAN_H -#define OPEN_FILE_USED (1U<<0) -#define OPEN_FILE_MMAPPED (1U<<1) - -struct open_file { - void *addr; - size_t length; - uint32_t flags; -}; - -struct array open_files = { 0, }; -#endif /* HAVE_SYS_MMAN_H */ - -void * -razor_file_get_contents(const char *filename, size_t *length, int private, - struct razor_error **error) -{ - int fd; - struct stat st; - void *addr = NULL; - size_t nb; - ssize_t res; -#if HAVE_SYS_MMAN_H - struct open_file *of, *ofend; -#endif - - fd = open(filename, O_RDONLY | O_BINARY); - if (fd < 0) { - razor_set_error_posix(error, filename); - return NULL; - } - - if (fstat(fd, &st) < 0) { - razor_set_error_posix(error, filename); - close(fd); - return NULL; - } - - *length = st.st_size; - -#if HAVE_SYS_MMAN_H - ofend = open_files.data + open_files.size; - for (of = open_files.data; of < ofend; of++) - if (!(of->flags & OPEN_FILE_USED)) - break; - if (of == ofend) { - of = array_add(&open_files, sizeof *of); - of->flags = 0; - } - - if (!private) { - addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); - if (addr == MAP_FAILED) - addr = NULL; - else - of->flags = OPEN_FILE_USED | OPEN_FILE_MMAPPED; - } -#endif /* HAVE_SYS_MMAN_H */ - if (!addr) { - addr = malloc(st.st_size); - if (addr) { -#if HAVE_SYS_MMAN_H - of->flags = OPEN_FILE_USED; -#endif - nb = 0; - while(nb < st.st_size) { - res = read(fd, addr + nb, st.st_size - nb); - if (res <= 0) { - razor_set_error_posix(error, filename); - free(addr); - addr = NULL; - break; - } - nb += res; - } - } else - razor_set_error(error, RAZOR_POSIX_ERROR, ENOMEM, NULL, - "Not enough memory"); - } - close(fd); - -#if HAVE_SYS_MMAN_H - of->addr = addr; - of->length = st.st_size; -#endif - - return addr; -} - -int razor_file_free_contents(void *addr, size_t length) -{ -#if HAVE_SYS_MMAN_H - int retval, mmapped; - struct open_file *of, *ofend; - - ofend = open_files.data + open_files.size; - for (of = open_files.data; of < ofend; of++) - if ((of->flags & OPEN_FILE_USED) && of->addr == addr) - break; - - if (of == ofend) - return 1; - - length = of->length; - mmapped = of->flags & OPEN_FILE_MMAPPED; - of->flags &= ~OPEN_FILE_USED; - - for (of = open_files.data; of < ofend; of++) - if (of->flags & OPEN_FILE_USED) - break; - - if (of == ofend) { - array_release(&open_files); - array_init(&open_files); - } - - if (mmapped) - return munmap(addr, length); -#endif - - free(addr); - return 0; -} - struct qsort_context { size_t size; razor_compare_with_data_func_t compare;