Support platforms without mmap
authorJ. Ali Harlow <ali@juiblex.co.uk>
Tue Jan 06 17:33:37 2009 +0000 (2009-01-06)
changeset 32266c281524c98
parent 321 6f804141872c
child 323 3b24a0bd41ee
Support platforms without mmap
configure.ac
librazor/razor-internal.h
librazor/razor.c
librazor/rpm.c
librazor/util.c
     1.1 --- a/configure.ac	Tue Jan 06 16:00:32 2009 +0000
     1.2 +++ b/configure.ac	Tue Jan 06 17:33:37 2009 +0000
     1.3 @@ -21,6 +21,7 @@
     1.4  AC_PROG_CC
     1.5  AM_PROG_CC_STDC
     1.6  AC_HEADER_STDC
     1.7 +AC_CHECK_HEADERS([sys/mman.h])
     1.8  AM_PROG_LIBTOOL
     1.9  AC_PROG_MAKE_SET
    1.10  AC_PROG_LN_S
     2.1 --- a/librazor/razor-internal.h	Tue Jan 06 16:00:32 2009 +0000
     2.2 +++ b/librazor/razor-internal.h	Tue Jan 06 17:33:37 2009 +0000
     2.3 @@ -222,6 +222,9 @@
     2.4  int razor_create_dir(const char *root, const char *path);
     2.5  int razor_write(int fd, const void *data, size_t size);
     2.6  
     2.7 +void *razor_file_get_contents(const char *filename, size_t *length);
     2.8 +int razor_file_free_contents(void *addr, size_t length);
     2.9 +
    2.10  
    2.11  typedef int (*razor_compare_with_data_func_t)(const void *p1,
    2.12  					      const void *p,
     3.1 --- a/librazor/razor.c	Tue Jan 06 16:00:32 2009 +0000
     3.2 +++ b/librazor/razor.c	Tue Jan 06 17:33:37 2009 +0000
     3.3 @@ -27,7 +27,6 @@
     3.4  #include <string.h>
     3.5  #include <sys/types.h>
     3.6  #include <sys/stat.h>
     3.7 -#include <sys/mman.h>
     3.8  #include <unistd.h>
     3.9  #include <fcntl.h>
    3.10  #include <errno.h>
    3.11 @@ -101,18 +100,13 @@
    3.12  			const char *filename)
    3.13  {
    3.14  	struct razor_set_section *s, *sections;
    3.15 -	struct stat stat;
    3.16  	struct array *array;
    3.17  	const char *pool;
    3.18 -	int fd, i;
    3.19 +	int i;
    3.20  
    3.21 -	fd = open(filename, O_RDONLY);
    3.22 -	if (fstat(fd, &stat) < 0)
    3.23 +	*header = razor_file_get_contents(filename, header_size);
    3.24 +	if (!*header)
    3.25  		return -1;
    3.26 -	*header = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
    3.27 -	if (*header == MAP_FAILED)
    3.28 -		return -1;
    3.29 -	*header_size = stat.st_size;
    3.30  
    3.31  	sections = (void *) *header + sizeof **header;
    3.32  	pool = (void *) sections + (*header)->num_sections * sizeof *sections;
    3.33 @@ -131,7 +125,6 @@
    3.34  		array->size = s->size;
    3.35  		array->alloc = s->size;
    3.36  	}
    3.37 -	close(fd);
    3.38  
    3.39  	return 0;
    3.40  }
    3.41 @@ -180,7 +173,7 @@
    3.42  	assert (set != NULL);
    3.43  
    3.44  	if (set->header) {
    3.45 -		munmap(set->header, set->header_size);
    3.46 +		razor_file_free_contents(set->header, set->header_size);
    3.47  	} else {
    3.48  		for (i = 0; i < ARRAY_SIZE(razor_sections); i++) {
    3.49  			a = (void *) set + razor_sections[i].offset;
    3.50 @@ -189,7 +182,8 @@
    3.51  	}
    3.52  
    3.53  	if (set->details_header) {
    3.54 -		munmap(set->details_header, set->details_header_size);
    3.55 +		razor_file_free_contents(set->details_header,
    3.56 +			set->details_header_size);
    3.57  	} else {
    3.58  		for (i = 0; i < ARRAY_SIZE(razor_details_sections); i++) {
    3.59  			a = (void *) set + razor_details_sections[i].offset;
    3.60 @@ -198,7 +192,8 @@
    3.61  	}
    3.62  
    3.63  	if (set->files_header) {
    3.64 -		munmap(set->files_header, set->files_header_size);
    3.65 +		razor_file_free_contents(set->files_header,
    3.66 +			set->files_header_size);
    3.67  	} else {
    3.68  		for (i = 0; i < ARRAY_SIZE(razor_files_sections); i++) {
    3.69  			a = (void *) set + razor_files_sections[i].offset;
     4.1 --- a/librazor/rpm.c	Tue Jan 06 16:00:32 2009 +0000
     4.2 +++ b/librazor/rpm.c	Tue Jan 06 17:33:37 2009 +0000
     4.3 @@ -23,7 +23,6 @@
     4.4  #include <string.h>
     4.5  #include <errno.h>
     4.6  #include <sys/stat.h>
     4.7 -#include <sys/mman.h>
     4.8  #include <sys/types.h>
     4.9  #include <sys/wait.h>
    4.10  #include <fcntl.h>
    4.11 @@ -353,10 +352,8 @@
    4.12  {
    4.13  	struct razor_rpm *rpm;
    4.14  	struct rpm_header_index *base, *index;
    4.15 -	struct stat buf;
    4.16  	unsigned int count, i, nindex, hsize;
    4.17  	const char *name;
    4.18 -	int fd;
    4.19  
    4.20  	assert (filename != NULL);
    4.21  
    4.22 @@ -365,25 +362,12 @@
    4.23  		return NULL;
    4.24  	memset(rpm, 0, sizeof *rpm);
    4.25  
    4.26 -	fd = open(filename, O_RDONLY);
    4.27 -	if (fd < 0) {
    4.28 -		fprintf(stderr, "couldn't open %s\n", filename);
    4.29 +	rpm->map = razor_file_get_contents(filename, &rpm->size);
    4.30 +	if (!rpm->map) {
    4.31 +		fprintf(stderr, "couldn't get contents of %s (%m)\n", filename);
    4.32  		return NULL;
    4.33  	}
    4.34  
    4.35 -	if (fstat(fd, &buf) < 0) {
    4.36 -		fprintf(stderr, "failed to stat %s (%m)\n", filename);
    4.37 -		return NULL;
    4.38 -	}
    4.39 -
    4.40 -	rpm->size = buf.st_size;
    4.41 -	rpm->map = mmap(NULL, rpm->size, PROT_READ, MAP_PRIVATE, fd, 0);
    4.42 -	if (rpm->map == MAP_FAILED) {
    4.43 -		fprintf(stderr, "couldn't mmap %s\n", filename);
    4.44 -		return NULL;
    4.45 -	}
    4.46 -	close(fd);
    4.47 -
    4.48  	rpm->signature = rpm->map + RPM_LEAD_SIZE;
    4.49  	nindex = ntohl(rpm->signature->nindex);
    4.50  	hsize = ntohl(rpm->signature->hsize);
    4.51 @@ -780,7 +764,7 @@
    4.52  	assert (rpm != NULL);
    4.53  
    4.54  	free(rpm->dirs);
    4.55 -	err = munmap(rpm->map, rpm->size);
    4.56 +	err = razor_file_free_contents(rpm->map, rpm->size);
    4.57  	free(rpm);
    4.58  
    4.59  	return err;
     5.1 --- a/librazor/util.c	Tue Jan 06 16:00:32 2009 +0000
     5.2 +++ b/librazor/util.c	Tue Jan 06 17:33:37 2009 +0000
     5.3 @@ -1,6 +1,7 @@
     5.4  /*
     5.5   * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
     5.6   * Copyright (C) 2008  Red Hat, Inc
     5.7 + * Copyright (C) 2009  J. Ali Harlow <ali@juiblex.co.uk>
     5.8   *
     5.9   * This program is free software; you can redistribute it and/or modify
    5.10   * it under the terms of the GNU General Public License as published by
    5.11 @@ -17,16 +18,27 @@
    5.12   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    5.13   */
    5.14  
    5.15 +#include "config.h"
    5.16 +
    5.17  #include <limits.h>
    5.18  #include <string.h>
    5.19 +#include <sys/types.h>
    5.20  #include <sys/stat.h>
    5.21  #include <stdlib.h>
    5.22  #include <stdio.h>
    5.23  #include <stdint.h>
    5.24  #include <unistd.h>
    5.25 +#include <fcntl.h>
    5.26 +#if HAVE_SYS_MMAN_H
    5.27 +#include <sys/mman.h>
    5.28 +#endif
    5.29  
    5.30  #include "razor-internal.h"
    5.31  
    5.32 +#ifndef O_BINARY
    5.33 +#define O_BINARY	0
    5.34 +#endif
    5.35 +
    5.36  int
    5.37  razor_create_dir(const char *root, const char *path)
    5.38  {
    5.39 @@ -92,6 +104,65 @@
    5.40  	return 0;
    5.41  }
    5.42  
    5.43 +void *
    5.44 +razor_file_get_contents(const char *filename, size_t *length)
    5.45 +{
    5.46 +	int fd;
    5.47 +	struct stat st;
    5.48 +	void *addr;
    5.49 +#if !HAVE_SYS_MMAN_H
    5.50 +	size_t nb;
    5.51 +	ssize_t res;
    5.52 +#endif
    5.53 +
    5.54 +	fd = open(filename, O_RDONLY | O_BINARY);
    5.55 +	if (fd < 0)
    5.56 +		return NULL;
    5.57 +
    5.58 +	if (fstat(fd, &st) < 0) {
    5.59 +		close(fd);
    5.60 +		return NULL;
    5.61 +	}
    5.62 +
    5.63 +	*length = st.st_size;
    5.64 +#if HAVE_SYS_MMAN_H
    5.65 +	addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
    5.66 +#else
    5.67 +	addr = malloc(st.st_size);
    5.68 +	if (addr) {
    5.69 +		nb = 0;
    5.70 +		while(nb < st.st_size) {
    5.71 +			res = read(fd, addr + nb, st.st_size - nb);
    5.72 +			if (res <= 0) {
    5.73 +				free(addr);
    5.74 +				addr = NULL;
    5.75 +				break;
    5.76 +			}
    5.77 +			nb += res;
    5.78 +		}
    5.79 +	}
    5.80 +#endif
    5.81 +	close(fd);
    5.82 +
    5.83 +#if HAVE_SYS_MMAN_H
    5.84 +	if (addr == MAP_FAILED)
    5.85 +		addr = NULL;
    5.86 +#endif
    5.87 +
    5.88 +	return addr;
    5.89 +}
    5.90 +
    5.91 +int
    5.92 +razor_file_free_contents(void *addr, size_t length)
    5.93 +{
    5.94 +#if HAVE_SYS_MMAN_H
    5.95 +	return munmap(addr, length);
    5.96 +#else
    5.97 +	free(addr);
    5.98 +	return 0;
    5.99 +#endif
   5.100 +}
   5.101 +
   5.102  struct qsort_context {
   5.103  	size_t size;
   5.104  	razor_compare_with_data_func_t compare;