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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [native/] [jni/] [native-lib/] [cpio.c] - Blame information for rev 774

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 774 jeremybenn
/* cpio.c - Common java file IO native functions
2
   Copyright (C) 2005 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
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)
9
any later version.
10
 
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.
15
 
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
19
02110-1301 USA.
20
 
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
24
combination.
25
 
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. */
37
 
38
/* do not move; needed here because of some macro definitions */
39
#include <config.h>
40
 
41
#include <stdio.h>
42
#include <stdlib.h>
43
#include <errno.h>
44
#include <string.h>
45
#include <sys/types.h>
46
#include <dirent.h>
47
 
48
#include <jni.h>
49
 
50
#if defined(HAVE_SYS_IOCTL_H)
51
#define BSD_COMP /* Get FIONREAD on Solaris2 */
52
#include <sys/ioctl.h>
53
#endif
54
#if defined(HAVE_SYS_FILIO_H) /* Get FIONREAD on Solaris 2.5 */
55
#include <sys/filio.h>
56
#endif
57
 
58
#if defined(HAVE_SYS_STAT_H)
59
#include <sys/stat.h>
60
#endif
61
 
62
#if defined(HAVE_FCNTL_H)
63
#include <fcntl.h>
64
#endif
65
 
66
#if defined(HAVE_UNISTD_H)
67
#include <unistd.h>
68
#endif
69
 
70
#if defined(HAVE_SYS_SELECT_H)
71
#include <sys/select.h>
72
#endif
73
 
74
#if defined(HAVE_STATVFS)
75
#include <sys/statvfs.h>
76
#endif
77
 
78
#include <utime.h>
79
 
80
#include "cpnative.h"
81
#include "cpio.h"
82
 
83
/* Some POSIX systems don't have O_SYNC and O_DYSNC so we define them here.  */
84
#if !defined (O_SYNC) && defined (O_FSYNC)
85
#define O_SYNC O_FSYNC
86
#endif
87
#if !defined (O_DSYNC) && defined (O_FSYNC)
88
#define O_DSYNC O_FSYNC
89
#endif
90
/* If O_DSYNC is still not defined, use O_SYNC (needed for newlib).  */
91
#if !defined (O_DSYNC)
92
#define O_DSYNC O_SYNC
93
#endif
94
 
95
JNIEXPORT int cpio_openFile (const char *filename, int *fd, int flags, int permissions)
96
{
97
  int sflags = 0;
98
  int rwflags = flags & CPFILE_FLAG_READWRITE;
99
  int perms;
100
 
101
  if (flags & CPFILE_FLAG_CREATE)
102
    sflags |= O_CREAT;
103
  if (flags & CPFILE_FLAG_APPEND)
104
    sflags |= O_APPEND;
105
  if (flags & CPFILE_FLAG_TRUNCATE)
106
    sflags |= O_TRUNC;
107
  if (flags & CPFILE_FLAG_SYNC)
108
    sflags |= O_SYNC;
109
  if (flags & CPFILE_FLAG_DSYNC)
110
    sflags |= O_DSYNC;
111
#if defined(O_BINARY)
112
  if (flags & CPFILE_FLAG_BINARY)
113
    sflags |= O_BINARY;
114
#endif
115
 
116
  switch (rwflags)
117
    {
118
    case CPFILE_FLAG_READ:
119
      sflags |= O_RDONLY;
120
      break;
121
    case CPFILE_FLAG_WRITE:
122
      sflags |= O_WRONLY;
123
      break;
124
    case CPFILE_FLAG_READWRITE:
125
      sflags |= O_RDWR;
126
      break;
127
    }
128
 
129
  if (permissions == CPFILE_PERMISSION_NORMAL)
130
          perms = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
131
  else
132
          perms = 0;
133
 
134
  *fd = open (filename, sflags, perms);
135
 
136
  if (*fd < 0)
137
    return errno;
138
 
139
  return CPNATIVE_OK;
140
}
141
 
142
JNIEXPORT int cpio_closeFile (int fd)
143
{
144
  if (close (fd) < 0)
145
    return errno;
146
 
147
  return CPNATIVE_OK;
148
}
149
 
