Coverage Report

Created: 2026-11-22 11:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hdf5/src/H5FDsec2.c
Line
Count
Source
1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2
 * Copyright by The HDF Group.                                               *
3
 * All rights reserved.                                                      *
4
 *                                                                           *
5
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
6
 * terms governing use, modification, and redistribution, is contained in    *
7
 * the LICENSE file, which can be found at the root of the source code       *
8
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
9
 * If you do not have access to either file, you may request a copy from     *
10
 * help@hdfgroup.org.                                                        *
11
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13
/*
14
 * Purpose: The POSIX unbuffered file driver using only the HDF5 public
15
 *          API and with a few optimizations: the lseek() call is made
16
 *          only when the current file position is unknown or needs to be
17
 *          changed based on previous I/O through this driver (don't mix
18
 *          I/O from this driver with I/O from other parts of the
19
 *          application to the same file).
20
 */
21
22
#include "H5FDmodule.h" /* This source code file is part of the H5FD module */
23
24
#include "H5private.h"   /* Generic Functions        */
25
#include "H5Eprivate.h"  /* Error handling           */
26
#include "H5Fprivate.h"  /* File access              */
27
#include "H5FDpkg.h"     /* File drivers             */
28
#include "H5FDsec2.h"    /* Sec2 file driver         */
29
#include "H5FLprivate.h" /* Free Lists               */
30
#include "H5Iprivate.h"  /* IDs                      */
31
#include "H5Pprivate.h"  /* Property lists           */
32
33
/* The driver identification number, initialized at runtime */
34
hid_t H5FD_SEC2_id_g = H5I_INVALID_HID;
35
36
/* The description of a file belonging to this driver. The 'eoa' and 'eof'
37
 * determine the amount of hdf5 address space in use and the high-water mark
38
 * of the file (the current size of the underlying filesystem file). The
39
 * 'pos' value is used to eliminate file position updates when they would be a
40
 * no-op. Unfortunately we've found systems that use separate file position
41
 * indicators for reading and writing so the lseek can only be eliminated if
42
 * the current operation is the same as the previous operation.  When opening
43
 * a file the 'eof' will be set to the current file size, `eoa' will be set
44
 * to zero, 'pos' will be set to H5F_ADDR_UNDEF (as it is when an error
45
 * occurs), and 'op' will be set to H5F_OP_UNKNOWN.
46
 */
47
typedef struct H5FD_sec2_t {
48
    H5FD_t  pub; /* public stuff, must be first      */
49
    int     fd;  /* the filesystem file descriptor   */
50
    haddr_t eoa; /* end of allocated region          */
51
    haddr_t eof; /* end of file; current file size   */
52
#ifndef H5_HAVE_PREADWRITE
53
    haddr_t        pos; /* current file I/O position        */
54
    H5FD_file_op_t op;  /* last operation                   */
55
#endif                  /* H5_HAVE_PREADWRITE */
56
    bool ignore_disabled_file_locks;
57
    char filename[H5FD_MAX_FILENAME_LEN]; /* Copy of file name from open operation */
58
#ifndef H5_HAVE_WIN32_API
59
    /* On most systems the combination of device and i-node number uniquely
60
     * identify a file.  Note that Cygwin, MinGW and other Windows POSIX
61
     * environments have the stat function (which fakes inodes)
62
     * and will use the 'device + inodes' scheme as opposed to the
63
     * Windows code further below.
64
     */
65
    dev_t device; /* file device number   */
66
    ino_t inode;  /* file i-node number   */
67
#else
68
    /* Files in windows are uniquely identified by the volume serial
69
     * number and the file index (both low and high parts).
70
     *
71
     * There are caveats where these numbers can change, especially
72
     * on FAT file systems.  On NTFS, however, a file should keep
73
     * those numbers the same until renamed or deleted (though you
74
     * can use ReplaceFile() on NTFS to keep the numbers the same
75
     * while renaming).
76
     *
77
     * See the MSDN "BY_HANDLE_FILE_INFORMATION Structure" entry for
78
     * more information.
79
     *
80
     * http://msdn.microsoft.com/en-us/library/aa363788(v=VS.85).aspx
81
     */
82
    DWORD nFileIndexLow;
83
    DWORD nFileIndexHigh;
84
    DWORD dwVolumeSerialNumber;
85
86
    HANDLE hFile; /* Native windows file handle */
87
#endif /* H5_HAVE_WIN32_API */
88
89
    /* Information from properties set by 'h5repart' tool
90
     *
91
     * Whether to eliminate the family driver info and convert this file to
92
     * a single file.
93
     */
94
    bool fam_to_single;
95
} H5FD_sec2_t;
96
97
/* Prototypes */
98
static H5FD_t *H5FD__sec2_open(const char *name, unsigned flags, hid_t fapl_id, haddr_t maxaddr);
99
static herr_t  H5FD__sec2_close(H5FD_t *_file);
100
static int     H5FD__sec2_cmp(const H5FD_t *_f1, const H5FD_t *_f2);
101
static herr_t  H5FD__sec2_query(const H5FD_t *_f1, unsigned long *flags);
102
static haddr_t H5FD__sec2_get_eoa(const H5FD_t *_file, H5FD_mem_t type);
103
static herr_t  H5FD__sec2_set_eoa(H5FD_t *_file, H5FD_mem_t type, haddr_t addr);
104
static haddr_t H5FD__sec2_get_eof(const H5FD_t *_file, H5FD_mem_t type);
105
static herr_t  H5FD__sec2_get_handle(H5FD_t *_file, hid_t fapl, void **file_handle);
106
static herr_t  H5FD__sec2_read(H5FD_t *_file, H5FD_mem_t type, hid_t fapl_id, haddr_t addr, size_t size,
107
                               void *buf);
108
static herr_t  H5FD__sec2_write(H5FD_t *_file, H5FD_mem_t type, hid_t fapl_id, haddr_t addr, size_t size,
109
                                const void *buf);
