41 import java.nio.file.*;
42 import java.nio.file.attribute.*;
43 import static java.nio.file.attribute.PosixFilePermission.*;
44 import static java.nio.file.FileVisitResult.*;
45 import java.io.IOException;
117 if (exprs.length() < 2)
118 throw new IllegalArgumentException(
"Invalid mode");
121 final Set<PosixFilePermission> toAdd =
new HashSet<PosixFilePermission>();
122 final Set<PosixFilePermission> toRemove =
new HashSet<PosixFilePermission>();
125 for (String expr: exprs.split(
",")) {
127 if (expr.length() < 2)
128 throw new IllegalArgumentException(
"Invalid mode");
136 boolean done =
false;
138 switch (expr.charAt(pos)) {
139 case 'u' : u =
true;
break;
140 case 'g' : g =
true;
break;
141 case 'o' : o =
true;
break;
142 case 'a' : u =
true; g =
true; o =
true;
break;
143 default : done =
true;
150 throw new IllegalArgumentException(
"Invalid mode");
153 char op = expr.charAt(pos++);
154 String mask = (expr.length() == pos) ?
"" : expr.substring(pos);
157 boolean add = (op ==
'+');
158 boolean remove = (op ==
'-');
159 boolean assign = (op ==
'=');
160 if (!add && !
remove && !assign)
161 throw new IllegalArgumentException(
"Invalid mode");
164 if (assign && mask.length() == 0) {
174 for (
int i=0; i<mask.length(); i++) {
175 switch (mask.charAt(i)) {
176 case 'r' : r =
true;
break;
177 case 'w' : w =
true;
break;
178 case 'x' : x =
true;
break;
180 throw new IllegalArgumentException(
"Invalid mode");
187 if (r) toAdd.add(OWNER_READ);
188 if (w) toAdd.add(OWNER_WRITE);
189 if (x) toAdd.add(OWNER_EXECUTE);
192 if (r) toAdd.add(GROUP_READ);
193 if (w) toAdd.add(GROUP_WRITE);
194 if (x) toAdd.add(GROUP_EXECUTE);
197 if (r) toAdd.add(OTHERS_READ);
198 if (w) toAdd.add(OTHERS_WRITE);
199 if (x) toAdd.add(OTHERS_EXECUTE);
204 if (r) toRemove.add(OWNER_READ);
205 if (w) toRemove.add(OWNER_WRITE);
206 if (x) toRemove.add(OWNER_EXECUTE);
209 if (r) toRemove.add(GROUP_READ);
210 if (w) toRemove.add(GROUP_WRITE);
211 if (x) toRemove.add(GROUP_EXECUTE);
214 if (r) toRemove.add(OTHERS_READ);
215 if (w) toRemove.add(OTHERS_WRITE);
216 if (x) toRemove.add(OTHERS_EXECUTE);
221 if (r) toAdd.add(OWNER_READ);
222 else toRemove.add(OWNER_READ);
223 if (w) toAdd.add(OWNER_WRITE);
224 else toRemove.add(OWNER_WRITE);
225 if (x) toAdd.add(OWNER_EXECUTE);
226 else toRemove.add(OWNER_EXECUTE);
229 if (r) toAdd.add(GROUP_READ);
230 else toRemove.add(GROUP_READ);
231 if (w) toAdd.add(GROUP_WRITE);
232 else toRemove.add(GROUP_WRITE);
233 if (x) toAdd.add(GROUP_EXECUTE);
234 else toRemove.add(GROUP_EXECUTE);
237 if (r) toAdd.add(OTHERS_READ);
238 else toRemove.add(OTHERS_READ);
239 if (w) toAdd.add(OTHERS_WRITE);
240 else toRemove.add(OTHERS_WRITE);
241 if (x) toAdd.add(OTHERS_EXECUTE);
242 else toRemove.add(OTHERS_EXECUTE);
250 public Set<PosixFilePermission> change(Set<PosixFilePermission> perms) {
252 perms.removeAll(toRemove);
270 Set<PosixFilePermission>
change(Set<PosixFilePermission> perms);
278 Set<PosixFilePermission> perms = Files.getPosixFilePermissions(
file);
279 Files.setPosixFilePermissions(
file, changer.
change(perms));
280 }
catch (IOException x) {
281 System.err.println(x);
310 System.err.println(
"WARNING: " + exc);
316 System.err.println(
"WARNING: " + exc);
322 System.err.println(
"java Chmod [-R] symbolic-mode-list file...");
326 public static void main(String[] args)
throws IOException {
331 if (args[argi].equals(
"-R")) {
335 maxDepth = Integer.MAX_VALUE;
342 Set<FileVisitOption> opts = Collections.emptySet();
343 while (argi < args.length) {
344 Path
file = Paths.get(args[argi]);
345 Files.walkFileTree(
file, opts, maxDepth, visitor);