File: tfile.c

package info (click to toggle)
libhdf4 4.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 30,384 kB
  • sloc: ansic: 128,700; sh: 15,015; fortran: 12,444; java: 5,863; xml: 1,205; makefile: 794; yacc: 678; pascal: 418; perl: 360; javascript: 203; lex: 163; csh: 41
file content (458 lines) | stat: -rw-r--r-- 18,122 bytes parent folder | download
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF.  The full HDF 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://support.hdfgroup.org/ftp/HDF/releases/.  *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "mfhdf.h"

#include "hdftest.h"
#include "nc_priv.h"

/********************************************************************
   Name: test_file_inuse() - tests preventing of an in-use file being
                removed at cleanup time.
                (bugzilla #376)

   Description:
    Sometime, when an error occurs, the cleanup process attempts to
    remove a file, which might still be in use (part of bugzilla #376.)
    The routine test_file_inuse is to test the fix that provides the
    underlying call to HPisfile_in_use, which should successfully
    determines whether a file is still in use before an attempt to remove.

    The main contents include:
    - a loop that repeatedly calls SDstart/DFACC_CREATE; only the first
    SDstart succeeds, the subsequent ones should fail.
    - SDcreate, SDwritedata, SDendaccess follow
    - outside of that loop is another loop to call SDend corresponding
    to the previous SDstart's
    - then, at the end, the file will be reopened; if the file doesn't
    exist and causes SDstart to fail, the test will fail.

    Before the fix, when the 2nd SDstart/DFACC_CREATE was called and
    failed because the file was being in use from the first call to
    SDstart/DFACC_CREATE, the cleaning process removed the file.

   Return value:
    The number of errors occurred in this routine.

*********************************************************************/

#define FILE_NAME "bug376.hdf" /* data file to test */
#define DIM0      10

static intn
test_file_inuse()
{
    int32       file_id, sd_id[5], sds_id[5];
    intn        statusn;
    int32       dims[1], start[1], edges[1], rank;
    int16       array_data[DIM0];
    const char *names[5] = {"data1", "data2", "data3", "data4", "data5"};
    intn        i, j;
    intn        num_errs = 0; /* number of errors so far */

    for (i = 0; i < 5; i++) {
        /* Create and open the file and initiate the SD interface. */
        sd_id[i] = SDstart(FILE_NAME, DFACC_CREATE);
        if (i == 0) {
            CHECK(sd_id[i], FAIL, "SDstart");
        } /* 1st SDstart must pass */
        else {
            VERIFY(sd_id[i], FAIL, "SDstart");
        }
        /* subsequent SDstart should fail, which causes the following calls
        to fail as well */

        /* Define the rank and dimensions of the data sets to be created. */
        rank     = 1;
        dims[0]  = DIM0;
        start[0] = 0;
        edges[0] = DIM0;

        /* Create the array data set. */
        sds_id[i] = SDcreate(sd_id[i], names[i], DFNT_INT16, rank, dims);
        if (i == 0) {
            CHECK(sds_id[i], FAIL, "SDcreate");
        } /* 1st SDcreate must pass */
        else
            VERIFY(sds_id[i], FAIL, "SDcreate");

        /* Fill the stored-data array with values. */
        for (j = 0; j < DIM0; j++) {
            /* Max values: i = 5, j = DIM0 (10), so the largest value
             * stored is 6 * 11 = 66
             */
            array_data[j] = (int16)((i + 1) * (j + 1));
        }

        /* Write data to the data set */
        statusn = SDwritedata(sds_id[i], start, NULL, edges, (void *)array_data);
        if (i == 0) {
            CHECK(statusn, FAIL, "SDwritedata");
        } /* 1st SDwritedata must pass */
        else
            VERIFY(statusn, FAIL, "SDwritedata");

        /* Terminate access to the data sets. */
        statusn = SDendaccess(sds_id[i]);
        if (i == 0) {
            CHECK(statusn, FAIL, "SDendaccess");
        } /* 1st SDendaccess must pass */
        else
            VERIFY(statusn, FAIL, "SDendaccess");

    } /* for i */

    for (i = 0; i < 5; i++) {
        /* Terminate access to the SD interface and close the file. */
        statusn = SDend(sd_id[i]);
        if (i == 0) {
            CHECK(statusn, FAIL, "SDend");
        } /* 1st SDend must pass */
        else
            VERIFY(statusn, FAIL, "SDend");
    }

    /* Try to open the file, which should exist */
    file_id = SDstart(FILE_NAME, DFACC_RDWR);
    CHECK(file_id, FAIL, "SDstart");

    statusn = SDend(file_id);
    CHECK(statusn, FAIL, "SDend");

    return num_errs;
} /* test_file_inuse */

