]> mj.ucw.cz Git - libucw.git/blob - lib/fb-param.c
Merge with git+ssh://git.ucw.cz/projects/sherlock/GIT/sherlock.git
[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   .read_ahead = 1,
21   .write_back = 1,
22 }; 
23
24 struct cf_section fbpar_cf = {
25 # define F(x) PTR_TO(struct fb_params, x)
26   CF_TYPE(struct fb_params),
27   CF_ITEMS {
28     CF_LOOKUP("Type", (int *)F(type), ((byte *[]){"std", "direct", "mmap", NULL})),
29     CF_UNS("BufSize", F(buffer_size)),
30     CF_UNS("KeepBackBuf", F(keep_back_buf)),
31     CF_UNS("ReadAhead", F(read_ahead)),
32     CF_UNS("WriteBack", F(write_back)),
33     CF_END
34   }
35 # undef F
36 };
37
38 static struct cf_section fbpar_global_cf = {
39   CF_ITEMS {
40     CF_SECTION("Defaults", &fbpar_def, &fbpar_cf),
41     CF_END
42   }
43 };
44
45 static void CONSTRUCTOR
46 fbpar_global_init(void)
47 {
48   cf_declare_section("FBParam", &fbpar_global_cf, 0);
49 }
50
51 static struct fastbuf *
52 bopen_fd_internal(int fd, struct fb_params *params, uns mode, byte *name)
53 {
54   byte buf[32];
55   if (!name)
56     sprintf(name = buf, "fd%d", fd);
57   struct fastbuf *fb;
58   switch (params->type)
59     {
60       case FB_STD:
61         fb = bfdopen_internal(fd, name,
62             params->buffer_size ? : fbpar_def.buffer_size);
63         if (params->keep_back_buf)
64           bconfig(fb, BCONFIG_KEEP_BACK_BUF, 1);
65         return fb;
66       case FB_DIRECT:
67         fb = fbdir_open_fd_internal(fd, name, params->asio,
68             params->buffer_size ? : fbpar_def.buffer_size,
69             params->read_ahead ? : fbpar_def.read_ahead,
70             params->write_back ? : fbpar_def.write_back);
71         if (!~mode && !fbdir_cheat && ((int)(mode = fcntl(fd, F_GETFL)) < 0 || fcntl(fd, F_SETFL, mode | O_DIRECT)) < 0)
72           log(L_WARN, "Cannot set O_DIRECT on fd %d: %m", fd);
73         return fb;
74       case FB_MMAP:
75         if (!~mode && (int)(mode = fcntl(fd, F_GETFL)) < 0)
76           die("Cannot get flags of fd %d: %m", fd);
77         return bfmmopen_internal(fd, name, mode);
78       default:
79         ASSERT(0);
80     }
81 }
82
83 static struct fastbuf *
84 bopen_file_internal(byte *name, int mode, struct fb_params *params, int try)
85 {
86   if (params->type == FB_DIRECT && !fbdir_cheat)
87     mode |= O_DIRECT;
88   if (params->type == FB_MMAP && (mode & O_ACCMODE) == O_WRONLY)
89     mode = (mode & ~O_ACCMODE) | O_RDWR;
90   int fd = sh_open(name, mode, 0666);
91   if (fd < 0)
92     if (try)
93       return NULL;
94     else
95       die("Unable to %s file %s: %m", (mode & O_CREAT) ? "create" : "open", name);
96   struct fastbuf *fb = bopen_fd_internal(fd, params, mode, name);
97   ASSERT(fb);
98   if (mode & O_APPEND)
99     bseek(fb, 0, SEEK_END);
100   return fb;
101 }
102
103 struct fastbuf *
104 bopen_file(byte *name, int mode, struct fb_params *params)
105 {
106   return bopen_file_internal(name, mode, params ? : &fbpar_def, 0);
107 }
108
109 struct fastbuf *
110 bopen_file_try(byte *name, int mode, struct fb_params *params)
111 {
112   return bopen_file_internal(name, mode, params ? : &fbpar_def, 1);
113 }
114
115 struct fastbuf *
116 bopen_fd(int fd, struct fb_params *params)
117 {
118   return bopen_fd_internal(fd, params ? : &fbpar_def, ~0U, NULL);
119 }
120
121 struct fastbuf *
122 bopen_tmp_file(struct fb_params *params)
123 {
124   byte buf[TEMP_FILE_NAME_LEN];
125   temp_file_name(buf);
126   struct fastbuf *fb = bopen_file_internal(buf, O_RDWR | O_CREAT | O_TRUNC, params, 0);
127   bconfig(fb, BCONFIG_IS_TEMP_FILE, 1);
128   return fb;
129 }