Пример #1
0
/*
 *  Copy 'how_much' bytes from the input file to the output file,
 *  starting at byte 'where' in the input file.
 *
 *  Returns 0 on success, -1 on failure.
 */
herr_t
copy_to_file( int infid, int ofid, ssize_t where, ssize_t how_much )
{
    static char buf[COPY_BUF_SIZE];
    off_t to;
    off_t from;
    ssize_t nchars = -1;
    ssize_t wnchars = -1;
    herr_t ret_value = 0;

    /* nothing to copy */
    if(how_much <= 0)
        goto done;

    from = where;
    to = 0;

    while(how_much > 0) {
        /* Seek to correct position in input file */
        HDlseek(infid,from,SEEK_SET);

        /* Read data to buffer */
        if (how_much > COPY_BUF_SIZE)
            nchars = HDread(infid,buf,(unsigned)COPY_BUF_SIZE);
        else
            nchars = HDread(infid,buf,(unsigned)how_much);
        if(nchars < 0) {
            ret_value = -1;
            goto done;
        } /* end if */

        /* Seek to correct position in output file */
        HDlseek(ofid,to,SEEK_SET);

        /* Update positions/size */
        how_much -= nchars;
        from += nchars;
        to += nchars;

        /* Write nchars bytes to output file */
        wnchars = nchars;
        while(nchars > 0) {
            wnchars = HDwrite(ofid,buf,(unsigned)nchars);
            if(wnchars < 0) {
                ret_value = -1;
                goto done;
            } /* end if */
            nchars -= wnchars;
        } /* end while */
    } /* end while */

done:
    return ret_value;
}  /* end copy_to_file */
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;
}
Пример #3
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 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;
}
Пример #4
0
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;
}
Пример #5
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);
}
Пример #6
0
int write_wo_file(void)
{
    int blkaddr;
    int blkaddr_old=0;
    int i;
    char buffer[BLOCKSIZE_DFT];
    int  ret_code;
    

    /* write block 1, 2, ... */
    for (i=1; i<nlinkedblock_g; i++){
	/* calculate where to write this block */
	blkaddr = i*part_size_g + i;
	/* store old block address in byte 0-3 */
	HDmemcpy(&buffer[0], &blkaddr_old, sizeof(blkaddr_old));
	/* fill the rest with the lowest byte of i */
	HDmemset(&buffer[4], i & 0xff, (size_t) (BLOCKSIZE_DFT-4));
	/* write the block */
#ifdef DEBUG
	printf("writing block at %d\n", blkaddr);
#endif
	HDlseek(write_fd_g, (HDoff_t)blkaddr, SEEK_SET);
	if ((ret_code=HDwrite(write_fd_g, buffer, (size_t)blocksize_g)) != blocksize_g){
	    printf("blkaddr write failed in partition %d\n", i);
	    return -1;
	}
	blkaddr_old = blkaddr;
    }
    /* write the last blkaddr in partition 0 */
    HDlseek(write_fd_g, (HDoff_t)0, SEEK_SET);
    if ((ret_code=HDwrite(write_fd_g, &blkaddr_old, (size_t)sizeof(blkaddr_old))) != sizeof(blkaddr_old)){
	printf("blkaddr write failed in partition %d\n", 0);
	return -1;
    }

    /* all writes done. return succeess. */
    printf("wrote %d blocks\n", nlinkedblock_g);
    return 0;
}
Пример #7
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
}
Пример #8
0
/*
 *  Write zeroes to fill the file from 'where' to 512, 1024, etc. bytes.
 *
 *  Returns the size of the padded file.
 */
hsize_t
write_pad(int ofile, hsize_t where)
{
    unsigned int i;
    char buf[1];
    hsize_t psize;

    buf[0] = '\0';

    HDlseek(ofile, (off_t) where, SEEK_SET);

    psize = compute_user_block_size (where);
    psize -= where;

    for(i = 0; i < psize; i++)
        HDwrite (ofile, buf, 1);

    return(where + psize);  /* the new size of the file. */
}
Пример #9
0
/*-------------------------------------------------------------------------
 * 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;
}
Пример #10
0
/*-------------------------------------------------------------------------
 * Function:    copy_some_to_file
 *
 * Purpose:     Copy part of the input file to output.
 *      infid: fd of file to read
 *      outfid: fd of file to write
 *      startin: offset of where to read from infid
 *      startout: offset of where to write to outfid
 *      limit: bytes to read/write
 *
 *    If limit is < 0, the entire input file is copied.
 *
 *    Note: this routine can be used to copy within
 *    the same file, i.e., infid and outfid can be the
 *    same file.
 *
 * Return:      Success:    last byte written in the output.
 *              Failure:    Exits program with EXIT_FAILURE value.
 *
 *-------------------------------------------------------------------------
 */
