Coverage Report

Created: 2023-05-28 06:42

/src/netcdf-c/libsrc/ncio.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *  Copyright 2018, University Corporation for Atmospheric Research
3
 *      See netcdf/COPYRIGHT file for copying and redistribution conditions.
4
 */
5
6
#ifdef HAVE_CONFIG_H
7
#include "config.h"
8
#endif
9
10
#include <stdlib.h>
11
12
#include "netcdf.h"
13
#include "ncio.h"
14
#include "fbits.h"
15
#include "ncuri.h"
16
#include "ncrc.h"
17
18
/* With the advent of diskless io, we need to provide
19
   for multiple ncio packages at the same time,
20
   so we have multiple versions of ncio_create.
21
*/
22
23
/* Define known ncio packages */
24
extern int posixio_create(const char*,int,size_t,off_t,size_t,size_t*,void*,ncio**,void** const);
25
extern int posixio_open(const char*,int,off_t,size_t,size_t*,void*,ncio**,void** const);
26
27
extern int stdio_create(const char*,int,size_t,off_t,size_t,size_t*,void*,ncio**,void** const);
28
extern int stdio_open(const char*,int,off_t,size_t,size_t*,void*,ncio**,void** const);
29
30
#ifdef USE_FFIO
31
extern int ffio_create(const char*,int,size_t,off_t,size_t,size_t*,void*,ncio**,void** const);
32
extern int ffio_open(const char*,int,off_t,size_t,size_t*,void*,ncio**,void** const);
33
#endif
34
35
#  ifdef USE_MMAP
36
     extern int mmapio_create(const char*,int,size_t,off_t,size_t,size_t*,void*,ncio**,void** const);
37
     extern int mmapio_open(const char*,int,off_t,size_t,size_t*,void*,ncio**,void** const);
38
#  endif
39
40
#ifdef ENABLE_BYTERANGE
41
    extern int httpio_open(const char*,int,off_t,size_t,size_t*,void*,ncio**,void** const);
42
#endif
43
44
#ifdef ENABLE_S3
45
    extern int s3io_open(const char*,int,off_t,size_t,size_t*,void*,ncio**,void** const);
46
#endif
47
48
     extern int memio_create(const char*,int,size_t,off_t,size_t,size_t*,void*,ncio**,void** const);
49
     extern int memio_open(const char*,int,off_t,size_t,size_t*,void*,ncio**,void** const);
50
51
/* Forward */
52
#ifdef ENABLE_BYTERANGE
53
static int urlmodetest(const char* path);
54
#endif
55
56
int
57
ncio_create(const char *path, int ioflags, size_t initialsz,
58
                       off_t igeto, size_t igetsz, size_t *sizehintp,
59
           void* parameters,
60
                       ncio** iopp, void** const mempp)
61
0
{
62
0
    if(fIsSet(ioflags,NC_DISKLESS)) {
63
0
        return memio_create(path,ioflags,initialsz,igeto,igetsz,sizehintp,parameters,iopp,mempp);
64
0
    } else if(fIsSet(ioflags,NC_INMEMORY)) {
65
0
        return memio_create(path,ioflags,initialsz,igeto,igetsz,sizehintp,parameters,iopp,mempp);
66
0
    }
67
0
#  ifdef USE_MMAP
68
0
    else if(fIsSet(ioflags,NC_MMAP)) {
69
0
        return mmapio_create(path,ioflags,initialsz,igeto,igetsz,sizehintp,parameters,iopp,mempp);
70
0
    }
71
0
#  endif /*USE_MMAP*/
72
73
#ifdef USE_STDIO
74
    return stdio_create(path,ioflags,initialsz,igeto,igetsz,sizehintp,parameters,iopp,mempp);
75
#elif defined(USE_FFIO)
76
    return ffio_create(path,ioflags,initialsz,igeto,igetsz,sizehintp,parameters,iopp,mempp);
77
#else
78
0
    return posixio_create(path,ioflags,initialsz,igeto,igetsz,sizehintp,parameters,iopp,mempp);
79
0
#endif
80
0
}
81
82
int
83
ncio_open(const char *path, int ioflags,
84
                     off_t igeto, size_t igetsz, size_t *sizehintp,
85
         void* parameters,
86
                     ncio** iopp, void** const mempp)
