netcdf-c/ncdump/ncpathcvt.c
Dennis Heimbigner efdec07f3d Extend the netcdf API to support programmatic changes to the plugin search path
Replaces PR https://github.com/Unidata/netcdf-c/pull/3024
         and PR https://github.com/Unidata/netcdf-c/pull/3033

re: https://github.com/Unidata/netcdf-c/issues/2753

As suggested by Ed Hartnett, This PR extends the netcdf.h API to support programmatic control over the search path used to locate plugins.

I created several different APIs, but finally settled on the following API as being the simplest possible. It does have the disadvantage that it requires use of a global lock (not implemented) if used in a threaded environment.

Specifically, note that modifying the plugin path must be done "atomically". That is, in a multi-threaded environment, it is important that the sequence of actions involved in setting up the plugin path must be done by a single processor or in some other way as to guarantee that two or more processors are not simultaneously accessing the plugin path get/set operations.

As an example, assume there exists a mutex lock called PLUGINLOCK. Then any processor accessing the plugin paths should operate as follows:
````
lock(PLUGINLOCK);
nc_plugin_path_get(...);
<rebuild plugin path>
nc_plugin_path_set(...);
unlock(PLUGINLOCK);
````
## Internal Architecture

It is assumed here that there only needs to be a single set of plugin path directories that is shared by all filter code and is independent of any file descriptor; it is global in other words. This means, for example, that the path list for NCZarr and for HDF5 will always be the same.

However internally, processing the set of plugin paths depends on the particular NC_FORMATX value (NC_FORMATX_NC_HDF5 and NC_FORMATX_NCZARR, currently). So the *nc_plugin_path_set* function, will take the paths it is given and propagate them to each of the NC_FORMATX dispatchers to store in a way that is appropriate to the given dispatcher.

There is a complication with respect to the *nc_plugin_path_get* function. It is possible for users to bypass the netcdf API and modify the HDF5 plugin paths directly. This can result in an inconsistent plugin path between the value used by HDF5 and the global value used by netcdf-c. Since there is no obvious fix for this, we warn the user of this possibility and otherwise ignore it.

## Test Changes
* New tests<br>
    a. unit_test/run_pluginpaths.sh -- was created to test this new capability.<br>
    b. A new test utility has been added as *unit_test/run_dfaltpluginpath.sh* to test the default plugin path list.
* New test support utilities<br>
    a. unit_test/ncpluginpath.c -- report current state of the plugin path<br>
    b. unit_test/tst_pluginpaths.c -- test program to support run_pluginpaths.sh

## Documentation
* A new file -- docs/pluginpath.md -- provides documentation of the new API. It includes some
  material taken fro filters.md.

## Other Major Changes
1. Cleanup the whole plugin path decision tree. This is described in the *docs/pluginpath.md* document and summarized in Addendum 2 below.
2. I noticed that the ncdump/testpathcvt.sh had been disabled, so fixed and re-enabled it. This necessitated some significant changes to dpathmgr.c.

## Misc. Changes
1. Add some path manipulation utilities to netcf_aux.h
2. Fix some minor bugs in netcdf_json.h
3. Convert netcdf_json.h and netcdf_proplist.h to BUILT_SOURCE.
4. Add NETCDF_ENABLE_HDF5 as synonym for USE_HDF5
5. Fix some size_t <-> int conversion warnings.
6. Encountered and fixed the Windows \r\n problem in tst_pluginpaths.c.
7. Cleanup some minor CMakeLists.txt problems.
8. Provide an implementation of echo -n since it appears to not be
   available on all platforms.
9. Add a property list mechanism to pass environmental information to filters.
10. Cleanup Doxyfile.in
11. Fixed a memory leak in libdap2; surprised that I did not find this earlier.

## Addendum 1: Proposed API

The API makes use of a counted vector of strings representing the sequence of directories in the path. The relevant type definition is as follows.
````
typedef struct NCPluginList {size_t ndirs; char** dirs;} NCPluginList;
````

The API proposed in this PR looks like this (from netcdf-c/include/netcdf_filter.h).

* ````int nc_plugin_path_ndirs(size_t* ndirsp);````
    Arguments: *ndirsp* -- store the number of directories in this memory.

    This function returns the number of directories in the sequence if internal directories of the internal plugin path list.

* ````int nc_plugin_path_get(NCPluginList* dirs);````
    Arguments:  *dirs* -- counted vector for storing the sequence of directies in the internal path list.

    This function returns the current sequence of directories from the internal plugin path list. Since this function does not modify the plugin path, it does not need to be locked; it is only when used to get the path to be modified that locking is required.  If the value of *dirs.dirs* is NULL (the normal case), then memory is allocated to hold the vector of directories. Otherwise, use the memory of *dirs.dirs* to hold the vector of directories.

