]> mj.ucw.cz Git - libucw.git/blobdiff - lib/fastbuf.c
Split page key to pos and fd. Wastes extra 4 bytes, but simplifies the code a lot.
[libucw.git] / lib / fastbuf.c
index 4c0bf6db01530482dc40f2b4ff5e91e929ebfc31..6bbadb40cf297d66e3db3e423aeea97788c0775f 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *     Sherlock Library -- Fast File Buffering
  *
- *     (c) 1997 Martin Mares, <mj@atrey.karlin.mff.cuni.cz>
+ *     (c) 1997--1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
  */
 
 #include <stdio.h>
@@ -12,6 +12,7 @@
 
 #include "lib.h"
 #include "fastbuf.h"
+#include "lfs.h"
 
 struct fastbuf *__bfdopen(int fd, uns buffer, byte *name)
 {
@@ -30,7 +31,10 @@ struct fastbuf *__bfdopen(int fd, uns buffer, byte *name)
 struct fastbuf *
 bopen(byte *name, uns mode, uns buffer)
 {
-  int fd = open(name, mode, 0666);
+  int fd;
+
+  mode |= SHERLOCK_O_LARGEFILE;
+  fd = open(name, mode, 0666);
 
   if (fd < 0)
     die("Unable to %s file %s: %m",
@@ -74,15 +78,22 @@ static void
 wrbuf(struct fastbuf *f)
 {
   int l = f->bptr - f->buffer;
+  char *c = f->buffer;
 
-  if (l)
+  while (l)
     {
-      if (write(f->fd, f->buffer, l) != l)
-       die("Error writing %s: %m");
-      f->bptr = f->buffer;
-      f->fdpos += l;
-      f->pos = f->fdpos;
+      int z = write(f->fd, c, l);
+      if (z <= 0)
+       die("Error writing %s: %m", f->name);
+      /* FIXME */
+      if (z != l)
+       log(L_ERROR "wrbuf: %d != %d (pos %Ld)", z, l, sh_seek(f->fd, 0, SEEK_CUR));
+      f->fdpos += z;
+      l -= z;
+      c += z;
     }
+  f->bptr = f->buffer;
+  f->pos = f->fdpos;
 }
 
 void bflush(struct fastbuf *f)
@@ -99,22 +110,22 @@ void bflush(struct fastbuf *f)
     }
 }
 
-inline void bsetpos(struct fastbuf *f, uns pos)
+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);
   else
     {
       bflush(f);
-      if (f->fdpos != pos && lseek(f->fd, pos, SEEK_SET) < 0)
+      if (f->fdpos != pos && sh_seek(f->fd, pos, SEEK_SET) < 0)
        die("lseek on %s: %m", f->name);
       f->fdpos = f->pos = pos;
     }
 }
 
-void bseek(struct fastbuf *f, uns pos, int whence)
+void bseek(struct fastbuf *f, sh_off_t pos, int whence)
 {
-  int l;
+  sh_off_t l;
 
   switch (whence)
     {
@@ -124,7 +135,7 @@ void bseek(struct fastbuf *f, uns pos, int whence)
       return bsetpos(f, btell(f) + pos);
     case SEEK_END:
       bflush(f);
-      l = lseek(f->fd, pos, whence);
+      l = sh_seek(f->fd, pos, whence);
       if (l < 0)
        die("lseek on %s: %m", f->name);
       f->fdpos = f->pos = l;
@@ -169,9 +180,9 @@ word bgetw_slow(struct fastbuf *f)
 #endif
 }
 
-ulg bgetl_slow(struct fastbuf *f)
+u32 bgetl_slow(struct fastbuf *f)
 {
-  ulg l = bgetc_slow(f);
+  u32 l = bgetc_slow(f);
 #ifdef CPU_BIG_ENDIAN
   l = (l << 8) | bgetc_slow(f);
   l = (l << 8) | bgetc_slow(f);
@@ -183,6 +194,32 @@ ulg bgetl_slow(struct fastbuf *f)
 #endif
 }
 
+u64 bgetq_slow(struct fastbuf *f)
+{
+  u32 l, h;
+#ifdef CPU_BIG_ENDIAN
+  h = bgetl_slow(f);
+  l = bgetl_slow(f);
+#else
+  l = bgetl_slow(f);
+  h = bgetl_slow(f);
+#endif
+  return ((u64) h << 32) | l;
+}
+
+u64 bget5_slow(struct fastbuf *f)
+{
+  u32 l, h;
+#ifdef CPU_BIG_ENDIAN
+  h = bgetc_slow(f);
+  l = bgetl_slow(f);
+#else
+  l = bgetl_slow(f);
+  h = bgetc_slow(f);
+#endif
+  return ((u64) h << 32) | l;
+}
+
 void bputw_slow(struct fastbuf *f, word w)
 {
 #ifdef CPU_BIG_ENDIAN
@@ -194,7 +231,7 @@ void bputw_slow(struct fastbuf *f, word w)
 #endif
 }
 
-void bputl_slow(struct fastbuf *f, ulg l)
+void bputl_slow(struct fastbuf *f, u32 l)
 {
 #ifdef CPU_BIG_ENDIAN
   bputc_slow(f, l >> 24);
@@ -209,6 +246,30 @@ void bputl_slow(struct fastbuf *f, ulg l)
 #endif
 }
 
+void bputq_slow(struct fastbuf *f, u64 q)
+{
+#ifdef CPU_BIG_ENDIAN
+  bputl_slow(f, q >> 32);
+  bputl_slow(f, q);
+#else
+  bputl_slow(f, q);
+  bputl_slow(f, q >> 32);
+#endif
+}
+
+void bput5_slow(struct fastbuf *f, u64 o)
+{
+  u32 hi = o >> 32;
+  u32 low = o;
+#ifdef CPU_BIG_ENDIAN
+  bputc_slow(f, hi);
+  bputl_slow(f, low);
+#else
+  bputl_slow(f, low);
+  bputc_slow(f, hi);
+#endif
+}
+
 void bread_slow(struct fastbuf *f, void *b, uns l)
 {
   while (l)
@@ -276,28 +337,24 @@ bgets(struct fastbuf *f, byte *b, uns l)
 void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
 {
   uns rf = f->bstop - f->bptr;
-  uns rt = t->bufend - t->bptr;
 
   if (!l)
     return;
-  if (rf && rt)
+  if (rf)
     {
-      uns k = l;
-      if (k > rf)
-       k = rf;
-      if (k > rt)
-       k = rt;
-      memcpy(t->bptr, f->bptr, k);
-      t->bptr += k;
+      uns k = (rf <= l) ? rf : l;
+      bwrite(t, f->bptr, k);
       f->bptr += k;
       l -= k;
     }
   while (l >= t->buflen)
     {
       wrbuf(t);
-      if (read(f->fd, t->buffer, t->buflen) != t->buflen)
+      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;
     }