diff options
author | Pedro Vicente Nunes <pvn@hdfgroup.org> | 2008-08-26 16:02:44 (GMT) |
---|---|---|
committer | Pedro Vicente Nunes <pvn@hdfgroup.org> | 2008-08-26 16:02:44 (GMT) |
commit | 23cd7c534be3305ef4886866befa3a4fdceab654 (patch) | |
tree | 908ed15602c1a30d182f1b91af120ec8d7d0d153 /tools/h5repack/h5repack_copy.c | |
parent | cb166460d03ebace16b39ad3af5f61cef2464b74 (diff) | |
download | hdf5-23cd7c534be3305ef4886866befa3a4fdceab654.zip hdf5-23cd7c534be3305ef4886866befa3a4fdceab654.tar.gz hdf5-23cd7c534be3305ef4886866befa3a4fdceab654.tar.bz2 |
[svn-r15533] #1184
Add a userblock to an HDF5 file during the repack. The user gives
give a filename and userblock size as command line parameters to
h5repack and the contents of that file are stored in the
userblock for the HDF5 file created by h5repack.
New flags to handle this -u and -b
Tested : windows, linux
Diffstat (limited to 'tools/h5repack/h5repack_copy.c')
-rw-r--r-- | tools/h5repack/h5repack_copy.c | 178 |
1 files changed, 172 insertions, 6 deletions
diff --git a/tools/h5repack/h5repack_copy.c b/tools/h5repack/h5repack_copy.c index 73b201e..35bf1c9 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 ) + { + /* 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 ) + { + copy_user_block( options->ublock_filename, fnameout, options->ublock_size); + } + + /* init table */ trav_table_init(&travt); @@ -235,7 +312,8 @@ int copy_objects(const char* fnamein, H5Fclose(fidin); H5Fclose(fidout); - if(ub_size > 0) + /* write only the input file user block if there is no user block file input */ + if(ub_size > 0 && options->ublock_size == 0) copy_user_block(fnamein, fnameout, ub_size); return 0; @@ -1240,3 +1318,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; +} |