source: trunk/gcc/libjava/java/io/FilePermission.java

Last change on this file was 1392, checked in by bird, 21 years ago

This commit was generated by cvs2svn to compensate for changes in r1391,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 8.4 KB
Line 
1/* java.lang.FilePermission
2 Copyright (C) 1998, 2000 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package java.io;
40
41import java.security.*;
42
43
44public final class FilePermission extends Permission implements Serializable
45{
46 static final long serialVersionUID = 7930732926638008763L;
47
48 private static final String CURRENT_DIRECTORY = 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;
55
56 private void cachePerms() {
57 // While race conditions could occur, they don't matter at all.
58
59 String action;
60 int i = actionsString.indexOf(',');
61 int startI = 0;
62 while(i != -1) {
63 action = actionsString.substring(startI,i);
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
73 startI = i+1;
74 i = actionsString.indexOf(',',startI);
75 }
76
77 action = actionsString.substring(startI);
78 if(action.equals("read"))
79 readPerm = true;
80 else if(action.equals("write"))
81 writePerm = true;
82 else if(action.equals("execute"))
83 executePerm = true;
84 else if(action.equals("delete"))
85 deletePerm = true;
86 }
87
88 /** Create a new FilePermission.
89 ** @param pathExpression an expression specifying the paths this
90 ** permission represents.
91 ** @param actionsString a comma-separated list of the actions this
92 ** permission represents.
93 ** @XXX what to do when the file string is malformed?
94 **/
95 public FilePermission(String pathExpression, String actionsString)
96 {
97 super(pathExpression);
98 this.actionsString = actionsString;
99 }
100
101 /** Get the actions this FilePermission supports.
102 ** @return the String representing the actions this FilePermission supports.
103 **/
104 public String getActions() {
105 return actionsString;
106 }
107
108 /** Get the hash code for this Object.<P>
109 ** FilePermission's hash code is calculated as the exclusive or of the target
110 ** String's hash code and the action String's hash code.
111 ** @specnote Sun did not specify how to calculate the hash code; I made this up.
112 ** @return the hash code for this Object.
113 **/
114 public int hashCode() {
115 return getName().hashCode() ^ actionsString.hashCode();
116 }
117
118 /** Check two FilePermissions for semantic equality.
119 ** Two FilePermissions are exactly equivalent if they have identical path
120 ** expressions and have exactly the same access permissions.
121 ** @param o the Object to compare to.
122 ** @return whether the Objects are semantically equivalent.
123 **/
124 public boolean equals(Object o) {
125 if(!(o instanceof FilePermission))
126 return false;
127 FilePermission p = (FilePermission)o;
128 if(!usingPerms)
129 cachePerms();
130 if(!p.usingPerms)
131 p.cachePerms();
132
133 String f1 = getName();
134 String f2 = p.getName();
135
136 /* Compare names, taking into account if they refer to a
137 * directory and one has a separator and the other does not.
138 */
139 if(f1.charAt(f1.length()) == File.separatorChar) {
140 if(f2.charAt(f2.length()) == File.separatorChar) {
141 if(!f2.equals(f1))
142 return false;
143 } else {
144 if(!f2.equals(f1.substring(0,f1.length()-1)))
145 return false;
146 }
147 } else {
148 if(f2.charAt(f2.length()) == File.separatorChar) {
149 if(!f1.equals(f2.substring(0,f2.length()-1)))
150 return false;
151 } else {
152 if(!f1.equals(f2))
153 return false;
154 }
155 }
156 return readPerm == p.readPerm && writePerm == p.writePerm && executePerm == p.executePerm && deletePerm == p.deletePerm;
157 }
158
159 /** Check to see if this permission implies another.
160 ** Permission A implies permission B if these things are all true:
161 ** <OL>
162 ** <LI>A and B are both FilePermissions.</LI>
163 ** <LI>All possible files in B are included in A (possibly more are in A).</LI>
164 ** <LI>All actions B supports, A also supports.</LI>
165 ** </OL>
166 ** @param p the Permission to compare against.
167 ** @return whether this Permission implies p
168 **/
169 public boolean implies(Permission p) {
170 FilePermission fp;
171 if(!(p instanceof FilePermission))
172 return false;
173 fp = (FilePermission)p;
174
175 String f1 = getName();
176 String f2 = fp.getName();
177 if(f1.charAt(0) != File.separatorChar) {
178 f1 = CURRENT_DIRECTORY + f1;
179 }
180 if(f2.charAt(0) != File.separatorChar) {
181 f2 = CURRENT_DIRECTORY + f2;
182 }
183
184 String sub1, sub2a, sub2b;
185 switch(f1.charAt(f1.length() - 1)) {
186 case '*':
187 sub1 = f1.substring(0,f1.length() - 1); // chop off "*"
188 if(f2.length() <= sub1.length()) {
189 /* If it's smaller, there is no way it could be part of this directory.
190 * If it's the same (or length - 1), it could be the same directory but
191 * specifies access to the directory rather than the files in it.
192 */
193 return false;
194 } else if(f2.charAt(sub1.length() - 1) == File.separatorChar) {
195 /* Make sure the part before the "/" is the same */
196 if(!f2.substring(0,sub1.length()).equals(sub1))
197 return false;
198 /* Make sure there are no subdirectories specified underneath this one */
199 String sub2 = f2.substring(sub1.length()+1);
200 if(f2.substring(sub1.length()+1).indexOf(File.separatorChar) != -1)
201 return false;
202 } else {
203 /* Obviously not equal: f2 is either not a directory or is not
204 * the same directory (its name continues further than we want)
205 */
206 return false;
207 }
208 break;
209 case '-':
210 sub1 = f1.substring(0,f1.length() - 2); // chop off "/-"
211 if(f2.length() < sub1.length()) {
212 /* If it's smaller, there is no way it could be part of this directory. */
213 return false;
214 } else if(f2.length() > sub1.length() && f2.charAt(sub1.length()) != File.separatorChar) {
215 return false;
216 } else if(!f2.substring(0,sub1.length()).equals(sub1))
217 return false;
218 break;
219/* Looks redundant with default case and won't compile anyway - arenn
220 case File.separatorChar:
221 if(f2.charAt(f2.length()) == File.separatorChar) {
222 if(!f2.equals(f1))
223 return false;
224 } else {
225 if(!f2.equals(f1.substring(0,f1.length()-1)))
226 return false;
227 }
228 break;
229*/
230 default:
231 if(f2.charAt(f2.length()) == File.separatorChar) {
232 if(!f1.equals(f2.substring(0,f2.length()-1)))
233 return false;
234 } else {
235 if(!f1.equals(f2))
236 return false;
237 }
238 break;
239 }
240
241 if(!usingPerms)
242 cachePerms();
243 if(!fp.usingPerms)
244 fp.cachePerms();
245
246 if(readPerm && !fp.readPerm)
247 return false;
248 if(writePerm && !fp.writePerm)
249 return false;
250 if(executePerm && !fp.executePerm)
251 return false;
252 if(deletePerm && !fp.deletePerm)
253 return false;
254
255 return true;
256 }
257}
Note: See TracBrowser for help on using the repository browser.