]> mj.ucw.cz Git - libucw.git/commitdiff
unfinished parametrized fastbufs
authorPavel Charvat <pavel.charvat@netcentrum.cz>
Tue, 29 May 2007 10:57:50 +0000 (12:57 +0200)
committerPavel Charvat <pavel.charvat@netcentrum.cz>
Tue, 29 May 2007 10:57:50 +0000 (12:57 +0200)
lib/fastbuf.h
lib/fb-direct.c
lib/fb-file.c
lib/fb-param.c

index 8d7a164e1d6f41fb435db25d2e58f39e9ec09303..14b403c742828cc17d8ef60cb29d75405210ae60 100644 (file)
@@ -75,6 +75,7 @@ struct fastbuf {
 
 /* FastIO on standard files (specify buffer size 0 to enable mmaping) */
 
+struct fastbuf *bfdopen_internal(int fd, uns buflen, byte *name);
 struct fastbuf *bopen(byte *name, uns mode, uns buflen);
 struct fastbuf *bopen_try(byte *name, uns mode, uns buflen);
 struct fastbuf *bopen_tmp(uns buflen);
@@ -96,7 +97,10 @@ struct fastbuf *bopen_mm(byte *name, uns mode);
 
 /* FastIO on files opened with O_DIRECT (see fb-direct.c for description) */
 
+extern uns fbdir_cheat;
+
 struct asio_queue;
+struct fastbuf *fbdir_open_fd_internal(int fd, struct asio_queue *io_queue, byte *name);
 struct fastbuf *fbdir_open(byte *name, uns mode, struct asio_queue *io_queue);
 struct fastbuf *fbdir_open_try(byte *name, uns mode, struct asio_queue *io_queue);
 struct fastbuf *fbdir_open_fd(int fd, struct asio_queue *io_queue);
@@ -113,11 +117,12 @@ enum fb_type {
 struct fb_params {
   enum fb_type type;
   uns buffer_size;
+  struct asio_queue *asio;
 };
 
 struct cf_section;
 extern struct cf_section fbpar_cf;
-extern struct fb_params fbpar_defaults;
+extern struct fb_params fbpar_def;
 
 struct fastbuf *bopen_file(byte *name, int mode, struct fb_params *params);
 struct fastbuf *bopen_file_try(byte *name, int mode, struct fb_params *params);
index 9eca31fdae34357f09a303c918734b57486a735a..e56d98400332f037dbe08dd042402e1cf0eb2db0 100644 (file)
@@ -37,7 +37,7 @@
 #include <unistd.h>
 #include <stdio.h>
 
-static uns fbdir_cheat;
+uns fbdir_cheat;
 static uns fbdir_buffer_size = 65536;
 static uns fbdir_read_ahead = 1;
 static uns fbdir_write_back = 1;
@@ -295,8 +295,8 @@ fbdir_config(struct fastbuf *f, uns item, int value)
     }
 }
 
