/* 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);
/* 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);
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);
#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;
}
}
-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);
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
}
}
-static struct fastbuf *
+struct fastbuf *
bfdopen_internal(int fd, uns buflen, byte *name)
{
int namelen = strlen(name) + 1;
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 *
#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,
};
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
}
};
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;
}