Commit 59b47f86 authored by Jonathan Huot's avatar Jonathan Huot Committed by Pål Hermunn Johansen

Add std.file_exists(path) to check if path exists

Conflicts:
	lib/libvmod_std/vmod.vcc
	lib/libvmod_std/vmod_std.c
parent 7fc09f9e
varnishtest "Test std.file_exists"
server s1 {
rxreq
txresp
} -start
varnish v1 -vcl+backend {
import ${vmod_std};
sub vcl_deliver {
set resp.http.existsA = std.file_exists("/non/existent");
set resp.http.existsB = std.file_exists("${tmpdir}/v1/_.vsm");
}
} -start
client c1 {
txreq
rxresp
expect resp.http.existsA == "false"
expect resp.http.existsB == "true"
} -run
......@@ -104,6 +104,16 @@ Description
Example
set beresp.http.served-by = std.fileread("/etc/hostname");
$Function BOOL file_exists(STRING path)
Description
Returns `true` if path or the file pointed to by path exists,
`false` otherwise.
Example
| if (std.file_exists("/etc/return_503")) {
| return (synth(503, "Varnish is in maintenance"));
| }
$Function VOID collect(HEADER hdr)
Description
......
......@@ -28,14 +28,17 @@
#include "config.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdlib.h>
#include <syslog.h>
#include <unistd.h>
#include "vrt.h"
#include "vtcp.h"
......@@ -176,6 +179,15 @@ vmod_syslog(VRT_CTX, VCL_INT fac, const char *fmt, ...)
va_end(ap);
}
VCL_BOOL __match_proto__(td_std_file_exists)
vmod_file_exists(VRT_CTX, VCL_STRING file_name)
{
struct stat st;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
return (stat(file_name, &st) == 0);
}
VCL_VOID __match_proto__(td_std_collect)
vmod_collect(VRT_CTX, VCL_HEADER hdr)
{
......
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