]> mj.ucw.cz Git - libucw.git/blobdiff - lib/fb-file.c
Some Perl modules have moved to UCW namespace.
[libucw.git] / lib / fb-file.c
index 5e1a6981eea4a66ad31644d61bc28911ef059e2c..04f2f4cf444442141fcfb3b4e74d5e852a9f1a99 100644 (file)
@@ -1,7 +1,7 @@
 /*
 /*
- *     Sherlock Library -- Fast Buffered I/O on Files
+ *     UCW Library -- Fast Buffered I/O on Files
  *
  *
- *     (c) 1997--2002 Martin Mares <mj@ucw.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.
  *
  *     This software may be freely distributed and used according to the terms
  *     of the GNU Lesser General Public License.
 #include "lib/fastbuf.h"
 #include "lib/lfs.h"
 
 #include "lib/fastbuf.h"
 #include "lib/lfs.h"
 
-#include <stdlib.h>
+#include <stdio.h>
 #include <string.h>
 #include <fcntl.h>
 #include <unistd.h>
 
 struct fb_file {
   struct fastbuf fb;
 #include <string.h>
 #include <fcntl.h>
 #include <unistd.h>
 
 struct fb_file {
   struct fastbuf fb;
-  int fd;                              /* File descriptor, -1 if not a real file */
+  int fd;                              /* File descriptor */
   int is_temp_file;                    /* 0=normal file, 1=temporary file, delete on close, -1=shared FD */
 };
 #define FB_FILE(f) ((struct fb_file *)(f)->is_fastbuf)
   int is_temp_file;                    /* 0=normal file, 1=temporary file, delete on close, -1=shared FD */
 };
 #define FB_FILE(f) ((struct fb_file *)(f)->is_fastbuf)
+#define FB_BUFFER(f) (byte *)(FB_FILE(f) + 1)
 
 static int
 bfd_refill(struct fastbuf *f)
 {
 
 static int
 bfd_refill(struct fastbuf *f)
 {
+  f->bptr = f->buffer = FB_BUFFER(f);
   int l = read(FB_FILE(f)->fd, f->buffer, f->bufend-f->buffer);
   if (l < 0)
     die("Error reading %s: %m", f->name);
   int l = read(FB_FILE(f)->fd, f->buffer, f->bufend-f->buffer);
   if (l < 0)
     die("Error reading %s: %m", f->name);
-  f->bptr = f->buffer;
   f->bstop = f->buffer + l;
   f->pos += l;
   return l;
   f->bstop = f->buffer + l;
   f->pos += l;
   return l;
@@ -39,7 +40,7 @@ static void
 bfd_spout(struct fastbuf *f)
 {
   int l = f->bptr - f->buffer;
 bfd_spout(struct fastbuf *f)
 {
   int l = f->bptr - f->buffer;
-  char *c = f->buffer;
+  byte *c = f->buffer;
 
   f->pos += l;
   while (l)
 
   f->pos += l;
   while (l)
@@ -50,21 +51,17 @@ bfd_spout(struct fastbuf *f)
       l -= z;
       c += z;
     }
       l -= z;
       c += z;
     }
-  f->bptr = f->buffer;
+  f->bptr = f->buffer = FB_BUFFER(f);
 }
 
 }
 
-static void
+static int
 bfd_seek(struct fastbuf *f, sh_off_t pos, int whence)
 {
 bfd_seek(struct fastbuf *f, sh_off_t pos, int whence)
 {
-  sh_off_t l;
-
-  if (whence == SEEK_SET && pos == f->pos)
-    return;
-
-  l = sh_seek(FB_FILE(f)->fd, pos, whence);
+  sh_off_t l = sh_seek(FB_FILE(f)->fd, pos, whence);
   if (l < 0)
   if (l < 0)
-    die("lseek on %s: %m", f->name);
+    return 0;
   f->pos = l;
   f->pos = l;
+  return 1;
 }
 
 static void
 }
 
 static void
@@ -74,7 +71,7 @@ bfd_close(struct fastbuf *f)
     {
     case 1:
       if (unlink(f->name) < 0)
     {
     case 1:
       if (unlink(f->name) < 0)
-       log(L_ERROR, "unlink(%s): %m", f->name);
+       msg(L_ERROR, "unlink(%s): %m", f->name);
     case 0:
       close(FB_FILE(f)->fd);
     }
     case 0:
       close(FB_FILE(f)->fd);
     }
