X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Ffb-file.c;h=1f6dcb2eeab3632f709c04659cbc6dc6a8062af6;hb=c47f8dd7a1e5a7fface9512f578b3451e16f7e04;hp=9ef992a8b6bf9cf80f3006a579f4000923ddfbc6;hpb=5d4c3f3f01730c49b82f9212749ce28f6f808658;p=libucw.git diff --git a/lib/fb-file.c b/lib/fb-file.c index 9ef992a8..1f6dcb2e 100644 --- a/lib/fb-file.c +++ b/lib/fb-file.c @@ -11,13 +11,14 @@ #include "lib/fastbuf.h" #include "lib/lfs.h" +#include #include #include #include struct fb_file { struct fastbuf fb; - int fd; /* File descriptor, -1 if not a real file */ + int fd; /* File descriptor */ 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) @@ -53,18 +54,14 @@ bfd_spout(struct fastbuf *f) f->bptr = f->buffer = FB_BUFFER(f); } -static void +static int bfd_seek(struct fastbuf *f, sh_off_t pos, int whence) { - sh_off_t l; - - if (whence == SEEK_SET && pos == f->pos) - return; - - l = sh_seek(FB_FILE(f)->fd, pos, whence); + sh_off_t l = sh_seek(FB_FILE(f)->fd, pos, whence); if (l < 0) - die("lseek on %s: %m", f->name); + return 0; f->pos = l; + return 1; } static void @@ -94,9 +91,10 @@ bfd_config(struct fastbuf *f, uns item, int value) } } -static struct fastbuf * -bfdopen_internal(int fd, uns buflen, byte *name) +struct fastbuf * +bfdopen_internal(int fd, byte *name, uns buflen) { + ASSERT(buflen); int namelen = strlen(name) + 1; struct fb_file *F = xmalloc(sizeof(struct fb_file) + buflen + namelen); struct fastbuf *f = &F->fb; @@ -120,34 +118,19 @@ bfdopen_internal(int fd, uns buflen, byte *name) struct fastbuf * bopen_try(byte *name, uns mode, uns buflen) { - int fd = sh_open(name, mode, 0666); - if (fd < 0) - return NULL; - struct fastbuf *b = bfdopen_internal(fd, buflen, name); - if (mode & O_APPEND) - bfd_seek(b, 0, SEEK_END); - return b; + return bopen_file_try(name, mode, &(struct fb_params){ .type = FB_STD, .buffer_size = buflen }); } struct fastbuf * bopen(byte *name, uns mode, uns buflen) { - if (!buflen) - return bopen_mm(name, mode); - struct fastbuf *b = bopen_try(name, mode, buflen); - if (!b) - die("Unable to %s file %s: %m", - (mode & O_CREAT) ? "create" : "open", name); - return b; + return bopen_file(name, mode, &(struct fb_params){ .type = FB_STD, .buffer_size = buflen }); } struct fastbuf * bfdopen(int fd, uns buflen) { - byte x[32]; - - sprintf(x, "fd%d", fd); - return bfdopen_internal(fd, buflen, x); + return bopen_fd(fd, &(struct fb_params){ .type = FB_STD, .buffer_size = buflen }); } struct fastbuf * @@ -168,7 +151,7 @@ bfilesync(struct fastbuf *b) #ifdef TEST -int main(int argc, char **argv) +int main(int argc UNUSED, char **argv UNUSED) { struct fastbuf *f, *t;