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)
}
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);
......
......@@ -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));
......
......@@ -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);
......
......@@ -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;
......
......@@ -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));
......
......@@ -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++;
......
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