2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / io / FilePermission.java
blob2c3f1773b8e4d25fe3cb1a1c77e74ffd5c623dd5
1 /* java.lang.FilePermission
2 Copyright (C) 1998, 2000, 2003 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 usingPerms = false;
50 private boolean readPerm = false;
51 private boolean writePerm = false;
52 private boolean executePerm = false;
53 private boolean deletePerm = false;
54 private String actionsString;
56 private void cachePerms()
58 // While race conditions could occur, they don't matter at all.
60 String action;
61 int i = actionsString.indexOf(',');
62 int startI = 0;
63 while(i != -1)
65 action = actionsString.substring(startI,i);
66 if(action.equals("read"))
67 readPerm = true;
68 else if(action.equals("write"))
69 writePerm = true;
70 else if(action.equals("execute"))
71 executePerm = true;
72 else if(action.equals("delete"))
73 deletePerm = true;
75 startI = i+1;
76 i = actionsString.indexOf(',',startI);
79 action = actionsString.substring(startI);
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;
90 /** Create a new FilePermission.
91 ** @param pathExpression an expression specifying the paths this
92 ** permission represents.
93 ** @param actionsString a comma-separated list of the actions this
94 ** permission represents.
95 ** FIXME: what to do when the file string is malformed?
96 **/
97 public FilePermission(String pathExpression, String actionsString)
99 super(pathExpression);
100 this.actionsString = actionsString;
103 /** Get the actions this FilePermission supports.
104 ** @return the String representing the actions this FilePermission supports.
106 public String getActions()
108 return actionsString;
111 /** Get the hash code for this Object.<P>
112 ** FilePermission's hash code is calculated as the exclusive or of the
113 ** target
114 ** String's hash code and the action String's hash code.
115 ** @specnote Sun did not specify how to calculate the hash code;
116 ** I made this up.
117 ** @return the hash code for this Object.
119 public int hashCode()
121 return getName().hashCode() ^ actionsString.hashCode();
124 /** Check two FilePermissions for semantic equality.
125 ** Two FilePermissions are exactly equivalent if they have identical path
126 ** expressions and have exactly the same access permissions.
127 ** @param o the Object to compare to.
128 ** @return whether the Objects are semantically equivalent.
130 public boolean equals(Object o)
132 if(!(o instanceof FilePermission))
133 return false;
134 FilePermission p = (FilePermission)o;
135 if(!usingPerms)
136 cachePerms();
137 if(!p.usingPerms)
138 p.cachePerms();
140 String f1 = getName();
141 String f2 = p.getName();
143 /* Compare names, taking into account if they refer to a
144 * directory and one has a separator and the other does not.
146 if(f1.length() > 0 && f1.charAt(f1.length() - 1) == File.separatorChar)
148 if(f2.length() > 0
149 && f2.charAt(f2.length() - 1) == File.separatorChar)
151 if(!f2.equals(f1))
152 return false;
154 else
156 if(!f2.equals(f1.substring(0,f1.length()-1)))
157 return false;
160 else
162 if(f2.length() > 0
163 && f2.charAt(f2.length() - 1) == File.separatorChar)
165 if(!f1.equals(f2.substring(0,f2.length()-1)))
166 return false;
168 else
170 if(!f1.equals(f2))
171 return false;
174 return readPerm == p.readPerm && writePerm == p.writePerm && executePerm == p.executePerm && deletePerm == p.deletePerm;
177 /** Check to see if this permission implies another.
178 ** Permission A implies permission B if these things are all true:
179 ** <OL>
180 ** <LI>A and B are both FilePermissions.</LI>
181 ** <LI>All possible files in B are included in A
182 ** (possibly more are in A).</LI>
183 ** <LI>All actions B supports, A also supports.</LI>
184 ** </OL>
185 ** @param p the Permission to compare against.
186 ** @return whether this Permission implies p
188 public boolean implies(Permission p)
190 FilePermission fp;
192 if(!(p instanceof FilePermission))
193 return false;
195 fp = (FilePermission)p;
197 String f1 = getName();
198 String f2 = fp.getName();
200 if(f1.charAt(0) != File.separatorChar)
202 f1 = CURRENT_DIRECTORY + f1;
204 if(f2.charAt(0) != File.separatorChar)
206 f2 = CURRENT_DIRECTORY + f2;
209 String sub1;
211 switch(f1.charAt(f1.length() - 1))
213 case '*':
214 sub1 = f1.substring(0,f1.length() - 1); // chop off "*"
215 if(f2.length() <= sub1.length())
217 /* If it's smaller, there is no way it could be part of this
218 * directory.
219 * If it's the same (or length - 1), it could be the same
220 * directory but
221 * specifies access to the directory rather than the files in it.
223 return false;
225 else if(f2.charAt(sub1.length() - 1) == File.separatorChar)
227 /* Make sure the part before the "/" is the same */
228 if(!f2.substring(0,sub1.length()).equals(sub1))
229 return false;
230 /* Make sure there are no subdirectories specified
231 underneath this one */
232 String sub2 = f2.substring(sub1.length()+1);
233 if(f2.substring(sub1.length()+1).indexOf(File.separatorChar)
234 != -1)
235 return false;
237 else
239 /* Obviously not equal: f2 is either not a directory or is not
240 * the same directory (its name continues further than we want)
242 return false;
244 break;
245 case '-':
246 sub1 = f1.substring(0,f1.length() - 2); // chop off "/-"
247 if(f2.length() < sub1.length())
249 /* If it's smaller, there is no way it could be part of
250 * this directory. */
251 return false;
253 else if(f2.length() > sub1.length() && f2.charAt(sub1.length())
254 != File.separatorChar)
256 return false;
259 else if(!f2.substring(0,sub1.length()).equals(sub1))
260 return false;
261 break;
262 /* Looks redundant with default case and won't compile anyway - arenn
263 case File.separatorChar:
264 if(f2.charAt(f2.length()) == File.separatorChar) {
265 if(!f2.equals(f1))
266 return false;
267 } else {
268 if(!f2.equals(f1.substring(0,f1.length()-1)))
269 return false;
271 break;
273 default:
274 if(f2.charAt(f2.length()) == File.separatorChar)
276 if(!f1.equals(f2.substring(0,f2.length()-1)))
277 return false;
279 else
281 if(!f1.equals(f2))
282 return false;
284 break;
287 if(!usingPerms)
288 cachePerms();
289 if(!fp.usingPerms)
290 fp.cachePerms();
292 if(readPerm && !fp.readPerm)
293 return false;
294 if(writePerm && !fp.writePerm)
295 return false;
296 if(executePerm && !fp.executePerm)
297 return false;
298 if(deletePerm && !fp.deletePerm)
299 return false;
301 return true;
303 } // class FilePermission