SIONlib  1.7.7
Scalable I/O library for parallel access to task-local files
sion_file.c
Go to the documentation of this file.
1 /****************************************************************************
2 ** SIONLIB http://www.fz-juelich.de/jsc/sionlib **
3 *****************************************************************************
4 ** Copyright (c) 2008-2019 **
5 ** Forschungszentrum Juelich, Juelich Supercomputing Centre **
6 ** **
7 ** Copyright (c) 2019 **
8 ** DataDirect Networks **
9 ** **
10 ** See the file COPYRIGHT in the package base directory for details **
11 ****************************************************************************/
12 
17 #define _XOPEN_SOURCE 700
18 
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <time.h>
23 #include <assert.h>
24 
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <errno.h>
30 
31 #if defined(_SION_SIONFWD)
32 #include <sionfwd/client.h>
33 #endif
34 
35 #include "sion.h"
36 #include "sion_debug.h"
37 #include "sion_error_handler.h"
38 #include "sion_internal.h"
39 #include "sion_file.h"
40 
41 #ifdef _SION_IME_NATIVE
42 #include "ime_native.h"
43 #endif
44 
53 _sion_fileptr *_sion_file_open(const char *fname, unsigned int flags, unsigned int addflags) {
54 
55  if(flags & SION_FILE_FLAG_ANSI) {
57  sion_fileptr->flags |= SION_FILE_FLAG_ANSI;
58 
59  if(flags & SION_FILE_FLAG_WRITE) {
60  sion_fileptr->flags |= SION_FILE_FLAG_WRITE;
61 
62  if(flags & SION_FILE_FLAG_CREATE) {
63  sion_fileptr->flags |= SION_FILE_FLAG_CREATE;
64  sion_fileptr->fileptr=_sion_file_open_ansi_write_create(fname,addflags);
65  } else {
66  sion_fileptr->fileptr=_sion_file_open_ansi_write_existing(fname,addflags);
67  }
68  } else {
69  sion_fileptr->flags |= SION_FILE_FLAG_READ;
70  sion_fileptr->fileptr=_sion_file_open_ansi_read(fname,addflags);
71  }
72 
73  /* could not open ANSI file */
74  if(!sion_fileptr->fileptr) {
75  free(sion_fileptr);
76  _sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_WARN,"error: could not open file (ANSI), %s %d %d, aborting ...\n", fname, flags, addflags);
77  return(NULL);
78  }
79 
80  return(sion_fileptr);
81  } else if (flags & SION_FILE_FLAG_POSIX) {
83  sion_fileptr->flags |= SION_FILE_FLAG_POSIX;
84 
85  if(flags & SION_FILE_FLAG_WRITE) {
86  sion_fileptr->flags |= SION_FILE_FLAG_WRITE;
87  if(flags & SION_FILE_FLAG_CREATE) {
88  sion_fileptr->flags |= SION_FILE_FLAG_CREATE;
89  sion_fileptr->fd=_sion_file_open_posix_write_create(fname,addflags);
90  } else {
91  sion_fileptr->fd=_sion_file_open_posix_write_existing(fname,addflags);
92  }
93  } else {
94  sion_fileptr->flags |= SION_FILE_FLAG_READ;
95  sion_fileptr->fd=_sion_file_open_posix_read(fname,addflags);
96  }
97 
98  /* could not open POSIX file */
99  if(sion_fileptr->fd<0) {
100  free(sion_fileptr);
101  _sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_WARN,"error: could not open file (POSIX), %s %d %d, aborting ...\n", fname, flags, addflags);
102  return(NULL);
103  }
104 
105  return(sion_fileptr);
106  }
107 #if defined(_SION_SIONFWD)
108  else if (flags & SION_FILE_FLAG_SIONFWD) {
110  sion_fileptr->flags |= SION_FILE_FLAG_SIONFWD;
111 
112  if(flags & SION_FILE_FLAG_WRITE) {
113  sion_fileptr->flags |= SION_FILE_FLAG_WRITE;
114  if(flags & SION_FILE_FLAG_CREATE) {
115  sion_fileptr->flags |= SION_FILE_FLAG_CREATE;
116  sion_fileptr->fd=_sion_file_open_sionfwd_write_create(fname,addflags);
117  } else {
118  sion_fileptr->fd=_sion_file_open_sionfwd_write_existing(fname,addflags);
119  }
120  } else {
121  sion_fileptr->flags |= SION_FILE_FLAG_READ;
122  sion_fileptr->fd=_sion_file_open_sionfwd_read(fname,addflags);
123  }
124 
125  /* could not open SIONFWD file */
126  if(sion_fileptr->fd == -1) {
127  free(sion_fileptr);
128  _sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_WARN,"error: could not open file (SIONFWD), %s %d %d, aborting ...\n", fname, flags, addflags);
129  return(NULL);
130  }
131 
132  return(sion_fileptr);
133  }
134 #endif
135 #ifdef _SION_IME_NATIVE
136  else if (flags & SION_FILE_FLAG_IME_NATIVE) {
138  sion_fileptr->flags |= SION_FILE_FLAG_IME_NATIVE;
139 
140  if (flags & SION_FILE_FLAG_WRITE) {
141  sion_fileptr->flags |= SION_FILE_FLAG_WRITE;
142  if (flags & SION_FILE_FLAG_CREATE) {
143  sion_fileptr->flags |= SION_FILE_FLAG_CREATE;
144  sion_fileptr->fd = _sion_file_open_ime_native_write_create(fname, addflags);
145  } else {
146  sion_fileptr->fd = _sion_file_open_ime_native_write_existing(fname, addflags);
147  }
148  } else {
149  sion_fileptr->flags |= SION_FILE_FLAG_READ;
150  sion_fileptr->fd = _sion_file_open_ime_native_read(fname, addflags);
151  }
152 
153  /* could not open IME native file */
154  if (sion_fileptr->fd < 0) {
155  free(sion_fileptr);
156  _sion_errorprint(SION_NOT_SUCCESS, _SION_ERROR_WARN, "error: could not open file (IME native), %s %d %d, aborting ...\n", fname,
157  flags, addflags);
158  return NULL;
159  }
160 
161  return sion_fileptr;
162  }
163 #endif
164  else {
165  /* unknown mode? */
166  _sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_WARN,"internal error: unknown open type, %s %d %d, aborting ...\n", fname, flags, addflags);
167  return(NULL);
168  }
169 }
170 
171 #define DFUNCTION "_sion_file_close"
178 int _sion_file_close( _sion_fileptr *sion_fileptr ) {
179  if (NULL == sion_fileptr) return SION_NOT_SUCCESS;
180 
181  int rc;
182  if(sion_fileptr->flags & SION_FILE_FLAG_ANSI) {
183  if(sion_fileptr->flags & SION_FILE_FLAG_SCNDANSI) {
184  _sion_file_close_ansi(sion_fileptr->second_fileptr);
185  sion_fileptr->second_fileptr=NULL;
186  }
187  rc=_sion_file_close_ansi(sion_fileptr->fileptr);
188  sion_fileptr->fileptr=NULL;
189  } else if(sion_fileptr->flags & SION_FILE_FLAG_POSIX) {
190  rc=_sion_file_close_posix(sion_fileptr->fd);
191  }
192 #if defined(_SION_SIONFWD)
193  else if(sion_fileptr->flags & SION_FILE_FLAG_SIONFWD) {
194  rc=_sion_file_close_sionfwd(sion_fileptr->fd);
195  }
196 #endif
197 #ifdef _SION_IME_NATIVE
198  else if (sion_fileptr->flags & SION_FILE_FLAG_IME_NATIVE) {
199  rc = _sion_file_close_ime_native(sion_fileptr->fd);
200  }
201 #endif
202  else {
203  rc = SION_NOT_SUCCESS;
204  }
205 
206  DPRINTFP((32, DFUNCTION, -1, "free now fileptr=%x %d rc=%d\n",sion_fileptr,sion_fileptr->flags,rc));
207  free(sion_fileptr);
208 
209  return (rc);
210 }
211 #undef DFUNCTION
212 
221 sion_int64 _sion_file_write(const void *data, sion_int64 bytes, _sion_fileptr *sion_fileptr ) {
222  if (NULL == sion_fileptr) return SION_SIZE_NOT_VALID;
223 
224  if(sion_fileptr->flags & SION_FILE_FLAG_ANSI) {
225  if(sion_fileptr->flags & SION_FILE_FLAG_SCNDANSI)
226  return _sion_file_write_ansi(data,bytes,sion_fileptr->second_fileptr);
227  else
228  return _sion_file_write_ansi(data,bytes,sion_fileptr->fileptr);
229  } else if(sion_fileptr->flags & SION_FILE_FLAG_POSIX) {
230  return _sion_file_write_posix(data,bytes,sion_fileptr->fd);
231  }
232 #if defined(_SION_SIONFWD)
233  else if(sion_fileptr->flags & SION_FILE_FLAG_SIONFWD) {
234  return _sion_file_write_sionfwd(data,bytes,sion_fileptr->fd,&sion_fileptr->position);
235  }
236 #endif
237 #ifdef _SION_IME_NATIVE
238  else if (sion_fileptr->flags & SION_FILE_FLAG_IME_NATIVE) {
239  return _sion_file_write_ime_native(data, bytes, sion_fileptr->fd);
240  }
241 #endif
242  else {
243  return _sion_errorprint(SION_SIZE_NOT_VALID,_SION_ERROR_RETURN,"_sion_file_write: cannot find valid file flag (flags=%d)\n",sion_fileptr->flags);
244  }
245 }
246 
257 sion_int64 _sion_file_read(void *data, sion_int64 bytes, _sion_fileptr *sion_fileptr ) {
258  if (NULL == sion_fileptr) return SION_SIZE_NOT_VALID;
259 
260  if(sion_fileptr->flags & SION_FILE_FLAG_ANSI) {
261  if(sion_fileptr->flags & SION_FILE_FLAG_SCNDANSI)
262  return _sion_file_read_ansi(data,bytes,sion_fileptr->second_fileptr);
263  else
264  return _sion_file_read_ansi(data,bytes,sion_fileptr->fileptr);
265  } else if(sion_fileptr->flags & SION_FILE_FLAG_POSIX) {
266  return _sion_file_read_posix(data,bytes,sion_fileptr->fd);
267  }
268 #if defined(_SION_SIONFWD)
269  else if(sion_fileptr->flags & SION_FILE_FLAG_SIONFWD) {
270  return _sion_file_read_sionfwd(data,bytes,sion_fileptr->fd,&sion_fileptr->position);
271  }
272 #endif
273 #ifdef _SION_IME_NATIVE
274  else if (sion_fileptr->flags & SION_FILE_FLAG_IME_NATIVE) {
275  return _sion_file_read_ime_native(data, bytes, sion_fileptr->fd);
276  }
277 #endif
278  else {
279  return _sion_errorprint(SION_SIZE_NOT_VALID,_SION_ERROR_RETURN,"_sion_file_read: cannot find valid file flag (flags=%d)\n",sion_fileptr->flags);
280  }
281 }
282 
290  if (NULL == sion_fileptr) return SION_SIZE_NOT_VALID;
291 
292  if(sion_fileptr->flags & SION_FILE_FLAG_ANSI) {
293  return _sion_file_get_opt_blksize_ansi(sion_fileptr->fileptr);
294  } else if(sion_fileptr->flags & SION_FILE_FLAG_POSIX) {
295  return _sion_file_get_opt_blksize_posix(sion_fileptr->fd);
296  }
297 #if defined(_SION_SIONFWD)
298  else if(sion_fileptr->flags & SION_FILE_FLAG_SIONFWD) {
299  return _sion_file_get_opt_blksize_sionfwd(sion_fileptr->fd);
300  }
301 #endif
302 #ifdef _SION_IME_NATIVE
303  else if (sion_fileptr->flags & SION_FILE_FLAG_IME_NATIVE) {
304  return _sion_file_get_opt_blksize_ime_native(sion_fileptr->fd);
305  }
306 #endif
307  else {
308  return _sion_errorprint(SION_SIZE_NOT_VALID,_SION_ERROR_RETURN,"_sion_file_get_opt_blksize: cannot find valid file flag (flags=%d)\n",sion_fileptr->flags);
309  }
310 }
311 
318 int _sion_file_stat_file(const char *fname) {
320 }
321 
329 int _sion_file_stat_file2(const char *fname, unsigned int apiflag) {
330  // _sion_file_stat_file above is incomplete, because it does not
331  // allow selecting an appropriate low-level API like all other functions
332  // in this file.
333  // Unfortunately its signature is declared in the public sion_common.h
334  // and thus cannot be changed without breaking backward compatibility.
335  // Thus, this new function is introduced and used internally in its place.
336  if ((apiflag & SION_FILE_FLAG_ANSI) || (apiflag & SION_FILE_FLAG_POSIX)) {
337 #if defined(_SION_LINUX) || defined(_SION_DARWIN) || defined(_SION_AIX) || defined(_SION_BGP) || defined(_SION_BGQ)
338  struct stat sbuf;
339  return 0 == stat(fname, &sbuf);
340 #else
341 #error "No platform selected."
342 #endif
343  }
344 #if defined(_SION_SIONFWD)
345  else if(apiflag & SION_FILE_FLAG_SIONFWD) {
346  return sionfwd_stat_file(fname);
347  }
348 #endif
349 #ifdef _SION_IME_NATIVE
350  else if (apiflag & SION_FILE_FLAG_IME_NATIVE) {
351  struct stat sbuf;
352  return 0 == ime_native_stat(fname, &sbuf);
353  }
354 #endif
355  else {
356  return _sion_errorprint(0,_SION_ERROR_RETURN,"_sion_file_stat_file2: cannot find valid api flag (flags=%d)\n",apiflag);
357  }
358 }
359 
367 sion_int64 _sion_file_set_position( _sion_fileptr *sion_fileptr, sion_int64 startpointer ) {
368  if (NULL == sion_fileptr) return SION_SIZE_NOT_VALID;
369 
370  if(sion_fileptr->flags & SION_FILE_FLAG_ANSI) {
371  if(sion_fileptr->flags & SION_FILE_FLAG_SCNDANSI) {
372  return _sion_file_set_position_ansi(sion_fileptr->second_fileptr, startpointer);
373  } else {
374  return _sion_file_set_position_ansi(sion_fileptr->fileptr, startpointer);
375  }
376  } else if(sion_fileptr->flags & SION_FILE_FLAG_POSIX) {
377  return _sion_file_set_position_posix(sion_fileptr->fd, startpointer);
378  }
379 #if defined(_SION_SIONFWD)
380  else if(sion_fileptr->flags & SION_FILE_FLAG_SIONFWD) {
381  sion_fileptr->position = startpointer;
382  return sion_fileptr->position;
383  }
384 #endif
385 #ifdef _SION_IME_NATIVE
386  else if (sion_fileptr->flags & SION_FILE_FLAG_IME_NATIVE) {
387  return _sion_file_set_position_ime_native(sion_fileptr->fd, startpointer);
388  }
389 #endif
390  else {
391  return _sion_errorprint(SION_SIZE_NOT_VALID,_SION_ERROR_RETURN,"_sion_file_set_position: cannot find valid file flag (flags=%d)\n",sion_fileptr->flags);
392  }
393 }
394 
401 sion_int64 _sion_file_get_position( _sion_fileptr *sion_fileptr ) {
402  if (NULL == sion_fileptr) return SION_SIZE_NOT_VALID;
403 
404  if(sion_fileptr->flags & SION_FILE_FLAG_ANSI) {
405  if(sion_fileptr->flags & SION_FILE_FLAG_SCNDANSI) {
406  return _sion_file_get_position_ansi(sion_fileptr->second_fileptr);
407  } else {
408  return _sion_file_get_position_ansi(sion_fileptr->fileptr);
409  }
410  } else if(sion_fileptr->flags & SION_FILE_FLAG_POSIX) {
411  return _sion_file_get_position_posix(sion_fileptr->fd);
412  }
413 #if defined(_SION_SIONFWD)
414  else if(sion_fileptr->flags & SION_FILE_FLAG_SIONFWD) {
415  return sion_fileptr->position;
416  }
417 #endif
418 #ifdef _SION_IME_NATIVE
419  else if (sion_fileptr->flags & SION_FILE_FLAG_IME_NATIVE) {
420  return _sion_file_get_position_ime_native(sion_fileptr->fd);
421  }
422 #endif
423  else {
424  return _sion_errorprint(SION_SIZE_NOT_VALID,_SION_ERROR_RETURN,"_sion_file_get_position: cannot find valid file flag (flags=%d)\n",sion_fileptr->flags);
425  }
426 }
427 
434 int _sion_file_flush( _sion_fileptr *sion_fileptr ) {
435  if (NULL == sion_fileptr) return SION_NOT_SUCCESS;
436 
437  if(sion_fileptr->flags & SION_FILE_FLAG_ANSI) {
438  if(sion_fileptr->flags & SION_FILE_FLAG_SCNDANSI) {
439  return _sion_file_flush_ansi(sion_fileptr->second_fileptr);
440  } else {
441  return _sion_file_flush_ansi(sion_fileptr->fileptr);
442  }
443  } else if(sion_fileptr->flags & SION_FILE_FLAG_POSIX) {
444  return _sion_file_flush_posix(sion_fileptr->fd);
445  }
446 #if defined(_SION_SIONFWD)
447  else if(sion_fileptr->flags & SION_FILE_FLAG_SIONFWD) {
448  return _sion_file_flush_sionfwd(sion_fileptr->fd);
449  }
450 #endif
451 #ifdef _SION_IME_NATIVE
452  else if (sion_fileptr->flags & SION_FILE_FLAG_IME_NATIVE) {
453  return _sion_file_flush_ime_native(sion_fileptr->fd);
454  }
455 #endif
456  else {
457  return _sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"_sion_file_flush: cannot find valid file flag (flags=%d)\n",sion_fileptr->flags);
458  }
459 }
460 
467 int _sion_file_purge( _sion_fileptr *sion_fileptr ) {
468  if( sion_fileptr == NULL ) return SION_NOT_SUCCESS;
469 
470  if(sion_fileptr->flags & SION_FILE_FLAG_ANSI) {
471  if(sion_fileptr->flags & SION_FILE_FLAG_SCNDANSI) {
472  return _sion_file_purge_ansi(sion_fileptr->second_fileptr);
473  } else {
474  return _sion_file_purge_ansi(sion_fileptr->fileptr);
475  }
476  } else if(sion_fileptr->flags & SION_FILE_FLAG_POSIX) {
477  return _sion_file_purge_posix(sion_fileptr->fd);
478 #if defined(_SION_SIONFWD)
479  } else if(sion_fileptr->flags & SION_FILE_FLAG_SIONFWD) {
480  return _sion_file_purge_sionfwd(sion_fileptr->fd);
481 #endif
482  } else {
483  return _sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"_sion_file_purge: cannot find valid file flag (flags=%d)\n",sion_fileptr->flags);
484  }
485 }
486 
495 int _sion_file_set_buffer(_sion_fileptr *sion_fileptr, char *buffer, sion_int32 buffer_size) {
496  if( sion_fileptr == NULL ) return SION_NOT_SUCCESS;
497 
498  if(sion_fileptr->flags & SION_FILE_FLAG_ANSI) {
499  if(sion_fileptr->flags & SION_FILE_FLAG_SCNDANSI) {
500  return _sion_file_set_buffer_ansi(sion_fileptr->second_fileptr, buffer, buffer_size);
501  } else {
502  return _sion_file_set_buffer_ansi(sion_fileptr->fileptr, buffer, buffer_size);
503  }
504  } else if(sion_fileptr->flags & SION_FILE_FLAG_POSIX) {
505  return _sion_file_set_buffer_posix(sion_fileptr->fd, buffer, buffer_size);
506  }
507 #if defined(_SION_SIONFWD)
508  else if(sion_fileptr->flags & SION_FILE_FLAG_SIONFWD) {
509  return _sion_file_set_buffer_sionfwd(sion_fileptr->fd, buffer, buffer_size);
510  }
511 #endif
512 #ifdef _SION_IME_NATIVE
513  else if (sion_fileptr->flags & SION_FILE_FLAG_IME_NATIVE) {
514  return _sion_file_set_buffer_ime_native(sion_fileptr->fd, buffer, buffer_size);
515  }
516 #endif
517  else {
518  return _sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"_sion_file_set_buffer: cannot find valid file flag (flags=%d)\n",sion_fileptr->flags);
519  }
520 }
521 
529 int _sion_file_set_second_fileptr( _sion_fileptr *sion_fileptr, FILE* fileptr) {
530  if( sion_fileptr == NULL ) return SION_NOT_SUCCESS;
531 
532  if(sion_fileptr->flags & SION_FILE_FLAG_ANSI) {
533  if(sion_fileptr->flags & SION_FILE_FLAG_SCNDANSI) {
534  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"internal error: second fileptr already set, aborting ...\n"));
535  } else {
536  sion_fileptr->flags |= SION_FILE_FLAG_SCNDANSI;
537  sion_fileptr->second_fileptr=fileptr;
538  return SION_SUCCESS;
539  }
540  } else {
541  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"internal error: second fileptr could not be set for non-ANSI file, aborting ...\n"));
542  }
543 }
544 
552  if( sion_fileptr == NULL ) return SION_NOT_SUCCESS;
553 
554  if(sion_fileptr->flags & SION_FILE_FLAG_ANSI) {
555  if(sion_fileptr->flags & SION_FILE_FLAG_SCNDANSI) {
556  sion_fileptr->flags &= ~SION_FILE_FLAG_SCNDANSI;
557  sion_fileptr->second_fileptr=NULL;
558  return SION_SUCCESS;
559  } else {
560  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"internal error: second fileptr was not set, aborting ...\n"));
561  }
562  } else {
563  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"internal error: second fileptr could not be unset for non-ANSI file, aborting ...\n"));
564  }
565 }
566 
573 int _sion_file_get_fd( _sion_fileptr *sion_fileptr) {
574  if( sion_fileptr == NULL ) return SION_ID_UNDEF;
575 
576  if(sion_fileptr->flags & SION_FILE_FLAG_POSIX) {
577  return sion_fileptr->fd;
578  } else if(sion_fileptr->flags & SION_FILE_FLAG_ANSI) {
579  if(sion_fileptr->flags & SION_FILE_FLAG_SCNDANSI) {
580  return fileno(sion_fileptr->second_fileptr);
581  } else {
582  return fileno(sion_fileptr->fileptr);
583  }
584  }
585 #if defined(_SION_SIONFWD)
586  else if(sion_fileptr->flags & SION_FILE_FLAG_SIONFWD) {
587  return SION_ID_UNDEF;
588  }
589 #endif
590 #ifdef _SION_IME_NATIVE
591  else if (sion_fileptr->flags & SION_FILE_FLAG_IME_NATIVE) {
592  return sion_fileptr->fd;
593  }
594 #endif
595  else {
596  return _sion_errorprint(SION_ID_UNDEF,_SION_ERROR_RETURN,"_sion_file_get_fd: cannot find valid file flag (flags=%d)\n",sion_fileptr->flags);
597  }
598 }
599 
600 /* ********************************************************************************************** */
601 /* *** INTERFACE Functions */
602 /* ********************************************************************************************** */
603 
611 FILE *_sion_file_open_ansi_write_create(const char *fname, unsigned int addflags) {
612 #if defined(_SION_LINUX) || defined(_SION_DARWIN)
613  return fopen(fname, "w");
614 #elif defined(_SION_AIX) || defined(_SION_BGP) || defined(_SION_BGQ)
615  int fd = open64(fname, O_CREAT | O_RDWR, 0664);
616  return fdopen(fd, "w");
617 #else
618 #error "No platform selected."
619  return NULL;
620 #endif
621 }
622 
630 FILE *_sion_file_open_ansi_write_existing(const char *fname, unsigned int addflags) {
631 #if defined(_SION_LINUX) || defined(_SION_DARWIN)
632  /* changed from w to r+ to prevent time truncation of empty existing file */
633  return fopen(fname, "r+");
634 #elif defined(_SION_AIX)
635  int fd = open64(fname, O_CREAT | O_RDWR, 0664);
636  return fdopen(fd, "r+");
637 #elif defined(_SION_BGP) || defined(_SION_BGQ)
638  int fd = open64(fname, O_RDWR, 0664);
639  return fdopen(fd, "r+");
640 #else
641 #error "No platform selected."
642  return NULL;
643 #endif
644 }
645 
653 FILE *_sion_file_open_ansi_read(const char *fname, unsigned int addflags) {
654 #if defined(_SION_LINUX) || defined(_SION_DARWIN)
655  return fopen(fname, "r");
656 #elif defined(_SION_AIX) || defined(_SION_BGP) || defined(_SION_BGQ)
657  int fd = open64(fname, O_RDONLY, 0664);
658  return fdopen(fd, "r");
659 #else
660 #error "No platform selected."
661  return NULL;
662 #endif
663 }
664 
671 int _sion_file_close_ansi(FILE *fileptr) {
672 #if defined(_SION_LINUX) || defined(_SION_DARWIN) || defined(_SION_AIX) || defined(_SION_BGP) || defined(_SION_BGQ)
673  return (0 == fclose(fileptr)) ? SION_SUCCESS : SION_NOT_SUCCESS;
674 #else
675 #error "No platform selected."
676  return SION_NOT_SUCCESS;
677 #endif
678 }
679 
686 long _sion_file_get_opt_blksize_ansi( FILE *fileptr ) {
687 #if defined(_SION_LINUX) || defined(_SION_DARWIN) || defined(_SION_AIX) || defined(_SION_BGP) || defined(_SION_BGQ)
688  int fd = fileno(fileptr);
689  struct stat sbuf;
690  if (fstat(fd, &sbuf) == 0) {
691  return sbuf.st_blksize;
692  } else {
693  return SION_SIZE_NOT_VALID;
694  }
695 #else
696 #error "No platform selected."
697  return SION_SIZE_NOT_VALID;
698 #endif
699 }
700 
708 sion_int64 _sion_file_set_position_ansi(FILE *fileptr, sion_int64 startpointer) {
709  sion_int64 newpos;
710 
711  DPRINTFP((32, "_sion_set_position_ansi", -1, "enter (to %lld) (to %ld)\n", (long long) startpointer, (long) startpointer));
712 
713 #if defined(_SION_LINUX) || defined(_SION_BGP) || defined(_SION_BGQ)
714  off_t offset = (off_t) startpointer;
715  if (offset != startpointer) {
716  DPRINTFP((32, "_sion_set_position_ansi", -1, "_sion_set_position_ansi: cannot set position to %ld (%zu),%lld (%zu) offset conversion error\n",
717  offset, sizeof(offset), startpointer, sizeof(startpointer)));
718  return(_sion_errorprint(SION_SIZE_NOT_VALID,_SION_ERROR_RETURN,"_sion_set_position_ansi: cannot set position to %ld (%zu),%lld (%zu) offset conversion error\n",
719  offset, sizeof(offset), startpointer, sizeof(startpointer)));
720  }
721  int fd = fileno(fileptr);
722  off_t result;
723  newpos = result = lseek(fd, offset, SEEK_SET);
724 #if defined(_SION_LINUX)
725  DPRINTFP((1024, "_sion_set_position_ansi", -1, "set position=%lld (LINUX)\n", (long long) result));
726 #endif
727 #elif defined(_SION_DARWIN)
728  long offset = (long) startpointer;
729  if (offset != startpointer) {
730  DPRINTFP((32, "_sion_set_position_ansi", -1, "_sion_set_position_ansi: cannot set position to %ld (%zu),%lld (%zu) offset conversion error\n",
731  offset, sizeof(offset), startpointer, sizeof(startpointer)));
732  return(_sion_errorprint(SION_SIZE_NOT_VALID,_SION_ERROR_RETURN,"_sion_set_position_ansi: cannot set position to %ld (%zu),%lld (%zu) offset conversion error\n",
733  offset, sizeof(offset), startpointer, sizeof(startpointer)));
734  }
735  off_t result;
736  newpos = result = fseek(fileptr, offset, SEEK_SET);
737  DPRINTFP((1024, "_sion_set_position_ansi", -1, "set position=%lld (LINUX)\n", (long long) result));
738 #elif defined(_SION_AIX)
739  off64_t offset = (off_t) startpointer;
740  off64_t result;
741  if (offset != startpointer) {
742  return(_sion_errorprint(SION_SIZE_NOT_VALID,_SION_ERROR_RETURN,"_sion_set_position_ansi: cannot set position to %ld (%zu),%lld (%zu) offset conversion error\n",
743  offset, sizeof(offset), startpointer, sizeof(startpointer)));
744  }
745  result = fseeko64(fileptr, offset, SEEK_SET);
746  newpos = result = ftello64(fileptr);
747 #else
748 #error "No platform selected."
749  newpos = SION_SIZE_NOT_VALID;
750 #endif
751  DPRINTFP((32, "_sion_set_position_ansi", -1, "leave (%lld --> %lld)\n",(long long) startpointer, (long long) newpos));
752 
753  return newpos;
754 }
755 
762 sion_int64 _sion_file_get_position_ansi(FILE *fileptr) {
763  sion_int64 result;
764 
765 #if defined(_SION_LINUX)
766  off_t resulto;
767  int fd = fileno(fileptr);
768  resulto = lseek(fd,0,SEEK_CUR);
769  result = (sion_int64) resulto;
770  DPRINTFP((4096, "_sion_get_position", -1, "get position=%ld (LINUX)\n", (long) result));
771 #elif defined(_SION_DARWIN)
772  off_t resulto;
773  resulto = ftello(fileptr);
774  result = (sion_int64) resulto;
775  DPRINTFP((4096, "_sion_get_position", -1, "get position=%ld (DARWIN)\n", (long) result));
776 #elif defined(_SION_AIX)
777  result = ftell(fileptr);
778  DPRINTFP((4096, "_sion_get_position", -1, "got position=%lld (AIX)\n", result));
779 #elif defined(_SION_BGP)
780  off_t resulto;
781  int fd = fileno(fileptr);
782  resulto = lseek(fd, 0, SEEK_CUR);
783  result = (sion_int64) resulto;
784  DPRINTFP((4096, "_sion_get_position", -1, "got position=%lld (BGP)\n", result));
785 #elif defined(_SION_BGQ)
786  off_t resulto;
787  int fd = fileno(fileptr);
788  resulto = lseek(fd, 0, SEEK_CUR);
789  result = (sion_int64) resulto;
790  DPRINTFP((4096, "_sion_get_position", -1, "got position=%lld (BGQ)\n", result));
791 #else
792 #error "No platform selected."
793  result = SION_SIZE_NOT_VALID;
794 #endif
795 
796  return (result);
797 }
798 
805 int _sion_file_flush_ansi(FILE *fileptr) {
806  return (0 == fflush(fileptr)) ? SION_SUCCESS : SION_NOT_SUCCESS;
807 }
808 
815 int _sion_file_purge_ansi(FILE *fileptr) {
816  return _sion_file_flush_ansi(fileptr);
817 }
818 
827 int _sion_file_set_buffer_ansi(FILE *fileptr, char *buffer, sion_int32 buffer_size) {
828  DPRINTFP((32, "_sion_file_set_buffer", -1, "set buffer of fileptr\n"));
829  return (0 == setvbuf(fileptr, buffer, _IOFBF, (size_t) buffer_size)) ? SION_SUCCESS : SION_NOT_SUCCESS;
830 }
831 
840 sion_int64 _sion_file_write_ansi(const void *data, sion_int64 bytes, FILE *fileptr ) {
841  return (bytes == fwrite(data, 1, bytes, fileptr)) ? bytes : -1;
842 }
843 
854 sion_int64 _sion_file_read_ansi(void *data, sion_int64 bytes, FILE *fileptr ) {
855  if (fread(data, 1, bytes, fileptr) < bytes) {
856  if (feof(fileptr)) {
857  return bytes;
858  } else {
859  return -1;
860  }
861  } else {
862  return bytes;
863  }
864 }
865 
873 int _sion_file_open_posix_write_create(const char *fname, unsigned int addflags) {
874  int fd;
875 
876 #if defined(_SION_LINUX) || defined(_SION_DARWIN)
877  do {
878  fd = open(fname, O_CREAT | O_RDWR, 0664);
879  } while (-1 == fd && EINTR == errno);
880 #elif defined(_SION_AIX) || defined(_SION_BGP) || defined(_SION_BGQ)
881  do {
882  fd = open64(fname, O_CREAT | O_RDWR, 0664);
883  } while (-1 == fd && EINTR == errno);
884 #else
885 #error "No platform selected."
886  fd = -1;
887 #endif
888 
889  return fd;
890 }
891 
899 int _sion_file_open_posix_write_existing(const char *fname, unsigned int addflags) {
900  int fd;
901 
902 #if defined(_SION_LINUX) || defined(_SION_DARWIN)
903  do {
904  fd = open(fname, O_RDWR, 0664);
905  } while (-1 == fd && EINTR == errno);
906 #elif defined(_SION_AIX) || defined(_SION_BGP) || defined(_SION_BGQ)
907  do {
908  fd = open64(fname, O_RDWR, 0664);
909  } while (-1 == fd && EINTR == errno);
910 #else
911 #error "No platform selected."
912  fd = -1;
913 #endif
914 
915  return fd;
916 }
917 
925 int _sion_file_open_posix_read(const char *fname, unsigned int addflags) {
926  int fd;
927 
928 #if defined(_SION_LINUX) || defined(_SION_DARWIN)
929  do {
930  fd = open(fname, O_RDONLY, 0664);
931  } while (-1 == fd && EINTR == errno);
932 #elif defined(_SION_AIX) || defined(_SION_BGP) || defined(_SION_BGQ)
933  do {
934  fd = open64(fname, O_RDONLY, 0664);
935  } while (-1 == fd && EINTR == errno);
936 #else
937 #error "No platform selected."
938  fd = -1;
939 #endif
940 
941  return (fd);
942 }
943 
951 #if defined(_SION_LINUX) || defined(_SION_DARWIN) || defined(_SION_AIX) || defined(_SION_BGP) || defined(_SION_BGQ)
952  return (0 == close(fd)) ? SION_SUCCESS : SION_NOT_SUCCESS;
953 #else
954 #error "No platform selected."
955  return SION_NOT_SUCCESS;
956 #endif
957 }
958 
966 #if defined(_SION_LINUX) || defined(_SION_DARWIN) || defined(_SION_AIX) || defined(_SION_BGP) || defined(_SION_BGQ)
967  struct stat sbuf;
968  if (fstat(fd, &sbuf) == 0) {
969  return sbuf.st_blksize;
970  } else {
971  return SION_SIZE_NOT_VALID;
972  }
973 #else
974 #error "No platform selected."
975  return SION_SIZE_NOT_VALID;
976 #endif
977 }
978 
986 sion_int64 _sion_file_set_position_posix(int fd, sion_int64 startpointer) {
987  sion_int64 newpos = SION_SIZE_NOT_VALID;
988 
989  DPRINTFP((32, "_sion_set_position_posix", -1, "enter (to %lld)\n", (long long) startpointer));
990 
991 #if defined(_SION_LINUX) || defined(_SION_DARWIN) || defined(_SION_BGP) || defined(_SION_BGQ)
992  off_t offset = (off_t) startpointer;
993  if (offset != startpointer) {
994  return (_sion_errorprint(SION_SIZE_NOT_VALID,_SION_ERROR_RETURN,"_sion_set_position_posix: cannot set position to %ld (%zu),%lld (%zu) offset conversion error\n",
995  offset, sizeof(offset), startpointer, sizeof(startpointer)));
996  }
997  off_t result;
998  newpos = result = lseek(fd, offset, SEEK_SET);
999 #if defined(_SION_LINUX) || defined(_SION_DARWIN)
1000  DPRINTFP((4096, "_sion_set_position_posix", -1, "set position=%lld (LINUX)\n", (long long) result));
1001 #endif
1002 #elif defined(_SION_AIX)
1003  off64_t offset = (off_t) startpointer;
1004  off64_t result;
1005  if (offset != startpointer) {
1006  return(_sion_errorprint(SION_SIZE_NOT_VALID,_SION_ERROR_RETURN,"_sion_set_position_posix: cannot set position to %ld (%zu),%lld (%zu) offset conversion error\n",
1007  offset, sizeof(offset), startpointer, sizeof(startpointer)));
1008  }
1009  newpos = result = lseek(fd, offset, SEEK_SET);
1010 #else
1011 #error "No platform selected."
1012  newpos = SION_SIZE_NOT_VALID;
1013 #endif
1014  DPRINTFP((32, "_sion_set_position_posix", -1, "leave (to %lld)\n",(long long) startpointer));
1015 
1016  return newpos;
1017 }
1018 
1025 sion_int64 _sion_file_get_position_posix(int fd) {
1026  sion_int64 result=SION_SIZE_NOT_VALID;
1027 
1028  off_t resulto;
1029  resulto = lseek(fd,0,SEEK_CUR);
1030  result = (sion_int64) resulto;
1031 
1032 #if defined(_SION_LINUX)
1033  DPRINTFP((4096, "_sion_get_position", -1, "get position=%ld (LINUX)\n", (long) result));
1034 #elif defined(_SION_DARWIN)
1035  DPRINTFP((4096, "_sion_get_position", -1, "get position=%ld (DARWIN)\n", (long) result));
1036 #elif defined(_SION_AIX)
1037  DPRINTFP((4096, "_sion_get_position", -1, "get position=%ld (AIX)\n", (long) result));
1038 #elif defined(_SION_BGP)
1039  DPRINTFP((4096, "_sion_get_position", -1, "get position=%ld (BGP)\n", (long) result));
1040 #elif defined(_SION_BGQ)
1041  DPRINTFP((4096, "_sion_get_position", -1, "get position=%ld (BGQ)\n", (long) result));
1042 #endif
1043 
1044  return (result);
1045 }
1046 
1054  int frc;
1055 
1056 #if defined(_SION_BGQ)
1057  /* BGQ 20.09.13: bad fsync performance since V1R2M1, try without */
1058  frc = 0;
1059 #else
1060  do {
1061  frc = fsync(fd);
1062  } while (frc != 0 && EINTR == errno);
1063 #endif
1064 
1065  return (0 == frc) ? SION_SUCCESS : SION_NOT_SUCCESS;
1066 }
1067 
1077  int frc;
1078 
1079 #if defined(_SION_BGQ)
1080  /* BGQ 20.09.13: bad fsync performance since V1R2M1, try without */
1081  frc = 1
1082 #else
1083  do {
1084  frc = fsync(fd);
1085  } while (frc != 0 && EINTR == errno);
1086 #endif
1087 
1088  return (0 == frc) ? SION_SUCCESS : SION_NOT_SUCCESS;
1089 }
1090 
1099 int _sion_file_set_buffer_posix(int fd, char *buffer, sion_int32 buffer_size) {
1100  DPRINTFP((32, "_sion_file_set_buffer", -1, "set buffer of fileptr\n"));
1101  return SION_SUCCESS;
1102 }
1103 
1112 sion_int64 _sion_file_write_posix(const void *data, sion_int64 bytes, int fd ) {
1113  ssize_t n = 0, k;
1114  while (1) {
1115  k = write(fd, data, bytes);
1116  if (k == -1) {
1117  if (errno != EINTR) {
1118  // I/O error, return -1
1119  return -1;
1120  } // else, interrupted before starting, retry
1121  } else if (k == bytes) {
1122  // requested amount has been written
1123  return n + k;
1124  } else {
1125  // k < bytes, presumably interrupted or no space left, retry
1126  // - retry after interruption should continue
1127  // - retry with no space left should lead to error
1128  bytes -= k;
1129  n += k;
1130  data = (char*)data + k;
1131  }
1132  }
1133 }
1134 
1145 sion_int64 _sion_file_read_posix(void *data, sion_int64 bytes, int fd ) {
1146  ssize_t n = 0, k;
1147  while (1) {
1148  k = read(fd, data, bytes);
1149  if (k == -1) {
1150  if (errno != EINTR) {
1151  // I/O error, return -1
1152  return -1;
1153  } // else, interrupted before starting, retry
1154  } else if (k == 0) {
1155  // presumably EOF, return number of bytes read up to here
1156  return n;
1157  } else if (k == bytes) {
1158  // requested amount has been read
1159  return n + k;
1160  } else {
1161  // k < bytes, presumably interrupted or EOF, retry
1162  // - retry after interruption should continue
1163  // - retry at EOF should lead to k == 0 next
1164  bytes -= k;
1165  n += k;
1166  data = (char*)data + k;
1167  }
1168  }
1169 }
1170 
1171 #if defined(_SION_SIONFWD)
1179 int _sion_file_open_sionfwd_write_create(const char *fname, unsigned int addflags) {
1180  return sionfwd_open(fname, SIONFWD_WRITE | SIONFWD_CREATE);
1181 }
1182 
1190 int _sion_file_open_sionfwd_write_existing(const char *fname, unsigned int addflags) {
1191  return sionfwd_open(fname, SIONFWD_WRITE);
1192 }
1193 
1201 int _sion_file_open_sionfwd_read(const char *fname, unsigned int addflags) {
1202  return sionfwd_open(fname, SIONFWD_READ);
1203 }
1204 
1211 int _sion_file_close_sionfwd(int fd) {
1212  return (0 == sionfwd_close(fd)) ? SION_SUCCESS : SION_NOT_SUCCESS;
1213 }
1214 
1221 long _sion_file_get_opt_blksize_sionfwd(int fd) {
1222  return sionfwd_stat_blksize(fd);
1223 }
1224 
1231 int _sion_file_flush_sionfwd(int fd) {
1232  return (sionfwd_flush(fd) == 0) ? SION_SUCCESS : SION_NOT_SUCCESS;
1233 }
1234 
1243 int _sion_file_purge_sionfwd(int fd) {
1244  return (sionfwd_flush(fd) == 0) ? SION_SUCCESS : SION_NOT_SUCCESS;
1245 }
1246 
1255 int _sion_file_set_buffer_sionfwd(int fd, char *buffer, sion_int32 buffer_size) {
1256  DPRINTFP((32, "_sion_file_set_buffer", -1, "set buffer of fileptr\n"));
1257  return SION_SUCCESS;
1258 }
1259 
1269 sion_int64 _sion_file_write_sionfwd(const void *data, sion_int64 bytes, int fd, sion_int64 *position) {
1270  sion_int64 written = sionfwd_pwrite(fd, data, bytes, *position);
1271  if (written != -1) { *position += written; }
1272  return written;
1273 }
1274 
1286 sion_int64 _sion_file_read_sionfwd(void *data, sion_int64 bytes, int fd, sion_int64 *position) {
1287  sion_int64 bread = sionfwd_pread(fd, data, bytes, *position);
1288  if (bread != -1) { *position += bread; }
1289  return bread;
1290 }
1291 #endif
1292 
1293 /* ********************************************************************************************** */
1294 /* *** IME NATIVE INTERFACE Functions */
1295 /* ********************************************************************************************** */
1296 
1297 #ifdef _SION_IME_NATIVE
1305 int _sion_file_open_ime_native_write_create(const char *fname, unsigned int addflags)
1306 {
1307  int fd;
1308 
1309  do {
1310  fd = ime_native_open(fname, O_CREAT | O_RDWR, 0664);
1311  } while (-1 == fd && EINTR == errno);
1312 
1313  return fd;
1314 }
1315 
1323 int _sion_file_open_ime_native_write_existing(const char *fname, unsigned int addflags)
1324 {
1325  int fd;
1326 
1327  do {
1328  fd = ime_native_open(fname, O_RDWR, 0664);
1329  } while (-1 == fd && EINTR == errno);
1330 
1331  return fd;
1332 }
1333 
1341 int _sion_file_open_ime_native_read(const char *fname, unsigned int addflags)
1342 {
1343  int fd;
1344 
1345  do {
1346  fd = ime_native_open(fname, O_RDONLY, 0664);
1347  } while (-1 == fd && EINTR == errno);
1348 
1349  return fd;
1350 }
1351 
1358 int _sion_file_close_ime_native(int fd)
1359 {
1360  return (0 == ime_native_close(fd)) ? SION_SUCCESS : SION_NOT_SUCCESS;
1361 }
1362 
1369 long _sion_file_get_opt_blksize_ime_native(int fd)
1370 {
1371  return SION_SIZE_NOT_VALID;
1372 }
1373 
1381 sion_int64 _sion_file_set_position_ime_native(int fd, sion_int64 startpointer)
1382 {
1383  sion_int64 newpos = SION_SIZE_NOT_VALID;
1384 
1385  DPRINTFP((32, "_sion_set_position_ime_native", -1, "enter (to %lld)\n", (long long)startpointer));
1386 
1387  off_t offset = (off_t)startpointer;
1388  if (offset != startpointer) {
1389  return _sion_errorprint(SION_SIZE_NOT_VALID, _SION_ERROR_RETURN,
1390  "_sion_set_position_ime_native: cannot set position to %ld (%zu),%lld (%zu) offset conversion error\n", offset, sizeof(offset),
1391  startpointer, sizeof(startpointer));
1392  }
1393  off_t result;
1394  newpos = result = ime_native_lseek(fd, offset, SEEK_SET);
1395 
1396  DPRINTFP((32, "_sion_set_position_ime_native", -1, "leave (to %lld)\n", (long long)startpointer));
1397 
1398  return newpos;
1399 }
1400 
1407 sion_int64 _sion_file_get_position_ime_native(int fd)
1408 {
1409  sion_int64 result = SION_SIZE_NOT_VALID;
1410 
1411  off_t resulto;
1412  resulto = ime_native_lseek(fd, 0, SEEK_CUR);
1413  result = (sion_int64)resulto;
1414 
1415  DPRINTFP((4096, "_sion_get_position", -1, "get position=%ld (LINUX)\n", (long)result));
1416 
1417  return result;
1418 }
1419 
1426 int _sion_file_flush_ime_native(int fd)
1427 {
1428  int frc = 0;
1429 
1430  do {
1431  frc = ime_native_fsync(fd);
1432  } while (frc != 0 && EINTR == errno);
1433 
1434  return (0 == frc) ? SION_SUCCESS : SION_NOT_SUCCESS;
1435 }
1436 
1445 int _sion_file_set_buffer_ime_native(int fd, char *buffer, int32_t buffer_size)
1446 {
1447  /* FIXME we can't really do something here. */
1448  DPRINTFP((32, "_sion_file_set_buffer", -1, "set buffer of fileptr\n"));
1449  return SION_SUCCESS;
1450 }
1451 
1460 sion_int64 _sion_file_write_ime_native(const void *data, sion_int64 bytes, int fd)
1461 {
1462  ssize_t n = 0, k;
1463  while (1) {
1464  k = ime_native_write(fd, data, bytes);
1465  if (k == -1) {
1466  if (errno != EINTR) {
1467  // I/O error, return -1
1468  return -1;
1469  } // else, interrupted before starting, retry
1470  } else if (k == bytes) {
1471  // requested amount has been written
1472  return n + k;
1473  } else {
1474  // k < bytes, presumably interrupted or no space left, retry
1475  // - retry after interruption should continue
1476  // - retry with no space left should lead to error
1477  bytes -= k;
1478  n += k;
1479  data = (char *)data + k;
1480  }
1481  }
1482 }
1483 
1494 sion_int64 _sion_file_read_ime_native(void *data, sion_int64 bytes, int fd)
1495 {
1496  ssize_t n = 0, k;
1497  while (1) {
1498  k = ime_native_read(fd, data, bytes);
1499  if (k == -1) {
1500  if (errno != EINTR) {
1501  // I/O error, return -1
1502  return -1;
1503  } // else, interrupted before starting, retry
1504  } else if (k == 0) {
1505  // presumably EOF, return number of bytes read up to here
1506  return n;
1507  } else if (k == bytes) {
1508  // requested amount has been read
1509  return n + k;
1510  } else {
1511  // k < bytes, presumably interrupted or EOF, retry
1512  // - retry after interruption should continue
1513  // - retry at EOF should lead to k == 0 next
1514  bytes -= k;
1515  n += k;
1516  data = (char *)data + k;
1517  }
1518  }
1519 }
1520 #endif
1521 
1527  _sion_fileptr *sion_fileptr;
1528 
1529  sion_fileptr = (_sion_fileptr *) malloc(sizeof(_sion_fileptr));
1530  if (sion_fileptr == NULL) {
1531  _sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"cannot allocate _sion_fileptr structure of size %lu (_sion_fileptr), aborting ...\n",
1532  (unsigned long) sizeof(_sion_fileptr));
1533  return(NULL);
1534  }
1535  sion_fileptr->fileptr = NULL;
1536  sion_fileptr->second_fileptr = NULL;
1537  sion_fileptr->fd = -1;
1538  sion_fileptr->flags = 0;
1539 #if defined(_SION_SIONFWD)
1540  sion_fileptr->position = 0;
1541 #endif
1542 
1543  return (sion_fileptr);
1544 }
1545 
1546 #define STR_PRT(X) case X: return # X
1550 char* _sion_fileptrflags_to_str (unsigned int flag) {
1551  switch (flag) {
1552  STR_PRT(SION_FILE_FLAG_ANSI);
1553  STR_PRT(SION_FILE_FLAG_SCNDANSI);
1554  STR_PRT(SION_FILE_FLAG_POSIX);
1555  STR_PRT(SION_FILE_FLAG_CREATE);
1556  STR_PRT(SION_FILE_FLAG_WRITE);
1557  STR_PRT(SION_FILE_FLAG_READ);
1558 #if defined(_SION_SIONFWD)
1559  STR_PRT(SION_FILE_FLAG_SIONFWD);
1560 #endif
1561  }
1562  return "";
1563 }
1564 
1569  int flags;
1570  if(!sion_fileptr) return("<undefined>");
1571  flags = sion_fileptr->flags;
1572 
1573  if(flags & SION_FILE_FLAG_ANSI) {
1574  if(flags & SION_FILE_FLAG_WRITE) {
1575  if(flags & SION_FILE_FLAG_CREATE) {
1576  return("<ANSI,WRITE,CREATE>");
1577  } else {
1578  return("<ANSI,WRITE>");
1579  }
1580  } else {
1581  return("<ANSI,READ>");
1582  }
1583  } else if (flags & SION_FILE_FLAG_POSIX) {
1584  if(flags & SION_FILE_FLAG_WRITE) {
1585  if(flags & SION_FILE_FLAG_CREATE) {
1586  return("<POSIX,WRITE,CREATE>");
1587  } else {
1588  return("<POSIX,WRITE>");
1589  }
1590  } else {
1591  return("<POSIX,READ>");
1592  }
1593  }
1594 #if defined(_SION_SIONFWD)
1595  else if (flags & SION_FILE_FLAG_SIONFWD) {
1596  if(flags & SION_FILE_FLAG_WRITE) {
1597  if(flags & SION_FILE_FLAG_CREATE) {
1598  return("<SIONFWD,WRITE,CREATE>");
1599  } else {
1600  return("<SIONFWD,WRITE>");
1601  }
1602  } else {
1603  return("<SIONFWD,READ>");
1604  }
1605  }
1606 #endif
1607 #ifdef _SION_IME_NATIVE
1608  else if (flags & SION_FILE_FLAG_IME_NATIVE) {
1609  if (flags & SION_FILE_FLAG_WRITE) {
1610  if (flags & SION_FILE_FLAG_CREATE) {
1611  return "<IME_NATIVE,WRITE,CREATE>";
1612  } else {
1613  return "<IME_NATIVE,WRITE>";
1614  }
1615  } else {
1616  return "<IME_NATIVE,READ>";
1617  }
1618  }
1619 #endif
1620  return("<unknown>");
1621 }
int _sion_file_stat_file2(const char *fname, unsigned int apiflag)
Check if file exists with appropriate low-level API.
Definition: sion_file.c:329
long _sion_file_get_opt_blksize(_sion_fileptr *sion_fileptr)
Get optional file system block size for a file.
Definition: sion_file.c:289
int _sion_file_flush(_sion_fileptr *sion_fileptr)
Flush data to file.
Definition: sion_file.c:434
int _sion_file_unset_second_fileptr(_sion_fileptr *sion_fileptr)
Unset second fileptr for file if opened with ANSI.
Definition: sion_file.c:551
sion_int64 _sion_file_read_posix(void *data, sion_int64 bytes, int fd)
POSIX: Read data from file.
Definition: sion_file.c:1145
int _sion_file_close(_sion_fileptr *sion_fileptr)
Close file and destroys fileptr structure.
Definition: sion_file.c:178
long _sion_file_get_opt_blksize_posix(int fd)
POSIX: Get optional file system block size for a file.
Definition: sion_file.c:965
sion_int64 _sion_file_set_position(_sion_fileptr *sion_fileptr, sion_int64 startpointer)
Set new position in file.
Definition: sion_file.c:367
int _sion_file_purge(_sion_fileptr *sion_fileptr)
Purge data to file.
Definition: sion_file.c:467
int _sion_file_close_posix(int fd)
POSIX: Close a file.
Definition: sion_file.c:950
int _sion_file_open_posix_write_existing(const char *fname, unsigned int addflags)
POSIX: Open a new file for writing.
Definition: sion_file.c:899
sion_int64 _sion_file_get_position_posix(int fd)
POSIX: Get the current position in file.
Definition: sion_file.c:1025
int _sion_file_set_buffer_posix(int fd, char *buffer, sion_int32 buffer_size)
POSIX: set buffer of fd.
Definition: sion_file.c:1099
sion_int64 _sion_file_get_position(_sion_fileptr *sion_fileptr)
Get new position in file.
Definition: sion_file.c:401
int _sion_file_set_buffer(_sion_fileptr *sion_fileptr, char *buffer, sion_int32 buffer_size)
Set buffer of fp.
Definition: sion_file.c:495
int _sion_file_get_fd(_sion_fileptr *sion_fileptr)
Utility function: Get POSIX fp.
Definition: sion_file.c:573
char * _sion_get_fileptr_desc(_sion_fileptr *sion_fileptr)
Definition: sion_file.c:1568
int _sion_file_open_posix_write_create(const char *fname, unsigned int addflags)
POSIX: Create and open a new file for writing.
Definition: sion_file.c:873
FILE * _sion_file_open_ansi_read(const char *fname, unsigned int addflags)
ANSI: Open a file for reading.
Definition: sion_file.c:653
sion_int64 _sion_file_write(const void *data, sion_int64 bytes, _sion_fileptr *sion_fileptr)
Write data to file.
Definition: sion_file.c:221
sion_int64 _sion_file_get_position_ansi(FILE *fileptr)
ANSI: Get the current position in file.
Definition: sion_file.c:762
_sion_fileptr * _sion_file_alloc_and_init_sion_fileptr(void)
Create and return _sion_fileptr.
Definition: sion_file.c:1526
char * _sion_fileptrflags_to_str(unsigned int flag)
Definition: sion_file.c:1550
_sion_fileptr * _sion_file_open(const char *fname, unsigned int flags, unsigned int addflags)
Create and open a new file for writing.
Definition: sion_file.c:53
int _sion_file_close_ansi(FILE *fileptr)
ANSI: Close a file.
Definition: sion_file.c:671
int _sion_file_set_buffer_ansi(FILE *fileptr, char *buffer, sion_int32 buffer_size)
ANSI: set buffer of fp.
Definition: sion_file.c:827
sion_int64 _sion_file_read(void *data, sion_int64 bytes, _sion_fileptr *sion_fileptr)
Read data from file.
Definition: sion_file.c:257
int _sion_file_stat_file(const char *fname)
Check if file exists (LARGE_FILE support on BlueGene)
Definition: sion_file.c:318
sion_int64 _sion_file_write_posix(const void *data, sion_int64 bytes, int fd)
POSIX: Write data to file.
Definition: sion_file.c:1112
int _sion_file_open_posix_read(const char *fname, unsigned int addflags)
POSIX: Open a file for reading.
Definition: sion_file.c:925
sion_int64 _sion_file_set_position_posix(int fd, sion_int64 startpointer)
POSIX: Set the start position for the current task.
Definition: sion_file.c:986
sion_int64 _sion_file_read_ansi(void *data, sion_int64 bytes, FILE *fileptr)
ANSI: Read data from file.
Definition: sion_file.c:854
FILE * _sion_file_open_ansi_write_existing(const char *fname, unsigned int addflags)
ANSI: Open a new file for writing.
Definition: sion_file.c:630
int _sion_file_flush_ansi(FILE *fileptr)
ANSI: Flush the data to the disk.
Definition: sion_file.c:805
int _sion_file_purge_ansi(FILE *fileptr)
ANSI: Purge the data to the disk.
Definition: sion_file.c:815
int _sion_file_purge_posix(int fd)
POSIX: Purge the data to the disk.
Definition: sion_file.c:1076
long _sion_file_get_opt_blksize_ansi(FILE *fileptr)
ANSI: Get optional file system block size for a file.
Definition: sion_file.c:686
sion_int64 _sion_file_set_position_ansi(FILE *fileptr, sion_int64 startpointer)
ANSI: Set the start position for the current task.
Definition: sion_file.c:708
int _sion_file_flush_posix(int fd)
POSIX: Flush the data to the disk.
Definition: sion_file.c:1053
sion_int64 _sion_file_write_ansi(const void *data, sion_int64 bytes, FILE *fileptr)
ANSI: Write data to file.
Definition: sion_file.c:840
FILE * _sion_file_open_ansi_write_create(const char *fname, unsigned int addflags)
ANSI: Create and open a new file for writing.
Definition: sion_file.c:611
int _sion_file_set_second_fileptr(_sion_fileptr *sion_fileptr, FILE *fileptr)
Set second fileptr for file if opened with ANSI.
Definition: sion_file.c:529
#define SION_FILE_FLAG_READ
Definition: sion_file.h:30
#define SION_FILE_FLAG_CREATE
Definition: sion_file.h:28
#define SION_FILE_FLAG_ANSI
Definition: sion_file.h:25
#define SION_FILE_FLAG_WRITE
Definition: sion_file.h:29
#define SION_FILE_FLAG_SCNDANSI
Definition: sion_file.h:26
#define SION_FILE_FLAG_POSIX
Definition: sion_file.h:27