From 3b0d474dd52fab80a5adca3b4b82c2db47a88657 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Mon, 23 Sep 2002 21:20:02 +0000 Subject: [PATCH] Introduced bfdopen_shared() which behaves like bfdopen(), but on bclose() the fd is left open. Especially useful for buffering stdin/out. --- lib/buckettool.c | 4 ++-- lib/fastbuf.h | 3 ++- lib/fb-file.c | 19 ++++++++++++++++--- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/buckettool.c b/lib/buckettool.c index b2f51715..ae55b3ca 100644 --- a/lib/buckettool.c +++ b/lib/buckettool.c @@ -108,7 +108,7 @@ insert(void) struct obuck_header h; byte *e; - in = bfdopen(0, 4096); + in = bfdopen_shared(0, 4096); obuck_init(1); do { @@ -123,7 +123,7 @@ insert(void) } while (e); obuck_cleanup(); - /* bclose(in) not done, we don't want fd 0 closed */ + bclose(in); } static void diff --git a/lib/fastbuf.h b/lib/fastbuf.h index f3a05952..7baa5013 100644 --- a/lib/fastbuf.h +++ b/lib/fastbuf.h @@ -69,13 +69,14 @@ struct fastbuf { struct fb_file { struct fastbuf fb; int fd; /* File descriptor, -1 if not a real file */ - int is_temp_file; /* Is a temporary file, delete on close */ + 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) struct fastbuf *bopen(byte *name, uns mode, uns buffer); struct fastbuf *bopen_tmp(uns buffer); struct fastbuf *bfdopen(int fd, uns buffer); +struct fastbuf *bfdopen_shared(int fd, uns buffer); void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l); #define FB_IS_TEMP_FILE(f) FB_FILE(f)->is_temp_file diff --git a/lib/fb-file.c b/lib/fb-file.c index 1fc85de5..73a2f143 100644 --- a/lib/fb-file.c +++ b/lib/fb-file.c @@ -63,9 +63,14 @@ 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); } @@ -113,6 +118,14 @@ bfdopen(int fd, uns buffer) return bfdopen_internal(fd, buffer, x); } +struct fastbuf * +bfdopen_shared(int fd, uns buffer) +{ + struct fastbuf *f = bfdopen(fd, buffer); + FB_FILE(f)->is_temp_file = -1; + return f; +} + void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l) { uns rf = f->bstop - f->bptr; -- 2.39.2