Debian Codegraph @

lib/fsaf.c

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 #include <assert.h>
20 #include <errno.h>
21 #include <stdbool.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include "align.h"
28 #include "fsaf.h"
29 #include "msg.h"
30
31 #define READAHEAD 1
32
33 FSAF * fsaf_fdopen(int fd, char const *fname)
34 {
35 FSAF * rv = malloc(sizeof *rv);
36 if (rv == 0) { errno = ENOMEM; return 0; }
37
38 rv->fname = strdup(fname);
39 if (rv->fname == NULL) { errno = ENOMEM; free(rv); return 0; }
40 rv->fd = fd;
41 rv->eof_mark = (size_t)(-1);
42 rv->buf_offset = 0;
43 rv->buf_size = 0;
44 rv->invalid_mark = 0;
45
46 struct stat stat;
47 int res = fstat(fd, &stat);
48 if (res == -1) {
49 free(rv);
50 return 0;
51 }
52
53 if (S_ISREG(stat.st_mode)) {
54 rv->eof_mark = stat.st_size;
55 }
56
57 rv->buf_capacity = 65536;
58 rv->buf = malloc(rv->buf_capacity);
59 if (rv->buf == 0) rv->buf_capacity = 0;
60
61 return rv;
62 }
63
64 void fsaf_close(FSAF * fp)
65 {
66 assert(fp != 0);
67
68 free(fp->fname);
69 free(fp->buf);
70 free(fp);
71 }
72
73
74 static void slide(FSAF *fp)
75 {
76 assert(fp->invalid_mark >= fp->buf_offset);
77 size_t delta = fp->invalid_mark - fp->buf_offset;
78 assert(fp->buf_capacity > delta);
79 if (delta == 0) return;
80 memmove(fp->buf, fp->buf + delta, fp->buf_size - delta);
81 fp->buf_offset += delta;
82 fp->buf_size -= delta;
83 }
84
85 void fsaf_slurp(FSAF * fp, size_t len)
86 {
87 assert(fp != 0);
88 assert(len > 0);
89 if (fp->invalid_mark - fp->buf_offset > fp->buf_size / 2) {
90 slide(fp);
91 }
92 if (fp->buf_size + len > fp->buf_capacity) {
93 size_t nc = fp->buf_capacity;
94 if (nc == 0) nc = 256;
95 while (nc < fp->buf_size + len) nc *= 2;
96 char * nb = realloc(fp->buf, nc);
97 if (nb != 0) {
98 fp->buf = nb;
99 fp->buf_capacity = nc;
100 } else {
101 slide(fp);
102 if (fp->buf_size > fp->buf_capacity) {
103 fatal_enomem(fp->fname);
104 }
105 if (fp->buf_size + len > fp->buf_capacity) {
106 len = fp->buf_capacity - fp->buf_size;
107 }
108 }
109 }
110 assert(len > 0);
111 assert(fp->buf_size + len <= fp->buf_capacity);
112
113 ssize_t res;
114 do {
115 // res = read(fp->fd, fp->buf + fp->buf_size, len);
116 res = read(fp->fd, fp->buf + fp->buf_size, fp->buf_capacity - fp->buf_size);
117 } while (res == -1 && errno == EINTR);
118 if (res == 0) {
119 fp->eof_mark = fp->buf_offset + fp->buf_size;
120 }
121 fp->buf_size += res;
122 }
123
124
125 void fsaf_invalidate(FSAF * fp, size_t offset)
126 {
127 assert(fp->eof_mark >= offset);
128 if (fp->invalid_mark >= offset) return;
129
130 fp->invalid_mark = offset;
131 }
132
133 #ifdef TESTMAIN
134 #include <stdio.h>
135 volatile char c;
136 int main(int argc, char * argv[])
137 {
138 if (argc == 1) {
139 set_loglevel(L_DEBUG);
140 FSAF * fp = fsaf_fdopen(STDIN_FILENO);
141 assert(fp != 0);
142 size_t i = 0;
143 while (i < fsaf_eof(fp)) {
144 struct fsaf_read_rv rr = fsaf_read(fp, i, 1);
145 //fwrite(rr.b, 1, rr.len, stdout);
146 c = rr.b[0];
147 i += rr.len;
148 }
149 fsaf_close(fp);
150 } else {
151 int ch;
152 while ((ch = getchar()) != EOF) {
153 c = ch;
154 }
155 }
156 return 0;
157 }
158 #endif