2 * UCW Library -- Fast Buffered I/O on Files
4 * (c) 1997--2004 Martin Mares <mj@ucw.cz>
5 * (c) 2007 Pavel Charvat <pchar@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
12 #include "lib/fastbuf.h"
22 int fd; /* File descriptor */
23 int is_temp_file; /* 0=normal file, 1=temporary file, delete on close, -1=shared FD */
24 int keep_back_buf; /* Optimize for backwards reading */
25 sh_off_t wpos; /* Real file position */
26 uns wlen; /* Window size */
28 #define FB_FILE(f) ((struct fb_file *)(f)->is_fastbuf)
29 #define FB_BUFFER(f) (byte *)(FB_FILE(f) + 1)
32 bfd_refill(struct fastbuf *f)
34 struct fb_file *F = FB_FILE(f);
35 byte *read_ptr = (f->buffer = FB_BUFFER(f));
36 uns blen = f->bufend - f->buffer, back = F->keep_back_buf ? blen >> 2 : 0, read_len = blen;
37 /* Forward or no seek */
38 if (F->wpos <= f->pos)
40 sh_off_t diff = f->pos - F->wpos;
41 /* Formula for long forward seeks (prefer lseek()) */
42 if (diff > ((sh_off_t)blen << 2))
45 f->bptr = f->buffer + back;
46 f->bstop = f->buffer + blen;
49 /* Short forward seek (prefer read() to skip data )*/
50 else if ((uns)diff >= back)
52 uns skip = diff - back;
56 int l = read(F->fd, f->buffer, MIN(skip, blen));
59 die("Error reading %s: %m", f->name);
68 /* Reuse part of the previous window and append new data (also F->wpos == f->pos) */
71 uns keep = back - (uns)diff;
73 back = diff + (keep = F->wlen);
75 memmove(f->buffer, f->buffer + F->wlen - keep, keep);
79 f->bptr = f->buffer + back;
80 f->bstop = f->buffer + blen;
85 sh_off_t diff = F->wpos - f->pos;
86 /* Formula for long backwards seeks (keep smaller backbuffer than for shorter seeks ) */
87 if (diff > ((sh_off_t)blen << 1))
89 if ((sh_off_t)back > f->pos)
93 /* Seek into previous window (do nothing... for example brewind) */
94 else if ((uns)diff <= F->wlen)
96 f->bstop = f->buffer + F->wlen;
97 f->bptr = f->bstop - diff;
102 if ((sh_off_t)back > f->pos)
104 f->bptr = f->buffer + back;
105 read_len = back + diff - F->wlen;
106 /* Reuse part of previous window */
107 if (F->wlen && read_len < blen)
109 uns keep = MIN(F->wlen, blen - read_len);
110 memmove(f->buffer + read_len, f->buffer, keep);
111 f->bstop = f->buffer + read_len + keep;
114 f->bstop = f->buffer + (read_len = blen);
117 F->wpos = f->pos + (f->buffer - f->bptr);
118 if (sh_seek(F->fd, F->wpos, SEEK_SET) < 0)
119 die("Error seeking %s: %m", f->name);
121 /* Read (part of) buffer */
124 int l = read(F->fd, read_ptr, read_len);
126 die("Error reading %s: %m", f->name);
128 if (unlikely(read_ptr < f->bptr))
131 break; /* Incomplete read because of EOF */
136 while (read_ptr <= f->bptr);
139 f->pos += f->bstop - f->bptr;
140 F->wlen = f->bstop - f->buffer;
141 return f->bstop - f->bptr;
143 /* Seeked behind EOF */
144 f->bptr = f->bstop = f->buffer;
150 bfd_spout(struct fastbuf *f)
152 /* Do delayed lseek() if needed */
153 if (FB_FILE(f)->wpos != f->pos && sh_seek(FB_FILE(f)->fd, f->pos, SEEK_SET) < 0)
154 die("Error seeking %s: %m", f->name);
156 int l = f->bptr - f->buffer;
159 /* Write the buffer */
160 FB_FILE(f)->wpos = (f->pos += l);
161 FB_FILE(f)->wlen = 0;
164 int z = write(FB_FILE(f)->fd, c, l);
166 die("Error writing %s: %m", f->name);
170 f->bptr = f->buffer = FB_BUFFER(f);
174 bfd_seek(struct fastbuf *f, sh_off_t pos, int whence)
176 /* Delay the seek for the next refill() or spout() call (if whence != SEEK_END). */
185 if ((pos > 0) ^ (l > f->pos))
190 l = sh_seek(FB_FILE(f)->fd, pos, SEEK_END);
193 FB_FILE(f)->wpos = f->pos = l;
194 FB_FILE(f)->wlen = 0;
202 bfd_close(struct fastbuf *f)
204 switch (FB_FILE(f)->is_temp_file)
207 if (unlink(f->name) < 0)
208 msg(L_ERROR, "unlink(%s): %m", f->name);
210 if (close(FB_FILE(f)->fd))
211 die("close(%s): %m", f->name);
217 bfd_config(struct fastbuf *f, uns item, int value)
221 case BCONFIG_IS_TEMP_FILE:
222 FB_FILE(f)->is_temp_file = value;
224 case BCONFIG_KEEP_BACK_BUF:
225 FB_FILE(f)->keep_back_buf = value;
233 bfdopen_internal(int fd, const char *name, uns buflen)
236 int namelen = strlen(name) + 1;
237 struct fb_file *F = xmalloc_zero(sizeof(struct fb_file) + buflen + namelen);
238 struct fastbuf *f = &F->fb;
240 bzero(F, sizeof(*F));
241 f->buffer = (byte *)(F+1);
242 f->bptr = f->bstop = f->buffer;
243 f->bufend = f->buffer + buflen;
245 memcpy(f->name, name, namelen);
247 f->refill = bfd_refill;
248 f->spout = bfd_spout;
250 f->close = bfd_close;
251 f->config = bfd_config;
252 f->can_overwrite_buffer = 2;
257 bopen_try(const char *name, uns mode, uns buflen)
259 return bopen_file_try(name, mode, &(struct fb_params){ .type = FB_STD, .buffer_size = buflen });
263 bopen(const char *name, uns mode, uns buflen)
265 return bopen_file(name, mode, &(struct fb_params){ .type = FB_STD, .buffer_size = buflen });
269 bfdopen(int fd, uns buflen)
271 return bopen_fd(fd, &(struct fb_params){ .type = FB_STD, .buffer_size = buflen });
275 bfdopen_shared(int fd, uns buflen)
277 struct fastbuf *f = bfdopen(fd, buflen);
278 FB_FILE(f)->is_temp_file = -1;
283 bfilesync(struct fastbuf *b)
286 if (fsync(FB_FILE(b)->fd) < 0)
287 msg(L_ERROR, "fsync(%s) failed: %m", b->name);
294 struct fastbuf *f, *t;
297 for (uns i = 0; i < 16; i++)
298 bwrite(f, "<hello>", 7);
299 bprintf(t, "%d\n", (int)btell(f));
302 bprintf(t, "\n%d %d\n", (int)btell(f), (int)btell(t));