-static struct fastbuf *
-fbdir_open_internal(byte *name, int fd, struct asio_queue *q)
+struct fastbuf *
+fbdir_open_fd_internal(int fd, struct asio_queue *q, byte *name)
 {
   int namelen = strlen(name) + 1;
   struct fb_direct *F = xmalloc(sizeof(struct fb_direct) + namelen);
@@ -323,48 +323,25 @@ fbdir_open_internal(byte *name, int fd, struct asio_queue *q)
 struct fastbuf *
 fbdir_open_try(byte *name, uns mode, struct asio_queue *q)
 {
-  if (!fbdir_cheat)
-    mode |= O_DIRECT;
-  int fd = sh_open(name, mode, 0666);
-  if (fd < 0)
-    return NULL;
-  struct fastbuf *b = fbdir_open_internal(name, fd, q);
-  if (mode & O_APPEND)
-    fbdir_seek(b, 0, SEEK_END);
-  return b;
+  return bopen_file_try(name, mode, &(struct fb_params){ .type = FB_DIRECT, .asio = q });
 }
 
 struct fastbuf *
 fbdir_open(byte *name, uns mode, struct asio_queue *q)
 {
-  struct fastbuf *b = fbdir_open_try(name, mode, q);
-  if (!b)
-    die("Unable to %s file %s: %m",
-       (mode & O_CREAT) ? "create" : "open", name);
-  return b;
+  return bopen_file(name, mode, &(struct fb_params){ .type = FB_DIRECT, .asio = q });
 }
 
 struct fastbuf *
 fbdir_open_fd(int fd, struct asio_queue *q)
 {
-  byte x[32];
-
-  sprintf(x, "fd%d", fd);
-  if (!fbdir_cheat && fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_DIRECT) < 0)
-    log(L_WARN, "Cannot set O_DIRECT on fd %d: %m", fd);
-  return fbdir_open_internal(x, fd, q);
+  return bopen_fd(fd, &(struct fb_params){ .type = FB_DIRECT, .asio = q });
 }
 
 struct fastbuf *
 fbdir_open_tmp(struct asio_queue *q)
 {
-  byte buf[TEMP_FILE_NAME_LEN];
-  struct fastbuf *f;
-
-  temp_file_name(buf);
-  f = fbdir_open(buf, O_RDWR | O_CREAT | O_TRUNC, q);
-  bconfig(f, BCONFIG_IS_TEMP_FILE, 1);
-  return f;
+  return bopen_tmp_file(&(struct fb_params){ .type = FB_DIRECT, .asio = q });
 }
 
 #ifdef TEST
index 0d1cc96cbd8cca573086d4302f76355398271142..cb479f3b55e01d3365adf7607d2e41b23c847da1 100644 (file)
@@ -91,7 +91,7 @@ bfd_config(struct fastbuf *f, uns item, int value)
     }
 }
 
