/libjava/classpath/vm/reference/java/io/VMFile.java

https://bitbucket.org/danchr/llvm-gcc · Java · 225 lines · 62 code · 27 blank · 136 comment · 6 complexity · e7e6e95134a1205496e194d34afcf0e8 MD5 · raw file

  1. /* VMFile.java -- Class for methods natively accessing files
  2. Copyright (C) 2004, 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.io;
  32. import gnu.classpath.Configuration;
  33. import gnu.java.io.PlatformHelper;
  34. /**
  35. * @author Michael Koch (konqueror@gmx.de)
  36. */
  37. final class VMFile
  38. {
  39. // FIXME: We support only case sensitive filesystems currently.
  40. static final boolean IS_CASE_SENSITIVE = true;
  41. static final boolean IS_DOS_8_3 = false;
  42. static
  43. {
  44. if (Configuration.INIT_LOAD_LIBRARY)
  45. {
  46. System.loadLibrary("javaio");
  47. }
  48. }
  49. /*
  50. * This native method does the actual work of getting the last file
  51. * modification time. It also does the existence check to avoid the
  52. * overhead of a call to exists()
  53. */
  54. static native long lastModified(String path);
  55. /*
  56. * This native method sets the permissions to make the file read only.
  57. */
  58. static native boolean setReadOnly(String path);
  59. /**
  60. * This method is used to create a temporary file
  61. */
  62. static native boolean create(String path) throws IOException;
  63. /*
  64. * This native function actually produces the list of file in this
  65. * directory
  66. */
  67. static native String[] list(String dirpath);
  68. /*
  69. * This native method actually performs the rename.
  70. */
  71. static native boolean renameTo(String targetpath, String destpath);
  72. /*
  73. * This native method actually determines the length of the file and
  74. * handles the existence check
  75. */
  76. static native long length(String path);
  77. /*
  78. * This native method does the actual checking of file existence.
  79. */
  80. static native boolean exists(String path);
  81. /*
  82. * This native method handles the actual deleting of the file
  83. */
  84. static native boolean delete(String path);
  85. /*
  86. * This method does the actual setting of the modification time.
  87. */
  88. static native boolean setLastModified(String path, long time);
  89. /*
  90. * This native method actually creates the directory
  91. */
  92. static native boolean mkdir(String dirpath);
  93. /*
  94. * This native method does the actual check of whether or not a file
  95. * is a plain file or not. It also handles the existence check to
  96. * eliminate the overhead of a call to exists()
  97. */
  98. static native boolean isFile(String path);
  99. /**
  100. * This native method checks file permissions for writing
  101. */
  102. static synchronized native boolean canWrite(String path);
  103. /**
  104. * This methods checks if a directory can be written to.
  105. */
  106. static boolean canWriteDirectory(File dir)
  107. {
  108. try
  109. {
  110. String filename = IS_DOS_8_3 ? "tst" : "test-dir-write";
  111. File test = File.createTempFile(filename, null, dir);
  112. return (test != null && test.delete());
  113. }
  114. catch (IOException ioe)
  115. {
  116. return false;
  117. }
  118. }
  119. /**
  120. * This native method checks file permissions for reading
  121. */
  122. static synchronized native boolean canRead(String path);
  123. /*
  124. * This method does the actual check of whether or not a file is a
  125. * directory or not. It also handle the existence check to eliminate
  126. * the overhead of a call to exists()
  127. */
  128. static native boolean isDirectory(String dirpath);
  129. /**
  130. * This method returns an array of filesystem roots. Some operating systems
  131. * have volume oriented filesystem. This method provides a mechanism for
  132. * determining which volumes exist. GNU systems use a single hierarchical
  133. * filesystem, so will have only one "/" filesystem root.
  134. *
  135. * @return An array of <code>File</code> objects for each filesystem root
  136. * available.
  137. *
  138. * @since 1.2
  139. */
  140. static File[] listRoots()
  141. {
  142. File[] roots = new File[1];
  143. roots[0] = new File("/");
  144. return roots;
  145. }
  146. /**
  147. * This method tests whether or not this file represents a "hidden" file.
  148. * On GNU systems, a file is hidden if its name begins with a "."
  149. * character. Files with these names are traditionally not shown with
  150. * directory listing tools.
  151. *
  152. * @return <code>true</code> if the file is hidden, <code>false</code>
  153. * otherwise.
  154. *
  155. * @since 1.2
  156. */
  157. static boolean isHidden(String path)
  158. {
  159. // FIXME: this only works on UNIX
  160. return getName(path).startsWith(".");
  161. }
  162. /**
  163. * This method returns the name of the file. This is everything in the
  164. * complete path of the file after the last instance of the separator
  165. * string.
  166. *
  167. * @return The file name
  168. */
  169. static String getName(String path)
  170. {
  171. int pos = PlatformHelper.lastIndexOfSeparator(path);
  172. if (pos == -1)
  173. return path;
  174. if (PlatformHelper.endWithSeparator(path))
  175. return "";
  176. return path.substring(pos + File.separator.length());
  177. }
  178. /**
  179. * This method returns a canonical representation of the pathname of
  180. * this file. The actual form of the canonical representation is
  181. * system-dependent. On the GNU system, conversion to canonical
  182. * form involves the removal of redundant separators, references to
  183. * "." and "..", and symbolic links.
  184. * <p>
  185. * Note that this method, unlike the other methods which return path
  186. * names, can throw an IOException. This is because native method
  187. * might be required in order to resolve the canonical path
  188. *
  189. * @exception IOException If an error occurs
  190. */
  191. public static native String toCanonicalForm(String path) throws IOException;
  192. }