150
JNIEXPORT int cpio_availableBytes (int fd, jlong *bytes_available)
151
{
152
#if defined (FIONREAD)
153
  ssize_t n;
154
 
155
  if (ioctl (fd, FIONREAD, (char *)&n) != 0)
156
    return errno;
157
 
158
  *bytes_available = n;
159
  return CPNATIVE_OK;
160
#elif defined(HAVE_FSTAT)
161
  struct stat statBuffer;
162
  off_t n;
163
  int result;
164
 
165
  *bytes_available = 0;
166
  if ((fstat (fd, &statBuffer) == 0) && S_ISREG (statBuffer.st_mode))
167
    {
168
      n = lseek (fd, 0, SEEK_CUR);
169
      if (n != -1)
170
       {
171
         *bytes_available = statBuffer.st_size - n;
172
         result = CPNATIVE_OK;
173
       }
174
      else
175
       {
176
         result = errno;
177
       }
178
    }
179
  else
180
    {
181
      result = errno;
182
    }
183
 
184
  return result;
185
#elif defined(HAVE_SELECT)
186
  fd_set filedescriptset;
187
  struct timeval tv;
188
  int result;
189
 
190
  *bytes_available = 0;
191
 
192
  FD_ZERO (&filedescriptset);
193
  FD_SET (fd,&filedescriptset);
194
  memset (&tv, 0, sizeof(tv));
195
 
196
  switch (select (fd+1, &filedescriptset, NULL, NULL, &tv))
197
    {
198
    case -1:
199
      result=errno;
200
      break;
201
    case  0:
202
      *bytes_available = 0;
203
      result = CPNATIVE_OK;
204
      break;
205
    default:
206
      *bytes_available = 1;
207
      result = CPNATIVE_OK;
208
      break;
209
    }
210
  return result;
211
 
212
#else
213
  *bytes_available = 0;
214
  return ENOTSUP;
215
#endif
216
}
217
 
218
JNIEXPORT int cpio_getFileSize (int fd, jlong *filesize)
219
{
220
  struct stat statBuffer;
221
 
222
  if (fstat(fd, &statBuffer) < 0)
223
    return errno;
224
 
225
  *filesize = statBuffer.st_size;
226
  return CPNATIVE_OK;
227
}
228
 
229
JNIEXPORT int cpio_getFilePosition (int fd, jlong *offset)
230
{
231
  *offset = lseek (fd, 0, SEEK_CUR);
232
  if (*offset < 0)
233
    return errno;
234
 
235
  return CPNATIVE_OK;
236
}
237
 
238
JNIEXPORT int cpio_setFilePosition (int fd, jlong position)
239
{
240
  if (lseek (fd, position, SEEK_SET) < 0)
241
    return errno;
242
 
243
  return CPNATIVE_OK;
244
}
245
 
246
JNIEXPORT int cpio_read (int fd, void *buffer, jint length, jint *bytes_read)
247
{
248
  *bytes_read = read (fd, buffer, length);
249
 
250
  if (*bytes_read < 0)
251
  {
252
    return errno;
253
  }
254
 
255
  return CPNATIVE_OK;
256
}
257
 
258
JNIEXPORT int cpio_write (int fd, const void *buffer, jint length, jint *bytes_written)
259
{
260
  *bytes_written = write (fd, buffer, length);
261
 
262
  if (*bytes_written < 0)
263
    return errno;
264
 
265
  return CPNATIVE_OK;
266
}
267
 
268
JNIEXPORT int cpio_fsync (int fd)
269
{
270
  if (fsync (fd) < 0)
271
    return errno;
272
 
273
  return CPNATIVE_OK;
274
}
275
 
276
JNIEXPORT int cpio_truncate (int fd, jlong size)
277
{
278
  if (ftruncate (fd, size) < 0)
279
    return errno;
280
 
281
  return CPNATIVE_OK;
282
}
283
 
