X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;ds=sidebyside;f=lib%2Ffb-file.c;h=915c1b40f78018ff4478eae894af59d487ca9fd7;hb=997624f88f37bec5cb96cf9fc3b3cac05ccc6ed8;hp=d89522ef53c982ba6bf3cf6303fbfe55c4178af2;hpb=1571781022499a9d0c32d249f89945d034d1cbff;p=libucw.git diff --git a/lib/fb-file.c b/lib/fb-file.c index d89522ef..915c1b40 100644 --- a/lib/fb-file.c +++ b/lib/fb-file.c @@ -1,29 +1,38 @@ /* * Sherlock Library -- Fast Buffered I/O on Files * - * (c) 1997--2000 Martin Mares + * (c) 1997--2004 Martin Mares + * + * 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 +#include #include #include #include +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; } @@ -31,19 +40,18 @@ static void 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 @@ -51,100 +59,98 @@ bfd_seek(struct fastbuf *f, sh_off_t pos, int whence) { 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); + 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(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->pos = b->fdpos = 0; - 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; - - 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; - } + struct fastbuf *f = bfdopen(fd, buflen); + FB_FILE(f)->is_temp_file = -1; + return f; } #ifdef TEST @@ -152,13 +158,14 @@ void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l) 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