OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [libjava/] [java/] [io/] [natFilePosix.cc] - Blame information for rev 14

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jlechner
// natFile.cc - Native part of File class for POSIX.
2
 
3
/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003  Free Software Foundation
4
 
5
   This file is part of libgcj.
6
 
7
This software is copyrighted work licensed under the terms of the
8
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9
details.  */
10
 
11
#include <config.h>
12
 
13
#include <stdio.h>
14
#include <errno.h>
15
#include <sys/param.h>
16
#include <sys/stat.h>
17
#include <sys/types.h>
18
#include <fcntl.h>
19
#ifdef HAVE_UNISTD_H
20
#include <unistd.h>
21
#endif
22
#include <stdlib.h>
23
#ifdef HAVE_DIRENT_H
24
#include <dirent.h>
25
#endif
26
#include <string.h>
27
#include <utime.h>
28
 
29
#include <gcj/cni.h>
30
#include <jvm.h>
31
#include <java/io/File.h>
32
#include <java/io/IOException.h>
33
#include <java/util/ArrayList.h>
34
#include <java/lang/String.h>
35
#include <java/io/FilenameFilter.h>
36
#include <java/io/FileFilter.h>
37
#include <java/lang/System.h>
38
 
39
jboolean
40
java::io::File::_access (jint query)
41
{
42
  char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
43
  jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
44
  buf[total] = '\0';
45
  JvAssert (query == READ || query == WRITE || query == EXISTS);
46
#ifdef HAVE_ACCESS
47
  int mode;
48
  if (query == READ)
49
    mode = R_OK;
50
  else if (query == WRITE)
51
    mode = W_OK;
52
  else
53
    mode = F_OK;
54
  return ::access (buf, mode) == 0;
55
#else
56
  return false;
57
#endif
58
}
59
 
60
jboolean
61
java::io::File::_stat (jint query)
62
{
63
  if (query == ISHIDDEN)
64
    return getName()->charAt(0) == '.';
65
 
66
#ifdef HAVE_STAT
67
  char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
68
  jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
69
  buf[total] = '\0';
70
 
71
  struct stat sb;
72
  if (::stat (buf, &sb))
73
    return false;
74
 
75
  JvAssert (query == DIRECTORY || query == ISFILE);
76
  jboolean r = S_ISDIR (sb.st_mode);
77
  return query == DIRECTORY ? r : ! r;
78
#else
79
  return false;
80
#endif
81
}
82
 
83
jlong
84
java::io::File::attr (jint query)
85
{
86
  char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
87
  jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
88
  buf[total] = '\0';
89
 
90
#ifdef HAVE_STAT
91
  struct stat sb;
92
  // FIXME: not sure about return value here.
93
  if (::stat (buf, &sb))
94
    return 0;
95
 
96
  JvAssert (query == MODIFIED || query == LENGTH);
97
  return query == MODIFIED ? (jlong)sb.st_mtime * 1000 : sb.st_size;
98
#else
99
  // There's no good choice here.
100
  return 23;
101
#endif
102
}
103
 
104
jstring
105
java::io::File::getCanonicalPath (void)
106
{
107
  // We use `+2' here because we might need to use `.' for our special
108
  // case.
109
  char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 2);
110
  char buf2[MAXPATHLEN];
111
  jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
112
 
113
  // Special case: treat "" the same as ".".
114
  if (total == 0)
115
    buf[total++] = '.';
116
 
117
  buf[total] = '\0';
118
 
119
#ifdef HAVE_REALPATH
120
  if (realpath (buf, buf2) == NULL)