* ````int nc_plugin_path_set(const NCPluginList* dirs);````
    Arguments: *dirs* -- counted vector for providing the new sequence of directories in the internal path list.

    This function empties the current internal path sequence and replaces it with the sequence of directories argument. Using an *ndirs* argument of 0 will clear the set of plugin paths.

## Addendum 2: Build-Time and Run-Time Constants.

### Build-Time Constants
<table style="border:2px solid black;border-collapse:collapse">
<tr style="outline: thin solid;" align="center"><td colspan="4">Table showing the build-time computation of NETCDF_PLUGIN_INSTALL_DIR and NETCDF_PLUGIN_SEARCH_PATH.</td>
<tr style="outline: thin solid" ><th>--with-plugin-dir<th>--prefix<th>NETCDF_PLUGIN_INSTALL_DIR<th>NETCDF_PLUGIN_SEARCH_PATH
<tr style="outline: thin solid" ><td>undefined<td>undefined<td>undefined<td>PLATFORMDEFALT
<tr style="outline: thin solid" ><td>undefined<td>&lt;abspath-prefix&gt;<td>&lt;abspath-prefix&gt;/hdf5/lib/plugin<td>&lt;abspath-prefix&gt;/hdf5/lib/plugin&lt;SEP&gt;PLATFORMDEFALT
<tr style="outline: thin solid" ><td>&lt;abspath-plugins&gt;<td>N.A.<td>&lt;abspath-plugins&gt;<td>&lt;abspath-plugins&gt;&lt;SEP&gt;PLATFORMDEFALT
</table>

<table style="border:2px solid black;border-collapse:collapse">
<tr style="outline: thin solid" align="center"><td colspan="2">Table showing the computation of the initial global plugin path</td>
<tr style="outline: thin solid"><th>HDF5_PLUGIN_PATH<th>Initial global plugin path
<tr style="outline: thin solid"><td>undefined<td>NETCDF_PLUGIN_SEARCH_PATH
<tr style="outline: thin solid"><td>&lt;path1;...pathn&gt;<td>&lt;path1;...pathn&gt;
</table>
2024-10-18 19:23:59 -06:00

307 lines
6.6 KiB
C