110
static herr_t  H5FD__sec2_truncate(H5FD_t *_file, hid_t dxpl_id, bool closing);
111
static herr_t  H5FD__sec2_lock(H5FD_t *_file, bool rw);
112
static herr_t  H5FD__sec2_unlock(H5FD_t *_file);
113
static herr_t  H5FD__sec2_delete(const char *filename, hid_t fapl_id);
114
static herr_t  H5FD__sec2_ctl(H5FD_t *_file, uint64_t op_code, uint64_t flags, const void *input,
115
                              void **output);
116
117
static const H5FD_class_t H5FD_sec2_g = {
118
    H5FD_CLASS_VERSION,    /* struct version       */
119
    H5FD_SEC2_VALUE,       /* value                */
120
    "sec2",                /* name                 */
121
    H5FD_MAXADDR,          /* maxaddr              */
122
    H5F_CLOSE_WEAK,        /* fc_degree            */
123
    NULL,                  /* terminate            */
124
    NULL,                  /* sb_size              */
125
    NULL,                  /* sb_encode            */
126
    NULL,                  /* sb_decode            */
127
    0,                     /* fapl_size            */
128
    NULL,                  /* fapl_get             */
129
    NULL,                  /* fapl_copy            */
130
    NULL,                  /* fapl_free            */
131
    0,                     /* dxpl_size            */
132
    NULL,                  /* dxpl_copy            */
133
    NULL,                  /* dxpl_free            */
134
    H5FD__sec2_open,       /* open                 */
135
    H5FD__sec2_close,      /* close                */
136
    H5FD__sec2_cmp,        /* cmp                  */
137
    H5FD__sec2_query,      /* query                */
138
    NULL,                  /* get_type_map         */
139
    NULL,                  /* alloc                */
140
    NULL,                  /* free                 */
141
    H5FD__sec2_get_eoa,    /* get_eoa              */
142
    H5FD__sec2_set_eoa,    /* set_eoa              */
143
    H5FD__sec2_get_eof,    /* get_eof              */
144
    H5FD__sec2_get_handle, /* get_handle           */
145
    H5FD__sec2_read,       /* read                 */
146
    H5FD__sec2_write,      /* write                */
147
    NULL,                  /* read_vector          */
148
    NULL,                  /* write_vector         */
149
    NULL,                  /* read_selection       */
150
    NULL,                  /* write_selection      */
151
    NULL,                  /* flush                */
152
    H5FD__sec2_truncate,   /* truncate             */
153
    H5FD__sec2_lock,       /* lock                 */
154
    H5FD__sec2_unlock,     /* unlock               */
155
    H5FD__sec2_delete,     /* del                  */
156
    H5FD__sec2_ctl,        /* ctl                  */
157
    H5FD_FLMAP_DICHOTOMY   /* fl_map               */
158
};
159
160
/* Declare a free list to manage the H5FD_sec2_t struct */
161
H5FL_DEFINE_STATIC(H5FD_sec2_t);
162
163
/*-------------------------------------------------------------------------
164
 * Function:    H5FD__sec2_register
165
 *
166
 * Purpose:     Register the driver with the library.
167
 *
168
 * Return:      SUCCEED/FAIL
169
 *
170
 *-------------------------------------------------------------------------
171
 */
172
herr_t
173
H5FD__sec2_register(void)
174
1
{
175
1
    herr_t ret_value = SUCCEED; /* Return value */
176
177
1
    FUNC_ENTER_PACKAGE
178
179
1
    if (H5I_VFL != H5I_get_type(H5FD_SEC2_id_g))
180
1
        if ((H5FD_SEC2_id_g = H5FD_register(&H5FD_sec2_g, sizeof(H5FD_class_t), false)) < 0)
181
0
            HGOTO_ERROR(H5E_VFL, H5E_CANTREGISTER, FAIL, "unable to register sec2 driver");
182
183
1
done:
184
1
    FUNC_LEAVE_NOAPI(ret_value)
185
1
} /* end H5FD__sec2_register() */
186
187
/*---------------------------------------------------------------------------
188
 * Function:    H5FD__sec2_unregister
189
 *
190
 * Purpose:     Reset library driver info.
191
 *
192
 * Returns:     SUCCEED (Can't fail)
193
 *
194
 *---------------------------------------------------------------------------
195
 */
196
herr_t
197
H5FD__sec2_unregister(void)
198
1
{
199
1
    FUNC_ENTER_PACKAGE_NOERR
200
201
    /* Reset VFL ID */
202
1
    H5FD_SEC2_id_g = H5I_INVALID_HID;
203
204
1
    FUNC_LEAVE_NOAPI(SUCCEED)
205
1
} /* end H5FD__sec2_unregister() */
206
207
/*-------------------------------------------------------------------------
208
 * Function:    H5Pset_fapl_sec2
209
 *
210
 * Purpose:     Modify the file access property list to use the H5FD_SEC2
211
 *              driver defined in this source file.  There are no driver
212
 *              specific properties.
213
 *
214
 * Return:      SUCCEED/FAIL
215
 *
216
 *-------------------------------------------------------------------------
217
 */
218
herr_t
219
H5Pset_fapl_sec2(hid_t fapl_id)
220
0
{
221
0
    H5P_genplist_t *plist; /* Property list pointer */
222
0
    herr_t          ret_value;
223
224
0
    FUNC_ENTER_API(FAIL)
225
226
0
    if (NULL == (plist = H5P_object_verify(fapl_id, H5P_FILE_ACCESS, false)))
227
0
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a file access property list");
228
229
0
    ret_value = H5P_set_driver(plist, H5FD_SEC2, NULL, NULL);
230
231
0
done:
232
0
    FUNC_LEAVE_API(ret_value)
233
0
} /* end H5Pset_fapl_sec2() */
234
235
/*-------------------------------------------------------------------------
236
 * Function:    H5FD__sec2_open
237
 *
238
 * Purpose:     Create and/or opens a file as an HDF5 file.
239
 *
240
 * Return:      Success:    A pointer to a new file data structure. The
241
 *                          public fields will be initialized by the
242
 *                          caller, which is always H5FD_open().
243
 *              Failure:    NULL
244
 *
245
 *-------------------------------------------------------------------------
246
 */
