OSDN Git Service

* java/io/natFilePosix.cc (getCanonicalPath): Handle case where
[pf3gnuchains/gcc-fork.git] / libjava / java / io / natFilePosix.cc
1 // natFile.cc - Native part of File class for POSIX.
2
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003  Free Software Foundation
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 #include <config.h>
12
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>
28
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>
38
39 jboolean
40 java::io::File::_access (jint query)
41 {
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
58 }
59
60 jboolean
61 java::io::File::_stat (jint query)
62 {
63   if (query == ISHIDDEN)
64     return getName()->charAt(0) == '.';
65
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';
70
71   struct stat sb;
72   if (::stat (buf, &sb))
73     return false;
74   
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
81 }
82
83 jlong
84 java::io::File::attr (jint query)
85 {
86   char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
87   jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
88   buf[total] = '\0';
89
90 #ifdef HAVE_STAT
91   struct stat sb;
92   // FIXME: not sure about return value here.
93   if (::stat (buf, &sb))
94     return 0;
95
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
102 }
103
104 jstring
105 java::io::File::getCanonicalPath (void)
106 {
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);
112
113   // Special case: treat "" the same as ".".
114   if (total == 0)
115     buf[total++] = '.';
116
117   buf[total] = '\0';
118
119 #ifdef HAVE_REALPATH
120   if (realpath (buf, buf2) == NULL)
121     {
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] != '/')
133         {
134           // Not absolute, so start with current directory.
135           if (getcwd (buf2, sizeof (buf2)) == NULL)
136             throw new IOException ();
137           out_idx = strlen (buf2);
138         }
139       else
140         {
141           buf2[0] = '/';
142           out_idx = 1;
143         } 
144       int in_idx = 0;
145       while (buf[in_idx] != '\0')
146         {
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)
155             {
156               // An empty component means we've reached the end.
157               break;
158             }
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] == '.')
163             {
164               // Found ".." component, lop off last part from existing
165               // buffer.
166               --out_idx;
167               while (out_idx > 0 && buf[out_idx] != '/')
168                 --out_idx;
169               // Can't go up past "/".
170               if (out_idx == 0)
171                 ++out_idx;
172             }
173           else
174             {
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;
180             }
181         }
182       buf[out_idx] = '\0';
183     }
184
185   // FIXME: what encoding to assume for file names?  This affects many
186   // calls.
187   return JvNewStringUTF (buf2);
188 #else
189   return JvNewStringUTF (buf);
190 #endif
191 }
192
193 jboolean
194 java::io::File::isAbsolute (void)
195 {
196   return path->length() > 0 && path->charAt(0) == '/';
197 }
198
199 jobjectArray
200 java::io::File::performList (java::io::FilenameFilter *filter, 
201                              java::io::FileFilter *fileFilter, 
202                              java::lang::Class *result_type)
203 {
204   /* Some systems have dirent.h, but no directory reading functions like
205      opendir.  */
206 #if defined(HAVE_DIRENT_H) && defined(HAVE_OPENDIR)
207   char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
208   jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
209   buf[total] = '\0';
210
211   DIR *dir = opendir (buf);
212   if (! dir)
213     return NULL;
214
215   java::util::ArrayList *list = new java::util::ArrayList ();
216   struct dirent *d;
217 #ifdef HAVE_READDIR_R
218   int name_max = pathconf (buf, _PC_NAME_MAX);
219   char dbuf[sizeof (struct dirent) + name_max + 1];
220   while (readdir_r (dir, (struct dirent *) dbuf, &d) == 0 && d != NULL)
221 #else /* HAVE_READDIR_R */
222   while ((d = readdir (dir)) != NULL)
223 #endif /* HAVE_READDIR_R */
224     {
225       // Omit "." and "..".
226       if (d->d_name[0] == '.'
227           && (d->d_name[1] == '\0'
228               || (d->d_name[1] == '.' && d->d_name[2] == '\0')))
229         continue;
230
231       jstring name = JvNewStringUTF (d->d_name);
232       if (filter && ! filter->accept(this, name))
233         continue;
234
235       if (result_type == &java::io::File::class$)
236         {
237           java::io::File *file = new java::io::File (this, name);
238           if (fileFilter && ! fileFilter->accept(file))
239             continue;
240
241           list->add(file);
242         }
243       else
244         list->add(name);
245     }
246
247   closedir (dir);
248
249   jobjectArray ret = JvNewObjectArray (list->size(), result_type, NULL);
250   list->toArray(ret);
251   return ret;
252 #else /* HAVE_DIRENT_H && HAVE_OPENDIR */
253   return NULL;
254 #endif /* HAVE_DIRENT_H && HAVE_OPENDIR */
255 }
256
257 jboolean
258 java::io::File::performMkdir (void)
259 {
260   char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
261   jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
262   buf[total] = '\0';
263
264 #ifdef HAVE_MKDIR
265   return ::mkdir (buf, 0755) == 0;
266 #else
267   return false;
268 #endif
269 }
270
271 jboolean
272 java::io::File::performSetReadOnly (void)
273 {
274   char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
275   jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
276   buf[total] = '\0';
277
278 #if defined (HAVE_STAT) && defined (HAVE_CHMOD)
279   struct stat sb;
280   if (::stat (buf, &sb))
281     return false;
282
283   if (::chmod(buf, sb.st_mode & 0555))
284     return false;  
285   return true;
286 #else
287   return false;
288 #endif
289 }
290
291 JArray< ::java::io::File *>*
292 java::io::File::performListRoots ()
293 {
294   ::java::io::File *f = new ::java::io::File (JvNewStringLatin1 ("/"));
295   JArray<java::io::File *> *unixroot
296     = reinterpret_cast <JArray<java::io::File *>*> 
297           (JvNewObjectArray (1, &java::io::File::class$, f));
298   elements (unixroot) [0] = f;
299   return unixroot;
300 }
301
302 jboolean
303 java::io::File::performRenameTo (File *dest)
304 {
305   char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
306   jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
307   buf[total] = '\0';
308   char *buf2
309     = (char *) __builtin_alloca (JvGetStringUTFLength (dest->path) + 1);
310   total = JvGetStringUTFRegion (dest->path, 0, dest->path->length(), buf2);
311   buf2[total] = '\0';
312
313 #ifdef HAVE_RENAME
314   return ::rename (buf, buf2) == 0;
315 #else
316   return false;
317 #endif
318 }
319
320 jboolean
321 java::io::File::performSetLastModified (jlong time)
322 {
323 #ifdef HAVE_UTIME
324   utimbuf tb;
325
326   char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
327   jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
328   buf[total] = '\0';
329   
330   tb.actime = time / 1000;
331   tb.modtime = time / 1000;
332   return ::utime (buf, &tb);
333 #else
334   return false;
335 #endif
336 }
337
338 jboolean
339 java::io::File::performCreate (void)
340 {
341   char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
342   jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
343   buf[total] = '\0';
344
345   int fd = ::open (buf, O_CREAT | O_EXCL, 0644);
346
347   if (fd < 0)
348     {
349       if (errno == EEXIST)
350         return false;
351       throw new IOException (JvNewStringLatin1 (strerror (errno)));
352     }
353   else
354     {
355       ::close (fd);
356       return true;
357     }
358 }
359
360 jboolean
361 java::io::File::performDelete (void)
362 {
363   char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
364   jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
365   buf[total] = '\0';
366
367 #ifdef HAVE_UNLINK
368 #ifdef HAVE_RMDIR
369   if (! ::rmdir (buf))
370     return true;
371   if (errno == ENOTDIR)
372 #endif // HAVE_RMDIR
373     return ::unlink (buf) == 0;
374 #endif // HAVE_UNLINK
375   return false;
376 }
377
378 void
379 java::io::File::init_native ()
380 {
381   maxPathLen = MAXPATHLEN;
382   caseSensitive = true;
383 }