284
JNIEXPORT int cpio_setFileSize (int native_fd, jlong new_size)
285
{
286
  jlong file_size;
287
  jlong save_offset;
288
  int result;
289
  char data;
290
  jint bytes_written;
291
 
292
  result = cpio_getFileSize (native_fd, &file_size);
293
  if (result != CPNATIVE_OK)
294
    return result;
295
 
296
  /* Save off current position */
297
  result = cpio_getFilePosition (native_fd, &save_offset);
298
  if (result != CPNATIVE_OK)
299
    return result;
300
 
301
  if (file_size < new_size)
302
    {
303
      /* File is too short -- seek to one byte short of where we want,
304
       * then write a byte */
305
 
306
      /* move to position n-1 */
307
      result = cpio_setFilePosition (native_fd, new_size-1);
308
      if (result != CPNATIVE_OK)
309
        return result;
310
 
311
      /* write a byte
312
         Note: This will fail if we somehow get here in read only mode
313
         * That shouldn't happen */
314
      data = '\0';
315
      result = cpio_write (native_fd, &data, 1, &bytes_written);
316
      if (result != CPNATIVE_OK)
317
        return result;
318
 
319
      /* Reposition file pointer to where we started if not beyond new len. */
320
      if (save_offset < new_size)
321
        {
322
          result = cpio_setFilePosition (native_fd, save_offset);
323
          if (result != CPNATIVE_OK)
324
            return result;
325
        }
326
    }
327
  else if (new_size < file_size)
328
    {
329
      /* File is too long - use ftruncate if available */
330
      result = cpio_truncate (native_fd, new_size);
331
      if (result != CPNATIVE_OK)
332
          return result;
333
 
334
      /* Reposition file pointer when it now is beyond the end of file. */
335
      if (new_size < save_offset)
336
        {
337
          result = cpio_setFilePosition (native_fd, new_size);
338
          if (result != CPNATIVE_OK)
339
            return result;
340
        }
341
    }
342
 
343
  return CPNATIVE_OK;
344
}
345
 
346
int cpio_setFileReadonly (const char *filename)
347
{
348
  struct stat statbuf;
349
 
350
  if (stat(filename, &statbuf) < 0)
351
    return errno;
352
 
353
#ifdef S_IWRITE 
354
  if (chmod(filename, statbuf.st_mode & ~(S_IWRITE | S_IWGRP | S_IWOTH)) < 0)
355
    return errno;
356
#endif
357
 
358
  return 0;
359
}
360
 
361
int cpio_chmod (const char *filename, int permissions)
362
{
363
  struct stat statbuf;
364
  int perms = 0;
365
 
366
  if (stat(filename, &statbuf) < 0)
367
    return errno;
368
 
369
  /* check for permission flags */
370
  if (permissions & CPFILE_FLAG_USR)
371
    {
372
      if (permissions & CPFILE_FLAG_READ)
373
        perms |= S_IRUSR;
374
 
375
      if (permissions & CPFILE_FLAG_WRITE)
376
        perms |= S_IWUSR;
377
 
378
      if (permissions & CPFILE_FLAG_EXEC)
379
        perms |= S_IXUSR;
380
    }
381
  else
382
    {
383
      if (permissions & CPFILE_FLAG_READ)
384
        perms |= (S_IRUSR | S_IRGRP | S_IROTH);
385
 
386
      if (permissions & CPFILE_FLAG_WRITE)
387
        perms |= (S_IWUSR | S_IWGRP | S_IWOTH);
388
 
389
      if (permissions & CPFILE_FLAG_EXEC)
390
        perms |= (S_IXUSR | S_IXGRP | S_IXOTH);
391
    }
392
 
393
  if (permissions & CPFILE_FLAG_OFF)
394
    perms = statbuf.st_mode & ~perms;
395
  else
396
    perms = statbuf.st_mode | perms;
397
 
398
  if (chmod(filename, perms) < 0)
399
    return errno;
400
 
401
  return 0;
402
}
403
 
404
JNIEXPORT long long
405
cpio_df (__attribute__((unused)) const char *path,
406
         __attribute__((unused)) CPFILE_DF_TYPE type)
407
{
408
  long long result = 0L;
409
 
410
#if defined(HAVE_STATVFS)
411
 
412
  long long scale_factor = 0L;
413
  struct statvfs buf;
414
 
415
  if (statvfs (path, &buf) < 0)
416
    return 0L;
417
 
418
  /* f_blocks, f_bfree and f_bavail are defined in terms of f_frsize */
419
  scale_factor = (long long) (buf.f_frsize);
420
 
421
  switch (type)
422
    {
423
      case TOTAL:
424
        result = (long long) (buf.f_blocks * scale_factor);
425
        break;
426
      case FREE:
427
        result = (long long) (buf.f_bfree * scale_factor);
428
        break;
429
      case USABLE:
430
        result = (long long) (buf.f_bavail * scale_factor);
431
        break;
432
      default:
433
        result = 0L;
434
        break;
435
    }
436
 
437
#endif
438
 
439
  return result;
440
}
441
 
