2012-03-15 07:26:48 +08:00
|
|
|
/*
|
2018-12-07 05:34:40 +08:00
|
|
|
* Copyright 2018, University Corporation for Atmospheric Research
|
2012-03-15 07:26:48 +08:00
|
|
|
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.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"
|
|
|
|
|
2012-03-15 07:26:48 +08:00
|
|
|
/*
|
|
|
|
Implement the ncstdio.h interface using unix stdio.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Forward */
|
|
|
|
static int NCFile_read(ncstdio*,void*,const size_t,size_t*);
|
|
|
|
static int NCFile_write(ncstdio*,const void*,const size_t,size_t*);
|
|
|
|
static int NCFile_free(ncstdio*);
|
|
|
|
static int NCFile_close(ncstdio*,int);
|
|
|
|
static int NCFile_flush(ncstdio*)
|
|
|
|
static int NCFile_seek(ncstdio*,off_t);
|
|
|
|
static int NCFile_sync(ncstdio*,off_t);
|
|
|
|
static int NCFile_uid(ncstdio*,int*);
|
|
|
|
|
|
|
|
/* Define the stdio.h base operators */
|
|
|
|
|
|
|
|
|
|
|
|
struct NCFile_ops NCFile_ops = {
|
|
|
|
NCFile_read,
|
|
|
|
NCFile_write,
|
|
|
|
NCFile_free,
|
|
|
|
NCFile_close,
|
|
|
|
NCFile_flush,
|
|
|
|
NCFile_seek,
|
|
|
|
NCFile_sync,
|
|
|
|
NCFile_uid
|
|
|
|
};
|
|
|
|
|
|
|
|
/* In order to implement the close with delete, we
|
|
|
|
need the file path.
|
|
|
|
*/
|
|
|
|
struct ncFileState {
|
|
|
|
char* path;
|
|
|
|
File* file;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct ncFileState
|
|
|
|
getState(ncstdio* iop)
|
|
|
|
{
|
|
|
|
if(iop != NULL) {
|
|
|
|
if(iop->state != NULL) {
|
|
|
|
return (struct ncFileState*)iop->state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ncFile_create(const char *path, int ioflags, ncstdio** filepp)
|
|
|
|
{
|
|
|
|
ncstdio* filep;
|
|
|
|
File* f;
|
|
|
|
struct ncFileState* state;
|
|
|
|
|
2022-02-09 11:53:30 +08:00
|
|
|
f = NCfopen(path,"w+");
|
2012-03-15 07:26:48 +08:00
|
|
|
if(f == NULL)
|
|
|
|
return errno;
|
|
|
|
filep = (ncstdio*)calloc(sizeof(ncstdio),1);
|
|
|
|
if(filep == NULL) {fclose(f); return NC_ENOMEM;}
|
|
|
|
state = (ncstdio*)calloc(sizeof(ncFileState),1);
|
|
|
|
if(state == NULL) {fclose(f); free(filep); return NC_ENOMEM;}
|
|
|
|
filep->ops = &NCFILE_ops;
|
|
|
|
filep->ioflags = ioflags;
|
|
|
|
filep->state = (void*)state;
|
|
|
|
state->path = strdup(path);
|
|
|
|
state->file = f;
|
|
|
|
if(filepp) *filepp = filep;
|
|
|
|
return NC_NOERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ncFile_open(const char *path, int ioflags, ncstdio** filepp)
|
|
|
|
{
|
|
|
|
ncstdio* filep;
|
|
|
|
File* f;
|
|
|
|
|
|
|
|
if(fIsSet(ioflags,NC_NOCLOBBER))
|
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");
|
2012-03-15 07:26:48 +08:00
|
|
|
else
|
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,"w+");
|
2012-03-15 07:26:48 +08:00
|
|
|
if(f == NULL)
|
|
|
|
return errno;
|
|
|
|
|
|
|
|
filep = (ncstdio*)calloc(sizeof(ncstdio),1);
|
|
|
|
if(filep == NULL) {fclose(f); return NC_ENOMEM;}
|
|
|
|
state = (ncstdio*)calloc(sizeof(ncFileState),1);
|
|
|
|
if(state == NULL) {fclose(f); free(filep); return NC_ENOMEM;}
|
|
|
|
filep->ops = &NCFILE_ops;
|
|
|
|
filep->ioflags = ioflags;
|
|
|
|
filep->state = (void*)state;
|
|
|
|
state->path = strdup(path);
|
|
|
|
state->file = f;
|
|
|
|
if(filepp) *filepp = filep;
|
|
|
|
return NC_NOERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NCFile_close(ncstdio* filep, int delfile)
|
|
|
|
{
|
|
|
|
struct ncFileState* state;
|
|
|
|
if(filep == NULL) return NC_EINVAL;
|
|
|
|
state = (struct ncFileState*)filep->state;
|
|
|
|
if(state == NULL || state->file == NULL) return NC_NOERR;
|
|
|
|
fclose(state->file);
|
|
|
|
state->file = NULL;
|
|
|
|
if(delfile)
|
|
|
|
unlink(state->path);
|
|
|
|
return NC_NOERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NCFile_free(ncstdio* filep)
|
|
|
|
{
|
|
|
|
struct ncFileState* state;
|
|
|
|
if(filep == NULL) return NC_NOERR;
|
|
|
|
state = (struct ncFileState*)filep->state;
|
|
|
|
if(state != NULL) {
|
|
|
|
if(state->file != NULL) return NC_EINVAL;
|
|
|
|
if(state->path != NULL)
|
|
|
|
free(state->path);
|
|
|
|
free(state);
|
|
|
|
}
|
|
|
|
free(filep);
|
|
|
|
return NC_NOERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NCFile_flush(ncstdio* filep);
|
|
|
|
{
|
|
|
|
File* state;
|
|
|
|
if(filep == NULL) return NC_EINVAL;
|
|
|
|
state = (struct ncFileState*)filep->state;
|
|
|
|
if(state == NULL) return NC_EINVAL;
|
|
|
|
if(state->file == NULL) return NC_EINVAL;
|
|
|
|
fflush(state->file);
|
|
|
|
return NC_NOERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NCFile_sync(ncstdio* filep);
|
|
|
|
{
|
|
|
|
File* state;
|
|
|
|
#ifdef USE_FSYNC
|
|
|
|
int fd;
|
|
|
|
#endif
|
|
|
|
if(filep == NULL) return NC_EINVAL;
|
|
|
|
state = (struct ncFileState*)filep->state;
|
|
|
|
if(state == NULL) return NC_EINVAL;
|
|
|
|
if(state->file == NULL) return NC_EINVAL;
|
|
|
|
#ifdef HAVE_FSYNC
|
|
|
|
#ifdef USE_FSYNC
|
|
|
|
fd = fileno(state->file);
|
2019-11-07 20:48:44 +08:00
|
|
|
#ifndef _WIN32
|
2012-03-15 07:26:48 +08:00
|
|
|
fsync(fd);
|
|
|
|
#else
|
|
|
|
_commit(fd);
|
2019-11-07 20:48:44 +08:00
|
|
|
#endif /* _WIN32 */
|
2012-03-15 07:26:48 +08:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
return NC_NOERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NCFile_seek(ncstdio* filep, off_t pos);
|
|
|
|
{
|
|
|
|
struct ncFileState* state;
|
|
|
|
if(filep == NULL) return NC_EINVAL;
|
|
|
|
state = (struct ncFileState*)filep->state;
|
|
|
|
if(state == NULL) return NC_EINVAL;
|
|
|
|
if(state->file == NULL) return NC_EINVAL;
|
|
|
|
if(!fseek(state->file,pos)) return (errno > 0 ?errno : EINVAL);
|
|
|
|
return NC_NOERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NCFile_read(ncstdio* filep, void* memory, const size_t size, size_t* actualp);
|
|
|
|
{
|
|
|
|
struct ncFileState* state;
|
|
|
|
size_t actual;
|
|
|
|
if(filep == NULL) return NC_EINVAL;
|
|
|
|
state = (struct ncFileState*)filep->state;
|
|
|
|
if(state == NULL || state->file == NULL) return NC_EINVAL;
|
|
|
|
actual = fread(memory,1,size,state->file);
|
|
|
|
if(actualp) *actualp = actual;
|
|
|
|
return (actual < size ? NC_EIO : NC_NOERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NCFile_write(ncstdio* filep, const void* memory, const size_t size, size_t* actual);
|
|
|
|
{
|
|
|
|
struct ncFileState* state;
|
|
|
|
size_t actual;
|
|
|
|
if(filep == NULL) return NC_EINVAL;
|
|
|
|
state = (struct ncFileState*)filep->state;
|
|
|
|
if(state == NULL || state->file == NULL) return NC_EINVAL;
|
|
|
|
actual = fwrite(memory,1,size,state->file);
|
|
|
|
if(actualp) *actualp = actual;
|
|
|
|
return (actual < size ? NC_EIO : NC_NOERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NCFile_uid(ncstdio* filep, int* idp)
|
|
|
|
{
|
|
|
|
struct ncFileState* state;
|
|
|
|
if(filep == NULL) return NC_EINVAL;
|
|
|
|
state = (struct ncFileState*)filep->state;
|
|
|
|
if(state == NULL || state->file == NULL) return NC_EINVAL;
|
|
|
|
if(idp) *idp = fileno(state->file);
|
|
|
|
return NC_NOERR;
|
|
|
|
}
|
|
|
|
|