summaryrefslogtreecommitdiff
path: root/hscript/util.cc
diff options
context:
space:
mode:
authorA. Wilcox <AWilcox@Wilcox-Tech.com>2019-11-02 17:33:28 -0500
committerA. Wilcox <AWilcox@Wilcox-Tech.com>2019-11-02 17:33:28 -0500
commitdd7559561a8a7f4fef7a4bb8b23e2894eca4594c (patch)
treea9feba9457e0d05eb6da967e3134f355e738e51f /hscript/util.cc
parent8270ab3308f2522d0150e9040ea44883ce175e70 (diff)
downloadhorizon-dd7559561a8a7f4fef7a4bb8b23e2894eca4594c.tar.gz
horizon-dd7559561a8a7f4fef7a4bb8b23e2894eca4594c.tar.bz2
horizon-dd7559561a8a7f4fef7a4bb8b23e2894eca4594c.tar.xz
horizon-dd7559561a8a7f4fef7a4bb8b23e2894eca4594c.zip
hscript: Add download_file helper and implement SigningKey::execute0.1.0
Diffstat (limited to 'hscript/util.cc')
-rw-r--r--hscript/util.cc61
1 files changed, 61 insertions, 0 deletions
diff --git a/hscript/util.cc b/hscript/util.cc
new file mode 100644
index 0000000..4a991c5
--- /dev/null
+++ b/hscript/util.cc
@@ -0,0 +1,61 @@
+/*
+ * util.cc - Implementation of useful utility routines
+ * libhscript, the HorizonScript library for
+ * Project Horizon
+ *
+ * Copyright (c) 2019 Adélie Linux and contributors. All rights reserved.
+ * This code is licensed under the AGPL 3.0 license, as noted in the
+ * LICENSE-code file in the root directory of this repository.
+ *
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+
+#include <string>
+#ifdef HAVE_LIBCURL
+# include <cstdio> /* fopen */
+# include <cstring> /* strerror */
+# include <curl/curl.h> /* curl_* */
+# include <errno.h> /* errno */
+#endif /* HAVE_LIBCURL */
+#include "util/output.hh"
+
+#ifdef HAVE_LIBCURL
+bool download_file(const std::string &url, const std::string &path) {
+ CURL *curl = curl_easy_init();
+ CURLcode result;
+ bool return_code = false;
+ char errbuf[CURL_ERROR_SIZE];
+ FILE *fp;
+
+ if(curl == nullptr) {
+ output_error("internal", "trouble initialising cURL library");
+ return false;
+ }
+
+ fp = fopen(path.c_str(), "w");
+ if(fp == nullptr) {
+ output_error("internal", "couldn't open " + path + " for writing",
+ strerror(errno));
+ curl_easy_cleanup(curl);
+ return false;
+ }
+
+ curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
+ curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
+
+ result = curl_easy_perform(curl);
+ if(result == CURLE_OK) {
+ return_code = true;
+ } else {
+ output_error("curl", "couldn't download file", errbuf);
+ }
+ curl_easy_cleanup(curl);
+ return return_code;
+}
+#else /* !HAVE_LIBCURL */
+bool download_file(const std::string &url, const std::string &path) {
+ output_error("internal", "can't download without linking to cURL");
+ return false;
+}
+#endif /* HAVE_LIBCURL */