summaryrefslogtreecommitdiffstats
path: root/tools/h5repack/h5repack_copy.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/h5repack/h5repack_copy.c')
-rw-r--r--tools/h5repack/h5repack_copy.c175
1 files changed, 170 insertions, 5 deletions
diff --git a/tools/h5repack/h5repack_copy.c b/tools/h5repack/h5repack_copy.c
index 73b201e..3bff946 100644
--- a/tools/h5repack/h5repack_copy.c
+++ b/tools/h5repack/h5repack_copy.c
@@ -23,6 +23,10 @@
extern char *progname;
+#if 0
+#define H5REPACK_DEBUG
+#endif
+
/*-------------------------------------------------------------------------
* macros
*-------------------------------------------------------------------------
@@ -39,6 +43,7 @@ static void print_dataset_info(hid_t dcpl_id,char *objname,double per, int pr);
static int do_copy_objects(hid_t fidin,hid_t fidout,trav_table_t *travt,pack_opt_t *options);
static int copy_attr(hid_t loc_in,hid_t loc_out,pack_opt_t *options);
static int copy_user_block(const char *infile, const char *outfile, hsize_t size);
+static void print_user_block(const char *filename, hid_t fid);
/*-------------------------------------------------------------------------
* Function: copy_objects
@@ -55,10 +60,12 @@ static int copy_user_block(const char *infile, const char *outfile, hsize_t si
* Peter Cao, June 13, 2007
* Add "-L, --latest" and other options to pack a file with the latest file format
*
- * Modification:
* Peter Cao, September 25, 2007
* Copy user block when repacking a file
*
+ * Pedro Vicente, August 20, 2008
+ * Add a user block to file if requested
+ *
*-------------------------------------------------------------------------
*/
@@ -84,7 +91,7 @@ int copy_objects(const char* fnamein,
/* get user block size */
{
- hid_t fcpl_in; /* File creation property list ID for input file */
+ hid_t fcpl_in; /* file creation property list ID for input file */
if((fcpl_in = H5Fget_create_plist(fidin)) < 0) {
error_msg(progname, "failed to retrieve file creation property list\n");
@@ -182,14 +189,84 @@ int copy_objects(const char* fnamein,
} /* end if */
} /* end if */
} /* end if */
+
+
+
+
+#if defined (H5REPACK_DEBUG)
+ print_user_block(fnamein,fidin);
+#endif
+
+
+ /*-------------------------------------------------------------------------
+ * set the new user userblock options in the FCPL (before H5Fcreate )
+ *-------------------------------------------------------------------------
+ */
- if((fidout = H5Fcreate(fnameout,H5F_ACC_TRUNC, fcpl, fapl)) < 0) {
+ if ( options->ublock_size > 0 && options->ublock_filename != NULL )
+ {
+ /* either use the FCPL already created or create a new one */
+ if(fcpl != H5P_DEFAULT)
+ {
+ /* set user block size */
+ if(H5Pset_userblock(fcpl, options->ublock_size) < 0)
+ {
+ error_msg(progname, "failed to set userblock size\n");
+ goto out;
+ }
+
+ }
+
+ else
+ {
+
+ /* create a file creation property list */
+ if((fcpl = H5Pcreate(H5P_FILE_CREATE)) < 0)
+ {
+ error_msg(progname, "fail to create a file creation property list\n");
+ goto out;
+ }
+
+ /* set user block size */
+ if(H5Pset_userblock(fcpl, options->ublock_size) < 0)
+ {
+ error_msg(progname, "failed to set userblock size\n");
+ goto out;
+ }
+
+ }
+
+
+
+ }
+
+ /*-------------------------------------------------------------------------
+ * create the output file
+ *-------------------------------------------------------------------------
+ */
+
+
+ if(options->verbose)
+ printf("Making file <%s>...\n",fnameout);
+
+
+ if((fidout = H5Fcreate(fnameout,H5F_ACC_TRUNC, fcpl, fapl)) < 0)
+ {
error_msg(progname, "<%s>: Could not create file\n", fnameout );
goto out;
} /* end if */
- if(options->verbose)
- printf("Making file <%s>...\n",fnameout);
+
+ /*-------------------------------------------------------------------------
+ * write a new user block if requested
+ *-------------------------------------------------------------------------
+ */
+ if ( options->ublock_size > 0 && options->ublock_filename != NULL )
+ {
+ copy_user_block( options->ublock_filename, fnameout, options->ublock_size);
+ }
+
+
/* init table */
trav_table_init(&travt);
@@ -1240,3 +1317,91 @@ done:
return status;
}
+
+
+/*-------------------------------------------------------------------------
+ * Function: print_user_block
+ *
+ * Purpose: print user block
+ *
+ * Return: 0, ok, -1 no
+ *
+ * Programmer: Pedro Vicente
+ *
+ * Date: August, 20, 2008
+ *
+ *-------------------------------------------------------------------------
+ */
+static
+void print_user_block(const char *filename, hid_t fid)
+{
+ int fh; /* file handle */
+ hsize_t ub_size; /* user block size */
+ hsize_t size; /* size read */
+ hid_t fcpl; /* file creation property list ID for HDF5 file */
+ int i;
+
+ /* get user block size */
+ if(( fcpl = H5Fget_create_plist(fid)) < 0)
+ {
+ error_msg(progname, "failed to retrieve file creation property list\n");
+ goto done;
+ }
+
+ if(H5Pget_userblock(fcpl, &ub_size) < 0)
+ {
+ error_msg(progname, "failed to retrieve userblock size\n");
+ goto done;
+ }
+
+ if(H5Pclose(fcpl) < 0)
+ {
+ error_msg(progname, "failed to close property list\n");
+ goto done;
+ }
+
+ /* open file */
+ if((fh = HDopen(filename, O_RDONLY, 0)) < 0)
+ {
+ goto done;
+ }
+
+ size = ub_size;
+
+ /* read file */
+ while(size > 0)
+ {
+ ssize_t nread; /* # of bytes read */
+ char rbuf[USERBLOCK_XFER_SIZE]; /* buffer for reading */
+
+ /* read buffer */
+ if(size > USERBLOCK_XFER_SIZE)
+ nread = HDread(fh, rbuf, (size_t)USERBLOCK_XFER_SIZE);
+ else
+ nread = HDread(fh, rbuf, (size_t)size);
+
+ for(i = 0; i < nread; i++)
+ {
+
+ printf("%c ", rbuf[i]);
+
+ }
+ printf("\n");
+
+ if(nread < 0)
+ {
+ goto done;
+ }
+
+
+ /* update size of userblock left to transfer */
+ size -= nread;
+ }
+
+done:
+ if(fh > 0)
+ HDclose(fh);
+
+
+ return;
+}