1
1
/* dctrl-tools - Debian control file inspection tools
2
Copyright © 2003, 2005 Antti-Juhani Kaijanaho
2
Copyright © 2003, 2005, 2012 Antti-Juhani Kaijanaho
4
4
This program is free software; you can redistribute it and/or modify
5
5
it under the terms of the GNU General Public License as published by
36
36
size_t invalid_mark;
37
37
size_t eof_mark; /* can be (size_t)(-1) if not reached yet */
38
#ifdef _POSIX_MAPPED_FILES
40
size_t topread; /* marks the top of what has been read ahead */
44
40
typedef struct fsaf_private FSAF;
46
/* True if FSAF should not use mmap even if it is possible. */
47
extern bool fsaf_mmap;
49
42
/* Open a FSAF for the given fd. Only read access is supported for
50
43
* now. The whole file is initially valid. */
51
44
FSAF * fsaf_fdopen(int fd, char const *fname);
61
54
struct fsaf_read_rv {
64
} fsaf_read(FSAF *, size_t offset, size_t len);
59
struct fsaf_read_rv fsaf_read(FSAF * fp, size_t offset, size_t len)
61
void fsaf_slurp(FSAF * fp, size_t len);
63
/* Reading nothing - since offset can be bogus in this
64
* situation, this could foul up our assumptions later, so
65
* return already here. */
67
return (struct fsaf_read_rv){ .b = "", .len = 0 };
70
/* Make sure we don't read past the EOF mark. */
71
if (offset + len > fp->eof_mark) len = fp->eof_mark - offset;
73
/* Ensure that we have enough data in the buffer. */
74
assert(offset >= fp->buf_offset);
75
if (offset - fp->buf_offset + len > fp->buf_size) {
76
fsaf_slurp(fp, offset - fp->buf_offset + len - fp->buf_size);
77
if (offset - fp->buf_offset + len > fp->buf_size) {
78
len = fp->buf_size - (offset - fp->buf_offset);
82
assert(offset - fp->buf_offset + len <= fp->buf_size);
83
assert(offset + len <= fp->eof_mark);
84
return (struct fsaf_read_rv){
85
.b = fp->buf + (offset - fp->buf_offset),
66
90
/* Behaves like fsaf_read except that the result is put in a malloc'd
67
91
* zero-terminated buffer. NULL return value indicates either memory