summaryrefslogtreecommitdiffstats
path: root/test/accum_swmr_reader.c
blob: 957ab1435fba65d7c518277a6e80d808bc197d80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "h5test.h"

#define H5F_FRIEND  /*suppress error about including H5Fpkg	  */
#define H5FD_FRIEND /*suppress error about including H5FDpkg	  */
#define H5FD_TESTING

#include "H5Fpkg.h"
#include "H5FDpkg.h"

#include "H5CXprivate.h" /* API Contexts                         */
#include "H5Iprivate.h"
#include "H5VLprivate.h" /* Virtual Object Layer                     */

/* Filename: this is the same as the define in accum.c used by test_swmr_write_big() */
const char *FILENAME[] = {"accum", "accum_swmr_big", NULL};

/*-------------------------------------------------------------------------
 * Function:    main
 *
 * Purpose:     This is the reader forked/execved by "test_swmr_write_big()"
 *		        test in accum.c.  The reader reads at address 1024 from the file
 *		        and verifies that the metadata in the accumulator at address
 * 		        1024 does get written to disk.
 *
 * Return:      Success: EXIT_SUCCESS
 *              Failure: EXIT_FAILURE
 *
 * Programmer:  Vailin Choi; June 2013
 *
 *-------------------------------------------------------------------------
 */
int
main(void)
{
    hid_t    fid  = -1;   /* File ID */
    hid_t    fapl = -1;   /* file access property list ID */
    H5F_t   *f    = NULL; /* File pointer */
    char     filename[1024];
    unsigned u;                      /* Local index variable */
    uint8_t  rbuf[1024];             /* Buffer for reading */
    uint8_t  buf[1024];              /* Buffer for holding the expected data */
    char    *driver         = NULL;  /* VFD string (from env variable) */
    hbool_t  api_ctx_pushed = FALSE; /* Whether API context pushed */

    /* Skip this test if SWMR I/O is not supported for the VFD specified
     * by the environment variable.
     */
    driver = HDgetenv(HDF5_DRIVER);
    if (!H5FD__supports_swmr_test(driver))
        return EXIT_SUCCESS;

    /* Initialize buffers */
    for (u = 0; u < 1024; u++) {
        rbuf[u] = 0; /* The buffer for reading */
        buf[u]  = 1; /* The expected data should be all 1s */
    }

    if ((fapl = h5_fileaccess()) < 0)
        FAIL_STACK_ERROR;
    h5_fixname(FILENAME[1], fapl, filename, sizeof filename);

    /* Open the file with SWMR_READ */
    if ((fid = H5Fopen(filename, H5F_ACC_RDONLY | H5F_ACC_SWMR_READ, fapl)) < 0)
        FAIL_STACK_ERROR;

    /* Push API context */
    if (H5CX_push() < 0)
        FAIL_STACK_ERROR;
    api_ctx_pushed = TRUE;

    /* Get H5F_t * to internal file structure */
    if (NULL == (f = (H5F_t *)H5VL_object(fid)))
        FAIL_STACK_ERROR;

    /* Should read in [1024, 2024] with buf data */
    if (H5F_block_read(f, H5FD_MEM_DEFAULT, (haddr_t)1024, (size_t)1024, rbuf) < 0)
        FAIL_STACK_ERROR;

    /* Verify the data read is correct */
    if (HDmemcmp(buf, rbuf, (size_t)1024) != 0)
        TEST_ERROR;

    /* CLose the file */
    if (H5Pclose(fapl) < 0)
        FAIL_STACK_ERROR;
    if (H5Fclose(fid) < 0)
        FAIL_STACK_ERROR;

    /* Pop API context */
    if (api_ctx_pushed && H5CX_pop(FALSE) < 0)
        FAIL_STACK_ERROR;
    api_ctx_pushed = FALSE;

    return EXIT_SUCCESS;

error:
    H5E_BEGIN_TRY
    {
        H5Pclose(fapl);
        H5Fclose(fid);
    }
    H5E_END_TRY;

    if (api_ctx_pushed)
        H5CX_pop(FALSE);

    return EXIT_FAILURE;
} /* end main() */