netcdf-c/libdispatch/ddispatch.c
Dennis Heimbigner 6901206927 Regularize the semantics of mkstemp.
re: https://github.com/Unidata/netcdf-c/issues/1827

The issue is partly resolved by this PR. The proximate problem appears to be that the semantics of mkstemp in **nix is different than the semantics of _mktemp_s in Windows. I had thought they were the same but that is incorrect. The _mktemp_s function will only produce 26 different files and so the netcdf temp file code will fail after about that many iterations.

So, to solve this, I created my own version of mkstemp for windows that uses a random number generator. This appears to solve the reported issue.  I also added the testcase ncdap_test/test_manyurls but made it conditional on --enable-dap-long-tests because it is very slow.

I did note that the provided test program now fails after some 800 iterations with a libcurl error claiming it cannot resolve the host name. My belief is that the library is just running out of resources at this point: too many open curl handles or some such. I doubt if this failure is fixable.

So bottom line is that it is really important to do nc_close when you are finished with a file.

Misc. Other Changes:

1. I took the opportunity to clean up some bad string hacks in the code. Specifically
    * change all uses of strncat to strlcat
    * remove old string hacks: occoncat and occopycat
2. Add heck to see if test.opendap.org is running and if not, then skip test
3. Make CYGWIN use TEMP environment variable
2021-05-14 11:33:03 -06:00

124 lines
2.8 KiB
C

/*
Copyright (c) 1998-2018 University Corporation for Atmospheric Research/Unidata
See LICENSE.txt for license information.
*/
#include "config.h"
#include "ncdispatch.h"
#include "ncuri.h"
#include "nclog.h"
#include "ncbytes.h"
#include "ncrc.h"
#include "ncoffsets.h"
#include "ncpathmgr.h"
/* Required for getcwd, other functions. */
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
/* Required for getcwd, other functions. */
#ifdef _WIN32
#include <direct.h>
#endif
#if defined(ENABLE_BYTERANGE) || defined(ENABLE_DAP) || defined(ENABLE_DAP4)
#include <curl/curl.h>
#endif
/* Define vectors of zeros and ones for use with various nc_get_varX functions */
/* Note, this form of initialization fails under Cygwin */
size_t NC_coord_zero[NC_MAX_VAR_DIMS] = {0};
size_t NC_coord_one[NC_MAX_VAR_DIMS] = {1};
ptrdiff_t NC_stride_one[NC_MAX_VAR_DIMS] = {1};
/*
static nc_type longtype = (sizeof(long) == sizeof(int)?NC_INT:NC_INT64);
static nc_type ulongtype = (sizeof(unsigned long) == sizeof(unsigned int)?NC_UINT:NC_UINT64);
*/
/* Allow dispatch to do general initialization and finalization */
int
NCDISPATCH_initialize(void)
{
int status = NC_NOERR;
int i;
NCRCglobalstate* globalstate = NULL;
for(i=0;i<NC_MAX_VAR_DIMS;i++) {
NC_coord_zero[i] = 0;
NC_coord_one[i] = 1;
NC_stride_one[i] = 1;
}
globalstate = ncrc_getglobalstate(); /* will allocate and clear */
/* Capture temp dir*/
{
char* tempdir = NULL;
#if defined _WIN32 || defined __MSYS__ || defined __CYGWIN__
tempdir = getenv("TEMP");
#else
tempdir = "/tmp";
#endif
if(tempdir == NULL) {
fprintf(stderr,"Cannot find a temp dir; using ./\n");
tempdir = ".";
}
globalstate->tempdir= strdup(tempdir);
}
/* Capture $HOME */
{
char* home = getenv("HOME");
if(home == NULL) {
/* use tempdir */
home = globalstate->tempdir;
}
globalstate->home = strdup(home);
}
/* Capture $CWD */
{
char cwdbuf[4096];
cwdbuf[0] = '\0';
(void)NCgetcwd(cwdbuf,sizeof(cwdbuf));
if(strlen(cwdbuf) == 0) {
/* use tempdir */
strcpy(cwdbuf, globalstate->tempdir);
}
globalstate->cwd = strdup(cwdbuf);
}
/* Now load RC File */
status = NC_rcload();
ncloginit();
/* Compute type alignments */
NC_compute_alignments();
/* Initialize curl if it is being used */
#if defined(ENABLE_BYTERANGE) || defined(ENABLE_DAP) || defined(ENABLE_DAP4)
{
CURLcode cstat = curl_global_init(CURL_GLOBAL_ALL);
if(cstat != CURLE_OK)
status = NC_ECURL;
}
#endif
return status;
}
int
NCDISPATCH_finalize(void)
{
int status = NC_NOERR;
ncrc_freeglobalstate();
#if defined(ENABLE_BYTERANGE) || defined(ENABLE_DAP) || defined(ENABLE_DAP4)
curl_global_cleanup();
#endif
return status;
}