2010-06-03 21:24:43 +08:00
|
|
|
/*
|
2018-12-07 05:34:40 +08:00
|
|
|
* Copyright 2018, University Corporation for Atmospheric Research
|
2010-06-03 21:24:43 +08:00
|
|
|
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
|
|
|
|
*/
|
|
|
|
/* $Id: winceio.c,v 1.2 2010/05/04 17:30:04 dmh Exp $ */
|
|
|
|
/* Dennis Heimbigner 2010-3-04 */
|
|
|
|
|
2017-04-19 23:29:35 +08:00
|
|
|
|
2017-03-12 03:03:17 +08:00
|
|
|
#if HAVE_CONFIG_H
|
2010-06-03 21:24:43 +08:00
|
|
|
#include <config.h>
|
2017-03-12 03:03:17 +08:00
|
|
|
#endif
|
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h> /* DEBUG */
|
|
|
|
|
|
|
|
#ifndef _WIN32_WCE
|
|
|
|
#include <errno.h>
|
|
|
|
#else
|
|
|
|
#define EPERM NC_EPERM
|
|
|
|
#define ENOMEM NC_ENOMEM
|
|
|
|
#define EINVAL NC_EINVAL
|
|
|
|
#define EIO NC_EINVAL
|
|
|
|
#define EEXIST NC_EEXIST
|
|
|
|
#endif
|
|
|
|
|
2016-03-08 01:58:02 +08:00
|
|
|
#ifndef NC_NOERR
|
|
|
|
#define NC_NOERR 0
|
2010-06-03 21:24:43 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
Codify cross-platform file paths
The netcdf-c code has to deal with a variety of platforms:
Windows, OSX, Linux, Cygwin, MSYS, etc. These platforms differ
significantly in the kind of file paths that they accept. So in
order to handle this, I have created a set of replacements for
the most common file system operations such as _open_ or _fopen_
or _access_ to manage the file path differences correctly.
A more limited version of this idea was already implemented via
the ncwinpath.h and dwinpath.c code. So this can be viewed as a
replacement for that code. And in path in many cases, the only
change that was required was to replace '#include <ncwinpath.h>'
with '#include <ncpathmgt.h>' and then replace file operation
calls with the NCxxx equivalent from ncpathmgr.h Note that
recently, the ncwinpath.h was renamed ncpathmgmt.h, so this pull
request should not require dealing with winpath.
The heart of the change is include/ncpathmgmt.h, which provides
alternate operations such as NCfopen or NCaccess and which properly
parse and rebuild path arguments to work for the platform on which
the code is executing. This mostly matters for Windows because of the
way that it uses backslash and drive letters, as compared to *nix*.
One important feature is that the user can do string manipulations
on a file path without having to worry too much about the platform
because the path management code will properly handle most mixed cases.
So one can for example concatenate a path suffix that uses forward
slashes to a Windows path and have it work correctly.
The conversion code is in libdispatch/dpathmgr.c, and the
important function there is NCpathcvt which does the proper
conversions to the local path format.
As a rule, most code should just replace their file operations with
the corresponding NCxxx ones defined in include/ncpathmgmt.h. These
NCxxx functions all call NCpathcvt on their path arguments before
executing the actual file operation.
In some rare cases, the client may need to directly use NCpathcvt,
but this should be avoided as much as possible. If there is a need
for supporting a new file operation not already in ncpathmgmt.h, then
use the code in dpathmgr.c as a template. Also please notify Unidata
so we can include it as a formal part or our supported operations.
Also, if you see an operation in the library that is not using the
NCxxx form, then please submit an issue so we can fix it.
Misc. Changes:
* Clean up the utf8 testing code; it is impossible to get some
tests to work under windows using shell scripts; the args do
not pass as utf8 but as some other encoding.
* Added an extra utf8 test case: test_unicode_path.sh
* Add a true test for HDF5 1.10.6 or later because as noted in
PR https://github.com/Unidata/netcdf-c/pull/1794,
HDF5 changed its Windows file path handling.
2021-03-05 04:41:31 +08:00
|
|
|
#include "ncpathmgr.h"
|
2010-06-03 21:24:43 +08:00
|
|
|
#include "ncio.h"
|
|
|
|
#include "fbits.h"
|
|
|
|
#include "rnd.h"
|
|
|
|
|
|
|
|
#if !defined(NDEBUG) && !defined(X_INT_MAX)
|
|
|
|
#define X_INT_MAX 2147483647
|
|
|
|
#endif
|
|
|
|
#if 0 /* !defined(NDEBUG) && !defined(X_ALIGN) */
|
|
|
|
#define X_ALIGN 4
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define ALWAYS_NC_SHARE 0 /* DEBUG */
|
|
|
|
|
|
|
|
#define DEFAULTBLKSIZE 32768
|
|
|
|
|
|
|
|
static FILE* descriptors[1024];
|
|
|
|
static int fdmax = 1; /* never use zero */
|
|
|
|
|
|
|
|
/* Begin OS */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* What is the preferred I/O block size?
|
|
|
|
* (This becomes the default *sizehint == ncp->chunk in the higher layers.)
|
|
|
|
* TODO: What is the the best answer here?
|
|
|
|
*/
|
|
|
|
static size_t
|
|
|
|
blksize(int fd)
|
|
|
|
{
|
|
|
|
return (size_t) DEFAULTBLKSIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sortof like ftruncate, except won't make the
|
|
|
|
* file shorter.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
fgrow(FILE* f, const off_t len)
|
|
|
|
{
|
2016-03-08 01:58:02 +08:00
|
|
|
int status = NC_NOERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
long pos = ftell(f);
|
|
|
|
long size;
|
2017-04-19 23:29:35 +08:00
|
|
|
pos = ftell(f);
|
2010-06-03 21:24:43 +08:00
|
|
|
status = fseek(f,0,SEEK_END);
|
|
|
|
if(ferror(f)) return EIO;
|
|
|
|
size = ftell(f);
|
|
|
|
status = fseek(f,pos,SEEK_SET);
|
|
|
|
if(ferror(f)) return EIO;
|
2016-03-08 01:58:02 +08:00
|
|
|
if(len < size) return NC_NOERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
else {
|
|
|
|
const long dumb = 0;
|
|
|
|
status = fseek(f, len-sizeof(dumb), SEEK_SET);
|
|
|
|
if(ferror(f)) return EIO;
|
|
|
|
fwrite((const void *)&dumb, 1, sizeof(dumb), f);
|
|
|
|
if(ferror(f)) return EIO;
|
|
|
|
status = fseek(f, pos, SEEK_SET);
|
|
|
|
if(ferror(f)) return EIO;
|
|
|
|
}
|
2016-03-08 01:58:02 +08:00
|
|
|
return NC_NOERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sortof like ftruncate, except won't make the file shorter. Differs
|
|
|
|
* from fgrow by only writing one byte at designated seek position, if
|
|
|
|
* needed.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
fgrow2(FILE* f, const off_t len)
|
|
|
|
{
|
2016-03-08 01:58:02 +08:00
|
|
|
int status = NC_NOERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
long pos = ftell(f);
|
|
|
|
long size;
|
2017-04-19 23:29:35 +08:00
|
|
|
pos = ftell(f);
|
2010-06-03 21:24:43 +08:00
|
|
|
status = fseek(f,0,SEEK_END);
|
|
|
|
if(ferror(f)) return EIO;
|
|
|
|
size = ftell(f);
|
|
|
|
status = fseek(f,pos,SEEK_SET);
|
|
|
|
if(ferror(f)) return EIO;
|
2016-03-08 01:58:02 +08:00
|
|
|
if(len < size) return NC_NOERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
else {
|
|
|
|
const char dumb = 0;
|
|
|
|
status = fseek(f, len-sizeof(dumb), SEEK_SET);
|
|
|
|
if(ferror(f)) return EIO;
|
|
|
|
fwrite((const void *)&dumb, 1, sizeof(dumb), f);
|
|
|
|
if(ferror(f)) return EIO;
|
|
|
|
status = fseek(f, pos, SEEK_SET);
|
|
|
|
if(ferror(f)) return EIO;
|
|
|
|
}
|
2016-03-08 01:58:02 +08:00
|
|
|
return NC_NOERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
/* End OS */
|
|
|
|
/* Begin ffio */
|
|
|
|
|
|
|
|
static int
|
2017-04-19 23:29:35 +08:00
|
|
|
fileio_pgout(ncio *const nciop,
|
2010-06-03 21:24:43 +08:00
|
|
|
off_t const offset, const size_t extent,
|
|
|
|
const void *const vp, off_t *posp)
|
|
|
|
{
|
2016-03-08 01:58:02 +08:00
|
|
|
int status = NC_NOERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
FILE* f = descriptors[nciop->fd];
|
|
|
|
|
|
|
|
#ifdef X_ALIGN
|
|
|
|
assert(offset % X_ALIGN == 0);
|
|
|
|
assert(extent % X_ALIGN == 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if(*posp != offset)
|
|
|
|
{
|
|
|
|
status = fseek(f, offset, SEEK_SET);
|
|
|
|
if(ferror(f)) return EIO;
|
|
|
|
*posp = offset;
|
|
|
|
}
|
|
|
|
fwrite(vp,1,extent,f);
|
|
|
|
if(ferror(f)) return EIO;
|
|
|
|
*posp += extent;
|
2016-03-08 01:58:02 +08:00
|
|
|
return NC_NOERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
fileio_pgin(ncio *const nciop,
|
|
|
|
off_t const offset, const size_t extent,
|
|
|
|
void *const vp, size_t *nreadp, off_t *posp)
|
|
|
|
{
|
2016-03-08 01:58:02 +08:00
|
|
|
int status = NC_NOERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
ssize_t nread;
|
|
|
|
int count;
|
|
|
|
|
|
|
|
FILE* f = descriptors[nciop->fd];
|
|
|
|
|
|
|
|
#ifdef X_ALIGN
|
|
|
|
assert(offset % X_ALIGN == 0);
|
|
|
|
assert(extent % X_ALIGN == 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if(*posp != offset)
|
|
|
|
{
|
|
|
|
status = fseek(f, offset, SEEK_SET);
|
|
|
|
if(ferror(f)) return EIO;
|
|
|
|
*posp = offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
nread = fread(vp,1,extent,f);
|
|
|
|
if(ferror(f)) return EIO;
|
|
|
|
*nreadp = nread;
|
|
|
|
*posp += nread;
|
2016-03-08 01:58:02 +08:00
|
|
|
return NC_NOERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
|
|
|
|
|
|
|
typedef struct ncio_ffio {
|
|
|
|
off_t pos;
|
|
|
|
/* buffer */
|
2017-04-19 23:29:35 +08:00
|
|
|
off_t bf_offset;
|
2010-06-03 21:24:43 +08:00
|
|
|
size_t bf_extent;
|
|
|
|
size_t bf_cnt;
|
|
|
|
void *bf_base;
|
|
|
|
} ncio_ffio;
|
|
|
|
|
|
|
|
static int
|
|
|
|
ncio_fileio_rel(ncio *const nciop, off_t offset, int rflags)
|
|
|
|
{
|
2016-03-08 01:58:02 +08:00
|
|
|
int status = NC_NOERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
FILE* f = descriptors[nciop->fd];
|
|
|
|
|
|
|
|
ncio_ffio *ffp = (ncio_ffio *)nciop->pvt;
|
|
|
|
|
|
|
|
assert(ffp->bf_offset <= offset);
|
|
|
|
assert(ffp->bf_cnt != 0);
|
|
|
|
assert(ffp->bf_cnt <= ffp->bf_extent);
|
|
|
|
#ifdef X_ALIGN
|
|
|
|
assert(offset < ffp->bf_offset + X_ALIGN);
|
|
|
|
assert(ffp->bf_cnt % X_ALIGN == 0 );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if(fIsSet(rflags, RGN_MODIFIED))
|
|
|
|
{
|
|
|
|
if(!fIsSet(nciop->ioflags, NC_WRITE))
|
|
|
|
return EPERM; /* attempt to write readonly file */
|
|
|
|
|
|
|
|
status = fileio_pgout(nciop, ffp->bf_offset,
|
|
|
|
ffp->bf_cnt,
|
|
|
|
ffp->bf_base, &ffp->pos);
|
|
|
|
/* if error, invalidate buffer anyway */
|
|
|
|
}
|
|
|
|
ffp->bf_offset = OFF_NONE;
|
|
|
|
ffp->bf_cnt = 0;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
ncio_fileio_get(ncio *const nciop,
|
|
|
|
off_t offset, size_t extent,
|
|
|
|
int rflags,
|
|
|
|
void **const vpp)
|
|
|
|
{
|
|
|
|
ncio_ffio *ffp = (ncio_ffio *)nciop->pvt;
|
2016-03-08 01:58:02 +08:00
|
|
|
int status = NC_NOERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
FILE* f = descriptors[nciop->fd];
|
|
|
|
#ifdef X_ALIGN
|
|
|
|
size_t rem;
|
|
|
|
#endif
|
2017-04-19 23:29:35 +08:00
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
if(fIsSet(rflags, RGN_WRITE) && !fIsSet(nciop->ioflags, NC_WRITE))
|
|
|
|
return EPERM; /* attempt to write readonly file */
|
|
|
|
|
|
|
|
assert(extent != 0);
|
|
|
|
assert(extent < X_INT_MAX); /* sanity check */
|
|
|
|
|
|
|
|
assert(ffp->bf_cnt == 0);
|
|
|
|
|
|
|
|
#ifdef X_ALIGN
|
|
|
|
/* round to seekable boundaries */
|
|
|
|
rem = offset % X_ALIGN;
|
|
|
|
if(rem != 0)
|
|
|
|
{
|
|
|
|
offset -= rem;
|
|
|
|
extent += rem;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const size_t rndup = extent % X_ALIGN;
|
|
|
|
if(rndup != 0)
|
|
|
|
extent += X_ALIGN - rndup;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(offset % X_ALIGN == 0);
|
|
|
|
assert(extent % X_ALIGN == 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if(ffp->bf_extent < extent)
|
|
|
|
{
|
|
|
|
if(ffp->bf_base != NULL)
|
|
|
|
{
|
|
|
|
free(ffp->bf_base);
|
|
|
|
ffp->bf_base = NULL;
|
|
|
|
ffp->bf_extent = 0;
|
|
|
|
}
|
|
|
|
assert(ffp->bf_extent == 0);
|
|
|
|
ffp->bf_base = malloc(extent);
|
|
|
|
if(ffp->bf_base == NULL)
|
|
|
|
return ENOMEM;
|
|
|
|
ffp->bf_extent = extent;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = fileio_pgin(nciop, offset,
|
|
|
|
extent,
|
|
|
|
ffp->bf_base,
|
|
|
|
&ffp->bf_cnt, &ffp->pos);
|
2016-03-08 01:58:02 +08:00
|
|
|
if(status != NC_NOERR)
|
2010-06-03 21:24:43 +08:00
|
|
|
return status;
|
|
|
|
|
|
|
|
ffp->bf_offset = offset;
|
|
|
|
|
|
|
|
if(ffp->bf_cnt < extent)
|
|
|
|
{
|
|
|
|
(void) memset((char *)ffp->bf_base + ffp->bf_cnt, 0,
|
|
|
|
extent - ffp->bf_cnt);
|
|
|
|
ffp->bf_cnt = extent;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef X_ALIGN
|
|
|
|
*vpp = (char *)ffp->bf_base + rem;
|
|
|
|
#else
|
|
|
|
*vpp = (char *)ffp->bf_base;
|
|
|
|
#endif
|
2016-03-08 01:58:02 +08:00
|
|
|
return NC_NOERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
ncio_fileio_move(ncio *const nciop, off_t to, off_t from,
|
|
|
|
size_t nbytes, int rflags)
|
|
|
|
{
|
2016-03-08 01:58:02 +08:00
|
|
|
int status = NC_NOERR;
|
2017-04-19 23:29:35 +08:00
|
|
|
off_t lower = from;
|
2010-06-03 21:24:43 +08:00
|
|
|
off_t upper = to;
|
|
|
|
char *base;
|
|
|
|
size_t diff = upper - lower;
|
|
|
|
size_t extent = diff + nbytes;
|
|
|
|
FILE* f = descriptors[nciop->fd];
|
|
|
|
|
|
|
|
rflags &= RGN_NOLOCK; /* filter unwanted flags */
|
|
|
|
|
|
|
|
if(to == from)
|
2016-03-08 01:58:02 +08:00
|
|
|
return NC_NOERR; /* NOOP */
|
2017-04-19 23:29:35 +08:00
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
if(to > from)
|
|
|
|
{
|
|
|
|
/* growing */
|
2017-04-19 23:29:35 +08:00
|
|
|
lower = from;
|
2010-06-03 21:24:43 +08:00
|
|
|
upper = to;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* shrinking */
|
|
|
|
lower = to;
|
|
|
|
upper = from;
|
|
|
|
}
|
|
|
|
|
|
|
|
diff = upper - lower;
|
|
|
|
extent = diff + nbytes;
|
|
|
|
|
|
|
|
status = ncio_fileio_get(nciop, lower, extent, RGN_WRITE|rflags,
|
|
|
|
(void **)&base);
|
|
|
|
|
2016-03-08 01:58:02 +08:00
|
|
|
if(status != NC_NOERR)
|
2010-06-03 21:24:43 +08:00
|
|
|
return status;
|
|
|
|
|
|
|
|
if(to > from)
|
2017-04-19 23:29:35 +08:00
|
|
|
(void) memmove(base + diff, base, nbytes);
|
2010-06-03 21:24:43 +08:00
|
|
|
else
|
2017-04-19 23:29:35 +08:00
|
|
|
(void) memmove(base, base + diff, nbytes);
|
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
(void) ncio_fileio_rel(nciop, lower, RGN_MODIFIED);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ncio_fileio_sync(ncio *const nciop)
|
|
|
|
{
|
|
|
|
FILE* f = descriptors[nciop->fd];
|
|
|
|
fflush(f);
|
2016-03-08 01:58:02 +08:00
|
|
|
return NC_NOERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ncio_fileio_free(void *const pvt)
|
|
|
|
{
|
|
|
|
ncio_ffio *ffp = (ncio_ffio *)pvt;
|
|
|
|
if(ffp == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(ffp->bf_base != NULL)
|
|
|
|
{
|
|
|
|
free(ffp->bf_base);
|
|
|
|
ffp->bf_base = NULL;
|
|
|
|
ffp->bf_offset = OFF_NONE;
|
|
|
|
ffp->bf_extent = 0;
|
|
|
|
ffp->bf_cnt = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
ncio_fileio_init2(ncio *const nciop, size_t *sizehintp)
|
|
|
|
{
|
|
|
|
FILE* f = descriptors[nciop->fd];
|
|
|
|
ncio_ffio *ffp = (ncio_ffio *)nciop->pvt;
|
|
|
|
|
|
|
|
assert(f != NULL);
|
|
|
|
|
|
|
|
ffp->bf_extent = *sizehintp;
|
|
|
|
|
|
|
|
assert(ffp->bf_base == NULL);
|
|
|
|
|
|
|
|
/* this is separate allocation because it may grow */
|
|
|
|
ffp->bf_base = malloc(ffp->bf_extent);
|
|
|
|
if(ffp->bf_base == NULL)
|
|
|
|
{
|
|
|
|
ffp->bf_extent = 0;
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
/* else */
|
2016-03-08 01:58:02 +08:00
|
|
|
return NC_NOERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
ncio_fileio_init(ncio *const nciop)
|
|
|
|
{
|
|
|
|
ncio_ffio *ffp = (ncio_ffio *)nciop->pvt;
|
2015-08-20 07:14:13 +08:00
|
|
|
p
|
2010-06-03 21:24:43 +08:00
|
|
|
*((ncio_relfunc **)&nciop->rel) = ncio_fileio_rel; /* cast away const */
|
|
|
|
*((ncio_getfunc **)&nciop->get) = ncio_fileio_get; /* cast away const */
|
|
|
|
*((ncio_movefunc **)&nciop->move) = ncio_fileio_move; /* cast away const */
|
|
|
|
*((ncio_syncfunc **)&nciop->sync) = ncio_fileio_sync; /* cast away const */
|
|
|
|
*((ncio_freefunc **)&nciop->free) = ncio_fileio_free; /* cast away const */
|
|
|
|
|
|
|
|
ffp->pos = -1;
|
|
|
|
ffp->bf_offset = OFF_NONE;
|
|
|
|
ffp->bf_extent = 0;
|
|
|
|
ffp->bf_cnt = 0;
|
|
|
|
ffp->bf_base = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
|
|
|
|
|
|
|
static void
|
|
|
|
ncio_free(ncio *nciop)
|
|
|
|
{
|
|
|
|
if(nciop == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(nciop->free != NULL)
|
|
|
|
nciop->free(nciop->pvt);
|
2017-04-19 23:29:35 +08:00
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
free(nciop);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ncio *
|
|
|
|
ncio_new(const char *path, int ioflags)
|
|
|
|
{
|
|
|
|
size_t sz_ncio = M_RNDUP(sizeof(ncio));
|
|
|
|
size_t sz_path = M_RNDUP(strlen(path) +1);
|
|
|
|
size_t sz_ncio_pvt;
|
|
|
|
ncio *nciop;
|
2017-04-19 23:29:35 +08:00
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
#if ALWAYS_NC_SHARE /* DEBUG */
|
|
|
|
fSet(ioflags, NC_SHARE);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if(fIsSet(ioflags, NC_SHARE))
|
|
|
|
fprintf(stderr, "NC_SHARE not implemented for fileio\n");
|
|
|
|
|
|
|
|
sz_ncio_pvt = sizeof(ncio_ffio);
|
|
|
|
|
|
|
|
nciop = (ncio *) malloc(sz_ncio + sz_path + sz_ncio_pvt);
|
|
|
|
if(nciop == NULL)
|
|
|
|
return NULL;
|
2017-04-19 23:29:35 +08:00
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
nciop->ioflags = ioflags;
|
|
|
|
*((int *)&nciop->fd) = -1; /* cast away const */
|
|
|
|
|
|
|
|
nciop->path = (char *) ((char *)nciop + sz_ncio);
|
|
|
|
(void) strcpy((char *)nciop->path, path); /* cast away const */
|
|
|
|
|
|
|
|
/* cast away const */
|
|
|
|
*((void **)&nciop->pvt) = (void *)(nciop->path + sz_path);
|
|
|
|
|
|
|
|
ncio_fileio_init(nciop);
|
|
|
|
|
|
|
|
return nciop;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Public below this point */
|
|
|
|
|
|
|
|
/* TODO: Is this reasonable for this platform? */
|
|
|
|
static const size_t NCIO_MINBLOCKSIZE = 0x100;
|
|
|
|
static const size_t NCIO_MAXBLOCKSIZE = 0x100000;
|
|
|
|
|
|
|
|
int
|
|
|
|
ncio_create(const char *path, int ioflags,
|
|
|
|
size_t initialsz,
|
|
|
|
off_t igeto, size_t igetsz, size_t *sizehintp,
|
2015-05-29 05:03:02 +08:00
|
|
|
void* parameters,
|
2010-06-03 21:24:43 +08:00
|
|
|
ncio **nciopp, void **const igetvpp)
|
|
|
|
{
|
|
|
|
ncio *nciop;
|
|
|
|
#ifdef _WIN32_WCE
|
|
|
|
char* oflags = "bw+"; /*==?(O_RDWR|O_CREAT|O_TRUNC) && binary*/
|
|
|
|
#else
|
|
|
|
char* oflags = "w+"; /*==?(O_RDWR|O_CREAT|O_TRUNC);*/
|
|
|
|
#endif
|
|
|
|
FILE* f;
|
|
|
|
int i,fd;
|
2016-03-08 01:58:02 +08:00
|
|
|
int status = NC_NOERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
if(initialsz < (size_t)igeto + igetsz)
|
|
|
|
initialsz = (size_t)igeto + igetsz;
|
|
|
|
|
|
|
|
fSet(ioflags, NC_WRITE);
|
|
|
|
|
|
|
|
if(path == NULL || *path == 0)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
nciop = ncio_new(path, ioflags);
|
|
|
|
if(nciop == NULL)
|
|
|
|
return ENOMEM;
|
|
|
|
|
|
|
|
if(fIsSet(ioflags, NC_NOCLOBBER)) {
|
|
|
|
/* Since we do not have use of the O_EXCL flag,
|
|
|
|
we need to fake it */
|
Codify cross-platform file paths
The netcdf-c code has to deal with a variety of platforms:
Windows, OSX, Linux, Cygwin, MSYS, etc. These platforms differ
significantly in the kind of file paths that they accept. So in
order to handle this, I have created a set of replacements for
the most common file system operations such as _open_ or _fopen_
or _access_ to manage the file path differences correctly.
A more limited version of this idea was already implemented via
the ncwinpath.h and dwinpath.c code. So this can be viewed as a
replacement for that code. And in path in many cases, the only
change that was required was to replace '#include <ncwinpath.h>'
with '#include <ncpathmgt.h>' and then replace file operation
calls with the NCxxx equivalent from ncpathmgr.h Note that
recently, the ncwinpath.h was renamed ncpathmgmt.h, so this pull
request should not require dealing with winpath.
The heart of the change is include/ncpathmgmt.h, which provides
alternate operations such as NCfopen or NCaccess and which properly
parse and rebuild path arguments to work for the platform on which
the code is executing. This mostly matters for Windows because of the
way that it uses backslash and drive letters, as compared to *nix*.
One important feature is that the user can do string manipulations
on a file path without having to worry too much about the platform
because the path management code will properly handle most mixed cases.
So one can for example concatenate a path suffix that uses forward
slashes to a Windows path and have it work correctly.
The conversion code is in libdispatch/dpathmgr.c, and the
important function there is NCpathcvt which does the proper
conversions to the local path format.
As a rule, most code should just replace their file operations with
the corresponding NCxxx ones defined in include/ncpathmgmt.h. These
NCxxx functions all call NCpathcvt on their path arguments before
executing the actual file operation.
In some rare cases, the client may need to directly use NCpathcvt,
but this should be avoided as much as possible. If there is a need
for supporting a new file operation not already in ncpathmgmt.h, then
use the code in dpathmgr.c as a template. Also please notify Unidata
so we can include it as a formal part or our supported operations.
Also, if you see an operation in the library that is not using the
NCxxx form, then please submit an issue so we can fix it.
Misc. Changes:
* Clean up the utf8 testing code; it is impossible to get some
tests to work under windows using shell scripts; the args do
not pass as utf8 but as some other encoding.
* Added an extra utf8 test case: test_unicode_path.sh
* Add a true test for HDF5 1.10.6 or later because as noted in
PR https://github.com/Unidata/netcdf-c/pull/1794,
HDF5 changed its Windows file path handling.
2021-03-05 04:41:31 +08:00
|
|
|
f = NCfopen(path,"r");
|
2010-06-03 21:24:43 +08:00
|
|
|
if(f != NULL) { /* do not overwrite */
|
|
|
|
(void)fclose(f);
|
|
|
|
return EEXIST;
|
2017-04-19 23:29:35 +08:00
|
|
|
}
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
|
Codify cross-platform file paths
The netcdf-c code has to deal with a variety of platforms:
Windows, OSX, Linux, Cygwin, MSYS, etc. These platforms differ
significantly in the kind of file paths that they accept. So in
order to handle this, I have created a set of replacements for
the most common file system operations such as _open_ or _fopen_
or _access_ to manage the file path differences correctly.
A more limited version of this idea was already implemented via
the ncwinpath.h and dwinpath.c code. So this can be viewed as a
replacement for that code. And in path in many cases, the only
change that was required was to replace '#include <ncwinpath.h>'
with '#include <ncpathmgt.h>' and then replace file operation
calls with the NCxxx equivalent from ncpathmgr.h Note that
recently, the ncwinpath.h was renamed ncpathmgmt.h, so this pull
request should not require dealing with winpath.
The heart of the change is include/ncpathmgmt.h, which provides
alternate operations such as NCfopen or NCaccess and which properly
parse and rebuild path arguments to work for the platform on which
the code is executing. This mostly matters for Windows because of the
way that it uses backslash and drive letters, as compared to *nix*.
One important feature is that the user can do string manipulations
on a file path without having to worry too much about the platform
because the path management code will properly handle most mixed cases.
So one can for example concatenate a path suffix that uses forward
slashes to a Windows path and have it work correctly.
The conversion code is in libdispatch/dpathmgr.c, and the
important function there is NCpathcvt which does the proper
conversions to the local path format.
As a rule, most code should just replace their file operations with
the corresponding NCxxx ones defined in include/ncpathmgmt.h. These
NCxxx functions all call NCpathcvt on their path arguments before
executing the actual file operation.
In some rare cases, the client may need to directly use NCpathcvt,
but this should be avoided as much as possible. If there is a need
for supporting a new file operation not already in ncpathmgmt.h, then
use the code in dpathmgr.c as a template. Also please notify Unidata
so we can include it as a formal part or our supported operations.
Also, if you see an operation in the library that is not using the
NCxxx form, then please submit an issue so we can fix it.
Misc. Changes:
* Clean up the utf8 testing code; it is impossible to get some
tests to work under windows using shell scripts; the args do
not pass as utf8 but as some other encoding.
* Added an extra utf8 test case: test_unicode_path.sh
* Add a true test for HDF5 1.10.6 or later because as noted in
PR https://github.com/Unidata/netcdf-c/pull/1794,
HDF5 changed its Windows file path handling.
2021-03-05 04:41:31 +08:00
|
|
|
f = NCfopen(path, oflags);
|
2010-06-03 21:24:43 +08:00
|
|
|
if(f == NULL)
|
|
|
|
{
|
|
|
|
status = errno;
|
|
|
|
goto unwind_new;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Locate an open pseudo file descriptor */
|
|
|
|
fd = -1;
|
|
|
|
for(i=1;i<fdmax;i++) {if(descriptors[i] == NULL) {fd=i;break;}}
|
|
|
|
if(fd < 0) {fd = fdmax; fdmax++;}
|
|
|
|
descriptors[fd] = f;
|
|
|
|
|
|
|
|
*((int *)&nciop->fd) = fd; /* cast away const */
|
|
|
|
if(*sizehintp < NCIO_MINBLOCKSIZE || *sizehintp > NCIO_MAXBLOCKSIZE)
|
|
|
|
{
|
|
|
|
/* Use default */
|
|
|
|
*sizehintp = blksize(fd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*sizehintp = M_RNDUP(*sizehintp);
|
|
|
|
}
|
|
|
|
|
|
|
|
status = ncio_fileio_init2(nciop, sizehintp);
|
2016-03-08 01:58:02 +08:00
|
|
|
if(status != NC_NOERR)
|
2010-06-03 21:24:43 +08:00
|
|
|
goto unwind_open;
|
|
|
|
|
|
|
|
if(initialsz != 0)
|
|
|
|
{
|
|
|
|
status = fgrow(f, (off_t)initialsz);
|
2016-03-08 01:58:02 +08:00
|
|
|
if(status != NC_NOERR)
|
2010-06-03 21:24:43 +08:00
|
|
|
goto unwind_open;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(igetsz != 0)
|
|
|
|
{
|
|
|
|
status = nciop->get(nciop,
|
|
|
|
igeto, igetsz,
|
|
|
|
RGN_WRITE,
|
|
|
|
igetvpp);
|
2016-03-08 01:58:02 +08:00
|
|
|
if(status != NC_NOERR)
|
2010-06-03 21:24:43 +08:00
|
|
|
goto unwind_open;
|
|
|
|
}
|
|
|
|
|
|
|
|
*nciopp = nciop;
|
2016-03-08 01:58:02 +08:00
|
|
|
return NC_NOERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
unwind_open:
|
|
|
|
(void) fclose(descriptors[fd]);
|
|
|
|
descriptors[fd] = NULL;
|
|
|
|
/* ?? unlink */
|
|
|
|
/*FALLTHRU*/
|
|
|
|
unwind_new:
|
|
|
|
ncio_free(nciop);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
ncio_open(const char *path,
|
|
|
|
int ioflags,
|
|
|
|
off_t igeto, size_t igetsz, size_t *sizehintp,
|
2015-05-29 05:03:02 +08:00
|
|
|
void* parameters,
|
2010-06-03 21:24:43 +08:00
|
|
|
ncio **nciopp, void **const igetvpp)
|
|
|
|
{
|
|
|
|
ncio *nciop;
|
2021-12-24 13:18:56 +08:00
|
|
|
char* oflags = fIsSet(ioflags, NC_WRITE) ? "r+" : "r";
|
2010-06-03 21:24:43 +08:00
|
|
|
FILE* f;
|
|
|
|
int i,fd;
|
2016-03-08 01:58:02 +08:00
|
|
|
int status = NC_NOERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
if(path == NULL || *path == 0)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
nciop = ncio_new(path, ioflags);
|
|
|
|
if(nciop == NULL)
|
|
|
|
return ENOMEM;
|
|
|
|
|
Codify cross-platform file paths
The netcdf-c code has to deal with a variety of platforms:
Windows, OSX, Linux, Cygwin, MSYS, etc. These platforms differ
significantly in the kind of file paths that they accept. So in
order to handle this, I have created a set of replacements for
the most common file system operations such as _open_ or _fopen_
or _access_ to manage the file path differences correctly.
A more limited version of this idea was already implemented via
the ncwinpath.h and dwinpath.c code. So this can be viewed as a
replacement for that code. And in path in many cases, the only
change that was required was to replace '#include <ncwinpath.h>'
with '#include <ncpathmgt.h>' and then replace file operation
calls with the NCxxx equivalent from ncpathmgr.h Note that
recently, the ncwinpath.h was renamed ncpathmgmt.h, so this pull
request should not require dealing with winpath.
The heart of the change is include/ncpathmgmt.h, which provides
alternate operations such as NCfopen or NCaccess and which properly
parse and rebuild path arguments to work for the platform on which
the code is executing. This mostly matters for Windows because of the
way that it uses backslash and drive letters, as compared to *nix*.
One important feature is that the user can do string manipulations
on a file path without having to worry too much about the platform
because the path management code will properly handle most mixed cases.
So one can for example concatenate a path suffix that uses forward
slashes to a Windows path and have it work correctly.
The conversion code is in libdispatch/dpathmgr.c, and the
important function there is NCpathcvt which does the proper
conversions to the local path format.
As a rule, most code should just replace their file operations with
the corresponding NCxxx ones defined in include/ncpathmgmt.h. These
NCxxx functions all call NCpathcvt on their path arguments before
executing the actual file operation.
In some rare cases, the client may need to directly use NCpathcvt,
but this should be avoided as much as possible. If there is a need
for supporting a new file operation not already in ncpathmgmt.h, then
use the code in dpathmgr.c as a template. Also please notify Unidata
so we can include it as a formal part or our supported operations.
Also, if you see an operation in the library that is not using the
NCxxx form, then please submit an issue so we can fix it.
Misc. Changes:
* Clean up the utf8 testing code; it is impossible to get some
tests to work under windows using shell scripts; the args do
not pass as utf8 but as some other encoding.
* Added an extra utf8 test case: test_unicode_path.sh
* Add a true test for HDF5 1.10.6 or later because as noted in
PR https://github.com/Unidata/netcdf-c/pull/1794,
HDF5 changed its Windows file path handling.
2021-03-05 04:41:31 +08:00
|
|
|
f = NCfopen(path, oflags);
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
if(f == NULL)
|
|
|
|
{
|
|
|
|
status = errno;
|
|
|
|
goto unwind_new;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Locate an open pseudo file descriptor */
|
|
|
|
fd = -1;
|
|
|
|
for(i=1;i<fdmax;i++) {if(descriptors[i] == NULL) {fd=i;break;}}
|
|
|
|
if(fd < 0) {fd = fdmax; fdmax++;}
|
|
|
|
descriptors[fd] = f;
|
|
|
|
|
|
|
|
*((int *)&nciop->fd) = fd; /* cast away const */
|
|
|
|
|
|
|
|
if(*sizehintp < NCIO_MINBLOCKSIZE || *sizehintp > NCIO_MAXBLOCKSIZE)
|
|
|
|
{
|
|
|
|
/* Use default */
|
|
|
|
*sizehintp = blksize(fd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*sizehintp = M_RNDUP(*sizehintp);
|
|
|
|
}
|
|
|
|
|
|
|
|
status = ncio_fileio_init2(nciop, sizehintp);
|
2016-03-08 01:58:02 +08:00
|
|
|
if(status != NC_NOERR)
|
2010-06-03 21:24:43 +08:00
|
|
|
goto unwind_open;
|
|
|
|
|
|
|
|
if(igetsz != 0)
|
|
|
|
{
|
|
|
|
status = nciop->get(nciop,
|
|
|
|
igeto, igetsz,
|
|
|
|
0,
|
|
|
|
igetvpp);
|
2016-03-08 01:58:02 +08:00
|
|
|
if(status != NC_NOERR)
|
2010-06-03 21:24:43 +08:00
|
|
|
goto unwind_open;
|
|
|
|
}
|
|
|
|
|
|
|
|
*nciopp = nciop;
|
2016-03-08 01:58:02 +08:00
|
|
|
return NC_NOERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
unwind_open:
|
|
|
|
(void) fclose(descriptors[fd]);
|
|
|
|
descriptors[fd] = NULL;
|
|
|
|
/*FALLTHRU*/
|
|
|
|
unwind_new:
|
|
|
|
ncio_free(nciop);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-19 23:29:35 +08:00
|
|
|
/*
|
|
|
|
* Get file size in bytes.
|
2010-06-03 21:24:43 +08:00
|
|
|
* Is use of fstatus = fseek() really necessary, or could we use standard fstat() call
|
|
|
|
* and get st_size member?
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ncio_filesize(ncio *nciop, off_t *filesizep)
|
|
|
|
{
|
2016-03-08 01:58:02 +08:00
|
|
|
int status = NC_NOERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
off_t filesize, current, reset;
|
|
|
|
FILE* f;
|
|
|
|
|
|
|
|
if(nciop == NULL)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
f = descriptors[nciop->fd];
|
|
|
|
|
|
|
|
current = ftell(f);
|
|
|
|
status = fseek(f, 0, SEEK_END); /* get size */
|
|
|
|
if(ferror(f)) return EIO;
|
|
|
|
*filesizep = ftell(f);
|
2017-04-19 23:29:35 +08:00
|
|
|
status = fseek(f, current, SEEK_SET); /* reset */
|
2010-06-03 21:24:43 +08:00
|
|
|
if(ferror(f)) return EIO;
|
2016-03-08 01:58:02 +08:00
|
|
|
return NC_NOERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sync any changes to disk, then extend file so its size is length.
|
|
|
|
* This is only intended to be called before close, if the file is
|
|
|
|
* open for writing and the actual size does not match the calculated
|
|
|
|
* size, perhaps as the result of having been previously written in
|
|
|
|
* NOFILL mode.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ncio_pad_length(ncio *nciop, off_t length)
|
|
|
|
{
|
2016-03-08 01:58:02 +08:00
|
|
|
int status = NC_NOERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
FILE* f;
|
|
|
|
|
|
|
|
if(nciop == NULL)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
f = descriptors[nciop->fd];
|
|
|
|
|
|
|
|
if(!fIsSet(nciop->ioflags, NC_WRITE))
|
|
|
|
return EPERM; /* attempt to write readonly file */
|
|
|
|
|
|
|
|
status = nciop->sync(nciop);
|
2016-03-08 01:58:02 +08:00
|
|
|
if(status != NC_NOERR)
|
2010-06-03 21:24:43 +08:00
|
|
|
return status;
|
|
|
|
|
|
|
|
status = fgrow2(f, length);
|
2016-03-08 01:58:02 +08:00
|
|
|
if(status != NC_NOERR)
|
2010-06-03 21:24:43 +08:00
|
|
|
return errno;
|
2016-03-08 01:58:02 +08:00
|
|
|
return NC_NOERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-19 23:29:35 +08:00
|
|
|
int
|
2010-06-03 21:24:43 +08:00
|
|
|
ncio_close(ncio *nciop, int doUnlink)
|
|
|
|
{
|
2016-03-08 01:58:02 +08:00
|
|
|
int status = NC_NOERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
FILE* f;
|
|
|
|
|
|
|
|
if(nciop == NULL)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
f = descriptors[nciop->fd];
|
|
|
|
|
|
|
|
nciop->sync(nciop);
|
|
|
|
|
|
|
|
(void) fclose(f);
|
|
|
|
|
|
|
|
descriptors[nciop->fd] = NULL;
|
2017-04-19 23:29:35 +08:00
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
if(doUnlink)
|
|
|
|
(void) unlink(nciop->path);
|
|
|
|
|
|
|
|
ncio_free(nciop);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|