1 |
780 |
jeremybenn |
/* VMFile.java -- Class for methods natively accessing files
|
2 |
|
|
Copyright (C) 2004, 2006 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
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.
|
10 |
|
|
|
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.
|
15 |
|
|
|
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., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
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.
|
25 |
|
|
|
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. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
package java.io;
|
40 |
|
|
|
41 |
|
|
import java.net.MalformedURLException;
|
42 |
|
|
import java.net.URL;
|
43 |
|
|
|
44 |
|
|
import gnu.classpath.Configuration;
|
45 |
|
|
import gnu.java.io.PlatformHelper;
|
46 |
|
|
|
47 |
|
|
|
48 |
|
|
/**
|
49 |
|
|
* @author Michael Koch (konqueror@gmx.de)
|
50 |
|
|
*/
|
51 |
|
|
final class VMFile
|
52 |
|
|
{
|
53 |
|
|
// FIXME: We support only case sensitive filesystems currently.
|
54 |
|
|
static final boolean IS_CASE_SENSITIVE = true;
|
55 |
|
|
static final boolean IS_DOS_8_3 = false;
|
56 |
|
|
|
57 |
|
|
static
|
58 |
|
|
{
|
59 |
|
|
if (Configuration.INIT_LOAD_LIBRARY)
|
60 |
|
|
{
|
61 |
|
|
System.loadLibrary("javaio");
|
62 |
|
|
}
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
/*
|
66 |
|
|
* This native method does the actual work of getting the last file
|
67 |
|
|
* modification time. It also does the existence check to avoid the
|
68 |
|
|
* overhead of a call to exists()
|
69 |
|
|
*/
|
70 |
|
|
static native long lastModified(String path);
|
71 |
|
|
|
72 |
|
|
/*
|
73 |
|
|
* This native method sets the permissions to make the file read only.
|
74 |
|
|
*/
|
75 |
|
|
static native boolean setReadOnly(String path);
|
76 |
|
|
|
77 |
|
|
/**
|
78 |
|
|
* This method is used to create a temporary file
|
79 |
|
|
*/
|
80 |
|
|
static native boolean create(String path) throws IOException;
|
81 |
|
|
|
82 |
|
|
/*
|
83 |
|
|
* This native function actually produces the list of files in this
|
84 |
|
|
* directory
|
85 |
|
|
*/
|
86 |
|
|
static native synchronized String[] list(String dirpath);
|
87 |
|
|
|
88 |
|
|
/*
|
89 |
|
|
* This native method actually performs the rename.
|
90 |
|
|
*/
|
91 |
|
|
static native boolean renameTo(String targetpath, String destpath);
|
92 |
|
|
|
93 |
|
|
/*
|
94 |
|
|
* This native method actually determines the length of the file and
|
95 |
|
|
* handles the existence check
|
96 |
|
|
*/
|
97 |
|
|
static native long length(String path);
|
98 |
|
|
|
99 |
|
|
/*
|
100 |
|
|
* This native method does the actual checking of file existence.
|
101 |
|
|
*/
|
102 |
|
|
static native boolean exists(String path);
|
103 |
|
|
|
104 |
|
|
/*
|
105 |
|
|
* This native method handles the actual deleting of the file
|
106 |
|
|
*/
|
107 |
|
|
static native boolean delete(String path);
|
108 |
|
|
|
109 |
|
|
/*
|
110 |
|
|
* This method does the actual setting of the modification time.
|
111 |
|
|
*/
|
112 |
|
|
static native boolean setLastModified(String path, long time);
|
113 |
|
|
|
114 |
|
|
/*
|
115 |
|
|
* This native method actually creates the directory
|
116 |
|
|
*/
|
117 |
|
|
static native boolean mkdir(String dirpath);
|
118 |
|
|
|
119 |
|
|
/**
|
120 |
|
|
* Gets the total bytes of the filesystem named by path.
|
121 |
|
|
*/
|
122 |
|
|
public static native long getTotalSpace(String path);
|
123 |
|
|
|
124 |
|
|
/**
|
125 |
|
|
* Gets the total free bytes of the filesystem named by path.
|
126 |
|
|
*/
|
127 |
|
|
public static native long getFreeSpace(String path);
|
128 |
|
|
|
129 |
|
|
/**
|
130 |
|
|
* Gets the available bytes of the filesystem named by path.
|
131 |
|
|
*/
|
132 |
|
|
public static native long getUsableSpace(String path);
|
133 |
|
|
|
134 |
|
|
/**
|
135 |
|
|
* Set the read permission of the file.
|
136 |
|
|
*/
|
137 |
|
|
public static synchronized native boolean setReadable(String path,
|
138 |
|
|
boolean readable,
|
139 |
|
|
boolean ownerOnly);
|
140 |
|
|
|
141 |
|
|
/**
|
142 |
|
|
* Set the write permission of the file.
|
143 |
|
|
*/
|
144 |
|
|
public static synchronized native boolean setWritable(String path,
|
145 |
|
|
boolean writable,
|
146 |
|
|
boolean ownerOnly);
|
147 |
|
|
|
148 |
|
|
/**
|
149 |
|
|
* Set the execute permission of the file.
|
150 |
|
|
*/
|
151 |
|
|
public static synchronized native boolean setExecutable(String path,
|
152 |
|
|
boolean executable,
|
153 |
|
|
boolean ownerOnly);
|
154 |
|
|
|
155 |
|
|
/*
|
156 |
|
|
* This native method does the actual check of whether or not a file
|
157 |
|
|
* is a plain file or not. It also handles the existence check to
|
158 |
|
|
* eliminate the overhead of a call to exists()
|
159 |
|
|
*/
|
160 |
|
|
static native boolean isFile(String path);
|
161 |
|
|
|
162 |
|
|
/**
|
163 |
|
|
* This native method checks file permissions for writing
|
164 |
|
|
*/
|
165 |
|
|
static synchronized native boolean canWrite(String path);
|
166 |
|
|
|
167 |
|
|
/**
|
168 |
|
|
* This methods checks if a directory can be written to.
|
169 |
|
|
*/
|
170 |
|
|
static native boolean canWriteDirectory(String path);
|
171 |
|
|
|
172 |
|
|
/**
|
173 |
|
|
* This native method checks file permissions for reading
|
174 |
|
|
*/
|
175 |
|
|
static synchronized native boolean canRead(String path);
|
176 |
|
|
|
177 |
|
|
/**
|
178 |
|
|
* This native method checks file permissions for execution
|
179 |
|
|
*/
|
180 |
|
|
static synchronized native boolean canExecute(String path);
|
181 |
|
|
|
182 |
|
|
/*
|
183 |
|
|
* This method does the actual check of whether or not a file is a
|
184 |
|
|
* directory or not. It also handle the existence check to eliminate
|
185 |
|
|
* the overhead of a call to exists()
|
186 |
|
|
*/
|
187 |
|
|
static native boolean isDirectory(String dirpath);
|
188 |
|
|
|
189 |
|
|
/**
|
190 |
|
|
* This methods checks if a directory can be written to.
|
191 |
|
|
*/
|
192 |
|
|
static boolean canWriteDirectory(File path)
|
193 |
|
|
{
|
194 |
|
|
return canWriteDirectory(path.getAbsolutePath());
|
195 |
|
|
}
|
196 |
|
|
|
197 |
|
|
/**
|
198 |
|
|
* This method returns an array of filesystem roots. Some operating systems
|
199 |
|
|
* have volume oriented filesystem. This method provides a mechanism for
|
200 |
|
|
* determining which volumes exist. GNU systems use a single hierarchical
|
201 |
|
|
* filesystem, so will have only one "/" filesystem root.
|
202 |
|
|
*
|
203 |
|
|
* @return An array of <code>File</code> objects for each filesystem root
|
204 |
|
|
* available.
|
205 |
|
|
*
|
206 |
|
|
* @since 1.2
|
207 |
|
|
*/
|
208 |
|
|
static File[] listRoots()
|
209 |
|
|
{
|
210 |
|
|
File[] roots = new File[1];
|
211 |
|
|
roots[0] = new File("/");
|
212 |
|
|
return roots;
|
213 |
|
|
}
|
214 |
|
|
|
215 |
|
|
/**
|
216 |
|
|
* This method tests whether or not this file represents a "hidden" file.
|
217 |
|
|
* On GNU systems, a file is hidden if its name begins with a "."
|
218 |
|
|
* character. Files with these names are traditionally not shown with
|
219 |
|
|
* directory listing tools.
|
220 |
|
|
*
|
221 |
|
|
* @return <code>true</code> if the file is hidden, <code>false</code>
|
222 |
|
|
* otherwise.
|
223 |
|
|
*
|
224 |
|
|
* @since 1.2
|
225 |
|
|
*/
|
226 |
|
|
static boolean isHidden(String path)
|
227 |
|
|
{
|
228 |
|
|
// FIXME: this only works on UNIX
|
229 |
|
|
return getName(path).startsWith(".");
|
230 |
|
|
}
|
231 |
|
|
|
232 |
|
|
/**
|
233 |
|
|
* This method returns the name of the file. This is everything in the
|
234 |
|
|
* complete path of the file after the last instance of the separator
|
235 |
|
|
* string.
|
236 |
|
|
*
|
237 |
|
|
* @return The file name
|
238 |
|
|
*/
|
239 |
|
|
static String getName(String path)
|
240 |
|
|
{
|
241 |
|
|
int pos = PlatformHelper.lastIndexOfSeparator(path);
|
242 |
|
|
if (pos == -1)
|
243 |
|
|
return path;
|
244 |
|
|
|
245 |
|
|
if (PlatformHelper.endWithSeparator(path))
|
246 |
|
|
return "";
|
247 |
|
|
|
248 |
|
|
return path.substring(pos + File.separator.length());
|
249 |
|
|
}
|
250 |
|
|
|
251 |
|
|
/**
|
252 |
|
|
* Returns the path as an absolute path name. The value returned is the
|
253 |
|
|
* current directory plus the separatory string plus the path of the file.
|
254 |
|
|
* The current directory is determined from the <code>user.dir</code> system
|
255 |
|
|
* property.
|
256 |
|
|
*
|
257 |
|
|
* @param path the path to convert to absolute path
|
258 |
|
|
*
|
259 |
|
|
* @return the absolute path that corresponds to <code>path</code>
|
260 |
|
|
*/
|
261 |
|
|
static String getAbsolutePath(String path)
|
262 |
|
|
{
|
263 |
|
|
if (File.separatorChar == '\\'
|
264 |
|
|
&& path.length() > 0 && path.charAt (0) == '\\')
|
265 |
|
|
{
|
266 |
|
|
// On Windows, even if the path starts with a '\\' it is not
|
267 |
|
|
// really absolute until we prefix the drive specifier from
|
268 |
|
|
// the current working directory to it.
|
269 |
|
|
return System.getProperty ("user.dir").substring (0, 2) + path;
|
270 |
|
|
}
|
271 |
|
|
else if (File.separatorChar == '\\'
|
272 |
|
|
&& path.length() > 1 && path.charAt (1) == ':'
|
273 |
|
|
&& ((path.charAt (0) >= 'a' && path.charAt (0) <= 'z')
|
274 |
|
|
|| (path.charAt (0) >= 'A' && path.charAt (0) <= 'Z')))
|
275 |
|
|
{
|
276 |
|
|
// On Windows, a process has a current working directory for
|
277 |
|
|
// each drive and a path like "G:foo\bar" would mean the
|
278 |
|
|
// absolute path "G:\wombat\foo\bar" if "\wombat" is the
|
279 |
|
|
// working directory on the G drive.
|
280 |
|
|
String drvDir = null;
|
281 |
|
|
try
|
282 |
|
|
{
|
283 |
|
|
drvDir = new File (path.substring (0, 2)).getCanonicalPath();
|
284 |
|
|
}
|
285 |
|
|
catch (IOException e)
|
286 |
|
|
{
|
287 |
|
|
drvDir = path.substring (0, 2) + "\\";
|
288 |
|
|
}
|
289 |
|
|
|
290 |
|
|
// Note: this would return "C:\\." for the path "C:.", if "\"
|
291 |
|
|
// is the working folder on the C drive, but this is
|
292 |
|
|
// consistent with what Sun's JRE 1.4.1.01 actually returns!
|
293 |
|
|
if (path.length() > 2)
|
294 |
|
|
return drvDir + '\\' + path.substring (2, path.length());
|
295 |
|
|
else
|
296 |
|
|
return drvDir;
|
297 |
|
|
}
|
298 |
|
|
else if (path.equals(""))
|
299 |
|
|
return System.getProperty ("user.dir");
|
300 |
|
|
else
|
301 |
|
|
return System.getProperty ("user.dir") + File.separatorChar + path;
|
302 |
|
|
}
|
303 |
|
|
|
304 |
|
|
/**
|
305 |
|
|
* This method returns true if the path represents an absolute file
|
306 |
|
|
* path and false if it does not. The definition of an absolute path varies
|
307 |
|
|
* by system. As an example, on GNU systems, a path is absolute if it starts
|
308 |
|
|
* with a "/".
|
309 |
|
|
*
|
310 |
|
|
* @param path the path to check
|
311 |
|
|
*
|
312 |
|
|
* @return <code>true</code> if path represents an absolute file name,
|
313 |
|
|
* <code>false</code> otherwise.
|
314 |
|
|
*/
|
315 |
|
|
static boolean isAbsolute(String path)
|
316 |
|
|
{
|
317 |
|
|
if (File.separatorChar == '\\')
|
318 |
|
|
return path.startsWith(File.separator + File.separator)
|
319 |
|
|
|| (path.length() > 2
|
320 |
|
|
&& ((path.charAt(0) >= 'a' && path.charAt(0) <= 'z')
|
321 |
|
|
|| (path.charAt(0) >= 'A' && path.charAt(0) <= 'Z'))
|
322 |
|
|
&& path.charAt(1) == ':'
|
323 |
|
|
&& path.charAt(2) == '\\');
|
324 |
|
|
else
|
325 |
|
|
return path.startsWith(File.separator);
|
326 |
|
|
}
|
327 |
|
|
|
328 |
|
|
/**
|
329 |
|
|
* Returns a <code>URL</code> with the <code>file:</code>
|
330 |
|
|
* protocol that represents this file. The exact form of this URL is
|
331 |
|
|
* system dependent.
|
332 |
|
|
*
|
333 |
|
|
* @param file the file to convert to URL
|
334 |
|
|
*
|
335 |
|
|
* @return a <code>URL</code> for this object.
|
336 |
|
|
*
|
337 |
|
|
* @throws MalformedURLException if the URL cannot be created
|
338 |
|
|
* successfully.
|
339 |
|
|
*/
|
340 |
|
|
static URL toURL(File file)
|
341 |
|
|
throws MalformedURLException
|
342 |
|
|
{
|
343 |
|
|
// On Win32, Sun's JDK returns URLs of the form "file:/c:/foo/bar.txt",
|
344 |
|
|
// while on UNIX, it returns URLs of the form "file:/foo/bar.txt".
|
345 |
|
|
if (File.separatorChar == '\\')
|
346 |
|
|
return new URL ("file:/" + file.getAbsolutePath().replace ('\\', '/')
|
347 |
|
|
+ (file.isDirectory() ? "/" : ""));
|
348 |
|
|
else
|
349 |
|
|
return new URL ("file:" + file.getAbsolutePath()
|
350 |
|
|
+ (file.isDirectory() ? "/" : ""));
|
351 |
|
|
}
|
352 |
|
|
|
353 |
|
|
/**
|
354 |
|
|
* This method returns a canonical representation of the pathname of
|
355 |
|
|
* this file. The actual form of the canonical representation is
|
356 |
|
|
* system-dependent. On the GNU system, conversion to canonical
|
357 |
|
|
* form involves the removal of redundant separators, references to
|
358 |
|
|
* "." and "..", and symbolic links.
|
359 |
|
|
* <p>
|
360 |
|
|
* Note that this method, unlike the other methods which return path
|
361 |
|
|
* names, can throw an IOException. This is because native method
|
362 |
|
|
* might be required in order to resolve the canonical path
|
363 |
|
|
*
|
364 |
|
|
* @exception IOException If an error occurs
|
365 |
|
|
*/
|
366 |
|
|
public static native String toCanonicalForm(String path) throws IOException;
|
367 |
|
|
}
|