87
104
{
88
104
#ifdef ENABLE_BYTERANGE
89
104
    int modetest = urlmodetest(path);
90
104
#endif
91
92
    /* Diskless open has the following constraints:
93
       1. file must be classic version 1 or 2 or 5
94
     */
95
104
    if(fIsSet(ioflags,NC_DISKLESS)) {
96
0
        return memio_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp);
97
0
    }
98
104
    if(fIsSet(ioflags,NC_INMEMORY)) {
99
104
        return memio_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp);
100
104
    }
101
0
#  ifdef USE_MMAP
102
0
    if(fIsSet(ioflags,NC_MMAP)) {
103
0
        return mmapio_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp);
104
0
    }
105
0
#  endif /*USE_MMAP*/
106
0
#  ifdef ENABLE_BYTERANGE
107
0
   if(modetest == NC_HTTP) {
108
0
        return httpio_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp);
109
0
   }
110
#  ifdef ENABLE_S3
111
   if(modetest == NC_S3SDK) {
112
       return s3io_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp);
113
   }
114
#  endif
115
0
#  endif /*ENABLE_BYTERANGE*/
116
117
#ifdef USE_STDIO
118
    return stdio_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp);
119
#elif defined(USE_FFIO)
120
    return ffio_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp);
121
#else
122
0
    return posixio_open(path,ioflags,igeto,igetsz,sizehintp,parameters,iopp,mempp);
123
0
#endif
124
0
}
125
126
/**************************************************/
127
/* wrapper functions for the ncio dispatch table */
128
129
int
130
ncio_rel(ncio* const nciop, off_t offset, int rflags)
131
4.05k
{
132
4.05k
    return nciop->rel(nciop,offset,rflags);
133
4.05k
}
134
135
int
136
ncio_get(ncio* const nciop, off_t offset, size_t extent,
137
      int rflags, void **const vpp)
138
4.13k
{
139
4.13k
    return nciop->get(nciop,offset,extent,rflags,vpp);
140
4.13k
}
141
142
int
143
ncio_move(ncio* const nciop, off_t to, off_t from, size_t nbytes, int rflags)
144
0
{
145
0
    return nciop->move(nciop,to,from,nbytes,rflags);
146
0
}
147
148
int
149
ncio_sync(ncio* const nciop)
150
104
{
151
104
    return nciop->sync(nciop);
152
104
}
153
154
int
155
ncio_filesize(ncio* const nciop, off_t *filesizep)
156
106
{
157
106
    return nciop->filesize(nciop,filesizep);
158
106
}
159
160
int
161
ncio_pad_length(ncio* const nciop, off_t length)
162
0
{
163
0
    return nciop->pad_length(nciop,length);
164
0
}
165
166
int
167
ncio_close(ncio* const nciop, int doUnlink)
168
104
{
169
    /* close and release all resources associated
170
       with nciop, including nciop
171
    */
172
104
    int status = nciop->close(nciop,doUnlink);
173
104
    return status;
174
104
}
175
176
/* URL utilities */
177
178
/*
179
Check mode flags and return:
180
NC_HTTP => byterange
181
NC_S3SDK => s3
182
0 => Not URL
183
*/
184
#ifdef ENABLE_BYTERANGE
185
static int
186
urlmodetest(const char* path)
187
104
{
188
104
    int kind = 0;
189
104
    NCURI* uri = NULL;
190
    
191
104
    ncuriparse(path,&uri);
192
104
    if(uri == NULL) return 0; /* Not URL */
193
0
    if(NC_testmode(uri, "bytes")) {
194
        /* NC_S3SDK takes priority over NC_HTTP */
195
0
        if(NC_testmode(uri, "s3")) kind = NC_S3SDK; else kind = NC_HTTP;
196
0
    } else
197
0
        kind = 0;
198
0
    ncurifree(uri);
199
0
    return kind;
200
104
}
201
#endif