From f3b7b7d34b3bf2b1be18914577c96b66dead379a Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 12 Dec 2017 17:33:03 +0100 Subject: Download and extract missing dependencies from github Using libcurl and libarchive --- src/curl.cpp | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 src/curl.cpp (limited to 'src/curl.cpp') diff --git a/src/curl.cpp b/src/curl.cpp new file mode 100644 index 0000000..f39cab8 --- /dev/null +++ b/src/curl.cpp @@ -0,0 +1,87 @@ +#include "../include/curl.hpp" +#include "../include/env.hpp" +#include +#include + +using namespace std; + +#ifdef DEBUG +#define CURL_DEBUG +#endif + +#undef CURL_DEBUG + +class CurlSession +{ +public: + CurlSession() + { + curl_global_init(CURL_GLOBAL_ALL); + } +}; + +static CurlSession curlSession; + +namespace sibs +{ + // TODO: Instead of writing to file, reading from file and extracting it; + // we can extract to file directly by putting libarchive code here + size_t writeToFile(void *ptr, size_t size, size_t nmemb, void *stream) + { + return fwrite(ptr, size, nmemb, (FILE*)stream); + } + + Result curl::downloadFile(const char *url, const char *filepath) + { + CURL *curl_handle = curl_easy_init(); + curl_easy_setopt(curl_handle, CURLOPT_URL, url); +#ifdef CURL_DEBUG + long verbose = 1L; + long noProgressMeter = 0L; +#else + long verbose = 0L; + long noProgressMeter = 1L; +#endif + curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, verbose); + curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, noProgressMeter); + curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, writeToFile); + curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, true); + + FILE *file = fopen(filepath, "wb"); + if(!file) + { + int error = errno; + curl_easy_cleanup(curl_handle); + + string errMsg = "Failed to open file for writing: "; + errMsg += filepath; + if(error != 0) + { + errMsg += "; Reason: "; + errMsg += strerror(error); + return Result::Err(errMsg); + } + else + { + return Result::Err(errMsg); + } + } + + curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, file); + printf("Downloading from url: %s\n", url); + CURLcode curlResponse = curl_easy_perform(curl_handle); + curl_easy_cleanup(curl_handle); + fclose(file); + + if(curlResponse != CURLE_OK) + { + string errMsg = "Failed to download file from url: "; + errMsg += url; + errMsg += "\nReason: "; + errMsg += curl_easy_strerror(curlResponse); + return Result::Err(errMsg); + } + + return Result::Ok(true); + } +} \ No newline at end of file -- cgit v1.2.3