Could someone qualified to make changes to CVS please apply the attached patch?

Here is the background.

I tried to use cab task on Linux (with libcabinet), and ran into two problems.
1) Task executed, but no cab file was created
2) No diagnostic information (error messages, error code, etc.) were provided


After investigation, it appeared that "listcab" was executed in the wrong directory - it did not respect the "basedir" attribute. Therefore, listcab could not find the files it needed to archive. Also, the return code of the exec was not checked, and the output was not piped through to the parent process. This might have not bothered the author (and other users), because the basedir might have defaulted to the build's top-level directory.

The enclosed patch fixes both problems:
1) calling Runtime.getRuntime().exec("listcab", null, baseDir);
2) by asynchronously gobbling listcab's output and error, and displaying the result in case an error occurred


The file Cab.java uses different coding styles (for example, opening curly brace placment) in different regions, so I tried to adhere to the style of the region that I was working on. Hope I didn't offent anyone.

Module: jakarta-ant
File: src/main/org/apache/tools/ant/taskdefs/optional/Cab.java

Please, e-mail me if someone actually applies this patch.
Thanks.
--
Ilya

P.S.: It's a superb tool you've made, guys. I am using XSL/T to automatically generate buildfiles. Works like a song!


Index: Cab.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/Cab.java,v
retrieving revision 1.11
diff -u -r1.11 Cab.java
--- Cab.java    2001/11/02 16:36:49     1.11
+++ Cab.java    2001/11/15 19:08:18
@@ -64,11 +64,14 @@
 import org.apache.tools.ant.types.Commandline;
 import org.apache.tools.ant.util.FileUtils;
 
+import java.io.BufferedReader;
 import java.io.File;
-import java.io.IOException;
-import java.io.PrintWriter;
 import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.io.OutputStream;
+import java.io.PrintWriter;
 import java.util.Enumeration;
 
 import java.util.Vector;
@@ -303,15 +306,45 @@
             sb.append("\n").append(cabFile.getAbsolutePath()).append("\n");
             
             try {
-                Process p = Runtime.getRuntime().exec("listcab");
+                Process p = Runtime.getRuntime().exec("listcab", null, 
baseDir);
                 OutputStream out = p.getOutputStream();
-                out.write(sb.toString().getBytes());
+               out.write(sb.toString().getBytes());
                 out.flush();
                 out.close();
+               AsyncStreamGobbler stdout = new 
AsyncStreamGobbler(p.getInputStream());
+               AsyncStreamGobbler stderr = new 
AsyncStreamGobbler(p.getErrorStream()); 
+               stdout.startGobbling();
+               stderr.startGobbling();
+               int result = -99;
+
+                // Wait for the end of output and error streams
+               try {
+                   result = p.waitFor();
+                   while(!(stdout.isDone() && !stderr.isDone())) {
+                       Thread.currentThread().sleep(200);
+                   }
+               } catch(InterruptedException ie) {
+                   log(ie.toString());
+               }
+
+                // Handle errors in listcab
+               if(result != 0) {
+                   log("Error executing listcab.");
+                   log("Error code: " + result);
+                   String output = stdout.getResult();
+                   String errors = stderr.getResult();
+                   if(output.length() > 0) {
+                       log("Output: \n" + output);
+                   }
+                   if(errors.length() > 0) {
+                       log("Errors: \n" + errors);
+                   }
+               }
+               
             } catch (IOException ex) {
                 String msg = "Problem creating " + cabFile + " " + 
ex.getMessage();
                 throw new BuildException(msg);
-            }
+           }
         } else {
             try {
                 File listFile = createListFile(files);
@@ -341,4 +374,41 @@
             }
         }
     }
+
+    private static class AsyncStreamGobbler implements Runnable {
+       public AsyncStreamGobbler(InputStream in) {
+           _in = in;
+       }
+       public void startGobbling() {
+           new Thread(this).start();
+       }
+       public synchronized String getResult() {
+           return _result.toString();
+       }
+       public void run() {
+           try {
+               _reader = new BufferedReader(new InputStreamReader(_in));
+               String line;
+               while((line = _reader.readLine()) != null) {
+                   synchronized(this) {
+                       _result.append(line + "\n");
+                   }
+               }
+               synchronized(this) {
+                   _done = true;
+               }
+           } catch(IOException e) {
+               _result.append("AsyncStreamGobbler exception: " + e);
+           }
+       }
+       public synchronized boolean isDone() {
+           return _done;
+       }
+
+       StringBuffer   _result = new StringBuffer();
+       BufferedReader _reader;
+       InputStream    _in;
+       boolean        _done = false;
+    }
+
 }

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to