247
static H5FD_t *
248
H5FD__sec2_open(const char *name, unsigned flags, hid_t fapl_id, haddr_t maxaddr)
249
315
{
250
315
    H5FD_sec2_t *file = NULL; /* sec2 VFD info            */
251
315
    int          fd   = -1;   /* File descriptor          */
252
315
    int          o_flags;     /* Flags for open() call    */
253
#ifdef H5_HAVE_WIN32_API
254
    struct _BY_HANDLE_FILE_INFORMATION fileinfo;
255
#endif
256
315
    h5_stat_t       sb;
257
315
    H5P_genplist_t *plist;            /* Property list pointer */
258
315
    H5FD_t         *ret_value = NULL; /* Return value */
259
260
315
    FUNC_ENTER_PACKAGE
261
262
    /* Sanity check on file offsets */
263
315
    HDcompile_assert(sizeof(HDoff_t) >= sizeof(size_t));
264
265
    /* Check arguments */
266
315
    if (!name || !*name)
267
0
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "invalid file name");
268
315
    if (0 == maxaddr || HADDR_UNDEF == maxaddr)
269
0
        HGOTO_ERROR(H5E_ARGS, H5E_BADRANGE, NULL, "bogus maxaddr");
270
315
    if (H5FD_ADDR_OVERFLOW(maxaddr))
271
0
        HGOTO_ERROR(H5E_ARGS, H5E_OVERFLOW, NULL, "bogus maxaddr");
272
273
    /* Build the open flags */
274
315
    o_flags = (H5F_ACC_RDWR & flags) ? O_RDWR : O_RDONLY;
275
315
    if (H5F_ACC_TRUNC & flags)
276
0
        o_flags |= O_TRUNC;
277
315
    if (H5F_ACC_CREAT & flags)
278
0
        o_flags |= O_CREAT;
279
315
    if (H5F_ACC_EXCL & flags)
280
0
        o_flags |= O_EXCL;
281
282
    /* Open the file */
283
315
    if ((fd = HDopen(name, o_flags, H5_POSIX_CREATE_MODE_RW)) < 0) {
284
0
        int myerrno = errno;
285
0
        HGOTO_ERROR(
286
0
            H5E_FILE, H5E_CANTOPENFILE, NULL,
287
0
            "unable to open file: name = '%s', errno = %d, error message = '%s', flags = %x, o_flags = %x",
288
0
            name, myerrno, strerror(myerrno), flags, (unsigned)o_flags);
289
0
    } /* end if */
290
291
315
    memset(&sb, 0, sizeof(h5_stat_t));
292
315
    if (HDfstat(fd, &sb) < 0)
293
315
        HSYS_GOTO_ERROR(H5E_FILE, H5E_BADFILE, NULL, "unable to fstat file");
294
295
    /* Create the new file struct */
296
315
    if (NULL == (file = H5FL_CALLOC(H5FD_sec2_t)))
297
0
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "unable to allocate file struct");
298
299
315
    file->fd = fd;
300
315
    H5_CHECKED_ASSIGN(file->eof, haddr_t, sb.st_size, h5_stat_size_t);
301
315
#ifndef H5_HAVE_PREADWRITE
302
315
    file->pos = HADDR_UNDEF;
303
315
    file->op  = OP_UNKNOWN;
304
315
#endif /* H5_HAVE_PREADWRITE */
305
#ifdef H5_HAVE_WIN32_API
306
    file->hFile = (HANDLE)_get_osfhandle(fd);
307
    if (INVALID_HANDLE_VALUE == file->hFile)
308
        HGOTO_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL, "unable to get Windows file handle");
309
310
    if (!GetFileInformationByHandle((HANDLE)file->hFile, &fileinfo))
311
        HGOTO_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL, "unable to get Windows file information");
312
313
    file->nFileIndexHigh       = fileinfo.nFileIndexHigh;
314
    file->nFileIndexLow        = fileinfo.nFileIndexLow;
315
    file->dwVolumeSerialNumber = fileinfo.dwVolumeSerialNumber;
316
#else  /* H5_HAVE_WIN32_API */
317
315
    file->device = sb.st_dev;
318
315
    file->inode  = sb.st_ino;
319
315
#endif /* H5_HAVE_WIN32_API */
320
321
    /* Get the FAPL */
322
315
    if (NULL == (plist = (H5P_genplist_t *)H5I_object(fapl_id)))
323
0
        HGOTO_ERROR(H5E_VFL, H5E_BADTYPE, NULL, "not a file access property list");
324
325
    /* Check the file locking flags in the fapl */
326
315
    if (H5FD_ignore_disabled_file_locks_p != FAIL)
327
        /* The environment variable was set, so use that preferentially */
328
0
        file->ignore_disabled_file_locks = H5FD_ignore_disabled_file_locks_p;
329
315
    else {
330
        /* Use the value in the property list */
331
315
        if (H5P_get(plist, H5F_ACS_IGNORE_DISABLED_FILE_LOCKS_NAME, &file->ignore_disabled_file_locks) < 0)
332
0
            HGOTO_ERROR(H5E_VFL, H5E_CANTGET, NULL, "can't get ignore disabled file locks property");
333
315
    }
334
335
    /* Retain a copy of the name used to open the file, for possible error reporting */
336
315
    strncpy(file->filename, name, sizeof(file->filename) - 1);
337
315
    file->filename[sizeof(file->filename) - 1] = '\0';
338
339
    /* Check for non-default FAPL */
340
315
    if (H5P_FILE_ACCESS_DEFAULT != fapl_id) {
341
342
        /* This step is for h5repart tool only. If user wants to change file driver from
343
         * family to one that uses single files (sec2, etc.) while using h5repart, this
344
         * private property should be set so that in the later step, the library can ignore
345
         * the family driver information saved in the superblock.
346
         */
347
0
        if (H5P_exist_plist(plist, H5F_ACS_FAMILY_TO_SINGLE_NAME) > 0)
348
0
            if (H5P_get(plist, H5F_ACS_FAMILY_TO_SINGLE_NAME, &file->fam_to_single) < 0)
349
0
                HGOTO_ERROR(H5E_VFL, H5E_CANTGET, NULL, "can't get property of changing family to single");
350
0
    } /* end if */
