[4.1] 59b47f8 Add std.file_exists(path) to check if path exists
PÃ¥l Hermunn Johansen
hermunn at varnish-software.com
Tue Apr 25 17:39:05 CEST 2017
commit 59b47f863ecbfad591ad2b665fcd51a76bc2f8cc
Author: Jonathan Huot <jonathan.huot at thomsonreuters.com>
Date: Wed Mar 29 14:59:14 2017 +0200
Add std.file_exists(path) to check if path exists
Conflicts:
lib/libvmod_std/vmod.vcc
lib/libvmod_std/vmod_std.c
diff --git a/bin/varnishtest/tests/m00029.vtc b/bin/varnishtest/tests/m00029.vtc
new file mode 100644
index 0000000..fbae76e
--- /dev/null
+++ b/bin/varnishtest/tests/m00029.vtc
@@ -0,0 +1,22 @@
+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
diff --git a/lib/libvmod_std/vmod.vcc b/lib/libvmod_std/vmod.vcc
index 97d3b5e..e7f1d6f 100644
--- a/lib/libvmod_std/vmod.vcc
+++ b/lib/libvmod_std/vmod.vcc
@@ -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
diff --git a/lib/libvmod_std/vmod_std.c b/lib/libvmod_std/vmod_std.c
index 685a348..2539fc0 100644
--- a/lib/libvmod_std/vmod_std.c
+++ b/lib/libvmod_std/vmod_std.c
@@ -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)
{
More information about the varnish-commit
mailing list