2 * UCW Library -- FastIO on files with run-time parametrization
4 * (c) 2007 Pavel Charvat <pchar@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
13 #include "lib/fastbuf.h"
18 struct fb_params fbpar_def = {
22 struct cf_section fbpar_cf = {
23 # define F(x) PTR_TO(struct fb_params, x)
24 CF_TYPE(struct fb_params),
27 CF_LOOKUP("Type", (int *)F(type), ((byte *[]){"std", "direct", "mmap", NULL})),
28 CF_UNS("BufSize", F(buffer_size)),
34 static struct cf_section fbpar_global_cf = {
36 CF_SECTION("Defaults", &fbpar_def, &fbpar_cf),
41 static void CONSTRUCTOR
42 fbpar_global_init(void)
44 cf_declare_section("FBParam", &fbpar_global_cf, 0);
47 static struct fastbuf *
48 bopen_fd_internal(int fd, struct fb_params *params, byte *name)
54 return bfdopen_internal(fd, params->buffer_size, name);
56 fb = fbdir_open_fd_internal(fd, params->asio, name);
57 if (!fbdir_cheat && fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_DIRECT) < 0)
58 log(L_WARN, "Cannot set O_DIRECT on fd %d: %m", fd);
67 static struct fastbuf *
68 bopen_file_internal(byte *name, int mode, struct fb_params *params, int try)
70 if (params->type == FB_DIRECT && !fbdir_cheat)
72 int fd = sh_open(name, mode, 0666);
77 die("Unable to %s file %s: %m", (mode & O_CREAT) ? "create" : "open", name);
78 struct fastbuf *fb = bopen_fd_internal(fd, params, name);
81 bseek(fb, 0, SEEK_END);
86 bopen_file(byte *name, int mode, struct fb_params *params)
88 return bopen_file_internal(name, mode, params ? : &fbpar_def, 0);
92 bopen_file_try(byte *name, int mode, struct fb_params *params)
94 return bopen_file_internal(name, mode, params ? : &fbpar_def, 1);
98 bopen_fd(int fd, struct fb_params *params)
101 sprintf(x, "fd%d", fd);
102 return bopen_fd_internal(fd, params ? : &fbpar_def, x);
106 bopen_tmp_file(struct fb_params *params)
108 byte buf[TEMP_FILE_NAME_LEN];
110 struct fastbuf *fb = bopen_file_internal(buf, O_RDWR | O_CREAT | O_TRUNC, params, 0);
111 bconfig(fb, BCONFIG_IS_TEMP_FILE, 1);