/*
- * Sherlock Library -- Fast File Buffering
+ * UCW Library -- Fast Buffered I/O
*
- * (c) 1997--1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
+ * (c) 1997--2004 Martin Mares <mj@ucw.cz>
+ *
+ * This software may be freely distributed and used according to the terms
+ * of the GNU Lesser General Public License.
*/
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <fcntl.h>
-#include <unistd.h>
-
-#include "lib.h"
-#include "fastbuf.h"
-#include "lfs.h"
-
-struct fastbuf *__bfdopen(int fd, uns buffer, byte *name)
-{
- struct fastbuf *b = xmalloc(sizeof(struct fastbuf));
-
- b->buflen = buffer;
- b->buffer = xmalloc(buffer);
- b->bptr = b->bstop = b->buffer;
- b->bufend = b->buffer + buffer;
- b->name = stralloc(name);
- b->pos = b->fdpos = 0;
- b->fd = fd;
- return b;
-}
-
-struct fastbuf *
-bopen(byte *name, uns mode, uns buffer)
-{
- int fd;
-
-#ifdef SHERLOCK_CONFIG_LFS
- mode |= O_LARGEFILE;
-#endif
- fd = open(name, mode, 0666);
+#include "lib/lib.h"
+#include "lib/fastbuf.h"
- if (fd < 0)
- die("Unable to %s file %s: %m",
- (mode & O_CREAT) ? "create" : "open", name);
- return __bfdopen(fd, buffer, name);
-}
-
-struct fastbuf *
-bfdopen(int fd, uns buffer)
-{
- byte x[32];
-
- sprintf(x, "fd%d", fd);
- return __bfdopen(fd, buffer, x);
-}
+#include <stdlib.h>
void bclose(struct fastbuf *f)
{
- bflush(f);
- close(f->fd);
- free(f->name);
- free(f->buffer);
- free(f);
-}
-
-static int
-rdbuf(struct fastbuf *f)
-{
- int l = read(f->fd, f->buffer, f->buflen);
-
- if (l < 0)
- die("Error reading %s: %m", f->name);
- f->bptr = f->buffer;
- f->bstop = f->buffer + l;
- f->pos = f->fdpos;
- f->fdpos += l;
- return l;
-}
-
-static void
-wrbuf(struct fastbuf *f)
-{
- int l = f->bptr - f->buffer;
-
- if (l)
+ if (f)
{
- if (write(f->fd, f->buffer, l) != l)
- die("Error writing %s: %m", f->name);
- f->bptr = f->buffer;
- f->fdpos += l;
- f->pos = f->fdpos;
+ bflush(f);
+ if (f->close)
+ f->close(f);
}
}
void bflush(struct fastbuf *f)
{
- if (f->bptr != f->buffer)
- { /* Have something to flush */
- if (f->bstop > f->buffer) /* Read data? */
- {
- f->bptr = f->bstop = f->buffer;
- f->pos = f->fdpos;
- }
- else /* Write data... */
- wrbuf(f);
- }
+ if (f->bptr > f->bstop)
+ f->spout(f);
+ else if (f->bstop > f->buffer)
+ f->bptr = f->bstop = f->buffer;
}
inline void bsetpos(struct fastbuf *f, sh_off_t pos)
{
- if (pos >= f->pos && (pos <= f->pos + (f->bptr - f->buffer) || pos <= f->pos + (f->bstop - f->buffer)))
- f->bptr = f->buffer + (pos - f->pos);
+ /* We can optimize seeks only when reading */
+ if (pos >= f->pos - (f->bstop - f->buffer) && pos <= f->pos)
+ f->bptr = f->bstop + (pos - f->pos);
else
{
bflush(f);
- if (f->fdpos != pos && sh_seek(f->fd, pos, SEEK_SET) < 0)
- die("lseek on %s: %m", f->name);
- f->fdpos = f->pos = pos;
+ f->seek(f, pos, SEEK_SET);
}
}
void bseek(struct fastbuf *f, sh_off_t pos, int whence)
{
- sh_off_t l;
-
switch (whence)
{
case SEEK_SET:
return bsetpos(f, btell(f) + pos);
case SEEK_END:
bflush(f);
- l = sh_seek(f->fd, pos, whence);
- if (l < 0)
- die("lseek on %s: %m", f->name);
- f->fdpos = f->pos = l;
+ f->seek(f, pos, SEEK_END);
break;
default:
die("bseek: invalid whence=%d", whence);
{
if (f->bptr < f->bstop)
return *f->bptr++;
- if (!rdbuf(f))
+ if (!f->refill(f))
return EOF;
return *f->bptr++;
}
{
if (f->bptr < f->bstop)
return *f->bptr;
- if (!rdbuf(f))
+ if (!f->refill(f))
return EOF;
return *f->bptr;
}
-void bputc_slow(struct fastbuf *f, byte c)
+void bputc_slow(struct fastbuf *f, uns c)
{
if (f->bptr >= f->bufend)
- wrbuf(f);
+ f->spout(f);
*f->bptr++ = c;
}
-word bgetw_slow(struct fastbuf *f)
+int bgetw_slow(struct fastbuf *f)
{
- word w = bgetc_slow(f);
+ int w1, w2;
+ w1 = bgetc_slow(f);
+ if (w1 < 0)
+ return w1;
+ w2 = bgetc_slow(f);
+ if (w2 < 0)
+ return w2;
#ifdef CPU_BIG_ENDIAN
- return (w << 8) | bgetc_slow(f);
+ return (w1 << 8) | w2;
#else
- return w | (bgetc_slow(f) << 8);
+ return w1 | (w2 << 8);
#endif
}
return ((u64) h << 32) | l;
}
-void bputw_slow(struct fastbuf *f, word w)
+void bputw_slow(struct fastbuf *f, uns w)
{
#ifdef CPU_BIG_ENDIAN
bputc_slow(f, w >> 8);
#endif
}
-void bread_slow(struct fastbuf *f, void *b, uns l)
+uns bread_slow(struct fastbuf *f, void *b, uns l, uns check)
{
+ uns total = 0;
while (l)
{
uns k = f->bstop - f->bptr;
if (!k)
{
- rdbuf(f);
+ f->refill(f);
k = f->bstop - f->bptr;
if (!k)
- die("bread on %s: file exhausted", f->name);
+ break;
}
if (k > l)
k = l;
f->bptr += k;
b = (byte *)b + k;
l -= k;
+ total += k;
}
+ if (check && total && l)
+ die("breadb: short read");
+ return total;
}
void bwrite_slow(struct fastbuf *f, void *b, uns l)
if (!k)
{
- wrbuf(f);
+ f->spout(f);
k = f->bufend - f->bptr;
}
if (k > l)
int k;
k = bgetc(f);
- if (k == EOF)
+ if (k < 0)
return NULL;
while (b < e)
{
- if (k == '\n' || k == EOF)
+ if (k == '\n' || k < 0)
{
*b = 0;
return b;
die("%s: Line too long", f->name);
}
-void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
+int
+bgets_nodie(struct fastbuf *f, byte *b, uns l)
{
- uns rf = f->bstop - f->bptr;
+ byte *start = b;
+ byte *e = b + l - 1;
+ int k;
- if (!l)
- return;
- if (rf)
+ k = bgetc(f);
+ if (k < 0)
+ return 0;
+ while (b < e)
{
- uns k = (rf <= l) ? rf : l;
- bwrite(t, f->bptr, k);
- f->bptr += k;
- l -= k;
+ if (k == '\n' || k < 0)
+ {
+ *b++ = 0;
+ return b - start;
+ }
+ *b++ = k;
+ k = bgetc(f);
}
- while (l >= t->buflen)
+ return -1;
+}
+
+byte *
+bgets0(struct fastbuf *f, byte *b, uns l)
+{
+ byte *e = b + l - 1;
+ int k;
+
+ k = bgetc(f);
+ if (k < 0)
+ return NULL;
+ while (b < e)
{
- wrbuf(t);
- if ((uns) read(f->fd, t->buffer, t->buflen) != t->buflen)
- die("bbcopy: %s exhausted", f->name);
- f->pos = f->fdpos;
- f->fdpos += t->buflen;
- f->bstop = f->bptr = f->buffer;
- t->bptr = t->bufend;
- l -= t->buflen;
+ if (k <= 0)
+ {
+ *b = 0;
+ return b;
+ }
+ *b++ = k;
+ k = bgetc(f);
}
+ die("%s: Line too long", f->name);
+}
+
+void
+bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l)
+{
while (l)
{
- uns k = t->bufend - t->bptr;
+ byte *fptr, *tptr;
+ uns favail, tavail, n;
- if (!k)
+ favail = bdirect_read_prepare(f, &fptr);
+ if (!favail)
{
- wrbuf(t);
- k = t->bufend - t->bptr;
+ if (l == ~0U)
+ return;
+ die("bbcopy: source exhausted");
}
- if (k > l)
- k = l;
- bread(f, t->bptr, k);
- t->bptr += k;
- l -= k;
+ tavail = bdirect_write_prepare(t, &tptr);
+ n = MIN(l, favail);
+ n = MIN(n, tavail);
+ memcpy(tptr, fptr, n);
+ bdirect_read_commit(f, fptr + n);
+ bdirect_write_commit(t, tptr + n);
+ if (l != ~0U)
+ l -= n;
}
}
-#ifdef TEST
+int
+bconfig(struct fastbuf *f, uns item, int value)
+{
+ return f->config ? f->config(f, item, value) : -1;
+}
+
+void
+brewind(struct fastbuf *f)
+{
+ bflush(f);
+ bsetpos(f, 0);
+}
-int main(int argc, char **argv)
+int
+bskip_slow(struct fastbuf *f, uns len)
{
- struct fastbuf *f, *t;
- int c;
-
- f = bopen("/etc/profile", O_RDONLY, 16);
- t = bfdopen(1, 13);
- bbcopy(f, t, 100);
- bclose(f);
- bclose(t);
+ while (len)
+ {
+ byte *buf;
+ uns l = bdirect_read_prepare(f, &buf);
+ if (!l)
+ return 0;
+ l = MIN(l, len);
+ bdirect_read_commit(f, buf+l);
+ len -= l;
+ }
+ return 1;
}
-#endif
+sh_off_t
+bfilesize(struct fastbuf *f)
+{
+ if (!f)
+ return 0;
+ sh_off_t pos = btell(f);
+ bseek(f, 0, SEEK_END);
+ sh_off_t len = btell(f);
+ bsetpos(f, pos);
+ return len;
+}