packio-simple

changeset 2:4767e7769c32 tip

packio-simple has more chances to ever get done
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 09 Aug 2015 05:14:29 +0300
parents eb07de55d0e6
children
files src/memmap.c src/memmap.h src/packio.c src/packio_impl.h
diffstat 4 files changed, 91 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/memmap.c	Sun Aug 09 05:14:29 2015 +0300
     1.3 @@ -0,0 +1,41 @@
     1.4 +#include <stdio.h>
     1.5 +#include <string.h>
     1.6 +#include <errno.h>
     1.7 +#include "memmap.h"
     1.8 +
     1.9 +#if defined(__unix__) || defined(__APPLE__)
    1.10 +#include <unistd.h>
    1.11 +#include <fcntl.h>
    1.12 +#include <sys/mman.h>
    1.13 +#include <sys/stat.h>
    1.14 +
    1.15 +int map_file(struct mmfile *mm, const char *fname)
    1.16 +{
    1.17 +	int fd;
    1.18 +	struct stat st;
    1.19 +
    1.20 +	if((fd = open(fname, O_RDONLY)) == -1) {
    1.21 +		fprintf(stderr, "map_file: failed to open %s: %s\n", fname, strerror(errno));
    1.22 +		return -1;
    1.23 +	}
    1.24 +	fstat(fd, &st);
    1.25 +
    1.26 +	if((mm->ptr = mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == (void*)-1) {
    1.27 +		close(fd);
    1.28 +		fprintf(stderr, "map_file: failed to map file %s: %s\n", fname, strerror(errno));
    1.29 +		return -1;
    1.30 +	}
    1.31 +	mm->size = st.st_size;
    1.32 +
    1.33 +	close(fd);
    1.34 +	return 0;
    1.35 +}
    1.36 +
    1.37 +void unmap_file(struct mmfile *mm)
    1.38 +{
    1.39 +	munmap(mm->ptr, mm->size);
    1.40 +}
    1.41 +
    1.42 +#else
    1.43 +/* implement on win32 */
    1.44 +#endif
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/memmap.h	Sun Aug 09 05:14:29 2015 +0300
     2.3 @@ -0,0 +1,12 @@
     2.4 +#ifndef MEMMAP_H_
     2.5 +#define MEMMAP_H_
     2.6 +
     2.7 +struct mmfile {
     2.8 +	void *ptr;
     2.9 +	unsigned long size;
    2.10 +};
    2.11 +
    2.12 +int map_file(struct mmfile *mm, const char *fname);
    2.13 +void unmap_file(struct mmfile *mm);
    2.14 +
    2.15 +#endif	/* MEMMAP_H_ */
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/packio.c	Sun Aug 09 05:14:29 2015 +0300
     3.3 @@ -0,0 +1,26 @@
     3.4 +#include <stdio.h>
     3.5 +#include <stdlib.h>
     3.6 +#include <string.h>
     3.7 +#include <errno.h>
     3.8 +#include "packio_impl.h"
     3.9 +
    3.10 +#define MAGIC	"PKIOPKG1"
    3.11 +#define MAXNAME	255
    3.12 +
    3.13 +struct header {
    3.14 +	char magic[8];
    3.15 +	int revision;
    3.16 +	int nfiles;
    3.17 +};
    3.18 +
    3.19 +int pack_load_packfile(struct packfile *pf, const char *fname)
    3.20 +{
    3.21 +	FILE *fp;
    3.22 +	struct header *hdr;
    3.23 +
    3.24 +	if(!(fp = fopen(fname, "rb"))) {
    3.25 +		fprintf(stderr, "failed to open file %s: %s\n", fname, strerror(errno));
    3.26 +		return -1;
    3.27 +	}
    3.28 +
    3.29 +}
     4.1 --- a/src/packio_impl.h	Sun Aug 09 03:15:07 2015 +0300
     4.2 +++ b/src/packio_impl.h	Sun Aug 09 05:14:29 2015 +0300
     4.3 @@ -6,6 +6,18 @@
     4.4  struct packfile {
     4.5  	int rev;	/* packfile revision */
     4.6  	struct rbtree *files;
     4.7 +
     4.8 +	unsigned char *maddr;
     4.9 +	unsigned long msize;
    4.10 +
    4.11 +	struct packfile *next;
    4.12 +};
    4.13 +
    4.14 +#define MAXNAME 255
    4.15 +
    4.16 +struct fileent {
    4.17 +	unsigned long offset, size;
    4.18 +	char name[MAXNAME + 1];
    4.19  };
    4.20  
    4.21  #endif	/* PACKIO_IMPL_H_ */