Commit 35b17acf authored by Dridi Boukelmoune's avatar Dridi Boukelmoune Committed by guillaume quintard

A VFIL_writefile function similar to VFIL_readfile

parent ecf13bf2
......@@ -33,6 +33,7 @@ struct vfil_path;
/* from libvarnish/vfil.c */
int seed_random(void);
char *VFIL_readfile(const char *pfx, const char *fn, ssize_t *sz);
int VFIL_writefile(const char *pfx, const char *fn, const char *buf, size_t sz);
int VFIL_nonblocking(int fd);
int VFIL_fsinfo(int fd, unsigned *pbs, uintmax_t *size, uintmax_t *space);
int VFIL_allocate(int fd, off_t size, int insist);
......
......@@ -85,6 +85,24 @@ vfil_readfd(int fd, ssize_t *sz)
return (f);
}
static int
vfil_writefd(int fd, const char *buf, size_t sz)
{
ssize_t len;
while (sz > 0) {
len = write(fd, buf, sz);
if (len < 0)
return (len);
if (len == 0)
break;
buf += len;
sz -= len;
}
return (sz == 0 ? 0 : -1);
}
static int
vfil_openfile(const char *pfx, const char *fn, int flags, int mode)
{
......@@ -118,6 +136,22 @@ VFIL_readfile(const char *pfx, const char *fn, ssize_t *sz)
return (r);
}
int
VFIL_writefile(const char *pfx, const char *fn, const char *buf, size_t sz)
{
int fd, err;
int r;
fd = vfil_openfile(pfx, fn, O_WRONLY|O_CREAT|O_TRUNC, 0660);
if (fd < 0)
return (fd);
r = vfil_writefd(fd, buf, sz);
err = errno;
AZ(close(fd));
errno = err;
return (r);
}
int
VFIL_nonblocking(int fd)
{
......
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