Coverage Report

Created: 2023-05-28 06:42

/src/netcdf-c/libnczarr/zcreate.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright 2003-2018, University Corporation for Atmospheric
2
 * Research. See COPYRIGHT file for copying and redistribution
3
 * conditions. */
4
5
/**
6
 * @file
7
 * @internal The netCDF-4 file functions relating to file creation.
8
 *
9
 * @author Dennis Heimbigner, Ed Hartnett
10
 */
11
12
#include "zincludes.h"
13
14
/** @internal These flags may not be set for create. */
15
static const int ILLEGAL_CREATE_FLAGS = (NC_NOWRITE|NC_MMAP|NC_DISKLESS|NC_64BIT_OFFSET|NC_CDF5);
16
17
/**
18
 * @internal Create a netCDF-4/NCZ file.
19
 *
20
 * @param path The file name of the new file.
21
 * @param cmode The creation mode flag.
22
 * @param initialsz The proposed initial file size (advisory)
23
 * @param nc Pointer to an instance of NC.
24
 *
25
 * @return ::NC_NOERR No error.
26
 * @return ::NC_EINVAL Invalid input (check cmode).
27
 * @return ::NC_EEXIST File exists and NC_NOCLOBBER used.
28
 * @return ::NC_EHDFERR ZARR returned error.
29
 * @ingroup netcdf4
30
 * @author Dennis Heimbigner, Ed Hartnett
31
 */
32
static int
33
ncz_create_file(const char *path, int cmode, size_t initialsz, const char** controls, int ncid)
34
0
{
35
0
    int retval = NC_NOERR;
36
0
    NC_FILE_INFO_T* h5 = NULL;
37
38
0
    assert(path);
39
0
    LOG((3, "%s: path %s mode 0x%x", __func__, path, cmode));
40
41
    /* Add necessary structs to hold netcdf-4 file data. */
42
0
    if ((retval = nc4_file_list_add(ncid, path, cmode, (void**)&h5)))
43
0
        BAIL(retval);
44
0
    assert(h5 && h5->root_grp);
45
0
    h5->root_grp->atts_read = 1;
46
47
0
    h5->mem.inmemory = ((cmode & NC_INMEMORY) == NC_INMEMORY);
48
0
    h5->mem.diskless = ((cmode & NC_DISKLESS) == NC_DISKLESS);
49
0
    h5->mem.persist = ((cmode & NC_PERSIST) == NC_PERSIST);
50
51
    /* Do format specific setup */
52
53
    /* Should check if file already exists, and if NC_NOCLOBBER is specified,
54
       return an error; but defer to the map */
55
  
56
0
    if((retval = ncz_create_dataset(h5,h5->root_grp,controls)))
57
0
  BAIL(retval);
58
59
    /* Define mode gets turned on automatically on create. */
60
0
    h5->flags |= NC_INDEF;
61
62
    /* Set provenance. */
63
0
    if ((retval = NCZ_new_provenance(h5)))
64
0
        BAIL(retval);
65
66
0
    return NC_NOERR;
67
68
0
exit: /*failure exit*/
69
0
    if(retval && h5)
70
0
        ncz_closeorabort(h5, NULL, 1); /* treat like abort */
71
0
    return retval;
72
0
}
73
74
/**
75
 * @internal Create a netCDF-4/NCZ file.
76
 *
77
 * @param path The file name of the new file.
78
 * @param cmode The creation mode flag.
79
 * @param initialsz Ignored by this function.
80
 * @param basepe Ignored by this function.
81
 * @param chunksizehintp Ignored by this function.
82
 * @param parameters pointer to struct holding extra data (e.g. for
83
 * parallel I/O) layer. Ignored if NULL.
84
 * @param dispatch Pointer to the dispatch table for this file.
85
 * @param nc_file Pointer to an instance of NC.
86
 *
87
 * @return ::NC_NOERR No error.
88
 * @return ::NC_EINVAL Invalid input (check cmode).
89
 * @ingroup netcdf4
90
 * @author Dennis Heimbigner, Ed Hartnett
91
 */
92
int
93
NCZ_create(const char* path, int cmode, size_t initialsz, int basepe,
94
           size_t *chunksizehintp, void *parameters,
95
           const NC_Dispatch *dispatch, int ncid)
96
0
{
97
0
    int stat = NC_NOERR;
98
0
    NCURI* uri = NULL;
99
100
0
    ZTRACE(0,"path=%s,cmode=%d,initialsz=%ld,ncid=%d)",path,cmode,initialsz,ncid);
101
    
102
0
    NC_UNUSED(parameters);
103
104
0
    assert(path);
105
106
0
    LOG((1, "%s: path %s cmode 0x%x ncid %d",
107
0
         __func__, path, cmode ,ncid));
108
109
    /* If this is our first file, initialize */
110
0
    if (!ncz_initialized) NCZ_initialize();
111
112
#ifdef LOGGING
113
    /* If nc logging level has changed, see if we need to turn on
114
     * NCZ's error messages. */
115
    NCZ_set_log_level();
116
#endif /* LOGGING */
117
118
    /* Check the cmode for validity. */
119
0
    if((cmode & ILLEGAL_CREATE_FLAGS) != 0)
120
0
        {stat = NC_EINVAL; goto done;}
121
122
    /* Turn on NC_WRITE */
123
0
    cmode |= NC_WRITE;
124
    
125
    /* Get the controls */
126
0
    ncuriparse(path,&uri);
127
0
    if(uri == NULL) goto done;
128
129
    /* Create the file */
130
0
   stat = ncz_create_file(path, cmode, initialsz, ncurifragmentparams(uri), ncid);
131
132
0
done:
133
0
    ncurifree(uri);
134
0
    return ZUNTRACE(stat);
135
0
}