2 * UCW Library -- FastIO on files with run-time parametrization
4 * (c) 2007 Pavel Charvat <pchar@ucw.cz>
5 * (c) 2007 Martin Mares <mj@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
14 #include "lib/fastbuf.h"
19 struct fb_params fbpar_def = {
25 struct cf_section fbpar_cf = {
26 # define F(x) PTR_TO(struct fb_params, x)
27 CF_TYPE(struct fb_params),
29 CF_LOOKUP("Type", (int *)F(type), ((char *[]){"std", "direct", "mmap", NULL})),
30 CF_UNS("BufSize", F(buffer_size)),
31 CF_UNS("KeepBackBuf", F(keep_back_buf)),
32 CF_UNS("ReadAhead", F(read_ahead)),
33 CF_UNS("WriteBack", F(write_back)),
39 static struct cf_section fbpar_global_cf = {
41 CF_SECTION("Defaults", &fbpar_def, &fbpar_cf),
46 static void CONSTRUCTOR
47 fbpar_global_init(void)
49 cf_declare_section("FBParam", &fbpar_global_cf, 0);
52 static struct fastbuf *
53 bopen_fd_internal(int fd, struct fb_params *params, uns mode, const char *name)
58 sprintf(buf, "fd%d", fd);
65 fb = bfdopen_internal(fd, name,
66 params->buffer_size ? : fbpar_def.buffer_size);
67 if (params->keep_back_buf)
68 bconfig(fb, BCONFIG_KEEP_BACK_BUF, 1);
71 fb = fbdir_open_fd_internal(fd, name, params->asio,
72 params->buffer_size ? : fbpar_def.buffer_size,
73 params->read_ahead ? : fbpar_def.read_ahead,
74 params->write_back ? : fbpar_def.write_back);
75 if (!~mode && !fbdir_cheat && ((int)(mode = fcntl(fd, F_GETFL)) < 0 || fcntl(fd, F_SETFL, mode | O_DIRECT)) < 0)
76 msg(L_WARN, "Cannot set O_DIRECT on fd %d: %m", fd);
79 if (!~mode && (int)(mode = fcntl(fd, F_GETFL)) < 0)
80 die("Cannot get flags of fd %d: %m", fd);
81 return bfmmopen_internal(fd, name, mode);
87 static struct fastbuf *
88 bopen_file_internal(const char *name, int mode, struct fb_params *params, int try)
92 if (params->type == FB_DIRECT && !fbdir_cheat)
94 if (params->type == FB_MMAP && (mode & O_ACCMODE) == O_WRONLY)
95 mode = (mode & ~O_ACCMODE) | O_RDWR;
96 int fd = sh_open(name, mode, 0666);
101 die("Unable to %s file %s: %m", (mode & O_CREAT) ? "create" : "open", name);
102 struct fastbuf *fb = bopen_fd_internal(fd, params, mode, name);
105 bseek(fb, 0, SEEK_END);
110 bopen_file(const char *name, int mode, struct fb_params *params)
112 return bopen_file_internal(name, mode, params, 0);
116 bopen_file_try(const char *name, int mode, struct fb_params *params)
118 return bopen_file_internal(name, mode, params, 1);
122 bopen_fd(int fd, struct fb_params *params)
124 return bopen_fd_internal(fd, params ? : &fbpar_def, ~0U, NULL);
127 /* Function for use by individual file back-ends */
130 bclose_file_helper(struct fastbuf *f, int fd, int is_temp_file)
132 switch (is_temp_file)
135 if (unlink(f->name) < 0)
136 msg(L_ERROR, "unlink(%s): %m", f->name);
139 die("close(%s): %m", f->name);
143 /* Compatibility wrappers */
146 bopen_try(const char *name, uns mode, uns buflen)
148 return bopen_file_try(name, mode, &(struct fb_params){ .type = FB_STD, .buffer_size = buflen });
152 bopen(const char *name, uns mode, uns buflen)
154 return bopen_file(name, mode, &(struct fb_params){ .type = FB_STD, .buffer_size = buflen });
158 bfdopen(int fd, uns buflen)
160 return bopen_fd(fd, &(struct fb_params){ .type = FB_STD, .buffer_size = buflen });
164 bfdopen_shared(int fd, uns buflen)
166 struct fastbuf *f = bfdopen(fd, buflen);
167 bconfig(f, BCONFIG_IS_TEMP_FILE, 2);