/********************************************************************
   Name: test_max_open_files() - tests the new API SDreset_maxopenfiles,
                SDget_maxopenfiles, SDget_numopenfiles,
                and SDgetfilename.
                (bugzilla #396 and #440)

   Description:
    There were multiple requests from the users to increase the maximum
    number of opened files allowed.  SDreset_maxopenfiles is added to
    allow the user to reset that value.  The current default value is 32.
    This API can be called anytime to increase it.  This test routine will
    carry out the following tests:

    - Get the current max, should be the default (32,) and the system limit
    - Reset current max to an arbitrary number that is larger than the
    default and verify
    - Try to create more files than the current max and all should
    succeed, because NC_open resets the current max to system limit
    automatically, when the number of opened files exceeds the current
    max
    - Get the current max and system limit and verify, current max
    should be the system limit
    - Get the current max another way, it should be the system limit again
    - Get the current number of files being opened
    - Reset current max to a value that is smaller than the current
    number of opened files; it shouldn't reset
    - Reset current max again to a value that is smaller than the
    current max but larger than the current number of opened files,
    that should work for there is no information loss
    - Try to create more files up to the system limit or NUM_FILES_HI,
    because the arrays have max NUM_FILES_HI elements in this test
    - Close all the files, then try opening all again to verify their
    names, this is to test bugzilla 440

   Return value:
    The number of errors occurred in this routine.

*********************************************************************/

#define NUM_FILES_LOW 35
#define NUM_FILES_HI  1024

