diff options
Diffstat (limited to 'src/H5FDstdio.c')
-rw-r--r-- | src/H5FDstdio.c | 88 |
1 files changed, 65 insertions, 23 deletions
diff --git a/src/H5FDstdio.c b/src/H5FDstdio.c index 46c8e81..59056c9 100644 --- a/src/H5FDstdio.c +++ b/src/H5FDstdio.c @@ -166,6 +166,7 @@ static herr_t H5FD_stdio_read(H5FD_t *lf, H5FD_mem_t type, hid_t fapl_id, haddr_ static herr_t H5FD_stdio_write(H5FD_t *lf, H5FD_mem_t type, hid_t fapl_id, haddr_t addr, size_t size, const void *buf); static herr_t H5FD_stdio_flush(H5FD_t *_file, hid_t dxpl_id, unsigned closing); +static herr_t H5FD_stdio_truncate(H5FD_t *_file, hid_t dxpl_id, hbool_t closing); static const H5FD_class_t H5FD_stdio_g = { "stdio", /*name */ @@ -194,6 +195,7 @@ static const H5FD_class_t H5FD_stdio_g = { H5FD_stdio_read, /*read */ H5FD_stdio_write, /*write */ H5FD_stdio_flush, /*flush */ + H5FD_stdio_truncate, /*truncate */ NULL, /*lock */ NULL, /*unlock */ H5FD_FLMAP_SINGLE /*fl_map */ @@ -943,19 +945,63 @@ H5FD_stdio_write(H5FD_t *_file, H5FD_mem_t type, hid_t dxpl_id, haddr_t addr, * Programmer: Robb Matzke * Wednesday, October 22, 1997 * - * Modifications: - * Ported to VFL/H5FD layer - QAK, 10/18/99 - * *------------------------------------------------------------------------- */ static herr_t H5FD_stdio_flush(H5FD_t *_file, hid_t dxpl_id, unsigned closing) { H5FD_stdio_t *file = (H5FD_stdio_t*)_file; - static const char *func="H5FD_stdio_flush"; /* Function Name for error reporting */ + static const char *func = "H5FD_stdio_flush"; /* Function Name for error reporting */ /* Shut compiler up */ - dxpl_id=dxpl_id; + dxpl_id = dxpl_id; + + /* Clear the error stack */ + H5Eclear2(H5E_DEFAULT); + + /* Only try to flush the file if we have write access */ + if(file->write_access) { + /* Flush */ + if(!closing) { + if(fflush(file->fp) < 0) + H5Epush_ret(func, H5E_ERR_CLS, H5E_IO, H5E_WRITEERROR, "fflush failed", -1) + + /* Reset last file I/O information */ + file->pos = HADDR_UNDEF; + file->op = H5FD_STDIO_OP_UNKNOWN; + } /* end if */ + } /* end if */ + + return(0); +} /* end H5FD_stdio_flush() */ + + +/*------------------------------------------------------------------------- + * Function: H5F_stdio_truncate + * + * Purpose: Makes sure that the true file size is the same (or larger) + * than the end-of-address. + * + * Errors: + * IO SEEKERROR fseek failed. + * IO WRITEERROR fflush or fwrite failed. + * + * Return: Non-negative on success/Negative on failure + * + * Programmer: Quincey Koziol + * Thursday, January 31, 2008 + * + *------------------------------------------------------------------------- + */ +static herr_t +H5FD_stdio_truncate(H5FD_t *_file, hid_t dxpl_id, hbool_t closing) +{ + H5FD_stdio_t *file = (H5FD_stdio_t*)_file; + static const char *func = "H5FD_stdio_truncate"; /* Function Name for error reporting */ + + /* Shut compiler up */ + dxpl_id = dxpl_id; + closing = closing; /* Clear the error stack */ H5Eclear2(H5E_DEFAULT); @@ -963,9 +1009,10 @@ H5FD_stdio_flush(H5FD_t *_file, hid_t dxpl_id, unsigned closing) /* Only try to flush the file if we have write access */ if(file->write_access) { /* Makes sure that the true file size is the same as the end-of-address. */ - if (file->eoa!=file->eof) { + if(file->eoa != file->eof) { + int fd = fileno(file->fp); /* File descriptor for HDF5 file */ + #ifdef _WIN32 - int fd=_fileno(file->fp); /* File descriptor for HDF5 file */ HFILE filehandle; /* Windows file handle */ LARGE_INTEGER li; /* 64-bit integer for SetFilePointer() call */ @@ -975,14 +1022,16 @@ H5FD_stdio_flush(H5FD_t *_file, hid_t dxpl_id, unsigned closing) /* Translate 64-bit integers into form Windows wants */ /* [This algorithm is from the Windows documentation for SetFilePointer()] */ li.QuadPart = (LONGLONG)file->eoa; - (void)SetFilePointer((HANDLE)filehandle,li.LowPart,&li.HighPart,FILE_BEGIN); - if(SetEndOfFile((HANDLE)filehandle)==0) - H5Epush_ret(func, H5E_ERR_CLS, H5E_IO, H5E_SEEKERROR, "unable to extend file properly", -1) + (void)SetFilePointer((HANDLE)filehandle, li.LowPart, &li.HighPart, FILE_BEGIN); + if(SetEndOfFile((HANDLE)filehandle) == 0) + H5Epush_ret(func, H5E_ERR_CLS, H5E_IO, H5E_SEEKERROR, "unable to truncate/extend file properly", -1) #else /* _WIN32 */ - int fd=fileno(file->fp); /* File descriptor for HDF5 file */ + /* Reset seek offset to beginning of file, so that file isn't re-extended later */ + rewind(file->fp); - if (-1==file_truncate(fd, (file_offset_t)file->eoa)) - H5Epush_ret(func, H5E_ERR_CLS, H5E_IO, H5E_SEEKERROR, "unable to extend file properly", -1) + /* Truncate file to proper length */ + if(-1 == file_truncate(fd, (file_offset_t)file->eoa)) + H5Epush_ret(func, H5E_ERR_CLS, H5E_IO, H5E_SEEKERROR, "unable to truncate/extend file properly", -1) #endif /* _WIN32 */ /* Update the eof value */ @@ -992,23 +1041,15 @@ H5FD_stdio_flush(H5FD_t *_file, hid_t dxpl_id, unsigned closing) file->pos = HADDR_UNDEF; file->op = H5FD_STDIO_OP_UNKNOWN; } /* end if */ - - /* - * Flush - */ - if(!closing) { - if (fflush(file->fp) < 0) - H5Epush_ret(func, H5E_ERR_CLS, H5E_IO, H5E_WRITEERROR, "fflush failed", -1) - } /* end if */ } /* end if */ else { /* Double-check for problems */ - if (file->eoa>file->eof) + if(file->eoa > file->eof) H5Epush_ret(func, H5E_ERR_CLS, H5E_IO, H5E_TRUNCATED, "eoa>eof!", -1) } /* end else */ return(0); -} +} /* end H5FD_stdio_truncate() */ #ifdef _H5private_H @@ -1019,3 +1060,4 @@ H5FD_stdio_flush(H5FD_t *_file, hid_t dxpl_id, unsigned closing) */ #error "Do not use HDF5 private definitions" #endif + |