X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Ffb-file.c;h=0dcf22a958978a2d0fe0ea0ab97ca94137f27d62;hb=2bc83c3abf7a59da99f7f713927ec36abb994093;hp=9caf9a189aaf45c33a2b7f83dc6381ab45bac0c9;hpb=59e023bb0dc1868bb2b58d1a648e2c9d9dbb4c0e;p=libucw.git diff --git a/lib/fb-file.c b/lib/fb-file.c index 9caf9a18..0dcf22a9 100644 --- a/lib/fb-file.c +++ b/lib/fb-file.c @@ -17,7 +17,7 @@ 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) @@ -56,12 +56,10 @@ bfd_spout(struct fastbuf *f) static void 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); f->pos = l; @@ -118,20 +116,26 @@ bfdopen_internal(int fd, uns buflen, byte *name) } struct fastbuf * -bopen(byte *name, uns mode, uns buflen) +bopen_try(byte *name, uns mode, uns buflen) { - struct fastbuf *b; - int fd; + 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; +} +struct fastbuf * +bopen(byte *name, uns mode, uns buflen) +{ if (!buflen) return bopen_mm(name, mode); - fd = sh_open(name, mode, 0666); - if (fd < 0) + struct fastbuf *b = bopen_try(name, mode, buflen); + if (!b) die("Unable to %s file %s: %m", (mode & O_CREAT) ? "create" : "open", name); - b = bfdopen_internal(fd, buflen, name); - if (mode & O_APPEND) - bfd_seek(b, 0, SEEK_END); return b; } @@ -162,7 +166,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;