コード例 #1
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;
}
コード例 #2
0
ファイル: h5jamgentest.c プロジェクト: ngcurrier/ProteusCFD
static void
create_textfile(const char *name, size_t size)
{
    char *buf;
    int fd;
    size_t i;
    char *bp;
    ssize_t nbytes;

    fd = HDcreat(name,0777);
    HDassert(fd >= 0);
    buf = (char *)HDcalloc(size, (size_t)1);
    HDassert(buf);

    /* fill buf with pattern */
    bp = buf;
    for(i = 0; i < size; i++)
        *bp++ = pattern[i % 10];

    nbytes = HDwrite(fd, buf, size);
    HDassert(nbytes >= 0);

    HDfree(buf);

    HDclose(fd);
}
コード例 #3
0
ファイル: gen_plist.c プロジェクト: Hulalazz/rnnlib
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;
}
コード例 #4
0
ファイル: h5test.c プロジェクト: EgoIncarnate/appleseed
/*-------------------------------------------------------------------------
 * 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;
}
コード例 #5
0
ファイル: h5unjam.c プロジェクト: ArielleBassanelli/gempak
/*
 *  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 */
コード例 #6
0
ファイル: big.c プロジェクト: einon/affymetrix-power-tools
/*-------------------------------------------------------------------------
 * 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);
}
コード例 #7
0
ファイル: twriteorder.c プロジェクト: FilipeMaia/hdf5
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;
}
コード例 #8
0
ファイル: zip_perf.c プロジェクト: FilipeMaia/hdf5
static void
write_file(Bytef *source, uLongf sourceLen)
{
    Bytef *d_ptr, *dest;
    uLongf d_len, destLen;
    struct timeval timer_start, timer_stop;

    /* destination buffer needs to be at least 0.1% larger than sourceLen
     * plus 12 bytes */
    destLen = (uLongf)((double)sourceLen + ((double)sourceLen * 0.1)) + 12;
    dest = (Bytef *)HDmalloc(destLen);

    if (!dest)
        error("out of memory");

    HDgettimeofday(&timer_start, NULL);
    compress_buffer(dest, &destLen, source, sourceLen);
    HDgettimeofday(&timer_stop, NULL);

    compression_time += ((double)timer_stop.tv_sec +
                            ((double)timer_stop.tv_usec) / MICROSECOND) -
                        ((double)timer_start.tv_sec +
                            ((double)timer_start.tv_usec) / MICROSECOND);

    if (report_once_flag) {
        HDfprintf(stdout, "\tCompression Ratio: %g\n", ((double)destLen) / (double)sourceLen);
        report_once_flag = 0;
    }

    d_ptr = dest;
    d_len = destLen;

    /* loop to make sure we write everything out that we want to write */
    for (;;) {
        int rc = (int)HDwrite(output, d_ptr, (size_t)d_len);

        if (rc == -1)
            error(HDstrerror(errno));

        if (rc == (int)d_len)
            break;

        d_len -= rc;
        d_ptr += rc;
    }

    HDfree(dest);
}
コード例 #9
0
ファイル: istore.c プロジェクト: ElaraFX/hdf5
/*-------------------------------------------------------------------------
 * 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
}
コード例 #10
0
ファイル: h5jam.c プロジェクト: ElaraFX/hdf5
/*
 *  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. */
}
コード例 #11
0
ファイル: twriteorder.c プロジェクト: FilipeMaia/hdf5
/* 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;
}
コード例 #12
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;
}
コード例 #13
0
ファイル: h5test.c プロジェクト: Hulalazz/rnnlib
/*-------------------------------------------------------------------------
 * 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;
}
コード例 #14
0
ファイル: h5jamgentest.c プロジェクト: ngcurrier/ProteusCFD
/* not used yet */
void
create_binfile(char *name, off_t size)
{
    char *buf;
    int fd;
    int i;
    char *bp;

    fd = creat(name,0777);
    HDassert(fd >= 0);

    buf = HDcalloc(size,1);
    HDassert(buf);

    /* fill buf with pattern */
    bp = buf;
    for (i = 0; i < size; i++)
        *bp++ = (char) i & 0xff;

    HDwrite(fd,buf,size);

    HDclose(fd);
}
コード例 #15
0
ファイル: gen_plist.c プロジェクト: Starlink/hdf5
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;
}
コード例 #16
0
ファイル: h5jamgentest.c プロジェクト: ngcurrier/ProteusCFD
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);
  }
}
コード例 #17
0
ファイル: getub.c プロジェクト: FilipeMaia/hdf5
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);
}
コード例 #18
0
ファイル: external.c プロジェクト: EgoIncarnate/appleseed
/*-------------------------------------------------------------------------
 * 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;
}
コード例 #19
0
ファイル: external.c プロジェクト: EgoIncarnate/appleseed
/*-------------------------------------------------------------------------
 * 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;
}
コード例 #20
0
ファイル: h5jam.c プロジェクト: ElaraFX/hdf5
/*-------------------------------------------------------------------------
 * 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() */
コード例 #21
0
ファイル: zip_perf.c プロジェクト: FilipeMaia/hdf5
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);
    }
}
コード例 #22
0
ファイル: gen_bad_offset.c プロジェクト: Starlink/hdf5
/*-------------------------------------------------------------------------
 * 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() */
コード例 #23
0
ファイル: h5jam.c プロジェクト: MattNapsAlot/rHDF5
/*-------------------------------------------------------------------------
 * 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);
}
コード例 #24
0
ファイル: h5clear_gentest.c プロジェクト: FilipeMaia/hdf5
/*-------------------------------------------------------------------------
 * 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);
}
コード例 #25
0
ファイル: iopipe.c プロジェクト: ElaraFX/hdf5
/*-------------------------------------------------------------------------
 * 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;
}
コード例 #26
0
ファイル: h5repart.c プロジェクト: Starlink/hdf5
/*-------------------------------------------------------------------------
 * 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 */