The patched Chmod task is aware of include/exclude pattern.

Changes to the task:
* new chmod takes 'dir' or 'file' attributes, together with old 'perm'
  attribute (file/dir are replacing old 'src' attribute)

* if both file and dir are specified or if neither is specified, task will
  throw build exception

* if 'file' is specified, no matter if include/exclude is used, the chmod
  will change the mode of this single specified file (or directory)


Examples:

single file:

  <chmod file="bin/file.sh" perm="u+rwx" />

single directory:
 
  <chmod file="bin" perm="ogu+rwx" />

list of files:

  <chmod dir="./bin" perm="u+rwx">
    <include name="*.sh" />
    <include name="*.csh" />
  </chmod>


regards
Mariusz

Index: src/main/org/apache/tools/ant/taskdefs/Chmod.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Chmod.java,v
retrieving revision 1.2
diff -u -r1.2 Chmod.java
--- src/main/org/apache/tools/ant/taskdefs/Chmod.java   2000/02/28 02:17:57     
1.2
+++ src/main/org/apache/tools/ant/taskdefs/Chmod.java   2000/05/25 23:21:47
@@ -60,33 +60,60 @@
 import java.util.*;
 
 /**
+ * Chmod equivalent for unix-like environments.
  *
- *
- * @author [EMAIL PROTECTED]
+ * @author [EMAIL PROTECTED] 
+ * @author Mariusz Nowostawski (Marni)  
+       <a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a>
  */
 
-public class Chmod extends Task {
+public class Chmod extends MatchingTask {
 
-    private File srcFile;
-    private String mod;
+  private File srcFile; //if we want to chmod a single file
+  private File srcDir;  //if we want to chmod a list of files
+  private String mod;
     
-    public void setSrc(String src) {
-       srcFile = project.resolveFile(src);
-    }
+  public void setFile(String src) {
+    srcFile = project.resolveFile(src);
+  }
 
-    public void setPerm(String perm) {
-       mod=perm;
-    }
+  public void setDir(String src) {
+    srcDir = project.resolveFile(src);
+  }
 
-    public void execute() throws BuildException {
-       try {
-           // XXX if OS=unix
-           if (System.getProperty("path.separator").equals(":") &&
-                    !System.getProperty("os.name").startsWith("Mac"))
-               Runtime.getRuntime().exec("chmod " + mod + " " + srcFile );
-       } catch (IOException ioe) {
-           // ignore, but warn
-           System.out.println("Error chmod" + ioe.toString() );
-       }
+  public void setPerm(String perm) {
+    mod=perm;
+  }
+
+  public void execute() throws BuildException {
+    try {
+      // XXX if OS=unix
+      if (System.getProperty("path.separator").equals(":") &&
+          !System.getProperty("os.name").startsWith("Mac")) {
+        
+        if(srcFile != null && srcDir == null) {
+          chmod(srcFile.toString());
+        } else if(srcFile == null && srcDir == null) {
+          project.log("The attribute 'file' or 'dir' needs to be set.", 
Project.MSG_WARN);
+          throw new BuildException("Required attribute not set in Chmod");
+        } else if(srcFile == null && srcDir != null) {
+          
+          DirectoryScanner ds = getDirectoryScanner(srcDir);
+          String[] files = ds.getIncludedFiles();
+          
+          for (int i = 0; i < files.length; i++) {
+            chmod(files[i]);
+          }
+        }
+      }
+    } catch (IOException ioe) {
+      // ignore, but warn
+      project.log("Error in Chmod " + ioe.toString() , Project.MSG_WARN);
     }
+  }
+
+
+  private void chmod(String file) throws BuildException, IOException {
+    Runtime.getRuntime().exec("chmod " + mod + " " + file);
+  }
 }

Reply via email to