1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
// natFile.cc - Native part of File class.
/* Copyright (C) 1998, 1999 Cygnus Solutions
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
#define _POSIX_PTHREAD_SEMANTICS
#ifndef _REENTRANT
# define _REENTRANT
#endif
#include <dirent.h>
#endif
#include <string.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/lang/String.h>
#include <java/io/FilenameFilter.h>
#include <java/lang/System.h>
jboolean
java::io::File::access (jstring canon, jint query)
{
if (! canon)
return false;
char buf[MAXPATHLEN];
jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
// FIXME?
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
java::io::File::stat (jstring canon, jint query)
{
if (! canon)
return false;
char buf[MAXPATHLEN];
jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
// FIXME?
buf[total] = '\0';
#ifdef HAVE_STAT
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
java::io::File::attr (jstring canon, jint query)
{
if (! canon)
return false;
char buf[MAXPATHLEN];
jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
// FIXME?
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);
// 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.
return 23;
#endif
}
jstring
java::io::File::getCanonicalPath (void)
{
char buf[MAXPATHLEN], buf2[MAXPATHLEN];
jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
// FIXME?
buf[total] = '\0';
#ifdef HAVE_REALPATH
if (realpath (buf, buf2) == NULL)
_Jv_Throw (new IOException (JvNewStringLatin1 (strerror (errno))));
// 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)
{
// 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
#if defined(__JV_POSIX_THREADS__) && defined(HAVE_READDIR_R)
static struct dirent *
get_entry (DIR *dir, struct dirent *e)
{
struct dirent *r;
if (readdir_r (dir, e, &r) || r == NULL)
return NULL;
return e;
}
#else /* defined(__JV_POSIX_THREADS__) && defined(HAVE_READDIR_R) */
static struct dirent *
get_entry (DIR *dir, struct dirent *)
{
return readdir (dir);
}
#endif /* defined(__JV_POSIX_THREADS__) && defined(HAVE_READDIR_R) */
#endif /* HAVE_DIRENT_H */
jstringArray
java::io::File::performList (jstring canon, FilenameFilter *filter)
{
if (! canon)
return NULL;
#ifdef HAVE_DIRENT_H
char buf[MAXPATHLEN];
jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
// FIXME?
buf[total] = '\0';
DIR *dir = opendir (buf);
if (! dir)
return NULL;
java::util::Vector *vec = new java::util::Vector ();
struct dirent *d, d2;
while ((d = get_entry (dir, &d2)) != NULL)
{
if (! strcmp (d->d_name, ".") || ! strcmp (d->d_name, ".."))
continue;
jstring name = JvNewStringUTF (d->d_name);
if (filter && ! filter->accept(this, name))
continue;
vec->addElement(name);
}
closedir (dir);
jobjectArray ret = JvNewObjectArray (vec->size(), canon->getClass(),
NULL);
vec->copyInto(ret);
return reinterpret_cast<jstringArray> (ret);
#else /* HAVE_DIRENT_H */
return NULL;
#endif /* HAVE_DIRENT_H */
}
jboolean
java::io::File::performMkdir (void)
{
char buf[MAXPATHLEN];
jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
// FIXME?
buf[total] = '\0';
#ifdef HAVE_MKDIR
return ::mkdir (buf, 0755) == 0;
#else
return false;
#endif
}
jboolean
java::io::File::performRenameTo (File *dest)
{
char buf[MAXPATHLEN];
jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
// FIXME?
buf[total] = '\0';
char buf2[MAXPATHLEN];
total = JvGetStringUTFRegion (dest->path, 0, dest->path->length(), buf2);
// FIXME?
buf2[total] = '\0';
#ifdef HAVE_RENAME
return ::rename (buf, buf2) == 0;
#else
return false;
#endif
}
jboolean
java::io::File::performDelete (jstring canon)
{
char buf[MAXPATHLEN];
jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
// FIXME?
buf[total] = '\0';
#ifdef HAVE_UNLINK
#ifdef HAVE_RMDIR
if (! ::rmdir (buf))
return true;
#endif // HAVE_RMDIR
if (errno == ENOTDIR)
return ::unlink (buf) == 0;
#endif // HAVE_UNLINK
return false;
}
|