/*
* Copyright 2018, University Corporation for Atmospheric Research
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#if defined(_WIN32) && !defined(__MINGW32__)
#include "XGetopt.h"
#else
#include <getopt.h>
#endif
#include "netcdf.h"
#include "netcdf_filter.h"
#include "netcdf_aux.h"
#include "ncpathmgr.h"
#include "ncbytes.h"
static const char* USAGE =
"ncpathcvt [-c|-m|-u|-w] [-e] [-h] [-k] [-p] [-x] [-F] [-D <driveletter>] [-B<char>] [-S<char>] PATH\n"
"Options\n"
" -h help"
" -e add backslash escapes to '\' and ' '\n"
" -B <char> convert occurrences of <char> to blank\n"
" -D <driveletter> use driveletter when needed; defaults to 'c'\n"
" -F convert occurrences of '\\' to '/'"
" -S <char> use <char> as path separator when parsing;\n"
" currently limited to ';' or ':' but defaults to ';'\n"
"Output type options:\n"
" -c convert to Cygwin form of path\n"
" -m convert to MSYS form of path: currently an alias for -w\n"
" -u convert to Unix form of path\n"
" -w convert to Windows form of path\n"
"Other options:\n"
" -k return kind of the local environment\n"
" -p return kind of the input path\n"
"\n"
"Default is to convert to the format used by the platform.\n"
;
#undef DEBUG
struct Options {
int target;
int escapes;
int drive;
int debug;
int blank;
int slash;
int pathkind;
int sep;
} cvtoptions;
static char* escape(const char* path);
static void usage(const char* msg);
static void
usage(const char* msg)
{
if(msg != NULL) fprintf(stderr,"%s\n",msg);
fprintf(stderr,"%s",USAGE);
if(msg == NULL) exit(0); else exit(1);
}
static char*
escape(const char* path)
{
size_t slen = strlen(path);
const char* p;
char* q;
char* epath = NULL;
const char* escapes = " \\";
epath = (char*)malloc((2*slen) + 1);
if(epath == NULL) usage("out of memtory");
p = path;
q = epath;
for(;*p;p++) {
if(strchr(escapes,*p) != NULL)
*q++ = '\\';
*q++ = *p;
}
*q = '\0';
return epath;
}
static char*
slash(const char* path)
{
size_t slen = strlen(path);
const char* p;
char* q;
char* epath = NULL;
epath = (char*)malloc(slen + 1);
if(epath == NULL) usage("out of memtory");
p = path;
q = epath;
for(;*p;p++) {
if(*p == '\\')
*q++ = '/';
else *q++ = *p;
}
*q = '\0';
return epath;
}
void
printlocalkind(void)
{
const char* s = NULL;
int kind = NCgetlocalpathkind();
switch (kind) {
case NCPD_NIX: s = "unix"; break;
case NCPD_MSYS: s = "msys"; break;
case NCPD_CYGWIN: s = "cygwin"; break;
case NCPD_WIN: s = "win"; break;
default: s = "unknown"; break;
}
printf("%s",s);
exit(0);
}
void
printenv(void)
{
#ifdef __CYGWIN__
printf(" __CYGWIN__");
#else
printf(" !__CYGWIN__");
#endif
#ifdef _MSC_VER
printf(" _MSC_VER");
#else
printf(" !_MSC_VER");
#endif
#ifdef _WIN32
printf(" _WIN32");
#else
printf(" !_WIN32");
#endif
#ifdef __MSYS__
printf(" __MSYS__");
#else
printf(" !__MSYS__");
#endif
#ifdef __MSYS2__
printf(" __MSYS2__");
#else
printf(" !__MSYS2__");
#endif
#ifdef __MINGW32__
printf(" __MINGW32__");
#else
printf(" !__MINGW32__");
#endif
printf("\n");
exit(0);
}
void
printpathkind(const char* path)
{
const char* s = NULL;
int kind = NCgetinputpathkind(path);
switch (kind) {
case NCPD_NIX: s = "unix"; break;
case NCPD_MSYS: s = "msys"; break;
case NCPD_CYGWIN: s = "cygwin"; break;
case NCPD_WIN: s = "win"; break;
case NCPD_REL: s = "relative"; break;
default: s = "unknown"; break;
}
printf("%s",s);
exit(0);
}
int
processdir(const char* indir, char** cvtdirp)
{
char* cvtdir = NULL;
if(cvtoptions.target == NCPD_UNKNOWN) {
cvtdir = NCpathcvt(indir);
} else {
cvtdir = NCpathcvt_test(indir,cvtoptions.target,(char)cvtoptions.drive);
}
if(cvtdir && cvtoptions.escapes) {
char* dir = cvtdir; cvtdir = NULL;
cvtdir = escape(dir);
free(dir);
}
if(cvtdir && cvtoptions.slash) {
char* dir = cvtdir; cvtdir = NULL;
cvtdir = slash(dir);
free(dir);
}
if(cvtdirp) {*cvtdirp = cvtdir; cvtdir = NULL;}
if(cvtdir) free(cvtdir);
return 0;
}
int
main(int argc, char** argv)
{
int c;
char* inpath = NULL;
NCPluginList indirs = {0,NULL};
NCbytes* outpath = ncbytesnew();
int stat = NC_NOERR;
size_t i;
memset((void*)&cvtoptions,0,sizeof(cvtoptions));
cvtoptions.drive = 'c';
cvtoptions.sep = ';';
while ((c = getopt(argc, argv, "B:D:FS:Xchkmpuwx")) != EOF) {
switch(c) {
case 'c': cvtoptions.target = NCPD_CYGWIN; break;
case 'h': usage(NULL); break;
case 'k': printlocalkind(); break;
case 'm': cvtoptions.target = NCPD_WIN; break; /* Aliased */
case 'p': cvtoptions.pathkind = 1; break;
case 'u': cvtoptions.target = NCPD_NIX; break;
case 'w': cvtoptions.target = NCPD_WIN; break;
case 'x': cvtoptions.escapes = 1; break;
case 'B':
cvtoptions.blank = optarg[0];
if(cvtoptions.blank < ' ' || cvtoptions.blank == '\177')
usage("Bad -B argument");
break;
case 'D': cvtoptions.drive = optarg[0]; break;
case 'F': cvtoptions.slash = 1; break;
case 'S': cvtoptions.sep = optarg[0]; break;
case 'X': printenv(); break;
case '?':
usage("unknown option");
break;
}
}
argc -= optind;
argv += optind;
/* If no file arguments left or more than one, print usage message. */
if (argc == 0)
usage("no path specified");
if (argc > 1)
usage("more than one path specified");
/* translate blanks */
inpath = (char*)malloc(strlen(argv[0])+1);
if(inpath == NULL) usage("Out of memory");
{
const char* p = argv[0];
char* q = inpath;
for(;*p;p++) {
char c = *p;
if(c == cvtoptions.blank) c = ' ';
*q++ = c;
}
*q = '\0';
}
if(cvtoptions.pathkind) {
printpathkind(inpath);
goto done;
}
/* Break using the path separator */
if((stat = ncaux_plugin_path_parse(inpath,cvtoptions.sep,&indirs)))
{usage(nc_strerror(stat));}
for(i=0;i<indirs.ndirs;i++) {
char* outdir = NULL;
if((stat = processdir(indirs.dirs[i],&outdir)))
{usage(nc_strerror(stat));}
if(i > 0) ncbytesappend(outpath,cvtoptions.sep);
ncbytescat(outpath,outdir);
nullfree(outdir);
}
printf("%s",ncbytescontents(outpath));
done:
if(inpath) free(inpath);
ncaux_plugin_path_clear(&indirs);
ncbytesfree(outpath);
return 0;
}