-static struct fastbuf *
+struct fastbuf *
 bfdopen_internal(int fd, uns buflen, byte *name)
 {
   int namelen = strlen(name) + 1;
@@ -118,34 +118,19 @@ bfdopen_internal(int fd, uns buflen, byte *name)
 struct fastbuf *
 bopen_try(byte *name, uns mode, uns buflen)
 {
-  int fd = sh_open(name, mode, 0666);
-  if (fd < 0)
-    return NULL;
-  struct fastbuf *b = bfdopen_internal(fd, buflen, name);
-  if (mode & O_APPEND)
-    bfd_seek(b, 0, SEEK_END);
-  return b;
+  return bopen_file_try(name, mode, &(struct fb_params){ .type = FB_STD, .buffer_size = buflen });
 }
 
 struct fastbuf *
 bopen(byte *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;
+  return bopen_file(name, mode, &(struct fb_params){ .type = FB_STD, .buffer_size = buflen });
 }
 
 struct fastbuf *
 bfdopen(int fd, uns buflen)
 {
-  byte x[32];
-
-  sprintf(x, "fd%d", fd);
-  return bfdopen_internal(fd, buflen, x);
+  return bopen_fd(fd, &(struct fb_params){ .type = FB_STD, .buffer_size = buflen });
 }
 
 struct fastbuf *
index 6fc5f27ac2043da3f95c193554ac03f87c0b9698..714ef6471402aa121a1cde4edc8ed6c02a11bf7f 100644 (file)
@@ -9,9 +9,13 @@
 
 #include "lib/lib.h"
 #include "lib/conf.h"
+#include "lib/lfs.h"
 #include "lib/fastbuf.h"
 
-struct fb_params fbpar_defaults = {
+#include <fcntl.h>
+#include <stdio.h>
+
+struct fb_params fbpar_def = {
   .buffer_size = 65536,
 }; 
 
@@ -29,7 +33,7 @@ struct cf_section fbpar_cf = {
 
 static struct cf_section fbpar_global_cf = {
   CF_ITEMS {
-    CF_SECTION("Defaults", &fbpar_defaults, &fbpar_cf),
+    CF_SECTION("Defaults", &fbpar_def, &fbpar_cf),
     CF_END
   }
 };
@@ -40,62 +44,70 @@ fbpar_global_init(void)
   cf_declare_section("FBParam", &fbpar_global_cf, 0);
 }
 
-struct fastbuf *
-bopen_file(byte *name, int mode, struct fb_params *params)
+static struct fastbuf *
+bopen_fd_internal(int fd, struct fb_params *params, byte *name)
 {
-  params = params ? : &fbpar_defaults;
+  struct fastbuf *fb;
   switch (params->type)
     {
       case FB_STD:
-        return bopen(name, mode, params->buffer_size);
+       return bfdopen_internal(fd, params->buffer_size, name);
       case FB_DIRECT:
-        return fbdir_open(name, mode, NULL);
-      default:
+       fb = fbdir_open_fd_internal(fd, params->asio, name);
+       if (!fbdir_cheat && fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_DIRECT) < 0)
+          log(L_WARN, "Cannot set O_DIRECT on fd %d: %m", fd);
+       return fb;
+      case FB_MMAP:
+       // FIXME
        ASSERT(0);
     }
+  ASSERT(0);
+}
+
+static struct fastbuf *
+bopen_file_internal(byte *name, int mode, struct fb_params *params, int try)
+{
+  if (params->type == FB_DIRECT && !fbdir_cheat)
+    mode |= O_DIRECT;
+  int fd = sh_open(name, mode, 0666);
+  if (fd < 0)
+    if (try)
+      return NULL;
+    else
+      die("Unable to %s file %s: %m", (mode & O_CREAT) ? "create" : "open", name);
+  struct fastbuf *fb = bopen_fd_internal(fd, params, name);
+  ASSERT(fb);
+  if (mode & O_APPEND)
+    bseek(fb, 0, SEEK_END);
+  return fb;
+}
+
+struct fastbuf *
+bopen_file(byte *name, int mode, struct fb_params *params)
+{
+  return bopen_file_internal(name, mode, params ? : &fbpar_def, 0);
 }
 
 struct fastbuf *
 bopen_file_try(byte *name, int mode, struct fb_params *params)
 {
-  params = params ? : &fbpar_defaults;
-  switch (params->type)
-    {
-      case FB_STD:
-        return bopen_try(name, mode, params->buffer_size);
-      case FB_DIRECT:
-        return fbdir_open_try(name, mode, NULL);
-      default:
-       ASSERT(0);
-    }
+  return bopen_file_internal(name, mode, params ? : &fbpar_def, 1);
 }
 
 struct fastbuf *
 bopen_fd(int fd, struct fb_params *params)
 {
-  params = params ? : &fbpar_defaults;
-  switch (params->type)
-    {
-      case FB_STD:
-        return bfdopen(fd, params->buffer_size);
-      case FB_DIRECT:
-        return fbdir_open_fd(fd, NULL);
-      default:
-       ASSERT(0);
-    }
+  byte x[32];
+  sprintf(x, "fd%d", fd);
+  return bopen_fd_internal(fd, params ? : &fbpar_def, x);
 }
 
 struct fastbuf *
 bopen_tmp_file(struct fb_params *params)
 {
-  params = params ? : &fbpar_defaults;
-  switch (params->type)
-    {
-      case FB_STD:
-        return bopen_tmp(params->buffer_size);
-      case FB_DIRECT:
-        return fbdir_open_tmp(NULL);
-      default:
-       ASSERT(0);
-    }
+  byte buf[TEMP_FILE_NAME_LEN];
+  temp_file_name(buf);
+  struct fastbuf *fb = bopen_file_internal(buf, O_RDWR | O_CREAT | O_TRUNC, params, 0);
+  bconfig(fb, BCONFIG_IS_TEMP_FILE, 1);
+  return fb;
 }