351
352
    /* Set return value */
353
315
    ret_value = (H5FD_t *)file;
354
355
315
done:
356
315
    if (NULL == ret_value) {
357
0
        if (fd >= 0)
358
0
            HDclose(fd);
359
0
        if (file)
360
0
            file = H5FL_FREE(H5FD_sec2_t, file);
361
0
    } /* end if */
362
363
315
    FUNC_LEAVE_NOAPI(ret_value)
364
315
} /* end H5FD__sec2_open() */
365
366
/*-------------------------------------------------------------------------
367
 * Function:    H5FD__sec2_close
368
 *
369
 * Purpose:     Closes an HDF5 file.
370
 *
371
 * Return:      Success:    SUCCEED
372
 *              Failure:    FAIL, file not closed.
373
 *
374
 *-------------------------------------------------------------------------
375
 */
376
static herr_t
377
H5FD__sec2_close(H5FD_t *_file)
378
315
{
379
315
    H5FD_sec2_t *file      = (H5FD_sec2_t *)_file;
380
315
    herr_t       ret_value = SUCCEED; /* Return value */
381
382
315
    FUNC_ENTER_PACKAGE
383
384
    /* Sanity check */
385
315
    assert(file);
386
387
    /* Close the underlying file */
388
315
    if (HDclose(file->fd) < 0)
389
315
        HSYS_GOTO_ERROR(H5E_IO, H5E_CANTCLOSEFILE, FAIL, "unable to close file");
390
391
    /* Release the file info */
392
315
    file = H5FL_FREE(H5FD_sec2_t, file);
393
394
315
done:
395
315
    FUNC_LEAVE_NOAPI(ret_value)
396
315
} /* end H5FD__sec2_close() */
397
398
/*-------------------------------------------------------------------------
399
 * Function:    H5FD__sec2_cmp
400
 *
401
 * Purpose:     Compares two files belonging to this driver using an
402
 *              arbitrary (but consistent) ordering.
403
 *
404
 * Return:      Success:    A value like strcmp()
405
 *              Failure:    never fails (arguments were checked by the
406
 *                          caller).
407
 *
408
 *-------------------------------------------------------------------------
409
 */
410
static int
411
H5FD__sec2_cmp(const H5FD_t *_f1, const H5FD_t *_f2)
412
227
{
413
227
    const H5FD_sec2_t *f1        = (const H5FD_sec2_t *)_f1;
414
227
    const H5FD_sec2_t *f2        = (const H5FD_sec2_t *)_f2;
415
227
    int                ret_value = 0;
416
417
227
    FUNC_ENTER_PACKAGE_NOERR
418
419
#ifdef H5_HAVE_WIN32_API
420
    if (f1->dwVolumeSerialNumber < f2->dwVolumeSerialNumber)
421
        HGOTO_DONE(-1);
422
    if (f1->dwVolumeSerialNumber > f2->dwVolumeSerialNumber)
423
        HGOTO_DONE(1);
424
425
    if (f1->nFileIndexHigh < f2->nFileIndexHigh)
426
        HGOTO_DONE(-1);
427
    if (f1->nFileIndexHigh > f2->nFileIndexHigh)
428
        HGOTO_DONE(1);
429
430
    if (f1->nFileIndexLow < f2->nFileIndexLow)
431
        HGOTO_DONE(-1);
432
    if (f1->nFileIndexLow > f2->nFileIndexLow)
433
        HGOTO_DONE(1);
434
#else  /* H5_HAVE_WIN32_API */
435
227
    if (f1->device < f2->device)
436
0
        HGOTO_DONE(-1);
437
227
    if (f1->device > f2->device)
438
0
        HGOTO_DONE(1);
439
440
227
    if (f1->inode < f2->inode)
441
227
        HGOTO_DONE(-1);
442
0
    if (f1->inode > f2->inode)
443
0
        HGOTO_DONE(1);
444
0
#endif /* H5_HAVE_WIN32_API */
445
446
227
done:
447
227
    FUNC_LEAVE_NOAPI(ret_value)
448
227
} /* end H5FD__sec2_cmp() */
449
450
/*-------------------------------------------------------------------------
451
 * Function:    H5FD__sec2_query
452
 *
453
 * Purpose:     Set the flags that this VFL driver is capable of supporting.
454
 *              (listed in H5FDpublic.h)
455
 *
456
 * Return:      SUCCEED (Can't fail)
457
 *
458
 *-------------------------------------------------------------------------
459
 */
460
static herr_t
461
H5FD__sec2_query(const H5FD_t *_file, unsigned long *flags /* out */)
462
630
{
463
630
    const H5FD_sec2_t *file = (const H5FD_sec2_t *)_file; /* sec2 VFD info */
464
465
630
    FUNC_ENTER_PACKAGE_NOERR
466
467
    /* Set the VFL feature flags that this driver supports */
468
    /* Notice: the Mirror VFD Writer currently uses only the Sec2 driver as
469
     * the underlying driver -- as such, the Mirror VFD implementation copies
470
     * these feature flags as its own. Any modifications made here must be
471
     * reflected in H5FDmirror.c
472
     * -- JOS 2020-01-13
473
     */
474
630
    if (flags) {
475
630
        *flags = 0;
476
630
        *flags |= H5FD_FEAT_AGGREGATE_METADATA;  /* OK to aggregate metadata allocations  */
477
630
        *flags |= H5FD_FEAT_ACCUMULATE_METADATA; /* OK to accumulate metadata for faster writes */
478
630
        *flags |= H5FD_FEAT_DATA_SIEVE; /* OK to perform data sieving for faster raw data reads & writes    */
479
630
        *flags |= H5FD_FEAT_AGGREGATE_SMALLDATA; /* OK to aggregate "small" raw data allocations */
480
630
        *flags |= H5FD_FEAT_POSIX_COMPAT_HANDLE; /* get_handle callback returns a POSIX file descriptor */
481
630
        *flags |=
482
630
            H5FD_FEAT_SUPPORTS_SWMR_IO; /* VFD supports the single-writer/multiple-readers (SWMR) pattern   */
483
630
        *flags |= H5FD_FEAT_DEFAULT_VFD_COMPATIBLE; /* VFD creates a file which can be opened with the default
484
                                                       VFD      */
485
486
        /* Check for flags that are set by h5repart */
487
630
        if (file && file->fam_to_single)
488
0
            *flags |= H5FD_FEAT_IGNORE_DRVRINFO; /* Ignore the driver info when file is opened (which
489
                                                    eliminates it) */
490
630
    }                                            /* end if */
491
492
630
    FUNC_LEAVE_NOAPI(SUCCEED)
493
630
} /* end H5FD__sec2_query() */
494
495
/*-------------------------------------------------------------------------
496
 * Function:    H5FD__sec2_get_eoa
497
 *
498
 * Purpose:     Gets the end-of-address marker for the file. The EOA marker
499
 *              is the first address past the last byte allocated in the
500
 *              format address space.
501
 *
502
 * Return:      The end-of-address marker.
503
 *
504
 *-------------------------------------------------------------------------
505
 */
