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