chiark / gitweb /
MacOS support.
authorRichard Kettlewell <richard@kakajou.wlan.anjou.terraraq.org.uk>
Wed, 15 Jun 2011 21:44:50 +0000 (22:44 +0100)
committerRichard Kettlewell <richard@kakajou.wlan.anjou.terraraq.org.uk>
Wed, 15 Jun 2011 21:44:50 +0000 (22:44 +0100)
Uses purge to implement --flush.

configure.ac
vbig.1
vbig.cc

index 58f96cb8714f7a829246eb80e57df772cf91e9a3..01285c3996fcfd659110bda61a135569af32fb95 100644 (file)
@@ -5,6 +5,16 @@ AM_INIT_AUTOMAKE([1.10])
 m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES])
 AC_CONFIG_SRCDIR([vbig.cc])
 AM_CONFIG_HEADER([config.h])
+AC_CANONICAL_HOST
+case "$host" in
+*-*-darwin* )
+  AC_DEFINE([PURGE_COMMAND],["purge"], [Command to purge filesystem cache])
+  ;;
+*-*-linux* )
+  AC_DEFINE([DROP_CACHE_FILE],["/proc/sys/vm/drop_caches"],
+           [Path to filesystem cache purge interface])
+  ;;
+esac
 AC_LANG([C++])
 AC_PROG_CXX
 AC_PROG_RANLIB
diff --git a/vbig.1 b/vbig.1
index 11cd637d02905f82d0798cbeea2f25ada8796281..1c8216f981b629bec3a237a665bd6f061d471c30 100644 (file)
--- a/vbig.1
+++ b/vbig.1
@@ -32,7 +32,7 @@ by the equivalent \fB--create\fR call.
 .TP
 .B --flush\fR, \fB-f
 Flush cached data after creating the file or before verifying it.
-Only root can use this option.
+On some platforms, only root can use this option.
 .TP
 .B --help\fR, \fB-h
 Displays a usage message.
diff --git a/vbig.cc b/vbig.cc
index cc81de96ba340e005a5f0ec92982ff2da8246603..54f3ea10b524640730b716524848a0c10cac17e7 100644 (file)
--- a/vbig.cc
+++ b/vbig.cc
@@ -9,9 +9,6 @@
 #include <fcntl.h>
 #include "Arcfour.h"
 
-// Path to magic file to drop filesystem caches
-static const char dropCaches[] = "/proc/sys/vm/drop_caches";
-
 // Command line options
 const struct option opts[] = {
   { "seed", required_argument, 0, 's' },
@@ -65,12 +62,27 @@ static void flushCache(FILE *fp) {
   // synced.
   if(fsync(fileno(fp)) < 0)
     fatal(errno, "fsync");
+#if defined DROP_CACHE_FILE
   int fd;
-  if((fd = open(dropCaches, O_WRONLY, 0)) < 0)
-    fatal(errno, "%s", dropCaches);
+  if((fd = open(DROP_CACHE_FILE, O_WRONLY, 0)) < 0)
+    fatal(errno, "%s", DROP_CACHE_FILE);
   if(write(fd, "3\n", 2) < 0)
-    fatal(errno, "%s", dropCaches);
+    fatal(errno, "%s", DROP_CACHE_FILE);
   close(fd);
+#elif defined PURGE_COMMAND
+  int rc;
+  if((rc = system(PURGE_COMMAND)) < 0)
+    fatal(errno, "executing %s", PURGE_COMMAND);
+  else if(rc) {
+    if(WIFSIGNALED(rc)) {
+      fprintf(stderr, "%s%s\n", 
+             strsignal(WTERMSIG(rc)),
+             WCOREDUMP(rc) ? " (core dumped)" : "");
+      exit(WTERMSIG(rc) + 128);
+    } else
+      exit(WEXITSTATUS(rc));
+  }
+#endif
 }
 
 int main(int argc, char **argv) {