Debian Codegraph @

src/fopen.c

raw symbols SCIP
1 /*****************************************************************************
2 *
3 * This example source code introduces a c library buffered I/O interface to
4 * URL reads it supports fopen(), fread(), fgets(), feof(), fclose(),
5 * rewind(). Supported functions have identical prototypes to their normal c
6 * lib namesakes and are preceaded by url_ .
7 *
8 * Using this code you can replace your program's fopen() with url_fopen()
9 * and fread() with url_fread() and it become possible to read remote streams
10 * instead of (only) local files. Local files (ie those that can be directly
11 * fopened) will drop back to using the underlying clib implementations
12 *
13 * See the main() function at the bottom that shows an app that retrives from a
14 * specified url using fgets() and fread() and saves as two output files.
15 *
16 * Copyright (c) 2003 Simtec Electronics
17 *
18 * Re-implemented by Vincent Sanders <[email protected]> with extensive
19 * reference to original curl example code
20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 * 3. The name of the author may not be used to endorse or promote products
30 * derived from this software without specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
33 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
35 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
36 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
41 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 *
43 * This example requires libcurl 7.9.7 or later.
44 *
45 * Modified for aide by Hannes von Haugwitz <[email protected]>
46 * based on modifications of previous version by Pablo Virolainen
47 * ([email protected]):
48 * 2013-05-14: - moved declarations to 'include/fopen.h'
49 * - removed (unneeded) main method
50 * 2022-02-12: - update #include statements
51 */
52
53 #include <string.h>
54 #include <stdlib.h>
55 #include <stdio.h>
56 #include <errno.h>
57 #include <sys/select.h>
58 #include <curl/curl.h>
59 #include "fopen.h"
60
61 /* we use a global one for convenience */
62 CURLM *multi_handle;
63
64 /* curl calls this routine to get more data */
65 static size_t write_callback(char *buffer,
66 size_t size,
67 size_t nitems,
68 void *userp)
69 {
70 char *newbuff;
71 size_t rembuff;
72
73 URL_FILE *url = (URL_FILE *)userp;
74 size *= nitems;
75
76 rembuff=url->buffer_len - url->buffer_pos; /* remaining space in buffer */
77
78 if(size > rembuff) {
79 /* not enough space in buffer */
80 newbuff=realloc(url->buffer,url->buffer_len + (size - rembuff));
81 if(newbuff==NULL) {
82 fprintf(stderr,"callback buffer grow failed\n");
83 size=rembuff;
84 }
85 else {
86 /* realloc suceeded increase buffer size*/
87 url->buffer_len+=size - rembuff;
88 url->buffer=newbuff;
89 }
90 }
91
92 memcpy(&url->buffer[url->buffer_pos], buffer, size);
93 url->buffer_pos += size;
94
95 return size;
96 }
97
98 /* use to attempt to fill the read buffer up to requested number of bytes */
99 static int fill_buffer(URL_FILE *file, size_t want)
100 {
101 fd_set fdread;
102 fd_set fdwrite;
103 fd_set fdexcep;
104 struct timeval timeout;
105 int rc;
106
107 /* only attempt to fill buffer if transactions still running and buffer
108 * doesnt exceed required size already
109 */
110 if((!file->still_running) || (file->buffer_pos > want))
111 return 0;
112
113 /* attempt to fill buffer */
114 do {
115 int maxfd = -1;
116 long curl_timeo = -1;
117
118 FD_ZERO(&fdread);
119 FD_ZERO(&fdwrite);
120 FD_ZERO(&fdexcep);
121
122 /* set a suitable timeout to fail on */
123 timeout.tv_sec = 60; /* 1 minute */
124 timeout.tv_usec = 0;
125
126 curl_multi_timeout(multi_handle, &curl_timeo);
127 if(curl_timeo >= 0) {
128 timeout.tv_sec = curl_timeo / 1000;
129 if(timeout.tv_sec > 1)
130 timeout.tv_sec = 1;
131 else
132 timeout.tv_usec = (curl_timeo % 1000) * 1000;
133 }
134
135 /* get file descriptors from the transfers */
136 curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
137
138 /* In a real-world program you OF COURSE check the return code of the
139 function calls. On success, the value of maxfd is guaranteed to be
140 greater or equal than -1. We call select(maxfd + 1, ...), specially
141 in case of (maxfd == -1), we call select(0, ...), which is basically
142 equal to sleep. */
143
144 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
145
146 switch(rc) {
147 case -1:
148 /* select error */
149 break;
150
151 case 0:
152 default:
153 /* timeout or readable/writable sockets */
154 curl_multi_perform(multi_handle, &file->still_running);
155 break;
156 }
157 } while(file->still_running && (file->buffer_pos < want));
158 return 1;
159 }
160
161 /* use to remove want bytes from the front of a files buffer */
162 static int use_buffer(URL_FILE *file,int want)
163 {
164 /* sort out buffer */
165 if((file->buffer_pos - want) <=0) {
166 /* ditch buffer - write will recreate */
167 if(file->buffer)
168 free(file->buffer);
169
170 file->buffer=NULL;
171 file->buffer_pos=0;
172 file->buffer_len=0;
173 }
174 else {
175 /* move rest down make it available for later */
176 memmove(file->buffer,
177 &file->buffer[want],
178 (file->buffer_pos - want));
179
180 file->buffer_pos -= want;
181 }
182 return 0;
183 }
184
185 URL_FILE *url_fopen(const char *url,const char *operation)
186 {
187 /* this code could check for URLs or types in the 'url' and
188 basicly use the real fopen() for standard files */
189
190 URL_FILE *file;
191 (void)operation;
192
193 file = malloc(sizeof(URL_FILE));
194 if(!file)
195 return NULL;
196
197 memset(file, 0, sizeof(URL_FILE));
198
199 if((file->handle.file=fopen(url,operation)))
200 file->type = CFTYPE_FILE; /* marked as URL */
201
202 else {
203 file->type = CFTYPE_CURL; /* marked as URL */
204 file->handle.curl = curl_easy_init();
205
206 curl_easy_setopt(file->handle.curl, CURLOPT_URL, url);
207 curl_easy_setopt(file->handle.curl, CURLOPT_WRITEDATA, file);
208 curl_easy_setopt(file->handle.curl, CURLOPT_VERBOSE, 0L);
209 curl_easy_setopt(file->handle.curl, CURLOPT_WRITEFUNCTION, write_callback);
210
211 if(!multi_handle)
212 multi_handle = curl_multi_init();
213
214 curl_multi_add_handle(multi_handle, file->handle.curl);
215
216 /* lets start the fetch */
217 curl_multi_perform(multi_handle, &file->still_running);
218
219 if((file->buffer_pos == 0) && (!file->still_running)) {
220 /* if still_running is 0 now, we should return NULL */
221
222 /* make sure the easy handle is not in the multi handle anymore */
223 curl_multi_remove_handle(multi_handle, file->handle.curl);
224
225 /* cleanup */
226 curl_easy_cleanup(file->handle.curl);
227
228 free(file);
229
230 file = NULL;
231 }
232 }
233 return file;
234 }
235
236 int url_fclose(URL_FILE *file)
237 {
238 int ret=0;/* default is good return */
239
240 switch(file->type) {
241 case CFTYPE_FILE:
242 ret=fclose(file->handle.file); /* passthrough */
243 break;
244
245 case CFTYPE_CURL:
246 /* make sure the easy handle is not in the multi handle anymore */
247 curl_multi_remove_handle(multi_handle, file->handle.curl);
248
249 /* cleanup */
250 curl_easy_cleanup(file->handle.curl);
251 break;
252
253 default: /* unknown or supported type - oh dear */
254 ret=EOF;
255 errno=EBADF;
256 break;
257 }
258
259 if(file->buffer)
260 free(file->buffer);/* free any allocated buffer space */
261
262 free(file);
263
264 return ret;
265 }
266
267 int url_feof(URL_FILE *file)
268 {
269 int ret=0;
270
271 switch(file->type) {
272 case CFTYPE_FILE:
273 ret=feof(file->handle.file);
274 break;
275
276 case CFTYPE_CURL:
277 if((file->buffer_pos == 0) && (!file->still_running))
278 ret = 1;
279 break;
280
281 default: /* unknown or supported type - oh dear */
282 ret=-1;
283 errno=EBADF;
284 break;
285 }
286 return ret;
287 }
288
289 size_t url_fread(void *ptr, size_t size, size_t nmemb, URL_FILE *file)
290 {
291 size_t want;
292
293 switch(file->type) {
294 case CFTYPE_FILE:
295 want=fread(ptr,size,nmemb,file->handle.file);
296 break;
297
298 case CFTYPE_CURL:
299 want = nmemb * size;
300
301 fill_buffer(file,want);
302
303 /* check if theres data in the buffer - if not fill_buffer()
304 * either errored or EOF */
305 if(!file->buffer_pos)
306 return 0;
307
308 /* ensure only available data is considered */
309 if(file->buffer_pos < want)
310 want = file->buffer_pos;
311
312 /* xfer data to caller */
313 memcpy(ptr, file->buffer, want);
314
315 use_buffer(file,want);
316
317 want = want / size; /* number of items */
318 break;
319
320 default: /* unknown or supported type - oh dear */
321 want=0;
322 errno=EBADF;
323 break;
324
325 }
326 return want;
327 }
328
329 char *url_fgets(char *ptr, size_t size, URL_FILE *file)
330 {
331 size_t want = size - 1;/* always need to leave room for zero termination */
332 size_t loop;
333
334 switch(file->type) {
335 case CFTYPE_FILE:
336 ptr = fgets(ptr,size,file->handle.file);
337 break;
338
339 case CFTYPE_CURL:
340 fill_buffer(file,want);
341
342 /* check if theres data in the buffer - if not fill either errored or
343 * EOF */
344 if(!file->buffer_pos)
345 return NULL;
346
347 /* ensure only available data is considered */
348 if(file->buffer_pos < want)
349 want = file->buffer_pos;
350
351 /*buffer contains data */
352 /* look for newline or eof */
353 for(loop=0;loop < want;loop++) {
354 if(file->buffer[loop] == '\n') {
355 want=loop+1;/* include newline */
356 break;
357 }
358 }
359
360 /* xfer data to caller */
361 memcpy(ptr, file->buffer, want);
362 ptr[want]=0;/* allways null terminate */
363
364 use_buffer(file,want);
365
366 break;
367
368 default: /* unknown or supported type - oh dear */
369 ptr=NULL;
370 errno=EBADF;
371 break;
372 }
373
374 return ptr;/*success */
375 }
376
377 void url_rewind(URL_FILE *file)
378 {
379 switch(file->type) {
380 case CFTYPE_FILE:
381 rewind(file->handle.file); /* passthrough */
382 break;
383
384 case CFTYPE_CURL:
385 /* halt transaction */
386 curl_multi_remove_handle(multi_handle, file->handle.curl);
387
388 /* restart */
389 curl_multi_add_handle(multi_handle, file->handle.curl);
390
391 /* ditch buffer - write will recreate - resets stream pos*/
392 if(file->buffer)
393 free(file->buffer);
394
395 file->buffer=NULL;
396 file->buffer_pos=0;
397 file->buffer_len=0;
398
399 break;
400
401 default: /* unknown or supported type - oh dear */
402 break;
403 }
404 }