static int
test_max_open_files()
{
    int32 fids[NUM_FILES_HI];         /* holds IDs of opened files */
    char  filename[NUM_FILES_HI][10]; /* holds generated file names */
    char  readfname[H4_MAX_NC_NAME];  /* file name retrieved from file id */
    intn  index, status, curr_max,    /* curr maximum number of open files allowed in HDF */
        sys_limit,                    /* maximum number of open files allowed by system */
        curr_max_bk,                  /* back up of curr_max */
        curr_opened,                  /* number of files currently being opened */
        temp_limit,                   /* temp var - num of files to be opened in this test */
        num_errs = 0;                 /* number of errors so far */

    /* Get the current max and system limit */
    status = SDget_maxopenfiles(&curr_max, &sys_limit);
    CHECK(status, FAIL, "test_max_open_files: SDget_maxopenfiles");
    VERIFY(curr_max, H4_MAX_NC_OPEN, "test_max_open_files: SDget_maxopenfiles");

    /* Reset current max to an arbitrary number and check */
    curr_max = SDreset_maxopenfiles(33);
    VERIFY(curr_max, 33, "test_max_open_files: SDreset_maxopenfiles");

    /* Try to create more files than the default max (currently, 32) and
       all should succeed */
    for (index = 0; index < NUM_FILES_LOW; index++) {
        /* Create a file */
        sprintf(filename[index], "file%i", index);
        fids[index] = SDstart(filename[index], DFACC_CREATE);
        CHECK(fids[index], FAIL, "test_max_open_files: SDstart");
    }

    /* Verify that NUM_FILES_LOW files are opened */
    curr_opened = SDget_numopenfiles();
    VERIFY(curr_opened, NUM_FILES_LOW, "test_max_open_files: SDget_numopenfiles");

    /* Now randomly close 3 files and check number of opened files */
    status = SDend(fids[5]);
    CHECK(status, FAIL, "test_max_open_files: SDend");
    status = SDend(fids[15]);
    CHECK(status, FAIL, "test_max_open_files: SDend");
    status = SDend(fids[25]);
    CHECK(status, FAIL, "test_max_open_files: SDend");
    curr_opened = SDget_numopenfiles();
    VERIFY(curr_opened, NUM_FILES_LOW - 3, "test_max_open_files: SDget_numopenfiles");

    /* Get the current max and system limit */
    status = SDget_maxopenfiles(&curr_max, &sys_limit);
    CHECK(status, FAIL, "test_max_open_files: SDget_maxopenfiles");
    VERIFY(curr_max, sys_limit, "test_max_open_files: SDget_maxopenfiles");

    /* Get the current max another way, it should be the system limit */
    curr_max = SDreset_maxopenfiles(0);
    VERIFY(curr_max, sys_limit, "test_max_open_files: SDreset_maxopenfiles");

    /* Reopen the 3 files above, and check the number of opened files again */
    fids[5] = SDstart(filename[5], DFACC_RDWR);
    CHECK(fids[5], FAIL, "test_max_open_files: SDstart");
    fids[15] = SDstart(filename[15], DFACC_RDWR);
    CHECK(fids[15], FAIL, "test_max_open_files: SDstart");
    fids[25] = SDstart(filename[25], DFACC_RDWR);
    CHECK(fids[25], FAIL, "test_max_open_files: SDstart");
    curr_opened = SDget_numopenfiles();
    VERIFY(curr_opened, NUM_FILES_LOW, "test_max_open_files: SDget_numopenfiles");

    /* Reset current max to a value that is smaller than the current
       number of opened files; it shouldn't reset */
    curr_max_bk = curr_max;
    curr_max    = SDreset_maxopenfiles(curr_opened - 1);
    VERIFY(curr_max, curr_max_bk, "test_max_open_files: SDreset_maxopenfiles");

    /* Reset current max again to a value that is smaller than the
       current max but larger than the current number of opened files,
       that should work for there is no information loss */
    curr_max = SDreset_maxopenfiles(curr_opened + 3);
    VERIFY(curr_max, curr_opened + 3, "test_max_open_files: SDreset_maxopenfiles");

    /* Try to create more files up to the system limit or NUM_FILES_HI,
       because the arrays have max NUM_FILES_HI elements in this test */
    temp_limit = sys_limit / 2;
    temp_limit = temp_limit > NUM_FILES_HI ? NUM_FILES_HI : temp_limit;
    for (index = NUM_FILES_LOW; index < temp_limit; index++) {
        /* Create a file */
        sprintf(filename[index], "file%i", index);
        fids[index] = SDstart(filename[index], DFACC_CREATE);

        /* if SDstart fails due to "too many open files," then adjust
           temp_limit so that further failure can be prevented, i.e.
           following SDend and SDstart */
        if ((fids[index] == FAIL) && (HEvalue(1) == DFE_TOOMANY))
            temp_limit = index;

        /* only CHECK returned value from SDstart if the failure wasn't
           because of "too many open files" */
        else
            CHECK(fids[index], FAIL, "test_max_open_files: SDstart");
    }

    /* Close all the files, then try opening all again to verify their
       names, this is to test bugzilla 440 */
    for (index = 0; index < temp_limit; index++) {
        status = SDend(fids[index]);
        CHECK(status, FAIL, "test_max_open_files: SDend");

        fids[index] = SDstart(filename[index], DFACC_RDWR);
        CHECK(fids[index], FAIL, "test_max_open_files: SDstart");
    }

    /* Verify their names */
    for (index = 0; index < temp_limit; index++) {
        status = SDgetfilename(fids[index], readfname);
        CHECK(status, FAIL, "test_max_open_files: SDgetfilename");

        /* Verify the file name retrieved against the original */
        if (strcmp(readfname, filename[index])) {
            fprintf(stderr, "SDgetfilename: incorrect file being opened - expected <%s>, retrieved <%s>\n",
                    filename[index], readfname);
        }
    }

    /* Close then remove all the files */
    for (index = 0; index < temp_limit; index++) {
        status = SDend(fids[index]);
        CHECK(status, FAIL, "test_max_open_files: SDend");
        remove(filename[index]);
    }

    return num_errs;
}

/********************************************************************
   Name: test_longfilename() - tests that the library can handle a very
                long file name (bugzilla 1331.)

   Description:
    The main contents include:
    - create a file with very long name
    - create a dataset and close it (this is to activate the failure)
    - SDend to close the file, and it would cause segfault before the
    fix was applied.

   Return value:
    The number of errors occurred in this routine.

*********************************************************************/

