1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
/*
* 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>
#include <vector>
#ifdef HAVE_LIBCURL
# include <cstdio> /* fopen */
# include <cstring> /* strerror */
# include <curl/curl.h> /* curl_* */
# include <errno.h> /* errno */
#endif /* HAVE_LIBCURL */
#ifdef HAS_INSTALL_ENV
# include <spawn.h> /* posix_spawnp */
# include <sys/wait.h> /* waitpid, W* */
# include <unistd.h> /* environ */
#endif
#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 */
int run_command(const std::string &cmd, const std::vector<std::string> &args) {
#ifdef HAS_INSTALL_ENV
const char **argv = new const char*[args.size() + 2];
pid_t child;
int status;
argv[0] = cmd.c_str();
for(unsigned long index = 0; index < args.size(); index++) {
argv[index + 1] = args.at(index).c_str();
}
argv[args.size() + 1] = nullptr;
status = posix_spawnp(&child, cmd.c_str(), nullptr, nullptr,
const_cast<char * const *>(argv), environ);
if(status != 0) {
/* extremely unlikely failure case */
output_error(cmd, "cannot fork", strerror(status));
delete[] argv;
return -1;
}
delete[] argv;
if(waitpid(child, &status, 0) == -1) {
/* unlikely failure case */
output_error(cmd, "waitpid", strerror(errno));
return -1;
}
if(!WIFEXITED(status)) {
output_error(cmd, "received fatal signal " +
std::to_string(WTERMSIG(status)));
return -1;
}
if(WEXITSTATUS(status) != 0) {
output_error(cmd, "exited abnormally with status " +
std::to_string(WEXITSTATUS(status)));
}
return WEXITSTATUS(status);
#else /* !HAS_INSTALL_ENV */
output_error(cmd, "can't spawn processes in runtine environment");
return -1;
#endif /* HAS_INSTALL_ENV */
}
|