FilePermission.java 9.0 KB

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