#define NX 2
#define NY 3
static int
test_longfilename()
{
    int32 fid;     /* file id */
    int32 dset1;   /* dataset ids */
    int32 dims[2]; /* variable shapes */
    char  dsname[10];
    char  filename[256];
    intn  status   = 0; /* status returned by called functions */
    intn  num_errs = 0; /* number of errors so far */

    strcpy(dsname, "dataset 1");
    strcpy(filename, "This file name has quite a few characters because it is used to test the fix of "
                     "bugzilla 1331. It has to be at least this long to see.");

    /* enter define mode */
    fid = SDstart(filename, DFACC_CREATE);
    CHECK(fid, FAIL, "test_longfilename: SDstart");

    /* Define dimension of the data set to be created. */
    dims[0] = NX;
    dims[1] = NY;

    /* Create a dataset to reproduce the problem before the fix */
    dset1 = SDcreate(fid, dsname, DFNT_FLOAT32, 2, dims);
    CHECK(dset1, FAIL, "test_longfilename: SDcreate");

    status = SDendaccess(dset1);
    CHECK(status, FAIL, "test_longfilename: SDendaccess");

    status = SDend(fid);
    CHECK(status, FAIL, "test_longfilename: SDend");

    return num_errs;
}

/********************************************************************
   Name: test_fileformat() - tests that a file format can be
                             determined (HDFFR-1519)

   Description:
    The main contents include:
    - call Hishdf() on an hdf file and a non-hdf file
    - call HDiscdf() on a cdf file and a non-cdf file
    - call HDisnetcdf() on a netCDF file and a non-netCDF file
    - call HDisnetcdf64() on a 64-bit netCDF file and a classic netCDF file

   Return value:
    The number of errors occurred in this routine.

*********************************************************************/

static int
test_fileformat()
{
    int32       fid;                          /* file id */
    intn        ishdf        = 0;             /* true if file has HDF format */
    intn        isnetcdf     = 0;             /* true if file has classic netCDF format */
    intn        isnetcdf64   = 0;             /* true if file has 64-bit netCDF format */
    intn        num_errs     = 0;             /* number of errors so far */
    const char *hdf_basename = "hdffile.hdf"; /* hdf file to test */
    intn        status       = 0;             /* status returned by called functions */

    /* Create an empty HDF file to test Hishdf. */
    fid = SDstart(hdf_basename, DFACC_CREATE);
    CHECK(fid, FAIL, "SDstart");
    status = SDend(fid);
    CHECK(status, FAIL, "test_longfilename: SDend");

    /* Verify that this is an HDF file */
    ishdf = Hishdf(hdf_basename);
    VERIFY(ishdf, TRUE, "test_fileformat: Hishdf");

    /* Verify that this is not a netCDF 64-bit file */
    isnetcdf64 = HDisnetcdf64(hdf_basename);
    VERIFY(isnetcdf64, FALSE, "test_fileformat: HDisnetcdf64");

    /* Test a classic netCDF file */
    {
        const char *netcdf_filename = get_srcdir_filename("Roy.nc");

        /* Verify that this is not an HDF file */
        ishdf = Hishdf(netcdf_filename);
        VERIFY(ishdf, FALSE, "test_fileformat: Hishdf");

        /* Verify that this is a classic netCDF file */
        isnetcdf = HDisnetcdf(netcdf_filename);
        VERIFY(isnetcdf, TRUE, "test_fileformat: HDisnetcdf");
    }

    /* Test a 64-bit netCDF file */
    {
        const char *netcdf_filename = get_srcdir_filename("Roy-64.nc");

        /* Verify that this is a netCDF 64-bit file */
        isnetcdf64 = HDisnetcdf64(netcdf_filename);
        VERIFY(isnetcdf64, TRUE, "test_fileformat: HDisnetcdf64");

        /* Verify that this is not a classic netCDF file */
        isnetcdf = HDisnetcdf(netcdf_filename);
        VERIFY(isnetcdf, FALSE, "test_fileformat: HDisnetcdf");
    }

    return num_errs;
}

/* Test driver for testing miscellaneous file related APIs. */
extern int
test_files()
{
    intn num_errs = 0; /* number of errors */

    /* Output message about test being performed */
    TESTING("miscellaneous file related functions (tfile.c)");

    /* Test that an in-use file is not removed in certain failure cleanup. */
    num_errs = num_errs + test_file_inuse();

    /* Test handling of number of open files */
    num_errs = num_errs + test_max_open_files();

    /* Test file with very long name */
    num_errs = num_errs + test_longfilename();

    /* Test determining of file format */
    num_errs = num_errs + test_fileformat();

    if (num_errs == 0)
        PASSED();
    else
        H4_FAILED();

    /* Return the number of errors that's been kept track of so far */
    return num_errs;
}