1 /* target_generic_file - Native methods for file operations
2    Copyright (C) 1998, 2004 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 /*
39 Description: generic target defintions of file functions
40 Systems    : all
41 */
42 
43 #ifndef __TARGET_GENERIC_FILE__
44 #define __TARGET_GENERIC_FILE__
45 
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 
50 /* check if target_native_file.h included */
51 #ifndef __TARGET_NATIVE_FILE__
52   #error Do NOT INCLUDE generic target files! Include the corresponding native target files instead!
53 #endif
54 
55 /****************************** Includes *******************************/
56 /* do not move; needed here because of some macro definitions */
57 #include "config.h"
58 
59 #include <stdlib.h>
60 #include <assert.h>
61 #include <fcntl.h>
62 
63 #include "target_native.h"
64 #include "target_native_math_int.h"
65 
66 /****************** Conditional compilation switches *******************/
67 
68 /***************************** Constants *******************************/
69 #ifndef TARGET_NATIVE_FILE_FILEFLAG_NONE
70   #define TARGET_NATIVE_FILE_FILEFLAG_NONE 0
71 #endif
72 #ifndef TARGET_NATIVE_FILE_FILEFLAG_CREATE
73   #define TARGET_NATIVE_FILE_FILEFLAG_CREATE O_CREAT
74 #endif
75 #ifndef TARGET_NATIVE_FILE_FILEFLAG_CREATE_FORCE
76   #define TARGET_NATIVE_FILE_FILEFLAG_CREATE_FORCE (O_CREAT|O_EXCL)
77 #endif
78 #ifndef TARGET_NATIVE_FILE_FILEFLAG_READ
79   #define TARGET_NATIVE_FILE_FILEFLAG_READ O_RDONLY
80 #endif
81 #ifndef TARGET_NATIVE_FILE_FILEFLAG_WRITE
82   #define TARGET_NATIVE_FILE_FILEFLAG_WRITE O_WRONLY
83 #endif
84 #ifndef TARGET_NATIVE_FILE_FILEFLAG_READWRITE
85   #define TARGET_NATIVE_FILE_FILEFLAG_READWRITE O_RDWR
86 #endif
87 #ifndef TARGET_NATIVE_FILE_FILEFLAG_TRUNCATE
88   #define TARGET_NATIVE_FILE_FILEFLAG_TRUNCATE O_TRUNC
89 #endif
90 #ifndef TARGET_NATIVE_FILE_FILEFLAG_APPEND
91   #define TARGET_NATIVE_FILE_FILEFLAG_APPEND O_APPEND
92 #endif
93 #ifndef TARGET_NATIVE_FILE_FILEFLAG_SYNC
94   #if !defined (O_SYNC) && defined (O_FSYNC)
95     #define TARGET_NATIVE_FILE_FILEFLAG_SYNC O_FSYNC
96   #else
97     #define TARGET_NATIVE_FILE_FILEFLAG_SYNC O_SYNC
98   #endif
99 #endif
100 #ifndef TARGET_NATIVE_FILE_FILEFLAG_DSYNC
101   #ifdef O_DSYNC
102     #define TARGET_NATIVE_FILE_FILEFLAG_DSYNC 0
103   #else
104     #define TARGET_NATIVE_FILE_FILEFLAG_DSYNC TARGET_NATIVE_FILE_FILEFLAG_SYNC
105   #endif
106 #endif
107 #ifndef TARGET_NATIVE_FILE_FILEFLAG_BINARY
108   #define TARGET_NATIVE_FILE_FILEFLAG_BINARY O_BINARY
109 #endif
110 
111 #ifndef TARGET_NATIVE_FILE_FILEPERMISSION_NORMAL
112   #define TARGET_NATIVE_FILE_FILEPERMISSION_NORMAL (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
113 #endif
114 
115 #ifndef TARGET_NATIVE_FILE_FILEPERMISSION_PRIVATE
116   #define TARGET_NATIVE_FILE_FILEPERMISSION_PRIVATE (S_IRUSR | S_IWUSR)
117 #endif
118 
119 #ifndef TARGET_NATIVE_FILE_FILEPERMISSION_READONLY
120   #define TARGET_NATIVE_FILE_FILEPERMISSION_READONLY (~(S_IWRITE|S_IWGRP|S_IWOTH))
121 #endif
122 
123 /***************************** Datatypes *******************************/
124 
125 /***************************** Variables *******************************/
126 
127 /****************************** Macros *********************************/
128 
129 /***********************************************************************\
130 * Name       : TARGET_NATIVE_FILE_OPEN
131 * Purpose    : open a file
132 * Input      : -
133 * Output     : -
134 * Return     : -
135 * Side-effect: unknown
136 * Notes      : file is created if it does not exist
137 \***********************************************************************/
138 
139 #ifndef TARGET_NATIVE_FILE_OPEN
140   #include <sys/types.h>
141   #include <sys/stat.h>
142   #include <fcntl.h>
143   #define TARGET_NATIVE_FILE_OPEN(filename,filedescriptor,flags,permissions,result) \
144     do { \
145       filedescriptor=open(filename, \
146                           flags, \
147                           permissions \
148                           ); \
149       if (filedescriptor >= 0) \
150         fcntl (filedescriptor,F_SETFD,FD_CLOEXEC); \
151       result=(filedescriptor>=0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
152    } while (0)
153 #endif
154 
155 /***********************************************************************\
156 * Name       : TARGET_NATIVE_FILE_OPEN_CREATE
157 * Purpose    : create a file
158 * Input      : -
159 * Output     : -
160 * Return     : -
161 * Side-effect: unknown
162 * Notes      : file is created if it does not exist
163 \***********************************************************************/
164 
165 #ifndef TARGET_NATIVE_FILE_OPEN_CREATE
166   #define TARGET_NATIVE_FILE_OPEN_CREATE(filename,filedescriptor,result) \
167     TARGET_NATIVE_FILE_OPEN(filename,\
168                             filedescriptor,\
169                             TARGET_NATIVE_FILE_FILEFLAG_CREATE_FORCE, \
170                             TARGET_NATIVE_FILE_FILEPERMISSION_NORMAL, \
171                             result \
172                            )
173 #endif
174 
175 /***********************************************************************\
176 * Name       : TARGET_NATIVE_FILE_OPEN_READ
177 * Purpose    : open an existing file for reading
178 * Input      : -
179 * Output     : -
180 * Return     : -
181 * Side-effect: unknown
182 * Notes      : -
183 \***********************************************************************/
184 
185 #ifndef TARGET_NATIVE_FILE_OPEN_READ
186   #define TARGET_NATIVE_FILE_OPEN_READ(filename,filedescriptor,result) \
187     TARGET_NATIVE_FILE_OPEN(filename, \
188                             filedescriptor,\
189                             TARGET_NATIVE_FILE_FILEFLAG_READ, \
190                             TARGET_NATIVE_FILE_FILEPERMISSION_NORMAL, \
191                             result \
192                            )
193 #endif
194 
195 /***********************************************************************\
196 * Name       : TARGET_NATIVE_FILE_OPEN_WRITE
197 * Purpose    : open an existing file for writing
198 * Input      : -
199 * Output     : -
200 * Return     : -
201 * Side-effect: unknown
202 * Notes      : -
203 \***********************************************************************/
204 
205 #ifndef TARGET_NATIVE_FILE_OPEN_WRITE
206   #define TARGET_NATIVE_FILE_OPEN_WRITE(filename,filedescriptor,result) \
207     TARGET_NATIVE_FILE_OPEN(filename, \
208                             filedescriptor, \
209                             TARGET_NATIVE_FILE_FILEFLAG_WRITE, \
210                             TARGET_NATIVE_FILE_FILEPERMISSION_NORMAL, \
211                             result \
212                            )
213 #endif
214 
215 /***********************************************************************\
216 * Name       : TARGET_NATIVE_FILE_OPEN_READWRITE
217 * Purpose    : create/open a file for reading/writing
218 * Input      : -
219 * Output     : -
220 * Return     : -
221 * Side-effect: unknown
222 * Notes      : file is created if it does not exist
223 \***********************************************************************/
224 
225 #ifndef TARGET_NATIVE_FILE_OPEN_READWRITE
226   #define TARGET_NATIVE_FILE_OPEN_READWRITE(filename,filedescriptor,result) \
227     TARGET_NATIVE_FILE_OPEN(filename, \
228                             filedescriptor, \
229                             TARGET_NATIVE_FILE_FILEFLAG_READWRITE, \
230                             TARGET_NATIVE_FILE_FILEPERMISSION_NORMAL, \
231                             result \
232                            )
233 #endif
234 
235 /***********************************************************************\
236 * Name       : TARGET_NATIVE_FILE_OPEN_READWRITE
237 * Purpose    : create/open a file for append
238 * Input      : -
239 * Output     : -
240 * Return     : -
241 * Side-effect: unknown
242 * Notes      : file is created if it does not exist
243 \***********************************************************************/
244 
245 #ifndef TARGET_NATIVE_FILE_OPEN_APPEND
246   #define TARGET_NATIVE_FILE_OPEN_APPEND(filename,filedescriptor,result) \
247     TARGET_NATIVE_FILE_OPEN_APPEND(filename, \
248                                    filedescriptor, \
249                                    TARGET_NATIVE_FILE_FILEFLAG_CREATE_FORCE|TARGET_NATIVE_FILE_FILEFLAG_APPEND, \
250                                    TARGET_NATIVE_FILE_FILEPERMISSION_NORMAL, \
251                                    result \
252                                   )
253 #endif
254 
255 /***********************************************************************\
256 * Name       : TARGET_NATIVE_FILE_CLOSE
257 * Purpose    : close a file
258 * Input      : -
259 * Output     : -
260 * Return     : -
261 * Side-effect: unknown
262 * Notes      : -
263 \***********************************************************************/
264 
265 #ifndef TARGET_NATIVE_FILE_CLOSE
266   #include <unistd.h>
267   #define TARGET_NATIVE_FILE_CLOSE(filedescriptor,result) \
268     do  { \
269       result=(close(filedescriptor)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
270    } while (0)
271 #endif
272 
273 /***********************************************************************\
274 * Name       : TARGET_NATIVE_FILE_VALID_FILE_DESCRIPTOR
275 * Purpose    : check if file-descriptor is valid
276 * Input      : -
277 * Output     : -
278 * Return     : -
279 * Side-effect: unknown
280 * Notes      : -
281 \***********************************************************************/
282 
283 #ifndef TARGET_NATIVE_FILE_VALID_FILE_DESCRIPTOR
284   #if   defined(HAVE_FCNTL)
285     #include <unistd.h>
286     #include <fcntl.h>
287     #define TARGET_NATIVE_FILE_VALID_FILE_DESCRIPTOR(filedescriptor,result) \
288       do { \
289         result=(fcntl(filedescriptor,F_GETFL,0)!=-1)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
290       } while(0)
291   #elif defined(HAVE_FSTAT)
292     #include <sys/types.h>
293     #include <sys/stat.h>
294     #include <unistd.h>
295     #define TARGET_NATIVE_FILE_VALID_FILE_DESCRIPTOR(filedescriptor,result) \
296       do { \
297         struct stat __stat; \
298         \
299         result=(fstat(filedescriptor,&__stat)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
300       } while(0)
301   #else
302     #error fcntl() nor fstat() available for checking if file descriptor is valid
303   #endif
304 #endif
305 
306 /***********************************************************************\
307 * Name       : TARGET_NATIVE_FILE_TELL
308 * Purpose    : get current file position
309 * Input      : -
310 * Output     : -
311 * Return     : -
312 * Side-effect: unknown
313 * Notes      : -
314 \***********************************************************************/
315 
316 #ifndef TARGET_NATIVE_FILE_TELL
317   #include <sys/types.h>
318   #include <unistd.h>
319   #define TARGET_NATIVE_FILE_TELL(filedescriptor,offset,result) \
320     do { \
321       offset=lseek(filedescriptor,TARGET_NATIVE_MATH_INT_INT64_CONST_0,SEEK_CUR); \
322       result=((offset)!=(off_t)-1)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
323     } while (0)
324 #endif
325 
326 /***********************************************************************\
327 * Name       : TARGET_NATIVE_FILE_SEEK_BEGIN|CURRENT|END
328 * Purpose    : set file position relativ to begin/current/end
329 * Input      : -
330 * Output     : -
331 * Return     : -
332 * Side-effect: unknown
333 * Notes      : -
334 \***********************************************************************/
335 
336 #ifndef TARGET_NATIVE_FILE_SEEK_BEGIN
337   #include <sys/types.h>
338   #include <unistd.h>
339   #define TARGET_NATIVE_FILE_SEEK_BEGIN(filedescriptor,offset,newoffset,result) \
340     do { \
341       newoffset=lseek(filedescriptor,offset,SEEK_SET); \
342       result=((newoffset)!=(off_t)-1)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
343     } while (0)
344 #endif
345 #ifndef TARGET_NATIVE_FILE_SEEK_CURRENT
346   #include <sys/types.h>
347   #include <unistd.h>
348   #define TARGET_NATIVE_FILE_SEEK_CURRENT(filedescriptor,offset,newoffset,result) \
349     do { \
350       newoffset=lseek(filedescriptor,offset,SEEK_CUR); \
351       result=((newoffset)!=(off_t)-1)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
352     } while (0)
353 #endif
354 #ifndef TARGET_NATIVE_FILE_SEEK_END
355   #include <sys/types.h>
356   #include <unistd.h>
357   #define TARGET_NATIVE_FILE_SEEK_END(filedescriptor,offset,newoffset,result) \
358     do { \
359       newoffset=lseek(filedescriptor,offset,SEEK_END); \
360       result=((newoffset)!=(off_t)-1)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
361     } while (0)
362 #endif
363 
364 /***********************************************************************\
365 * Name       : TARGET_NATIVE_FILE_TRUNCATE
366 * Purpose    : truncate a file
367 * Input      : -
368 * Output     : -
369 * Return     : -
370 * Side-effect: unknown
371 * Notes      : -
372 \***********************************************************************/
373 
374 #ifndef TARGET_NATIVE_FILE_TRUNCATE
375   #include <unistd.h>
376   #define TARGET_NATIVE_FILE_TRUNCATE(filedescriptor,offset,result) \
377     do { \
378       result=(ftruncate(filedescriptor,offset)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
379     } while (0)
380 #endif
381 
382 /***********************************************************************\
383 * Name       : TARGET_NATIVE_FILE_SIZE
384 * Purpose    : get size of file (in bytes)
385 * Input      : -
386 * Output     : -
387 * Return     : -
388 * Side-effect: unknown
389 * Notes      : -
390 \***********************************************************************/
391 
392 #ifndef TARGET_NATIVE_FILE_SIZE
393   #include <sys/types.h>
394   #include <sys/stat.h>
395   #include <unistd.h>
396   #define TARGET_NATIVE_FILE_SIZE(filedescriptor,length,result) \
397     do { \
398       struct stat __statBuffer; \
399       \
400       result=(fstat(filedescriptor,&__statBuffer)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
401       length=TARGET_NATIVE_MATH_INT_INT32_TO_INT64(__statBuffer.st_size); \
402     } while (0)
403 #endif
404 
405 /***********************************************************************\
406 * Name       : TARGET_NATIVE_FILE_AVAILABLE
407 * Purpose    : get available bytes for read
408 * Input      : -
409 * Output     : -
410 * Return     : -
411 * Side-effect: unknown
412 * Notes      : -
413 \***********************************************************************/
414 
415 #ifndef TARGET_NATIVE_FILE_AVAILABLE
416   #ifdef HAVE_SYS_IOCTL_H
417     #define BSD_COMP /* Get FIONREAD on Solaris2 */
418     #include <sys/ioctl.h>
419   #endif
420   #ifdef HAVE_SYS_FILIO_H /* Get FIONREAD on Solaris 2.5 */
421     #include <sys/filio.h>
422   #endif
423   #if defined (FIONREAD)
424     #define TARGET_NATIVE_FILE_AVAILABLE(filedescriptor,length,result) \
425       do { \
426         ssize_t __n; \
427         \
428         result=(ioctl(filedescriptor,FIONREAD,(char*)&__n)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
429         length=TARGET_NATIVE_MATH_INT_INT32_TO_INT64(__n); \
430       } while (0)
431   #elif defined(HAVE_FSTAT)
432     #include <sys/types.h>
433     #include <sys/stat.h>
434     #include <unistd.h>
435     #define TARGET_NATIVE_FILE_AVAILABLE(filedescriptor,length,result) \
436       do { \
437         struct stat __statBuffer; \
438         off_t       __n; \
439         \
440         length=0; \
441         \
442         if ((fstat(filedescriptor,&__statBuffer)==0) && S_ISREG(__statBuffer.st_mode)) \
443         { \
444           __n=(lseek(filedescriptor,0,SEEK_CUR)); \
445           if (__n!=-1) \
446           { \
447             length=TARGET_NATIVE_MATH_INT_INT32_TO_INT64(__statBuffer.st_size-__n); \
448             result=TARGET_NATIVE_OK; \
449           } \
450           else \
451           { \
452             result=TARGET_NATIVE_ERROR; \
453           } \
454         } \
455         else \
456         { \
457           result=TARGET_NATIVE_ERROR; \
458         } \
459       } while (0)
460   #elif defined(HAVE_SELECT)
461     #include <string.h>
462     #include <sys/select.h>
463     #define TARGET_NATIVE_FILE_AVAILABLE(filedescriptor,length,result) \
464       do { \
465         fd_set         __filedescriptset; \
466         struct timeval __timeval; \
467         \
468         length=0; \
469         \
470         FD_ZERO(&__filedescriptset); \
471         FD_SET(filedescriptor,&__filedescriptset); \
472         memset(&__timeval,0,sizeof(__timeval)); \
473         switch (select(filedescriptor+1,&__filedescriptset,NULL,NULL,&__timeval)==0) \
474         { \
475           case -1: result=TARGET_NATIVE_ERROR; break; \
476           case  0: length=JNI_JLONG_CONST_0; result=TARGET_NATIVE_OK; break; \
477           default: length=JNI_JLONG_CONST_1; result=TARGET_NATIVE_OK; break; \
478         } \
479       } while (0)
480   #else
481     #define TARGET_NATIVE_FILE_AVAILABLE(filedescriptor,length,result) \
482       do { \
483         errno=TARGET_NATIVE_ERROR_OPERATION_NOT_PERMITTED; \
484         length=0; \
485         result=TARGET_NATIVE_ERROR; \
486       } while (0)
487   #endif
488 #endif
489 
490 /***********************************************************************\
491 * Name       : TARGET_NATIVE_FILE_READ|WRITE
492 * Purpose    : read/write from/to frile
493 * Input      : -
494 * Output     : -
495 * Return     : -
496 * Side-effect: unknown
497 * Notes      : -
498 \***********************************************************************/
499 
500 #ifndef TARGET_NATIVE_FILE_READ
501   #include <unistd.h>
502   #define TARGET_NATIVE_FILE_READ(filedescriptor,buffer,length,bytesRead,result) \
503     do { \
504       bytesRead=read(filedescriptor,buffer,length); \
505       result=(bytesRead!=-1)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
506     } while (0)
507 #endif
508 #ifndef TARGET_NATIVE_FILE_WRITE
509   #include <unistd.h>
510   #define TARGET_NATIVE_FILE_WRITE(filedescriptor,buffer,length,bytesWritten,result) \
511     do { \
512       bytesWritten=write(filedescriptor,buffer,length); \
513       result=(bytesWritten!=-1)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
514     } while (0)
515 #endif
516 
517 /***********************************************************************\
518 * Name       : TARGET_NATIVE_FILE_SET_MODE_READONLY
519 * Purpose    : set file mode to read-only
520 * Input      : -
521 * Output     : -
522 * Return     : -
523 * Side-effect: unknown
524 * Notes      : -
525 \***********************************************************************/
526 
527 #ifndef TARGET_NATIVE_FILE_SET_MODE_READONLY
528   #include <sys/types.h>
529   #include <sys/stat.h>
530   #include <unistd.h>
531   #define TARGET_NATIVE_FILE_SET_MODE_READONLY(filename,result) \
532     do { \
533       struct stat __statBuffer; \
534       \
535       if (stat(filename,&__statBuffer)==0) { \
536         result=(chmod(filename,__statBuffer.st_mode & TARGET_NATIVE_FILE_FILEPERMISSION_READONLY)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
537       } else { \
538         result=TARGET_NATIVE_ERROR; \
539       } \
540     } while (0)
541 #endif
542 
543 /***********************************************************************\
544 * Name       : TARGET_NATIVE_FILE_EXISTS
545 * Purpose    : check if file exists
546 * Input      : -
547 * Output     : -
548 * Return     : -
549 * Side-effect: unknown
550 * Notes      : -
551 \***********************************************************************/
552 
553 #ifndef TARGET_NATIVE_FILE_EXISTS
554   #include <sys/types.h>
555   #include <sys/stat.h>
556   #include <unistd.h>
557   #define TARGET_NATIVE_FILE_EXISTS(filename,result) \
558     do { \
559       struct stat __statBuffer; \
560       \
561       result=(stat(filename,&__statBuffer)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
562     } while (0)
563 #endif
564 
565 /***********************************************************************\
566 * Name       : TARGET_NATIVE_FILE_IS_FILE
567 * Purpose    : check if directory entry is a file
568 * Input      : -
569 * Output     : -
570 * Return     : -
571 * Side-effect: unknown
572 * Notes      : -
573 \***********************************************************************/
574 
575 #ifndef TARGET_NATIVE_FILE_IS_FILE
576   #include <sys/types.h>
577   #include <sys/stat.h>
578   #include <unistd.h>
579   #define TARGET_NATIVE_FILE_IS_FILE(filename,result) \
580     do { \
581       struct stat __statBuffer; \
582       \
583       result=((stat(filename,&__statBuffer)==0) && (S_ISREG(__statBuffer.st_mode)))?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
584     } while (0)
585 #endif
586 
587 /***********************************************************************\
588 * Name       : TARGET_NATIVE_FILE_IS_DIRECTORY
589 * Purpose    : check if directory entry is a directory
590 * Input      : -
591 * Output     : -
592 * Return     : -
593 * Side-effect: unknown
594 * Notes      : -
595 \***********************************************************************/
596 
597 #ifndef TARGET_NATIVE_FILE_IS_DIRECTORY
598   #include <sys/types.h>
599   #include <sys/stat.h>
600   #include <unistd.h>
601   #define TARGET_NATIVE_FILE_IS_DIRECTORY(filename,result) \
602     do { \
603       struct stat __statBuffer; \
604       \
605       result=((stat(filename,&__statBuffer)==0) && (S_ISDIR(__statBuffer.st_mode)))?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
606     } while (0)
607 #endif
608 
609 /***********************************************************************\
610 * Name       : TARGET_NATIVE_FILE_GET_LAST_MODIFIED
611 * Purpose    : get last modification time of file (milliseconds)
612 * Input      : -
613 * Output     : -
614 * Return     : -
615 * Side-effect: unknown
616 * Notes      : -
617 \***********************************************************************/
618 
619 #ifndef TARGET_NATIVE_FILE_GET_LAST_MODIFIED
620   #include <sys/types.h>
621   #include <sys/stat.h>
622   #include <unistd.h>
623   #define TARGET_NATIVE_FILE_GET_LAST_MODIFIED(filename,time,result) \
624     do { \
625       struct stat __statBuffer; \
626       \
627       time=TARGET_NATIVE_MATH_INT_INT64_CONST_0; \
628       if (stat(filename,&__statBuffer)==0) { \
629         time=TARGET_NATIVE_MATH_INT_INT64_MUL(TARGET_NATIVE_MATH_INT_INT32_TO_INT64(__statBuffer.st_mtime), \
630                                               TARGET_NATIVE_MATH_INT_INT32_TO_INT64(1000) \
631                                              ); \
632         result=TARGET_NATIVE_OK; \
633       } else { \
634         result=TARGET_NATIVE_ERROR; \
635       } \
636     } while (0)
637 #endif
638 
639 /***********************************************************************\
640 * Name       : TARGET_NATIVE_FILE_SET_LAST_MODIFIED
641 * Purpose    : set last modification time of file (milliseconds)
642 * Input      : -
643 * Output     : -
644 * Return     : -
645 * Side-effect: unknown
646 * Notes      : -
647 \***********************************************************************/
648 
649 #ifndef TARGET_NATIVE_FILE_SET_LAST_MODIFIED
650   #include <sys/types.h>
651   #include <sys/stat.h>
652   #include <unistd.h>
653   #ifdef HAVE_UTIME_H
654     #include <utime.h>
655   #elif HAVE_SYS_UTIME_H
656     #include <sys/utime.h>
657   #else
658     #error utime.h not found. Please check configuration.
659   #endif
660   #define TARGET_NATIVE_FILE_SET_LAST_MODIFIED(filename,time,result) \
661     do { \
662       struct stat    __statBuffer; \
663       struct utimbuf __utimeBuffer; \
664       \
665       if (stat(filename,&__statBuffer)==0) { \
666         __utimeBuffer.actime =__statBuffer.st_atime; \
667         __utimeBuffer.modtime=TARGET_NATIVE_MATH_INT_INT64_TO_INT32(TARGET_NATIVE_MATH_INT_INT64_DIV(time, \
668                                                                                                      TARGET_NATIVE_MATH_INT_INT32_TO_INT64(1000) \
669                                                                                                     ) \
670                                                               ); \
671         result=(utime(filename,&__utimeBuffer)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
672       } else { \
673         result=TARGET_NATIVE_ERROR; \
674       } \
675     } while (0)
676 #endif
677 
678 /***********************************************************************\
679 * Name       : TARGET_NATIVE_FILE_DELETE
680 * Purpose    : delete a file,link or directory
681 * Input      : -
682 * Output     : -
683 * Return     : -
684 * Side-effect: unknown
685 * Notes      : -
686 \***********************************************************************/
687 
688 #ifndef TARGET_NATIVE_FILE_DELETE
689   #define TARGET_NATIVE_FILE_DELETE(filename,result) \
690     do { \
691       result=((unlink(filename)==0) || (rmdir(filename)==0))?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
692     } while (0)
693 #endif
694 
695 /***********************************************************************\
696 * Name       : TARGET_NATIVE_FILE_RENAME
697 * Purpose    : delete a file, link or directory
698 * Input      : -
699 * Output     : -
700 * Return     : -
701 * Side-effect: unknown
702 * Notes      : -
703 \***********************************************************************/
704 
705 #ifndef TARGET_NATIVE_FILE_RENAME
706   #define TARGET_NATIVE_FILE_RENAME(oldfilename,newfilename,result) \
707     do { \
708       result=(rename(oldfilename,newfilename)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
709     } while (0)
710 #endif
711 
712 /***********************************************************************\
713 * Name       : TARGET_NATIVE_FILE_MAKE_DIR
714 * Purpose    : create new directory
715 * Input      : name - directory name
716 * Output     : result - 1 if successful, 0 otherwise
717 * Return     : -
718 * Side-effect: unknown
719 * Notes      : -
720 \***********************************************************************/
721 
722 #ifndef TARGET_NATIVE_FILE_MAKE_DIR
723   #include <sys/stat.h>
724   #define TARGET_NATIVE_FILE_MAKE_DIR(name,result) \
725     do { \
726       result=((mkdir(name,(S_IRWXO|S_IRWXG|S_IRWXU))==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR); \
727     } while (0)
728 #endif
729 
730 /***********************************************************************\
731 * Name       : TARGET_NATIVE_FILE_GET_CWD
732 * Purpose    : get current working directory
733 * Input      : -
734 * Output     : -
735 * Return     : -
736 * Side-effect: unknown
737 * Notes      : -
738 \***********************************************************************/
739 
740 #ifndef TARGET_NATIVE_FILE_GET_CWD
741   #include <unistd.h>
742   #define TARGET_NATIVE_FILE_GET_CWD(path,maxPathLength,result) \
743     do {\
744       result=(getcwd(path,maxPathLength)!=NULL)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
745     } while (0)
746 #endif
747 
748 /***********************************************************************\
749 * Name       : TARGET_NATIVE_FILE_OPEN_DIR
750 * Purpose    : open directory for reading entries.
751 * Input      : -
752 * Output     : handle - handle if not error, NULL otherwise
753 * Return     : -
754 * Side-effect: unknown
755 * Notes      : -
756 \***********************************************************************/
757 
758 #ifndef TARGET_NATIVE_FILE_OPEN_DIR
759   #include <sys/types.h>
760   #include <dirent.h>
761   #define TARGET_NATIVE_FILE_OPEN_DIR(filename,handle,result) \
762     do { \
763       handle=(void*)opendir(filename); \
764       result=(handle!=NULL)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
765     } while(0)
766 #endif
767 
768 /***********************************************************************\
769 * Name       : TARGET_NATIVE_FILE_CLOSE_DIR
770 * Purpose    : close directory
771 * Input      : -
772 * Output     : -
773 * Return     : -
774 * Side-effect: unknown
775 * Notes      : -
776 \***********************************************************************/
777 
778 #ifndef TARGET_NATIVE_FILE_CLOSE_DIR
779   #include <sys/types.h>
780   #include <dirent.h>
781   #define TARGET_NATIVE_FILE_CLOSE_DIR(handle,result) \
782     do { \
783       closedir((DIR*)handle); \
784       result=TARGET_NATIVE_OK; \
785     }  while(0)
786 #endif
787 
788 /***********************************************************************\
789 * Name       : TARGET_NATIVE_FILE_READ_DIR
790 * Purpose    : read directory entry
791 * Input      : -
792 * Output     : -
793 * Return     : -
794 * Side-effect: unknown
795 * Notes      : -
796 \***********************************************************************/
797 
798 /* XXX ??? name als buffer? */
799 #ifndef TARGET_NATIVE_FILE_READ_DIR
800   #include <sys/types.h>
801   #include <dirent.h>
802   #define TARGET_NATIVE_FILE_READ_DIR(handle,name,result) \
803     do { \
804       struct dirent *__direntBuffer; \
805       \
806       name=NULL; \
807       \
808       __direntBuffer=readdir((DIR*)handle); \
809       if (__direntBuffer!=NULL) { \
810         name=__direntBuffer->d_name; \
811         result=TARGET_NATIVE_OK; \
812       } else { \
813         result=TARGET_NATIVE_ERROR; \
814       } \
815     } while (0)
816 #endif
817 
818 /***********************************************************************\
819 * Name       : TARGET_NATIVE_FILE_FSYNC
820 * Purpose    : do filesystem sync
821 * Input      : -
822 * Output     : -
823 * Return     : -
824 * Side-effect: unknown
825 * Notes      : -
826 \***********************************************************************/
827 
828 #ifndef TARGET_NATIVE_FILE_FSYNC
829   #define TARGET_NATIVE_FILE_FSYNC(filedescriptor,result) \
830     do { \
831       result=(fsync(filedescriptor)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
832     } while(0)
833 #endif
834 
835 /***************************** Functions *******************************/
836 
837 #ifdef __cplusplus
838 }
839 #endif
840 
841 #endif /* __TARGET_GENERIC_FILE__ */
842 
843 /* end of file */
844 
845