506
static haddr_t
507
H5FD__sec2_get_eoa(const H5FD_t *_file, H5FD_mem_t H5_ATTR_UNUSED type)
508
43.3k
{
509
43.3k
    const H5FD_sec2_t *file = (const H5FD_sec2_t *)_file;
510
511
43.3k
    FUNC_ENTER_PACKAGE_NOERR
512
513
43.3k
    FUNC_LEAVE_NOAPI(file->eoa)
514
43.3k
} /* end H5FD__sec2_get_eoa() */
515
516
/*-------------------------------------------------------------------------
517
 * Function:    H5FD__sec2_set_eoa
518
 *
519
 * Purpose:     Set the end-of-address marker for the file. This function is
520
 *              called shortly after an existing HDF5 file is opened in order
521
 *              to tell the driver where the end of the HDF5 data is located.
522
 *
523
 * Return:      SUCCEED (Can't fail)
524
 *
525
 *-------------------------------------------------------------------------
526
 */
527
static herr_t
528
H5FD__sec2_set_eoa(H5FD_t *_file, H5FD_mem_t H5_ATTR_UNUSED type, haddr_t addr)
529
1.59k
{
530
1.59k
    H5FD_sec2_t *file = (H5FD_sec2_t *)_file;
531
532
1.59k
    FUNC_ENTER_PACKAGE_NOERR
533
534
1.59k
    file->eoa = addr;
535
536
1.59k
    FUNC_LEAVE_NOAPI(SUCCEED)
537
1.59k
} /* end H5FD__sec2_set_eoa() */
538
539
/*-------------------------------------------------------------------------
540
 * Function:    H5FD__sec2_get_eof
541
 *
542
 * Purpose:     Returns the end-of-file marker, which is the greater of
543
 *              either the filesystem end-of-file or the HDF5 end-of-address
544
 *              markers.
545
 *
546
 * Return:      End of file address, the first address past the end of the
547
 *              "file", either the filesystem file or the HDF5 file.
548
 *
549
 *-------------------------------------------------------------------------
550
 */
551
static haddr_t
552
H5FD__sec2_get_eof(const H5FD_t *_file, H5FD_mem_t H5_ATTR_UNUSED type)
553
1.25k
{
554
1.25k
    const H5FD_sec2_t *file = (const H5FD_sec2_t *)_file;
555
556
1.25k
    FUNC_ENTER_PACKAGE_NOERR
557
558
1.25k
    FUNC_LEAVE_NOAPI(file->eof)
559
1.25k
} /* end H5FD__sec2_get_eof() */
560
561
/*-------------------------------------------------------------------------
562
 * Function:       H5FD__sec2_get_handle
563
 *
564
 * Purpose:        Returns the file handle of sec2 file driver.
565
 *
566
 * Returns:        SUCCEED/FAIL
567
 *
568
 *-------------------------------------------------------------------------
569
 */
570
static herr_t
571
H5FD__sec2_get_handle(H5FD_t *_file, hid_t H5_ATTR_UNUSED fapl, void **file_handle)
572
0
{
573
0
    H5FD_sec2_t *file      = (H5FD_sec2_t *)_file;
574
0
    herr_t       ret_value = SUCCEED;
575
576
0
    FUNC_ENTER_PACKAGE
577
578
0
    if (!file_handle)
579
0
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "file handle not valid");
580
581
0
    *file_handle = &(file->fd);
582
583
0
done:
584
0
    FUNC_LEAVE_NOAPI(ret_value)
585
0
} /* end H5FD__sec2_get_handle() */
586
587
/*-------------------------------------------------------------------------
588
 * Function:    H5FD__sec2_read
589
 *
590
 * Purpose:     Reads SIZE bytes of data from FILE beginning at address ADDR
591
 *              into buffer BUF according to data transfer properties in
592
 *              DXPL_ID.
593
 *
594
 * Return:      Success:    SUCCEED. Result is stored in caller-supplied
595
 *                          buffer BUF.
596
 *              Failure:    FAIL, Contents of buffer BUF are undefined.
597
 *
598
 *-------------------------------------------------------------------------
599
 */
600
static herr_t
601
H5FD__sec2_read(H5FD_t *_file, H5FD_mem_t H5_ATTR_UNUSED type, hid_t H5_ATTR_UNUSED dxpl_id, haddr_t addr,
602
                size_t size, void *buf /*out*/)
603
19.5k
{
604
19.5k
    H5FD_sec2_t *file      = (H5FD_sec2_t *)_file;
605
19.5k
    HDoff_t      offset    = (HDoff_t)addr;
606
19.5k
    herr_t       ret_value = SUCCEED; /* Return value */
607
608
19.5k
    FUNC_ENTER_PACKAGE
609
610
19.5k
    assert(file && file->pub.cls);
611
19.5k
    assert(buf);
612
613
    /* Check for overflow conditions */
614
19.5k
    if (!H5_addr_defined(addr))
615
0
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "addr undefined, addr = %llu", (unsigned long long)addr);
616
19.5k
    if (H5FD_REGION_OVERFLOW(addr, size))
617
0
        HGOTO_ERROR(H5E_ARGS, H5E_OVERFLOW, FAIL, "addr overflow, addr = %llu", (unsigned long long)addr);
