chiark / gitweb /
Compress the screenshots file on output
authorIan Jackson <ian@liberator.relativity.greenend.org.uk>
Thu, 9 Jul 2009 18:43:26 +0000 (19:43 +0100)
committerIan Jackson <ian@liberator.relativity.greenend.org.uk>
Thu, 9 Jul 2009 18:43:26 +0000 (19:43 +0100)
pctb/common.h
pctb/convert.c

index 92ba2daa13d65f27cdf97fe332c48c8ee3ef5108..699d1619d0099962a48b7adc5161f46b4e5ef557 100644 (file)
@@ -42,6 +42,7 @@
 #include <unistd.h>
 #include <dirent.h>
 #include <inttypes.h>
+#include <fnmatch.h>
 
 #include <pcre.h>
 
index e6e0102e6b02b69fb7031d52de2a43a80dc03e18..366ea725191132f4fbc719196d70cec2f2a5765f 100644 (file)
@@ -66,6 +66,8 @@ FILE *screenshot_file;
 const char *o_ocean, *o_pirate;
 int o_quiet;
 
+static pid_t screenshot_compressor=-1;
+
 enum flags o_flags=
    ff_charset_allowedit |
    ff_dict_fetch|ff_dict_submit|ff_dict_pirate;
@@ -80,10 +82,31 @@ static void vbadusage(const char *fmt, va_list al) {
 DEFINE_VWRAPPERF(static, badusage, NORET);
 
 static void open_screenshot_file(const char *mode) {
-  screenshot_file= fopen(o_screenshot_fn, mode);
-  if (!screenshot_file)
-    fatal("could not open screenshots file `%s': %s",
-         o_screenshot_fn, strerror(errno));
+  if (!fnmatch("*.gz",o_screenshot_fn,0)) {
+    int zfd, pipefds[2];
+    sysassert( (zfd= open(o_screenshot_fn, O_WRONLY|O_CREAT|O_TRUNC,
+                         0666)) >= 0);
+    sysassert(! pipe(pipefds) );
+    sysassert( (screenshot_compressor=fork()) != -1 );
+    if (!screenshot_compressor) {
+      sysassert( dup2(pipefds[0],0)==0 );
+      sysassert( dup2(zfd,1)==1 );
+      sysassert(! close(zfd) );
+      sysassert(! close(pipefds[0]) );
+      sysassert(! close(pipefds[1]) );
+      execlp("gzip","gzip","-1",(char*)0);
+      sysassert(!"execlp gzip for screenshots");
+    }
+    sysassert(! close(zfd) );
+    sysassert(! close(pipefds[0]) );
+    sysassert( screenshot_file= fdopen(pipefds[1], "w") );
+
+  } else {
+    screenshot_file= fopen(o_screenshot_fn, mode);
+    if (!screenshot_file)
+      fatal("could not open screenshots file `%s': %s",
+           o_screenshot_fn, strerror(errno));
+  }
 }
 
 static void run_analysis(void) {
@@ -275,7 +298,8 @@ int main(int argc, char **argv) {
             o_serv_dict_submit,  o_flags & ff_dict_submit);
 
   if (!o_screenshot_fn)
-    o_screenshot_fn= masprintf("%s/_pages.ppm",get_vardir());
+    o_screenshot_fn= masprintf("%s/_pages.ppm%s", get_vardir(),
+                              o_mode & mf_readscreenshot ? "" : ".gz");
 
   /* Actually do the work */
             
@@ -331,6 +355,10 @@ int main(int argc, char **argv) {
     default: abort();
     }
   }
+  if (screenshot_compressor!=-1) {
+    progress("Finishing compressing screenshots...");
+    waitpid_check_exitstatus(screenshot_compressor,"screenshots gzip",0);
+  }
   progress_log("Finished.");
   return 0;
 }