]> mj.ucw.cz Git - libucw.git/blobdiff - lib/fb-mmap.c
Cleanup of file fastbufs.
[libucw.git] / lib / fb-mmap.c
index 41e65832653c8c16180920d78172bc6221428e63..a57d1034c32b04cfdaa4aec66912a633d7b4ee5d 100644 (file)
@@ -7,6 +7,8 @@
  *     of the GNU Lesser General Public License.
  */
 
+#undef LOCAL_DEBUG
+
 #include "lib/lib.h"
 #include "lib/fastbuf.h"
 #include "lib/lfs.h"
@@ -40,6 +42,7 @@ struct fb_mmap {
   sh_off_t file_size;
   sh_off_t file_extend;
   sh_off_t window_pos;
+  uns window_size;
   int mode;
 };
 #define FB_MMAP(f) ((struct fb_mmap *)(f)->is_fastbuf)
@@ -51,15 +54,15 @@ bfmm_map_window(struct fastbuf *f)
   sh_off_t pos0 = f->pos & ~(sh_off_t)(CPU_PAGE_SIZE-1);
   int l = MIN((sh_off_t)mmap_window_size, F->file_extend - pos0);
   uns ll = ALIGN_TO(l, CPU_PAGE_SIZE);
-  uns oll = ALIGN_TO(f->bufend - f->buffer, CPU_PAGE_SIZE);
   int prot = ((F->mode & O_ACCMODE) == O_RDONLY) ? PROT_READ : (PROT_READ | PROT_WRITE);
 
   DBG(" ... Mapping %x(%x)+%x(%x) len=%x extend=%x", (int)pos0, (int)f->pos, ll, l, (int)F->file_size, (int)F->file_extend);
-  if (ll != oll && f->buffer)
+  if (ll != F->window_size && f->buffer)
     {
-      munmap(f->buffer, oll);
+      munmap(f->buffer, F->window_size);
       f->buffer = NULL;
     }
+  F->window_size = ll;
   if (!f->buffer)
     f->buffer = sh_mmap(NULL, ll, prot, MAP_SHARED, F->fd, pos0);
   else
@@ -126,7 +129,7 @@ bfmm_seek(struct fastbuf *f, sh_off_t pos, int whence)
     ASSERT(whence == SEEK_SET);
   ASSERT(pos >= 0 && pos <= FB_MMAP(f)->file_size);
   f->pos = pos;
-  f->bptr = f->bstop = f->bufend;      /* force refill/spout call */
+  f->bptr = f->bstop = f->bufend = f->buffer;  /* force refill/spout call */
   DBG("Seek -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
   return 1;
 }
@@ -137,36 +140,32 @@ bfmm_close(struct fastbuf *f)
   struct fb_mmap *F = FB_MMAP(f);
 
   if (f->buffer)
-    munmap(f->buffer, ALIGN_TO(f->bufend-f->buffer, CPU_PAGE_SIZE));
+    munmap(f->buffer, F->window_size);
   if (F->file_extend > F->file_size &&
       sh_ftruncate(F->fd, F->file_size))
     die("ftruncate(%s): %m", f->name);
-  switch (F->is_temp_file)
-    {
-    case 1:
-      if (unlink(f->name) < 0)
-       log(L_ERROR, "unlink(%s): %m", f->name);
-    case 0:
-      close(F->fd);
-    }
+  bclose_file_helper(f, F->fd, F->is_temp_file);
   xfree(f);
 }
 
 static int
 bfmm_config(struct fastbuf *f, uns item, int value)
 {
+  int orig;
+
   switch (item)
     {
     case BCONFIG_IS_TEMP_FILE:
+      orig = FB_MMAP(f)->is_temp_file;
       FB_MMAP(f)->is_temp_file = value;
-      return 0;
+      return orig;
     default:
       return -1;
     }
 }
 
-static struct fastbuf *
-bfmmopen_internal(int fd, byte *name, uns mode)
+struct fastbuf *
+bfmmopen_internal(int fd, const char *name, uns mode)
 {
   int namelen = strlen(name) + 1;
   struct fb_mmap *F = xmalloc(sizeof(struct fb_mmap) + namelen);
@@ -177,6 +176,8 @@ bfmmopen_internal(int fd, byte *name, uns mode)
   memcpy(f->name, name, namelen);
   F->fd = fd;
   F->file_extend = F->file_size = sh_seek(fd, 0, SEEK_END);
+  if (F->file_size < 0)
+    die("seek(%s): %m", name);
   if (mode & O_APPEND)
     f->pos = F->file_size;
   F->mode = mode;
@@ -189,26 +190,13 @@ bfmmopen_internal(int fd, byte *name, uns mode)
   return f;
 }
 
-struct fastbuf *
-bopen_mm(byte *name, uns mode)
-{
-  int fd;
-
-  if ((mode & O_ACCMODE) == O_WRONLY)
-    mode = (mode & ~O_ACCMODE) | O_RDWR;
-  fd = sh_open(name, mode, 0666);
-  if (fd < 0)
-    die("Unable to %s file %s: %m",
-       (mode & O_CREAT) ? "create" : "open", name);
-  return bfmmopen_internal(fd, name, mode);
-}
-
 #ifdef TEST
 
 int main(int argc, char **argv)
 {
-  struct fastbuf *f = bopen_mm(argv[1], O_RDONLY);
-  struct fastbuf *g = bopen_mm(argv[2], O_RDWR | O_CREAT | O_TRUNC);
+  struct fb_params par = { .type = FB_MMAP };
+  struct fastbuf *f = bopen_file(argv[1], O_RDONLY, &par);
+  struct fastbuf *g = bopen_file(argv[2], O_RDWR | O_CREAT | O_TRUNC, &par);
   int c;
 
   DBG("Copying");