2001-04-01 13:16:40 +02:00
|
|
|
// natFile.cc - Native part of File class for POSIX.
|
1999-04-07 16:42:40 +02:00
|
|
|
|
2003-01-21 21:45:57 +01:00
|
|
|
/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation
|
1999-04-07 16:42:40 +02:00
|
|
|
|
|
|
|
This file is part of libgcj.
|
|
|
|
|
|
|
|
This software is copyrighted work licensed under the terms of the
|
|
|
|
Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
|
|
#ifdef HAVE_DIRENT_H
|
|
|
|
#include <dirent.h>
|
|
|
|
#endif
|
|
|
|
#include <string.h>
|
2001-04-01 13:16:40 +02:00
|
|
|
#include <utime.h>
|
1999-04-07 16:42:40 +02:00
|
|
|
|
1999-09-11 00:03:10 +02:00
|
|
|
#include <gcj/cni.h>
|
1999-04-07 16:42:40 +02:00
|
|
|
#include <jvm.h>
|
|
|
|
#include <java/io/File.h>
|
|
|
|
#include <java/io/IOException.h>
|
2001-04-01 13:16:40 +02:00
|
|
|
#include <java/util/ArrayList.h>
|
1999-04-07 16:42:40 +02:00
|
|
|
#include <java/lang/String.h>
|
|
|
|
#include <java/io/FilenameFilter.h>
|
2001-04-01 13:16:40 +02:00
|
|
|
#include <java/io/FileFilter.h>
|
1999-04-07 16:42:40 +02:00
|
|
|
#include <java/lang/System.h>
|
|
|
|
|
|
|
|
jboolean
|
2001-07-18 20:35:12 +02:00
|
|
|
java::io::File::_access (jint query)
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2002-02-08 05:48:03 +01:00
|
|
|
char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
|
2001-04-01 13:16:40 +02:00
|
|
|
jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
|
1999-04-07 16:42:40 +02:00
|
|
|
buf[total] = '\0';
|
|
|
|
JvAssert (query == READ || query == WRITE || query == EXISTS);
|
|
|
|
#ifdef HAVE_ACCESS
|
|
|
|
int mode;
|
|
|
|
if (query == READ)
|
|
|
|
mode = R_OK;
|
|
|
|
else if (query == WRITE)
|
|
|
|
mode = W_OK;
|
|
|
|
else
|
|
|
|
mode = F_OK;
|
|
|
|
return ::access (buf, mode) == 0;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
jboolean
|
2001-07-18 20:35:12 +02:00
|
|
|
java::io::File::_stat (jint query)
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2003-01-21 21:45:57 +01:00
|
|
|
if (query == ISHIDDEN)
|
|
|
|
return getName()->charAt(0) == '.';
|
|
|
|
|
|
|
|
#ifdef HAVE_STAT
|
2002-02-08 05:48:03 +01:00
|
|
|
char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
|
2001-04-01 13:16:40 +02:00
|
|
|
jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
|
1999-04-07 16:42:40 +02:00
|
|
|
buf[total] = '\0';
|
|
|
|
|
|
|
|
struct stat sb;
|
|
|
|
if (::stat (buf, &sb))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
JvAssert (query == DIRECTORY || query == ISFILE);
|
|
|
|
jboolean r = S_ISDIR (sb.st_mode);
|
|
|
|
return query == DIRECTORY ? r : ! r;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
jlong
|
2001-04-01 13:16:40 +02:00
|
|
|
java::io::File::attr (jint query)
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2002-02-08 05:48:03 +01:00
|
|
|
char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
|
2001-04-01 13:16:40 +02:00
|
|
|
jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
|
1999-04-07 16:42:40 +02:00
|
|
|
buf[total] = '\0';
|
|
|
|
|
|
|
|
#ifdef HAVE_STAT
|
|
|
|
struct stat sb;
|
|
|
|
// FIXME: not sure about return value here.
|
|
|
|
if (::stat (buf, &sb))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
JvAssert (query == MODIFIED || query == LENGTH);
|
1999-07-26 04:51:44 +02:00
|
|
|
return query == MODIFIED ? (jlong)sb.st_mtime * 1000 : sb.st_size;
|
1999-04-07 16:42:40 +02:00
|
|
|
#else
|
|
|
|
// There's no good choice here.
|
|
|
|
return 23;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
jstring
|
|
|
|
java::io::File::getCanonicalPath (void)
|
|
|
|
{
|
2002-06-12 19:01:02 +02:00
|
|
|
// We use `+2' here because we might need to use `.' for our special
|
|
|
|
// case.
|
|
|
|
char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 2);
|
2002-02-07 20:18:35 +01:00
|
|
|
char buf2[MAXPATHLEN];
|
1999-04-07 16:42:40 +02:00
|
|
|
jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
|
2002-06-12 19:01:02 +02:00
|
|
|
|
|
|
|
// Special case: treat "" the same as ".".
|
|
|
|
if (total == 0)
|
|
|
|
buf[total++] = '.';
|
|
|
|
|
1999-04-07 16:42:40 +02:00
|
|
|
buf[total] = '\0';
|
|
|
|
|
|
|
|
#ifdef HAVE_REALPATH
|
|
|
|
if (realpath (buf, buf2) == NULL)
|
exception.cc (java_eh_info): Make value type jthrowable.
* exception.cc (java_eh_info): Make value type jthrowable.
(_Jv_type_matcher): Remove now unneeded cast.
(_Jv_Throw): Make argument type jthrowable. Munge name
for SJLJ_EXCEPTIONS here ...
* gcj/cni.h: ... not here.
(JvThrow): Remove.
* gcj/javaprims.h (_Jv_Throw, _Jv_Sjlj_Throw): Update declarations.
* defineclass.cc, interpret.cc, jni.cc, posix-threads.cc,
prims.cc, resolve.cc, gnu/gcj/runtime/natFirstThread.cc,
gnu/gcj/xlib/natDrawable.cc, gnu/gcj/xlib/natFont.cc,
gnu/gcj/xlib/natWMSizeHints.cc, gnu/gcj/xlib/natWindowAttributes.cc,
gnu/gcj/xlib/natXImage.cc, java/io/natFile.cc,
java/io/natFileDescriptorEcos.cc, java/io/natFileDescriptorPosix.cc,
java/io/natFileDescriptorWin32.cc, java/io/natFileWin32.cc,
java/lang/natClass.cc, java/lang/natClassLoader.cc,
java/lang/natDouble.cc, java/lang/natObject.cc,
java/lang/natPosixProcess.cc, java/lang/natRuntime.cc,
java/lang/natString.cc, java/lang/natSystem.cc,
java/lang/natThread.cc, java/lang/reflect/natArray.cc,
java/lang/reflect/natConstructor.cc, java/lang/reflect/natField.cc,
java/lang/reflect/natMethod.cc, java/util/zip/natDeflater.cc,
java/util/zip/natInflater.cc:
Use throw, not JvThrow or _Jv_Throw.
From-SVN: r40838
2001-03-26 09:05:32 +02:00
|
|
|
throw new IOException (JvNewStringLatin1 (strerror (errno)));
|
1999-04-07 16:42:40 +02:00
|
|
|
|
|
|
|
// FIXME: what encoding to assume for file names? This affects many
|
|
|
|
// calls.
|
|
|
|
return JvNewStringUTF (buf2);
|
|
|
|
#else
|
|
|
|
return JvNewStringUTF (buf);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
jboolean
|
|
|
|
java::io::File::isAbsolute (void)
|
|
|
|
{
|
2003-01-21 21:45:57 +01:00
|
|
|
return path->length() > 0 && path->charAt(0) == '/';
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2001-04-01 13:16:40 +02:00
|
|
|
jobjectArray
|
|
|
|
java::io::File::performList (java::io::FilenameFilter *filter,
|
|
|
|
java::io::FileFilter *fileFilter,
|
|
|
|
java::lang::Class *result_type)
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2002-02-07 04:24:12 +01:00
|
|
|
/* Some systems have dirent.h, but no directory reading functions like
|
|
|
|
opendir. */
|
|
|
|
#if defined(HAVE_DIRENT_H) && defined(HAVE_OPENDIR)
|
2002-02-08 05:48:03 +01:00
|
|
|
char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
|
2001-04-01 13:16:40 +02:00
|
|
|
jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
|
1999-04-07 16:42:40 +02:00
|
|
|
buf[total] = '\0';
|
|
|
|
|
|
|
|
DIR *dir = opendir (buf);
|
|
|
|
if (! dir)
|
|
|
|
return NULL;
|
|
|
|
|
2001-04-01 13:16:40 +02:00
|
|
|
java::util::ArrayList *list = new java::util::ArrayList ();
|
2001-06-01 06:04:10 +02:00
|
|
|
struct dirent *d;
|
|
|
|
#ifdef HAVE_READDIR_R
|
|
|
|
int name_max = pathconf (buf, _PC_NAME_MAX);
|
|
|
|
char dbuf[sizeof (struct dirent) + name_max + 1];
|
|
|
|
while (readdir_r (dir, (struct dirent *) dbuf, &d) == 0 && d != NULL)
|
|
|
|
#else /* HAVE_READDIR_R */
|
|
|
|
while ((d = readdir (dir)) != NULL)
|
|
|
|
#endif /* HAVE_READDIR_R */
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2001-04-01 13:16:40 +02:00
|
|
|
// Omit "." and "..".
|
|
|
|
if (d->d_name[0] == '.'
|
|
|
|
&& (d->d_name[1] == '\0'
|
|
|
|
|| (d->d_name[1] == '.' && d->d_name[2] == '\0')))
|
1999-04-07 16:42:40 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
jstring name = JvNewStringUTF (d->d_name);
|
|
|
|
if (filter && ! filter->accept(this, name))
|
|
|
|
continue;
|
2002-02-07 20:18:35 +01:00
|
|
|
|
2001-04-01 13:16:40 +02:00
|
|
|
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);
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
closedir (dir);
|
|
|
|
|
2001-04-01 13:16:40 +02:00
|
|
|
jobjectArray ret = JvNewObjectArray (list->size(), result_type, NULL);
|
|
|
|
list->toArray(ret);
|
|
|
|
return ret;
|
2002-02-07 04:24:12 +01:00
|
|
|
#else /* HAVE_DIRENT_H && HAVE_OPENDIR */
|
1999-04-07 16:42:40 +02:00
|
|
|
return NULL;
|
2002-02-07 04:24:12 +01:00
|
|
|
#endif /* HAVE_DIRENT_H && HAVE_OPENDIR */
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
jboolean
|
|
|
|
java::io::File::performMkdir (void)
|
|
|
|
{
|
2002-02-08 05:48:03 +01:00
|
|
|
char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
|
1999-04-07 16:42:40 +02:00
|
|
|
jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
|
|
|
|
buf[total] = '\0';
|
|
|
|
|
|
|
|
#ifdef HAVE_MKDIR
|
|
|
|
return ::mkdir (buf, 0755) == 0;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-04-01 13:16:40 +02:00
|
|
|
jboolean
|
|
|
|
java::io::File::performSetReadOnly (void)
|
|
|
|
{
|
2002-02-08 05:48:03 +01:00
|
|
|
char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
|
2001-04-01 13:16:40 +02:00
|
|
|
jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
|
|
|
|
buf[total] = '\0';
|
|
|
|
|
2001-05-01 01:09:39 +02:00
|
|
|
#if defined (HAVE_STAT) && defined (HAVE_CHMOD)
|
2001-04-01 13:16:40 +02:00
|
|
|
struct stat sb;
|
|
|
|
if (::stat (buf, &sb))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (::chmod(buf, sb.st_mode & 0555))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
JArray< ::java::io::File *>*
|
|
|
|
java::io::File::performListRoots ()
|
|
|
|
{
|
2002-02-07 20:18:35 +01:00
|
|
|
::java::io::File *f = new ::java::io::File (JvNewStringLatin1 ("/"));
|
|
|
|
JArray<java::io::File *> *unixroot
|
|
|
|
= reinterpret_cast <JArray<java::io::File *>*>
|
|
|
|
(JvNewObjectArray (1, &java::io::File::class$, f));
|
|
|
|
elements (unixroot) [0] = f;
|
2001-04-01 13:16:40 +02:00
|
|
|
return unixroot;
|
|
|
|
}
|
|
|
|
|
1999-04-07 16:42:40 +02:00
|
|
|
jboolean
|
|
|
|
java::io::File::performRenameTo (File *dest)
|
|
|
|
{
|
2002-02-08 05:48:03 +01:00
|
|
|
char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
|
1999-04-07 16:42:40 +02:00
|
|
|
jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
|
|
|
|
buf[total] = '\0';
|
2002-02-08 05:48:03 +01:00
|
|
|
char *buf2
|
|
|
|
= (char *) __builtin_alloca (JvGetStringUTFLength (dest->path) + 1);
|
1999-04-07 16:42:40 +02:00
|
|
|
total = JvGetStringUTFRegion (dest->path, 0, dest->path->length(), buf2);
|
|
|
|
buf2[total] = '\0';
|
|
|
|
|
|
|
|
#ifdef HAVE_RENAME
|
|
|
|
return ::rename (buf, buf2) == 0;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
jboolean
|
2001-04-01 13:16:40 +02:00
|
|
|
java::io::File::performSetLastModified (jlong time)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_UTIME
|
|
|
|
utimbuf tb;
|
|
|
|
|
2002-02-08 05:48:03 +01:00
|
|
|
char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
|
2001-04-01 13:16:40 +02:00
|
|
|
jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
|
|
|
|
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)
|
|
|
|
{
|
2002-02-08 05:48:03 +01:00
|
|
|
char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
|
2001-04-01 13:16:40 +02:00
|
|
|
jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
|
|
|
|
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)
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2002-02-08 05:48:03 +01:00
|
|
|
char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
|
2001-04-01 13:16:40 +02:00
|
|
|
jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
|
1999-04-07 16:42:40 +02:00
|
|
|
buf[total] = '\0';
|
|
|
|
|
|
|
|
#ifdef HAVE_UNLINK
|
|
|
|
#ifdef HAVE_RMDIR
|
|
|
|
if (! ::rmdir (buf))
|
|
|
|
return true;
|
|
|
|
if (errno == ENOTDIR)
|
2001-05-01 01:09:39 +02:00
|
|
|
#endif // HAVE_RMDIR
|
1999-04-07 16:42:40 +02:00
|
|
|
return ::unlink (buf) == 0;
|
|
|
|
#endif // HAVE_UNLINK
|
|
|
|
return false;
|
|
|
|
}
|
2001-04-01 13:16:40 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
java::io::File::init_native ()
|
|
|
|
{
|
|
|
|
maxPathLen = MAXPATHLEN;
|
|
|
|
caseSensitive = true;
|
|
|
|
}
|