From d55b86ab4fbcc8807e105e8c99c56488dadc6aeb Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Tue, 4 Jul 2000 09:30:21 +0000 Subject: [PATCH] Make Chmod a MatchingTask. Submitted by: Mariusz Nowostawski git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@267730 13f79535-47bb-0310-9956-ffa450edef68 --- docs/index.html | 66 +++++++++++++++++-- .../org/apache/tools/ant/taskdefs/Chmod.java | 61 +++++++++++++---- 2 files changed, 109 insertions(+), 18 deletions(-) diff --git a/docs/index.html b/docs/index.html index d46a634b9..b80d23c62 100644 --- a/docs/index.html +++ b/docs/index.html @@ -620,8 +620,18 @@ if the org.whatever.Myclass is found in Ant's classpath.


Chmod

Description

-

Changes the permissions of a file. Right now it has efect only under Unix. +

Changes the permissions of a file or all files inside a specified directory. Right now it has efect only under Unix. The permissions are also UNIX style, like the argument for the chmod command.

+

It is possible to refine the set of files whose permissions are changed. This can be +done with the includes, includesfile, excludes, excludesfile and defaultexcludes +attributes. With the includes or includesfile attribute you specify the files you want to +have included by using patterns. The exclude or excludesfile attribute is used to specify +the files you want to have excluded. This is also done with patterns. And +finally with the defaultexcludes attribute, you can specify whether you +want to use default exclusions or not. See the section on directory based tasks, on how the +inclusion/exclusion of files works, and how to write patterns. The patterns are +relative to the dir directory.

Parameters

@@ -629,24 +639,72 @@ The permissions are also UNIX style, like the argument for the chmod command.

Description + + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + +
Required
filethe file or single directory of which the permissions + must be changed.exactly one of the two
dirthe directory which holds the files whose permissions + must be changed.
srcthe file of which the permissions must be changed.Yesthe file or single directory of which the permissions + must be changed (deprecated, use file instead).No
perm the new permissions. Yes
includescomma separated list of patterns of files that must be + included. All files are included when omitted.No
includesfilethe name of a file. Each line of this file is + taken to be an include patternNo
excludescomma separated list of patterns of files that must be + excluded. No files (except default excludes) are excluded when omitted.No
excludesfilethe name of a file. Each line of this file is + taken to be an exclude patternNo
defaultexcludesindicates whether default excludes should be used or not + ("yes"/"no"). Default excludes are used when omitted.No

Examples

-

<chmod src="${dist}/start.sh" perm="ugo+rx" +

<chmod file="${dist}/start.sh" perm="ugo+rx" />

makes the "start.sh" file readable and executable for anyone on a UNIX system.

+
+    <chmod dir="${dist}/bin" perm="ugo+rx" includes="**/*.sh" />
+
+ +

makes all ".sh" files below ${dist}/bin +readable and executable for anyone on a UNIX system.


Copydir

Description

diff --git a/src/main/org/apache/tools/ant/taskdefs/Chmod.java b/src/main/org/apache/tools/ant/taskdefs/Chmod.java index d092b4e03..5595f9879 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Chmod.java +++ b/src/main/org/apache/tools/ant/taskdefs/Chmod.java @@ -60,33 +60,66 @@ import java.io.*; import java.util.*; /** - * + * Chmod equivalent for unix-like environments. * * @author costin@eng.sun.com + * @author Mariusz Nowostawski (Marni) mnowostawski@infoscience.otago.ac.nz */ -public class Chmod extends Task { +public class Chmod extends MatchingTask { - private File srcFile; + private File srcFile; //if we want to chmod a single file or dir + private File srcDir; //if we want to chmod a list of files private String mod; + public void setFile(String src) { + srcFile = project.resolveFile(src); + } + + public void setDir(String src) { + srcDir = project.resolveFile(src); + } + public void setSrc(String src) { - srcFile = project.resolveFile(src); + project.log("The src attribute is deprecated. " + + "Please use the file attribute.", + Project.MSG_WARN); + setFile(src); } public void setPerm(String perm) { - mod=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")) - Runtime.getRuntime().exec("chmod " + mod + " " + srcFile ); - } catch (IOException ioe) { - // ignore, but warn - System.out.println("Error chmod" + ioe.toString() ); - } + 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", location); + } 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); } }