@@ -95,14 +92,14 @@ bfd_config(struct fastbuf *f, uns item, int value)
 }
 
 static struct fastbuf *
 }
 
 static struct fastbuf *
-bfdopen_internal(int fd, uns buflen, byte *name)
+bfdopen_internal(int fd, uns buflen, const char *name)
 {
   int namelen = strlen(name) + 1;
   struct fb_file *F = xmalloc(sizeof(struct fb_file) + buflen + namelen);
   struct fastbuf *f = &F->fb;
 
   bzero(F, sizeof(*F));
 {
   int namelen = strlen(name) + 1;
   struct fb_file *F = xmalloc(sizeof(struct fb_file) + buflen + namelen);
   struct fastbuf *f = &F->fb;
 
   bzero(F, sizeof(*F));
-  f->buffer = (char *)(F+1);
+  f->buffer = (byte *)(F+1);
   f->bptr = f->bstop = f->buffer;
   f->bufend = f->buffer + buflen;
   f->name = f->bufend;
   f->bptr = f->bstop = f->buffer;
   f->bufend = f->buffer + buflen;
   f->name = f->bufend;
@@ -113,54 +110,62 @@ bfdopen_internal(int fd, uns buflen, byte *name)
   f->seek = bfd_seek;
   f->close = bfd_close;
   f->config = bfd_config;
   f->seek = bfd_seek;
   f->close = bfd_close;
   f->config = bfd_config;
+  f->can_overwrite_buffer = 2;
   return f;
 }
 
 struct fastbuf *
   return f;
 }
 
 struct fastbuf *
-bopen(byte *name, uns mode, uns buffer)
+bopen_try(const char *name, uns mode, uns buflen)
 {
 {
-  struct fastbuf *b;
-
-#if 0 /* FIXME: Dirty hack for testing fb-mmap */
-  if (buffer >= 65536 && (mode == O_RDONLY))
-    {
-      log(L_DEBUG, "bopen_mm hack: %s", name);
-      if (mode & O_WRONLY)
-       mode = (mode & ~O_WRONLY) | O_RDWR;
-      return bopen_mm(name, mode);
-    }
-#endif
-
   int fd = sh_open(name, mode, 0666);
   if (fd < 0)
   int fd = sh_open(name, mode, 0666);
   if (fd < 0)
-    die("Unable to %s file %s: %m",
-       (mode & O_CREAT) ? "create" : "open", name);
-  b = bfdopen_internal(fd, buffer, name);
+    return NULL;
+  struct fastbuf *b = bfdopen_internal(fd, buflen, name);
   if (mode & O_APPEND)
     bfd_seek(b, 0, SEEK_END);
   return b;
 }
 
 struct fastbuf *
   if (mode & O_APPEND)
     bfd_seek(b, 0, SEEK_END);
   return b;
 }
 
 struct fastbuf *
-bfdopen(int fd, uns buffer)
+bopen(const char *name, uns mode, uns buflen)
+{
+  if (!buflen)
+    return bopen_mm(name, mode);
+  struct fastbuf *b = bopen_try(name, mode, buflen);
+  if (!b)
+    die("Unable to %s file %s: %m",
+       (mode & O_CREAT) ? "create" : "open", name);
+  return b;
+}
+
+struct fastbuf *
+bfdopen(int fd, uns buflen)
 {
   byte x[32];
 
   sprintf(x, "fd%d", fd);
 {
   byte x[32];
 
   sprintf(x, "fd%d", fd);
-  return bfdopen_internal(fd, buffer, x);
+  return bfdopen_internal(fd, buflen, x);
 }
 
 struct fastbuf *
 }
 
 struct fastbuf *
-bfdopen_shared(int fd, uns buffer)
+bfdopen_shared(int fd, uns buflen)
 {
 {
-  struct fastbuf *f = bfdopen(fd, buffer);
+  struct fastbuf *f = bfdopen(fd, buflen);
   FB_FILE(f)->is_temp_file = -1;
   return f;
 }
 
   FB_FILE(f)->is_temp_file = -1;
   return f;
 }
 
+void
+bfilesync(struct fastbuf *b)
+{
+  bflush(b);
+  if (fsync(FB_FILE(b)->fd) < 0)
+    msg(L_ERROR, "fsync(%s) failed: %m", b->name);
+}
+
 #ifdef TEST
 
 #ifdef TEST
 
-int main(int argc, char **argv)
+int main(int argc UNUSED, char **argv UNUSED)
 {
   struct fastbuf *f, *t;
 
 {
   struct fastbuf *f, *t;