]> mj.ucw.cz Git - libucw.git/blob - lib/fb-param.c
unfinished parametrized fastbufs
[libucw.git] / lib / fb-param.c
1 /*
2  *      UCW Library -- FastIO on files with run-time parametrization
3  *
4  *      (c) 2007 Pavel Charvat <pchar@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include "lib/lib.h"
11 #include "lib/conf.h"
12 #include "lib/lfs.h"
13 #include "lib/fastbuf.h"
14
15 #include <fcntl.h>
16 #include <stdio.h>
17
18 struct fb_params fbpar_def = {
19   .buffer_size = 65536,
20 }; 
21
22 struct cf_section fbpar_cf = {
23 # define F(x) PTR_TO(struct fb_params, x)
24   CF_TYPE(struct fb_params),
25   CF_ITEMS {
26     // FIXME
27     CF_LOOKUP("Type", (int *)F(type), ((byte *[]){"std", "direct", "mmap", NULL})),
28     CF_UNS("BufSize", F(buffer_size)),
29     CF_END
30   }
31 # undef F
32 };
33
34 static struct cf_section fbpar_global_cf = {
35   CF_ITEMS {
36     CF_SECTION("Defaults", &fbpar_def, &fbpar_cf),
37     CF_END
38   }
39 };
40
41 static void CONSTRUCTOR
42 fbpar_global_init(void)
43 {
44   cf_declare_section("FBParam", &fbpar_global_cf, 0);
45 }
46
47 static struct fastbuf *
48 bopen_fd_internal(int fd, struct fb_params *params, byte *name)
49 {
50   struct fastbuf *fb;
51   switch (params->type)
52     {
53       case FB_STD:
54         return bfdopen_internal(fd, params->buffer_size, name);
55       case FB_DIRECT:
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);
59         return fb;
60       case FB_MMAP:
61         // FIXME
62         ASSERT(0);
63     }
64   ASSERT(0);
65 }
66
67 static struct fastbuf *
68 bopen_file_internal(byte *name, int mode, struct fb_params *params, int try)
69 {
70   if (params->type == FB_DIRECT && !fbdir_cheat)
71     mode |= O_DIRECT;
72   int fd = sh_open(name, mode, 0666);
73   if (fd < 0)
74     if (try)
75       return NULL;
76     else
77       die("Unable to %s file %s: %m", (mode & O_CREAT) ? "create" : "open", name);
78   struct fastbuf *fb = bopen_fd_internal(fd, params, name);
79   ASSERT(fb);
80   if (mode & O_APPEND)
81     bseek(fb, 0, SEEK_END);
82   return fb;
83 }
84
85 struct fastbuf *
86 bopen_file(byte *name, int mode, struct fb_params *params)
87 {
88   return bopen_file_internal(name, mode, params ? : &fbpar_def, 0);
89 }
90
91 struct fastbuf *
92 bopen_file_try(byte *name, int mode, struct fb_params *params)
93 {
94   return bopen_file_internal(name, mode, params ? : &fbpar_def, 1);
95 }
96
97 struct fastbuf *
98 bopen_fd(int fd, struct fb_params *params)
99 {
100   byte x[32];
101   sprintf(x, "fd%d", fd);
102   return bopen_fd_internal(fd, params ? : &fbpar_def, x);
103 }
104
105 struct fastbuf *
106 bopen_tmp_file(struct fb_params *params)
107 {
108   byte buf[TEMP_FILE_NAME_LEN];
109   temp_file_name(buf);
110   struct fastbuf *fb = bopen_file_internal(buf, O_RDWR | O_CREAT | O_TRUNC, params, 0);
111   bconfig(fb, BCONFIG_IS_TEMP_FILE, 1);
112   return fb;
113 }