618
619
19.5k
#ifndef H5_HAVE_PREADWRITE
620
    /* Seek to the correct location (if we don't have pread) */
621
19.5k
    if (addr != file->pos || OP_READ != file->op)
622
17.4k
        if (HDlseek(file->fd, (HDoff_t)addr, SEEK_SET) < 0)
623
19.5k
            HSYS_GOTO_ERROR(H5E_IO, H5E_SEEKERROR, FAIL, "unable to seek to proper position");
624
19.5k
#endif /* H5_HAVE_PREADWRITE */
625
626
    /* Read data, being careful of interrupted system calls, partial results,
627
     * and the end of the file.
628
     */
629
39.0k
    while (size > 0) {
630
19.5k
        h5_posix_io_t     bytes_in   = 0;  /* # of bytes to read       */
631
19.5k
        h5_posix_io_ret_t bytes_read = -1; /* # of bytes actually read */
632
633
        /* Trying to read more bytes than the return type can handle is
634
         * undefined behavior in POSIX.
635
         */
636
19.5k
        if (size > H5_POSIX_MAX_IO_BYTES)
637
0
            bytes_in = H5_POSIX_MAX_IO_BYTES;
638
19.5k
        else
639
19.5k
            bytes_in = (h5_posix_io_t)size;
640
641
19.5k
        do {
642
#ifdef H5_HAVE_PREADWRITE
643
            bytes_read = HDpread(file->fd, buf, bytes_in, offset);
644
            if (bytes_read > 0)
645
                offset += bytes_read;
646
#else
647
19.5k
            bytes_read = HDread(file->fd, buf, bytes_in);
648
19.5k
#endif /* H5_HAVE_PREADWRITE */
649
19.5k
        } while (-1 == bytes_read && EINTR == errno);
650
651
19.5k
        if (-1 == bytes_read) { /* error */
652
0
            int  myerrno = errno;
653
0
            char time_str[32];
654
655
0
            H5_get_localtime_str(time_str, sizeof(time_str));
656
0
#ifndef H5_HAVE_PREADWRITE
657
0
            offset = HDlseek(file->fd, 0, SEEK_CUR);
658
0
#endif
659
660
0
            HGOTO_ERROR(H5E_IO, H5E_READERROR, FAIL,
661
0
                        "file read failed: time = %s, filename = '%s', file descriptor = %d, errno = %d, "
662
0
                        "error message = '%s', buf = %p, total read size = %zu, bytes this sub-read = %llu, "
663
0
                        "offset = %llu",
664
0
                        time_str, file->filename, file->fd, myerrno, strerror(myerrno), buf, size,
665
0
                        (unsigned long long)bytes_in, (unsigned long long)offset);
666
0
        } /* end if */
667
668
19.5k
        if (0 == bytes_read) {
669
            /* end of file but not end of format address space */
670
4
            memset(buf, 0, size);
671
4
            break;
672
4
        } /* end if */
673
674
19.5k
        assert(bytes_read >= 0);
675
19.5k
        assert((size_t)bytes_read <= size);
676
677
19.5k
        size -= (size_t)bytes_read;
678
19.5k
        addr += (haddr_t)bytes_read;
679
19.5k
        buf = (char *)buf + bytes_read;
680
19.5k
    } /* end while */
681
682
19.5k
#ifndef H5_HAVE_PREADWRITE
683
    /* Update current position */
684
19.5k
    file->pos = addr;
685
19.5k
    file->op  = OP_READ;
686
19.5k
#endif /* H5_HAVE_PREADWRITE */
687
688
19.5k
done:
689
19.5k
#ifndef H5_HAVE_PREADWRITE
690
19.5k
    if (ret_value < 0) {
691
        /* Reset last file I/O information */
692
0
        file->pos = HADDR_UNDEF;
693
0
        file->op  = OP_UNKNOWN;
694
0
    }  /* end if */
695
19.5k
#endif /* H5_HAVE_PREADWRITE */
696
697
19.5k
    FUNC_LEAVE_NOAPI(ret_value)
698
19.5k
} /* end H5FD__sec2_read() */
699
700
/*-------------------------------------------------------------------------
701
 * Function:    H5FD__sec2_write
702
 *
703
 * Purpose:     Writes SIZE bytes of data to FILE beginning at address ADDR
704
 *              from buffer BUF according to data transfer properties in
705
 *              DXPL_ID.
706
 *
707
 * Return:      SUCCEED/FAIL
708
 *
709
 *-------------------------------------------------------------------------
710
 */
711
static herr_t
712
H5FD__sec2_write(H5FD_t *_file, H5FD_mem_t H5_ATTR_UNUSED type, hid_t H5_ATTR_UNUSED dxpl_id, haddr_t addr,
713
                 size_t size, const void *buf)
714
0
{
715
0
    H5FD_sec2_t *file      = (H5FD_sec2_t *)_file;
716
0
    HDoff_t      offset    = (HDoff_t)addr;
717
0
    herr_t       ret_value = SUCCEED; /* Return value */
718
719
0
    FUNC_ENTER_PACKAGE
720
721
0
    assert(file && file->pub.cls);
722
0
    assert(buf);
723
724
    /* Check for overflow conditions */
725
0
    if (!H5_addr_defined(addr))
726
0
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "addr undefined, addr = %llu", (unsigned long long)addr);
727
0
    if (H5FD_REGION_OVERFLOW(addr, size))
728
0
        HGOTO_ERROR(H5E_ARGS, H5E_OVERFLOW, FAIL, "addr overflow, addr = %llu, size = %llu",
729
0
                    (unsigned long long)addr, (unsigned long long)size);
730
731
0
#ifndef H5_HAVE_PREADWRITE
732
    /* Seek to the correct location (if we don't have pwrite) */
733
0
    if (addr != file->pos || OP_WRITE != file->op)
734
0
        if (HDlseek(file->fd, (HDoff_t)addr, SEEK_SET) < 0)
735
0
            HSYS_GOTO_ERROR(H5E_IO, H5E_SEEKERROR, FAIL, "unable to seek to proper position");
