Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / io / FilePermission.java
blob9151bf66964832da9f5c1c56341813aa9c3b9b70
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");
50 private boolean readPerm = false;
51 private boolean writePerm = false;
52 private boolean executePerm = false;
53 private boolean deletePerm = false;
54 private final String actionsString;
56 // Checks and caches the actions
57 private void checkPerms() throws IllegalArgumentException
59 String action;
60 int i = actionsString.indexOf(',');
61 int startI = 0;
62 while (i != -1)
64 action = actionsString.substring(startI, i).trim().toLowerCase();
65 if (action.equals("read"))
66 readPerm = true;
67 else if (action.equals("write"))
68 writePerm = true;
69 else if (action.equals("execute"))
70 executePerm = true;
71 else if (action.equals("delete"))
72 deletePerm = true;
73 else
74 throw new IllegalArgumentException("Unknown action: " + action);
76 startI = i + 1;
77 i = actionsString.indexOf(',', startI);
80 action = actionsString.substring(startI).trim().toLowerCase();
81 if (action.equals("read"))
82 readPerm = true;
83 else if (action.equals("write"))
84 writePerm = true;
85 else if (action.equals("execute"))
86 executePerm = true;
87 else if (action.equals("delete"))
88 deletePerm = true;
89 else
90 throw new IllegalArgumentException("Unknown action: " + action);
93 /**
94 * Create a new FilePermission.
96 * @param pathExpression an expression specifying the paths this
97 * permission represents.
98 * @param actionsString a comma-separated list of the actions this
99 * permission represents. The actions must be "read", "write",
100 * "execute" and/or "delete".
102 public FilePermission(String pathExpression, String actionsString)
104 // FIXME: what to do when the file string is malformed?
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();
115 * Get the actions this FilePermission supports.
116 * @return the String representing the actions this FilePermission supports.
118 public String getActions()
120 return actionsString;
124 * Get the hash code for this Object.<P>
125 * FilePermission's hash code is calculated as the exclusive or of the
126 * target
127 * String's hash code and the action String's hash code.
128 * @specnote Sun did not specify how to calculate the hash code;
129 * I made this up.
130 * @return the hash code for this Object.
132 public int hashCode()
134 return getName().hashCode() ^ actionsString.hashCode();
138 * Check two FilePermissions for semantic equality.
139 * Two FilePermissions are exactly equivalent if they have identical path
140 * expressions and have exactly the same access permissions.
141 * @param o the Object to compare to.
142 * @return whether the Objects are semantically equivalent.
144 public boolean equals(Object o)
146 if (! (o instanceof FilePermission))
147 return false;
148 FilePermission p = (FilePermission) o;
150 String f1 = getName();
151 String f2 = p.getName();
153 // Compare names, taking into account if they refer to a directory
154 // and one has a separator and the other does not.
155 if (f1.length() > 0 && f1.charAt(f1.length() - 1) == File.separatorChar)
157 if (f2.length() > 0
158 && f2.charAt(f2.length() - 1) == File.separatorChar)
160 if (! f2.equals(f1))
161 return false;
163 else
165 if (! f2.equals(f1.substring(0, f1.length() - 1)))
166 return false;
169 else
171 if (f2.length() > 0
172 && f2.charAt(f2.length() - 1) == File.separatorChar)
174 if (! f1.equals(f2.substring(0, f2.length() - 1)))
175 return false;
177 else
179 if (! f1.equals(f2))
180 return false;
183 return (readPerm == p.readPerm
184 && writePerm == p.writePerm
185 && executePerm == p.executePerm
186 && deletePerm == p.deletePerm);
190 * Check to see if this permission implies another.
191 * Permission A implies permission B if these things are all true:
192 * <OL>
193 * <LI>A and B are both FilePermissions.</LI>
194 * <LI>All possible files in B are included in A
195 * (possibly more are in A).</LI>
196 * <LI>All actions B supports, A also supports.</LI>
197 * </OL>
198 * @param p the Permission to compare against.
199 * @return whether this Permission implies p
201 public boolean implies(Permission p)
203 FilePermission fp;
205 if (! (p instanceof FilePermission))
206 return false;
208 fp = (FilePermission) p;
210 String f1 = getName();
211 String f2 = fp.getName();
213 if (f1.charAt(0) != File.separatorChar)
214 f1 = CURRENT_DIRECTORY + f1;
215 if (f2.charAt(0) != File.separatorChar)
216 f2 = CURRENT_DIRECTORY + f2;
218 String sub1;
220 switch (f1.charAt(f1.length() - 1))
222 case '*':
223 sub1 = f1.substring(0, f1.length() - 1); // chop off "*"
224 if (f2.length() <= sub1.length())
226 // If it's smaller, there is no way it could be part of
227 // this directory. If it's the same (or length - 1), it
228 // could be the same directory but specifies access to
229 // 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
246 // is not the same directory (its name continues further
247 // than we want).
248 return false;
250 break;
251 case '-':
252 // Chop off "/-".
253 sub1 = f1.substring(0, f1.length() - 2);
254 if (f2.length() < sub1.length())
256 // If it's smaller, there is no way it could be part of
257 // this directory.
258 return false;
260 else if (f2.length() > sub1.length()
261 && f2.charAt(sub1.length()) != File.separatorChar)
262 return false;
263 else if (! f2.substring(0, sub1.length()).equals(sub1))
264 return false;
265 break;
267 default:
268 if (f2.charAt(f2.length() - 1) == File.separatorChar)
270 if (! f1.equals(f2.substring(0, f2.length() - 1)))
271 return false;
273 else if (!f1.equals(f2))
274 return false;
275 break;
278 if (readPerm && ! fp.readPerm)
279 return false;
280 if (writePerm && ! fp.writePerm)
281 return false;
282 if (executePerm && ! fp.executePerm)
283 return false;
284 if (deletePerm && ! fp.deletePerm)
285 return false;
287 return true;