Commit e45eb41e authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Give vreadfile() a size pointer return argument.

Expose vreadfd(), also with a size pointer return argument.

Use vreadfd() in std.fileread()
parent b15c0184
...@@ -252,7 +252,7 @@ mgt_run_cc(const char *vcl, struct vsb *sb, int C_flag) ...@@ -252,7 +252,7 @@ mgt_run_cc(const char *vcl, struct vsb *sb, int C_flag)
} }
if (C_flag) { if (C_flag) {
csrc = vreadfile(NULL, sf); csrc = vreadfile(NULL, sf, NULL);
XXXAN(csrc); XXXAN(csrc);
(void)fputs(csrc, stdout); (void)fputs(csrc, stdout);
free(csrc); free(csrc);
...@@ -524,7 +524,7 @@ mcf_config_load(struct cli *cli, const char * const *av, void *priv) ...@@ -524,7 +524,7 @@ mcf_config_load(struct cli *cli, const char * const *av, void *priv)
return; return;
} }
vcl = vreadfile(mgt_vcl_dir, av[3]); vcl = vreadfile(mgt_vcl_dir, av[3], NULL);
if (vcl == NULL) { if (vcl == NULL) {
cli_out(cli, "Cannot open '%s'", av[3]); cli_out(cli, "Cannot open '%s'", av[3]);
cli_result(cli, CLIS_PARAM); cli_result(cli, CLIS_PARAM);
......
...@@ -553,7 +553,7 @@ main(int argc, char * const *argv) ...@@ -553,7 +553,7 @@ main(int argc, char * const *argv)
} }
if (f_arg != NULL) { if (f_arg != NULL) {
vcl = vreadfile(NULL, f_arg); vcl = vreadfile(NULL, f_arg, NULL);
if (vcl == NULL) { if (vcl == NULL) {
fprintf(stderr, "Cannot read '%s': %s\n", fprintf(stderr, "Cannot read '%s': %s\n",
f_arg, strerror(errno)); f_arg, strerror(errno));
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include <errno.h> #include <errno.h>
#include <time.h> #include <time.h>
#include <stdint.h> #include <stdint.h>
#include <sys/types.h>
#include "vas.h" #include "vas.h"
...@@ -99,7 +100,8 @@ void varnish_version(const char *); ...@@ -99,7 +100,8 @@ void varnish_version(const char *);
/* from libvarnish/vtmpfile.c */ /* from libvarnish/vtmpfile.c */
int vtmpfile(char *); 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); const char* vcs_version(void);
......
...@@ -77,8 +77,8 @@ vtmpfile(char *template) ...@@ -77,8 +77,8 @@ vtmpfile(char *template)
/* not reached */ /* not reached */
} }
static char * char *
vreadfd(int fd) vreadfd(int fd, ssize_t *sz)
{ {
struct stat st; struct stat st;
char *f; char *f;
...@@ -92,11 +92,13 @@ vreadfd(int fd) ...@@ -92,11 +92,13 @@ vreadfd(int fd)
i = read(fd, f, st.st_size); i = read(fd, f, st.st_size);
assert(i == st.st_size); assert(i == st.st_size);
f[i] = '\0'; f[i] = '\0';
if (sz != NULL)
*sz = st.st_size;
return (f); return (f);
} }
char * char *
vreadfile(const char *pfx, const char *fn) vreadfile(const char *pfx, const char *fn, ssize_t *sz)
{ {
int fd, err; int fd, err;
char *r; char *r;
...@@ -111,7 +113,7 @@ vreadfile(const char *pfx, const char *fn) ...@@ -111,7 +113,7 @@ vreadfile(const char *pfx, const char *fn)
fd = open(fn, O_RDONLY); fd = open(fn, O_RDONLY);
if (fd < 0) if (fd < 0)
return (NULL); return (NULL);
r = vreadfd(fd); r = vreadfd(fd, sz);
err = errno; err = errno;
AZ(close(fd)); AZ(close(fd));
errno = err; errno = err;
......
...@@ -418,7 +418,7 @@ vcc_file_source(const struct vcc *tl, struct vsb *sb, const char *fn) ...@@ -418,7 +418,7 @@ vcc_file_source(const struct vcc *tl, struct vsb *sb, const char *fn)
char *f; char *f;
struct source *sp; struct source *sp;
f = vreadfile(tl->vcl_dir, fn); f = vreadfile(tl->vcl_dir, fn, NULL);
if (f == NULL) { if (f == NULL) {
vsb_printf(sb, "Cannot read file '%s': %s\n", vsb_printf(sb, "Cannot read file '%s': %s\n",
fn, strerror(errno)); fn, strerror(errno));
......
...@@ -151,14 +151,11 @@ vmod_fileread(struct sess *sp, struct vmod_priv *priv, const char *file_name) ...@@ -151,14 +151,11 @@ vmod_fileread(struct sess *sp, struct vmod_priv *priv, const char *file_name)
iter->file_name = strdup(file_name); iter->file_name = strdup(file_name);
iter->last_modification = buf.st_mtime; iter->last_modification = buf.st_mtime;
iter->contents = malloc(buf.st_size + 1); iter->contents = vreadfd(fd, &iter->file_sz);
AN(iter->contents); AN(iter->contents);
iter->file_sz = read(fd, iter->contents, buf.st_size);
assert(iter->file_sz == buf.st_size); assert(iter->file_sz == buf.st_size);
AZ(close(fd)); AZ(close(fd));
iter->contents[iter->file_sz] = '\0';
VSLIST_INSERT_HEAD(list, iter, next); VSLIST_INSERT_HEAD(list, iter, next);
filelist_update++; filelist_update++;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment