librazor/util.c
changeset 327 c85643dd7164
parent 300 455eaa569767
child 337 c89695ed29f0
     1.1 --- a/librazor/util.c	Sun Jun 29 10:55:17 2008 +0100
     1.2 +++ b/librazor/util.c	Thu Jan 08 13:51:07 2009 +0000
     1.3 @@ -1,6 +1,7 @@
     1.4  /*
     1.5   * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
     1.6   * Copyright (C) 2008  Red Hat, Inc
     1.7 + * Copyright (C) 2009  J. Ali Harlow <ali@juiblex.co.uk>
     1.8   *
     1.9   * This program is free software; you can redistribute it and/or modify
    1.10   * it under the terms of the GNU General Public License as published by
    1.11 @@ -17,16 +18,27 @@
    1.12   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    1.13   */
    1.14  
    1.15 +#include "config.h"
    1.16 +
    1.17  #include <limits.h>
    1.18  #include <string.h>
    1.19 +#include <sys/types.h>
    1.20  #include <sys/stat.h>
    1.21  #include <stdlib.h>
    1.22  #include <stdio.h>
    1.23  #include <stdint.h>
    1.24  #include <unistd.h>
    1.25 +#include <fcntl.h>
    1.26 +#if HAVE_SYS_MMAN_H
    1.27 +#include <sys/mman.h>
    1.28 +#endif
    1.29  
    1.30  #include "razor-internal.h"
    1.31  
    1.32 +#ifndef O_BINARY
    1.33 +#define O_BINARY	0
    1.34 +#endif
    1.35 +
    1.36  int
    1.37  razor_create_dir(const char *root, const char *path)
    1.38  {
    1.39 @@ -92,6 +104,65 @@
    1.40  	return 0;
    1.41  }
    1.42  
    1.43 +void *
    1.44 +razor_file_get_contents(const char *filename, size_t *length)
    1.45 +{
    1.46 +	int fd;
    1.47 +	struct stat st;
    1.48 +	void *addr;
    1.49 +#if !HAVE_SYS_MMAN_H
    1.50 +	size_t nb;
    1.51 +	ssize_t res;
    1.52 +#endif
    1.53 +
    1.54 +	fd = open(filename, O_RDONLY | O_BINARY);
    1.55 +	if (fd < 0)
    1.56 +		return NULL;
    1.57 +
    1.58 +	if (fstat(fd, &st) < 0) {
    1.59 +		close(fd);
    1.60 +		return NULL;
    1.61 +	}
    1.62 +
    1.63 +	*length = st.st_size;
    1.64 +#if HAVE_SYS_MMAN_H
    1.65 +	addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
    1.66 +#else
    1.67 +	addr = malloc(st.st_size);
    1.68 +	if (addr) {
    1.69 +		nb = 0;
    1.70 +		while(nb < st.st_size) {
    1.71 +			res = read(fd, addr + nb, st.st_size - nb);
    1.72 +			if (res <= 0) {
    1.73 +				free(addr);
    1.74 +				addr = NULL;
    1.75 +				break;
    1.76 +			}
    1.77 +			nb += res;
    1.78 +		}
    1.79 +	}
    1.80 +#endif
    1.81 +	close(fd);
    1.82 +
    1.83 +#if HAVE_SYS_MMAN_H
    1.84 +	if (addr == MAP_FAILED)
    1.85 +		addr = NULL;
    1.86 +#endif
    1.87 +
    1.88 +	return addr;
    1.89 +}
    1.90 +
    1.91 +int
    1.92 +razor_file_free_contents(void *addr, size_t length)
    1.93 +{
    1.94 +#if HAVE_SYS_MMAN_H
    1.95 +	return munmap(addr, length);
    1.96 +#else
    1.97 +	free(addr);
    1.98 +	return 0;
    1.99 +#endif
   1.100 +}
   1.101 +
   1.102  struct qsort_context {
   1.103  	size_t size;
   1.104  	razor_compare_with_data_func_t compare;