/*------------------------------------------------------------------------- * Function: h5_make_local_copy * * Purpose: Make copy of file. Some tests write to data files under that * are under version control. Those tests should make a copy of * the versioned file and write to the copy. This function * prepends srcdir to the name of the file to be copied and uses * the name of the copy as is. * * Return: Success: 0 * * Failure: -1 * * Programmer: Larry Knox * Monday, October 13, 2009 * * Modifications: * *------------------------------------------------------------------------- */ hid_t h5_make_local_copy(char *origfilename, char *local_copy_name) { int fd_old = (-1), fd_new = (-1); /* File descriptors for copying data */ ssize_t nread; /* Number of bytes read in */ char buf[READ_BUF_SIZE]; /* Buffer for copying data */ char filename[FILENAME_BUF_SIZE] = ""; #ifdef H5_VMS HDstrcat(filename, origfilename); #else char * srcdir = HDgetenv("srcdir"); /* The source directory */ if(srcdir && ((HDstrlen(srcdir) + HDstrlen(origfilename) + 6) < FILENAME_BUF_SIZE)) { HDstrcpy(filename, srcdir); HDstrcat(filename, "/"); } HDstrcat(filename, origfilename); #endif /* Copy old file into temporary file */ if((fd_old = HDopen(filename, O_RDONLY, 0666)) < 0) return -1; if((fd_new = HDopen(local_copy_name, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0) return -1; /* Copy data */ while((nread = HDread(fd_old, buf, (size_t)READ_BUF_SIZE)) > 0) HDwrite(fd_new, buf, (size_t)nread); /* Close files */ if(HDclose(fd_old) < 0) return -1; if(HDclose(fd_new) < 0) return -1; return 0; }
/*------------------------------------------------------------------------- * Function: same_contents * * Purpose: Determines whether two files are exactly the same. * * Return: Success: nonzero if same, zero if different. * * Failure: zero * * Programmer: Robb Matzke * Wednesday, March 4, 1998 * * Modifications: * *------------------------------------------------------------------------- */ static int same_contents (const char *name1, const char *name2) { int fd1, fd2; ssize_t n1, n2; char buf1[1024], buf2[1024]; fd1 = HDopen(name1, O_RDONLY, 0666); fd2 = HDopen(name2, O_RDONLY, 0666); assert(fd1 >= 0 && fd2 >= 0); while(1) { /* Asserts will catch negative return values */ n1 = HDread(fd1, buf1, sizeof(buf1)); n2 = HDread(fd2, buf2, sizeof(buf2)); assert(n1 >= 0 && (size_t)n1 <= sizeof(buf1)); assert(n2 >= 0 && (size_t)n2 <= sizeof(buf2)); assert(n1 == n2); if(n1 == 0 && n2 == 0) break; if(HDmemcmp(buf1, buf2, (size_t)n1)) { HDclose(fd1); HDclose(fd2); return 0; } } HDclose(fd1); HDclose(fd2); return 1; }
static int encode_plist(hid_t plist_id, int little_endian, const char *filename_le, const char *filename_be) { int fd = 0; /* file descriptor */ herr_t ret = 0; void *temp_buf = NULL; size_t temp_size = 0; ssize_t write_size; /* first call to encode returns only the size of the buffer needed */ if((ret = H5Pencode(plist_id, NULL, &temp_size)) < 0) assert(ret > 0); temp_buf = (void *)HDmalloc(temp_size); assert(temp_buf); if((ret = H5Pencode(plist_id, temp_buf, &temp_size)) < 0) assert(ret > 0); if(little_endian) fd = HDopen(filename_le, O_RDWR | O_CREAT | O_TRUNC, 0666); else fd = HDopen(filename_be, O_RDWR | O_CREAT | O_TRUNC, 0666); assert(fd > 0); write_size = HDwrite(fd, temp_buf, temp_size); assert(write_size == (ssize_t)temp_size); HDclose(fd); HDfree(temp_buf); return 1; }
static int test_plists(const char *filename1, const char *filename2) { int fd_le, fd_be; size_t size_le = 0, size_be = 0; void *buf_le = NULL, *buf_be = NULL; hid_t plist_le, plist_be; /* dataset create prop. list */ const char *testfile; testfile = H5_get_srcdir_filename(filename1); if((fd_le = HDopen(testfile, O_RDONLY, 0666)) < 0) TEST_ERROR size_le = HDlseek(fd_le, (HDoff_t)0, SEEK_END); HDlseek(fd_le, (HDoff_t)0, SEEK_SET); buf_le = (void *)HDmalloc(size_le); if(HDread(fd_le, buf_le, size_le) < 0) TEST_ERROR HDclose(fd_le); testfile = H5_get_srcdir_filename(filename2); if((fd_be = HDopen(testfile, O_RDONLY, 0666)) < 0) TEST_ERROR size_be = HDlseek(fd_be, (HDoff_t)0, SEEK_END); HDlseek(fd_be, (HDoff_t)0, SEEK_SET); buf_be = (void *)HDmalloc(size_be); if(HDread(fd_be, buf_be, size_be) < 0) TEST_ERROR HDclose(fd_be); if((plist_le = H5Pdecode(buf_le)) < 0) FAIL_STACK_ERROR if((plist_be = H5Pdecode(buf_be)) < 0) FAIL_STACK_ERROR if(!H5Pequal(plist_le, plist_be)) FAIL_PUTS_ERROR("PLIST encoding/decoding comparison failed\n") if((H5Pclose(plist_le)) < 0) FAIL_STACK_ERROR if((H5Pclose(plist_be)) < 0) FAIL_STACK_ERROR HDfree(buf_le); HDfree(buf_be); return 1; error: printf("***** Plist Encode/Decode tests FAILED! *****\n"); return -1; }
/*------------------------------------------------------------------------- * Function: supports_big * * Purpose: Determines if the file system of the current working * directory supports big files. * * Return: Success: Non-zero if big files are supported; zero * otherwise. * * Failure: zero * * Programmer: Raymond Lu * Wednesday, April 18, 2007 * * Modifications: * *------------------------------------------------------------------------- */ static fsizes_t supports_big(void) { int fd = -1; fsizes_t fsize = NOFILE; if((fd=HDopen("y.h5", O_RDWR|O_TRUNC|O_CREAT, 0666)) < 0) goto error; /* Write a few byte at the beginning */ if(5!=HDwrite(fd, "hello", (size_t)5)) goto quit; fsize = SFILE; /* Write a few bytes at 2GB */ if(HDlseek(fd, 2*GB, SEEK_SET)!=2*GB) goto quit; if(5!=HDwrite(fd, "hello", (size_t)5)) goto quit; fsize = LFILE; /* Write a few bytes at 4GB */ if(HDlseek(fd, 4*GB, SEEK_SET) != 4*GB) goto quit; if(5!=HDwrite(fd, "hello", (size_t)5)) goto quit; fsize = XLFILE; /* If this supports sparse_file, write a few bytes at 32GB */ if(!sparse_support) goto quit; if(HDlseek(fd, 32*GB, SEEK_SET) != 32*GB) goto quit; if(5!=HDwrite(fd, "hello", (size_t)5)) goto quit; fsize = HUGEFILE; quit: if(HDclose(fd) < 0) goto error; if(HDremove("y.h5") < 0) goto error; return fsize; error: if(fd >= 0){ HDclose(fd); HDremove("y.h5"); } return fsize; }
/*------------------------------------------------------------------------- * Function: h5_make_local_copy * * Purpose: Make copy of file. Some tests write to data files under that * are under version control. Those tests should make a copy of * the versioned file and write to the copy. This function * prepends srcdir to the name of the file to be copied and uses * the name of the copy as is. * * Return: Success: 0 * * Failure: -1 * * Programmer: Larry Knox * Monday, October 13, 2009 * * Modifications: * *------------------------------------------------------------------------- */ hid_t h5_make_local_copy(const char *origfilename, const char *local_copy_name) { int fd_old = (-1), fd_new = (-1); /* File descriptors for copying data */ ssize_t nread; /* Number of bytes read in */ char buf[READ_BUF_SIZE]; /* Buffer for copying data */ const char *filename = H5_get_srcdir_filename(origfilename);; /* Get the test file name to copy */ /* Copy old file into temporary file */ if((fd_old = HDopen(filename, O_RDONLY, 0666)) < 0) return -1; if((fd_new = HDopen(local_copy_name, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0) return -1; /* Copy data */ while((nread = HDread(fd_old, buf, (size_t)READ_BUF_SIZE)) > 0) HDwrite(fd_new, buf, (size_t)nread); /* Close files */ if(HDclose(fd_old) < 0) return -1; if(HDclose(fd_new) < 0) return -1; return 0; }
/*------------------------------------------------------------------------- * Function: is_sparse * * Purpose: Determines if the file system of the current working * directory supports holes. * * Return: Success: Non-zero if holes are supported; zero * otherwise. * * Failure: zero * * Programmer: Robb Matzke * Wednesday, July 15, 1998 * *------------------------------------------------------------------------- */ static int is_sparse(void) { int fd; h5_stat_t sb; if ((fd=HDopen("x.h5", O_RDWR|O_TRUNC|O_CREAT, 0666)) < 0) return 0; if (HDlseek(fd, (off_t)(1024*1024), SEEK_SET)!=1024*1024) return 0; if (5!=HDwrite(fd, "hello", (size_t)5)) return 0; if (HDclose(fd) < 0) return 0; if (HDstat("x.h5", &sb) < 0) return 0; if (HDremove("x.h5") < 0) return 0; #ifdef H5_HAVE_STAT_ST_BLOCKS return ((unsigned long)sb.st_blocks*512 < (unsigned long)sb.st_size); #else return (0); #endif }
int read_wo_file(void) { int read_fd; int blkaddr=0; int ret_code; int linkedblocks_read=0; char buffer[BLOCKSIZE_DFT]; /* Open the data file */ if ((read_fd = HDopen(DATAFILE, O_RDONLY, 0)) < 0) { printf("READER: error from open\n"); return -1; } /* keep reading the initial block address until it is non-zero before proceeding. */ while (blkaddr == 0){ HDlseek(read_fd, (HDoff_t)0, SEEK_SET); if ((ret_code=HDread(read_fd, &blkaddr, (size_t)sizeof(blkaddr))) != sizeof(blkaddr)){ printf("blkaddr read failed in partition %d\n", 0); return -1; } } linkedblocks_read++; /* got a non-zero blkaddr. Proceed down the linked blocks. */ #ifdef DEBUG printf("got initial block address=%d\n", blkaddr); #endif while (blkaddr != 0){ HDlseek(read_fd, (HDoff_t)blkaddr, SEEK_SET); if ((ret_code=HDread(read_fd, buffer, (size_t)blocksize_g)) != blocksize_g){ printf("blkaddr read failed in partition %d\n", 0); return -1; } linkedblocks_read++; /* retrieve the block address in byte 0-3 */ HDmemcpy(&blkaddr, &buffer[0], sizeof(blkaddr)); #ifdef DEBUG printf("got next block address=%d\n", blkaddr); #endif } printf("read %d blocks\n", linkedblocks_read); return 0; }
/* Create the test file with initial "empty" file, that is, * partition 0 has a null (0) address. * * Return: 0 succeed; -1 fail. */ int create_wo_file(void) { int blkaddr=0; /* blkaddress of next linked block */ int ret_code; /* Create the data file */ if ((write_fd_g = HDopen(DATAFILE, O_RDWR|O_TRUNC|O_CREAT, 0664)) < 0) { printf("WRITER: error from open\n"); return -1; } blkaddr=0; /* write it to partition 0 */ if ((ret_code=HDwrite(write_fd_g, &blkaddr, (size_t)SIZE_BLKADDR)) != SIZE_BLKADDR){ printf("blkaddr write failed\n"); return -1; } /* File initialized, return success */ return 0; }
/*------------------------------------------------------------------------- * Function: supports_big * * Purpose: Determines if the file system of the current working * directory supports big files. * * Return: Success: Non-zero if big files are supported; zero * otherwise. * * Failure: zero * * Programmer: Raymond Lu * Wednesday, April 18, 2007 * * Modifications: * *------------------------------------------------------------------------- */ static int supports_big(void) { int fd; if ((fd=HDopen("y.h5", O_RDWR|O_TRUNC|O_CREAT, 0666))<0) return 0; /* Write a few bytes at 2GB */ if (HDlseek(fd, BIG_FILE, SEEK_SET)!=BIG_FILE) return 0; if (5!=HDwrite(fd, "hello", (size_t)5)) return 0; /* Write a few bytes at 4GB */ if (HDlseek(fd, 2*BIG_FILE, SEEK_SET) != 2*BIG_FILE) return 0; if (5!=HDwrite(fd, "hello", (size_t)5)) return 0; if (HDclose(fd)<0) return 0; if (HDunlink("y.h5")<0) return 0; return (1); }
static void fill_with_random_data(Bytef *src, uLongf src_len) { register unsigned u; h5_stat_t stat_buf; if (HDstat("/dev/urandom", &stat_buf) == 0) { uLongf len = src_len; Bytef *buf = src; int fd = HDopen("/dev/urandom", O_RDONLY, 0); HDfprintf(stdout, "Using /dev/urandom for random data\n"); if (fd < 0) error(HDstrerror(errno)); for (;;) { ssize_t rc = HDread(fd, buf, src_len); if (rc == -1) error(HDstrerror(errno)); if (rc == (ssize_t)len) break; buf += rc; len -= rc; } } else { HDfprintf(stdout, "Using random() for random data\n"); for (u = 0; u < src_len; ++u) src[u] = (Bytef)(0xff & HDrandom()); } if (compress_percent) { unsigned long s = src_len * compress_percent / 100; HDmemset(src, '\0', s); } }
/*------------------------------------------------------------------------- * Function: enough_room * * Purpose: Tries to create a bunch of sparse files to see if quotas will * get in the way. Some systems also have problems opening * enough files and we'll check that too. * * Return: Success: Non-zero * * Failure: zero * * Programmer: Robb Matzke * Thursday, August 6, 1998 * * Modifications: * *------------------------------------------------------------------------- */ static int enough_room(hid_t fapl) { int ret_value=0; int fd[68]; size_t i, size = (size_t)1 << 30; char filename[1024], name[1024]; /* Initialize file descriptors */ for (i=0; i<NELMTS(fd); i++) fd[i] = -1; /* Get file name template */ HDassert(H5FD_FAMILY==H5Pget_driver(fapl)); h5_fixname(FILENAME[0], fapl, filename, sizeof(filename)); /* Create files */ for (i=0; i<NELMTS(fd); i++) { HDsnprintf(name, sizeof(name), filename, i); if ((fd[i]=HDopen(name, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0) { goto done; } if ((off_t)size != HDlseek(fd[i], (off_t)size, SEEK_SET)) { goto done; } if (1!=HDwrite(fd[i], "X", (size_t)1)) { goto done; } } ret_value = 1; done: for (i=0; i<NELMTS(fd) && fd[i]>=0; i++) { HDsnprintf(name, sizeof(name), filename, i); if(HDclose(fd[i]) < 0) ret_value=0; HDremove(name); } return ret_value; }
static int encode_plist(hid_t plist_id, int little_endian, int word_length, const char *filename_prefix) { int fd = 0; /* file descriptor */ herr_t ret = 0; void *temp_buf = NULL; size_t temp_size = 0; ssize_t write_size; char filename[1024]; /* Generate filename */ if((ret = HDsnprintf(filename, sizeof(filename), "%s%d%s", filename_prefix, word_length, little_endian ? "le" : "be")) < 0) HDassert(ret > 0); /* first call to encode returns only the size of the buffer needed */ if((ret = H5Pencode(plist_id, NULL, &temp_size)) < 0) HDassert(ret > 0); temp_buf = (void *)HDmalloc(temp_size); HDassert(temp_buf); if((ret = H5Pencode(plist_id, temp_buf, &temp_size)) < 0) HDassert(ret > 0); fd = HDopen(filename, O_RDWR | O_CREAT | O_TRUNC, H5_POSIX_CREATE_MODE_RW); HDassert(fd > 0); write_size = HDwrite(fd, temp_buf, temp_size); HDassert(write_size == (ssize_t)temp_size); HDclose(fd); HDfree(temp_buf); return 1; }
/*------------------------------------------------------------------------- * Function: main * * Purpose: Split an hdf5 file * * Return: Success: * * Failure: * * Programmer: Robb Matzke * Wednesday, May 13, 1998 * * Modifications: * *------------------------------------------------------------------------- */ int main (int argc, char *argv[]) { const char *prog_name; /*program name */ size_t blk_size=1024; /*size of each I/O block */ char *buf=NULL; /*I/O block buffer */ size_t n, i; /*counters */ ssize_t nio; /*I/O return value */ int argno=1; /*program argument number */ int src, dst=-1; /*source & destination files */ int need_seek=FALSE; /*destination needs to seek? */ int need_write; /*data needs to be written? */ h5_stat_t sb; /*temporary file stat buffer */ int verbose=FALSE; /*display file names? */ const char *src_gen_name; /*general source name */ char *src_name=NULL; /*source member name */ int src_is_family; /*is source name a family name? */ int src_membno=0; /*source member number */ const char *dst_gen_name; /*general destination name */ char *dst_name=NULL; /*destination member name */ int dst_is_family; /*is dst name a family name? */ int dst_membno=0; /*destination member number */ off_t left_overs=0; /*amount of zeros left over */ off_t src_offset=0; /*offset in source member */ off_t dst_offset=0; /*offset in destination member */ off_t src_size; /*source logical member size */ off_t src_act_size; /*source actual member size */ off_t dst_size=1 GB; /*destination logical memb size */ hid_t fapl; /*file access property list */ hid_t file; hsize_t hdsize; /*destination logical memb size */ hbool_t family_to_sec2=FALSE; /*change family to sec2 driver? */ /* * Get the program name from argv[0]. Use only the last component. */ if ((prog_name=strrchr (argv[0], '/'))) prog_name++; else prog_name = argv[0]; /* * Parse switches. */ while (argno<argc && '-'==argv[argno][0]) { if (!strcmp (argv[argno], "-v")) { verbose = TRUE; argno++; } else if (!strcmp(argv[argno], "-V")) { printf("This is %s version %u.%u release %u\n", prog_name, H5_VERS_MAJOR, H5_VERS_MINOR, H5_VERS_RELEASE); exit(EXIT_SUCCESS); } else if (!strcmp (argv[argno], "-family_to_sec2")) { family_to_sec2 = TRUE; argno++; } else if ('b'==argv[argno][1]) { blk_size = (size_t)get_size (prog_name, &argno, argc, argv); } else if ('m'==argv[argno][1]) { dst_size = get_size (prog_name, &argno, argc, argv); } else { usage (prog_name); } /* end if */ } /* end while */ /* allocate names */ if(NULL == (src_name = (char *)HDcalloc((size_t)NAMELEN, sizeof(char)))) exit(EXIT_FAILURE); if(NULL == (dst_name = (char *)HDcalloc((size_t)NAMELEN, sizeof(char)))) exit(EXIT_FAILURE); /* * Get the name for the source file and open the first member. The size * of the first member determines the logical size of all the members. */ if (argno>=argc) usage (prog_name); src_gen_name = argv[argno++]; sprintf (src_name, src_gen_name, src_membno); src_is_family = strcmp (src_name, src_gen_name); if ((src = HDopen(src_name, O_RDONLY)) < 0) { HDperror(src_name); HDexit(EXIT_FAILURE); } if (HDfstat(src, &sb)<0) { perror ("fstat"); exit (EXIT_FAILURE); } src_size = src_act_size = sb.st_size; if (verbose) fprintf (stderr, "< %s\n", src_name); /* * Get the name for the destination file and open the first member. */ if (argno>=argc) usage (prog_name); dst_gen_name = argv[argno++]; sprintf (dst_name, dst_gen_name, dst_membno); dst_is_family = strcmp (dst_name, dst_gen_name); if ((dst = HDopen(dst_name, O_RDWR|O_CREAT|O_TRUNC, H5_POSIX_CREATE_MODE_RW)) < 0) { HDperror(dst_name); HDexit(EXIT_FAILURE); } if (verbose) fprintf (stderr, "> %s\n", dst_name); /* No more arguments */ if (argno<argc) usage (prog_name); /* Now the real work, split the file */ buf = (char *)HDmalloc(blk_size); while (src_offset<src_size) { /* Read a block. The amount to read is the minimum of: * 1. The I/O block size * 2. What's left to write in the destination member * 3. Left over zeros or what's left in the source member. */ n = blk_size; if (dst_is_family) n = (size_t)MIN((off_t)n, dst_size-dst_offset); if (left_overs) { n = (size_t)MIN ((off_t)n, left_overs); left_overs = left_overs - (off_t)n; need_write = FALSE; } else if (src_offset<src_act_size) { n = (size_t)MIN ((off_t)n, src_act_size-src_offset); if ((nio=HDread (src, buf, n))<0) { perror ("read"); exit (EXIT_FAILURE); } else if ((size_t)nio!=n) { fprintf (stderr, "%s: short read\n", src_name); exit (EXIT_FAILURE); } for (i=0; i<n; i++) { if (buf[i]) break; } need_write = (i<n); } else { n = 0; left_overs = src_size - src_act_size; need_write = FALSE; } /* * If the block contains non-zero data then write it to the * destination, otherwise just remember that we'll have to do a seek * later in the destination when we finally get non-zero data. */ if (need_write) { if (need_seek && HDlseek (dst, dst_offset, SEEK_SET)<0) { perror ("HDlseek"); exit (EXIT_FAILURE); } if ((nio=HDwrite (dst, buf, n))<0) { perror ("write"); exit (EXIT_FAILURE); } else if ((size_t)nio!=n) { fprintf (stderr, "%s: short write\n", dst_name); exit (EXIT_FAILURE); } need_seek = FALSE; } else { need_seek = TRUE; } /* * Update the source offset and open the next source family member if * necessary. The source stream ends at the first member which * cannot be opened because it doesn't exist. At the end of the * source stream, update the destination offset and break out of the * loop. The destination offset must be updated so we can fix * trailing holes. */ src_offset = src_offset + (off_t)n; if (src_offset==src_act_size) { HDclose (src); if (!src_is_family) { dst_offset = dst_offset + (off_t)n; break; } sprintf (src_name, src_gen_name, ++src_membno); if ((src = HDopen(src_name, O_RDONLY)) < 0 && ENOENT == errno) { dst_offset = dst_offset + (off_t)n; break; } else if (src<0) { perror (src_name); exit (EXIT_FAILURE); } if (HDfstat (src, &sb)<0) { perror ("fstat"); exit (EXIT_FAILURE); } src_act_size = sb.st_size; if (src_act_size>src_size) { fprintf (stderr, "%s: member truncated to %lu bytes\n", src_name, (unsigned long)src_size); } src_offset = 0; if (verbose) fprintf (stderr, "< %s\n", src_name); } /* * Update the destination offset, opening a new member if one will be * needed. The first member is extended to the logical member size * but other members might be smaller if they end with a hole. */ dst_offset = dst_offset + (off_t)n; if (dst_is_family && dst_offset==dst_size) { if (0==dst_membno) { if (HDlseek (dst, dst_size-1, SEEK_SET)<0) { perror ("HDHDlseek"); exit (EXIT_FAILURE); } if (HDread (dst, buf, 1)<0) { perror ("read"); exit (EXIT_FAILURE); } if (HDlseek (dst, dst_size-1, SEEK_SET)<0) { perror ("HDlseek"); exit (EXIT_FAILURE); } if (HDwrite (dst, buf, 1)<0) { perror ("write"); exit (EXIT_FAILURE); } } HDclose (dst); sprintf (dst_name, dst_gen_name, ++dst_membno); if ((dst = HDopen(dst_name, O_RDWR|O_CREAT|O_TRUNC, H5_POSIX_CREATE_MODE_RW)) < 0) { HDperror(dst_name); HDexit(EXIT_FAILURE); } dst_offset = 0; need_seek = FALSE; if (verbose) fprintf (stderr, "> %s\n", dst_name); } } /* * Make sure the last family member is the right size and then close it. * The last member can't end with a hole or hdf5 will think that the * family has been truncated. */ if (need_seek) { if (HDlseek (dst, dst_offset-1, SEEK_SET)<0) { perror ("HDlseek"); exit (EXIT_FAILURE); } if (HDread (dst, buf, 1)<0) { perror ("read"); exit (EXIT_FAILURE); } if (HDlseek (dst, dst_offset-1, SEEK_SET)<0) { perror ("HDlseek"); exit (EXIT_FAILURE); } if (HDwrite (dst, buf, 1)<0) { perror ("write"); exit (EXIT_FAILURE); } } HDclose (dst); /* Modify family driver information saved in superblock through private property. * These private properties are for this tool only. */ if ((fapl=H5Pcreate(H5P_FILE_ACCESS))<0) { perror ("H5Pcreate"); exit (EXIT_FAILURE); } if(family_to_sec2) { /* The user wants to change file driver from family to sec2. Open the file * with sec2 driver. This property signals the library to ignore the family * driver information saved in the superblock. */ if(H5Pset(fapl, H5F_ACS_FAMILY_TO_SEC2_NAME, &family_to_sec2) < 0) { perror ("H5Pset"); exit (EXIT_FAILURE); } } else { /* Modify family size saved in superblock through private property. It signals * library to save the new member size(specified in command line) in superblock. * This private property is for this tool only. */ if(H5Pset_fapl_family(fapl, H5F_FAMILY_DEFAULT, H5P_DEFAULT) < 0) { perror ("H5Pset_fapl_family"); exit (EXIT_FAILURE); } /* Set the property of the new member size as hsize_t */ hdsize = (hsize_t)dst_size; if(H5Pset(fapl, H5F_ACS_FAMILY_NEWSIZE_NAME, &hdsize) < 0) { perror ("H5Pset"); exit (EXIT_FAILURE); } } /* If the new file is a family file, try to open file for "read and write" to * flush metadata. Flushing metadata will update the superblock to the new * member size. If the original file is a family file and the new file is a sec2 * file, the property FAMILY_TO_SEC2 will signal the library to switch to sec2 * driver when the new file is opened. If the original file is a sec2 file and the * new file can only be a sec2 file, reopen the new file should fail. There's * nothing to do in this case. */ H5E_BEGIN_TRY { file=H5Fopen(dst_gen_name, H5F_ACC_RDWR, fapl); } H5E_END_TRY; if(file>=0) { if(H5Fclose(file)<0) { perror ("H5Fclose"); exit (EXIT_FAILURE); } /* end if */ } /* end if */ if(H5Pclose(fapl)<0) { perror ("H5Pclose"); exit (EXIT_FAILURE); } /* end if */ /* Free resources and return */ HDfree(src_name); HDfree(dst_name); HDfree(buf); return EXIT_SUCCESS; } /* end main */
int main (int argc, const char *argv[]) { int fd; unsigned int size; char *filename; long res; char *buf; h5tools_setprogname(PROGRAMNAME); h5tools_setstatus(EXIT_SUCCESS); /* Initialize h5tools lib */ h5tools_init(); parse_command_line (argc, argv); if (nbytes == NULL) { /* missing arg */ error_msg("missing size\n"); usage (h5tools_getprogname()); exit (EXIT_FAILURE); } if (argc <= (opt_ind)) { error_msg("missing file name\n"); usage (h5tools_getprogname()); exit (EXIT_FAILURE); } filename = HDstrdup (argv[opt_ind]); size = 0; res = sscanf (nbytes, "%u", &size); if (res == EOF) { /* fail */ error_msg("missing file name\n"); usage (h5tools_getprogname()); exit (EXIT_FAILURE); } fd = HDopen (filename, O_RDONLY, 0); if (fd < 0) { error_msg("can't open file %s\n", filename); exit (EXIT_FAILURE); } buf = (char *)HDmalloc ((unsigned)(size + 1)); if (buf == NULL) { HDclose (fd); exit (EXIT_FAILURE); } res = HDread (fd, buf, (unsigned)size); if (res < (long)size) { if (buf) HDfree (buf); HDclose (fd); exit (EXIT_FAILURE); } HDwrite (1, buf, (unsigned)size); if (buf) HDfree (buf); HDclose (fd); return (EXIT_SUCCESS); }
/*------------------------------------------------------------------------- * Function: test_2 * * Purpose: Tests reading from an external file set. * * Return: Success: 0 * * Failure: number of errors * * Programmer: Robb Matzke * Wednesday, March 4, 1998 * * Modifications: * *------------------------------------------------------------------------- */ static int test_2 (hid_t fapl) { hid_t file=-1; /*file to write to */ hid_t dcpl=-1; /*dataset creation properties */ hid_t space=-1; /*data space */ hid_t dset=-1; /*dataset */ hid_t grp=-1; /*group to emit diagnostics */ int fd; /*external file descriptors */ size_t i, j; /*miscellaneous counters */ hssize_t n; /*bytes of I/O */ char filename[1024]; /*file names */ int part[25], whole[100]; /*raw data buffers */ hsize_t cur_size; /*current data space size */ hid_t hs_space; /*hyperslab data space */ hsize_t hs_start = 30; /*hyperslab starting offset */ hsize_t hs_count = 25; /*hyperslab size */ int temparray[10] = {0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f}; TESTING("read external dataset"); /* Write the data to external files directly */ for (i=0; i<4; i++) { for (j=0; j<25; j++) { part[j] = (int)(i*25+j); } sprintf (filename, "extern_%lua.raw", (unsigned long)i+1); fd = HDopen(filename, O_RDWR|O_CREAT|O_TRUNC, 0666); assert (fd>=0); /* n = lseek (fd, (off_t)(i*10), SEEK_SET); */ n = HDwrite(fd,temparray,(size_t)i*10); assert (n>=0 && (size_t)n==i*10); n = HDwrite(fd, part, sizeof(part)); assert (n==sizeof(part)); HDclose(fd); } /* * Create the file and an initial group. This causes messages about * debugging to be emitted before we start playing games with what the * output looks like. */ h5_fixname(FILENAME[1], fapl, filename, sizeof filename); if((file = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0) FAIL_STACK_ERROR if((grp = H5Gcreate2(file, "emit-diagnostics", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR if(H5Gclose(grp) < 0) FAIL_STACK_ERROR /* Create the dataset */ if((dcpl = H5Pcreate(H5P_DATASET_CREATE)) < 0) goto error; if(H5Pset_external(dcpl, "extern_1a.raw", (off_t)0, (hsize_t)sizeof part) < 0 || H5Pset_external(dcpl, "extern_2a.raw", (off_t)10, (hsize_t)sizeof part) < 0 || H5Pset_external(dcpl, "extern_3a.raw", (off_t)20, (hsize_t)sizeof part) < 0 || H5Pset_external(dcpl, "extern_4a.raw", (off_t)30, (hsize_t)sizeof part) < 0) goto error; cur_size = 100; if((space = H5Screate_simple(1, &cur_size, NULL)) < 0) goto error; if((dset = H5Dcreate2(file, "dset1", H5T_NATIVE_INT, space, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0) goto error; /* * Read the entire dataset and compare with the original */ memset(whole, 0, sizeof(whole)); if(H5Dread(dset, H5T_NATIVE_INT, space, space, H5P_DEFAULT, whole) < 0) goto error; for(i = 0; i < 100; i++) if(whole[i] != (signed)i) { H5_FAILED(); puts(" Incorrect value(s) read."); goto error; } /* end if */ /* * Read the middle of the dataset */ if((hs_space = H5Scopy(space)) < 0) goto error; if(H5Sselect_hyperslab(hs_space, H5S_SELECT_SET, &hs_start, NULL, &hs_count, NULL) < 0) goto error; HDmemset(whole, 0, sizeof(whole)); if(H5Dread(dset, H5T_NATIVE_INT, hs_space, hs_space, H5P_DEFAULT, whole) < 0) goto error; if(H5Sclose(hs_space) < 0) goto error; for(i = hs_start; i<hs_start+hs_count; i++) { if(whole[i] != (signed)i) { H5_FAILED(); puts(" Incorrect value(s) read."); goto error; } } if (H5Dclose(dset) < 0) goto error; if (H5Pclose(dcpl) < 0) goto error; if (H5Sclose(space) < 0) goto error; if (H5Fclose(file) < 0) goto error; PASSED(); return 0; error: H5E_BEGIN_TRY { H5Dclose(dset); H5Pclose(dcpl); H5Sclose(space); H5Fclose(file); } H5E_END_TRY; return 1; }
/*------------------------------------------------------------------------- * Function: main * * Purpose: HDF5 user block jammer * * Return: Success: 0 * Failure: 1 * * Programmer: * * Modifications: * *------------------------------------------------------------------------- */ int main (int argc, const char *argv[]) { int ufid = -1; int h5fid = -1; int ofid = -1; void *edata; H5E_auto2_t func; hid_t ifile = -1; hid_t plist = -1; herr_t status; htri_t testval; hsize_t usize; hsize_t h5fsize; hsize_t startub; hsize_t where; hsize_t newubsize; off_t fsize; h5_stat_t sbuf; h5_stat_t sbuf2; int res; h5tools_setprogname(PROGRAMNAME); h5tools_setstatus(EXIT_SUCCESS); /* Disable error reporting */ H5Eget_auto2(H5E_DEFAULT, &func, &edata); H5Eset_auto2(H5E_DEFAULT, NULL, NULL); /* Initialize h5tools lib */ h5tools_init(); parse_command_line (argc, argv); if (ub_file == NULL) { /* no user block */ error_msg("missing arguemnt for -u <user_file>.\n"); help_ref_msg(stderr); leave (EXIT_FAILURE); } testval = H5Fis_hdf5 (ub_file); if (testval > 0) { error_msg("-u <user_file> cannot be HDF5 file, but it appears to be an HDF5 file.\n"); help_ref_msg(stderr); leave (EXIT_FAILURE); } if (input_file == NULL) { error_msg("missing arguemnt for -i <HDF5 file>.\n"); help_ref_msg(stderr); leave (EXIT_FAILURE); } testval = H5Fis_hdf5 (input_file); if (testval <= 0) { error_msg("Input HDF5 file \"%s\" is not HDF5 format.\n", input_file); help_ref_msg(stderr); leave (EXIT_FAILURE); } ifile = H5Fopen (input_file, H5F_ACC_RDONLY, H5P_DEFAULT); if (ifile < 0) { error_msg("Can't open input HDF5 file \"%s\"\n", input_file); leave (EXIT_FAILURE); } plist = H5Fget_create_plist (ifile); if (plist < 0) { error_msg("Can't get file creation plist for file \"%s\"\n", input_file); H5Fclose(ifile); leave (EXIT_FAILURE); } status = H5Pget_userblock (plist, &usize); if (status < 0) { error_msg("Can't get user block for file \"%s\"\n", input_file); H5Pclose(plist); H5Fclose(ifile); leave (EXIT_FAILURE); } H5Pclose(plist); H5Fclose(ifile); ufid = HDopen(ub_file, O_RDONLY, 0); if(ufid < 0) { error_msg("unable to open user block file \"%s\"\n", ub_file); leave (EXIT_FAILURE); } res = HDfstat(ufid, &sbuf); if(res < 0) { error_msg("Can't stat file \"%s\"\n", ub_file); HDclose (ufid); leave (EXIT_FAILURE); } fsize = (off_t)sbuf.st_size; h5fid = HDopen(input_file, O_RDONLY, 0); if(h5fid < 0) { error_msg("unable to open HDF5 file for read \"%s\"\n", input_file); HDclose (ufid); leave (EXIT_FAILURE); } res = HDfstat(h5fid, &sbuf2); if(res < 0) { error_msg("Can't stat file \"%s\"\n", input_file); HDclose (h5fid); HDclose (ufid); leave (EXIT_FAILURE); } h5fsize = (hsize_t)sbuf2.st_size; if (output_file == NULL) { ofid = HDopen (input_file, O_WRONLY, 0); if (ofid < 0) { error_msg("unable to open output file \"%s\"\n", output_file); HDclose (h5fid); HDclose (ufid); leave (EXIT_FAILURE); } } else { ofid = HDopen (output_file, O_WRONLY | O_CREAT | O_TRUNC, 0644); if (ofid < 0) { error_msg("unable to create output file \"%s\"\n", output_file); HDclose (h5fid); HDclose (ufid); leave (EXIT_FAILURE); } } newubsize = compute_user_block_size ((hsize_t) fsize); startub = usize; if (usize > 0) { if (do_clobber == TRUE) { /* where is max of the current size or the new UB */ if (usize > newubsize) { newubsize = usize; } startub = 0; /*blast the old */ } else { /* add new ub to current ublock, pad to new offset */ newubsize += usize; newubsize = compute_user_block_size ((hsize_t) newubsize); } } /* copy the HDF5 from starting at usize to starting at newubsize: * makes room at 'from' for new ub */ /* if no current ub, usize is 0 */ copy_some_to_file (h5fid, ofid, usize, newubsize, (ssize_t) (h5fsize - usize)); /* copy the old ub to the beginning of the new file */ if (!do_clobber) { where = copy_some_to_file (h5fid, ofid, (hsize_t) 0, (hsize_t) 0, (ssize_t) usize); } /* copy the new ub to the end of the ub */ where = copy_some_to_file (ufid, ofid, (hsize_t) 0, startub, (ssize_t) - 1); /* pad the ub */ where = write_pad (ofid, where); if(ub_file) HDfree (ub_file); if(input_file) HDfree (input_file); if(output_file) HDfree (output_file); if(ufid >= 0) HDclose (ufid); if(h5fid >= 0) HDclose (h5fid); if(ofid >= 0) HDclose (ofid); return h5tools_getstatus(); }
static void do_write_test(unsigned long file_size, unsigned long min_buf_size, unsigned long max_buf_size) { uLongf src_len, total_len; struct timeval timer_start, timer_stop; double total_time; Bytef *src; for (src_len = min_buf_size; src_len <= max_buf_size; src_len <<= 1) { register unsigned long i, iters; iters = file_size / src_len; src = (Bytef *)HDcalloc(1, sizeof(Bytef) * src_len); if (!src) { cleanup(); error("out of memory"); } compression_time = 0.0; if (random_test) fill_with_random_data(src, src_len); HDfprintf(stdout, "Buffer size == "); if (src_len >= ONE_KB && (src_len % ONE_KB) == 0) { if (src_len >= ONE_MB && (src_len % ONE_MB) == 0) { HDfprintf(stdout, "%ldMB", src_len / ONE_MB); } else { HDfprintf(stdout, "%ldKB", src_len / ONE_KB); } } else { HDfprintf(stdout, "%ld", src_len); } HDfprintf(stdout, "\n"); /* do uncompressed data write */ HDgettimeofday(&timer_start, NULL); output = HDopen(filename, O_RDWR | O_CREAT, S_IRWXU); if (output == -1) error(HDstrerror(errno)); for (i = 0; i <= iters; ++i) { Bytef *s_ptr = src; uLong s_len = src_len; /* loop to make sure we write everything out that we want to write */ for (;;) { ssize_t rc = HDwrite(output, s_ptr, s_len); if (rc == -1) error(HDstrerror(errno)); if (rc == (ssize_t)s_len) break; s_len -= rc; s_ptr += rc; } } HDclose(output); HDgettimeofday(&timer_stop, NULL); total_time = ((double)timer_stop.tv_sec + ((double)timer_stop.tv_usec) / MICROSECOND) - ((double)timer_start.tv_sec + ((double)timer_start.tv_usec) / MICROSECOND); HDfprintf(stdout, "\tUncompressed Write Time: %.2fs\n", total_time); HDfprintf(stdout, "\tUncompressed Write Throughput: %.2fMB/s\n", MB_PER_SEC(file_size, total_time)); HDunlink(filename); /* do compressed data write */ output = HDopen(filename, O_RDWR | O_CREAT, S_IRWXU); if (output == -1) error(HDstrerror(errno)); report_once_flag = 1; HDgettimeofday(&timer_start, NULL); for (total_len = 0; total_len < file_size; total_len += src_len) write_file(src, src_len); HDclose(output); HDgettimeofday(&timer_stop, NULL); total_time = ((double)timer_stop.tv_sec + ((double)timer_stop.tv_usec) / MICROSECOND) - ((double)timer_start.tv_sec + ((double)timer_start.tv_usec) / MICROSECOND); HDfprintf(stdout, "\tCompressed Write Time: %.2fs\n", total_time); HDfprintf(stdout, "\tCompressed Write Throughput: %.2fMB/s\n", MB_PER_SEC(file_size, total_time)); HDfprintf(stdout, "\tCompression Time: %gs\n", compression_time); HDunlink(filename); HDfree(src); } }
/*------------------------------------------------------------------------- * Function: main * * Purpose: HDF5 user block unjammer * * Return: Success: 0 * Failure: 1 * * Programmer: * * Modifications: * *------------------------------------------------------------------------- */ int main(int argc, const char *argv[]) { int ifid = -1; int ufid = -1; int h5fid = -1; void *edata; H5E_auto2_t func; hid_t ifile = -1; hid_t plist = -1; off_t fsize; hsize_t usize; htri_t testval; herr_t status; int res; h5_stat_t sbuf; h5tools_setprogname(PROGRAMNAME); h5tools_setstatus(EXIT_SUCCESS); /* Disable error reporting */ H5Eget_auto2(H5E_DEFAULT, &func, &edata); H5Eset_auto2(H5E_DEFAULT, NULL, NULL); /* Initialize h5tools lib */ h5tools_init(); parse_command_line(argc, argv); if (input_file == NULL) { /* no user block */ error_msg("missing arguemnt for HDF5 file input.\n"); help_ref_msg(stderr); leave (EXIT_FAILURE); } testval = H5Fis_hdf5(input_file); if (testval <= 0) { error_msg("Input HDF5 file \"%s\" is not HDF\n", input_file); help_ref_msg (stderr); h5tools_setstatus(EXIT_FAILURE); goto done; } ifile = H5Fopen(input_file, H5F_ACC_RDONLY , H5P_DEFAULT); if (ifile < 0) { error_msg("Can't open input HDF5 file \"%s\"\n", input_file); h5tools_setstatus(EXIT_FAILURE); goto done; } plist = H5Fget_create_plist(ifile); if (plist < 0) { error_msg("Can't get file creation plist for file \"%s\"\n", input_file); h5tools_setstatus(EXIT_FAILURE); goto done; } status = H5Pget_userblock(plist, & usize); if (status < 0) { error_msg("Can't get user block for file \"%s\"\n", input_file); h5tools_setstatus(EXIT_FAILURE); goto done; } H5Pclose(plist); H5Fclose(ifile); if (usize == 0) { /* no user block to remove: message? */ error_msg("\"%s\" has no user block: no change to file\n", input_file); h5tools_setstatus(EXIT_SUCCESS); goto done; } ifid = HDopen(input_file,O_RDONLY,0); if(ifid < 0) { error_msg("unable to open input HDF5 file \"%s\"\n", input_file); h5tools_setstatus(EXIT_FAILURE); goto done; } res = HDfstat(ifid, &sbuf); if(res < 0) { error_msg("Can't stat file \"%s\"\n", input_file); h5tools_setstatus(EXIT_FAILURE); goto done; } fsize = sbuf.st_size; if (do_delete && (ub_file != NULL)) { error_msg("??\"%s\"\n", ub_file); h5tools_setstatus(EXIT_FAILURE); goto done; } if (ub_file == NULL) { /* write to sdtout */ ufid = HDdup(1); } else { ufid = HDopen(ub_file,O_WRONLY|O_CREAT|O_TRUNC, 0644 ); if (ufid < 0) { error_msg("unable to open user block file for output\"%s\"\n", ub_file); h5tools_setstatus(EXIT_FAILURE); goto done; } } if (output_file == NULL) { h5fid = HDopen(input_file,O_WRONLY, 0); if (h5fid < 0) { error_msg("unable to open output HDF5 file \"%s\"\n", input_file); h5tools_setstatus(EXIT_FAILURE); goto done; } } else { h5fid = HDopen(output_file,O_WRONLY|O_CREAT|O_TRUNC, 0644 ); if (h5fid < 0) { error_msg("unable to open output HDF5 file \"%s\"\n", output_file); h5tools_setstatus(EXIT_FAILURE); goto done; } } /* copy from 0 to 'usize - 1' into ufid */ if (!do_delete) { if(copy_to_file(ifid, ufid, 0, (ssize_t) usize) < 0) { error_msg("unable to copy user block to output file \"%s\"\n", ub_file); h5tools_setstatus(EXIT_FAILURE); goto done; } } /* copy from usize to end of file into h5fid, * starting at end of user block if present */ if(copy_to_file(ifid, h5fid, (ssize_t) usize, (ssize_t)(fsize - (ssize_t)usize)) < 0) { error_msg("unable to copy hdf5 data to output file \"%s\"\n", output_file); h5tools_setstatus(EXIT_FAILURE); goto done; } done: if (ub_file) { HDfree (ub_file); if (ufid >= 0) HDclose(ufid); } if (input_file) HDfree (input_file); if (output_file) HDfree (output_file); if (h5fid >= 0) HDclose(h5fid); if (ifid >= 0) HDclose(ifid); return h5tools_getstatus(); }
/*------------------------------------------------------------------------- * Function: main * * Purpose: HDF5 user block jammer * * Return: Success: 0 * Failure: 1 * * Programmer: * * Modifications: * *------------------------------------------------------------------------- */ int main (int argc, const char *argv[]) { int ufid; int h5fid; int ofid; void *edata; H5E_auto_t func; hid_t ifile; hid_t plist; herr_t status; htri_t testval; hsize_t usize; hsize_t h5fsize; hsize_t startub; hsize_t where; hsize_t newubsize; off_t fsize; struct stat sbuf; struct stat sbuf2; int res; /* Disable error reporting */ H5Eget_auto (&func, &edata); H5Eset_auto (NULL, NULL); parse_command_line (argc, argv); if (ub_file == NULL) { /* no user block */ error_msg (progname, "no user block file name\n"); usage (progname); exit (EXIT_FAILURE); } if (input_file == NULL) { /* no user block */ error_msg (progname, "no HDF5 file\n"); usage (progname); exit (EXIT_FAILURE); } testval = H5Fis_hdf5 (input_file); if (testval <= 0) { error_msg (progname, "Input HDF5 file is not HDF \"%s\"\n", input_file); exit (EXIT_FAILURE); } ifile = H5Fopen (input_file, H5F_ACC_RDONLY, H5P_DEFAULT); if (ifile < 0) { error_msg (progname, "Can't open input HDF5 file \"%s\"\n", input_file); exit (EXIT_FAILURE); } plist = H5Fget_create_plist (ifile); if (plist < 0) { error_msg (progname, "Can't get file creation plist for file \"%s\"\n", input_file); exit (EXIT_FAILURE); } status = H5Pget_userblock (plist, &usize); if (status < 0) { error_msg (progname, "Can't get user block for file \"%s\"\n", input_file); exit (EXIT_FAILURE); } H5Pclose (plist); H5Fclose (ifile); ufid = HDopen (ub_file, O_RDONLY, 0); if (ufid < 0) { error_msg (progname, "unable to open user block file \"%s\"\n", ub_file); exit (EXIT_FAILURE); } res = stat (ub_file, &sbuf); if (res < 0) { error_msg (progname, "Can't stat file \"%s\"\n", ub_file); exit (EXIT_FAILURE); } fsize = sbuf.st_size; h5fid = HDopen (input_file, O_RDONLY, 0); if (h5fid < 0) { error_msg (progname, "unable to open HDF5 file for read \"%s\"\n", input_file); exit (EXIT_FAILURE); } res = stat (input_file, &sbuf2); if (res < 0) { error_msg (progname, "Can't stat file \"%s\"\n", input_file); exit (EXIT_FAILURE); } h5fsize = sbuf2.st_size; if (output_file == NULL) { ofid = HDopen (input_file, O_WRONLY, 0); if (ofid < 0) { error_msg (progname, "unable to open output file \"%s\"\n", output_file); exit (EXIT_FAILURE); } } else { ofid = HDopen (output_file, O_WRONLY | O_CREAT | O_TRUNC, 0644); if (ofid < 0) { error_msg (progname, "unable to create output file \"%s\"\n", output_file); exit (EXIT_FAILURE); } } newubsize = compute_user_block_size ((hsize_t) fsize); startub = usize; if (usize > 0) { if (do_clobber == TRUE) { /* where is max of the current size or the new UB */ if (usize > newubsize) { newubsize = usize; } startub = 0; /*blast the old */ } else { /* add new ub to current ublock, pad to new offset */ newubsize += usize; newubsize = compute_user_block_size ((hsize_t) newubsize); } } /* copy the HDF5 from starting at usize to starting at newubsize: * makes room at 'from' for new ub */ /* if no current ub, usize is 0 */ copy_some_to_file (h5fid, ofid, usize, newubsize, (ssize_t) (h5fsize - usize)); /* copy the old ub to the beginning of the new file */ if (!do_clobber) { where = copy_some_to_file (h5fid, ofid, (hsize_t) 0, (hsize_t) 0, (ssize_t) usize); } /* copy the new ub to the end of the ub */ where = copy_some_to_file (ufid, ofid, (hsize_t) 0, startub, (ssize_t) - 1); /* pad the ub */ where = write_pad (ofid, where); HDclose (ufid); HDclose (h5fid); HDclose (ofid); return d_status; }
/*------------------------------------------------------------------------- * Function: test_3 * * Purpose: Tests writing to an external file set. * * Return: Success: 0 * * Failure: number of errors * * Programmer: Robb Matzke * Wednesday, March 4, 1998 * * Modifications: * *------------------------------------------------------------------------- */ static int test_3 (hid_t fapl) { hid_t file=-1; /*file to which to write */ hid_t dcpl=-1; /*dataset creation properties */ hid_t mem_space=-1; /*memory data space */ hid_t file_space=-1; /*file data space */ hid_t dset=-1; /*dataset */ unsigned i; /*miscellaneous counters */ int fd; /*external file descriptor */ int part[25],whole[100]; /*raw data buffers */ hsize_t cur_size=100; /*current data space size */ hsize_t max_size=200; /*maximum data space size */ hsize_t hs_start=100; /*hyperslab starting offset */ hsize_t hs_count=100; /*hyperslab size */ char filename[1024]; /*file name */ int temparray[10] = {0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f}; TESTING("write external dataset"); /* Create another file */ h5_fixname(FILENAME[2], fapl, filename, sizeof filename); if ((file=H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0) { goto error; } /* Create the external file list */ if((dcpl=H5Pcreate(H5P_DATASET_CREATE)) < 0) goto error; if (H5Pset_external(dcpl, "extern_1b.raw", (off_t)0, (hsize_t)sizeof part) < 0 || H5Pset_external(dcpl, "extern_2b.raw", (off_t)10, (hsize_t)sizeof part) < 0 || H5Pset_external(dcpl, "extern_3b.raw", (off_t)20, (hsize_t)sizeof part) < 0 || H5Pset_external(dcpl, "extern_4b.raw", (off_t)30, H5F_UNLIMITED) < 0) goto error; /* Make sure the output files are fresh*/ for (i=1; i<=4; i++) { sprintf(filename, "extern_%db.raw", i); if ((fd= HDopen(filename, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0) { H5_FAILED(); printf(" cannot open %s: %s\n", filename, strerror(errno)); goto error; } HDwrite(fd, temparray, (i-1)*10); HDclose(fd); } /* Create the dataset */ if((mem_space = H5Screate_simple(1, &cur_size, &max_size)) < 0) goto error; if((file_space = H5Scopy(mem_space)) < 0) goto error; if((dset = H5Dcreate2(file, "dset1", H5T_NATIVE_INT, file_space, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0) goto error; /* Write the entire dataset and compare with the original */ for(i = 0; i < cur_size; i++) whole[i] = i; if(H5Dwrite(dset, H5T_NATIVE_INT, mem_space, file_space, H5P_DEFAULT, whole) < 0) goto error; for(i = 0; i < 4; i++) { char name1[64], name2[64]; sprintf(name1, "extern_%da.raw", i + 1); sprintf(name2, "extern_%db.raw", i + 1); if(!same_contents(name1, name2)) { H5_FAILED(); puts (" Output differs from expected value."); goto error; } /* end if */ } /* end for */ /* Extend the dataset by another 100 elements */ if(H5Dset_extent(dset, &max_size) < 0) goto error; if(H5Sclose(file_space) < 0) goto error; if((file_space = H5Dget_space(dset)) < 0) goto error; /* Write second half of dataset */ for(i = 0; i < hs_count; i++) whole[i] = 100 + i; if(H5Sselect_hyperslab(file_space, H5S_SELECT_SET, &hs_start, NULL, &hs_count, NULL) < 0) goto error; if(H5Dwrite(dset, H5T_NATIVE_INT, mem_space, file_space, H5P_DEFAULT, whole) < 0) goto error; if(H5Dclose(dset) < 0) goto error; if(H5Pclose(dcpl) < 0) goto error; if(H5Sclose(mem_space) < 0) goto error; if(H5Sclose(file_space) < 0) goto error; if(H5Fclose(file) < 0) goto error; PASSED(); return 0; error: H5E_BEGIN_TRY { H5Dclose(dset); H5Pclose(dcpl); H5Sclose(mem_space); H5Sclose(file_space); H5Fclose(file); } H5E_END_TRY; return 1; }
static void gent_ub(const char * filename, size_t ub_size, size_t ub_fill) { hid_t fid, group, attr, dataset, space; hid_t create_plist; hsize_t dims[2]; int data[2][2], dset1[10][10], dset2[20]; char buf[BUF_SIZE]; int i, j; size_t u; float dset2_1[10], dset2_2[3][5]; int fd; char *bp; if(ub_size > 0) { create_plist = H5Pcreate(H5P_FILE_CREATE); H5Pset_userblock(create_plist, (hsize_t)ub_size); fid = H5Fcreate(filename, H5F_ACC_TRUNC, create_plist, H5P_DEFAULT); } else { fid = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); } /* create groups */ group = H5Gcreate2(fid, "/g1", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Gclose(group); group = H5Gcreate2(fid, "/g2", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Gclose(group); group = H5Gcreate2(fid, "/g1/g1.1", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Gclose(group); group = H5Gcreate2(fid, "/g1/g1.2", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Gclose(group); group = H5Gcreate2(fid, "/g1/g1.2/g1.2.1", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Gclose(group); /* root attributes */ group = H5Gopen2(fid, "/", H5P_DEFAULT); dims[0] = 10; space = H5Screate_simple(1, dims, NULL); attr = H5Acreate2(group, "attr1", H5T_STD_I8BE, space, H5P_DEFAULT, H5P_DEFAULT); sprintf(buf, "abcdefghi"); H5Awrite(attr, H5T_NATIVE_SCHAR, buf); H5Sclose(space); H5Aclose(attr); dims[0] = 2; dims[1] = 2; space = H5Screate_simple(2, dims, NULL); attr = H5Acreate2(group, "attr2", H5T_STD_I32BE, space, H5P_DEFAULT, H5P_DEFAULT); data[0][0] = 0; data[0][1] = 1; data[1][0] = 2; data[1][1] = 3; H5Awrite(attr, H5T_NATIVE_INT, data); H5Sclose(space); H5Aclose(attr); H5Gclose(group); group = H5Gopen2(fid, "/g1/g1.1", H5P_DEFAULT); /* dset1.1.1 */ dims[0] = 10; dims[1] = 10; space = H5Screate_simple(2, dims, NULL); dataset = H5Dcreate2(group, "dset1.1.1", H5T_STD_I32BE, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); for (i = 0; i < 10; i++) for (j = 0; j < 10; j++) dset1[i][j] = j*i; H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, dset1); H5Sclose(space); /* attributes of dset1.1.1 */ dims[0] = 27; space = H5Screate_simple(1, dims, NULL); attr = H5Acreate2(dataset, "attr1", H5T_STD_I8BE, space, H5P_DEFAULT, H5P_DEFAULT); sprintf(buf, "1st attribute of dset1.1.1"); H5Awrite(attr, H5T_NATIVE_SCHAR, buf); H5Sclose(space); H5Aclose(attr); dims[0] = 27; space = H5Screate_simple(1, dims, NULL); attr = H5Acreate2(dataset, "attr2", H5T_STD_I8BE, space, H5P_DEFAULT, H5P_DEFAULT); sprintf(buf, "2nd attribute of dset1.1.1"); H5Awrite(attr, H5T_NATIVE_SCHAR, buf); H5Sclose(space); H5Aclose(attr); H5Dclose(dataset); /* dset1.1.2 */ dims[0] = 20; space = H5Screate_simple(1, dims, NULL); dataset = H5Dcreate2(group, "dset1.1.2", H5T_STD_I32BE, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); for (i = 0; i < 20; i++) dset2[i] = i; H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, dset2); H5Sclose(space); H5Dclose(dataset); H5Gclose(group); /* external link */ H5Lcreate_external("somefile", "somepath", fid, "/g1/g1.2/extlink", H5P_DEFAULT, H5P_DEFAULT); /* soft link */ group = H5Gopen2(fid, "/g1/g1.2/g1.2.1", H5P_DEFAULT); H5Lcreate_soft("somevalue", group, "slink", H5P_DEFAULT, H5P_DEFAULT); H5Gclose(group); group = H5Gopen2(fid, "/g2", H5P_DEFAULT); /* dset2.1 */ dims[0] = 10; space = H5Screate_simple(1, dims, NULL); dataset = H5Dcreate2(group, "dset2.1", H5T_IEEE_F32BE, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); for (i = 0; i < 10; i++) dset2_1[i] = (float)((float)i * 0.1F + 1.0F); H5Dwrite(dataset, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, dset2_1); H5Sclose(space); H5Dclose(dataset); /* dset2.2 */ dims[0] = 3; dims[1] = 5; space = H5Screate_simple(2, dims, NULL); dataset = H5Dcreate2(group, "dset2.2", H5T_IEEE_F32BE, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); for (i = 0; i < 3; i++) for (j = 0; j < 5; j++) dset2_2[i][j] = (float)(((float)i + 1.0F) * (float)j * 0.1F); H5Dwrite(dataset, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, dset2_2); H5Sclose(space); H5Dclose(dataset); H5Gclose(group); /* user-defined link */ H5Lregister(UD_link_class); H5Lcreate_ud(fid, "/g2/udlink", (H5L_type_t)MY_LINKCLASS, NULL, (size_t)0, H5P_DEFAULT, H5P_DEFAULT); H5Fclose(fid); /* If a user block is being used, write to it here */ if(ub_size > 0) { ssize_t nbytes; HDassert(ub_size <= BUF_SIZE); fd = HDopen(filename, O_RDWR); HDassert(fd >= 0); /* fill buf with pattern */ HDmemset(buf, '\0', ub_size); bp = buf; for (u = 0; u < ub_fill; u++) *bp++ = pattern[u % 10]; nbytes = HDwrite(fd, buf, ub_size); HDassert(nbytes >= 0); HDclose(fd); } }
/*------------------------------------------------------------------------- * Function: main * * Generate an HDF5 file with groups, datasets and symbolic links. * After the file is generated, write bad offset values to * the heap at 3 locations in the file: * (A) Open the file: * fd = HDopen(TESTFILE, O_RDWR, 0663); * (B) Position the file at: * (1) HDlseek(fd, (HDoff_t)880, SEEK_SET); * "/group1/group2": replace heap offset "8" by bad offset * (2) HDlseek(fd, (HDoff_t)1512, SEEK_SET); * "/dsetA": replace name offset into private heap "72" by bad offset * (3) HDlseek(fd, (HDoff_t)1616, SEEK_SET); * /soft_one: replace link value offset in the scratch pad "32" by bad offset * (C) Write the bad offset value to the file for (1), (2) and (3): * write(fd, &val, sizeof(val)); * * Note: if the groups/datasets/symbolic links are changed in the file, * the above locations need to be adjusted accordingly. * * Return: EXIT_SUCCESS/EXIT_FAILURE * *------------------------------------------------------------------------- */ int main(void) { hid_t fid = -1, gid1 = -1, gid2 = -1; /* File and group IDs */ hid_t did = -1, sid = -1; /* Dataset and dataspace IDs */ int fd = -1; /* File descriptor */ int64_t val = 999; /* Bad offset value */ /* Create the test file */ if((fid = H5Fcreate(TESTFILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR /* Create two groups */ if((gid1 = H5Gcreate2(fid, GRP1, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR if((gid2 = H5Gcreate2(gid1, GRP2, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR /* Close the groups */ if(H5Gclose(gid1) < 0) FAIL_STACK_ERROR if(H5Gclose(gid2) < 0) FAIL_STACK_ERROR /* Create soft links to the groups */ if(H5Lcreate_soft("/group1", fid, SOFT1, H5P_DEFAULT, H5P_DEFAULT) < 0) FAIL_STACK_ERROR if(H5Lcreate_soft("/group1/group2", fid, SOFT2, H5P_DEFAULT, H5P_DEFAULT) < 0) FAIL_STACK_ERROR /* Create a dataset */ if((sid = H5Screate(H5S_SCALAR)) < 0) FAIL_STACK_ERROR if((did = H5Dcreate2(fid, DSET, H5T_NATIVE_INT, sid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR /* Close the dataset */ if(H5Dclose(did) < 0) FAIL_STACK_ERROR /* Close the dataspace */ if(H5Sclose(sid) < 0) FAIL_STACK_ERROR /* Close the file */ if(H5Fclose(fid) < 0) FAIL_STACK_ERROR /* * Write bad offset values at 3 locations in the file */ /* Open the file */ if((fd = HDopen(TESTFILE, O_RDWR, 0663)) < 0) FAIL_STACK_ERROR /* Position the file for /group1/group2: replace heap offset "8" by bad offset */ if(HDlseek(fd, (HDoff_t)880, SEEK_SET) < 0) FAIL_STACK_ERROR /* Write the bad offset value to the file */ if(HDwrite(fd, &val, sizeof(val)) < 0) FAIL_STACK_ERROR /* Position the file for /dsetA: replace name offset into private heap "72" by bad offset */ if(HDlseek(fd, (HDoff_t)1512, SEEK_SET) < 0) FAIL_STACK_ERROR /* Write the bad offset value to the file */ if(HDwrite(fd, &val, sizeof(val)) < 0) FAIL_STACK_ERROR /* Position the file for /soft_one: replace link value offset in the scratch pad "32" by bad offset */ if(HDlseek(fd, (HDoff_t)1616, SEEK_SET) < 0) FAIL_STACK_ERROR /* Write the bad offset value to the file */ if(HDwrite(fd, &val, sizeof(val)) < 0) FAIL_STACK_ERROR /* Close the file */ if(HDclose(fd) < 0) FAIL_STACK_ERROR return EXIT_SUCCESS; error: H5E_BEGIN_TRY { H5Gclose(gid1); H5Gclose(gid2); H5Dclose(did); H5Sclose(sid); H5Fclose(fid); } H5E_END_TRY; return EXIT_FAILURE; } /* end main() */
/*------------------------------------------------------------------------- * Function: main * * Purpose: * * Return: Success: * * Failure: * * Programmer: Robb Matzke * Thursday, March 12, 1998 * * Modifications: * *------------------------------------------------------------------------- */ int main (void) { static hsize_t size[2] = {REQUEST_SIZE_X, REQUEST_SIZE_Y}; static unsigned nread = NREAD_REQUESTS, nwrite = NWRITE_REQUESTS; unsigned char *the_data = NULL; hid_t file, dset, file_space = -1; herr_t status; #ifdef H5_HAVE_GETRUSAGE struct rusage r_start, r_stop; #else struct timeval r_start, r_stop; #endif struct timeval t_start, t_stop; int fd; unsigned u; hssize_t n; off_t offset; hsize_t start[2]; hsize_t count[2]; #ifdef H5_HAVE_SYS_TIMEB struct _timeb *tbstart = malloc(sizeof(struct _timeb)); struct _timeb *tbstop = malloc(sizeof(struct _timeb)); #endif /* * The extra cast in the following statement is a bug workaround for the * Win32 version 5.0 compiler. * 1998-11-06 ptl */ printf ("I/O request size is %1.1fMB\n", (double)(hssize_t)(size[0]*size[1])/1024.0F*1024); /* Open the files */ file = H5Fcreate (HDF5_FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); assert (file>=0); fd = HDopen (RAW_FILE_NAME, O_RDWR|O_CREAT|O_TRUNC, 0666); assert (fd>=0); /* Create the dataset */ file_space = H5Screate_simple (2, size, size); assert(file_space >= 0); dset = H5Dcreate2(file, "dset", H5T_NATIVE_UCHAR, file_space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); assert(dset >= 0); the_data = (unsigned char *)malloc((size_t)(size[0] * size[1])); /* initial fill for lazy malloc */ HDmemset(the_data, 0xAA, (size_t)(size[0] * size[1])); /* Fill raw */ synchronize (); #ifdef H5_HAVE_GETRUSAGE HDgetrusage(RUSAGE_SELF, &r_start); #endif #ifdef H5_HAVE_GETTIMEOFDAY HDgettimeofday(&t_start, NULL); #else #ifdef H5_HAVE_SYS_TIMEB _ftime(tbstart); #endif #endif fprintf (stderr, HEADING, "fill raw"); for(u = 0; u < nwrite; u++) { putc (PROGRESS, stderr); HDfflush(stderr); HDmemset(the_data, 0xAA, (size_t)(size[0]*size[1])); } #ifdef H5_HAVE_GETRUSAGE HDgetrusage(RUSAGE_SELF, &r_stop); #endif #ifdef H5_HAVE_GETTIMEOFDAY HDgettimeofday(&t_stop, NULL); #else #ifdef H5_HAVE_SYS_TIMEB _ftime(tbstop); t_start.tv_sec = tbstart->time; t_start.tv_usec = tbstart->millitm; t_stop.tv_sec = tbstop->time; t_stop.tv_usec = tbstop->millitm; #endif #endif putc ('\n', stderr); print_stats ("fill raw", &r_start, &r_stop, &t_start, &t_stop, (size_t)(nread*size[0]*size[1])); /* Fill hdf5 */ synchronize (); #ifdef H5_HAVE_GETRUSAGE HDgetrusage(RUSAGE_SELF, &r_start); #endif #ifdef H5_HAVE_GETTIMEOFDAY HDgettimeofday(&t_start, NULL); #else #ifdef H5_HAVE_SYS_TIMEB _ftime(tbstart); #endif #endif fprintf (stderr, HEADING, "fill hdf5"); for(u = 0; u < nread; u++) { putc (PROGRESS, stderr); HDfflush(stderr); status = H5Dread (dset, H5T_NATIVE_UCHAR, file_space, file_space, H5P_DEFAULT, the_data); assert (status>=0); } #ifdef H5_HAVE_GETRUSAGE HDgetrusage(RUSAGE_SELF, &r_stop); #endif #ifdef H5_HAVE_GETTIMEOFDAY HDgettimeofday(&t_stop, NULL); #else #ifdef H5_HAVE_SYS_TIMEB _ftime(tbstop); t_start.tv_sec = tbstart->time; t_start.tv_usec = tbstart->millitm; t_stop.tv_sec = tbstop->time; t_stop.tv_usec = tbstop->millitm; #endif #endif putc ('\n', stderr); print_stats ("fill hdf5", &r_start, &r_stop, &t_start, &t_stop, (size_t)(nread*size[0]*size[1])); /* Write the raw dataset */ synchronize (); #ifdef H5_HAVE_GETRUSAGE HDgetrusage(RUSAGE_SELF, &r_start); #endif #ifdef H5_HAVE_GETTIMEOFDAY HDgettimeofday(&t_start, NULL); #else #ifdef H5_HAVE_SYS_TIMEB _ftime(tbstart); #endif #endif fprintf (stderr, HEADING, "out raw"); for(u = 0; u < nwrite; u++) { putc (PROGRESS, stderr); HDfflush(stderr); offset = HDlseek (fd, (off_t)0, SEEK_SET); assert (0==offset); n = HDwrite (fd, the_data, (size_t)(size[0]*size[1])); assert (n>=0 && (size_t)n==size[0]*size[1]); } #ifdef H5_HAVE_GETRUSAGE HDgetrusage(RUSAGE_SELF, &r_stop); #endif #ifdef H5_HAVE_GETTIMEOFDAY HDgettimeofday(&t_stop, NULL); #else #ifdef H5_HAVE_SYS_TIMEB _ftime(tbstop); t_start.tv_sec = tbstart->time; t_start.tv_usec = tbstart->millitm; t_stop.tv_sec = tbstop->time; t_stop.tv_usec = tbstop->millitm; #endif #endif putc ('\n', stderr); print_stats ("out raw", &r_start, &r_stop, &t_start, &t_stop, (size_t)(nread*size[0]*size[1])); /* Write the hdf5 dataset */ synchronize (); #ifdef H5_HAVE_GETRUSAGE HDgetrusage(RUSAGE_SELF, &r_start); #endif #ifdef H5_HAVE_GETTIMEOFDAY HDgettimeofday(&t_start, NULL); #else #ifdef H5_HAVE_SYS_TIMEB _ftime(tbstart); #endif #endif fprintf (stderr, HEADING, "out hdf5"); for(u = 0; u < nwrite; u++) { putc (PROGRESS, stderr); HDfflush(stderr); status = H5Dwrite (dset, H5T_NATIVE_UCHAR, H5S_ALL, H5S_ALL, H5P_DEFAULT, the_data); assert (status>=0); } #ifdef H5_HAVE_GETRUSAGE HDgetrusage(RUSAGE_SELF, &r_stop); #endif #ifdef H5_HAVE_GETTIMEOFDAY HDgettimeofday(&t_stop, NULL); #else #ifdef H5_HAVE_SYS_TIMEB _ftime(tbstop); t_start.tv_sec = tbstart->time; t_start.tv_usec = tbstart->millitm; t_stop.tv_sec = tbstop->time; t_stop.tv_usec = tbstop->millitm; #endif #endif putc ('\n', stderr); print_stats ("out hdf5", &r_start, &r_stop, &t_start, &t_stop, (size_t)(nread*size[0]*size[1])); /* Read the raw dataset */ synchronize (); #ifdef H5_HAVE_GETRUSAGE HDgetrusage(RUSAGE_SELF, &r_start); #endif #ifdef H5_HAVE_GETTIMEOFDAY HDgettimeofday(&t_start, NULL); #else #ifdef H5_HAVE_SYS_TIMEB _ftime(tbstart); #endif #endif fprintf (stderr, HEADING, "in raw"); for(u = 0; u < nread; u++) { putc (PROGRESS, stderr); HDfflush(stderr); offset = HDlseek (fd, (off_t)0, SEEK_SET); assert (0==offset); n = HDread (fd, the_data, (size_t)(size[0]*size[1])); assert (n>=0 && (size_t)n==size[0]*size[1]); } #ifdef H5_HAVE_GETRUSAGE HDgetrusage(RUSAGE_SELF, &r_stop); #endif #ifdef H5_HAVE_GETTIMEOFDAY HDgettimeofday(&t_stop, NULL); #else #ifdef H5_HAVE_SYS_TIMEB _ftime(tbstop); t_start.tv_sec = tbstart->time; t_start.tv_usec = tbstart->millitm; t_stop.tv_sec = tbstop->time; t_stop.tv_usec = tbstop->millitm; #endif #endif putc ('\n', stderr); print_stats ("in raw", &r_start, &r_stop, &t_start, &t_stop, (size_t)(nread*size[0]*size[1])); /* Read the hdf5 dataset */ synchronize (); #ifdef H5_HAVE_GETRUSAGE HDgetrusage(RUSAGE_SELF, &r_start); #endif #ifdef H5_HAVE_GETTIMEOFDAY HDgettimeofday(&t_start, NULL); #else #ifdef H5_HAVE_SYS_TIMEB _ftime(tbstart); #endif #endif fprintf (stderr, HEADING, "in hdf5"); for(u = 0; u < nread; u++) { putc (PROGRESS, stderr); HDfflush(stderr); status = H5Dread (dset, H5T_NATIVE_UCHAR, file_space, file_space, H5P_DEFAULT, the_data); assert (status>=0); } #ifdef H5_HAVE_GETRUSAGE HDgetrusage(RUSAGE_SELF, &r_stop); #endif #ifdef H5_HAVE_GETTIMEOFDAY HDgettimeofday(&t_stop, NULL); #else #ifdef H5_HAVE_SYS_TIMEB _ftime(tbstop); t_start.tv_sec = tbstart->time; t_start.tv_usec = tbstart->millitm; t_stop.tv_sec = tbstop->time; t_stop.tv_usec = tbstop->millitm; #endif #endif putc ('\n', stderr); print_stats ("in hdf5", &r_start, &r_stop, &t_start, &t_stop, (size_t)(nread*size[0]*size[1])); /* Read hyperslab */ assert (size[0]>20 && size[1]>20); start[0] = start[1] = 10; count[0] = count[1] = size[0]-20; status = H5Sselect_hyperslab (file_space, H5S_SELECT_SET, start, NULL, count, NULL); assert (status>=0); synchronize (); #ifdef H5_HAVE_GETRUSAGE HDgetrusage(RUSAGE_SELF, &r_start); #endif #ifdef H5_HAVE_GETTIMEOFDAY HDgettimeofday(&t_start, NULL); #else #ifdef H5_HAVE_SYS_TIMEB _ftime(tbstart); #endif #endif fprintf (stderr, HEADING, "in hdf5 partial"); for(u = 0; u < nread; u++) { putc (PROGRESS, stderr); HDfflush(stderr); status = H5Dread (dset, H5T_NATIVE_UCHAR, file_space, file_space, H5P_DEFAULT, the_data); assert (status>=0); } #ifdef H5_HAVE_GETRUSAGE HDgetrusage(RUSAGE_SELF, &r_stop); #endif #ifdef H5_HAVE_GETTIMEOFDAY HDgettimeofday(&t_stop, NULL); #else #ifdef H5_HAVE_SYS_TIMEB _ftime(tbstop); t_start.tv_sec = tbstart->time; t_start.tv_usec = tbstart->millitm; t_stop.tv_sec = tbstop->time; t_stop.tv_usec = tbstop->millitm; #endif #endif putc('\n', stderr); print_stats("in hdf5 partial", &r_start, &r_stop, &t_start, &t_stop, (size_t)(nread*count[0]*count[1])); /* Close everything */ HDclose(fd); H5Dclose(dset); H5Sclose(file_space); H5Fclose(file); free(the_data); return 0; }
/*------------------------------------------------------------------------- * Function: main * * Purpose: To create HDF5 files with non-zero status_flags in the superblock * via flushing and exiting without closing the library. * * Due to file locking, status_flag in the superblock will be * nonzero after H5Fcreate. The library will clear status_flags * on file closing. This program, after "H5Fcreate" the files, * exits without going through library closing. Thus, status_flags * for these files are not cleared and users cannot open them. * * These files are used by "h5clear" to see if the tool clears * status_flags properly so users can open the files afterwards. * * Return: Success: 0 * Failure: 1 * * Programmer: Vailin Choi; July 2013 * *------------------------------------------------------------------------- */ int main(void) { hid_t fid; /* File ID */ hid_t fapl, new_fapl; /* File access property lists */ char fname[512]; /* File name */ hbool_t new_format; /* To use latest library format or not */ int fd; /* File descriptor */ uint8_t super_vers; /* Superblock version */ ssize_t bytes_written; /* The # of bytes written to the file */ /* Create a copy of the file access property list */ if((fapl = H5Pcreate(H5P_FILE_ACCESS)) < 0) goto error; /* Copy the file access property list */ if((new_fapl = H5Pcopy(fapl)) < 0) goto error; /* Set to latest library format */ if(H5Pset_libver_bounds(new_fapl, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST) < 0) goto error; /* Create file with/without latest library format */ for(new_format = FALSE; new_format <= TRUE; new_format++) { hid_t fapl2, my_fapl; /* File access property lists */ /* Set to use the appropriate file access property list */ if(new_format) fapl2 = new_fapl; else fapl2 = fapl; /* * Create a sec2 file */ if((my_fapl = H5Pcopy(fapl2)) < 0) goto error; /* Create the file */ sprintf(fname, "%s%s", new_format? "latest_":"", FILENAME[0]); if((fid = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, my_fapl)) < 0) goto error; /* Flush the file */ if(H5Fflush(fid, H5F_SCOPE_GLOBAL) < 0) goto error; /* Close the property list */ if(H5Pclose(my_fapl) < 0) goto error; /* * Create a core file */ /* Create a copy of file access property list */ if((my_fapl = H5Pcopy(fapl2)) < 0) goto error; /* Setup the fapl for the family file driver */ if(H5Pset_fapl_core(my_fapl, (size_t)CORE_INCREMENT, TRUE) < 0) goto error; /* Create the file */ sprintf(fname, "%s%s", new_format? "latest_":"", FILENAME[1]); if((fid = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, my_fapl)) < 0) goto error; /* Flush the file */ if(H5Fflush(fid, H5F_SCOPE_GLOBAL) < 0) goto error; /* Close the property list */ if(H5Pclose(my_fapl) < 0) goto error; /* * Create a family file */ /* Create a copy of file access property list */ if((my_fapl = H5Pcopy(fapl2)) < 0) goto error; /* Setup the fapl for the family file driver */ if(H5Pset_fapl_family(my_fapl, (hsize_t)FAMILY_SIZE, H5P_DEFAULT) < 0) goto error; /* Create the file */ sprintf(fname, "%s%s", new_format? "latest_":"", FILENAME[2]); if((fid = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, my_fapl)) < 0) goto error; /* Flush the file */ if(H5Fflush(fid, H5F_SCOPE_GLOBAL) < 0) goto error; /* Close the property list */ if(H5Pclose(my_fapl) < 0) goto error; /* * Create a split file */ /* Create a copy of file access property list */ my_fapl = H5Pcopy(fapl2); /* Setup the fapl for the split file driver */ H5Pset_fapl_split(my_fapl, "-m.h5", H5P_DEFAULT, "-r.h5", H5P_DEFAULT); /* Create the file */ sprintf(fname, "%s%s", new_format? "latest_":"", FILENAME[3]); if((fid = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, my_fapl)) < 0) goto error; /* Flush the file */ if(H5Fflush(fid, H5F_SCOPE_GLOBAL) < 0) goto error; /* Close the property list */ if(H5Pclose(my_fapl) < 0) goto error; /* * Create a sec2 file but change its superblock version # */ /* Create a copy of file access property list */ if((my_fapl = H5Pcopy(fapl2)) < 0) goto error; /* Create the file */ sprintf(fname, "%s%s", new_format? "latest_":"", FILENAME[4]); if((fid = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, my_fapl)) < 0) goto error; /* Flush the file */ if(H5Fflush(fid, H5F_SCOPE_GLOBAL) < 0) goto error; /* Close the property list */ if(H5Pclose(my_fapl) < 0) goto error; /* Open the test file via system call "open" */ if((fd = HDopen(fname, O_RDWR, 0666)) < 0) { HDfprintf(stdout, "cannot open the file\n"); goto error; } /* Position the file to superblock version via system call "lseek" */ if(HDlseek(fd, (off_t)SUPER_VERS_OFF, SEEK_SET) < 0) { HDfprintf(stdout, "cannot lseek the file superblock version\n"); goto error; } /* Change to an incorrect superblock version */ super_vers = SUPER_VERS_LATEST + 1; /* Write to the file via system call "write" */ if((bytes_written = HDwrite(fd, &super_vers, (size_t)SUPER_VERS_SIZE)) < 0) { HDfprintf(stdout, "cannot write to the file with incorrect superblock version\n"); goto error; } /* Close the file via system call "close" */ if(HDclose(fd) < 0) { HDfprintf(stdout, "cannot close the file\n"); goto error; } } /* end for */ /* Close the property lists */ if(H5Pclose(fapl) < 0) goto error; if(H5Pclose(new_fapl) < 0) goto error; fflush(stdout); fflush(stderr); /* Not going through library closing by calling _exit(0) with success */ HD_exit(0); error: /* Exit with failure */ HD_exit(1); }
/*------------------------------------------------------------------------- * Function: test * * Purpose: The guts of the test * * Return: Success: 0 * * Failure: number of errors * * Programmer: Robb Matzke * Wednesday, September 30, 1998 * * Modifications: * *------------------------------------------------------------------------- */ static int test(fill_t fill_style, const double splits[], hbool_t verbose, hbool_t use_rdcc) { hid_t file = (-1), fapl = (-1), dcpl = (-1), xfer = (-1), mspace = (-1), fspace = (-1), dset = (-1); hsize_t ch_size[1] = {1}; /*chunk size */ hsize_t cur_size[1] = {1000}; /*current dataset size */ hsize_t max_size[1] = {H5S_UNLIMITED}; /*maximum dataset size */ hsize_t hs_start[1]; /*hyperslab start offset*/ hsize_t hs_count[1] = {1}; /*hyperslab nelmts */ int fd = (-1); /*h5 file direct */ int *had = NULL; /*for random filling */ const char *sname=NULL; /*fill style nam */ int mdc_nelmts; /*num meta objs to cache*/ hsize_t i; int j; h5_stat_t sb; if((fapl = H5Pcreate(H5P_FILE_ACCESS)) < 0) goto error; if(!use_rdcc) { if(H5Pget_cache(fapl, &mdc_nelmts, NULL, NULL, NULL) < 0) goto error; if(H5Pset_cache(fapl, mdc_nelmts, 0, 0, 0.0F) < 0) goto error; } if((file = H5Fcreate(FILE_NAME_1, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0) goto error; if((dcpl = H5Pcreate(H5P_DATASET_CREATE)) < 0) goto error; if(H5Pset_chunk(dcpl, 1, ch_size) < 0) goto error; if((xfer = H5Pcreate(H5P_DATASET_XFER)) < 0) goto error; if(H5Pset_btree_ratios(xfer, splits[0], splits[1], splits[2]) < 0) goto error; if((fspace = H5Screate_simple(1, cur_size, max_size)) < 0) goto error; if((mspace = H5Screate_simple(1, ch_size, ch_size)) < 0) goto error; if((dset = H5Dcreate2(file, "chunked", H5T_NATIVE_INT, fspace, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0) goto error; if ((fd=HDopen(FILE_NAME_1, O_RDONLY, 0666)) < 0) goto error; if(FILL_RANDOM==fill_style) had = calloc((size_t)cur_size[0], sizeof(int)); for (i=1; i<=cur_size[0]; i++) { /* Decide which chunk to write to */ switch (fill_style) { case FILL_FORWARD: hs_start[0] = i-1; break; case FILL_REVERSE: hs_start[0] = cur_size[0]-i; break; case FILL_INWARD: hs_start[0] = i%2 ? i/2 : cur_size[0]-i/2; break; case FILL_OUTWARD: j = (int)(cur_size[0]-i)+1; hs_start[0] = j%2 ? j/2 : (hssize_t)cur_size[0]-j/2; break; case FILL_RANDOM: for (j=HDrand()%(int)cur_size[0]; had[j]; j=(j+1)%(int)cur_size[0]) /*void*/; hs_start[0] = j; had[j] = 1; break; case FILL_ALL: abort(); default: /* unknown request */ HDfprintf(stderr, "Unknown fill style\n"); goto error; break; } /* Write the chunk */ if (H5Sselect_hyperslab(fspace, H5S_SELECT_SET, hs_start, NULL, hs_count, NULL) < 0) goto error; if (H5Dwrite(dset, H5T_NATIVE_INT, mspace, fspace, xfer, &i) < 0) { goto error; } /* Determine overhead */ if (verbose) { if (H5Fflush(file, H5F_SCOPE_LOCAL) < 0) goto error; if (HDfstat(fd, &sb) < 0) goto error; /* * The extra cast in the following statement is a bug workaround * for the Win32 version 5.0 compiler. * 1998-11-06 ptl */ printf("%4lu %8.3f ***\n", (unsigned long)i, (double)(hssize_t)(sb.st_size-i*sizeof(int))/(hssize_t)i); } } if(had) { free(had); had = NULL; } /* end if */ H5Dclose(dset); H5Sclose(mspace); H5Sclose(fspace); H5Pclose(dcpl); H5Pclose(xfer); H5Fclose(file); if (!verbose) { switch (fill_style) { case FILL_FORWARD: sname = "forward"; break; case FILL_REVERSE: sname = "reverse"; break; case FILL_INWARD: sname = "inward"; break; case FILL_OUTWARD: sname = "outward"; break; case FILL_RANDOM: sname = "random"; break; case FILL_ALL: abort(); default: /* unknown request */ HDfprintf(stderr, "Unknown fill style\n"); goto error; break; } if (HDfstat(fd, &sb) < 0) goto error; printf("%-7s %8.3f\n", sname, (double)(hssize_t)(sb.st_size-cur_size[0]*sizeof(int))/ (hssize_t)cur_size[0]); } HDclose(fd); return 0; error: H5Dclose(dset); H5Sclose(mspace); H5Sclose(fspace); H5Pclose(dcpl); H5Pclose(xfer); H5Fclose(file); if(had) free(had); HDclose(fd); return 1; }