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