1 /* java_io_VMFile.c - Native methods for java.io.File class
2 Copyright (C) 1998, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA
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
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. */
38 /* do not move; needed here because of some macro definitions */
47 #include "target_native.h"
48 #ifndef WITHOUT_FILESYSTEM
49 #include "target_native_file.h"
51 #include "target_native_math_int.h"
53 #include "java_io_VMFile.h"
55 /*************************************************************************/
58 * Method to create an empty file.
60 * Class: java_io_VMFile
62 * Signature: (Ljava/lang/String;)Z
65 JNIEXPORT jboolean JNICALL
66 Java_java_io_VMFile_create (JNIEnv
* env
,
67 jclass clazz
__attribute__ ((__unused__
)),
70 #ifndef WITHOUT_FILESYSTEM
75 filename
= JCL_jstring_to_cstring (env
, name
);
81 TARGET_NATIVE_FILE_OPEN_CREATE (filename
, fd
, result
);
82 if (result
!= TARGET_NATIVE_OK
)
86 JCL_ThrowException (env
,
87 "java/io/IOException",
88 TARGET_NATIVE_LAST_ERROR_STRING ());
89 JCL_free_cstring (env
, name
, filename
);
92 TARGET_NATIVE_FILE_CLOSE (fd
, result
);
94 JCL_free_cstring (env
, name
, filename
);
96 #else /* not WITHOUT_FILESYSTEM */
98 #endif /* not WITHOUT_FILESYSTEM */
101 /*************************************************************************/
104 * This method checks to see if we have read permission on a file.
106 * Class: java_io_VMFile
108 * Signature: (Ljava/lang/String;)Z
111 JNIEXPORT jboolean JNICALL
112 Java_java_io_VMFile_canRead (JNIEnv
* env
,
113 jobject obj
__attribute__ ((__unused__
)),
116 #ifndef WITHOUT_FILESYSTEM
117 const char *filename
;
121 /* Don't use the JCL convert function because it throws an exception
123 filename
= (*env
)->GetStringUTFChars (env
, name
, 0);
124 if (filename
== NULL
)
129 /* The lazy man's way out. We actually do open the file for reading
130 briefly to verify it can be done */
131 TARGET_NATIVE_FILE_OPEN_READ (filename
, fd
, result
);
132 (*env
)->ReleaseStringUTFChars (env
, name
, filename
);
133 if (result
!= TARGET_NATIVE_OK
)
137 TARGET_NATIVE_FILE_CLOSE (fd
, result
);
140 #else /* not WITHOUT_FILESYSTEM */
142 #endif /* not WITHOUT_FILESYSTEM */
145 /*************************************************************************/
148 * This method checks to see if we have write permission on a file.
150 * Class: java_io_VMFile
152 * Signature: (Ljava/lang/String;)Z
155 JNIEXPORT jboolean JNICALL
156 Java_java_io_VMFile_canWrite (JNIEnv
* env
,
157 jobject obj
__attribute__ ((__unused__
)),
160 #ifndef WITHOUT_FILESYSTEM
161 const char *filename
;
165 /* Don't use the JCL convert function because it throws an exception
167 filename
= (*env
)->GetStringUTFChars (env
, name
, 0);
168 if (filename
== NULL
)
173 /* The lazy man's way out. We actually do open the file for writing
174 briefly to verify it can be done */
175 TARGET_NATIVE_FILE_OPEN_READWRITE (filename
, fd
, result
);
176 (*env
)->ReleaseStringUTFChars (env
, name
, filename
);
177 if (result
!= TARGET_NATIVE_OK
)
181 TARGET_NATIVE_FILE_CLOSE (fd
, result
);
184 #else /* not WITHOUT_FILESYSTEM */
186 #endif /* not WITHOUT_FILESYSTEM */
189 /*************************************************************************/
192 * This method makes a file read only.
194 * Class: java_io_VMFile
195 * Method: setReadOnly
196 * Signature: (Ljava/lang/String;)Z
199 JNIEXPORT jboolean JNICALL
200 Java_java_io_VMFile_setReadOnly (JNIEnv
* env
,
201 jobject obj
__attribute__ ((__unused__
)),
204 #ifndef WITHOUT_FILESYSTEM
205 const char *filename
;
208 /* Don't use the JCL convert function because it throws an exception
210 filename
= (*env
)->GetStringUTFChars (env
, name
, 0);
211 if (filename
== NULL
)
216 TARGET_NATIVE_FILE_SET_MODE_READONLY (filename
, result
);
217 (*env
)->ReleaseStringUTFChars (env
, name
, filename
);
219 return ((result
== TARGET_NATIVE_OK
) ? 1 : 0);
220 #else /* not WITHOUT_FILESYSTEM */
222 #endif /* not WITHOUT_FILESYSTEM */
225 /*************************************************************************/
228 * This method checks to see if a file exists.
230 * Class: java_io_VMFile
232 * Signature: (Ljava/lang/String;)Z
235 JNIEXPORT jboolean JNICALL
236 Java_java_io_VMFile_exists (JNIEnv
* env
,
237 jobject obj
__attribute__ ((__unused__
)),
240 #ifndef WITHOUT_FILESYSTEM
241 const char *filename
;
244 /* Don't use the JCL convert function because it throws an exception
246 filename
= (*env
)->GetStringUTFChars (env
, name
, 0);
247 if (filename
== NULL
)
252 TARGET_NATIVE_FILE_EXISTS (filename
, result
);
253 (*env
)->ReleaseStringUTFChars (env
, name
, filename
);
255 return ((result
== TARGET_NATIVE_OK
) ? 1 : 0);
256 #else /* not WITHOUT_FILESYSTEM */
258 #endif /* not WITHOUT_FILESYSTEM */
261 /*************************************************************************/
264 * This method checks to see if a file is a "plain" file; that is, not
265 * a directory, pipe, etc.
267 * Class: java_io_VMFile
269 * Signature: (Ljava/lang/String;)Z
272 JNIEXPORT jboolean JNICALL
273 Java_java_io_VMFile_isFile (JNIEnv
* env
,
274 jobject obj
__attribute__ ((__unused__
)),
277 #ifndef WITHOUT_FILESYSTEM
278 const char *filename
;
281 /* Don't use the JCL convert function because it throws an exception
283 filename
= (*env
)->GetStringUTFChars (env
, name
, 0);
284 if (filename
== NULL
)
289 TARGET_NATIVE_FILE_IS_FILE (filename
, result
);
290 (*env
)->ReleaseStringUTFChars (env
, name
, filename
);
292 return ((result
== TARGET_NATIVE_OK
) ? 1 : 0);
293 #else /* not WITHOUT_FILESYSTEM */
295 #endif /* not WITHOUT_FILESYSTEM */
298 /*************************************************************************/
301 * This method checks to see if a file is a directory or not.
303 * Class: java_io_VMFile
304 * Method: isDirectory
305 * Signature: (Ljava/lang/String;)Z
308 JNIEXPORT jboolean JNICALL
309 Java_java_io_VMFile_isDirectory (JNIEnv
* env
,
310 jobject obj
__attribute__ ((__unused__
)),
313 #ifndef WITHOUT_FILESYSTEM
314 const char *filename
;
317 /* Don't use the JCL convert function because it throws an exception
319 filename
= (*env
)->GetStringUTFChars (env
, name
, 0);
320 if (filename
== NULL
)
325 TARGET_NATIVE_FILE_IS_DIRECTORY (filename
, result
);
326 (*env
)->ReleaseStringUTFChars (env
, name
, filename
);
328 return ((result
== TARGET_NATIVE_OK
) ? 1 : 0);
329 #else /* not WITHOUT_FILESYSTEM */
331 #endif /* not WITHOUT_FILESYSTEM */
334 /*************************************************************************/
337 * This method returns the length of the file.
339 * Class: java_io_VMFile
341 * Signature: (Ljava/lang/String;)J
344 JNIEXPORT jlong JNICALL
345 Java_java_io_VMFile_length (JNIEnv
* env
,
346 jobject obj
__attribute__ ((__unused__
)),
349 #ifndef WITHOUT_FILESYSTEM
350 const char *filename
;
355 /* Don't use the JCL convert function because it throws an exception
357 filename
= (*env
)->GetStringUTFChars (env
, name
, 0);
358 if (filename
== NULL
)
360 return (TARGET_NATIVE_MATH_INT_INT64_CONST_0
);
363 /* open file for reading, get size and close file */
364 TARGET_NATIVE_FILE_OPEN_READ (filename
, tmpfd
, result
);
365 if (result
!= TARGET_NATIVE_OK
)
367 return (TARGET_NATIVE_MATH_INT_INT64_CONST_0
);
369 TARGET_NATIVE_FILE_SIZE (tmpfd
, length
, result
);
370 if (result
!= TARGET_NATIVE_OK
)
372 TARGET_NATIVE_FILE_CLOSE (tmpfd
, result
);
373 return (TARGET_NATIVE_MATH_INT_INT64_CONST_0
);
375 TARGET_NATIVE_FILE_CLOSE (tmpfd
, result
);
376 (*env
)->ReleaseStringUTFChars (env
, name
, filename
);
379 TARGET_NATIVE_OK
) ? length
: TARGET_NATIVE_MATH_INT_INT64_CONST_0
);
380 #else /* not WITHOUT_FILESYSTEM */
381 return (TARGET_NATIVE_MATH_INT_INT64_CONST_0
);
382 #endif /* not WITHOUT_FILESYSTEM */
385 /*************************************************************************/
388 * This method returns the modification date of the file.
390 * Class: java_io_VMFile
391 * Method: lastModified
392 * Signature: (Ljava/lang/String;)J
395 JNIEXPORT jlong JNICALL
396 Java_java_io_VMFile_lastModified (JNIEnv
* env
,
397 jobject obj
__attribute__ ((__unused__
)),
400 #ifndef WITHOUT_FILESYSTEM
401 const char *filename
;
405 /* Don't use the JCL convert function because it throws an exception
407 filename
= (*env
)->GetStringUTFChars (env
, name
, 0);
408 if (filename
== NULL
)
410 return (TARGET_NATIVE_MATH_INT_INT64_CONST_0
);
413 TARGET_NATIVE_FILE_GET_LAST_MODIFIED (filename
, mtime
, result
);
414 (*env
)->ReleaseStringUTFChars (env
, name
, filename
);
417 TARGET_NATIVE_OK
) ? mtime
: TARGET_NATIVE_MATH_INT_INT64_CONST_0
);
418 #else /* not WITHOUT_FILESYSTEM */
419 return (TARGET_NATIVE_MATH_INT_INT64_CONST_0
);
420 #endif /* not WITHOUT_FILESYSTEM */
423 /*************************************************************************/
426 * This method sets the modification date of the file.
428 * Class: java_io_VMFile
429 * Method: setLastModified
430 * Signature: (Ljava/lang/String;J)Z
433 JNIEXPORT jboolean JNICALL
434 Java_java_io_VMFile_setLastModified (JNIEnv
* env
,
435 jobject obj
__attribute__ ((__unused__
)),
436 jstring name
, jlong newtime
)
438 #ifndef WITHOUT_FILESYSTEM
439 const char *filename
;
442 /* Don't use the JCL convert function because it throws an exception
444 filename
= (*env
)->GetStringUTFChars (env
, name
, 0);
445 if (filename
== NULL
)
450 TARGET_NATIVE_FILE_SET_LAST_MODIFIED (filename
, newtime
, result
);
451 (*env
)->ReleaseStringUTFChars (env
, name
, filename
);
453 return ((result
== TARGET_NATIVE_OK
) ? 1 : 0);
454 #else /* not WITHOUT_FILESYSTEM */
456 #endif /* not WITHOUT_FILESYSTEM */
459 /*************************************************************************/
462 * This method deletes a file (actually a name for a file - additional
463 * links could exist).
465 * Class: java_io_VMFile
467 * Signature: (Ljava/lang/String;)Z
470 JNIEXPORT jboolean JNICALL
471 Java_java_io_VMFile_delete (JNIEnv
* env
,
472 jobject obj
__attribute__ ((__unused__
)),
475 #ifndef WITHOUT_FILESYSTEM
476 const char *filename
;
479 /* Don't use the JCL convert function because it throws an exception
481 filename
= (*env
)->GetStringUTFChars (env
, name
, 0);
482 if (filename
== NULL
)
487 TARGET_NATIVE_FILE_DELETE (filename
, result
);
488 (*env
)->ReleaseStringUTFChars (env
, name
, filename
);
490 return ((result
== TARGET_NATIVE_OK
) ? 1 : 0);
491 #else /* not WITHOUT_FILESYSTEM */
493 #endif /* not WITHOUT_FILESYSTEM */
496 /*************************************************************************/
499 * This method creates a directory.
501 * Class: java_io_VMFile
503 * Signature: (Ljava/lang/String;)Z
506 JNIEXPORT jboolean JNICALL
507 Java_java_io_VMFile_mkdir (JNIEnv
* env
,
508 jobject obj
__attribute__ ((__unused__
)),
511 #ifndef WITHOUT_FILESYSTEM
512 const char *pathname
;
515 /* Don't use the JCL convert function because it throws an exception
517 pathname
= (*env
)->GetStringUTFChars (env
, name
, 0);
518 if (pathname
== NULL
)
523 TARGET_NATIVE_FILE_MAKE_DIR (pathname
, result
);
524 (*env
)->ReleaseStringUTFChars (env
, name
, pathname
);
526 return ((result
== TARGET_NATIVE_OK
) ? 1 : 0);
527 #else /* not WITHOUT_FILESYSTEM */
529 #endif /* not WITHOUT_FILESYSTEM */
532 /*************************************************************************/
535 * This method renames a (link to a) file.
537 * Class: java_io_VMFile
539 * Signature: (Ljava/lang/String;Ljava/lang/String;)Z
542 JNIEXPORT jboolean JNICALL
543 Java_java_io_VMFile_renameTo (JNIEnv
* env
,
544 jobject obj
__attribute__ ((__unused__
)),
545 jstring t
, jstring d
)
547 #ifndef WITHOUT_FILESYSTEM
548 const char *old_filename
, *new_filename
;
551 /* Don't use the JCL convert function because it throws an exception
553 old_filename
= (*env
)->GetStringUTFChars (env
, t
, 0);
554 if (old_filename
== NULL
)
559 new_filename
= (*env
)->GetStringUTFChars (env
, d
, 0);
560 if (new_filename
== NULL
)
562 (*env
)->ReleaseStringUTFChars (env
, t
, old_filename
);
566 TARGET_NATIVE_FILE_RENAME (old_filename
, new_filename
, result
);
567 (*env
)->ReleaseStringUTFChars (env
, d
, new_filename
);
568 (*env
)->ReleaseStringUTFChars (env
, t
, old_filename
);
570 return ((result
== TARGET_NATIVE_OK
) ? 1 : 0);
571 #else /* not WITHOUT_FILESYSTEM */
573 #endif /* not WITHOUT_FILESYSTEM */
576 /*************************************************************************/
579 * This method returns an array of String representing all the files
580 * in a directory except "." and "..".
582 * Class: java_io_VMFile
584 * Signature: (Ljava/lang/String;)[Ljava/lang/String;
587 JNIEXPORT jobjectArray JNICALL
588 Java_java_io_VMFile_list (JNIEnv
* env
, jobject obj
589 __attribute__ ((__unused__
)), jstring name
)
591 #ifndef WITHOUT_FILESYSTEM
592 const int REALLOC_SIZE
= 10;
598 const char *filename
;
599 unsigned long int filelist_count
, max_filelist_count
;
602 jobjectArray filearray
;
606 /* Don't use the JCL convert function because it throws an exception
608 dirname
= (*env
)->GetStringUTFChars (env
, name
, 0);
614 /* open directory for reading */
615 TARGET_NATIVE_FILE_OPEN_DIR (dirname
, handle
, result
);
617 (*env
)->ReleaseStringUTFChars (env
, name
, dirname
);
619 if (result
!= TARGET_NATIVE_OK
)
624 /* allocate filelist */
625 filelist
= (char **) JCL_malloc (env
, sizeof (char *) * REALLOC_SIZE
);
626 if (filelist
== NULL
)
628 TARGET_NATIVE_FILE_CLOSE_DIR (handle
, result
);
632 max_filelist_count
= REALLOC_SIZE
;
634 /* read the files from the directory */
635 TARGET_NATIVE_FILE_READ_DIR (handle
, filename
, result
);
636 while (result
== TARGET_NATIVE_OK
)
638 if ((strcmp (filename
, ".") != 0) && (strcmp (filename
, "..") != 0))
640 /* allocate more memory if necessary */
641 if (filelist_count
>= max_filelist_count
)
643 tmp_filelist
= (char **) JCL_realloc (env
,
645 (max_filelist_count
+
648 if (tmp_filelist
== NULL
)
650 for (i
= 0; i
< filelist_count
; i
++)
652 JCL_free (env
, filelist
[i
]);
654 JCL_free (env
, filelist
);
655 TARGET_NATIVE_FILE_CLOSE_DIR (handle
, result
);
658 filelist
= tmp_filelist
;
659 max_filelist_count
+= REALLOC_SIZE
;
662 /* save entry in list (avoid strdup, because it is not ANSI C, thus difficult to port) */
663 filelist
[filelist_count
] =
664 (char *) JCL_malloc (env
, strlen (filename
) + 1);
665 assert (filelist
[filelist_count
] != NULL
);
666 strcpy (filelist
[filelist_count
], filename
);
670 /* read next directory entry */
671 TARGET_NATIVE_FILE_READ_DIR (handle
, filename
, result
);
674 /* close directory */
675 TARGET_NATIVE_FILE_CLOSE_DIR (handle
, result
);
677 /* put the list of files into a Java String array and return it */
678 str_clazz
= (*env
)->FindClass (env
, "java/lang/String");
679 if (str_clazz
== NULL
)
681 for (i
= 0; i
< filelist_count
; i
++)
683 JCL_free (env
, filelist
[i
]);
685 JCL_free (env
, filelist
);
688 filearray
= (*env
)->NewObjectArray (env
, filelist_count
, str_clazz
, 0);
689 if (filearray
== NULL
)
691 for (i
= 0; i
< filelist_count
; i
++)
693 JCL_free (env
, filelist
[i
]);
695 JCL_free (env
, filelist
);
698 for (i
= 0; i
< filelist_count
; i
++)
700 /* create new string */
701 str
= (*env
)->NewStringUTF (env
, filelist
[i
]);
704 /* We don't clean up everything here, but if this failed,
705 something serious happened anyway */
706 for (i
= 0; i
< filelist_count
; i
++)
708 JCL_free (env
, filelist
[i
]);
710 JCL_free (env
, filelist
);
714 /* save into array */
715 (*env
)->SetObjectArrayElement (env
, filearray
, i
, str
);
717 /* delete local reference */
718 (*env
)->DeleteLocalRef (env
, str
);
722 for (i
= 0; i
< filelist_count
; i
++)
724 JCL_free (env
, filelist
[i
]);
726 JCL_free (env
, filelist
);
729 #else /* not WITHOUT_FILESYSTEM */
731 #endif /* not WITHOUT_FILESYSTEM */