/src/netcdf-c/libnczarr/zcreate.c
Line | Count | Source |
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, NClist* 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 | 0 | if((retval = ncz_create_dataset(h5,h5->root_grp,controls))) |
54 | 0 | BAIL(retval); |
55 | | |
56 | | /* Define mode gets turned on automatically on create. */ |
57 | 0 | h5->flags |= NC_INDEF; |
58 | | |
59 | | /* Set provenance. */ |
60 | 0 | if ((retval = NCZ_new_provenance(h5))) |
61 | 0 | BAIL(retval); |
62 | | |
63 | 0 | return NC_NOERR; |
64 | | |
65 | 0 | exit: /*failure exit*/ |
66 | 0 | if(retval && h5) |
67 | 0 | ncz_closeorabort(h5, NULL, 1); /* treat like abort */ |
68 | 0 | return retval; |
69 | 0 | } |
70 | | |
71 | | /** |
72 | | * @internal Create a netCDF-4/NCZ file. |
73 | | * |
74 | | * @param path The file name of the new file. |
75 | | * @param cmode The creation mode flag. |
76 | | * @param initialsz Ignored by this function. |
77 | | * @param basepe Ignored by this function. |
78 | | * @param chunksizehintp Ignored by this function. |
79 | | * @param parameters pointer to struct holding extra data (e.g. for |
80 | | * parallel I/O) layer. Ignored if NULL. |
81 | | * @param dispatch Pointer to the dispatch table for this file. |
82 | | * @param nc_file Pointer to an instance of NC. |
83 | | * |
84 | | * @return ::NC_NOERR No error. |
85 | | * @return ::NC_EINVAL Invalid input (check cmode). |
86 | | * @ingroup netcdf4 |
87 | | * @author Dennis Heimbigner, Ed Hartnett |
88 | | */ |
89 | | int |
90 | | NCZ_create(const char* path, int cmode, size_t initialsz, int basepe, |
91 | | size_t *chunksizehintp, void *parameters, |
92 | | const NC_Dispatch *dispatch, int ncid) |
93 | 0 | { |
94 | 0 | int stat = NC_NOERR; |
95 | 0 | NCURI* uri = NULL; |
96 | |
|
97 | 0 | ZTRACE(0,"path=%s,cmode=%d,initialsz=%ld,ncid=%d)",path,cmode,initialsz,ncid); |
98 | | |
99 | 0 | NC_UNUSED(parameters); |
100 | |
|
101 | 0 | assert(path); |
102 | |
|
103 | 0 | LOG((1, "%s: path %s cmode 0x%x ncid %d", |
104 | 0 | __func__, path, cmode ,ncid)); |
105 | | |
106 | | /* If this is our first file, initialize */ |
107 | 0 | if (!ncz_initialized) NCZ_initialize(); |
108 | |
|
109 | | #ifdef LOGGING |
110 | | /* If nc logging level has changed, see if we need to turn on |
111 | | * NCZ's error messages. */ |
112 | | NCZ_set_log_level(); |
113 | | #endif /* LOGGING */ |
114 | | |
115 | | /* Check the cmode for validity. */ |
116 | 0 | if((cmode & ILLEGAL_CREATE_FLAGS) != 0) |
117 | 0 | {stat = NC_EINVAL; goto done;} |
118 | | |
119 | | /* Turn on NC_WRITE */ |
120 | 0 | cmode |= NC_WRITE; |
121 | | |
122 | | /* Get the controls */ |
123 | 0 | ncuriparse(path,&uri); |
124 | 0 | if(uri == NULL) goto done; |
125 | | |
126 | | /* Create the file */ |
127 | 0 | stat = ncz_create_file(path, cmode, initialsz, ncurifragmentparams(uri), ncid); |
128 | |
|
129 | 0 | done: |
130 | 0 | ncurifree(uri); |
131 | 0 | return ZUNTRACE(stat); |
132 | 0 | } |