/*************************************************************************** * Copyright (C) 2006 by Torben H. Nielsen * * torben@t-hoerup.dk * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include #include #include #include #include "dblayer.h" #include "daemon.h" #include "dlworker.h" typedef struct { char *filename; FILE *stream; int error; } CurlFile; int writefile(void *buffer, size_t size, size_t nmemb, void *stream) { CurlFile *out = (CurlFile*) stream; int retval; if( !out->stream) { /* open file for writing */ out->stream=fopen(out->filename, "wb"); if(!out->stream) { out->error = errno; return -1; /* failure, can't open file to write */ } } return fwrite(buffer, size, nmemb, out->stream); } void downloadfile(dlrecord *record) { char buf[200]; CURL *curl = 0; CURLcode res; CurlFile outfile; int dlok = 1; long responsecode; outfile.error = 0; outfile.stream = 0; outfile.filename = record->filename; curl_global_init(CURL_GLOBAL_DEFAULT); // meybe we should have this in main() ??? curl = curl_easy_init(); if (!curl) { sprintf(buf, "Could not initialize libcurl"); log_message(buf); sql_abort_download(record, buf); return; } sql_start_download( record ); curl_easy_setopt(curl, CURLOPT_URL, record->URL); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &outfile); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefile); curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1); /* Switch on full protocol/debug output */ if (CONFIG->debug) curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); res = curl_easy_perform(curl); if (res == CURLE_OK) { sql_stop_download( record ); sprintf(buf, "download of %s completed successfully", record->URL); log_message(buf); } else { if (outfile.error == 0) { // could we open the output file stream ? curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responsecode); sprintf(buf, "download of %s failed : %s (response code:%ld)", record->URL, curl_easy_strerror(res),responsecode ); } else { sprintf(buf,"download of %s failed : %s", record->URL, strerror(outfile.error)); } log_message(buf); sql_abort_download(record, buf); } curl_easy_cleanup(curl); curl_global_cleanup(); if (outfile.stream) fclose(outfile.stream); }