-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathdplugins.c
340 lines (300 loc) · 9.14 KB
/
dplugins.c
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
/*
* Copyright 2018, University Corporation for Atmospheric Research
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
*/
/**************************************************/
/* Global state plugin path implementation */
/**
* @file
* Functions for working with plugins.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef _MSC_VER
#include <io.h>
#endif
#include "netcdf.h"
#include "netcdf_filter.h"
#include "ncdispatch.h"
#include "nc4internal.h"
#include "nclog.h"
#include "ncbytes.h"
#include "ncplugins.h"
#include "netcdf_aux.h"
/*
Unified plugin related code
*/
/**************************************************/
/* Plugin-path API */
/* list of environment variables to check for plugin roots */
#define PLUGIN_ENV "HDF5_PLUGIN_PATH"
/* Control path verification */
#define PLUGINPATHVERIFY "NC_PLUGIN_PATH_VERIFY"
/*Forward*/
static int buildinitialpluginpath(NCPluginList* dirs);
static int NC_plugin_path_initialized = 0;
static int NC_plugin_path_verify = 1;
/**
* This function is called as part of nc_initialize.
* Its purpose is to initialize the plugin paths state.
*
* @return NC_NOERR
*
* @author Dennis Heimbigner
*/
EXTERNL int
nc_plugin_path_initialize(void)
{
int stat = NC_NOERR;
struct NCglobalstate* gs = NULL;
NCPluginList dirs = {0,NULL};
#ifdef USE_HDF5
int hdf5found = 0; /* 1 => we got a legit plugin path set from HDF5 */
#endif
if(!NC_initialized) nc_initialize();
if(NC_plugin_path_initialized != 0) goto done;
NC_plugin_path_initialized = 1;
if(getenv(PLUGINPATHVERIFY) != NULL) NC_plugin_path_verify = 1;
gs = NC_getglobalstate();
/**
* When the netcdf-c library initializes itself (at runtime), it chooses an
* initial global plugin path using the following rules, which are those used
* by the HDF5 library, except as modified for plugin install (which HDF5 does not support).
*/
/* Initialize the implementations */
#ifdef NETCDF_ENABLE_NCZARR_FILTERS
if((stat = NCZ_plugin_path_initialize())) goto done;
#endif
#ifdef USE_HDF5
if((stat = NC4_hdf5_plugin_path_initialize())) goto done;
#endif
/* Compute the initial global plugin path */
assert(dirs.ndirs == 0 && dirs.dirs == NULL);
if((stat = buildinitialpluginpath(&dirs))) goto done; /* Construct a default */
/* Sync to the actual implementations */
#ifdef USE_HDF5
if(!hdf5found)
{if((stat = NC4_hdf5_plugin_path_set(&dirs))) goto done;}
#endif
#ifdef NETCDF_ENABLE_NCZARR_FILTERS
if((stat = NCZ_plugin_path_set(&dirs))) goto done;
#endif
/* Set the global plugin dirs sequence */
assert(gs->pluginpaths == NULL);
gs->pluginpaths = nclistnew();
if(dirs.ndirs > 0) {
size_t i;
char** dst;
nclistsetlength(gs->pluginpaths,dirs.ndirs);
dst = (char**)nclistcontents(gs->pluginpaths);
assert(dst != NULL);
for(i=0;i<dirs.ndirs;i++)
dst[i] = strdup(dirs.dirs[i]);
}
done:
ncaux_plugin_path_clear(&dirs);
return NCTHROW(stat);
}
/**
* This function is called as part of nc_finalize()
* Its purpose is to clean-up plugin path state.
*
* @return NC_NOERR
*
* @author Dennis Heimbigner
*/
int
nc_plugin_path_finalize(void)
{
int stat = NC_NOERR;
struct NCglobalstate* gs = NC_getglobalstate();
if(NC_plugin_path_initialized == 0) goto done;
NC_plugin_path_initialized = 0;
NC_plugin_path_verify = 0;
/* Finalize the actual implementatios */
#ifdef NETCDF_ENABLE_NCZARR_FILTERS
if((stat = NCZ_plugin_path_finalize())) goto done;
#endif
#ifdef USE_HDF5
if((stat = NC4_hdf5_plugin_path_finalize())) goto done;
#endif
nclistfreeall(gs->pluginpaths); gs->pluginpaths = NULL;
done:
return NCTHROW(stat);
}
/**
* Return the length of the current sequence of directories
* in the internal global plugin path list.
* @param ndirsp length is returned here
* @return NC_NOERR | NC_EXXX
*
* @author Dennis Heimbigner
*/
int
nc_plugin_path_ndirs(size_t* ndirsp)
{
int stat = NC_NOERR;
size_t ndirs = 0;
struct NCglobalstate* gs = NC_getglobalstate();
if(gs->pluginpaths == NULL) gs->pluginpaths = nclistnew(); /* suspenders and belt */
ndirs = nclistlength(gs->pluginpaths);
/* Verify that the implementation plugin paths are consistent in length*/
if(NC_plugin_path_verify) {
#ifdef NETCDF_ENABLE_HDF5
{
size_t ndirs5 = 0;
if((stat=NC4_hdf5_plugin_path_ndirs(&ndirs5))) goto done;
assert(ndirs5 == ndirs);
}
#endif /*NETCDF_ENABLE_HDF5*/
#ifdef NETCDF_ENABLE_NCZARR_FILTERS
{
size_t ndirsz = 0;
if((stat=NCZ_plugin_path_ndirs(&ndirsz))) goto done;
assert(ndirsz == ndirs);
}
#endif /*NETCDF_ENABLE_NCZARR_FILTERS*/
}
if(ndirsp) *ndirsp = ndirs;
done:
return NCTHROW(stat);
}
/**
* Return the current sequence of directories in the internal global
* plugin path list. Since this function does not modify the plugin path,
* it can be called at any time.
* @param dirs pointer to an NCPluginList object
* @return NC_NOERR | NC_EXXX
* @author Dennis Heimbigner
*
* WARNING: if dirs->dirs is NULL, then space for the directory
* vector will be allocated. If not NULL, then the specified space will
* be overwritten with the vector.
*
* @author: Dennis Heimbigner
*/
int
nc_plugin_path_get(NCPluginList* dirs)
{
int stat = NC_NOERR;
struct NCglobalstate* gs = NC_getglobalstate();
size_t i;
if(gs->pluginpaths == NULL) gs->pluginpaths = nclistnew(); /* suspenders and belt */
if(dirs == NULL) goto done;
dirs->ndirs = nclistlength(gs->pluginpaths);
if(dirs->ndirs > 0 && dirs->dirs == NULL) {
if((dirs->dirs = (char**)calloc(dirs->ndirs,sizeof(char*)))==NULL)
{stat = NC_ENOMEM; goto done;}
}
for(i=0;i<dirs->ndirs;i++) {
const char* dir = nclistget(gs->pluginpaths,i);
dirs->dirs[i] = nulldup(dir);
}
/* Verify that the implementation plugin paths are consistent */
if(NC_plugin_path_verify) {
#ifdef NETCDF_ENABLE_HDF5
{
size_t i;
NCPluginList l5 = {0,NULL};
if((stat=NC4_hdf5_plugin_path_get(&l5))) goto done;
assert(l5.ndirs == nclistlength(gs->pluginpaths));
for(i=0;i<l5.ndirs;i++) {
assert(strcmp(dirs->dirs[i],l5.dirs[i])==0);
nullfree(l5.dirs[i]);
}
nullfree(l5.dirs);
}
#endif /*NETCDF_ENABLE_HDF5*/
#ifdef NETCDF_ENABLE_NCZARR_FILTERS
{
size_t i;
NCPluginList lz = {0,NULL};
if((stat=NCZ_plugin_path_get(&lz))) goto done;
assert(lz.ndirs == nclistlength(gs->pluginpaths));
for(i=0;i<lz.ndirs;i++) {
assert(strcmp(dirs->dirs[i],lz.dirs[i])==0);
nullfree(lz.dirs[i]);
}
nullfree(lz.dirs);
}
#endif /*NETCDF_ENABLE_NCZARR_FILTERS*/
}
done:
return NCTHROW(stat);
}
/**
* Empty the current internal path sequence
* and replace with the sequence of directories argument.
* Using a dirs->ndirs argument of 0 will clear the set of plugin dirs.
*
* @param dirs to overwrite the current internal dir list
* @return NC_NOERR | NC_EXXX
*
* @author Dennis Heimbigner
*/
int
nc_plugin_path_set(NCPluginList* dirs)
{
int stat = NC_NOERR;
struct NCglobalstate* gs = NC_getglobalstate();
if(dirs == NULL) {stat = NC_EINVAL; goto done;}
/* Clear the current dir list */
nclistfreeall(gs->pluginpaths);
gs->pluginpaths = nclistnew();
if(dirs->ndirs > 0) {
size_t i;
assert(gs->pluginpaths != NULL);
for(i=0;i<dirs->ndirs;i++) {
nclistpush(gs->pluginpaths,nulldup(dirs->dirs[i]));
}
}
/* Sync the global plugin path set to the individual implementations */
#ifdef NETCDF_ENABLE_HDF5
if((stat = NC4_hdf5_plugin_path_set(dirs))) goto done;
#endif
#ifdef NETCDF_ENABLE_NCZARR_FILTERS
if((stat = NCZ_plugin_path_set(dirs))) goto done;
#endif
done:
return NCTHROW(stat);
}
/**
* When the netcdf-c library initializes itself (at runtime), it chooses an
* initial global plugin path using the following rules, which are those used
* by the HDF5 library, except as modified for plugin install (which HDF5 does not support).
*
* Note: In the following, PLATFORMPATH is:
* -- /usr/local/lhdf5/lib/plugin if platform is *nix*
* -- %ALLUSERSPROFILE%/hdf5/lib/plugin if platform is Windows or Mingw
* and in the following, PLATFORMSEP is:
* -- ":" if platform is *nix*
* -- ";" if platform is Windows or Mingw
* and
* NETCDF_PLUGIN_SEARCH_PATH is the value constructed at build-time (see configure.ac)
*
* Table showing the computation of the initial global plugin path
* =================================================
* | HDF5_PLUGIN_PATH | Initial global plugin path |
* =================================================
* | undefined | NETCDF_PLUGIN_SEARCH_PATH |
* -------------------------------------------------
* | <path1;...pathn> | <path1;...pathn> |
* -------------------------------------------------
*/
static int
buildinitialpluginpath(NCPluginList* dirs)
{
int stat = NC_NOERR;
const char* hdf5path = NULL;
/* Find the plugin directory root(s) */
hdf5path = getenv(PLUGIN_ENV); /* HDF5_PLUGIN_PATH */
if(hdf5path == NULL) {
/* Use NETCDF_PLUGIN_SEARCH_PATH */
hdf5path = NETCDF_PLUGIN_SEARCH_PATH;
}
if((stat = ncaux_plugin_path_parse(hdf5path,'\0',dirs))) goto done;
done:
return stat;
}