736
0
#endif /* H5_HAVE_PREADWRITE */
737
738
    /* Write the data, being careful of interrupted system calls and partial
739
     * results
740
     */
741
0
    while (size > 0) {
742
0
        h5_posix_io_t     bytes_in    = 0;  /* # of bytes to write  */
743
0
        h5_posix_io_ret_t bytes_wrote = -1; /* # of bytes written   */
744
745
        /* Trying to write more bytes than the return type can handle is
746
         * undefined behavior in POSIX.
747
         */
748
0
        if (size > H5_POSIX_MAX_IO_BYTES)
749
0
            bytes_in = H5_POSIX_MAX_IO_BYTES;
750
0
        else
751
0
            bytes_in = (h5_posix_io_t)size;
752
753
0
        do {
754
#ifdef H5_HAVE_PREADWRITE
755
            bytes_wrote = HDpwrite(file->fd, buf, bytes_in, offset);
756
            if (bytes_wrote > 0)
757
                offset += bytes_wrote;
758
#else
759
0
            bytes_wrote = HDwrite(file->fd, buf, bytes_in);
760
0
#endif /* H5_HAVE_PREADWRITE */
761
0
        } while (-1 == bytes_wrote && EINTR == errno);
762
763
0
        if (-1 == bytes_wrote) { /* error */
764
0
            int  myerrno = errno;
765
0
            char time_str[32];
766
767
0
            H5_get_localtime_str(time_str, sizeof(time_str));
768
0
#ifndef H5_HAVE_PREADWRITE
769
0
            offset = HDlseek(file->fd, 0, SEEK_CUR);
770
0
#endif
771
772
0
            HGOTO_ERROR(H5E_IO, H5E_WRITEERROR, FAIL,
773
0
                        "file write failed: time = %s, filename = '%s', file descriptor = %d, errno = %d, "
774
0
                        "error message = '%s', buf = %p, total write size = %zu, bytes this sub-write = "
775
0
                        "%llu, offset = %llu",
776
0
                        time_str, file->filename, file->fd, myerrno, strerror(myerrno), buf, size,
777
0
                        (unsigned long long)bytes_in, (unsigned long long)offset);
778
0
        } /* end if */
779
780
0
        assert(bytes_wrote > 0);
781
0
        assert((size_t)bytes_wrote <= size);
782
783
0
        size -= (size_t)bytes_wrote;
784
0
        addr += (haddr_t)bytes_wrote;
785
0
        buf = (const char *)buf + bytes_wrote;
786
0
    } /* end while */
787
788
    /* Update current position and eof */
789
0
#ifndef H5_HAVE_PREADWRITE
790
0
    file->pos = addr;
791
0
    file->op  = OP_WRITE;
792
0
#endif /* H5_HAVE_PREADWRITE */
793
0
    if (addr > file->eof)
794
0
        file->eof = addr;
795
796
0
done:
797
0
#ifndef H5_HAVE_PREADWRITE
798
0
    if (ret_value < 0) {
799
        /* Reset last file I/O information */
800
0
        file->pos = HADDR_UNDEF;
801
0
        file->op  = OP_UNKNOWN;
802
0
    }  /* end if */
803
0
#endif /* H5_HAVE_PREADWRITE */
804
805
0
    FUNC_LEAVE_NOAPI(ret_value)
806
0
} /* end H5FD__sec2_write() */
807
808
/*-------------------------------------------------------------------------
809
 * Function:    H5FD__sec2_truncate
810
 *
811
 * Purpose:     Makes sure that the true file size is the same (or larger)
812
 *              than the end-of-address.
813
 *
814
 * Return:      SUCCEED/FAIL
815
 *
816
 *-------------------------------------------------------------------------
817
 */
818
static herr_t
819
H5FD__sec2_truncate(H5FD_t *_file, hid_t H5_ATTR_UNUSED dxpl_id, bool H5_ATTR_UNUSED closing)
820
0
{
821
0
    H5FD_sec2_t *file      = (H5FD_sec2_t *)_file;
822
0
    herr_t       ret_value = SUCCEED; /* Return value */
823
824
0
    FUNC_ENTER_PACKAGE
825
826
0
    assert(file);
827
828
    /* Extend the file to make sure it's large enough */
829
0
    if (!H5_addr_eq(file->eoa, file->eof)) {
830
#ifdef H5_HAVE_WIN32_API
831
        LARGE_INTEGER li;       /* 64-bit (union) integer for SetFilePointer() call */
832
        DWORD         dwPtrLow; /* Low-order pointer bits from SetFilePointer()
833
                                 * Only used as an error code here.
834
                                 */
835
        DWORD dwError;          /* DWORD error code from GetLastError() */
836
        BOOL  bError;           /* Boolean error flag */
837
838
        /* Windows uses this odd QuadPart union for 32/64-bit portability */
839
        li.QuadPart = (LONGLONG)file->eoa;
840
841
        /* Extend the file to make sure it's large enough.
842
         *
843
         * Since INVALID_SET_FILE_POINTER can technically be a valid return value
844
         * from SetFilePointer(), we also need to check GetLastError().
845
         */
846
        dwPtrLow = SetFilePointer(file->hFile, li.LowPart, &li.HighPart, FILE_BEGIN);
847
        if (INVALID_SET_FILE_POINTER == dwPtrLow) {
848
            dwError = GetLastError();
849
            if (dwError != NO_ERROR)
850
                HGOTO_ERROR(H5E_FILE, H5E_FILEOPEN, FAIL, "unable to set file pointer");
851
        }
852
853
        bError = SetEndOfFile(file->hFile);
854
        if (0 == bError)
855
            HGOTO_ERROR(H5E_IO, H5E_SEEKERROR, FAIL, "unable to extend file properly");
856
#else  /* H5_HAVE_WIN32_API */
857
0
        if (-1 == HDftruncate(file->fd, (HDoff_t)file->eoa))
858
0
            HSYS_GOTO_ERROR(H5E_IO, H5E_SEEKERROR, FAIL, "unable to extend file properly");
859
0
#endif /* H5_HAVE_WIN32_API */
860
861
        /* Update the eof value */
862
0
        file->eof = file->eoa;
863
864
0
#ifndef H5_HAVE_PREADWRITE
865
        /* Reset last file I/O information */
866
0
        file->pos = HADDR_UNDEF;
867
0
        file->op  = OP_UNKNOWN;
868
0
#endif /* H5_HAVE_PREADWRITE */
869
0
    }  /* end if */
870
871
0
done:
872
0
    FUNC_LEAVE_NOAPI(ret_value)
873
0
} /* end H5FD__sec2_truncate() */
874
875
/*-------------------------------------------------------------------------
876
 * Function:    H5FD__sec2_lock
877
 *
878
 * Purpose:     To place an advisory lock on a file.
879
 *    The lock type to apply depends on the parameter "rw":
880
 *      true--opens for write: an exclusive lock
881
 *      false--opens for read: a shared lock
882
 *
883
 * Return:      SUCCEED/FAIL
884
 *
885
 *-------------------------------------------------------------------------
886
 */
