Debian Codegraph @

lib/fsaf.h

raw
1 /* dctrl-tools - Debian control file inspection tools
2 Copyright © 2003, 2005, 2012 Antti-Juhani Kaijanaho
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #ifndef FSAF_H
20 #define FSAF_H
21
22 #include <assert.h>
23 #include <stdbool.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 /* FAST (MOSTLY) SEQUENTIAL-ACCESS FILE LAYER */
28
29 struct fsaf_private {
30 char *fname;
31 int fd;
32 char * buf;
33 size_t buf_capacity;
34 size_t buf_offset;
35 size_t buf_size;
36 size_t invalid_mark;
37 size_t eof_mark; /* can be (size_t)(-1) if not reached yet */
38 };
39
40 typedef struct fsaf_private FSAF;
41
42 /* Open a FSAF for the given fd. Only read access is supported for
43 * now. The whole file is initially valid. */
44 FSAF * fsaf_fdopen(int fd, char const *fname);
45
46 /* Close the given FSAF. This DOES NOT close the underlying fd. */
47 void fsaf_close(FSAF *);
48
49 /* Return a pointer to a buffer containing len bytes from the FSAF
50 * starting at offset. Note that if offset is below the invalid mark,
51 * we return b = NULL. This is a CHEAP operation, if the full range
52 * has already been accessed. The buffer may be invalidated by any
53 * subsequent call to this function. */
54 struct fsaf_read_rv {
55 char const * b;
56 size_t len;
57 };
58 static inline
59 struct fsaf_read_rv fsaf_read(FSAF * fp, size_t offset, size_t len)
60 {
61 void fsaf_slurp(FSAF * fp, size_t len);
62
63 /* Reading nothing - since offset can be bogus in this
64 * situation, this could foul up our assumptions later, so
65 * return already here. */
66 if (len == 0) {
67 return (struct fsaf_read_rv){ .b = "", .len = 0 };
68 }
69
70 /* Make sure we don't read past the EOF mark. */
71 if (offset + len > fp->eof_mark) len = fp->eof_mark - offset;
72
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);
79 }
80 }
81
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),
86 .len = len
87 };
88 }
89
90 /* Behaves like fsaf_read except that the result is put in a malloc'd
91 * zero-terminated buffer. NULL return value indicates either memory
92 * allocation failure or that the read was below the invalid mark. */
93 static inline
94 char * fsaf_getas(FSAF * fp, size_t offset, size_t len)
95 {
96 struct fsaf_read_rv r = fsaf_read(fp, offset, len);
97 if (r.b == 0) return 0;
98 char * rv = malloc(r.len+1);
99 if (rv == 0) return 0;
100 memcpy(rv, r.b, r.len);
101 rv[r.len] = 0;
102 return rv;
103 }
104
105 static inline
106 int fsaf_getc(FSAF * fp, size_t offset)
107 {
108 if (offset >= fp->eof_mark) return -1;
109 struct fsaf_read_rv r = fsaf_read(fp, offset, 1);
110 if (offset >= fp->eof_mark) return -1;
111 assert(r.len == 1);
112 return (unsigned char)r.b[0];
113 }
114
115
116 /* Invalidate all bytes in FSAF up to and excluding offset. */
117 void fsaf_invalidate(FSAF *, size_t offset);
118
119 /* Return an upper bound for the end of file, either the smallest
120 * offset beyond eof or (size_t)(-1). */
121 static inline
122 size_t fsaf_eof(FSAF * fp) { return fp->eof_mark; }
123
124
125 #endif /* FSAF_H */