00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifdef HAVE_CONFIG_H
00021 # include <config.h>
00022 #endif
00023
00024 #include <stdlib.h>
00025 #include <stdio.h>
00026 #include <string.h>
00027 #include <sys/types.h>
00028 #include <unistd.h>
00029 #include <errno.h>
00030
00031 #include <curl/curl.h>
00032 #include <curl/types.h>
00033 #include <curl/easy.h>
00034 #include <glib.h>
00035
00036 #include "ftp.h"
00037 #include "util.h"
00038 #include "libuau.h"
00039 #include "error.h"
00040
00041 #ifdef WITH_DMALLOC
00042 # include <dmalloc.h>
00043 #endif
00044
00045 static AProgressCallback downloadCallback_global = NULL;
00046
00047 static size_t callback(void *ptr, size_t size, size_t nmemb, void *data);
00048
00049 gboolean
00050 lutil_ftp_downloadToFile(const char* url, const char* downloadTo) {
00051 CURL *curl_handle;
00052 CURLcode ret;
00053 FILE *tempFile;
00054 char errorBuffer[CURL_ERROR_SIZE];
00055
00056 tempFile = fopen(downloadTo, "w");
00057 if (tempFile == NULL) {
00058 ERROR("Couldn't download file to temporary location %s: %s", downloadTo, strerror(errno));
00059 return FALSE;
00060 }
00061
00062 DBUGOUT("Retrieving url %s to %s", url, downloadTo);
00063
00064 curl_handle = curl_easy_init();
00065
00066 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
00067 curl_easy_setopt(curl_handle, CURLOPT_FILE, (void *)tempFile);
00068 curl_easy_setopt(curl_handle, CURLOPT_ERRORBUFFER, errorBuffer);
00069 curl_easy_setopt(curl_handle, CURLOPT_FAILONERROR, 1);
00070 if (downloadCallback_global != NULL) {
00071 curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, FALSE);
00072 curl_easy_setopt(curl_handle, CURLOPT_PROGRESSFUNCTION, downloadCallback_global);
00073 }
00074 ret = curl_easy_perform(curl_handle);
00075
00076 curl_easy_cleanup(curl_handle);
00077
00078 fclose(tempFile);
00079
00080 if (ret != 0) {
00081 unlink(downloadTo);
00082 ERROR("Couldn't download %s: %s", url, errorBuffer);
00083 return FALSE;
00084 } else {
00085 return TRUE;
00086 }
00087 }
00088
00089
00090 GString *
00091 lutil_ftp_getURL(const char* url) {
00092 AData chunk;
00093 GString *data;
00094 CURL *curl_handle;
00095 CURLcode ret;
00096 char errorBuffer[CURL_ERROR_SIZE];
00097
00098 chunk.data = NULL;
00099 chunk.size = 0;
00100
00101 DBUGOUT("Retrieving url: %s", url);
00102
00103 curl_handle = curl_easy_init();
00104
00105 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
00106 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, callback);
00107 curl_easy_setopt(curl_handle, CURLOPT_FILE, (void *)&chunk);
00108 curl_easy_setopt(curl_handle, CURLOPT_ERRORBUFFER, errorBuffer);
00109 curl_easy_setopt(curl_handle, CURLOPT_FAILONERROR, 1);
00110
00111
00112
00113
00114
00115
00116 ret = curl_easy_perform(curl_handle);
00117
00118 curl_easy_cleanup(curl_handle);
00119
00120 if (ret != 0) {
00121 g_free(chunk.data);
00122 ERROR("Couldn't download %s: %s", url, errorBuffer);
00123 return NULL;
00124 } else {
00125 data = g_string_new_len(chunk.data, chunk.size);
00126 g_free(chunk.data);
00127 return data;
00128 }
00129 }
00130
00131 void
00132 lutil_ftp_setCallbackFunc(AProgressCallback callback) {
00133 downloadCallback_global = callback;
00134 }
00135
00136 void
00137 lutil_ftp_resetCallbackFunc(void) {
00138 downloadCallback_global = NULL;
00139 }
00140
00141
00142
00143 static size_t
00144 callback(void *ptr, size_t size, size_t nmemb, void *data) {
00145 int actualSize = size * nmemb;
00146 AData *file = (AData *)data;
00147
00148 file->data = (char *) g_realloc(file->data, file->size + actualSize + 1);
00149 if (file->data != NULL) {
00150 memcpy(&(file->data[file->size]), ptr, actualSize);
00151 file->size += actualSize;
00152 file->data[file->size] = '\0';
00153 }
00154 return actualSize;
00155 }
00156