chiark / gitweb /
Compress _pages.ppm for faster execution (less disk traffic)
[ypp-sc-tools.web-live.git] / pctb / convert.c
index 366ea725191132f4fbc719196d70cec2f2a5765f..4ded228f9d130ffb7a40bfb2758872afe6678b4f 100644 (file)
@@ -81,28 +81,13 @@ static void vbadusage(const char *fmt, va_list al) {
 }
 DEFINE_VWRAPPERF(static, badusage, NORET);
 
-static void open_screenshot_file(const char *mode) {
+static void open_screenshot_file(int for_write) {
   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") );
-
+    int mode= for_write ? O_WRONLY|O_CREAT|O_TRUNC : O_RDONLY;
+    sysassert(! gzopen(o_screenshot_fn, mode, &screenshot_file,
+                      &screenshot_compressor, "-1") );
   } else {
-    screenshot_file= fopen(o_screenshot_fn, mode);
+    screenshot_file= fopen(o_screenshot_fn, for_write ? "w" : "r");
     if (!screenshot_file)
       fatal("could not open screenshots file `%s': %s",
            o_screenshot_fn, strerror(errno));
@@ -298,8 +283,7 @@ 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%s", get_vardir(),
-                              o_mode & mf_readscreenshot ? "" : ".gz");
+    o_screenshot_fn= masprintf("%s/_pages.ppm.gz", get_vardir());
 
   /* Actually do the work */
             
@@ -328,18 +312,21 @@ int main(int argc, char **argv) {
   }
 
   if (o_mode & mf_screenshot) {
-    open_screenshot_file("w");
+    open_screenshot_file(1);
     if (o_flags & ff_singlepage) take_one_screenshot();
     else take_screenshots();
     progress_log("OK for you to move the mouse now, and you can"
                 " use the YPP client again.");
+    progress("Finishing handling screenshots...");
+    gzclose(&screenshot_file,&screenshot_compressor,"screenshots output");
   }
   if (o_mode & mf_readscreenshot) {
     if ((o_flags & ff_upload) && !(o_flags & ff_testservers))
       badusage("must not reuse screenshots for upload to live PCTB database");
-    open_screenshot_file("r");
+    open_screenshot_file(0);
     if (o_flags & ff_singlepage) read_one_screenshot();
     else read_screenshots();
+    gzclose(&screenshot_file,&screenshot_compressor,"screenshots input");
   }
   if (o_mode & mf_analyse) {
     if (o_flags & ff_needisland) {
@@ -355,10 +342,6 @@ 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;
 }