2012-03-26 09:34:32 +08:00
|
|
|
/*
|
2018-12-07 05:34:40 +08:00
|
|
|
* Copyright 2018, University Corporation for Atmospheric Research
|
2012-03-26 09:34:32 +08:00
|
|
|
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
|
|
|
|
*/
|
|
|
|
|
Provide byte-range reading of remote datasets
re: issue https://github.com/Unidata/netcdf-c/issues/1251
Assume that you have the URL to a remote dataset
which is a normal netcdf-3 or netcdf-4 file.
This PR allows the netcdf-c to read that dataset's
contents as a netcdf file using HTTP byte ranges
if the remote server supports byte-range access.
Originally, this PR was set up to access Amazon S3 objects,
but it can also access other remote datasets such as those
provided by a Thredds server via the HTTPServer access protocol.
It may also work for other kinds of servers.
Note that this is not intended as a true production
capability because, as is known, this kind of access to
can be quite slow. In addition, the byte-range IO drivers
do not currently do any sort of optimization or caching.
An additional goal here is to gain some experience with
the Amazon S3 REST protocol.
This architecture and its use documented in
the file docs/byterange.dox.
There are currently two test cases:
1. nc_test/tst_s3raw.c - this does a simple open, check format, close cycle
for a remote netcdf-3 file and a remote netcdf-4 file.
2. nc_test/test_s3raw.sh - this uses ncdump to investigate some remote
datasets.
This PR also incorporates significantly changed model inference code
(see the superceded PR https://github.com/Unidata/netcdf-c/pull/1259).
1. It centralizes the code that infers the dispatcher.
2. It adds support for byte-range URLs
Other changes:
1. NC_HDF5_finalize was not being properly called by nc_finalize().
2. Fix minor bug in ncgen3.l
3. fix memory leak in nc4info.c
4. add code to walk the .daprc triples and to replace protocol=
fragment tag with a more general mode= tag.
Final Note:
Th inference code is still way too complicated. We need to move
to the validfile() model used by netcdf Java, where each
dispatcher is asked if it can process the file. This decentralizes
the inference code. This will be done after all the major new
dispatchers (PIO, Zarr, etc) have been implemented.
2019-01-02 09:27:36 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
2017-03-12 03:03:17 +08:00
|
|
|
#endif
|
|
|
|
|
2012-03-26 09:34:32 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "netcdf.h"
|
|
|
|
#include "ncio.h"
|
|
|
|
#include "fbits.h"
|
2022-01-12 10:05:46 +08:00
|
|
|
#include "ncuri.h"
|
|
|
|
#include "ncrc.h"
|
2012-03-26 09:34:32 +08:00
|
|
|
|
|
|
|
/* With the advent of diskless io, we need to provide
|
|
|
|
for multiple ncio packages at the same time,
|
|
|
|
so we have multiple versions of ncio_create.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Define known ncio packages */
|
2015-05-29 05:03:02 +08:00
|
|
|
extern int posixio_create(const char*,int,size_t,off_t,size_t,size_t*,void*,ncio**,void** const);
|
|
|
|
extern int posixio_open(const char*,int,off_t,size_t,size_t*,void*,ncio**,void** const);
|
2012-03-26 09:34:32 +08:00
|
|
|
|
2015-08-20 07:14:13 +08:00
|
|
|
extern int stdio_create(const char*,int,size_t,off_t,size_t,size_t*,void*,ncio**,void** const);
|
|
|
|
extern int stdio_open(const char*,int,off_t,size_t,size_t*,void*,ncio**,void** const);
|
|
|
|
|
2012-04-13 10:41:00 +08:00
|
|
|
#ifdef USE_FFIO
|
2015-05-29 05:03:02 +08:00
|
|
|
extern int ffio_create(const char*,int,size_t,off_t,size_t,size_t*,void*,ncio**,void** const);
|
|
|
|
extern int ffio_open(const char*,int,off_t,size_t,size_t*,void*,ncio**,void** const);
|
2012-04-13 10:41:00 +08:00
|
|
|
#endif
|
|
|
|
|
2012-05-15 11:13:08 +08:00
|
|
|
# ifdef USE_MMAP
|
2015-05-29 05:03:02 +08:00
|
|
|
extern int mmapio_create(const char*,int,size_t,off_t,size_t,size_t*,void*,ncio**,void** const);
|
|
|
|
extern int mmapio_open(const char*,int,off_t,size_t,size_t*,void*,ncio**,void** const);
|
2012-06-24 03:25:49 +08:00
|
|
|
# endif
|
Provide byte-range reading of remote datasets
re: issue https://github.com/Unidata/netcdf-c/issues/1251
Assume that you have the URL to a remote dataset
which is a normal netcdf-3 or netcdf-4 file.
This PR allows the netcdf-c to read that dataset's
contents as a netcdf file using HTTP byte ranges
if the remote server supports byte-range access.
Originally, this PR was set up to access Amazon S3 objects,
but it can also access other remote datasets such as those
provided by a Thredds server via the HTTPServer access protocol.
It may also work for other kinds of servers.
Note that this is not intended as a true production
capability because, as is known, this kind of access to
can be quite slow. In addition, the byte-range IO drivers
do not currently do any sort of optimization or caching.
An additional goal here is to gain some experience with
the Amazon S3 REST protocol.
This architecture and its use documented in
the file docs/byterange.dox.
There are currently two test cases:
1. nc_test/tst_s3raw.c - this does a simple open, check format, close cycle
for a remote netcdf-3 file and a remote netcdf-4 file.
2. nc_test/test_s3raw.sh - this uses ncdump to investigate some remote
datasets.
This PR also incorporates significantly changed model inference code
(see the superceded PR https://github.com/Unidata/netcdf-c/pull/1259).
1. It centralizes the code that infers the dispatcher.
2. It adds support for byte-range URLs
Other changes:
1. NC_HDF5_finalize was not being properly called by nc_finalize().
2. Fix minor bug in ncgen3.l
3. fix memory leak in nc4info.c
4. add code to walk the .daprc triples and to replace protocol=
fragment tag with a more general mode= tag.
Final Note:
Th inference code is still way too complicated. We need to move
to the validfile() model used by netcdf Java, where each
dispatcher is asked if it can process the file. This decentralizes
the inference code. This will be done after all the major new
dispatchers (PIO, Zarr, etc) have been implemented.
2019-01-02 09:27:36 +08:00
|
|
|
|
2019-02-25 07:54:13 +08:00
|
|
|
#ifdef ENABLE_BYTERANGE
|
Provide byte-range reading of remote datasets
re: issue https://github.com/Unidata/netcdf-c/issues/1251
Assume that you have the URL to a remote dataset
which is a normal netcdf-3 or netcdf-4 file.
This PR allows the netcdf-c to read that dataset's
contents as a netcdf file using HTTP byte ranges
if the remote server supports byte-range access.
Originally, this PR was set up to access Amazon S3 objects,
but it can also access other remote datasets such as those
provided by a Thredds server via the HTTPServer access protocol.
It may also work for other kinds of servers.
Note that this is not intended as a true production
capability because, as is known, this kind of access to
can be quite slow. In addition, the byte-range IO drivers
do not currently do any sort of optimization or caching.
An additional goal here is to gain some experience with
the Amazon S3 REST protocol.
This architecture and its use documented in
the file docs/byterange.dox.
There are currently two test cases:
1. nc_test/tst_s3raw.c - this does a simple open, check format, close cycle
for a remote netcdf-3 file and a remote netcdf-4 file.
2. nc_test/test_s3raw.sh - this uses ncdump to investigate some remote
datasets.
This PR also incorporates significantly changed model inference code
(see the superceded PR https://github.com/Unidata/netcdf-c/pull/1259).
1. It centralizes the code that infers the dispatcher.
2. It adds support for byte-range URLs
Other changes:
1. NC_HDF5_finalize was not being properly called by nc_finalize().
2. Fix minor bug in ncgen3.l
3. fix memory leak in nc4info.c
4. add code to walk the .daprc triples and to replace protocol=
fragment tag with a more general mode= tag.
Final Note:
Th inference code is still way too complicated. We need to move
to the validfile() model used by netcdf Java, where each
dispatcher is asked if it can process the file. This decentralizes
the inference code. This will be done after all the major new
dispatchers (PIO, Zarr, etc) have been implemented.
2019-01-02 09:27:36 +08:00
|
|
|
extern int httpio_open(const char*,int,off_t,size_t,size_t*,void*,ncio**,void** const);
|
|
|
|
#endif
|
|
|
|
|
2021-10-30 10:06:37 +08:00
|
|
|
#ifdef ENABLE_S3_SDK
|
|
|
|
extern int s3io_open(const char*,int,off_t,size_t,size_t*,void*,ncio**,void** const);
|
|
|
|
#endif
|
|
|
|
|
2015-05-29 05:03:02 +08:00
|
|
|
extern int memio_create(const char*,int,size_t,off_t,size_t,size_t*,void*,ncio**,void** const);
|
|
|
|
extern int memio_open(const char*,int,off_t,size_t,size_t*,void*,ncio**,void** const);
|
2012-03-26 09:34:32 +08:00
|
|
|
|
2022-01-12 10:05:46 +08:00
|
|
|
/* Forward */
|
|
|
|
static int urlmodetest(const char* path);
|
|
|
|
|
2012-03-26 09:34:32 +08:00
|
|
|
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,
|
2012-03-26 09:34:32 +08:00
|
|
|
ncio** iopp, void** const mempp)
|
|
|
|
{
|
2018-10-11 03:32:17 +08:00
|
|
|
if(fIsSet(ioflags,NC_DISKLESS)) {
|
|
|
|
return memio_create(path,ioflags,initialsz,igeto,igetsz,sizehintp,parameters,iopp,mempp);
|
|
|
|
} else if(fIsSet(ioflags,NC_INMEMORY)) {
|
|
|
|
return memio_create(path,ioflags,initialsz,igeto,igetsz,sizehintp,parameters,iopp,mempp);
|
|
|
|
}
|
2012-05-15 11:13:08 +08:00
|
|
|
# ifdef USE_MMAP
|
2018-10-11 03:32:17 +08:00
|
|
|
else if(fIsSet(ioflags,NC_MMAP)) {
|
2015-05-29 05:03:02 +08:00
|
|
|
return mmapio_create(path,ioflags,initialsz,igeto,igetsz,sizehintp,parameters,iopp,mempp);
|
2012-06-24 03:25:49 +08:00
|
|
|
}
|
2018-10-11 03:32:17 +08:00
|
|
|
# endif /*USE_MMAP*/
|
2012-04-13 10:41:00 +08:00
|
|
|
|
2015-08-20 07:14:13 +08:00
|
|
|
#ifdef USE_STDIO
|
|
|
|
return stdio_create(path,ioflags,initialsz,igeto,igetsz,sizehintp,parameters,iopp,mempp);
|
|
|
|
#elif defined(USE_FFIO)
|
2015-05-29 05:03:02 +08:00
|
|
|
return ffio_create(path,ioflags,initialsz,igeto,igetsz,sizehintp,parameters,iopp,mempp);
|
2012-03-26 09:34:32 +08:00
|
|
|
#else
|
2015-05-29 05:03:02 +08:00
|
|
|
return posixio_create(path,ioflags,initialsz,igeto,igetsz,sizehintp,parameters,iopp,mempp);
|
2012-03-26 09:34:32 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2012-03-26 09:34:32 +08:00
|
|
|
ncio** iopp, void** const mempp)
|
|
|
|
{
|
2022-02-09 11:53:30 +08:00
|
|
|
#ifdef ENABLE_BYTERANGE
|
2022-01-12 10:05:46 +08:00
|
|
|
int modetest = urlmodetest(path);
|
2022-02-09 11:53:30 +08:00
|
|
|
#endif
|
2022-01-12 10:05:46 +08:00
|
|
|
|
2012-04-09 06:47:38 +08:00
|
|
|
/* Diskless open has the following constraints:
|
2018-02-26 12:45:31 +08:00
|
|
|
1. file must be classic version 1 or 2 or 5
|
2012-04-09 06:47:38 +08:00
|
|
|
*/
|
2018-10-11 03:32:17 +08:00
|
|
|
if(fIsSet(ioflags,NC_DISKLESS)) {
|
|
|
|
return memio_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp);
|
|
|
|
}
|
2018-02-26 12:45:31 +08:00
|
|
|
if(fIsSet(ioflags,NC_INMEMORY)) {
|
2018-10-11 03:32:17 +08:00
|
|
|
return memio_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp);
|
|
|
|
}
|
2012-05-15 11:13:08 +08:00
|
|
|
# ifdef USE_MMAP
|
2018-10-11 03:32:17 +08:00
|
|
|
if(fIsSet(ioflags,NC_MMAP)) {
|
2015-05-29 05:03:02 +08:00
|
|
|
return mmapio_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp);
|
2012-04-09 06:47:38 +08:00
|
|
|
}
|
2018-10-11 03:32:17 +08:00
|
|
|
# endif /*USE_MMAP*/
|
2019-02-25 07:54:13 +08:00
|
|
|
# ifdef ENABLE_BYTERANGE
|
2022-01-12 10:05:46 +08:00
|
|
|
if(modetest == NC_HTTP) {
|
Provide byte-range reading of remote datasets
re: issue https://github.com/Unidata/netcdf-c/issues/1251
Assume that you have the URL to a remote dataset
which is a normal netcdf-3 or netcdf-4 file.
This PR allows the netcdf-c to read that dataset's
contents as a netcdf file using HTTP byte ranges
if the remote server supports byte-range access.
Originally, this PR was set up to access Amazon S3 objects,
but it can also access other remote datasets such as those
provided by a Thredds server via the HTTPServer access protocol.
It may also work for other kinds of servers.
Note that this is not intended as a true production
capability because, as is known, this kind of access to
can be quite slow. In addition, the byte-range IO drivers
do not currently do any sort of optimization or caching.
An additional goal here is to gain some experience with
the Amazon S3 REST protocol.
This architecture and its use documented in
the file docs/byterange.dox.
There are currently two test cases:
1. nc_test/tst_s3raw.c - this does a simple open, check format, close cycle
for a remote netcdf-3 file and a remote netcdf-4 file.
2. nc_test/test_s3raw.sh - this uses ncdump to investigate some remote
datasets.
This PR also incorporates significantly changed model inference code
(see the superceded PR https://github.com/Unidata/netcdf-c/pull/1259).
1. It centralizes the code that infers the dispatcher.
2. It adds support for byte-range URLs
Other changes:
1. NC_HDF5_finalize was not being properly called by nc_finalize().
2. Fix minor bug in ncgen3.l
3. fix memory leak in nc4info.c
4. add code to walk the .daprc triples and to replace protocol=
fragment tag with a more general mode= tag.
Final Note:
Th inference code is still way too complicated. We need to move
to the validfile() model used by netcdf Java, where each
dispatcher is asked if it can process the file. This decentralizes
the inference code. This will be done after all the major new
dispatchers (PIO, Zarr, etc) have been implemented.
2019-01-02 09:27:36 +08:00
|
|
|
return httpio_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp);
|
|
|
|
}
|
2021-10-30 10:06:37 +08:00
|
|
|
# ifdef ENABLE_S3_SDK
|
2022-01-12 10:05:46 +08:00
|
|
|
if(modetest == NC_S3SDK) {
|
2021-10-30 10:06:37 +08:00
|
|
|
return s3io_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp);
|
|
|
|
}
|
|
|
|
# endif
|
2019-02-25 07:54:13 +08:00
|
|
|
# endif /*ENABLE_BYTERANGE*/
|
Provide byte-range reading of remote datasets
re: issue https://github.com/Unidata/netcdf-c/issues/1251
Assume that you have the URL to a remote dataset
which is a normal netcdf-3 or netcdf-4 file.
This PR allows the netcdf-c to read that dataset's
contents as a netcdf file using HTTP byte ranges
if the remote server supports byte-range access.
Originally, this PR was set up to access Amazon S3 objects,
but it can also access other remote datasets such as those
provided by a Thredds server via the HTTPServer access protocol.
It may also work for other kinds of servers.
Note that this is not intended as a true production
capability because, as is known, this kind of access to
can be quite slow. In addition, the byte-range IO drivers
do not currently do any sort of optimization or caching.
An additional goal here is to gain some experience with
the Amazon S3 REST protocol.
This architecture and its use documented in
the file docs/byterange.dox.
There are currently two test cases:
1. nc_test/tst_s3raw.c - this does a simple open, check format, close cycle
for a remote netcdf-3 file and a remote netcdf-4 file.
2. nc_test/test_s3raw.sh - this uses ncdump to investigate some remote
datasets.
This PR also incorporates significantly changed model inference code
(see the superceded PR https://github.com/Unidata/netcdf-c/pull/1259).
1. It centralizes the code that infers the dispatcher.
2. It adds support for byte-range URLs
Other changes:
1. NC_HDF5_finalize was not being properly called by nc_finalize().
2. Fix minor bug in ncgen3.l
3. fix memory leak in nc4info.c
4. add code to walk the .daprc triples and to replace protocol=
fragment tag with a more general mode= tag.
Final Note:
Th inference code is still way too complicated. We need to move
to the validfile() model used by netcdf Java, where each
dispatcher is asked if it can process the file. This decentralizes
the inference code. This will be done after all the major new
dispatchers (PIO, Zarr, etc) have been implemented.
2019-01-02 09:27:36 +08:00
|
|
|
|
2015-08-20 07:14:13 +08:00
|
|
|
#ifdef USE_STDIO
|
|
|
|
return stdio_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp);
|
|
|
|
#elif defined(USE_FFIO)
|
2015-05-29 05:03:02 +08:00
|
|
|
return ffio_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp);
|
2012-03-26 09:34:32 +08:00
|
|
|
#else
|
2015-05-29 05:03:02 +08:00
|
|
|
return posixio_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp);
|
2012-03-26 09:34:32 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/**************************************************/
|
|
|
|
/* wrapper functions for the ncio dispatch table */
|
|
|
|
|
|
|
|
int
|
2017-03-09 08:01:10 +08:00
|
|
|
ncio_rel(ncio* const nciop, off_t offset, int rflags)
|
2012-03-26 09:34:32 +08:00
|
|
|
{
|
|
|
|
return nciop->rel(nciop,offset,rflags);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2017-03-09 08:01:10 +08:00
|
|
|
ncio_get(ncio* const nciop, off_t offset, size_t extent,
|
2012-03-26 09:34:32 +08:00
|
|
|
int rflags, void **const vpp)
|
|
|
|
{
|
|
|
|
return nciop->get(nciop,offset,extent,rflags,vpp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2017-03-09 08:01:10 +08:00
|
|
|
ncio_move(ncio* const nciop, off_t to, off_t from, size_t nbytes, int rflags)
|
2012-03-26 09:34:32 +08:00
|
|
|
{
|
|
|
|
return nciop->move(nciop,to,from,nbytes,rflags);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2017-03-09 08:01:10 +08:00
|
|
|
ncio_sync(ncio* const nciop)
|
2012-03-26 09:34:32 +08:00
|
|
|
{
|
|
|
|
return nciop->sync(nciop);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2017-03-09 08:01:10 +08:00
|
|
|
ncio_filesize(ncio* const nciop, off_t *filesizep)
|
2012-03-26 09:34:32 +08:00
|
|
|
{
|
|
|
|
return nciop->filesize(nciop,filesizep);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2017-03-09 08:01:10 +08:00
|
|
|
ncio_pad_length(ncio* const nciop, off_t length)
|
2012-03-26 09:34:32 +08:00
|
|
|
{
|
|
|
|
return nciop->pad_length(nciop,length);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2017-03-09 08:01:10 +08:00
|
|
|
ncio_close(ncio* const nciop, int doUnlink)
|
2012-03-26 09:34:32 +08:00
|
|
|
{
|
|
|
|
/* close and release all resources associated
|
|
|
|
with nciop, including nciop
|
|
|
|
*/
|
|
|
|
int status = nciop->close(nciop,doUnlink);
|
|
|
|
return status;
|
|
|
|
}
|
2022-01-12 10:05:46 +08:00
|
|
|
|
|
|
|
/* URL utilities */
|
|
|
|
|
|
|
|
/*
|
|
|
|
Check mode flags and return:
|
|
|
|
NC_HTTP => byterange
|
|
|
|
NC_S3SDK => s3
|
|
|
|
0 => Not URL
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
urlmodetest(const char* path)
|
|
|
|
{
|
|
|
|
int kind = 0;
|
|
|
|
NCURI* uri = NULL;
|
|
|
|
|
|
|
|
ncuriparse(path,&uri);
|
|
|
|
if(uri == NULL) return 0; /* Not URL */
|
2022-01-25 06:22:24 +08:00
|
|
|
if(NC_testmode(uri, "bytes")) {
|
|
|
|
/* NC_S3SDK takes priority over NC_HTTP */
|
|
|
|
if(NC_testmode(uri, "s3")) kind = NC_S3SDK; else kind = NC_HTTP;
|
|
|
|
} else
|
2022-01-12 10:05:46 +08:00
|
|
|
kind = 0;
|
|
|
|
ncurifree(uri);
|
|
|
|
return kind;
|
|
|
|
}
|