From dbfb5db5873040a8efabceb95171d4c90690f9ea Mon Sep 17 00:00:00 2001 From: phk Date: Fri, 21 Nov 2008 11:32:56 +0000 Subject: [PATCH] Add a vreadfile() utility function, which reads a file into malloc'ed memory git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@3414 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/include/libvarnish.h | 1 + varnish-cache/lib/libvarnish/vtmpfile.c | 21 +++++++++++++++++++++ varnish-cache/lib/libvcl/vcc_compile.c | 17 +++-------------- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/varnish-cache/include/libvarnish.h b/varnish-cache/include/libvarnish.h index c023fbeb..99ef27d8 100644 --- a/varnish-cache/include/libvarnish.h +++ b/varnish-cache/include/libvarnish.h @@ -86,6 +86,7 @@ void varnish_version(const char *); /* from libvarnish/vtmpfile.c */ int vtmpfile(char *); +char *vreadfile(int fd); /* * assert(), AN() and AZ() are static checks that should not happen. diff --git a/varnish-cache/lib/libvarnish/vtmpfile.c b/varnish-cache/lib/libvarnish/vtmpfile.c index 3e251485..733efa26 100644 --- a/varnish-cache/lib/libvarnish/vtmpfile.c +++ b/varnish-cache/lib/libvarnish/vtmpfile.c @@ -35,6 +35,9 @@ #include #include #include +#include + +#include #include "libvarnish.h" @@ -74,3 +77,21 @@ vtmpfile(char *template) } /* not reached */ } + +char * +vreadfile(int fd) +{ + struct stat st; + char *f; + int i; + + assert(0 == fstat(fd, &st)); + if (!S_ISREG(st.st_mode)) + return (NULL); + f = malloc(st.st_size + 1); + assert(f != NULL); + i = read(fd, f, st.st_size); + assert(i == st.st_size); + f[i] = '\0'; + return (f); +} diff --git a/varnish-cache/lib/libvcl/vcc_compile.c b/varnish-cache/lib/libvcl/vcc_compile.c index 0db0c569..a7674ce5 100644 --- a/varnish-cache/lib/libvcl/vcc_compile.c +++ b/varnish-cache/lib/libvcl/vcc_compile.c @@ -402,8 +402,6 @@ static struct source * vcc_file_source(struct vsb *sb, const char *fn, int fd) { char *f; - int i; - struct stat st; struct source *sp; if (fd < 0) { @@ -414,19 +412,10 @@ vcc_file_source(struct vsb *sb, const char *fn, int fd) return (NULL); } } - assert(0 == fstat(fd, &st)); - if (! S_ISREG(st.st_mode)) { - vsb_printf(sb, "File '%s' is not a regular file\n", fn); - AZ(close(fd)); - return (NULL); - } - f = malloc(st.st_size + 1); - assert(f != NULL); - i = read(fd, f, st.st_size); - assert(i == st.st_size); + f = vreadfile(fd); + AN(f); AZ(close(fd)); - f[i] = '\0'; - sp = vcc_new_source(f, f + i, fn); + sp = vcc_new_source(f, NULL, fn); sp->freeit = f; return (sp); } -- 2.39.5