887
static herr_t
888
H5FD__sec2_lock(H5FD_t *_file, bool rw)
889
315
{
890
315
    H5FD_sec2_t *file = (H5FD_sec2_t *)_file; /* VFD file struct          */
891
315
    int          lock_flags;                  /* file locking flags       */
892
315
    herr_t       ret_value = SUCCEED;         /* Return value             */
893
894
315
    FUNC_ENTER_PACKAGE
895
896
315
    assert(file);
897
898
    /* Set exclusive or shared lock based on rw status */
899
315
    lock_flags = rw ? LOCK_EX : LOCK_SH;
900
901
    /* Place a non-blocking lock on the file */
902
315
    if (HDflock(file->fd, lock_flags | LOCK_NB) < 0) {
903
0
        if (file->ignore_disabled_file_locks && ENOSYS == errno) {
904
            /* When errno is set to ENOSYS, the file system does not support
905
             * locking, so ignore it.
906
             */
907
0
            errno = 0;
908
0
        }
909
0
        else
910
0
            HSYS_GOTO_ERROR(H5E_VFL, H5E_CANTLOCKFILE, FAIL, "unable to lock file");
911
0
    }
912
913
315
done:
914
315
    FUNC_LEAVE_NOAPI(ret_value)
915
315
} /* end H5FD__sec2_lock() */
916
917
/*-------------------------------------------------------------------------
918
 * Function:    H5FD__sec2_unlock
919
 *
920
 * Purpose:     To remove the existing lock on the file
921
 *
922
 * Return:      SUCCEED/FAIL
923
 *
924
 *-------------------------------------------------------------------------
925
 */
926
static herr_t
927
H5FD__sec2_unlock(H5FD_t *_file)
928
0
{
929
0
    H5FD_sec2_t *file      = (H5FD_sec2_t *)_file; /* VFD file struct          */
930
0
    herr_t       ret_value = SUCCEED;              /* Return value             */
931
932
0
    FUNC_ENTER_PACKAGE
933
934
0
    assert(file);
935
936
0
    if (HDflock(file->fd, LOCK_UN) < 0) {
937
0
        if (file->ignore_disabled_file_locks && ENOSYS == errno) {
938
            /* When errno is set to ENOSYS, the file system does not support
939
             * locking, so ignore it.
940
             */
941
0
            errno = 0;
942
0
        }
943
0
        else
944
0
            HSYS_GOTO_ERROR(H5E_VFL, H5E_CANTUNLOCKFILE, FAIL, "unable to unlock file");
945
0
    }
946
947
0
done:
948
0
    FUNC_LEAVE_NOAPI(ret_value)
949
0
} /* end H5FD__sec2_unlock() */
950
951
/*-------------------------------------------------------------------------
952
 * Function:    H5FD__sec2_delete
953
 *
954
 * Purpose:     Delete a file
955
 *
956
 * Return:      SUCCEED/FAIL
957
 *
958
 *-------------------------------------------------------------------------
959
 */
960
static herr_t
961
H5FD__sec2_delete(const char *filename, hid_t H5_ATTR_UNUSED fapl_id)
962
0
{
963
0
    herr_t ret_value = SUCCEED; /* Return value */
964
965
0
    FUNC_ENTER_PACKAGE
966
967
0
    assert(filename);
968
969
0
    if (HDremove(filename) < 0)
970
0
        HSYS_GOTO_ERROR(H5E_VFL, H5E_CANTDELETEFILE, FAIL, "unable to delete file");
971
972
0
done:
973
0
    FUNC_LEAVE_NOAPI(ret_value)
974
0
} /* end H5FD__sec2_delete() */
975
976
/*-------------------------------------------------------------------------
977
 * Function:    H5FD__sec2_ctl
978
 *
979
 * Purpose:     Sec2 VFD version of the ctl callback.
980
 *
981
 *              The desired operation is specified by the op_code
982
 *              parameter.
983
 *
984
 *              The flags parameter controls management of op_codes that
985
 *              are unknown to the callback
986
 *
987
 *              The input and output parameters allow op_code specific
988
 *              input and output
989
 *
990
 *              At present, no op codes are supported by this VFD.
991
 *
992
 * Return:      Non-negative on success/Negative on failure
993
 *
994
 *-------------------------------------------------------------------------
995
 */
996
static herr_t
997
H5FD__sec2_ctl(H5FD_t H5_ATTR_UNUSED *_file, uint64_t H5_ATTR_UNUSED op_code, uint64_t flags,
998
               const void H5_ATTR_UNUSED *input, void H5_ATTR_UNUSED **output)
999
0
{
1000
0
    herr_t ret_value = SUCCEED;
1001
1002
0
    FUNC_ENTER_PACKAGE
1003
1004
    /* No op codes are understood. */
1005
0
    if (flags & H5FD_CTL_FAIL_IF_UNKNOWN_FLAG)
1006
0
        HGOTO_ERROR(H5E_VFL, H5E_FCNTL, FAIL, "unknown op_code and fail if unknown flag is set");
1007
1008
0
done:
1009
0
    FUNC_LEAVE_NOAPI(ret_value)
1010
0
} /* end H5FD__sec2_ctl() */