[master] e45eb41 Give vreadfile() a size pointer return argument.

Poul-Henning Kamp phk at varnish-cache.org
Wed May 11 10:17:10 CEST 2011


commit e45eb41e0cf92804c79cb21db68e33986c50563c
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Wed May 11 08:16:19 2011 +0000

    Give vreadfile() a size pointer return argument.
    
    Expose vreadfd(), also with a size pointer return argument.
    
    Use vreadfd() in std.fileread()

diff --git a/bin/varnishd/mgt_vcc.c b/bin/varnishd/mgt_vcc.c
index de70939..a3028ad 100644
--- a/bin/varnishd/mgt_vcc.c
+++ b/bin/varnishd/mgt_vcc.c
@@ -252,7 +252,7 @@ mgt_run_cc(const char *vcl, struct vsb *sb, int C_flag)
 	}
 
 	if (C_flag) {
-		csrc = vreadfile(NULL, sf);
+		csrc = vreadfile(NULL, sf, NULL);
 		XXXAN(csrc);
 		(void)fputs(csrc, stdout);
 		free(csrc);
@@ -524,7 +524,7 @@ mcf_config_load(struct cli *cli, const char * const *av, void *priv)
 		return;
 	}
 
-	vcl = vreadfile(mgt_vcl_dir, av[3]);
+	vcl = vreadfile(mgt_vcl_dir, av[3], NULL);
 	if (vcl == NULL) {
 		cli_out(cli, "Cannot open '%s'", av[3]);
 		cli_result(cli, CLIS_PARAM);
diff --git a/bin/varnishd/varnishd.c b/bin/varnishd/varnishd.c
index aa9d876..7f423f1 100644
--- a/bin/varnishd/varnishd.c
+++ b/bin/varnishd/varnishd.c
@@ -553,7 +553,7 @@ main(int argc, char * const *argv)
 	}
 
 	if (f_arg != NULL) {
-		vcl = vreadfile(NULL, f_arg);
+		vcl = vreadfile(NULL, f_arg, NULL);
 		if (vcl == NULL) {
 			fprintf(stderr, "Cannot read '%s': %s\n",
 			    f_arg, strerror(errno));
diff --git a/include/libvarnish.h b/include/libvarnish.h
index e706e55..039e373 100644
--- a/include/libvarnish.h
+++ b/include/libvarnish.h
@@ -31,6 +31,7 @@
 #include <errno.h>
 #include <time.h>
 #include <stdint.h>
+#include <sys/types.h>
 
 #include "vas.h"
 
@@ -99,7 +100,8 @@ void varnish_version(const char *);
 
 /* from libvarnish/vtmpfile.c */
 int vtmpfile(char *);
-char *vreadfile(const char *pfx, const char *fn);
+char *vreadfile(const char *pfx, const char *fn, ssize_t *sz);
+char *vreadfd(int fd, ssize_t *sz);
 
 const char* vcs_version(void);
 
diff --git a/lib/libvarnish/vtmpfile.c b/lib/libvarnish/vtmpfile.c
index 9b5c1b2..99160c7 100644
--- a/lib/libvarnish/vtmpfile.c
+++ b/lib/libvarnish/vtmpfile.c
@@ -77,8 +77,8 @@ vtmpfile(char *template)
 	/* not reached */
 }
 
-static char *
-vreadfd(int fd)
+char *
+vreadfd(int fd, ssize_t *sz)
 {
 	struct stat st;
 	char *f;
@@ -92,11 +92,13 @@ vreadfd(int fd)
 	i = read(fd, f, st.st_size);
 	assert(i == st.st_size);
 	f[i] = '\0';
+	if (sz != NULL)
+		*sz = st.st_size;
 	return (f);
 }
 
 char *
-vreadfile(const char *pfx, const char *fn)
+vreadfile(const char *pfx, const char *fn, ssize_t *sz)
 {
 	int fd, err;
 	char *r;
@@ -111,7 +113,7 @@ vreadfile(const char *pfx, const char *fn)
 		fd = open(fn, O_RDONLY);
 	if (fd < 0)
 		return (NULL);
-	r = vreadfd(fd);
+	r = vreadfd(fd, sz);
 	err = errno;
 	AZ(close(fd));
 	errno = err;
diff --git a/lib/libvcl/vcc_compile.c b/lib/libvcl/vcc_compile.c
index 103c141..823b2a6 100644
--- a/lib/libvcl/vcc_compile.c
+++ b/lib/libvcl/vcc_compile.c
@@ -418,7 +418,7 @@ vcc_file_source(const struct vcc *tl, struct vsb *sb, const char *fn)
 	char *f;
 	struct source *sp;
 
-	f = vreadfile(tl->vcl_dir, fn);
+	f = vreadfile(tl->vcl_dir, fn, NULL);
 	if (f == NULL) {
 		vsb_printf(sb, "Cannot read file '%s': %s\n",
 		    fn, strerror(errno));
diff --git a/lib/libvmod_std/vmod_std_fileread.c b/lib/libvmod_std/vmod_std_fileread.c
index 9171a4b..22d05ad 100644
--- a/lib/libvmod_std/vmod_std_fileread.c
+++ b/lib/libvmod_std/vmod_std_fileread.c
@@ -151,14 +151,11 @@ vmod_fileread(struct sess *sp, struct vmod_priv *priv, const char *file_name)
 	iter->file_name = strdup(file_name);
 	iter->last_modification = buf.st_mtime;
 
-	iter->contents = malloc(buf.st_size + 1);
+	iter->contents = vreadfd(fd, &iter->file_sz);
 	AN(iter->contents);
-	iter->file_sz = read(fd, iter->contents, buf.st_size);
 	assert(iter->file_sz == buf.st_size);
 	AZ(close(fd));
 
-	iter->contents[iter->file_sz] = '\0';
-
 	VSLIST_INSERT_HEAD(list, iter, next);
 
 	filelist_update++;



More information about the varnish-commit mailing list