Commit f4047540 by Bryce McKinlay Committed by Bryce McKinlay

1.3-Compliant Implementation of java.io.File.

	* java/lang/natSystem.cc (init_properties): Get "file.separator",
	"path.separator", and "java.io.tmpdir" from the File class, instead
	of setting them explicitly.
	* java/io/File.java: Do not canonicalize paths for security manager
	checks. Call init_native() from static initializer. Do not pass path
	argument to native methods. New native method declarations. Some
	security manager checks moved to checkWrite().
	(equals): Check file system case sensitivity and act appropriatly.
	(hashCode): Likewise.
	(isHidden): New method implemented.
	(performList): Changed prototype. Now takes a class argument specifying
	the class of the returned array: Strings or File objects. Also added
	FileFilter argument.
	(listFiles): New variants with "File" return type implemented.
	(createTempFile): Use createNewFile(). Use maxPathLen.
	(setReadOnly): New method implemented.
	(listRoots): Likewise.
	(compareTo): Likewise.
	(setLastModified): Likewise.
	(checkWrite): New method.
	(setPath): Removed.
	* java/io/natFile.cc: Various functions no longer take canonical path
	argument.
	(stat): Handle ISHIDDEN query.
	(isAbsolute): Remove WIN32 cruft.
	(performList): New arguments. Handle returning either File[] or
	String[] arrays. Check with FileFilter or FilenameFilter arguments as
	appropriate. Use an ArrayList, not a Vector, for the temporary list.
	(performSetReadOnly): New method implemented.
	(performListRoots): Likewise.
	(performSetLastModified): Likewise.
	(performCreate): Likewise.
	(init_native): New initialization function.
	* java/io/natFileWin32.cc: Various functions no longer take canonical
	path argument.
	(stat): Add FIXME about ISHIDDEN query.
	(performList): New arguments. Handle returning either File[] or String[]
	arrays. Check with FileFilter or FilenameFilter arguments as
	appropriate. Use an ArrayList, not a Vector, for the temporary list.
	(performSetReadOnly): New. Stubbed.
	(performListRoots): Likewise.
	(performSetLastModified): Likewise.
	(performCreate): Likewise.
	(init_native) New initialization function.
	* configure.in: Check for utime() and chmod().
	* configure: Rebuilt.
	* include/config.h.in: Rebuilt.

	Resolves PR libgcj/1759.

