14 #define _XOPEN_SOURCE 700
22 #include <sys/types.h>
28 #if defined(_SION_SIONFWD)
29 #include <sionfwd/client.h>
34 #include "sion_error_handler.h"
67 if(!sion_fileptr->fileptr) {
69 _sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_WARN,
"error: could not open file (ANSI), %s %d %d, aborting ...\n", fname, flags, addflags);
92 if(sion_fileptr->fd<0) {
94 _sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_WARN,
"error: could not open file (POSIX), %s %d %d, aborting ...\n", fname, flags, addflags);
99 #if defined(_SION_SIONFWD)
108 sion_fileptr->fd=_sion_file_open_sionfwd_write_create(fname,addflags);
110 sion_fileptr->fd=_sion_file_open_sionfwd_write_existing(fname,addflags);
114 sion_fileptr->fd=_sion_file_open_sionfwd_read(fname,addflags);
118 if(sion_fileptr->fd == -1) {
120 _sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_WARN,
"error: could not open file (SIONFWD), %s %d %d, aborting ...\n", fname, flags, addflags);
124 return(sion_fileptr);
128 _sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_WARN,
"internal error: unknown open type, %s %d %d, aborting ...\n", fname, flags, addflags);
133 #define DFUNCTION "_sion_file_close"
141 if (NULL == sion_fileptr)
return SION_NOT_SUCCESS;
147 sion_fileptr->second_fileptr=NULL;
150 sion_fileptr->fileptr=NULL;
153 #if defined(_SION_SIONFWD)
155 rc=_sion_file_close_sionfwd(sion_fileptr->fd);
158 rc = SION_NOT_SUCCESS;
161 DPRINTFP((32, DFUNCTION, -1,
"free now fileptr=%x %d rc=%d\n",sion_fileptr,sion_fileptr->flags,rc));
177 if (NULL == sion_fileptr)
return SION_SIZE_NOT_VALID;
186 #if defined(_SION_SIONFWD)
188 return _sion_file_write_sionfwd(data,bytes,sion_fileptr->fd,&sion_fileptr->position);
191 return _sion_errorprint(SION_SIZE_NOT_VALID,_SION_ERROR_RETURN,
"_sion_file_write: cannot find valid file flag (flags=%d)\n",sion_fileptr->flags);
206 if (NULL == sion_fileptr)
return SION_SIZE_NOT_VALID;
215 #if defined(_SION_SIONFWD)
217 return _sion_file_read_sionfwd(data,bytes,sion_fileptr->fd,&sion_fileptr->position);
220 return _sion_errorprint(SION_SIZE_NOT_VALID,_SION_ERROR_RETURN,
"_sion_file_read: cannot find valid file flag (flags=%d)\n",sion_fileptr->flags);
231 if (NULL == sion_fileptr)
return SION_SIZE_NOT_VALID;
237 #if defined(_SION_SIONFWD)
239 return _sion_file_get_opt_blksize_sionfwd(sion_fileptr->fd);
242 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);
271 #if defined(_SION_LINUX) || defined(_SION_DARWIN) || defined(_SION_AIX) || defined(_SION_BGP) || defined(_SION_BGQ)
273 return 0 == stat(fname, &sbuf);
275 #error "No platform selected."
277 #if defined(_SION_SIONFWD)
279 return sionfwd_stat_file(fname);
282 return _sion_errorprint(0,_SION_ERROR_RETURN,
"_sion_file_stat_file2: cannot find valid api flag (flags=%d)\n",apiflag);
294 if (NULL == sion_fileptr)
return SION_SIZE_NOT_VALID;
304 #if defined(_SION_SIONFWD)
306 sion_fileptr->position = startpointer;
307 return sion_fileptr->position;
310 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);
321 if (NULL == sion_fileptr)
return SION_SIZE_NOT_VALID;
331 #if defined(_SION_SIONFWD)
333 return sion_fileptr->position;
336 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);
347 if (NULL == sion_fileptr)
return SION_NOT_SUCCESS;
357 #if defined(_SION_SIONFWD)
359 return _sion_file_flush_sionfwd(sion_fileptr->fd);
362 return _sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,
"_sion_file_flush: cannot find valid file flag (flags=%d)\n",sion_fileptr->flags);
373 if( sion_fileptr == NULL )
return SION_NOT_SUCCESS;
383 #if defined(_SION_SIONFWD)
385 return _sion_file_purge_sionfwd(sion_fileptr->fd);
388 return _sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,
"_sion_file_purge: cannot find valid file flag (flags=%d)\n",sion_fileptr->flags);
401 if( sion_fileptr == NULL )
return SION_NOT_SUCCESS;
411 #if defined(_SION_SIONFWD)
413 return _sion_file_set_buffer_sionfwd(sion_fileptr->fd, buffer, buffer_size);
416 return _sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,
"_sion_file_set_buffer: cannot find valid file flag (flags=%d)\n",sion_fileptr->flags);
428 if( sion_fileptr == NULL )
return SION_NOT_SUCCESS;
432 return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,
"internal error: second fileptr already set, aborting ...\n"));
435 sion_fileptr->second_fileptr=fileptr;
439 return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,
"internal error: second fileptr could not be set for non-ANSI file, aborting ...\n"));
450 if( sion_fileptr == NULL )
return SION_NOT_SUCCESS;
455 sion_fileptr->second_fileptr=NULL;
458 return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,
"internal error: second fileptr was not set, aborting ...\n"));
461 return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,
"internal error: second fileptr could not be unset for non-ANSI file, aborting ...\n"));
472 if( sion_fileptr == NULL )
return SION_ID_UNDEF;
475 return sion_fileptr->fd;
478 return fileno(sion_fileptr->second_fileptr);
480 return fileno(sion_fileptr->fileptr);
482 #if defined(_SION_SIONFWD)
484 return SION_ID_UNDEF;
487 return _sion_errorprint(SION_ID_UNDEF,_SION_ERROR_RETURN,
"_sion_file_get_fd: cannot find valid file flag (flags=%d)\n",sion_fileptr->flags);
503 #if defined(_SION_LINUX) || defined(_SION_DARWIN)
504 return fopen(fname,
"w");
505 #elif defined(_SION_AIX) || defined(_SION_BGP) || defined(_SION_BGQ)
506 int fd = open64(fname, O_CREAT | O_RDWR, 0664);
507 return fdopen(fd,
"w");
509 #error "No platform selected."
522 #if defined(_SION_LINUX) || defined(_SION_DARWIN)
524 return fopen(fname,
"r+");
525 #elif defined(_SION_AIX)
526 int fd = open64(fname, O_CREAT | O_RDWR, 0664);
527 return fdopen(fd,
"r+");
528 #elif defined(_SION_BGP) || defined(_SION_BGQ)
529 int fd = open64(fname, O_RDWR, 0664);
530 return fdopen(fd,
"r+");
532 #error "No platform selected."
545 #if defined(_SION_LINUX) || defined(_SION_DARWIN)
546 return fopen(fname,
"r");
547 #elif defined(_SION_AIX) || defined(_SION_BGP) || defined(_SION_BGQ)
548 int fd = open64(fname, O_RDONLY, 0664);
549 return fdopen(fd,
"r");
551 #error "No platform selected."
563 #if defined(_SION_LINUX) || defined(_SION_DARWIN) || defined(_SION_AIX) || defined(_SION_BGP) || defined(_SION_BGQ)
564 return (0 == fclose(fileptr)) ? SION_SUCCESS : SION_NOT_SUCCESS;
566 #error "No platform selected."
567 return SION_NOT_SUCCESS;
578 #if defined(_SION_LINUX) || defined(_SION_DARWIN) || defined(_SION_AIX) || defined(_SION_BGP) || defined(_SION_BGQ)
579 int fd = fileno(fileptr);
581 if (fstat(fd, &sbuf) == 0) {
582 return sbuf.st_blksize;
584 return SION_SIZE_NOT_VALID;
587 #error "No platform selected."
588 return SION_SIZE_NOT_VALID;
602 DPRINTFP((32,
"_sion_set_position_ansi", -1,
"enter (to %lld) (to %ld)\n", (
long long) startpointer, (
long) startpointer));
604 #if defined(_SION_LINUX) || defined(_SION_BGP) || defined(_SION_BGQ)
605 off_t offset = (off_t) startpointer;
606 if (offset != startpointer) {
607 DPRINTFP((32,
"_sion_set_position_ansi", -1,
"_sion_set_position_ansi: cannot set position to %ld (%zu),%lld (%zu) offset conversion error\n",
608 offset,
sizeof(offset), startpointer,
sizeof(startpointer)));
609 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",
610 offset,
sizeof(offset), startpointer,
sizeof(startpointer)));
612 int fd = fileno(fileptr);
614 newpos = result = lseek(fd, offset, SEEK_SET);
615 #if defined(_SION_LINUX)
616 DPRINTFP((1024,
"_sion_set_position_ansi", -1,
"set position=%lld (LINUX)\n", (
long long) result));
618 #elif defined(_SION_DARWIN)
619 long offset = (long) startpointer;
620 if (offset != startpointer) {
621 DPRINTFP((32,
"_sion_set_position_ansi", -1,
"_sion_set_position_ansi: cannot set position to %ld (%zu),%lld (%zu) offset conversion error\n",
622 offset,
sizeof(offset), startpointer,
sizeof(startpointer)));
623 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",
624 offset,
sizeof(offset), startpointer,
sizeof(startpointer)));
627 newpos = result = fseek(fileptr, offset, SEEK_SET);
628 DPRINTFP((1024,
"_sion_set_position_ansi", -1,
"set position=%lld (LINUX)\n", (
long long) result));
629 #elif defined(_SION_AIX)
630 off64_t offset = (off_t) startpointer;
632 if (offset != startpointer) {
633 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",
634 offset,
sizeof(offset), startpointer,
sizeof(startpointer)));
636 result = fseeko64(fileptr, offset, SEEK_SET);
637 newpos = result = ftello64(fileptr);
639 #error "No platform selected."
640 newpos = SION_SIZE_NOT_VALID;
642 DPRINTFP((32,
"_sion_set_position_ansi", -1,
"leave (%lld --> %lld)\n",(
long long) startpointer, (
long long) newpos));
656 #if defined(_SION_LINUX)
658 int fd = fileno(fileptr);
659 resulto = lseek(fd,0,SEEK_CUR);
660 result = (sion_int64) resulto;
661 DPRINTFP((4096,
"_sion_get_position", -1,
"get position=%ld (LINUX)\n", (
long) result));
662 #elif defined(_SION_DARWIN)
664 resulto = ftello(fileptr);
665 result = (sion_int64) resulto;
666 DPRINTFP((4096,
"_sion_get_position", -1,
"get position=%ld (DARWIN)\n", (
long) result));
667 #elif defined(_SION_AIX)
668 result = ftell(fileptr);
669 DPRINTFP((4096,
"_sion_get_position", -1,
"got position=%lld (AIX)\n", result));
670 #elif defined(_SION_BGP)
672 int fd = fileno(fileptr);
673 resulto = lseek(fd, 0, SEEK_CUR);
674 result = (sion_int64) resulto;
675 DPRINTFP((4096,
"_sion_get_position", -1,
"got position=%lld (BGP)\n", result));
676 #elif defined(_SION_BGQ)
678 int fd = fileno(fileptr);
679 resulto = lseek(fd, 0, SEEK_CUR);
680 result = (sion_int64) resulto;
681 DPRINTFP((4096,
"_sion_get_position", -1,
"got position=%lld (BGQ)\n", result));
683 #error "No platform selected."
684 result = SION_SIZE_NOT_VALID;
697 return (0 == fflush(fileptr)) ? SION_SUCCESS : SION_NOT_SUCCESS;
719 DPRINTFP((32,
"_sion_file_set_buffer", -1,
"set buffer of fileptr\n"));
720 return (0 == setvbuf(fileptr, buffer, _IOFBF, (
size_t) buffer_size)) ? SION_SUCCESS : SION_NOT_SUCCESS;
732 return (bytes == fwrite(data, 1, bytes, fileptr)) ? bytes : -1;
746 if (fread(data, 1, bytes, fileptr) < bytes) {
767 #if defined(_SION_LINUX) || defined(_SION_DARWIN)
769 fd = open(fname, O_CREAT | O_RDWR, 0664);
770 }
while (-1 == fd && EINTR == errno);
771 #elif defined(_SION_AIX) || defined(_SION_BGP) || defined(_SION_BGQ)
773 fd = open64(fname, O_CREAT | O_RDWR, 0664);
774 }
while (-1 == fd && EINTR == errno);
776 #error "No platform selected."
793 #if defined(_SION_LINUX) || defined(_SION_DARWIN)
795 fd = open(fname, O_RDWR, 0664);
796 }
while (-1 == fd && EINTR == errno);
797 #elif defined(_SION_AIX) || defined(_SION_BGP) || defined(_SION_BGQ)
799 fd = open64(fname, O_RDWR, 0664);
800 }
while (-1 == fd && EINTR == errno);
802 #error "No platform selected."
819 #if defined(_SION_LINUX) || defined(_SION_DARWIN)
821 fd = open(fname, O_RDONLY, 0664);
822 }
while (-1 == fd && EINTR == errno);
823 #elif defined(_SION_AIX) || defined(_SION_BGP) || defined(_SION_BGQ)
825 fd = open64(fname, O_RDONLY, 0664);
826 }
while (-1 == fd && EINTR == errno);
828 #error "No platform selected."
842 #if defined(_SION_LINUX) || defined(_SION_DARWIN) || defined(_SION_AIX) || defined(_SION_BGP) || defined(_SION_BGQ)
843 return (0 == close(fd)) ? SION_SUCCESS : SION_NOT_SUCCESS;
845 #error "No platform selected."
846 return SION_NOT_SUCCESS;
857 #if defined(_SION_LINUX) || defined(_SION_DARWIN) || defined(_SION_AIX) || defined(_SION_BGP) || defined(_SION_BGQ)
859 if (fstat(fd, &sbuf) == 0) {
860 return sbuf.st_blksize;
862 return SION_SIZE_NOT_VALID;
865 #error "No platform selected."
866 return SION_SIZE_NOT_VALID;
878 sion_int64 newpos = SION_SIZE_NOT_VALID;
880 DPRINTFP((32,
"_sion_set_position_posix", -1,
"enter (to %lld)\n", (
long long) startpointer));
882 #if defined(_SION_LINUX) || defined(_SION_DARWIN) || defined(_SION_BGP) || defined(_SION_BGQ)
883 off_t offset = (off_t) startpointer;
884 if (offset != startpointer) {
885 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",
886 offset,
sizeof(offset), startpointer,
sizeof(startpointer)));
889 newpos = result = lseek(fd, offset, SEEK_SET);
890 #if defined(_SION_LINUX) || defined(_SION_DARWIN)
891 DPRINTFP((4096,
"_sion_set_position_posix", -1,
"set position=%lld (LINUX)\n", (
long long) result));
893 #elif defined(_SION_AIX)
894 off64_t offset = (off_t) startpointer;
896 if (offset != startpointer) {
897 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",
898 offset,
sizeof(offset), startpointer,
sizeof(startpointer)));
900 newpos = result = lseek(fd, offset, SEEK_SET);
902 #error "No platform selected."
903 newpos = SION_SIZE_NOT_VALID;
905 DPRINTFP((32,
"_sion_set_position_posix", -1,
"leave (to %lld)\n",(
long long) startpointer));
917 sion_int64 result=SION_SIZE_NOT_VALID;
920 resulto = lseek(fd,0,SEEK_CUR);
921 result = (sion_int64) resulto;
923 #if defined(_SION_LINUX)
924 DPRINTFP((4096,
"_sion_get_position", -1,
"get position=%ld (LINUX)\n", (
long) result));
925 #elif defined(_SION_DARWIN)
926 DPRINTFP((4096,
"_sion_get_position", -1,
"get position=%ld (DARWIN)\n", (
long) result));
927 #elif defined(_SION_AIX)
928 DPRINTFP((4096,
"_sion_get_position", -1,
"get position=%ld (AIX)\n", (
long) result));
929 #elif defined(_SION_BGP)
930 DPRINTFP((4096,
"_sion_get_position", -1,
"get position=%ld (BGP)\n", (
long) result));
931 #elif defined(_SION_BGQ)
932 DPRINTFP((4096,
"_sion_get_position", -1,
"get position=%ld (BGQ)\n", (
long) result));
947 #if defined(_SION_BGQ)
953 }
while (frc != 0 && EINTR == errno);
956 return (0 == frc) ? SION_SUCCESS : SION_NOT_SUCCESS;
970 #if defined(_SION_BGQ)
976 }
while (frc != 0 && EINTR == errno);
979 return (0 == frc) ? SION_SUCCESS : SION_NOT_SUCCESS;
991 DPRINTFP((32,
"_sion_file_set_buffer", -1,
"set buffer of fileptr\n"));
1006 k = write(fd, data, bytes);
1008 if (errno != EINTR) {
1012 }
else if (k == bytes) {
1021 data = (
char*)data + k;
1039 k = read(fd, data, bytes);
1041 if (errno != EINTR) {
1045 }
else if (k == 0) {
1048 }
else if (k == bytes) {
1057 data = (
char*)data + k;
1062 #if defined(_SION_SIONFWD)
1070 int _sion_file_open_sionfwd_write_create(
const char *fname,
unsigned int addflags) {
1071 return sionfwd_open(fname, SIONFWD_WRITE | SIONFWD_CREATE);
1081 int _sion_file_open_sionfwd_write_existing(
const char *fname,
unsigned int addflags) {
1082 return sionfwd_open(fname, SIONFWD_WRITE);
1092 int _sion_file_open_sionfwd_read(
const char *fname,
unsigned int addflags) {
1093 return sionfwd_open(fname, SIONFWD_READ);
1102 int _sion_file_close_sionfwd(
int fd) {
1103 return (0 == sionfwd_close(fd)) ? SION_SUCCESS : SION_NOT_SUCCESS;
1112 long _sion_file_get_opt_blksize_sionfwd(
int fd) {
1113 return sionfwd_stat_blksize(fd);
1122 int _sion_file_flush_sionfwd(
int fd) {
1123 return (sionfwd_flush(fd) == 0) ? SION_SUCCESS : SION_NOT_SUCCESS;
1134 int _sion_file_purge_sionfwd(
int fd) {
1135 return (sionfwd_flush(fd) == 0) ? SION_SUCCESS : SION_NOT_SUCCESS;
1146 int _sion_file_set_buffer_sionfwd(
int fd,
char *buffer, sion_int32 buffer_size) {
1147 DPRINTFP((32,
"_sion_file_set_buffer", -1,
"set buffer of fileptr\n"));
1148 return SION_SUCCESS;
1160 sion_int64 _sion_file_write_sionfwd(
const void *data, sion_int64 bytes,
int fd, sion_int64 *position) {
1161 sion_int64 written = sionfwd_pwrite(fd, data, bytes, *position);
1162 if (written != -1) { *position += written; }
1177 sion_int64 _sion_file_read_sionfwd(
void *data, sion_int64 bytes,
int fd, sion_int64 *position) {
1178 sion_int64 bread = sionfwd_pread(fd, data, bytes, *position);
1179 if (bread != -1) { *position += bread; }
1192 if (sion_fileptr == NULL) {
1193 _sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,
"cannot allocate _sion_fileptr structure of size %lu (_sion_fileptr), aborting ...\n",
1197 sion_fileptr->fileptr = NULL;
1198 sion_fileptr->second_fileptr = NULL;
1199 sion_fileptr->fd = -1;
1200 sion_fileptr->flags = 0;
1201 sion_fileptr->position = 0;
1203 return (sion_fileptr);
1206 #define STR_PRT(X) case X: return # X
1228 if(!sion_fileptr)
return(
"<undefined>");
1229 flags = sion_fileptr->flags;
1234 return(
"<ANSI,WRITE,CREATE>");
1236 return(
"<ANSI,WRITE>");
1239 return(
"<ANSI,READ>");
1245 return(
"<POSIX,WRITE,CREATE>");
1247 return(
"<POSIX,WRITE>");
1250 return(
"<POSIX,READ>");
1256 return(
"<SIONFWD,WRITE,CREATE>");
1258 return(
"<SIONFWD,WRITE>");
1261 return(
"<SIONFWD,READ>");
1266 return(
"<unknown>");