source: trunk/src/gcc/libjava/java/io/natFilePosix.cc@ 64

Last change on this file since 64 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 7.4 KB
Line 
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
7This software is copyrighted work licensed under the terms of the
8Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9details. */
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
39jboolean
40java::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
60jboolean
61java::io::File::_stat (jint query)
62{
63 char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
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
83jlong
84java::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
104jstring
105java::io::File::getCanonicalPath (void)
106{
107 char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
108 char buf2[MAXPATHLEN];
109 jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
110 buf[total] = '\0';
111
112#ifdef HAVE_REALPATH
113 if (realpath (buf, buf2) == NULL)
114 throw new IOException (JvNewStringLatin1 (strerror (errno)));
115
116 // FIXME: what encoding to assume for file names? This affects many
117 // calls.
118 return JvNewStringUTF (buf2);
119#else
120 return JvNewStringUTF (buf);
121#endif
122}
123
124jboolean
125java::io::File::isAbsolute (void)
126{
127 return path->charAt(0) == '/';
128}
129
130jobjectArray
131java::io::File::performList (java::io::FilenameFilter *filter,
132 java::io::FileFilter *fileFilter,
133 java::lang::Class *result_type)
134{
135 /* Some systems have dirent.h, but no directory reading functions like
136 opendir. */
137#if defined(HAVE_DIRENT_H) && defined(HAVE_OPENDIR)
138 char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
139 jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
140 buf[total] = '\0';
141
142 DIR *dir = opendir (buf);
143 if (! dir)
144 return NULL;
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
188jboolean
189java::io::File::performMkdir (void)
190{
191 char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
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
202jboolean
203java::io::File::performSetReadOnly (void)
204{
205 char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
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
222JArray< ::java::io::File *>*
223java::io::File::performListRoots ()
224{
225 ::java::io::File *f = new ::java::io::File (JvNewStringLatin1 ("/"));
226 JArray<java::io::File *> *unixroot
227 = reinterpret_cast <JArray<java::io::File *>*>
228 (JvNewObjectArray (1, &java::io::File::class$, f));
229 elements (unixroot) [0] = f;
230 return unixroot;
231}
232
233jboolean
234java::io::File::performRenameTo (File *dest)
235{
236 char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
237 jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
238 buf[total] = '\0';
239 char *buf2
240 = (char *) __builtin_alloca (JvGetStringUTFLength (dest->path) + 1);
241 total = JvGetStringUTFRegion (dest->path, 0, dest->path->length(), buf2);
242 buf2[total] = '\0';
243
244#ifdef HAVE_RENAME
245 return ::rename (buf, buf2) == 0;
246#else
247 return false;
248#endif
249}
250
251jboolean
252java::io::File::performSetLastModified (jlong time)
253{
254#ifdef HAVE_UTIME
255 utimbuf tb;
256
257 char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
258 jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
259 buf[total] = '\0';
260
261 tb.actime = time / 1000;
262 tb.modtime = time / 1000;
263 return ::utime (buf, &tb);
264#else
265 return false;
266#endif
267}
268
269jboolean
270java::io::File::performCreate (void)
271{
272 char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
273 jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
274 buf[total] = '\0';
275
276 int fd = ::open (buf, O_CREAT | O_EXCL, 0644);
277
278 if (fd < 0)
279 {
280 if (errno == EEXIST)
281 return false;
282 throw new IOException (JvNewStringLatin1 (strerror (errno)));
283 }
284 else
285 {
286 ::close (fd);
287 return true;
288 }
289}
290
291jboolean
292java::io::File::performDelete (void)
293{
294 char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
295 jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
296 buf[total] = '\0';
297
298#ifdef HAVE_UNLINK
299#ifdef HAVE_RMDIR
300 if (! ::rmdir (buf))
301 return true;
302 if (errno == ENOTDIR)
303#endif // HAVE_RMDIR
304 return ::unlink (buf) == 0;
305#endif // HAVE_UNLINK
306 return false;
307}
308
309void
310java::io::File::init_native ()
311{
312 maxPathLen = MAXPATHLEN;
313 caseSensitive = true;
314}
Note: See TracBrowser for help on using the repository browser.