From-SVN: r40985
parent e74061a9
2001-04-01 Bryce McKinlay <bryce@albatross.co.nz>
1.3-Compliant Implementation of java.io.File.
* java/lang/natSystem.cc (init_properties): Get "file.separator",
"path.separator", and "java.io.tmpdir" from the File class, instead
of setting them explicitly.
* java/io/File.java: Do not canonicalize paths for security manager
checks. Call init_native() from static initializer. Do not pass path
argument to native methods. New native method declarations. Some
security manager checks moved to checkWrite().
(equals): Check file system case sensitivity and act appropriatly.
(hashCode): Likewise.
(isHidden): New method implemented.
(performList): Changed prototype. Now takes a class argument specifying
the class of the returned array: Strings or File objects. Also added
FileFilter argument.
(listFiles): New variants with "File" return type implemented.
(createTempFile): Use createNewFile(). Use maxPathLen.
(setReadOnly): New method implemented.
(listRoots): Likewise.
(compareTo): Likewise.
(setLastModified): Likewise.
(checkWrite): New method.
(setPath): Removed.
* java/io/natFile.cc: Various functions no longer take canonical path
argument.
(stat): Handle ISHIDDEN query.
(isAbsolute): Remove WIN32 cruft.
(performList): New arguments. Handle returning either File[] or String[]
arrays. Check with FileFilter or FilenameFilter arguments as
appropriate. Use an ArrayList, not a Vector, for the temporary list.
(performSetReadOnly): New method implemented.
(performListRoots): Likewise.
(performSetLastModified): Likewise.
(performCreate): Likewise.
(init_native): New initialization function.
* java/io/natFileWin32.cc: Various functions no longer take canonical
path argument.
(stat): Add FIXME about ISHIDDEN query.
(performList): New arguments. Handle returning either File[] or String[]
arrays. Check with FileFilter or FilenameFilter arguments as
appropriate. Use an ArrayList, not a Vector, for the temporary list.
(performSetReadOnly): New. Stubbed.
(performListRoots): Likewise.
(performSetLastModified): Likewise.
(performCreate): Likewise.
(init_native) New initialization function.
* configure.in: Check for utime() and chmod().
* configure: Rebuilt.
* include/config.h.in: Rebuilt.
Resolves PR libgcj/1759.
2001-03-28 Richard Henderson <rth@redhat.com>
IA-64 ABI Exception Handling:
......
......@@ -431,7 +431,7 @@ if test -n "${with_cross_host}"; then
else
AC_CHECK_FUNCS(strerror ioctl select fstat open fsync sleep)
AC_CHECK_FUNCS(gmtime_r localtime_r readdir_r getpwuid_r getcwd)
AC_CHECK_FUNCS(access stat mkdir rename rmdir unlink realpath)
AC_CHECK_FUNCS(access stat mkdir rename rmdir unlink realpath utime chmod)
AC_CHECK_FUNCS(iconv nl_langinfo setlocale)
AC_CHECK_FUNCS(inet_aton inet_addr, break)
AC_CHECK_FUNCS(inet_pton uname inet_ntoa)
......@@ -669,6 +669,7 @@ changequote(<<,>>)
gcjvers="`$GCJ -v 2>&1 | sed -n 's/^.*version \([^ ]*\).*$/\1/p'`"
changequote([,])
AC_DEFINE_UNQUOTED(GCJVERSION, "$gcjvers")
AC_SUBST(GCJVERSION)
AC_SUBST(AM_RUNTESTFLAGS)
......
......@@ -135,9 +135,6 @@
getenv("GCJ_PROPERTIES"). */
#undef DISABLE_GETENV_PROPERTIES
/* Define if using setjmp/longjmp exceptions. */
#undef SJLJ_EXCEPTIONS
/* Define if you have /proc/self/exe */
#undef HAVE_PROC_SELF_EXE
......@@ -167,6 +164,9 @@
/* Define if you have the backtrace function. */
#undef HAVE_BACKTRACE
/* Define if you have the chmod function. */
#undef HAVE_CHMOD
/* Define if you have the execvp function. */
#undef HAVE_EXECVP
......@@ -287,6 +287,9 @@
/* Define if you have the unlink function. */
#undef HAVE_UNLINK
/* Define if you have the utime function. */
#undef HAVE_UTIME
/* Define if you have the <arpa/inet.h> header file. */
#undef HAVE_ARPA_INET_H
......@@ -359,6 +362,9 @@
/* Version number of package */
#undef VERSION
/* Define if the compiler is configured for setjmp/longjmp exceptions. */
#undef SJLJ_EXCEPTIONS
/* Required define if using POSIX threads */
#undef _REENTRANT
......
// natFile.cc - Native part of File class.
// natFile.cc - Native part of File class for POSIX.
/* Copyright (C) 1998, 1999, 2000 Free Software Foundation
/* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation
This file is part of libgcj.
......@@ -24,23 +24,23 @@ details. */
#include <dirent.h>
#endif
#include <string.h>
#include <utime.h>
#include <gcj/cni.h>
#include <jvm.h>
#include <java/io/File.h>
#include <java/io/IOException.h>
#include <java/util/Vector.h>
#include <java/util/ArrayList.h>
#include <java/lang/String.h>
#include <java/io/FilenameFilter.h>
#include <java/io/FileFilter.h>
#include <java/lang/System.h>
jboolean
java::io::File::access (jstring canon, jint query)
java::io::File::access (jint query)
{
if (! canon)
return false;
char buf[MAXPATHLEN];
jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
// FIXME?
buf[total] = '\0';
JvAssert (query == READ || query == WRITE || query == EXISTS);
......@@ -59,15 +59,16 @@ java::io::File::access (jstring canon, jint query)
}
jboolean
java::io::File::stat (jstring canon, jint query)
java::io::File::stat (jint query)
{
if (! canon)
return false;
char buf[MAXPATHLEN];
jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
// FIXME?
buf[total] = '\0';
if (query == ISHIDDEN)
return (getName()->charAt(0) == '.');
#ifdef HAVE_STAT
struct stat sb;
if (::stat (buf, &sb))
......@@ -82,13 +83,10 @@ java::io::File::stat (jstring canon, jint query)
}
jlong
java::io::File::attr (jstring canon, jint query)
java::io::File::attr (jint query)
{
if (! canon)
return false;
char buf[MAXPATHLEN];
jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
// FIXME?
buf[total] = '\0';
......@@ -99,8 +97,6 @@ java::io::File::attr (jstring canon, jint query)
return 0;
JvAssert (query == MODIFIED || query == LENGTH);
// FIXME: time computation is very POSIX-specific -- POSIX and Java
// have the same Epoch.
return query == MODIFIED ? (jlong)sb.st_mtime * 1000 : sb.st_size;
#else
// There's no good choice here.
......@@ -131,23 +127,7 @@ java::io::File::getCanonicalPath (void)
jboolean
java::io::File::isAbsolute (void)
{
// FIXME: cpp define name.
// FIXME: cygwin.
#ifdef WIN32
if (path->charAt(0) == '/' || path->charAt(0) == '\\')
return true;
if (path->length() < 3)
return false;
// Hard-code A-Za-z because Windows (I think) can't use non-ASCII
// letters as drive names.
if ((path->charAt(0) < 'a' || path->charAt(0) > 'z')
&& (path->charAt(0) < 'A' || path->charAt(0) > 'Z'))
return false;
return (path->charAt(1) == ':'
&& (path->charAt(2) == '/' || path->charAt(2) == '\\'));
#else
return path->charAt(0) == '/';
#endif
}
#ifdef HAVE_DIRENT_H
......@@ -173,15 +153,14 @@ get_entry (DIR *dir, struct dirent *)
#endif /* defined(__JV_POSIX_THREADS__) && defined(HAVE_READDIR_R) */
#endif /* HAVE_DIRENT_H */
jstringArray
java::io::File::performList (jstring canon, FilenameFilter *filter)
jobjectArray
java::io::File::performList (java::io::FilenameFilter *filter,
java::io::FileFilter *fileFilter,
java::lang::Class *result_type)
{
if (! canon)
return NULL;
#ifdef HAVE_DIRENT_H
char buf[MAXPATHLEN];
jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
// FIXME?
buf[total] = '\0';
......@@ -189,26 +168,37 @@ java::io::File::performList (jstring canon, FilenameFilter *filter)
if (! dir)
return NULL;
java::util::Vector *vec = new java::util::Vector ();
java::util::ArrayList *list = new java::util::ArrayList ();
struct dirent *d, d2;
while ((d = get_entry (dir, &d2)) != NULL)
{
if (! strcmp (d->d_name, ".") || ! strcmp (d->d_name, ".."))
// Omit "." and "..".
if (d->d_name[0] == '.'
&& (d->d_name[1] == '\0'
|| (d->d_name[1] == '.' && d->d_name[2] == '\0')))
continue;
jstring name = JvNewStringUTF (d->d_name);
if (filter && ! filter->accept(this, name))
continue;
vec->addElement(name);
if (result_type == &java::io::File::class$)
{
java::io::File *file = new java::io::File (this, name);
if (fileFilter && ! fileFilter->accept(file))
continue;
list->add(file);
}
else
list->add(name);
}
closedir (dir);
jobjectArray ret = JvNewObjectArray (vec->size(), canon->getClass(),
NULL);
vec->copyInto(ret);
return reinterpret_cast<jstringArray> (ret);
jobjectArray ret = JvNewObjectArray (list->size(), result_type, NULL);
list->toArray(ret);
return ret;
#else /* HAVE_DIRENT_H */
return NULL;
#endif /* HAVE_DIRENT_H */
......@@ -230,6 +220,42 @@ java::io::File::performMkdir (void)
}
jboolean
java::io::File::performSetReadOnly (void)
{
char buf[MAXPATHLEN];
jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
// FIXME?
buf[total] = '\0';
#ifdef HAVE_STAT && HAVE_CHMOD
struct stat sb;
if (::stat (buf, &sb))
return false;
if (::chmod(buf, sb.st_mode & 0555))
return false;
return true;
#else
return false;
#endif
}
static JArray<java::io::File *> *unixroot;
JArray< ::java::io::File *>*
java::io::File::performListRoots ()
{
if (unixroot == NULL)
{
::java::io::File *f = new ::java::io::File (JvNewStringLatin1 ("/"));
unixroot = reinterpret_cast <JArray<java::io::File *>*>
(JvNewObjectArray (1, &java::io::File::class$, f));
elements (unixroot) [0] = f;
}
return unixroot;
}
jboolean
java::io::File::performRenameTo (File *dest)
{
char buf[MAXPATHLEN];
......@@ -249,10 +275,52 @@ java::io::File::performRenameTo (File *dest)
}
jboolean
java::io::File::performDelete (jstring canon)
java::io::File::performSetLastModified (jlong time)
{
#ifdef HAVE_UTIME
utimbuf tb;
char buf[MAXPATHLEN];
jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
// FIXME?
buf[total] = '\0';
tb.actime = time / 1000;
tb.modtime = time / 1000;
return ::utime (buf, &tb);
#else
return false;
#endif
}
jboolean
java::io::File::performCreate (void)
{
char buf[MAXPATHLEN];
jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
// FIXME?
buf[total] = '\0';
int fd = ::open (buf, O_CREAT | O_EXCL, 0644);
if (fd < 0)
{
if (errno == EEXIST)
return false;
throw new IOException (JvNewStringLatin1 (strerror (errno)));
}
else
{
::close (fd);
return true;
}
}
jboolean
java::io::File::performDelete (void)
{
char buf[MAXPATHLEN];
jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
// FIXME?
buf[total] = '\0';
......@@ -266,3 +334,17 @@ java::io::File::performDelete (jstring canon)
#endif // HAVE_UNLINK
return false;
}
void
java::io::File::init_native ()
{
separator = JvNewStringLatin1 ("/");
pathSeparator = JvNewStringLatin1 (":");
char *tmp = ::getenv("TMPDIR");
if (! tmp)
tmp = "/tmp";
tmpdir = JvNewStringLatin1 (tmp);
maxPathLen = MAXPATHLEN;
caseSensitive = true;
}
// natFileWin32.cc - Native part of File class.
// natFileWin32.cc - Native part of File class for Win32.
/* Copyright (C) 1998, 1999 Red Hat, Inc.
/* Copyright (C) 1998, 1999, 2001 Red Hat, Inc.
This file is part of libgcj.
......@@ -25,12 +25,10 @@ details. */
#include <java/lang/System.h>
jboolean
java::io::File::access (jstring canon, jint query)
java::io::File::access (jint query)
{
if (! canon)
return false;
char buf[MAX_PATH];
jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
// FIXME?
buf[total] = '\0';
......@@ -48,15 +46,15 @@ java::io::File::access (jstring canon, jint query)
}
jboolean
java::io::File::stat (jstring canon, jint query)
java::io::File::stat (jint query)
{
if (! canon)
return false;
char buf[MAX_PATH];
jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
// FIXME?
buf[total] = '\0';
// FIXME: Need to handle ISHIDDEN query.
JvAssert (query == DIRECTORY || query == ISFILE);
DWORD attributes = GetFileAttributes (buf);
......@@ -70,12 +68,10 @@ java::io::File::stat (jstring canon, jint query)
}
jlong
java::io::File::attr (jstring canon, jint query)
java::io::File::attr (jint query)
{
if (! canon)
return false;
char buf[MAX_PATH];
jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
// FIXME?
buf[total] = '\0';
......@@ -128,12 +124,12 @@ java::io::File::isAbsolute (void)
}
jstringArray
java::io::File::performList (jstring canon, FilenameFilter *filter)
java::io::File::performList (java::io::FilenameFilter *filter,
java::io::FileFilter *fileFilter,
java::lang::Class *result_type)
{
if (! canon)
return NULL;
char buf[MAX_PATH];
jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
// FIXME?
strcpy(&buf[total], "\\*.*");
......@@ -142,16 +138,28 @@ java::io::File::performList (jstring canon, FilenameFilter *filter)
if (handle == INVALID_HANDLE_VALUE)
return NULL;
java::util::Vector *vec = new java::util::Vector ();
java::util::ArrayList *list = new java::util::ArrayList ();
do
{
if (strcmp (data.cFileName, ".") && strcmp (data.cFileName, ".."))
{
jstring name = JvNewStringUTF (data.cFileName);
if (! filter || (filter && filter->accept(this, name)))
vec->addElement (name);
}
if (filter && ! filter->accept(this, name))
continue;
if (result_type == &java::io::File::class$)
{
java::io::File *file = new java::io::File (this, name);
if (fileFilter && ! fileFilter->accept(file))
continue;
list->add(file);
}
else
list->add(name);
}
}
while (FindNextFile (handle, &data));
......@@ -160,7 +168,7 @@ java::io::File::performList (jstring canon, FilenameFilter *filter)
FindClose (handle);
jobjectArray ret = JvNewObjectArray (vec->size(), canon->getClass(), NULL);
jobjectArray ret = JvNewObjectArray (vec->size(), path->getClass(), NULL);
vec->copyInto (ret);
return reinterpret_cast<jstringArray> (ret);
}
......@@ -177,6 +185,20 @@ java::io::File::performMkdir (void)
}
jboolean
java::io::File::performSetReadOnly (void)
{
// PLEASE IMPLEMENT ME
return false;
}
JArray< ::java::io::File *>*
java::io::File::performListRoots ()
{
// PLEASE IMPLEMENT ME
return NULL;
}
jboolean
java::io::File::performRenameTo (File *dest)
{
char buf[MAX_PATH];
......@@ -192,10 +214,24 @@ java::io::File::performRenameTo (File *dest)
}
jboolean
java::io::File::performDelete (jstring canon)
java::io::File::performSetLastModified (jlong time)
{
// PLEASE IMPLEMENT ME
return false;
}
jboolean
java::io::File::performCreate (void)
{
// PLEASE IMPLEMENT ME
return false;
}
jboolean
java::io::File::performDelete ()
{
char buf[MAX_PATH];
jsize total = JvGetStringUTFRegion(canon, 0, canon->length(), buf);
jsize total = JvGetStringUTFRegion(path, 0, path->length(), buf);
// FIXME?
buf[total] = '\0';
......@@ -208,3 +244,13 @@ java::io::File::performDelete (jstring canon)
else
return (DeleteFile (buf)) ? true : false;
}
void
java::io::File::init_native ()
{
separator = JvNewStringLatin1 ("\\");
pathSeparator = JvNewStringLatin1 (";");
tmpdir = JvNewStringLatin1 ("C:\\temp"); // FIXME?
maxPathLen = MAX_PATH;
caseSensitive = false;
}
......@@ -55,6 +55,7 @@ details. */
#include <java/lang/StringBuffer.h>
#include <java/util/Properties.h>
#include <java/util/TimeZone.h>
#include <java/io/File.h>
#include <java/io/PrintStream.h>
#include <java/io/InputStream.h>
......@@ -323,20 +324,19 @@ java::lang::System::init_properties (void)
SET ("file.encoding", default_file_encoding);
JvInitClass (&java::io::File::class$);
newprops->put (JvNewStringLatin1 ("file.separator"),
java::io::File::separator);
newprops->put (JvNewStringLatin1 ("path.separator"),
java::io::File::pathSeparator);
newprops->put (JvNewStringLatin1 ("java.io.tmpdir"),
java::io::File::tmpdir);
#ifdef WIN32
SET ("file.separator", "\\");
SET ("path.separator", ";");
SET ("line.separator", "\r\n");
SET ("java.io.tmpdir", "C:\\temp");
#else
// Unix.
SET ("file.separator", "/");
SET ("path.separator", ":");
SET ("line.separator", "\n");
char *tmpdir = ::getenv("TMPDIR");
if (! tmpdir)
tmpdir = "/tmp";
SET ("java.io.tmpdir", tmpdir);
#endif
#ifdef HAVE_UNAME
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment