* java/io/FilePermission.java (usingPerms): Removed.
[official-gcc.git] / libjava / java / io / FilePermission.java
blob7d86c292dad82c61db93679269d73b934feff97d
1 /* java.lang.FilePermission
2 Copyright (C) 1998, 2000, 2003, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.io;
41 import java.security.Permission;
43 public final class FilePermission extends Permission implements Serializable
45 static final long serialVersionUID = 7930732926638008763L;
47 private static final String CURRENT_DIRECTORY =
48 System.getProperty("user.dir");
49 private boolean readPerm = false;
50 private boolean writePerm = false;
51 private boolean executePerm = false;
52 private boolean deletePerm = false;
53 private final String actionsString;
55 // Checks and caches the actions
56 private void checkPerms() throws IllegalArgumentException
58 String action;
59 int i = actionsString.indexOf(',');
60 int startI = 0;
61 while(i != -1)
63 action = actionsString.substring(startI,i).trim().toLowerCase();
64 if(action.equals("read"))
65 readPerm = true;
66 else if(action.equals("write"))
67 writePerm = true;
68 else if(action.equals("execute"))
69 executePerm = true;
70 else if(action.equals("delete"))
71 deletePerm = true;
72 else
73 throw new IllegalArgumentException("Unknown action: " + action);
75 startI = i+1;
76 i = actionsString.indexOf(',',startI);
79 action = actionsString.substring(startI).trim().toLowerCase();
80 if(action.equals("read"))
81 readPerm = true;
82 else if(action.equals("write"))
83 writePerm = true;
84 else if(action.equals("execute"))
85 executePerm = true;
86 else if(action.equals("delete"))
87 deletePerm = true;
88 else
89 throw new IllegalArgumentException("Unknown action: " + action);
93 * Create a new FilePermission.
95 * @param pathExpression an expression specifying the paths this
96 * permission represents.
97 * @param actionsString a comma-separated list of the actions this
98 * permission represents. The actions must be "read", "write",
99 * "execute" and/or "delete".
101 * FIXME: what to do when the file string is malformed?
103 public FilePermission(String pathExpression, String actionsString)
105 super(pathExpression);
106 if (pathExpression == null)
107 throw new NullPointerException("pathExpression");
108 if (actionsString == null)
109 throw new IllegalArgumentException("actionsString");
110 this.actionsString = actionsString;
111 checkPerms();
114 /** Get the actions this FilePermission supports.
115 ** @return the String representing the actions this FilePermission supports.
117 public String getActions()
119 return actionsString;
122 /** Get the hash code for this Object.<P>
123 ** FilePermission's hash code is calculated as the exclusive or of the
124 ** target
125 ** String's hash code and the action String's hash code.
126 ** @specnote Sun did not specify how to calculate the hash code;
127 ** I made this up.
128 ** @return the hash code for this Object.
130 public int hashCode()
132 return getName().hashCode() ^ actionsString.hashCode();
135 /** Check two FilePermissions for semantic equality.
136 ** Two FilePermissions are exactly equivalent if they have identical path
137 ** expressions and have exactly the same access permissions.
138 ** @param o the Object to compare to.
139 ** @return whether the Objects are semantically equivalent.
141 public boolean equals(Object o)
143 if(!(o instanceof FilePermission))
144 return false;
145 FilePermission p = (FilePermission)o;
147 String f1 = getName();
148 String f2 = p.getName();
150 /* Compare names, taking into account if they refer to a
151 * directory and one has a separator and the other does not.
153 if(f1.length() > 0 && f1.charAt(f1.length() - 1) == File.separatorChar)
155 if(f2.length() > 0
156 && f2.charAt(f2.length() - 1) == File.separatorChar)
158 if(!f2.equals(f1))
159 return false;
161 else
163 if(!f2.equals(f1.substring(0,f1.length()-1)))
164 return false;
167 else
169 if(f2.length() > 0
170 && f2.charAt(f2.length() - 1) == File.separatorChar)
172 if(!f1.equals(f2.substring(0,f2.length()-1)))
173 return false;
175 else
177 if(!f1.equals(f2))
178 return false;
181 return readPerm == p.readPerm && writePerm == p.writePerm && executePerm == p.executePerm && deletePerm == p.deletePerm;
184 /** Check to see if this permission implies another.
185 ** Permission A implies permission B if these things are all true:
186 ** <OL>
187 ** <LI>A and B are both FilePermissions.</LI>
188 ** <LI>All possible files in B are included in A
189 ** (possibly more are in A).</LI>
190 ** <LI>All actions B supports, A also supports.</LI>
191 ** </OL>
192 ** @param p the Permission to compare against.
193 ** @return whether this Permission implies p
195 public boolean implies(Permission p)
197 FilePermission fp;
199 if(!(p instanceof FilePermission))
200 return false;
202 fp = (FilePermission)p;
204 String f1 = getName();
205 String f2 = fp.getName();
207 if(f1.charAt(0) != File.separatorChar)
209 f1 = CURRENT_DIRECTORY + f1;
211 if(f2.charAt(0) != File.separatorChar)
213 f2 = CURRENT_DIRECTORY + f2;
216 String sub1;
218 switch(f1.charAt(f1.length() - 1))
220 case '*':
221 sub1 = f1.substring(0,f1.length() - 1); // chop off "*"
222 if(f2.length() <= sub1.length())
224 /* If it's smaller, there is no way it could be part of this
225 * directory.
226 * If it's the same (or length - 1), it could be the same
227 * directory but
228 * specifies access to the directory rather than the files in it.
230 return false;
232 else if(f2.charAt(sub1.length() - 1) == File.separatorChar)
234 /* Make sure the part before the "/" is the same */
235 if(!f2.substring(0,sub1.length()).equals(sub1))
236 return false;
237 /* Make sure there are no subdirectories specified
238 underneath this one */
239 if(f2.substring(sub1.length()+1).indexOf(File.separatorChar)
240 != -1)
241 return false;
243 else
245 /* Obviously not equal: f2 is either not a directory or is not
246 * the same directory (its name continues further than we want)
248 return false;
250 break;
251 case '-':
252 sub1 = f1.substring(0,f1.length() - 2); // chop off "/-"
253 if(f2.length() < sub1.length())
255 /* If it's smaller, there is no way it could be part of
256 * this directory. */
257 return false;
259 else if(f2.length() > sub1.length() && f2.charAt(sub1.length())
260 != File.separatorChar)
262 return false;
265 else if(!f2.substring(0,sub1.length()).equals(sub1))
266 return false;
267 break;
268 /* Looks redundant with default case and won't compile anyway - arenn
269 case File.separatorChar:
270 if(f2.charAt(f2.length()) == File.separatorChar) {
271 if(!f2.equals(f1))
272 return false;
273 } else {
274 if(!f2.equals(f1.substring(0,f1.length()-1)))
275 return false;
277 break;
279 default:
280 if(f2.charAt(f2.length() - 1) == File.separatorChar)
282 if(!f1.equals(f2.substring(0,f2.length() - 1)))
283 return false;
285 else
287 if(!f1.equals(f2))
288 return false;
290 break;
293 if(readPerm && !fp.readPerm)
294 return false;
295 if(writePerm && !fp.writePerm)
296 return false;
297 if(executePerm && !fp.executePerm)
298 return false;
299 if(deletePerm && !fp.deletePerm)
300 return false;
302 return true;
304 } // class FilePermission