1. /*
  2. * Copyright 2000-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs;
  18. import java.io.File;
  19. import java.io.IOException;
  20. import org.apache.tools.ant.BuildException;
  21. import org.apache.tools.ant.Project;
  22. import org.apache.tools.ant.taskdefs.condition.Os;
  23. import org.apache.tools.ant.types.Commandline;
  24. import org.apache.tools.ant.types.FileSet;
  25. import org.apache.tools.ant.types.PatternSet;
  26. /**
  27. * Chmod equivalent for unix-like environments.
  28. *
  29. * @since Ant 1.1
  30. *
  31. * @ant.task category="filesystem"
  32. * @todo Refactor so it does not extend from ExecuteOn and then turn around
  33. * and unsupport several attributes.
  34. */
  35. public class Chmod extends ExecuteOn {
  36. private FileSet defaultSet = new FileSet();
  37. private boolean defaultSetDefined = false;
  38. private boolean havePerm = false;
  39. /**
  40. * Chmod task for setting file and directory permissions.
  41. */
  42. public Chmod() {
  43. super.setExecutable("chmod");
  44. super.setParallel(true);
  45. super.setSkipEmptyFilesets(true);
  46. }
  47. /**
  48. * @see org.apache.tools.ant.ProjectComponent#setProject
  49. */
  50. public void setProject(Project project) {
  51. super.setProject(project);
  52. defaultSet.setProject(project);
  53. }
  54. /**
  55. * The file or single directory of which the permissions must be changed.
  56. * @param src
  57. */
  58. public void setFile(File src) {
  59. FileSet fs = new FileSet();
  60. fs.setFile(src);
  61. addFileset(fs);
  62. }
  63. /**
  64. * The directory which holds the files whose permissions must be changed.
  65. * @param src
  66. */
  67. public void setDir(File src) {
  68. defaultSet.setDir(src);
  69. }
  70. /**
  71. * The new permissions.
  72. * @param perm
  73. */
  74. public void setPerm(String perm) {
  75. createArg().setValue(perm);
  76. havePerm = true;
  77. }
  78. /**
  79. * Add a name entry on the include list.
  80. */
  81. public PatternSet.NameEntry createInclude() {
  82. defaultSetDefined = true;
  83. return defaultSet.createInclude();
  84. }
  85. /**
  86. * Add a name entry on the exclude list.
  87. */
  88. public PatternSet.NameEntry createExclude() {
  89. defaultSetDefined = true;
  90. return defaultSet.createExclude();
  91. }
  92. /**
  93. * Add a set of patterns.
  94. */
  95. public PatternSet createPatternSet() {
  96. defaultSetDefined = true;
  97. return defaultSet.createPatternSet();
  98. }
  99. /**
  100. * Sets the set of include patterns. Patterns may be separated by a comma
  101. * or a space.
  102. *
  103. * @param includes the string containing the include patterns
  104. */
  105. public void setIncludes(String includes) {
  106. defaultSetDefined = true;
  107. defaultSet.setIncludes(includes);
  108. }
  109. /**
  110. * Sets the set of exclude patterns. Patterns may be separated by a comma
  111. * or a space.
  112. *
  113. * @param excludes the string containing the exclude patterns
  114. */
  115. public void setExcludes(String excludes) {
  116. defaultSetDefined = true;
  117. defaultSet.setExcludes(excludes);
  118. }
  119. /**
  120. * Sets whether default exclusions should be used or not.
  121. *
  122. * @param useDefaultExcludes "true"|"on"|"yes" when default exclusions
  123. * should be used, "false"|"off"|"no" when they
  124. * shouldn't be used.
  125. */
  126. public void setDefaultexcludes(boolean useDefaultExcludes) {
  127. defaultSetDefined = true;
  128. defaultSet.setDefaultexcludes(useDefaultExcludes);
  129. }
  130. protected void checkConfiguration() {
  131. if (!havePerm) {
  132. throw new BuildException("Required attribute perm not set in chmod",
  133. getLocation());
  134. }
  135. if (defaultSetDefined && defaultSet.getDir(getProject()) != null) {
  136. addFileset(defaultSet);
  137. }
  138. super.checkConfiguration();
  139. }
  140. public void execute() throws BuildException {
  141. /*
  142. * In Ant 1.1, <chmod dir="foo" /> means, change the permissions
  143. * of directory foo, not anything inside of it. This is the case the
  144. * second branch of the if statement below catches for backwards
  145. * compatibility.
  146. */
  147. if (defaultSetDefined || defaultSet.getDir(getProject()) == null) {
  148. try {
  149. super.execute();
  150. } finally {
  151. if (defaultSetDefined && defaultSet.getDir(getProject()) != null) {
  152. filesets.removeElement(defaultSet);
  153. }
  154. }
  155. } else if (isValidOs()) {
  156. // we are chmodding the given directory
  157. Execute execute = prepareExec();
  158. Commandline cloned = (Commandline) cmdl.clone();
  159. cloned.createArgument().setValue(defaultSet.getDir(getProject())
  160. .getPath());
  161. try {
  162. execute.setCommandline(cloned.getCommandline());
  163. runExecute(execute);
  164. } catch (IOException e) {
  165. throw new BuildException("Execute failed: " + e, e, getLocation());
  166. } finally {
  167. // close the output file if required
  168. logFlush();
  169. }
  170. }
  171. }
  172. /**
  173. * @ant.attribute ignore="true"
  174. */
  175. public void setExecutable(String e) {
  176. throw new BuildException(getTaskType()
  177. + " doesn\'t support the executable attribute", getLocation());
  178. }
  179. /**
  180. * @ant.attribute ignore="true"
  181. */
  182. public void setCommand(Commandline cmdl) {
  183. throw new BuildException(getTaskType()
  184. + " doesn\'t support the command attribute", getLocation());
  185. }
  186. /**
  187. * @ant.attribute ignore="true"
  188. */
  189. public void setSkipEmptyFilesets(boolean skip) {
  190. throw new BuildException(getTaskType()
  191. + " doesn\'t support the skipemptyfileset attribute", getLocation());
  192. }
  193. /**
  194. * @ant.attribute ignore="true"
  195. */
  196. public void setAddsourcefile(boolean b) {
  197. throw new BuildException(getTaskType()
  198. + " doesn\'t support the addsourcefile attribute", getLocation());
  199. }
  200. protected boolean isValidOs() {
  201. return Os.isFamily("unix") && super.isValidOs();
  202. }
  203. }