121
    {
122
      // If realpath failed, we have to come up with a canonical path
123
      // anyway.  We do this with purely textual manipulation.
124
      // FIXME: this isn't perfect.  You can construct a case where
125
      // we get a different answer from the JDK:
126
      // mkdir -p /tmp/a/b/c
127
      // ln -s /tmp/a/b /tmp/a/z
128
      // ... getCanonicalPath("/tmp/a/z/c/nosuchfile")
129
      // We will give /tmp/a/z/c/nosuchfile, while the JDK will
130
      // give /tmp/a/b/c/nosuchfile.
131
      int out_idx;
132
      if (buf[0] != '/')
133
        {
134
          // Not absolute, so start with current directory.
135
          if (getcwd (buf2, sizeof (buf2)) == NULL)
136
            throw new IOException ();
137
          out_idx = strlen (buf2);
138
        }
139
      else
140
        {
141
          buf2[0] = '/';
142
          out_idx = 1;
143
        }
144
      int in_idx = 0;
145
      while (buf[in_idx] != '\0')
146
        {
147
          // Skip '/'s.
148
          while (buf[in_idx] == '/')
149
            ++in_idx;
150
          int elt_start = in_idx;
151
          // Find next '/' or end of path.
152
          while (buf[in_idx] != '\0' && buf[in_idx] != '/')
153
            ++in_idx;
154
          if (in_idx == elt_start)
155
            {
156
              // An empty component means we've reached the end.
157
              break;
158
            }
159
          int len = in_idx - elt_start;
160
          if (len == 1 && buf[in_idx] == '.')
161
            continue;
162
          if (len == 2 && buf[in_idx] == '.' && buf[in_idx + 1] == '.')
163
            {
164
              // Found ".." component, lop off last part from existing
165
              // buffer.
166
              --out_idx;
167
              while (out_idx > 0 && buf2[out_idx] != '/')
168
                --out_idx;
169
              // Can't go up past "/".
170
              if (out_idx == 0)
171
                ++out_idx;
172
            }
173
          else
174
            {
175
              // Append a real path component to the output.
176
              if (out_idx > 1)
177
                buf2[out_idx++] = '/';
178
              strncpy (&buf2[out_idx], &buf[elt_start], len);
179
              out_idx += len;
180
            }
181
        }
182
 
183
      buf2[out_idx] = '\0';
184
    }
185
 
186
  // FIXME: what encoding to assume for file names?  This affects many
187
  // calls.
188
  return JvNewStringUTF (buf2);
189
#else
190
  return JvNewStringUTF (buf);
191
#endif
192
}
193
 
194
jboolean
195
java::io::File::isAbsolute (void)
196
{
197
  return path->length() > 0 && path->charAt(0) == '/';
198
}
199
 
200
jobjectArray
201
java::io::File::performList (java::io::FilenameFilter *filter,
202
                             java::io::FileFilter *fileFilter,
203
                             java::lang::Class *result_type)
204
{
205
  /* Some systems have dirent.h, but no directory reading functions like
206
     opendir.  */
207
#if defined(HAVE_DIRENT_H) && defined(HAVE_OPENDIR)
208
  char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
209
  jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
210
  buf[total] = '\0';
211
 
212
  DIR *dir = opendir (buf);
213
  if (! dir)
214
    return NULL;
215
 
216
  java::util::ArrayList *list = new java::util::ArrayList ();
217
  struct dirent *d;
218
#ifdef HAVE_READDIR_R
219
  int name_max = pathconf (buf, _PC_NAME_MAX);
220
  char dbuf[sizeof (struct dirent) + name_max + 1];
221
  while (readdir_r (dir, (struct dirent *) dbuf, &d) == 0 && d != NULL)
222
#else /* HAVE_READDIR_R */
223
  while ((d = readdir (dir)) != NULL)
224
#endif /* HAVE_READDIR_R */
225
    {
226
      // Omit "." and "..".
227
      if (d->d_name[0] == '.'
228
          && (d->d_name[1] == '\0'
229
              || (d->d_name[1] == '.' && d->d_name[2] == '\0')))
230
        continue;
231
 
232
      jstring name = JvNewStringUTF (d->d_name);
233
      if (filter && ! filter->accept(this, name))
234
        continue;
235
 
236
      if (result_type == &java::io::File::class$)
237
        {
238
          java::io::File *file = new java::io::File (this, name);
239
          if (fileFilter && ! fileFilter->accept(file))
240
            continue;
241
 
242
          list->add(file);
243
        }
244
      else
245
        list->add(name);
246
    }
247
 
248
  closedir (dir);
249
 
250
  jobjectArray ret = JvNewObjectArray (list->size(), result_type, NULL);
251
  list->toArray(ret);
252
  return ret;
253
#else /* HAVE_DIRENT_H && HAVE_OPENDIR */
254
  return NULL;
255
#endif /* HAVE_DIRENT_H && HAVE_OPENDIR */
256
}
257
 