hsize_t
copy_some_to_file(int infid, int outfid, hsize_t startin, hsize_t startout,
       ssize_t limit)
{
    char buf[1024];
    h5_stat_t sbuf;
    int res;
    ssize_t tot = 0;
    ssize_t howmuch = 0;
    ssize_t nchars = -1;
    ssize_t to;
    ssize_t from;
    ssize_t toend;
    ssize_t fromend;

    if(startin > startout) {
        /* this case is prohibited */
        error_msg("copy_some_to_file: panic: startin > startout?\n");
        exit (EXIT_FAILURE);
    } /* end if */

    if(limit < 0) {
        res = HDfstat(infid, &sbuf);
        if(res < 0) {
            error_msg("Can't stat file \n");
            HDexit(EXIT_FAILURE);
        } /* end if */

        howmuch = (ssize_t)sbuf.st_size;
    } else {
        howmuch = limit;
    } /* end if */

    if(0 == howmuch)
        return 0;

    toend = (ssize_t) startout + howmuch;
    fromend = (ssize_t) startin + howmuch;

    if (howmuch > 512) {
        to = toend - 512;
        from = fromend - 512;
    } else {
        to = toend - howmuch;
        from = fromend - howmuch;
    } /* end if */

    while (howmuch > 0) {
        HDlseek(outfid, (off_t) to, SEEK_SET);
        HDlseek(infid, (off_t) from, SEEK_SET);

        if (howmuch > 512) {
            nchars = HDread(infid, buf, (unsigned) 512);
        } else {
            nchars = HDread(infid, buf, (unsigned)howmuch);
        } /* end if */

        if (nchars <= 0) {
            error_msg("Read error \n");
            HDexit(EXIT_FAILURE);
        } /* end if */

        if(HDwrite (outfid, buf, (unsigned) nchars) < 0) {
            error_msg("Write error \n");
            HDexit(EXIT_FAILURE);
        }

        tot += nchars;
        howmuch -= nchars;
        if(howmuch > 512) {
            to -= nchars;
            from -= nchars;
        } else {
            to -= howmuch;
            from -= howmuch;
        } /* end if */
    } /* end while */

    return (hsize_t)tot + (hsize_t)startout;
} /* end copy_some_to_file() */
Пример #11
0
/*-------------------------------------------------------------------------
 * Function:    copy_some_to_file
 *
 * Purpose:     Copy part of the input file to output.
 *		  infid: fd of file to read
 *		  outfid: fd of file to write
 *		  startin: offset of where to read from infid
 *		  startout: offset of where to write to outfid
 *		  limit: bytes to read/write
 *
 *		If limit is < 0, the entire input file is copied.
 *
 *		Note: this routine can be used to copy within
 *		the same file, i.e., infid and outfid can be the
 *		same file.
 *
 * Return:      Success:    last byte written in the output.
 *              Failure:    Exits program with EXIT_FAILURE value.
 *
 * Programmer:
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
hsize_t
copy_some_to_file (int infid, int outfid, hsize_t startin, hsize_t startout,
		   ssize_t limit)
{
  char buf[1024];
  struct stat sbuf;
  int res;
  ssize_t tot = 0;
  ssize_t howmuch = 0;
  ssize_t nchars = -1;
/* used in assertion check
  ssize_t ncw = -1;
*/
  ssize_t to;
  ssize_t from;
  ssize_t toend;
  ssize_t fromend;

  if (startin > startout)
    {
      /* this case is prohibited */
      error_msg (progname, "copy_some_to_file: panic: startin > startout?\n");
      exit (EXIT_FAILURE);
    }

  if (limit < 0)
    {
      res = fstat (infid, &sbuf);

      if (res < 0)
	{
	  error_msg (progname, "Can't stat file \n");
	  exit (EXIT_FAILURE);
	}

      howmuch = sbuf.st_size;
    }
  else
    {
      howmuch = limit;
    }

  if (howmuch == 0)
    {
      return 0;
    }

  /* assert (howmuch > 0) */

  toend = (ssize_t) startout + howmuch;
  fromend = (ssize_t) startin + howmuch;

  if (howmuch > 512)
    {
      to = toend - 512;
      from = fromend - 512;
    }
  else
    {
      to = toend - howmuch;
      from = fromend - howmuch;
    }

  while (howmuch > 0)
    {
      HDlseek (outfid, (off_t) to, SEEK_SET);
      HDlseek (infid, (off_t) from, SEEK_SET);

      if (howmuch > 512)
	{
	  nchars = HDread (infid, buf, (unsigned) 512);
	}
      else
	{
	  nchars = HDread (infid, buf, (unsigned)howmuch);
	}

      if (nchars <= 0)
	{
	  printf ("huh? \n");
	  exit (1);
	}
      /*ncw = */ HDwrite (outfid, buf, (unsigned) nchars);

      /* assert (ncw == nchars) */

      tot += nchars;
      howmuch -= nchars;
      if (howmuch > 512)
	{
	  to -= nchars;
	  from -= nchars;
	}
      else
	{
	  to -= howmuch;
	  from -= howmuch;
	}
    }

  /* assert howmuch == 0 */
  /* assert tot == limit */

  return ((hsize_t) tot + (hsize_t) startout);
}
Пример #12
0
/*-------------------------------------------------------------------------
 * 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 */
Пример #13
0
/*-------------------------------------------------------------------------
 * 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;
}
Пример #14
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);
}
Пример #15
0
/*-------------------------------------------------------------------------
 * 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() */