OSDN Git Service

embedded updates
[pf3gnuchains/gcc-fork.git] / libjava / java / io / natFile.cc
1 // natFile.cc - Native part of File class for POSIX.
2
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002  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[MAXPATHLEN];
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   char buf[MAXPATHLEN];
64   jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
65   buf[total] = '\0';
66
67   if (query == ISHIDDEN)
68     return (getName()->charAt(0) == '.');
69
70 #ifdef HAVE_STAT
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[MAXPATHLEN];
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   char buf[MAXPATHLEN], buf2[MAXPATHLEN];
108   jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
109   buf[total] = '\0';
110
111 #ifdef HAVE_REALPATH
112   if (realpath (buf, buf2) == NULL)
113     throw new IOException (JvNewStringLatin1 (strerror (errno)));
114
115   // FIXME: what encoding to assume for file names?  This affects many
116   // calls.
117   return JvNewStringUTF (buf2);
118 #else
119   return JvNewStringUTF (buf);
120 #endif
121 }
122
123 jboolean
124 java::io::File::isAbsolute (void)
125 {
126   return path->charAt(0) == '/';
127 }
128
129 jobjectArray
130 java::io::File::performList (java::io::FilenameFilter *filter, 
131                              java::io::FileFilter *fileFilter, 
132                              java::lang::Class *result_type)
133 {
134   /* Some systems have dirent.h, but no directory reading functions like
135      opendir.  */
136 #if defined(HAVE_DIRENT_H) && defined(HAVE_OPENDIR)
137   char buf[MAXPATHLEN];
138   jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
139   buf[total] = '\0';
140
141   DIR *dir = opendir (buf);
142   if (! dir)
143     return NULL;
144
145
146   java::util::ArrayList *list = new java::util::ArrayList ();
147   struct dirent *d;
148 #ifdef HAVE_READDIR_R
149   int name_max = pathconf (buf, _PC_NAME_MAX);
150   char dbuf[sizeof (struct dirent) + name_max + 1];
151   while (readdir_r (dir, (struct dirent *) dbuf, &d) == 0 && d != NULL)
152 #else /* HAVE_READDIR_R */
153   while ((d = readdir (dir)) != NULL)
154 #endif /* HAVE_READDIR_R */
155     {
156       // Omit "." and "..".
157       if (d->d_name[0] == '.'
158           && (d->d_name[1] == '\0'
159               || (d->d_name[1] == '.' && d->d_name[2] == '\0')))
160         continue;
161
162       jstring name = JvNewStringUTF (d->d_name);
163       if (filter && ! filter->accept(this, name))
164         continue;
165       
166       if (result_type == &java::io::File::class$)
167         {
168           java::io::File *file = new java::io::File (this, name);
169           if (fileFilter && ! fileFilter->accept(file))
170             continue;
171
172           list->add(file);
173         }
174       else
175         list->add(name);
176     }
177
178   closedir (dir);
179
180   jobjectArray ret = JvNewObjectArray (list->size(), result_type, NULL);
181   list->toArray(ret);
182   return ret;
183 #else /* HAVE_DIRENT_H && HAVE_OPENDIR */
184   return NULL;
185 #endif /* HAVE_DIRENT_H && HAVE_OPENDIR */
186 }
187
188 jboolean
189 java::io::File::performMkdir (void)
190 {
191   char buf[MAXPATHLEN];
192   jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
193   buf[total] = '\0';
194
195 #ifdef HAVE_MKDIR
196   return ::mkdir (buf, 0755) == 0;
197 #else
198   return false;
199 #endif
200 }
201
202 jboolean
203 java::io::File::performSetReadOnly (void)
204 {
205   char buf[MAXPATHLEN];
206   jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
207   buf[total] = '\0';
208
209 #if defined (HAVE_STAT) && defined (HAVE_CHMOD)
210   struct stat sb;
211   if (::stat (buf, &sb))
212     return false;
213
214   if (::chmod(buf, sb.st_mode & 0555))
215     return false;  
216   return true;
217 #else
218   return false;
219 #endif
220 }
221
222 static JArray<java::io::File *> *unixroot;
223
224 JArray< ::java::io::File *>*
225 java::io::File::performListRoots ()
226 {
227   if (unixroot == NULL)
228     {
229       ::java::io::File *f = new ::java::io::File (JvNewStringLatin1 ("/"));
230       unixroot = reinterpret_cast <JArray<java::io::File *>*> 
231                    (JvNewObjectArray (1, &java::io::File::class$, f));
232       elements (unixroot) [0] = f;
233     }
234   return unixroot;
235 }
236
237 jboolean
238 java::io::File::performRenameTo (File *dest)
239 {
240   char buf[MAXPATHLEN];
241   jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
242   buf[total] = '\0';
243   char buf2[MAXPATHLEN];
244   total = JvGetStringUTFRegion (dest->path, 0, dest->path->length(), buf2);
245   buf2[total] = '\0';
246
247 #ifdef HAVE_RENAME
248   return ::rename (buf, buf2) == 0;
249 #else
250   return false;
251 #endif
252 }
253
254 jboolean
255 java::io::File::performSetLastModified (jlong time)
256 {
257 #ifdef HAVE_UTIME
258   utimbuf tb;
259
260   char buf[MAXPATHLEN];
261   jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
262   buf[total] = '\0';
263   
264   tb.actime = time / 1000;
265   tb.modtime = time / 1000;
266   return ::utime (buf, &tb);
267 #else
268   return false;
269 #endif
270 }
271
272 jboolean
273 java::io::File::performCreate (void)
274 {
275   char buf[MAXPATHLEN];
276   jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
277   buf[total] = '\0';
278
279   int fd = ::open (buf, O_CREAT | O_EXCL, 0644);
280
281   if (fd < 0)
282     {
283       if (errno == EEXIST)
284         return false;
285       throw new IOException (JvNewStringLatin1 (strerror (errno)));
286     }
287   else
288     {
289       ::close (fd);
290       return true;
291     }
292 }
293
294 jboolean
295 java::io::File::performDelete (void)
296 {
297   char buf[MAXPATHLEN];
298   jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
299   buf[total] = '\0';
300
301 #ifdef HAVE_UNLINK
302 #ifdef HAVE_RMDIR
303   if (! ::rmdir (buf))
304     return true;
305   if (errno == ENOTDIR)
306 #endif // HAVE_RMDIR
307     return ::unlink (buf) == 0;
308 #endif // HAVE_UNLINK
309   return false;
310 }
311
312 void
313 java::io::File::init_native ()
314 {
315   maxPathLen = MAXPATHLEN;
316   caseSensitive = true;
317 }