2 * Sherlock Library -- Fast File Buffering
4 * (c) 1997--1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
17 struct fastbuf *__bfdopen(int fd, uns buffer, byte *name)
19 struct fastbuf *b = xmalloc(sizeof(struct fastbuf));
22 b->buffer = xmalloc(buffer);
23 b->bptr = b->bstop = b->buffer;
24 b->bufend = b->buffer + buffer;
25 b->name = stralloc(name);
26 b->pos = b->fdpos = 0;
32 bopen(byte *name, uns mode, uns buffer)
36 mode |= SHERLOCK_O_LARGEFILE;
37 fd = open(name, mode, 0666);
40 die("Unable to %s file %s: %m",
41 (mode & O_CREAT) ? "create" : "open", name);
42 return __bfdopen(fd, buffer, name);
46 bfdopen(int fd, uns buffer)
50 sprintf(x, "fd%d", fd);
51 return __bfdopen(fd, buffer, x);
54 void bclose(struct fastbuf *f)
64 rdbuf(struct fastbuf *f)
66 int l = read(f->fd, f->buffer, f->buflen);
69 die("Error reading %s: %m", f->name);
71 f->bstop = f->buffer + l;
78 wrbuf(struct fastbuf *f)
80 int l = f->bptr - f->buffer;
85 int z = write(f->fd, c, l);
87 die("Error writing %s: %m", f->name);
90 log(L_ERROR "wrbuf: %d != %d (pos %Ld)", z, l, sh_seek(f->fd, 0, SEEK_CUR));
99 void bflush(struct fastbuf *f)
101 if (f->bptr != f->buffer)
102 { /* Have something to flush */
103 if (f->bstop > f->buffer) /* Read data? */
105 f->bptr = f->bstop = f->buffer;
108 else /* Write data... */
113 inline void bsetpos(struct fastbuf *f, sh_off_t pos)
115 if (pos >= f->pos && (pos <= f->pos + (f->bptr - f->buffer) || pos <= f->pos + (f->bstop - f->buffer)))
116 f->bptr = f->buffer + (pos - f->pos);
120 if (f->fdpos != pos && sh_seek(f->fd, pos, SEEK_SET) < 0)
121 die("lseek on %s: %m", f->name);
122 f->fdpos = f->pos = pos;
126 void bseek(struct fastbuf *f, sh_off_t pos, int whence)
133 return bsetpos(f, pos);
135 return bsetpos(f, btell(f) + pos);
138 l = sh_seek(f->fd, pos, whence);
140 die("lseek on %s: %m", f->name);
141 f->fdpos = f->pos = l;
144 die("bseek: invalid whence=%d", whence);
148 int bgetc_slow(struct fastbuf *f)
150 if (f->bptr < f->bstop)
157 int bpeekc_slow(struct fastbuf *f)
159 if (f->bptr < f->bstop)
166 void bputc_slow(struct fastbuf *f, byte c)
168 if (f->bptr >= f->bufend)
173 word bgetw_slow(struct fastbuf *f)
175 word w = bgetc_slow(f);
176 #ifdef CPU_BIG_ENDIAN
177 return (w << 8) | bgetc_slow(f);
179 return w | (bgetc_slow(f) << 8);
183 u32 bgetl_slow(struct fastbuf *f)
185 u32 l = bgetc_slow(f);
186 #ifdef CPU_BIG_ENDIAN
187 l = (l << 8) | bgetc_slow(f);
188 l = (l << 8) | bgetc_slow(f);
189 return (l << 8) | bgetc_slow(f);
191 l = (bgetc_slow(f) << 8) | l;
192 l = (bgetc_slow(f) << 16) | l;
193 return (bgetc_slow(f) << 24) | l;
197 u64 bgetq_slow(struct fastbuf *f)
200 #ifdef CPU_BIG_ENDIAN
207 return ((u64) h << 32) | l;
210 u64 bget5_slow(struct fastbuf *f)
213 #ifdef CPU_BIG_ENDIAN
220 return ((u64) h << 32) | l;
223 void bputw_slow(struct fastbuf *f, word w)
225 #ifdef CPU_BIG_ENDIAN
226 bputc_slow(f, w >> 8);
230 bputc_slow(f, w >> 8);
234 void bputl_slow(struct fastbuf *f, u32 l)
236 #ifdef CPU_BIG_ENDIAN
237 bputc_slow(f, l >> 24);
238 bputc_slow(f, l >> 16);
239 bputc_slow(f, l >> 8);
243 bputc_slow(f, l >> 8);
244 bputc_slow(f, l >> 16);
245 bputc_slow(f, l >> 24);
249 void bputq_slow(struct fastbuf *f, u64 q)
251 #ifdef CPU_BIG_ENDIAN
252 bputl_slow(f, q >> 32);
256 bputl_slow(f, q >> 32);
260 void bput5_slow(struct fastbuf *f, u64 o)
264 #ifdef CPU_BIG_ENDIAN
273 void bread_slow(struct fastbuf *f, void *b, uns l)
277 uns k = f->bstop - f->bptr;
282 k = f->bstop - f->bptr;
284 die("bread on %s: file exhausted", f->name);
288 memcpy(b, f->bptr, k);
295 void bwrite_slow(struct fastbuf *f, void *b, uns l)
299 uns k = f->bufend - f->bptr;
304 k = f->bufend - f->bptr;
308 memcpy(f->bptr, b, k);
315 byte * /* Non-standard */
316 bgets(struct fastbuf *f, byte *b, uns l)
326 if (k == '\n' || k == EOF)
334 die("%s: Line too long", f->name);
337 void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
339 uns rf = f->bstop - f->bptr;
345 uns k = (rf <= l) ? rf : l;
346 bwrite(t, f->bptr, k);
350 while (l >= t->buflen)
353 if ((uns) read(f->fd, t->buffer, t->buflen) != t->buflen)
354 die("bbcopy: %s exhausted", f->name);
356 f->fdpos += t->buflen;
357 f->bstop = f->bptr = f->buffer;
363 uns k = t->bufend - t->bptr;
368 k = t->bufend - t->bptr;
372 bread(f, t->bptr, k);
380 int main(int argc, char **argv)
382 struct fastbuf *f, *t;
385 f = bopen("/etc/profile", O_RDONLY, 16);