X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;ds=inline;f=lib%2Ffb-file.c;h=cbfe1b0ca31fc2771a92a4a5a5670d8f27a6d4b8;hb=ff36b07f44efa12a78809ee05bd6d0c25fc60495;hp=1fc85de5ed500445ec178a237961ce9bdd63a974;hpb=cf83e06e4158617e981fef5bf3878315b23bf7f9;p=libucw.git diff --git a/lib/fb-file.c b/lib/fb-file.c index 1fc85de5..cbfe1b0c 100644 --- a/lib/fb-file.c +++ b/lib/fb-file.c @@ -1,7 +1,7 @@ /* - * Sherlock Library -- Fast Buffered I/O on Files + * UCW Library -- Fast Buffered I/O on Files * - * (c) 1997--2002 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. @@ -16,13 +16,21 @@ #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) { + 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 += l; return l; @@ -32,7 +40,7 @@ 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) @@ -43,7 +51,7 @@ bfd_spout(struct fastbuf *f) l -= z; c += z; } - f->bptr = f->buffer; + f->bptr = f->buffer = FB_BUFFER(f); } static void @@ -63,12 +71,30 @@ bfd_seek(struct fastbuf *f, sh_off_t pos, int whence) static void bfd_close(struct fastbuf *f) { - close(FB_FILE(f)->fd); - if (FB_FILE(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) { @@ -77,7 +103,7 @@ bfdopen_internal(int fd, uns buflen, byte *name) struct fastbuf *f = &F->fb; bzero(F, sizeof(*F)); - f->buffer = (char *)(F+1); + f->buffer = (byte *)(F+1); f->bptr = f->bstop = f->buffer; f->bufend = f->buffer + buflen; f->name = f->bufend; @@ -87,75 +113,52 @@ bfdopen_internal(int fd, uns buflen, byte *name) 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) { struct fastbuf *b; - int fd = sh_open(name, mode, 0666); + 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); - b = 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; - uns tbuflen = t->bufend - t->buffer; + struct fastbuf *f = bfdopen(fd, buflen); + FB_FILE(f)->is_temp_file = -1; + return f; +} - ASSERT(f->close == bfd_close); - ASSERT(t->close == bfd_close); - if (!l) - return; - if (rf) - { - uns k = MIN(rf, l); - bwrite(t, f->bptr, k); - f->bptr += k; - l -= k; - if (!l) - return; - } - while (l >= tbuflen) - { - t->spout(t); - if ((uns) read(FB_FILE(f)->fd, t->buffer, tbuflen) != tbuflen) - die("bbcopy: %s exhausted", f->name); - f->pos += tbuflen; - f->bstop = f->bptr = f->buffer; - t->bptr = t->bufend; - l -= tbuflen; - } - 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