summaryrefslogtreecommitdiff
path: root/fsaf.h
blob: f95cf54b82c4462d18bdb86b3a1f234770e02897 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*  dctrl-tools - Debian control file inspection tools
    Copyright (C) 2003, 2005 Antti-Juhani Kaijanaho

    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
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef FSAF_H
#define FSAF_H

#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

/* FAST (MOSTLY) SEQUENTIAL-ACCESS FILE LAYER */

struct fsaf_private {
	char *fname;
	int fd;
	char * buf;
	size_t buf_capacity;
	size_t buf_offset;
	size_t buf_size;
	size_t invalid_mark;
	size_t eof_mark; /* can be (size_t)(-1) if not reached yet */
#ifdef _POSIX_MAPPED_FILES
	bool mapped;
	size_t topread; /* marks the top of what has been read ahead */
#endif
};

typedef struct fsaf_private FSAF;

/* True if FSAF should not use mmap even if it is possible. */
extern bool fsaf_mmap;

/* Open a FSAF for the given fd. Only read access is supported for
 * now.  The whole file is initially valid.  */
FSAF * fsaf_fdopen(int fd, char const *fname);

/* Close the given FSAF.  This DOES NOT close the underlying fd.  */
void fsaf_close(FSAF *);

/* Return a pointer to a buffer containing len bytes from the FSAF
 * starting at offset.  Note that if offset is below the invalid mark,
 * we return b = NULL.  This is a CHEAP operation, if the full range
 * has already been accessed.  The buffer may be invalidated by any
 * subsequent call to this function. */
struct fsaf_read_rv {
	char const * b;
	size_t len;
} fsaf_read(FSAF *, size_t offset, size_t len);

/* Behaves like fsaf_read except that the result is put in a malloc'd
 * zero-terminated buffer.  NULL return value indicates either memory
 * allocation failure or that the read was below the invalid mark.  */
static inline
char * fsaf_getas(FSAF * fp, size_t offset, size_t len)
{
	struct fsaf_read_rv r = fsaf_read(fp, offset, len);
	if (r.b == 0) return 0;
	char * rv = malloc(r.len+1);
	if (rv == 0) return 0;
	memcpy(rv, r.b, r.len);
	rv[r.len] = 0;
	return rv;
}

static inline
int fsaf_getc(FSAF * fp, size_t offset)
{
	if (offset >= fp->eof_mark) return -1;
	struct fsaf_read_rv r = fsaf_read(fp, offset, 1);
	if (offset >= fp->eof_mark) return -1;
	assert(r.len == 1);
	return (unsigned char)r.b[0];
}


/* Invalidate all bytes in FSAF up to and excluding offset.  */
void fsaf_invalidate(FSAF *, size_t offset);

/* Return an upper bound for the end of file, either the smallest
 * offset beyond eof or (size_t)(-1). */
static inline
size_t fsaf_eof(FSAF * fp) { return fp->eof_mark; }


#endif /* FSAF_H */