forked from Unidata/netcdf-c
/
ocread.c
265 lines (242 loc) · 6.98 KB
/
ocread.c
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* Copyright 2009, UCAR/Unidata and OPeNDAP, Inc.
See the COPYRIGHT file for more information. */
#include "config.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef _WIN32
#include <io.h>
#ifndef O_BINARY
#define O_BINARY _O_BINARY
#endif
#endif
#include "ocinternal.h"
#include "ocdebug.h"
#include "ochttp.h"
#include "ocread.h"
#include "occurlfunctions.h"
#include "ncwinpath.h"
/*Forward*/
static int readpacket(OCstate* state, NCURI*, NCbytes*, OCdxd, long*);
static int readfile(const char* path, const char* suffix, NCbytes* packet);
static int readfiletofile(const char* path, const char* suffix, FILE* stream, off_t*);
int
readDDS(OCstate* state, OCtree* tree)
{
int stat = OC_NOERR;
long lastmodified = -1;
ncurisetquery(state->uri,tree->constraint);
#ifdef OCDEBUG
fprintf(stderr,"readDDS:\n");
#endif
stat = readpacket(state,state->uri,state->packet,OCDDS,
&lastmodified);
if(stat == OC_NOERR) state->ddslastmodified = lastmodified;
return stat;
}
int
readDAS(OCstate* state, OCtree* tree)
{
int stat = OC_NOERR;
ncurisetquery(state->uri,tree->constraint);
#ifdef OCDEBUG
fprintf(stderr,"readDAS:\n");
#endif
stat = readpacket(state,state->uri,state->packet,OCDAS,NULL);
return stat;
}
#if 0
int
readversion(OCstate* state, NCURI* url, NCbytes* packet)
{
return readpacket(state,url,packet,OCVER,NULL);
}
#endif
const char*
ocdxdextension(OCdxd dxd)
{
switch(dxd) {
case OCDDS: return ".dds";
case OCDAS: return ".das";
case OCDATADDS: return ".dods";
default: break;
}
return NULL;
}
static int
readpacket(OCstate* state, NCURI* url,NCbytes* packet,OCdxd dxd,long* lastmodified)
{
int stat = OC_NOERR;
int fileprotocol = 0;
const char* suffix = ocdxdextension(dxd);
char* fetchurl = NULL;
CURL* curl = state->curl;
fileprotocol = (strcmp(url->protocol,"file")==0);
if(fileprotocol && !state->curlflags.proto_file) {
/* Short circuit file://... urls*/
/* We do this because the test code always needs to read files*/
fetchurl = ncuribuild(url,NULL,NULL,NCURIBASE);
stat = readfile(fetchurl,suffix,packet);
} else {
int flags = NCURIBASE;
if(!fileprotocol) flags |= NCURIQUERY;
flags |= NCURIENCODE;
fetchurl = ncuribuild(url,NULL,suffix,flags);
MEMCHECK(fetchurl,OC_ENOMEM);
if(ocdebug > 0)
{fprintf(stderr,"fetch url=%s\n",fetchurl); fflush(stderr);}
stat = ocfetchurl(curl,fetchurl,packet,lastmodified,&state->creds);
if(stat)
oc_curl_printerror(state);
if(ocdebug > 0)
{fprintf(stderr,"fetch complete\n"); fflush(stderr);}
}
free(fetchurl);
#ifdef OCDEBUG
{
fprintf(stderr,"readpacket: packet.size=%lu\n",
(unsigned long)ncbyteslength(packet));
}
#endif
return OCTHROW(stat);
}
int
readDATADDS(OCstate* state, OCtree* tree, OCflags flags)
{
int stat = OC_NOERR;
long lastmod = -1;
#ifdef OCDEBUG
fprintf(stderr,"readDATADDS:\n");
#endif
if((flags & OCONDISK) == 0) {
ncurisetquery(state->uri,tree->constraint);
stat = readpacket(state,state->uri,state->packet,OCDATADDS,&lastmod);
if(stat == OC_NOERR)
state->datalastmodified = lastmod;
tree->data.datasize = ncbyteslength(state->packet);
} else { /*((flags & OCONDISK) != 0) */
NCURI* url = state->uri;
int fileprotocol = 0;
char* readurl = NULL;
fileprotocol = (strcmp(url->protocol,"file")==0);
if(fileprotocol && !state->curlflags.proto_file) {
readurl = ncuribuild(url,NULL,NULL,NCURIBASE);
stat = readfiletofile(readurl, ".dods", tree->data.file, &tree->data.datasize);
} else {
int flags = NCURIBASE;
if(!fileprotocol) flags |= NCURIQUERY;
flags |= NCURIENCODE;
ncurisetquery(url,tree->constraint);
readurl = ncuribuild(url,NULL,".dods",flags);
MEMCHECK(readurl,OC_ENOMEM);
if (ocdebug > 0)
{fprintf(stderr, "fetch url=%s\n", readurl);fflush(stderr);}
stat = ocfetchurl_file(state->curl, readurl, tree->data.file,
&tree->data.datasize, &lastmod);
if(stat == OC_NOERR)
state->datalastmodified = lastmod;
if (ocdebug > 0)
{fprintf(stderr,"fetch complete\n"); fflush(stderr);}
}
free(readurl);
}
return OCTHROW(stat);
}
static int
readfiletofile(const char* path, const char* suffix, FILE* stream, off_t* sizep)
{
int stat = OC_NOERR;
NCbytes* packet = ncbytesnew();
size_t len;
/* check for leading file:/// */
if(ocstrncmp(path,"file:///",8)==0) path += 7; /* assume absolute path*/
stat = readfile(path,suffix,packet);
#ifdef OCDEBUG
fprintf(stderr,"readfiletofile: packet.size=%lu\n",
(unsigned long)ncbyteslength(packet));
#endif
if(stat != OC_NOERR) goto unwind;
len = nclistlength(packet);
if(stat == OC_NOERR) {
size_t written;
fseek(stream,0,SEEK_SET);
written = fwrite(ncbytescontents(packet),1,len,stream);
if(written != len) {
#ifdef OCDEBUG
fprintf(stderr,"readfiletofile: written!=length: %lu :: %lu\n",
(unsigned long)written,(unsigned long)len);
#endif
stat = OC_EIO;
}
}
if(sizep != NULL) *sizep = len;
unwind:
ncbytesfree(packet);
return OCTHROW(stat);
}
static int
readfile(const char* path, const char* suffix, NCbytes* packet)
{
int stat = OC_NOERR;
char buf[1024];
char filename[1024];
int fd = -1;
int flags = 0;
off_t filesize = 0;
off_t totalread = 0;
/* check for leading file:/// */
if(ocstrncmp(path,"file://",7)==0) path += 7; /* assume absolute path*/
if(!occopycat(filename,sizeof(filename),2,path,(suffix != NULL ? suffix : "")))
return OCTHROW(OC_EOVERRUN);
flags = O_RDONLY;
#ifdef O_BINARY
flags |= O_BINARY;
#endif
fd = NCopen2(filename,flags);
if(fd < 0) {
nclog(NCLOGERR,"open failed: %s file=|%s|",ocerrstring(errno),filename);
stat = OC_EOPEN;
goto done;
}
/* Get the file size */
filesize = lseek(fd,(off_t)0,SEEK_END);
if(filesize < 0) {
stat = OC_EIO;
nclog(NCLOGERR,"lseek failed: %s",filename);
goto done;
}
/* Move file pointer back to the beginning of the file */
(void)lseek(fd,(off_t)0,SEEK_SET);
stat = OC_NOERR;
for(totalread=0;;) {
off_t count = (off_t)read(fd,buf,sizeof(buf));
if(count == 0)
break; /*eof*/
else if(count < 0) {
stat = OC_EIO;
nclog(NCLOGERR,"read failed: %s",filename);
goto done;
}
ncbytesappendn(packet,buf,(unsigned long)count);
totalread += count;
}
if(totalread < filesize) {
stat = OC_EIO;
nclog(NCLOGERR,"short read: |%s|=%lu read=%lu\n",
filename,(unsigned long)filesize,(unsigned long)totalread);
goto done;
}
done:
#ifdef OCDEBUG
fprintf(stderr,"readfile: filesize=%lu totalread=%lu\n",
(unsigned long)filesize,(unsigned long)totalread);
#endif
if(fd >= 0) close(fd);
return OCTHROW(stat);
}