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 | #ifndef MAXPATHLEN
|
---|
30 | # define MAXPATHLEN 1024
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | #include <gcj/cni.h>
|
---|
34 | #include <jvm.h>
|
---|
35 | #include <java/io/File.h>
|
---|
36 | #include <java/io/IOException.h>
|
---|
37 | #include <java/util/ArrayList.h>
|
---|
38 | #include <java/lang/String.h>
|
---|
39 | #include <java/io/FilenameFilter.h>
|
---|
40 | #include <java/io/FileFilter.h>
|
---|
41 | #include <java/lang/System.h>
|
---|
42 |
|
---|
43 | #ifdef _SCO_DS
|
---|
44 | # undef HAVE_READDIR_R
|
---|
45 | #endif
|
---|
46 |
|
---|
47 | jboolean
|
---|
48 | java::io::File::_access (jint query)
|
---|
49 | {
|
---|
50 | char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
|
---|
51 | jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
|
---|
52 | buf[total] = '\0';
|
---|
53 | JvAssert (query == READ || query == WRITE || query == EXISTS);
|
---|
54 | #ifdef HAVE_ACCESS
|
---|
55 | int mode;
|
---|
56 | if (query == READ)
|
---|
57 | mode = R_OK;
|
---|
58 | else if (query == WRITE)
|
---|
59 | mode = W_OK;
|
---|
60 | else
|
---|
61 | mode = F_OK;
|
---|
62 | return ::access (buf, mode) == 0;
|
---|
63 | #else
|
---|
64 | return false;
|
---|
65 | #endif
|
---|
66 | }
|
---|
67 |
|
---|
68 | jboolean
|
---|
69 | java::io::File::_stat (jint query)
|
---|
70 | {
|
---|
71 | if (query == ISHIDDEN)
|
---|
72 | return getName()->charAt(0) == '.';
|
---|
73 |
|
---|
74 | #ifdef HAVE_STAT
|
---|
75 | char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
|
---|
76 | jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
|
---|
77 | buf[total] = '\0';
|
---|
78 |
|
---|
79 | struct stat sb;
|
---|
80 | if (::stat (buf, &sb))
|
---|
81 | return false;
|
---|
82 |
|
---|
83 | JvAssert (query == DIRECTORY || query == ISFILE);
|
---|
84 | jboolean r = S_ISDIR (sb.st_mode);
|
---|
85 | return query == DIRECTORY ? r : ! r;
|
---|
86 | #else
|
---|
87 | return false;
|
---|
88 | #endif
|
---|
89 | }
|
---|
90 |
|
---|
91 | jlong
|
---|
92 | java::io::File::attr (jint query)
|
---|
93 | {
|
---|
94 | char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
|
---|
95 | jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
|
---|
96 | buf[total] = '\0';
|
---|
97 |
|
---|
98 | #ifdef HAVE_STAT
|
---|
99 | struct stat sb;
|
---|
100 | // FIXME: not sure about return value here.
|
---|
101 | if (::stat (buf, &sb))
|
---|
102 | return 0;
|
---|
103 |
|
---|
104 | JvAssert (query == MODIFIED || query == LENGTH);
|
---|
105 | return query == MODIFIED ? (jlong)sb.st_mtime * 1000 : sb.st_size;
|
---|
106 | #else
|
---|
107 | // There's no good choice here.
|
---|
108 | return 23;
|
---|
109 | #endif
|
---|
110 | }
|
---|
111 |
|
---|
112 | jstring
|
---|
113 | java::io::File::getCanonicalPath (void)
|
---|
114 | {
|
---|
115 | // We use `+2' here because we might need to use `.' for our special
|
---|
116 | // case.
|
---|
117 | char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 2);
|
---|
118 | char buf2[MAXPATHLEN];
|
---|
119 | jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
|
---|
120 |
|
---|
121 | // Special case: treat "" the same as ".".
|
---|
122 | if (total == 0)
|
---|
123 | buf[total++] = '.';
|
---|
124 |
|
---|
125 | buf[total] = '\0';
|
---|
126 |
|
---|
127 | #ifdef HAVE_REALPATH
|
---|
128 | if (realpath (buf, buf2) == NULL)
|
---|
129 | throw new IOException (JvNewStringLatin1 (strerror (errno)));
|
---|
130 |
|
---|
131 | // FIXME: what encoding to assume for file names? This affects many
|
---|
132 | // calls.
|
---|
133 | return JvNewStringUTF (buf2);
|
---|
134 | #else
|
---|
135 | return JvNewStringUTF (buf);
|
---|
136 | #endif
|
---|
137 | }
|
---|
138 |
|
---|
139 | jboolean
|
---|
140 | java::io::File::isAbsolute (void)
|
---|
141 | {
|
---|
142 | return path->length() > 0 && path->charAt(0) == '/';
|
---|
143 | }
|
---|
144 |
|
---|
145 | jobjectArray
|
---|
146 | java::io::File::performList (java::io::FilenameFilter *filter,
|
---|
147 | java::io::FileFilter *fileFilter,
|
---|
148 | java::lang::Class *result_type)
|
---|
149 | {
|
---|
150 | /* Some systems have dirent.h, but no directory reading functions like
|
---|
151 | opendir. */
|
---|
152 | #if defined(HAVE_DIRENT_H) && defined(HAVE_OPENDIR)
|
---|
153 | char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
|
---|
154 | jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
|
---|
155 | buf[total] = '\0';
|
---|
156 |
|
---|
157 | DIR *dir = opendir (buf);
|
---|
158 | if (! dir)
|
---|
159 | return NULL;
|
---|
160 |
|
---|
161 | java::util::ArrayList *list = new java::util::ArrayList ();
|
---|
162 | struct dirent *d;
|
---|
163 | #ifdef HAVE_READDIR_R
|
---|
164 | int name_max = pathconf (buf, _PC_NAME_MAX);
|
---|
165 | char dbuf[sizeof (struct dirent) + name_max + 1];
|
---|
166 | while (readdir_r (dir, (struct dirent *) dbuf, &d) == 0 && d != NULL)
|
---|
167 | #else /* HAVE_READDIR_R */
|
---|
168 | while ((d = readdir (dir)) != NULL)
|
---|
169 | #endif /* HAVE_READDIR_R */
|
---|
170 | {
|
---|
171 | // Omit "." and "..".
|
---|
172 | if (d->d_name[0] == '.'
|
---|
173 | && (d->d_name[1] == '\0'
|
---|
174 | || (d->d_name[1] == '.' && d->d_name[2] == '\0')))
|
---|
175 | continue;
|
---|
176 |
|
---|
177 | jstring name = JvNewStringUTF (d->d_name);
|
---|
178 | if (filter && ! filter->accept(this, name))
|
---|
179 | continue;
|
---|
180 |
|
---|
181 | if (result_type == &java::io::File::class$)
|
---|
182 | {
|
---|
183 | java::io::File *file = new java::io::File (this, name);
|
---|
184 | if (fileFilter && ! fileFilter->accept(file))
|
---|
185 | continue;
|
---|
186 |
|
---|
187 | list->add(file);
|
---|
188 | }
|
---|
189 | else
|
---|
190 | list->add(name);
|
---|
191 | }
|
---|
192 |
|
---|
193 | closedir (dir);
|
---|
194 |
|
---|
195 | jobjectArray ret = JvNewObjectArray (list->size(), result_type, NULL);
|
---|
196 | list->toArray(ret);
|
---|
197 | return ret;
|
---|
198 | #else /* HAVE_DIRENT_H && HAVE_OPENDIR */
|
---|
199 | return NULL;
|
---|
200 | #endif /* HAVE_DIRENT_H && HAVE_OPENDIR */
|
---|
201 | }
|
---|
202 |
|
---|
203 | jboolean
|
---|
204 | java::io::File::performMkdir (void)
|
---|
205 | {
|
---|
206 | char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
|
---|
207 | jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
|
---|
208 | buf[total] = '\0';
|
---|
209 |
|
---|
210 | #ifdef HAVE_MKDIR
|
---|
211 | return ::mkdir (buf, 0755) == 0;
|
---|
212 | #else
|
---|
213 | return false;
|
---|
214 | #endif
|
---|
215 | }
|
---|
216 |
|
---|
217 | jboolean
|
---|
218 | java::io::File::performSetReadOnly (void)
|
---|
219 | {
|
---|
220 | char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
|
---|
221 | jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
|
---|
222 | buf[total] = '\0';
|
---|
223 |
|
---|
224 | #if defined (HAVE_STAT) && defined (HAVE_CHMOD)
|
---|
225 | struct stat sb;
|
---|
226 | if (::stat (buf, &sb))
|
---|
227 | return false;
|
---|
228 |
|
---|
229 | if (::chmod(buf, sb.st_mode & 0555))
|
---|
230 | return false;
|
---|
231 | return true;
|
---|
232 | #else
|
---|
233 | return false;
|
---|
234 | #endif
|
---|
235 | }
|
---|
236 |
|
---|
237 | JArray< ::java::io::File *>*
|
---|
238 | java::io::File::performListRoots ()
|
---|
239 | {
|
---|
240 | ::java::io::File *f = new ::java::io::File (JvNewStringLatin1 ("/"));
|
---|
241 | JArray<java::io::File *> *unixroot
|
---|
242 | = reinterpret_cast <JArray<java::io::File *>*>
|
---|
243 | (JvNewObjectArray (1, &java::io::File::class$, f));
|
---|
244 | elements (unixroot) [0] = f;
|
---|
245 | return unixroot;
|
---|
246 | }
|
---|
247 |
|
---|
248 | jboolean
|
---|
249 | java::io::File::performRenameTo (File *dest)
|
---|
250 | {
|
---|
251 | char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
|
---|
252 | jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
|
---|
253 | buf[total] = '\0';
|
---|
254 | char *buf2
|
---|
255 | = (char *) __builtin_alloca (JvGetStringUTFLength (dest->path) + 1);
|
---|
256 | total = JvGetStringUTFRegion (dest->path, 0, dest->path->length(), buf2);
|
---|
257 | buf2[total] = '\0';
|
---|
258 |
|
---|
259 | #ifdef HAVE_RENAME
|
---|
260 | return ::rename (buf, buf2) == 0;
|
---|
261 | #else
|
---|
262 | return false;
|
---|
263 | #endif
|
---|
264 | }
|
---|
265 |
|
---|
266 | jboolean
|
---|
267 | java::io::File::performSetLastModified (jlong time)
|
---|
268 | {
|
---|
269 | #ifdef HAVE_UTIME
|
---|
270 | utimbuf tb;
|
---|
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 | tb.actime = time / 1000;
|
---|
277 | tb.modtime = time / 1000;
|
---|
278 | return ::utime (buf, &tb);
|
---|
279 | #else
|
---|
280 | return false;
|
---|
281 | #endif
|
---|
282 | }
|
---|
283 |
|
---|
284 | jboolean
|
---|
285 | java::io::File::performCreate (void)
|
---|
286 | {
|
---|
287 | char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
|
---|
288 | jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
|
---|
289 | buf[total] = '\0';
|
---|
290 |
|
---|
291 | int fd = ::open (buf, O_CREAT | O_EXCL, 0644);
|
---|
292 |
|
---|
293 | if (fd < 0)
|
---|
294 | {
|
---|
295 | if (errno == EEXIST)
|
---|
296 | return false;
|
---|
297 | throw new IOException (JvNewStringLatin1 (strerror (errno)));
|
---|
298 | }
|
---|
299 | else
|
---|
300 | {
|
---|
301 | ::close (fd);
|
---|
302 | return true;
|
---|
303 | }
|
---|
304 | }
|
---|
305 |
|
---|
306 | jboolean
|
---|
307 | java::io::File::performDelete (void)
|
---|
308 | {
|
---|
309 | char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
|
---|
310 | jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
|
---|
311 | buf[total] = '\0';
|
---|
312 |
|
---|
313 | #ifdef HAVE_UNLINK
|
---|
314 | #ifdef HAVE_RMDIR
|
---|
315 | if (! ::rmdir (buf))
|
---|
316 | return true;
|
---|
317 | if (errno == ENOTDIR)
|
---|
318 | #endif // HAVE_RMDIR
|
---|
319 | return ::unlink (buf) == 0;
|
---|
320 | #endif // HAVE_UNLINK
|
---|
321 | return false;
|
---|
322 | }
|
---|
323 |
|
---|
324 | void
|
---|
325 | java::io::File::init_native ()
|
---|
326 | {
|
---|
327 | maxPathLen = MAXPATHLEN;
|
---|
328 | caseSensitive = true;
|
---|
329 | }
|
---|