Merge from mainline.
[official-gcc.git] / libjava / java / io / natFilePosix.cc
blob580b5955ac0546782b8dd6197078bb6472a2b936
1 // natFile.cc - Native part of File class for POSIX.
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 #include <config.h>
13 #include <stdio.h>
14 #include <errno.h>
15 #include <sys/param.h>
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 #include <fcntl.h>
19 #ifdef HAVE_UNISTD_H
20 #include <unistd.h>
21 #endif
22 #include <stdlib.h>
23 #ifdef HAVE_DIRENT_H
24 #include <dirent.h>
25 #endif
26 #include <string.h>
27 #include <utime.h>
29 #include <gcj/cni.h>
30 #include <jvm.h>
31 #include <java/io/File.h>
32 #include <java/io/IOException.h>
33 #include <java/util/ArrayList.h>
34 #include <java/lang/String.h>
35 #include <java/io/FilenameFilter.h>
36 #include <java/io/FileFilter.h>
37 #include <java/lang/System.h>
39 jboolean
40 java::io::File::_access (jint query)
42 char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
43 jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
44 buf[total] = '\0';
45 JvAssert (query == READ || query == WRITE || query == EXISTS);
46 #ifdef HAVE_ACCESS
47 int mode;
48 if (query == READ)
49 mode = R_OK;
50 else if (query == WRITE)
51 mode = W_OK;
52 else
53 mode = F_OK;
54 return ::access (buf, mode) == 0;
55 #else
56 return false;
57 #endif
60 jboolean
61 java::io::File::_stat (jint query)
63 if (query == ISHIDDEN)
64 return getName()->charAt(0) == '.';
66 #ifdef HAVE_STAT
67 char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
68 jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
69 buf[total] = '\0';
71 struct stat sb;
72 if (::stat (buf, &sb))
73 return false;
75 JvAssert (query == DIRECTORY || query == ISFILE);
76 jboolean r = S_ISDIR (sb.st_mode);
77 return query == DIRECTORY ? r : ! r;
78 #else
79 return false;
80 #endif
83 jlong
84 java::io::File::attr (jint query)
86 char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
87 jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
88 buf[total] = '\0';
90 #ifdef HAVE_STAT
91 struct stat sb;
92 // FIXME: not sure about return value here.
93 if (::stat (buf, &sb))
94 return 0;
96 JvAssert (query == MODIFIED || query == LENGTH);
97 return query == MODIFIED ? (jlong)sb.st_mtime * 1000 : sb.st_size;
98 #else
99 // There's no good choice here.
100 return 23;
101 #endif
104 jstring
105 java::io::File::getCanonicalPath (void)
107 // We use `+2' here because we might need to use `.' for our special
108 // case.
109 char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 2);
110 char buf2[MAXPATHLEN];
111 jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
113 // Special case: treat "" the same as ".".
114 if (total == 0)
115 buf[total++] = '.';
117 buf[total] = '\0';
119 #ifdef HAVE_REALPATH
120 if (realpath (buf, buf2) == NULL)
122 // If realpath failed, we have to come up with a canonical path
123 // anyway. We do this with purely textual manipulation.
124 // FIXME: this isn't perfect. You can construct a case where
125 // we get a different answer from the JDK:
126 // mkdir -p /tmp/a/b/c
127 // ln -s /tmp/a/b /tmp/a/z
128 // ... getCanonicalPath("/tmp/a/z/c/nosuchfile")
129 // We will give /tmp/a/z/c/nosuchfile, while the JDK will
130 // give /tmp/a/b/c/nosuchfile.
131 int out_idx;
132 if (buf[0] != '/')
134 // Not absolute, so start with current directory.
135 if (getcwd (buf2, sizeof (buf2)) == NULL)
136 throw new IOException ();
137 out_idx = strlen (buf2);
139 else
141 buf2[0] = '/';
142 out_idx = 1;
144 int in_idx = 0;
145 while (buf[in_idx] != '\0')
147 // Skip '/'s.
148 while (buf[in_idx] == '/')
149 ++in_idx;
150 int elt_start = in_idx;
151 // Find next '/' or end of path.
152 while (buf[in_idx] != '\0' && buf[in_idx] != '/')
153 ++in_idx;
154 if (in_idx == elt_start)
156 // An empty component means we've reached the end.
157 break;
159 int len = in_idx - elt_start;
160 if (len == 1 && buf[in_idx] == '.')
161 continue;
162 if (len == 2 && buf[in_idx] == '.' && buf[in_idx + 1] == '.')
164 // Found ".." component, lop off last part from existing
165 // buffer.
166 --out_idx;
167 while (out_idx > 0 && buf2[out_idx] != '/')
168 --out_idx;
169 // Can't go up past "/".
170 if (out_idx == 0)
171 ++out_idx;
173 else
175 // Append a real path component to the output.
176 if (out_idx > 1)
177 buf2[out_idx++] = '/';
178 strncpy (&buf2[out_idx], &buf[elt_start], len);
179 out_idx += len;
183 buf2[out_idx] = '\0';
186 // FIXME: what encoding to assume for file names? This affects many
187 // calls.
188 return JvNewStringUTF (buf2);
189 #else
190 return JvNewStringUTF (buf);
191 #endif
194 jboolean
195 java::io::File::isAbsolute (void)
197 return path->length() > 0 && path->charAt(0) == '/';
200 jobjectArray
201 java::io::File::performList (java::io::FilenameFilter *filter,
202 java::io::FileFilter *fileFilter,
203 java::lang::Class *result_type)
205 /* Some systems have dirent.h, but no directory reading functions like
206 opendir. */
207 #if defined(HAVE_DIRENT_H) && defined(HAVE_OPENDIR)
208 char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
209 jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
210 buf[total] = '\0';
212 DIR *dir = opendir (buf);
213 if (! dir)
214 return NULL;
216 java::util::ArrayList *list = new java::util::ArrayList ();
217 struct dirent *d;
218 #ifdef HAVE_READDIR_R
219 int name_max = pathconf (buf, _PC_NAME_MAX);
220 char dbuf[sizeof (struct dirent) + name_max + 1];
221 while (readdir_r (dir, (struct dirent *) dbuf, &d) == 0 && d != NULL)
222 #else /* HAVE_READDIR_R */
223 while ((d = readdir (dir)) != NULL)
224 #endif /* HAVE_READDIR_R */
226 // Omit "." and "..".
227 if (d->d_name[0] == '.'
228 && (d->d_name[1] == '\0'
229 || (d->d_name[1] == '.' && d->d_name[2] == '\0')))
230 continue;
232 jstring name = JvNewStringUTF (d->d_name);
233 if (filter && ! filter->accept(this, name))
234 continue;
236 if (result_type == &java::io::File::class$)
238 java::io::File *file = new java::io::File (this, name);
239 if (fileFilter && ! fileFilter->accept(file))
240 continue;
242 list->add(file);
244 else
245 list->add(name);
248 closedir (dir);
250 jobjectArray ret = JvNewObjectArray (list->size(), result_type, NULL);
251 list->toArray(ret);
252 return ret;
253 #else /* HAVE_DIRENT_H && HAVE_OPENDIR */
254 return NULL;
255 #endif /* HAVE_DIRENT_H && HAVE_OPENDIR */
258 jboolean
259 java::io::File::performMkdir (void)
261 char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
262 jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
263 buf[total] = '\0';
265 #ifdef HAVE_MKDIR
266 return ::mkdir (buf, 0755) == 0;
267 #else
268 return false;
269 #endif
272 jboolean
273 java::io::File::performSetReadOnly (void)
275 char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
276 jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
277 buf[total] = '\0';
279 #if defined (HAVE_STAT) && defined (HAVE_CHMOD)
280 struct stat sb;
281 if (::stat (buf, &sb))
282 return false;
284 if (::chmod(buf, sb.st_mode & 0555))
285 return false;
286 return true;
287 #else
288 return false;
289 #endif
292 JArray< ::java::io::File *>*
293 java::io::File::performListRoots ()
295 ::java::io::File *f = new ::java::io::File (JvNewStringLatin1 ("/"));
296 JArray<java::io::File *> *unixroot
297 = reinterpret_cast <JArray<java::io::File *>*>
298 (JvNewObjectArray (1, &java::io::File::class$, f));
299 elements (unixroot) [0] = f;
300 return unixroot;
303 jboolean
304 java::io::File::performRenameTo (File *dest)
306 char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
307 jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
308 buf[total] = '\0';
309 char *buf2
310 = (char *) __builtin_alloca (JvGetStringUTFLength (dest->path) + 1);
311 total = JvGetStringUTFRegion (dest->path, 0, dest->path->length(), buf2);
312 buf2[total] = '\0';
314 #ifdef HAVE_RENAME
315 return ::rename (buf, buf2) == 0;
316 #else
317 return false;
318 #endif
321 jboolean
322 java::io::File::performSetLastModified (jlong time)
324 #ifdef HAVE_UTIME
325 utimbuf tb;
327 char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
328 jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
329 buf[total] = '\0';
331 tb.actime = time / 1000;
332 tb.modtime = time / 1000;
333 return ::utime (buf, &tb);
334 #else
335 return false;
336 #endif
339 jboolean
340 java::io::File::performCreate (void)
342 char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
343 jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
344 buf[total] = '\0';
346 int fd = ::open (buf, O_CREAT | O_EXCL, 0644);
348 if (fd < 0)
350 if (errno == EEXIST)
351 return false;
352 throw new IOException (JvNewStringLatin1 (strerror (errno)));
354 else
356 ::close (fd);
357 return true;
361 jboolean
362 java::io::File::performDelete (void)
364 char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
365 jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
366 buf[total] = '\0';
368 #ifdef HAVE_UNLINK
369 #ifdef HAVE_RMDIR
370 if (! ::rmdir (buf))
371 return true;
372 if (errno == ENOTDIR)
373 #endif // HAVE_RMDIR
374 return ::unlink (buf) == 0;
375 #endif // HAVE_UNLINK
376 return false;
379 void
380 java::io::File::init_native ()
382 maxPathLen = MAXPATHLEN;
383 caseSensitive = true;