From 0cf81a4421f9a4d267245a3041508b617a01d68d Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 28 Dec 2017 01:02:16 +0100 Subject: Add curl get, add packages file --- backend/ninja/Ninja.cpp | 3 ++ include/PackageVersion.hpp | 56 +++++++++++++++++++++++++++++++++ include/curl.hpp | 12 +++++++- packages.json | 7 +++++ src/curl.cpp | 77 ++++++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 152 insertions(+), 3 deletions(-) create mode 100644 include/PackageVersion.hpp create mode 100644 packages.json diff --git a/backend/ninja/Ninja.cpp b/backend/ninja/Ninja.cpp index 59b0a3a..e290436 100644 --- a/backend/ninja/Ninja.cpp +++ b/backend/ninja/Ninja.cpp @@ -174,6 +174,9 @@ namespace backend printf("Dependency not found in global lib, trying to download from github\n"); // TODO: Download several dependencies at the same time by adding them to a list // and then iterate them and download them all using several threads. + // All dependecies should be downloaded at the same time, this includes dependencies of dependencies. + // If a dependency is missing, fail build BEFORE downloading dependencies and before compiling anything. + // You do not want to possibly wait several minutes only for build to fail when there is no compilation error. // TODO: If return error is invalid url, then the message should be converted to // invalid package name/version. A check should be done if it is the name or version diff --git a/include/PackageVersion.hpp b/include/PackageVersion.hpp new file mode 100644 index 0000000..99f8342 --- /dev/null +++ b/include/PackageVersion.hpp @@ -0,0 +1,56 @@ +#ifndef SIBS_PACKAGEVERSION_HPP +#define SIBS_PACKAGEVERSION_HPP + +#include + +namespace sibs +{ + /* + * Different package version syntax: + * ANY: "major.minor.patch" (xxx.xxx.xxx) + * Checks for package in several different places. First pkg-config, then local lib dir (~/.sibs/lib), then git/server. + * Example: "2.1.002" + * GIT: "branch:revision" + * Checks for package in any of the configures git servers (default: github). + * Example: "master:HEAD" + * LATEST: "latest" + * Checks for package in several different places. First git/server then pkg-config then local lib dir (~/.sibs/lib). + * + * + * GIT and LATEST first check if local lib (~/.sibs/lib) contains latest version by comparing the revision to remote revision. + */ + class PackageVersion + { + public: + enum class Type + { + ANY, + GIT, + LATEST + }; + + PackageVersion(int _major, int _minor, int _patch) : type(Type::ANY), major(_major), minor(_minor), patch(_patch) {} + PackageVersion(const std::string &&_gitBranch, const std::string &&_gitRevision) : + type(Type::GIT), + gitBranch(_gitBranch), + gitRevision(_gitRevision) + { + + } + + static PackageVersion latest() { return PackageVersion(Type::LATEST); } + private: + PackageVersion(Type _type) : type(_type) {} + private: + Type type; + + int major; + int minor; + int patch; + + std::string gitBranch; + std::string gitRevision; + }; +} + +#endif //SIBS_PACKAGEVERSION_HPP diff --git a/include/curl.hpp b/include/curl.hpp index 49dfe12..7c0ddbe 100644 --- a/include/curl.hpp +++ b/include/curl.hpp @@ -2,13 +2,23 @@ #define SIBS_CURL_HPP #include "Result.hpp" +#include namespace sibs { + class HttpResult + { + public: + long httpCode; + bool success; + std::string str; + }; + class curl { public: - static Result downloadFile(const char *url, const char *filepath); + static sibs::Result downloadFile(const char *url, const char *filepath); + static HttpResult get(const char *url); }; } diff --git a/packages.json b/packages.json new file mode 100644 index 0000000..3196663 --- /dev/null +++ b/packages.json @@ -0,0 +1,7 @@ +{ + "xxhash": { + "0.1.0": { + "urls": ["https://gateway.ipfs.io/ipfs/QmVJH2p3Y28iQ6te5wR6bCDnzDYzDFvEqJDuLf4Ykn2d3Z"] + } + } +} diff --git a/src/curl.cpp b/src/curl.cpp index e141e78..56c19ec 100644 --- a/src/curl.cpp +++ b/src/curl.cpp @@ -18,6 +18,11 @@ public: { curl_global_init(CURL_GLOBAL_ALL); } + + ~CurlSession() + { + curl_global_cleanup(); + } }; static CurlSession curlSession; @@ -26,9 +31,17 @@ 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) + size_t writeToFile(char *data, size_t size, size_t nmemb, FILE *stream) { - return fwrite(ptr, size, nmemb, (FILE*)stream); + if(!stream) return 0; + return fwrite(data, size, nmemb, stream); + } + + size_t writeToString(char *data, size_t size, size_t nmemb, string *writerData) + { + if(!writerData) return 0; + writerData->append(data, size * nmemb); + return size * nmemb; } Result curl::downloadFile(const char *url, const char *filepath) @@ -46,6 +59,7 @@ namespace sibs curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, noProgressMeter); curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, writeToFile); curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, true); + curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "SIBS"); FILE *file = fopen(filepath, "wb"); if(!file) @@ -85,6 +99,9 @@ namespace sibs errMsg += url; errMsg += "\nReason: Expected http response code 200 (OK), got: "; errMsg += to_string(httpCode); + errMsg += " ("; + errMsg += curl_easy_strerror(curlResponse); + errMsg += ")"; return Result::Err(errMsg); } else @@ -96,4 +113,60 @@ namespace sibs return Result::Err(errMsg); } } + + HttpResult curl::get(const char *url) + { + HttpResult result; + + 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, writeToString); + curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, true); + curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "Hacker"); + + curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &result.str); + printf("Downloading from url: %s\n", url); + CURLcode curlResponse = curl_easy_perform(curl_handle); + + long httpCode = 0; + curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &httpCode); + curl_easy_cleanup(curl_handle); + + result.httpCode = httpCode; + if(httpCode == 200 && curlResponse == CURLE_OK) + { + result.success = true; + return result; + } + + result.success = false; + if(httpCode != 200) + { + string errMsg = "Failed to download file from url: "; + errMsg += url; + errMsg += "\nReason: Expected http response code 200 (OK), got: "; + errMsg += to_string(httpCode); + errMsg += " ("; + errMsg += curl_easy_strerror(curlResponse); + errMsg += ")"; + return result; + } + else + { + string errMsg = "Failed to download file from url: "; + errMsg += url; + errMsg += "\nReason: "; + errMsg += curl_easy_strerror(curlResponse); + return result; + } + } } \ No newline at end of file -- cgit v1.2.3