Mailing List Archive

r3414 - in trunk/varnish-cache: include lib/libvarnish lib/libvcl
Author: phk
Date: 2008-11-21 12:32:56 +0100 (Fri, 21 Nov 2008)
New Revision: 3414

Modified:
trunk/varnish-cache/include/libvarnish.h
trunk/varnish-cache/lib/libvarnish/vtmpfile.c
trunk/varnish-cache/lib/libvcl/vcc_compile.c
Log:
Add a vreadfile() utility function, which reads a file into malloc'ed
memory



Modified: trunk/varnish-cache/include/libvarnish.h
===================================================================
--- trunk/varnish-cache/include/libvarnish.h 2008-11-21 09:50:22 UTC (rev 3413)
+++ trunk/varnish-cache/include/libvarnish.h 2008-11-21 11:32:56 UTC (rev 3414)
@@ -86,6 +86,7 @@

/* from libvarnish/vtmpfile.c */
int vtmpfile(char *);
+char *vreadfile(int fd);

/*
* assert(), AN() and AZ() are static checks that should not happen.

Modified: trunk/varnish-cache/lib/libvarnish/vtmpfile.c
===================================================================
--- trunk/varnish-cache/lib/libvarnish/vtmpfile.c 2008-11-21 09:50:22 UTC (rev 3413)
+++ trunk/varnish-cache/lib/libvarnish/vtmpfile.c 2008-11-21 11:32:56 UTC (rev 3414)
@@ -35,7 +35,10 @@
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
+#include <unistd.h>

+#include <sys/stat.h>
+
#include "libvarnish.h"

int
@@ -74,3 +77,21 @@
}
/* 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);
+}

Modified: trunk/varnish-cache/lib/libvcl/vcc_compile.c
===================================================================
--- trunk/varnish-cache/lib/libvcl/vcc_compile.c 2008-11-21 09:50:22 UTC (rev 3413)
+++ trunk/varnish-cache/lib/libvcl/vcc_compile.c 2008-11-21 11:32:56 UTC (rev 3414)
@@ -402,8 +402,6 @@
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 @@
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);
}