File: ncio.c

package info (click to toggle)
netcdf 1%3A4.9.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 115,904 kB
  • sloc: ansic: 278,886; sh: 14,183; cpp: 5,971; yacc: 2,612; makefile: 1,999; lex: 1,218; javascript: 280; xml: 173; awk: 2
file content (201 lines) | stat: -rw-r--r-- 5,860 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 *	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 "netcdf.h"
#include "ncio.h"
#include "fbits.h"
#include "ncuri.h"
#include "ncrc.h"

/* 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 */
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);

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);

#ifdef USE_FFIO
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);
#endif

#  ifdef USE_MMAP
     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);
#  endif

#ifdef NETCDF_ENABLE_BYTERANGE
    extern int httpio_open(const char*,int,off_t,size_t,size_t*,void*,ncio**,void** const);
#endif

#ifdef NETCDF_ENABLE_S3
    extern int s3io_open(const char*,int,off_t,size_t,size_t*,void*,ncio**,void** const);
#endif

     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);

/* Forward */
#ifdef NETCDF_ENABLE_BYTERANGE
static int urlmodetest(const char* path);
#endif

int
ncio_create(const char *path, int ioflags, size_t initialsz,
                       off_t igeto, size_t igetsz, size_t *sizehintp,
		       void* parameters,
                       ncio** iopp, void** const mempp)
{
    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);
    }
#  ifdef USE_MMAP
    else if(fIsSet(ioflags,NC_MMAP)) {
        return mmapio_create(path,ioflags,initialsz,igeto,igetsz,sizehintp,parameters,iopp,mempp);
    }
#  endif /*USE_MMAP*/

#ifdef USE_STDIO
    return stdio_create(path,ioflags,initialsz,igeto,igetsz,sizehintp,parameters,iopp,mempp);
#elif defined(USE_FFIO)
    return ffio_create(path,ioflags,initialsz,igeto,igetsz,sizehintp,parameters,iopp,mempp);
#else
    return posixio_create(path,ioflags,initialsz,igeto,igetsz,sizehintp,parameters,iopp,mempp);
#endif
}

int
ncio_open(const char *path, int ioflags,
                     off_t igeto, size_t igetsz, size_t *sizehintp,
		     void* parameters,
                     ncio** iopp, void** const mempp)
{
#ifdef NETCDF_ENABLE_BYTERANGE
    int modetest = urlmodetest(path);
#endif

    /* Diskless open has the following constraints:
       1. file must be classic version 1 or 2 or 5
     */
    if(fIsSet(ioflags,NC_DISKLESS)) {
        return memio_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp);
    }
    if(fIsSet(ioflags,NC_INMEMORY)) {
        return memio_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp);
    }
#  ifdef USE_MMAP
    if(fIsSet(ioflags,NC_MMAP)) {
        return mmapio_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp);
    }
#  endif /*USE_MMAP*/
#  ifdef NETCDF_ENABLE_BYTERANGE
   if(modetest == NC_HTTP) {
        return httpio_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp);
   }
#  ifdef NETCDF_ENABLE_S3
   if(modetest == NC_S3SDK) {
       return s3io_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp);
   }
#  endif
#  endif /*NETCDF_ENABLE_BYTERANGE*/

#ifdef USE_STDIO
    return stdio_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp);
#elif defined(USE_FFIO)
    return ffio_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp);
#else
    return posixio_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp);
#endif
}

/**************************************************/
/* wrapper functions for the ncio dispatch table */

int
ncio_rel(ncio* const nciop, off_t offset, int rflags)
{
    return nciop->rel(nciop,offset,rflags);
}

int
ncio_get(ncio* const nciop, off_t offset, size_t extent,
			int rflags, void **const vpp)
{
    return nciop->get(nciop,offset,extent,rflags,vpp);
}

int
ncio_move(ncio* const nciop, off_t to, off_t from, size_t nbytes, int rflags)
{
    return nciop->move(nciop,to,from,nbytes,rflags);
}

int
ncio_sync(ncio* const nciop)
{
    return nciop->sync(nciop);
}

int
ncio_filesize(ncio* const nciop, off_t *filesizep)
{
    return nciop->filesize(nciop,filesizep);
}

int
ncio_pad_length(ncio* const nciop, off_t length)
{
    return nciop->pad_length(nciop,length);
}

int
ncio_close(ncio* const nciop, int doUnlink)
{
    /* close and release all resources associated
       with nciop, including nciop
    */
    int status = nciop->close(nciop,doUnlink);
    return status;
}

/* URL utilities */

/*
Check mode flags and return:
NC_HTTP => byterange
NC_S3SDK => s3
0 => Not URL
*/
#ifdef NETCDF_ENABLE_BYTERANGE
static int
urlmodetest(const char* path)
{
    int kind = 0;
    NCURI* uri = NULL;
    
    ncuriparse(path,&uri);
    if(uri == NULL) return 0; /* Not URL */
    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
        kind = 0;
    ncurifree(uri);
    return kind;
}
#endif