2 * UCW Library -- Fast Buffered I/O on Files
4 * (c) 1997--2004 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include "lib/fastbuf.h"
20 int fd; /* File descriptor */
21 int is_temp_file; /* 0=normal file, 1=temporary file, delete on close, -1=shared FD */
23 #define FB_FILE(f) ((struct fb_file *)(f)->is_fastbuf)
24 #define FB_BUFFER(f) (byte *)(FB_FILE(f) + 1)
27 bfd_refill(struct fastbuf *f)
29 f->bptr = f->buffer = FB_BUFFER(f);
30 int l = read(FB_FILE(f)->fd, f->buffer, f->bufend-f->buffer);
32 die("Error reading %s: %m", f->name);
33 f->bstop = f->buffer + l;
39 bfd_spout(struct fastbuf *f)
41 int l = f->bptr - f->buffer;
47 int z = write(FB_FILE(f)->fd, c, l);
49 die("Error writing %s: %m", f->name);
53 f->bptr = f->buffer = FB_BUFFER(f);
57 bfd_seek(struct fastbuf *f, sh_off_t pos, int whence)
59 if (whence == SEEK_SET && pos == f->pos)
62 sh_off_t l = sh_seek(FB_FILE(f)->fd, pos, whence);
70 bfd_close(struct fastbuf *f)
72 switch (FB_FILE(f)->is_temp_file)
75 if (unlink(f->name) < 0)
76 log(L_ERROR, "unlink(%s): %m", f->name);
78 close(FB_FILE(f)->fd);
84 bfd_config(struct fastbuf *f, uns item, int value)
88 case BCONFIG_IS_TEMP_FILE:
89 FB_FILE(f)->is_temp_file = value;
96 static struct fastbuf *
97 bfdopen_internal(int fd, uns buflen, byte *name)
99 int namelen = strlen(name) + 1;
100 struct fb_file *F = xmalloc(sizeof(struct fb_file) + buflen + namelen);
101 struct fastbuf *f = &F->fb;
103 bzero(F, sizeof(*F));
104 f->buffer = (byte *)(F+1);
105 f->bptr = f->bstop = f->buffer;
106 f->bufend = f->buffer + buflen;
108 memcpy(f->name, name, namelen);
110 f->refill = bfd_refill;
111 f->spout = bfd_spout;
113 f->close = bfd_close;
114 f->config = bfd_config;
115 f->can_overwrite_buffer = 2;
120 bopen_try(byte *name, uns mode, uns buflen)
122 int fd = sh_open(name, mode, 0666);
125 struct fastbuf *b = bfdopen_internal(fd, buflen, name);
127 bfd_seek(b, 0, SEEK_END);
132 bopen(byte *name, uns mode, uns buflen)
135 return bopen_mm(name, mode);
136 struct fastbuf *b = bopen_try(name, mode, buflen);
138 die("Unable to %s file %s: %m",
139 (mode & O_CREAT) ? "create" : "open", name);
144 bfdopen(int fd, uns buflen)
148 sprintf(x, "fd%d", fd);
149 return bfdopen_internal(fd, buflen, x);
153 bfdopen_shared(int fd, uns buflen)
155 struct fastbuf *f = bfdopen(fd, buflen);
156 FB_FILE(f)->is_temp_file = -1;
161 bfilesync(struct fastbuf *b)
164 if (fsync(FB_FILE(b)->fd) < 0)
165 log(L_ERROR, "fsync(%s) failed: %m", b->name);
170 int main(int argc UNUSED, char **argv UNUSED)
172 struct fastbuf *f, *t;
174 f = bopen("/etc/profile", O_RDONLY, 16);
177 printf("%d %d\n", (int)btell(f), (int)btell(t));