442
int cpio_checkAccess (const char *filename, unsigned int flag)
443
{
444
  struct stat statbuf;
445
  unsigned int perms = 0;
446
 
447
  if (stat(filename, &statbuf) < 0)
448
    return errno;
449
 
450
  switch (flag)
451
    {
452
    case CPFILE_FLAG_READ:
453
      perms = R_OK;
454
      break;
455
 
456
    case CPFILE_FLAG_WRITE:
457
      perms = W_OK;
458
      break;
459
 
460
    case CPFILE_FLAG_EXEC:
461
    default:
462
      perms = X_OK;
463
      break;
464
    }
465
 
466
  return (access (filename, perms));
467
}
468
 
469
int cpio_isFileExists (const char *filename)
470
{
471
  struct stat statbuf;
472
 
473
  if (stat(filename, &statbuf) < 0)
474
    {
475
      return errno;
476
    }
477
 
478
  return 0;
479
}
480
 
481
int cpio_checkType (const char *filename, jint *entryType)
482
{
483
  struct stat statbuf;
484
 
485
  if (stat(filename, &statbuf) < 0)
486
    return errno;
487
 
488
  if (S_ISDIR(statbuf.st_mode))
489
    *entryType = CPFILE_DIRECTORY;
490
  else
491
    *entryType = CPFILE_FILE;
492
 
493
  return 0;
494
}
495
 
496
int cpio_getModificationTime (const char *filename, jlong *mtime)
497
{
498
  struct stat statbuf;
499
 
500
  if (stat(filename, &statbuf) < 0)
501
    return errno;
502
 
503
  *mtime = (jlong)statbuf.st_mtime * (jlong)1000;
504
 
505
  return 0;
506
}
507
 
508
int cpio_setModificationTime (const char *filename, jlong mtime)
509
{
510
  struct stat statbuf;
511
  struct utimbuf buf;
512
 
513
  if (stat(filename, &statbuf) < 0)
514
    return errno;
515
 
516
  buf.actime = statbuf.st_atime;
517
  buf.modtime = mtime / 1000;
518
 
519
  if (utime(filename, &buf) < 0)
520
    return errno;
521
 
522
  return 0;
523
}
524
 
525
int cpio_removeFile (const char *filename)
526
{
527
  if (unlink(filename) < 0 && rmdir(filename) < 0)
528
    return errno;
529
 
530
  return 0;
531
}
532
 
533
int cpio_mkdir (const char *path)
534
{
535
  if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) < 0)
536
    return errno;
537
 
538
  return 0;
539
}
540
 
541
int cpio_rename (const char *old_name, const char *new_name)
542
{
543
  if (rename(old_name, new_name) < 0)
544
    return errno;
545
 
546
  return 0;
547
}
548
 
549
int cpio_openDir (const char *dirname, void **handle)
550
{
551
  *handle = (void *)opendir(dirname);
552
  if (*handle == NULL)
553
    return errno;
554
 
555
  return 0;
556
}
557
 
558
int cpio_closeDir (void *handle)
559
{
560
  closedir((DIR *)handle);
561
  return 0;
562
}
563
 
564
 
565
int cpio_readDir (void *handle, char *filename)
566
{
567
  struct dirent *dBuf;
568
 
569
  errno = 0;
570
  dBuf = readdir((DIR *)handle);
571
 
572
  if (dBuf == NULL)
573
    {
574
      /* Some OS's (OS X) return NULL on end-of-dir, but
575
         don't set errno to anything. */
576
      if (errno == 0)
577
        return ENOENT; /* Whatever. */
578
      return errno;
579
    }
580
 
581
  strncpy (filename, dBuf->d_name, FILENAME_MAX - 1);
582
  return 0;
583
}
584
 
585
int
586
cpio_closeOnExec(int fd)
587
{
588
        if (fcntl (fd, F_SETFD, FD_CLOEXEC) == -1)
589
          return errno;
590
 
591
        return 0;
592
}

powered by: WebSVN 2.1.0

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