summaryrefslogtreecommitdiffstats
path: root/src/H5FDcore.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/H5FDcore.c')
-rw-r--r--src/H5FDcore.c63
1 files changed, 54 insertions, 9 deletions
diff --git a/src/H5FDcore.c b/src/H5FDcore.c
index cbbda53..b27949b 100644
--- a/src/H5FDcore.c
+++ b/src/H5FDcore.c
@@ -49,6 +49,7 @@ typedef struct H5FD_core_t {
haddr_t eoa; /*end of allocated region */
haddr_t eof; /*current allocated size */
size_t increment; /*multiples for mem allocation */
+ hbool_t backing_store; /*write to file name on flush */
int fd; /*backing store file descriptor */
hbool_t dirty; /*changes not saved? */
} H5FD_core_t;
@@ -368,23 +369,29 @@ done:
* Modifications:
* Robb Matzke, 1999-10-19
* The backing store file is created and opened if specified.
+ *
+ * Raymond Lu, 2006-11-30
+ * Enabled the driver to read an existing file depending on
+ * the setting of the backing_store and file open flags.
*-------------------------------------------------------------------------
*/
static H5FD_t *
-H5FD_core_open(const char *name, unsigned UNUSED flags, hid_t fapl_id,
+H5FD_core_open(const char *name, unsigned flags, hid_t fapl_id,
haddr_t maxaddr)
{
+ int o_flags;
H5FD_core_t *file=NULL;
H5FD_core_fapl_t *fa=NULL;
H5P_genplist_t *plist; /* Property list pointer */
+ h5_stat_t sb;
int fd=-1;
H5FD_t *ret_value;
FUNC_ENTER_NOAPI(H5FD_core_open, NULL)
/* Check arguments */
- if (!(H5F_ACC_CREAT & flags))
- HGOTO_ERROR(H5E_ARGS, H5E_UNSUPPORTED, NULL, "must create core files, not open them")
+ if (!name || !*name)
+ HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "invalid file name")
if (0==maxaddr || HADDR_UNDEF==maxaddr)
HGOTO_ERROR(H5E_ARGS, H5E_BADRANGE, NULL, "bogus maxaddr")
if (ADDR_OVERFLOW(maxaddr))
@@ -395,10 +402,18 @@ H5FD_core_open(const char *name, unsigned UNUSED flags, hid_t fapl_id,
fa = H5P_get_driver_info(plist);
} /* end if */
- /* Open backing store */
- if (fa && fa->backing_store && name &&
- (fd=HDopen(name, (O_CREAT|O_TRUNC|O_RDWR), 0666))<0)
- HGOTO_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL, "unable to open backing store")
+ /* Build the open flags */
+ o_flags = (H5F_ACC_RDWR & flags) ? O_RDWR : O_RDONLY;
+ if (H5F_ACC_TRUNC & flags) o_flags |= O_TRUNC;
+ if (H5F_ACC_CREAT & flags) o_flags |= O_CREAT;
+ if (H5F_ACC_EXCL & flags) o_flags |= O_EXCL;
+
+ /* Open backing store. The only case that backing store is off is when
+ * the backing_store flag is off and H5F_ACC_CREAT is on. */
+ if(fa->backing_store || !(H5F_ACC_CREAT & flags)) {
+ if (fa && (fd=HDopen(name, o_flags, 0666))<0)
+ HGOTO_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL, "unable to open file")
+ }
/* Create the new file struct */
if (NULL==(file=H5MM_calloc(sizeof(H5FD_core_t))))
@@ -414,6 +429,34 @@ H5FD_core_open(const char *name, unsigned UNUSED flags, hid_t fapl_id,
*/
file->increment = (fa && fa->increment>0) ? fa->increment : H5FD_CORE_INCREMENT;
+ /* If save data in backing store. */
+ file->backing_store = fa->backing_store;
+
+ /* If an existing file is opened, load the whole file into memory. */
+ if(!(H5F_ACC_CREAT & flags)) {
+ unsigned char *x;
+ size_t size;
+
+ if (HDfstat(file->fd, &sb)<0)
+ HSYS_GOTO_ERROR(H5E_FILE, H5E_BADFILE, NULL, "unable to fstat file")
+
+ size = (size_t)sb.st_size;
+
+ if(size) {
+ if (NULL==file->mem)
+ x = (unsigned char*)H5MM_malloc(size);
+
+ if (!x)
+ HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "unable to allocate memory block")
+
+ file->mem = x;
+ file->eof = size;
+
+ if(HDread(file->fd, file->mem, size)<0)
+ HGOTO_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL, "unable to read file")
+ }
+ }
+
/* Set return value */
ret_value=(H5FD_t *)file;
@@ -808,7 +851,9 @@ done:
* Friday, October 15, 1999
*
* Modifications:
- *
+ * Raymond Lu, 2006-11-30
+ * Added a condition check for backing store flag, for an
+ * existing file can be opened for read and write now.
*-------------------------------------------------------------------------
*/
/* ARGSUSED */
@@ -821,7 +866,7 @@ H5FD_core_flush(H5FD_t *_file, hid_t UNUSED dxpl_id, unsigned UNUSED closing)
FUNC_ENTER_NOAPI(H5FD_core_flush, FAIL)
/* Write to backing store */
- if (file->dirty && file->fd>=0) {
+ if (file->dirty && file->fd>=0 && file->backing_store) {
haddr_t size = file->eof;
unsigned char *ptr = file->mem;