/[classpath]/classpath/native/jni/java-io/java_io_File.c
ViewVC logotype

Contents of /classpath/native/jni/java-io/java_io_File.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (show annotations) (download)
Fri Oct 25 12:43:24 2002 UTC (21 years, 5 months ago) by mark
Branch: MAIN
CVS Tags: classpath-0_05-release
Changes since 1.4: +0 -7 lines
File MIME type: text/plain
    * java/io/File.java: Remove all commented out security checks.
    (listFiles(FilenameFilter)): Return null when filelist == null.
    (listFiles(FileFilter)): Return null when fobjlist == null, return
    filelist when filter == null.
    * native/jni/java-io/java_io_File.c: Return empty array for empty dir.

1 /* File.c - Native methods for java.io.File class
2 Copyright (C) 1998 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
37
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <errno.h>
43 #include <sys/types.h>
44 #include <fcntl.h>
45 #include <utime.h>
46 #include <unistd.h>
47 #include <sys/stat.h>
48 #include <dirent.h>
49
50 #include <jni.h>
51 #include <jcl.h>
52 #include "javaio.h"
53
54 #include "java_io_File.h"
55
56 /*************************************************************************/
57
58 /*
59 * Method to create an empty file.
60 *
61 * Class: java_io_File
62 * Method: createInternal
63 * Signature: (Ljava/lang/String;)Z
64 */
65
66 JNIEXPORT jboolean JNICALL
67 Java_java_io_File_createInternal(JNIEnv *env, jclass clazz, jstring name)
68 {
69 const char *fname;
70 int fd;
71
72 fname = JCL_jstring_to_cstring(env, name);
73 if (!fname)
74 return(0);
75
76 fd = open(fname, O_CREAT|O_EXCL|O_RDWR, 0777);
77 if (fd == -1)
78 {
79 if (errno != EEXIST)
80 JCL_ThrowException(env, "java/io/IOException", strerror(errno));
81 return(0);
82 }
83
84 close(fd);
85 return(1);
86 }
87
88 /*************************************************************************/
89
90 /*
91 * This method checks to see if we have read permission on a file.
92 *
93 * Class: java_io_File
94 * Method: canReadInternal
95 * Signature: (Ljava/lang/String;)Z
96 */
97
98 JNIEXPORT jboolean JNICALL
99 Java_java_io_File_canReadInternal(JNIEnv *env, jobject obj, jstring name)
100 {
101 const char *fname;
102 int fd;
103
104 /* Don't use the JCL convert function because it throws an exception
105 on failure */
106 fname = (*env)->GetStringUTFChars(env, name, 0);
107 if (!fname)
108 return(0);
109
110 /* The lazy man's way out. We actually do open the file for reading
111 briefly to verify it can be done */
112 fd = open(fname, O_RDONLY);
113 (*env)->ReleaseStringUTFChars(env, name, fname);
114
115 if (fd == -1)
116 return(0);
117
118 close(fd);
119 return(1);
120 }
121
122 /*************************************************************************/
123
124 /*
125 * This method checks to see if we have write permission on a file.
126 *
127 * Class: java_io_File
128 * Method: canWriteInternal
129 * Signature: (Ljava/lang/String;)Z
130 */
131
132 JNIEXPORT jboolean JNICALL
133 Java_java_io_File_canWriteInternal(JNIEnv *env, jobject obj, jstring name)
134 {
135 const char *fname;
136 int fd;
137
138 /* Don't use the JCL convert function because it throws an exception
139 on failure */
140 fname = (*env)->GetStringUTFChars(env, name, 0);
141 if (!fname)
142 return(0);
143
144 /* The lazy man's way out. We actually do open the file for writing
145 briefly to verify it can be done */
146 fd = open(fname, O_RDWR);
147 (*env)->ReleaseStringUTFChars(env, name, fname);
148
149 if (fd == -1)
150 return(0);
151
152 close(fd);
153 return(1);
154 }
155
156 /*************************************************************************/
157
158 /*
159 * This method makes a file read only.
160 *
161 * Class: java_io_File
162 * Method: setReadOnlyInternal
163 * Signature: (Ljava/lang/String;)Z
164 */
165
166 JNIEXPORT jboolean JNICALL
167 Java_java_io_File_setReadOnlyInternal(JNIEnv *env, jobject obj, jstring name)
168 {
169 const char *fname;
170 struct stat buf;
171 mode_t newmode;
172 int rc;
173
174 /* Don't use the JCL convert function because it throws an exception
175 on failure */
176 fname = (*env)->GetStringUTFChars(env, name, 0);
177 if (!fname)
178 return(0);
179
180 rc = stat(fname, &buf);
181
182 if (rc == -1)
183 {
184 (*env)->ReleaseStringUTFChars(env, name, fname);
185 return(0);
186 }
187
188 newmode = buf.st_mode;
189 newmode = newmode & (~(S_IWRITE|S_IWGRP|S_IWOTH));
190
191 rc = chmod(fname, newmode);
192 (*env)->ReleaseStringUTFChars(env, name, fname);
193
194 if (rc == -1)
195 return(0);
196 else
197 return(1);
198 }
199
200 /*************************************************************************/
201
202 /*
203 * This method checks to see if a file exists.
204 *
205 * Class: java_io_File
206 * Method: existsInternal
207 * Signature: (Ljava/lang/String;)Z
208 */
209
210 JNIEXPORT jboolean JNICALL
211 Java_java_io_File_existsInternal(JNIEnv *env, jobject obj, jstring name)
212 {
213 const char *fname;
214 struct stat buf;
215 int rc;
216
217 /* Don't use the JCL convert function because it throws an exception
218 on failure */
219 fname = (*env)->GetStringUTFChars(env, name, 0);
220 if (!fname)
221 return(0);
222
223 rc = stat(fname, &buf);
224 (*env)->ReleaseStringUTFChars(env, name, fname);
225
226 if (rc == -1)
227 return(0);
228 else
229 return(1);
230 }
231
232 /*************************************************************************/
233
234 /*
235 * This method checks to see if a file is a "plain" file; that is, not
236 * a directory, pipe, etc.
237 *
238 * Class: java_io_File
239 * Method: isFileInternal
240 * Signature: (Ljava/lang/String;)Z
241 */
242
243 JNIEXPORT jboolean JNICALL
244 Java_java_io_File_isFileInternal(JNIEnv *env, jobject obj, jstring name)
245 {
246 const char *fname;
247 struct stat buf;
248 int rc;
249
250 /* Don't use the JCL convert function because it throws an exception
251 on failure */
252 fname = (*env)->GetStringUTFChars(env, name, 0);
253 if (!fname)
254 return(0);
255
256 rc = lstat(fname, &buf);
257 (*env)->ReleaseStringUTFChars(env, name, fname);
258
259 if (rc == -1)
260 return(0);
261 if (S_ISREG(buf.st_mode))
262 return(1);
263 else
264 return(0);
265 }
266
267 /*************************************************************************/
268
269 /*
270 * This method checks to see if a file is a directory or not.
271 *
272 * Class: java_io_File
273 * Method: isDirectoryInternal
274 * Signature: (Ljava/lang/String;)Z
275 */
276
277 JNIEXPORT jboolean JNICALL
278 Java_java_io_File_isDirectoryInternal(JNIEnv *env, jobject obj, jstring name)
279 {
280 const char *fname;
281 struct stat buf;
282 int rc;
283
284 /* Don't use the JCL convert function because it throws an exception
285 on failure */
286 fname = (*env)->GetStringUTFChars(env, name, 0);
287 if (!fname)
288 return(0);
289
290 rc = lstat(fname, &buf);
291 (*env)->ReleaseStringUTFChars(env, name, fname);
292
293 if (rc == -1)
294 return(0);
295 if (S_ISDIR(buf.st_mode))
296 return(1);
297 else
298 return(0);
299 }
300
301 /*************************************************************************/
302
303 /*
304 * This method returns the length of the file.
305 *
306 * Class: java_io_File
307 * Method: lengthInternal
308 * Signature: (Ljava/lang/String;)J
309 */
310
311 JNIEXPORT jlong JNICALL
312 Java_java_io_File_lengthInternal(JNIEnv *env, jobject obj, jstring name)
313 {
314 const char *fname;
315 struct stat buf;
316 int rc;
317
318 /* Don't use the JCL convert function because it throws an exception
319 on failure */
320 fname = (*env)->GetStringUTFChars(env, name, 0);
321 if (!fname)
322 return(0);
323
324 rc = stat(fname, &buf);
325 (*env)->ReleaseStringUTFChars(env, name, fname);
326
327 if (rc == -1)
328 return(0);
329
330 return(buf.st_size);
331 }
332
333 /*************************************************************************/
334
335 /*
336 * This method returns the modification date of the file.
337 *
338 * Class: java_io_File
339 * Method: lastModifiedInternal
340 * Signature: (Ljava/lang/String;)J
341 */
342
343 JNIEXPORT jlong JNICALL
344 Java_java_io_File_lastModifiedInternal(JNIEnv *env, jobject obj, jstring name)
345 {
346 const char *fname;
347 struct stat buf;
348 int rc;
349 jlong mtime;
350
351 /* Don't use the JCL convert function because it throws an exception
352 on failure */
353 fname = (*env)->GetStringUTFChars(env, name, 0);
354 if (!fname)
355 return(0);
356
357 rc = stat(fname, &buf);
358 (*env)->ReleaseStringUTFChars(env, name, fname);
359
360 if (rc == -1)
361 return(0);
362
363 // milliseconds required
364 mtime = buf.st_mtime;
365 mtime = mtime * 1000;
366 return(mtime);
367 }
368
369 /*************************************************************************/
370
371 /*
372 * This method sets the modification date of the file.
373 *
374 * Class: java_io_File
375 * Method: setLastModifiedInternal
376 * Signature: (Ljava/lang/String;J)Z
377 */
378
379 JNIEXPORT jboolean JNICALL
380 Java_java_io_File_setLastModifiedInternal(JNIEnv *env, jobject obj,
381 jstring name, jlong newtime)
382 {
383 const char *fname;
384 struct stat buf;
385 struct utimbuf ut;
386 int rc;
387
388 /* Don't use the JCL convert function because it throws an exception
389 on failure */
390 fname = (*env)->GetStringUTFChars(env, name, 0);
391 if (!fname)
392 return(0);
393
394 rc = stat(fname, &buf);
395
396 if (rc == -1)
397 {
398 (*env)->ReleaseStringUTFChars(env, name, fname);
399 return(0);
400 }
401
402 ut.actime = buf.st_atime;
403 ut.modtime = buf.st_mtime;
404
405 rc = utime(fname, &ut);
406 (*env)->ReleaseStringUTFChars(env, name, fname);
407
408 if (rc == -1)
409 return(0);
410 else
411 return(1);
412 }
413
414 /*************************************************************************/
415
416 /*
417 * This method deletes a file (actually a name for a file - additional
418 * links could exist).
419 *
420 * Class: java_io_File
421 * Method: deleteInternal
422 * Signature: (Ljava/lang/String;)Z
423 */
424
425 JNIEXPORT jboolean JNICALL
426 Java_java_io_File_deleteInternal(JNIEnv *env, jobject obj, jstring name)
427 {
428 const char *fname;
429 int rc;
430
431 /* Don't use the JCL convert function because it throws an exception
432 on failure */
433 fname = (*env)->GetStringUTFChars(env, name, 0);
434 if (!fname)
435 return(0);
436
437 rc = unlink(fname);
438 if (rc == -1)
439 rc = rmdir(fname);
440 (*env)->ReleaseStringUTFChars(env, name, fname);
441
442 if (rc == -1)
443 return(0);
444 else
445 return(1);
446 }
447
448 /*************************************************************************/
449
450 /*
451 * This method creates a directory.
452 *
453 * Class: java_io_File
454 * Method: mkdirInternal
455 * Signature: (Ljava/lang/String;)Z
456 */
457
458 JNIEXPORT jboolean JNICALL
459 Java_java_io_File_mkdirInternal(JNIEnv *env, jobject obj, jstring name)
460 {
461 const char *fname;
462 int rc;
463
464 /* Don't use the JCL convert function because it throws an exception
465 on failure */
466 fname = (*env)->GetStringUTFChars(env, name, 0);
467 if (!fname)
468 return(0);
469
470 rc = mkdir(fname, 0777);
471 (*env)->ReleaseStringUTFChars(env, name, fname);
472
473 if (rc == -1)
474 return(0);
475 else
476 return(1);
477 }
478
479 /*************************************************************************/
480
481 /*
482 * This method renames a (link to a) file.
483 *
484 * Class: java_io_File
485 * Method: renameToInternal
486 * Signature: (Ljava/lang/String;Ljava/lang/String;)Z
487 */
488
489 JNIEXPORT jboolean JNICALL
490 Java_java_io_File_renameToInternal(JNIEnv *env, jobject obj, jstring t, jstring d)
491 {
492 const char *target, *destination;
493 int rc;
494
495 /* Don't use the JCL convert function because it throws an exception
496 on failure */
497 target = (*env)->GetStringUTFChars(env, t, 0);
498 if (!target)
499 return(0);
500
501 destination = (*env)->GetStringUTFChars(env, d, 0);
502 if (!destination)
503 {
504 (*env)->ReleaseStringUTFChars(env, t, target);
505 return(0);
506 }
507
508 rc = rename(target, destination);
509 (*env)->ReleaseStringUTFChars(env, t, target);
510 (*env)->ReleaseStringUTFChars(env, d, destination);
511
512 if (rc == -1)
513 return(0);
514 else
515 return(1);
516 }
517
518 /*************************************************************************/
519
520 /*
521 * This method returns an array of String representing all the files
522 * in a directory except "." and "..".
523 *
524 * Class: java_io_File
525 * Method: listInternal
526 * Signature: (Ljava/lang/String;)[Ljava/lang/String;
527 */
528
529 JNIEXPORT jobjectArray JNICALL
530 Java_java_io_File_listInternal(JNIEnv *env, jobject obj, jstring name)
531 {
532 static jclass str_clazz = 0;
533 int realloc_size = 10;
534 const char *dirname;
535 char **filelist;
536 jobjectArray retarray;
537 DIR *dir;
538 struct dirent *dirent;
539 int i, j;
540
541 /* Don't use the JCL convert function because it throws an exception
542 on failure */
543 dirname = (*env)->GetStringUTFChars(env, name, 0);
544 if (!dirname)
545 return(0);
546
547 /* Read the files from the directory */
548 filelist = (char **)JCL_malloc(env, sizeof(char *) * realloc_size);
549 //filelist = (char **)malloc(sizeof(char *) * realloc_size);
550 dir = opendir(dirname);
551 (*env)->ReleaseStringUTFChars(env, name, dirname);
552 if (!filelist || !dir)
553 return(0);
554
555 for (i = 0;;)
556 {
557 dirent = readdir(dir);
558 if (!dirent)
559 break;
560
561 if (!strcmp(dirent->d_name, ".") || !strcmp(dirent->d_name, ".."))
562 continue;
563
564 /* Allocate more memory if necessary */
565 if ((((i + 1) % realloc_size) == 0) && (i != 0))
566 {
567 char **newlist;
568
569 newlist = JCL_realloc(env, filelist, ((i + 1) + realloc_size) *
570 sizeof(char *));
571 //newlist = realloc(filelist, (i + 1) + realloc_size);
572 if (!filelist)
573 {
574 free(filelist);
575 return(0);
576 }
577 filelist = newlist;
578 }
579
580 filelist[i] = strdup(dirent->d_name);
581 ++i;
582 }
583 closedir(dir);
584
585 /* Now put the list of files into a Java String array and return it */
586 str_clazz = (*env)->FindClass(env, "java/lang/String");
587 if (!str_clazz)
588 {
589 free(filelist);
590 return(0);
591 }
592
593 retarray = (*env)->NewObjectArray(env, i, str_clazz, 0);
594 if (!retarray)
595 {
596 free(filelist);
597 return(0);
598 }
599
600 for (j = 0; j < i; j++)
601 {
602 jstring str;
603
604 str = (*env)->NewStringUTF(env, filelist[j]);
605 if (!str)
606 {
607 /* We don't clean up everything here, but if this failed,
608 something serious happened anyway */
609 free(filelist);
610 return(0);
611 }
612
613 (*env)->SetObjectArrayElement(env, retarray, j, str);
614 }
615
616 return(retarray);
617 }
618

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26