Debian Codegraph @

src/download.c

raw symbols SCIP
1 /* *
2 * Reimplementation of C_download (the "internal" method for download.file).
3 */
4 #include "curl-common.h"
5
6 SEXP R_download_curl(SEXP url, SEXP destfile, SEXP quiet, SEXP mode, SEXP ptr, SEXP nonblocking) {
7 if(!Rf_isString(url))
8 Rf_error("Argument 'url' must be string.");
9
10 if(!Rf_isString(destfile))
11 Rf_error("Argument 'destfile' must be string.");
12
13 if(!Rf_isLogical(quiet))
14 Rf_error("Argument 'quiet' must be TRUE/FALSE.");
15
16 if(!Rf_isString(mode))
17 Rf_error("Argument 'mode' must be string.");
18
19 /* get the handle */
20 CURL *handle = get_handle(ptr);
21 reset_errbuf(get_ref(ptr));
22
23 /* open file */
24 FILE *dest = fopen(CHAR(Rf_asChar(destfile)), CHAR(Rf_asChar(mode)));
25 if(!dest)
26 Rf_error("Failed to open file %s.", CHAR(Rf_asChar(destfile)));
27
28 /* set options */
29 curl_easy_setopt(handle, CURLOPT_URL, Rf_translateCharUTF8(Rf_asChar(url)));
30 curl_easy_setopt(handle, CURLOPT_NOPROGRESS, (long) Rf_asLogical(quiet));
31 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, push_disk);
32 curl_easy_setopt(handle, CURLOPT_WRITEDATA, dest);
33 curl_easy_setopt(handle, CURLOPT_FAILONERROR, 1L);
34
35 /* perform blocking request */
36 CURLcode status = Rf_asLogical(nonblocking) ?
37 curl_perform_with_interrupt(handle) : curl_easy_perform(handle);
38
39 /* cleanup */
40 curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 1L);
41 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, NULL);
42 curl_easy_setopt(handle, CURLOPT_WRITEDATA, NULL);
43 curl_easy_setopt(handle, CURLOPT_FAILONERROR, 0L);
44 fclose(dest);
45
46 /* raise for curl errors */
47 assert_status(status, get_ref(ptr));
48 return Rf_ScalarInteger(0);
49 }