258
jboolean
259
java::io::File::performMkdir (void)
260
{
261
  char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
262
  jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
263
  buf[total] = '\0';
264
 
265
#ifdef HAVE_MKDIR
266
  return ::mkdir (buf, 0755) == 0;
267
#else
268
  return false;
269
#endif
270
}
271
 
272
jboolean
273
java::io::File::performSetReadOnly (void)
274
{
275
  char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
276
  jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
277
  buf[total] = '\0';
278
 
279
#if defined (HAVE_STAT) && defined (HAVE_CHMOD)
280
  struct stat sb;
281
  if (::stat (buf, &sb))
282
    return false;
283
 
284
  if (::chmod(buf, sb.st_mode & 0555))
285
    return false;
286
  return true;
287
#else
288
  return false;
289
#endif
290
}
291
 
292
JArray< ::java::io::File *>*
293
java::io::File::performListRoots ()
294
{
295
  ::java::io::File *f = new ::java::io::File (JvNewStringLatin1 ("/"));
296
  JArray<java::io::File *> *unixroot
297
    = reinterpret_cast <JArray<java::io::File *>*>
298
          (JvNewObjectArray (1, &java::io::File::class$, f));
299
  elements (unixroot) [0] = f;
300
  return unixroot;
301
}
302
 
303
jboolean
304
java::io::File::performRenameTo (File *dest)
305
{
306
  char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
307
  jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
308
  buf[total] = '\0';
309
  char *buf2
310
    = (char *) __builtin_alloca (JvGetStringUTFLength (dest->path) + 1);
311
  total = JvGetStringUTFRegion (dest->path, 0, dest->path->length(), buf2);
312
  buf2[total] = '\0';
313
 
314
#ifdef HAVE_RENAME
315
  return ::rename (buf, buf2) == 0;
316
#else
317
  return false;
318
#endif
319
}
320
 
321
jboolean
322
java::io::File::performSetLastModified (jlong time)
323
{
324
#ifdef HAVE_UTIME
325
  utimbuf tb;
326
 
327
  char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
328
  jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
329
  buf[total] = '\0';
330
 
331
  tb.actime = time / 1000;
332
  tb.modtime = time / 1000;
333
  return ::utime (buf, &tb);
334
#else
335
  return false;
336
#endif
337
}
338
 
339
jboolean
340
java::io::File::performCreate (void)
341
{
342
  char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
343
  jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
344
  buf[total] = '\0';
345
 
346
  int fd = ::open (buf, O_CREAT | O_EXCL, 0644);
347
 
348
  if (fd < 0)
349
    {
350
      if (errno == EEXIST)
351
        return false;
352
      throw new IOException (JvNewStringLatin1 (strerror (errno)));
353
    }
354
  else
355
    {
356
      ::close (fd);
357
      return true;
358
    }
359
}
360
 
361
jboolean
362
java::io::File::performDelete (void)
363
{
364
  char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
365
  jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
366
  buf[total] = '\0';
367
 
368
#ifdef HAVE_UNLINK
369
#ifdef HAVE_RMDIR
370
  if (! ::rmdir (buf))
371
    return true;
372
  if (errno == ENOTDIR)
373
#endif // HAVE_RMDIR
374
    return ::unlink (buf) == 0;
375
#endif // HAVE_UNLINK
376
  return false;
377
}
378
 
379
void
380
java::io::File::init_native ()
381
{
382
  maxPathLen = MAXPATHLEN;
383
  caseSensitive = true;
384
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.