/*
- * Sherlock Library -- Fast Buffered I/O on Files
+ * UCW Library -- Fast Buffered I/O on Files
*
- * (c) 1997--2000 Martin Mares <mj@ucw.cz>
+ * (c) 1997--2004 Martin Mares <mj@ucw.cz>
+ *
+ * This software may be freely distributed and used according to the terms
+ * of the GNU Lesser General Public License.
*/
#include "lib/lib.h"
#include "lib/fastbuf.h"
#include "lib/lfs.h"
-#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
+struct fb_file {
+ struct fastbuf fb;
+ int fd; /* File descriptor, -1 if not a real file */
+ int is_temp_file; /* 0=normal file, 1=temporary file, delete on close, -1=shared FD */
+};
+#define FB_FILE(f) ((struct fb_file *)(f)->is_fastbuf)
+#define FB_BUFFER(f) (byte *)(FB_FILE(f) + 1)
+
static int
bfd_refill(struct fastbuf *f)
{
- int l = read(f->fd, f->buffer, f->buflen);
-
+ f->bptr = f->buffer = FB_BUFFER(f);
+ int l = read(FB_FILE(f)->fd, f->buffer, f->bufend-f->buffer);
if (l < 0)
die("Error reading %s: %m", f->name);
- f->bptr = f->buffer;
f->bstop = f->buffer + l;
- f->pos = f->fdpos;
- f->fdpos += l;
+ f->pos += l;
return l;
}
bfd_spout(struct fastbuf *f)
{
int l = f->bptr - f->buffer;
- char *c = f->buffer;
+ byte *c = f->buffer;
+ f->pos += l;
while (l)
{
- int z = write(f->fd, c, l);
+ int z = write(FB_FILE(f)->fd, c, l);
if (z <= 0)
die("Error writing %s: %m", f->name);
- f->fdpos += z;
l -= z;
c += z;
}
- f->bptr = f->buffer;
- f->pos = f->fdpos;
+ f->bptr = f->buffer = FB_BUFFER(f);
}
static void
{
sh_off_t l;
- if (whence == SEEK_SET && pos == f->fdpos)
+ if (whence == SEEK_SET && pos == f->pos)
return;
- l = sh_seek(f->fd, pos, whence);
+ l = sh_seek(FB_FILE(f)->fd, pos, whence);
if (l < 0)
die("lseek on %s: %m", f->name);
- f->fdpos = f->pos = l;
+ f->pos = l;
}
static void
bfd_close(struct fastbuf *f)
{
- close(f->fd);
- if (f->is_temp_file && unlink(f->name) < 0)
- die("unlink(%s): %m", f->name);
+ switch (FB_FILE(f)->is_temp_file)
+ {
+ case 1:
+ if (unlink(f->name) < 0)
+ log(L_ERROR, "unlink(%s): %m", f->name);
+ case 0:
+ close(FB_FILE(f)->fd);
+ }
+ xfree(f);
+}
+
+static int
+bfd_config(struct fastbuf *f, uns item, int value)
+{
+ switch (item)
+ {
+ case BCONFIG_IS_TEMP_FILE:
+ FB_FILE(f)->is_temp_file = value;
+ return 0;
+ default:
+ return -1;
+ }
}
static struct fastbuf *
bfdopen_internal(int fd, uns buflen, byte *name)
{
int namelen = strlen(name) + 1;
- struct fastbuf *b = xmalloc_zero(sizeof(struct fastbuf) + buflen + namelen);
-
- b->buflen = buflen;
- b->buffer = (char *)(b+1);
- b->bptr = b->bstop = b->buffer;
- b->bufend = b->buffer + buflen;
- b->name = b->bufend;
- strcpy(b->name, name);
- b->fd = fd;
- b->refill = bfd_refill;
- b->spout = bfd_spout;
- b->seek = bfd_seek;
- b->close = bfd_close;
- return b;
+ struct fb_file *F = xmalloc(sizeof(struct fb_file) + buflen + namelen);
+ struct fastbuf *f = &F->fb;
+
+ bzero(F, sizeof(*F));
+ f->buffer = (byte *)(F+1);
+ f->bptr = f->bstop = f->buffer;
+ f->bufend = f->buffer + buflen;
+ f->name = f->bufend;
+ memcpy(f->name, name, namelen);
+ F->fd = fd;
+ f->refill = bfd_refill;
+ f->spout = bfd_spout;
+ f->seek = bfd_seek;
+ f->close = bfd_close;
+ f->config = bfd_config;
+ f->can_overwrite_buffer = 2;
+ return f;
}
struct fastbuf *
-bopen(byte *name, uns mode, uns buffer)
+bopen(byte *name, uns mode, uns buflen)
{
- int fd = sh_open(name, mode, 0666);
+ struct fastbuf *b;
+ int fd;
+
+ if (!buflen)
+ return bopen_mm(name, mode);
+ fd = sh_open(name, mode, 0666);
if (fd < 0)
die("Unable to %s file %s: %m",
(mode & O_CREAT) ? "create" : "open", name);
- return bfdopen_internal(fd, buffer, name);
+ b = bfdopen_internal(fd, buflen, name);
+ if (mode & O_APPEND)
+ bfd_seek(b, 0, SEEK_END);
+ return b;
}
struct fastbuf *
-bfdopen(int fd, uns buffer)
+bfdopen(int fd, uns buflen)
{
byte x[32];
sprintf(x, "fd%d", fd);
- return bfdopen_internal(fd, buffer, x);
+ return bfdopen_internal(fd, buflen, x);
}
-void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
+struct fastbuf *
+bfdopen_shared(int fd, uns buflen)
{
- uns rf = f->bstop - f->bptr;
+ struct fastbuf *f = bfdopen(fd, buflen);
+ FB_FILE(f)->is_temp_file = -1;
+ return f;
+}
- if (!l)
- return;
- if (rf)
- {
- uns k = (rf <= l) ? rf : l;
- bwrite(t, f->bptr, k);
- f->bptr += k;
- l -= k;
- }
- while (l >= t->buflen)
- {
- t->spout(t);
- if ((uns) read(f->fd, t->buffer, t->buflen) != t->buflen)
- die("bbcopy: %s exhausted", f->name);
- f->pos = f->fdpos;
- f->fdpos += t->buflen;
- f->bstop = f->bptr = f->buffer;
- t->bptr = t->bufend;
- l -= t->buflen;
- }
- while (l)
- {
- uns k = t->bufend - t->bptr;
-
- if (!k)
- {
- t->spout(t);
- k = t->bufend - t->bptr;
- }
- if (k > l)
- k = l;
- bread(f, t->bptr, k);
- t->bptr += k;
- l -= k;
- }
+void
+bfilesync(struct fastbuf *b)
+{
+ bflush(b);
+ if (fsync(FB_FILE(b)->fd) < 0)
+ log(L_ERROR, "fsync(%s) failed: %m", b->name);
}
#ifdef TEST
int main(int argc, char **argv)
{
struct fastbuf *f, *t;
- int c;
f = bopen("/etc/profile", O_RDONLY, 16);
t = bfdopen(1, 13);
bbcopy(f, t, 100);
+ printf("%d %d\n", (int)btell(f), (int)btell(t));
bclose(f);
bclose(t);
+ return 0;
}
#endif