-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownloader.cpp
More file actions
228 lines (215 loc) · 6.89 KB
/
Downloader.cpp
File metadata and controls
228 lines (215 loc) · 6.89 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "Downloader.h"
#include "Logger.h"
#include <algorithm>
#include <cstddef>
#include <curl/curl.h>
#include <fstream>
#include <mutex>
// NOTE: The Downloader initialization handle stuff in this file is an
// implementation detail needed because some of the underlying libraries being
// used require global init and deinit.
// The first Downloader class instance created will trigger init, and deinit
// will occur when the last Downloader instance is destroyed.
// This is a little janky in that init+deinit could occur multiple times if a
// Downloader instance is created after all previous instances have been
// destroyed, but it seems like a better starting point than requiring that
// main() call static functions.
namespace
{
// Curl requires a C-style callback handler, so this function accumulates
// downloaded data into a file
std::size_t WriteToFile(
char const* dataPtr
, std::size_t size
, std::size_t nmemb
, std::ofstream* filePtr
)
{
if (!dataPtr || !filePtr)
{
// LOGWRN(logger, "Null pointer(s) passed to Curl file write handler");
return 0;
}
const std::size_t numBytes(size * nmemb);
filePtr->write(dataPtr, numBytes);
// LOGINF(logger, "Wrote " << numBytes << " bytes to download file");
return numBytes;
}
// Curl requires a C-style callback handler, so this function accumulates
// downloaded data into a string
std::size_t WriteToString(
char const* dataPtr
, std::size_t size
, std::size_t nmemb
, std::string* stringPtr
)
{
if (!dataPtr || !stringPtr)
{
// LOGWRN(logger, "Null pointer(s) passed to Curl string write handler");
return 0;
}
const std::size_t numBytes(size * nmemb);
stringPtr->append(dataPtr, numBytes);
return numBytes;
}
// Curl requires a C-style callback handler, so this function accumulates
// downloaded data into a binary data buffer
std::size_t WriteToVector(
char const* dataPtr
, std::size_t size
, std::size_t nmemb
, std::vector<char>* vPtr
)
{
if (!dataPtr || !vPtr)
{
// LOGWRN(logger, "Null pointer(s) passed to Curl vector write handler");
return 0;
}
const std::size_t numBytes(size * nmemb);
std::copy(dataPtr, dataPtr + numBytes, std::back_inserter(*vPtr));
return numBytes;
}
} // anonymous namespace end
namespace rustLaunchSite
{
Downloader::Downloader(Logger& logger)
: initHandle_{GetInitHandle(logger)}
, logger_{logger}
{
}
bool Downloader::GetUrlToFile(
const std::filesystem::path& file,
std::string_view url
) const
{
std::ofstream outFile(
file, std::ofstream::out | std::ofstream::binary | std::ofstream::trunc
);
if (!outFile)
{
LOGWRN(logger_, "Failed to open output file for write: " << file);
return false;
}
auto* curlPtr(curl_easy_init());
if (!curlPtr)
{
LOGWRN(logger_, "curl_easy_init() returned nullptr");
outFile.close();
std::filesystem::remove(file);
return false;
}
CURLcode curlCode(CURLE_OK);
curlCode = curlCode == CURLE_OK ?
curl_easy_setopt(curlPtr, CURLOPT_URL, url.data()) : curlCode;
curlCode = curlCode == CURLE_OK ?
curl_easy_setopt(curlPtr, CURLOPT_FOLLOWLOCATION, 1L) : curlCode;
curlCode = curlCode == CURLE_OK ?
curl_easy_setopt(curlPtr, CURLOPT_WRITEFUNCTION, WriteToFile) : curlCode;
curlCode = curlCode == CURLE_OK ?
curl_easy_setopt(curlPtr, CURLOPT_WRITEDATA, &outFile) : curlCode;
curlCode = curlCode == CURLE_OK ?
curl_easy_perform(curlPtr) : curlCode;
curl_easy_cleanup(curlPtr);
outFile.close();
if (curlCode != CURLE_OK)
{
LOGWRN(logger_, "Curl failure for URL `" << url << "`: " << curl_easy_strerror(curlCode) << "");
std::filesystem::remove(file);
return false;
}
return true;
}
std::string Downloader::GetUrlToString(std::string_view url) const
{
std::string retVal{};
auto* curlPtr(curl_easy_init());
if (!curlPtr)
{
LOGWRN(logger_, "curl_easy_init() returned nullptr");
return retVal;
}
CURLcode curlCode(CURLE_OK);
curlCode = curlCode == CURLE_OK ?
curl_easy_setopt(curlPtr, CURLOPT_URL, url.data()) : curlCode;
curlCode = curlCode == CURLE_OK ?
curl_easy_setopt(curlPtr, CURLOPT_FOLLOWLOCATION, 1L) : curlCode;
curlCode = curlCode == CURLE_OK ?
curl_easy_setopt(curlPtr, CURLOPT_USERAGENT, "rustLaunchSite") : curlCode;
curlCode = curlCode == CURLE_OK ?
curl_easy_setopt(curlPtr, CURLOPT_WRITEFUNCTION, WriteToString) : curlCode;
curlCode = curlCode == CURLE_OK ?
curl_easy_setopt(curlPtr, CURLOPT_WRITEDATA, &retVal) : curlCode;
curlCode = curlCode == CURLE_OK ?
curl_easy_perform(curlPtr) : curlCode;
curl_easy_cleanup(curlPtr);
if (curlCode != CURLE_OK)
{
LOGWRN(logger_, "Curl failure for URL `" << url << "`: " << curl_easy_strerror(curlCode) << "");
retVal.clear();
}
return retVal;
}
std::vector<char> Downloader::GetUrlToVector(std::string_view url) const
{
std::vector<char> retVal{};
auto* curlPtr(curl_easy_init());
if (!curlPtr)
{
LOGWRN(logger_, "curl_easy_init() returned nullptr");
return retVal;
}
CURLcode curlCode(CURLE_OK);
curlCode = curlCode == CURLE_OK ?
curl_easy_setopt(curlPtr, CURLOPT_URL, url.data()) : curlCode;
curlCode = curlCode == CURLE_OK ?
curl_easy_setopt(curlPtr, CURLOPT_FOLLOWLOCATION, 1L) : curlCode;
curlCode = curlCode == CURLE_OK ?
curl_easy_setopt(curlPtr, CURLOPT_USERAGENT, "rustLaunchSite") : curlCode;
curlCode = curlCode == CURLE_OK ?
curl_easy_setopt(curlPtr, CURLOPT_WRITEFUNCTION, WriteToVector) : curlCode;
curlCode = curlCode == CURLE_OK ?
curl_easy_setopt(curlPtr, CURLOPT_WRITEDATA, &retVal) : curlCode;
curlCode = curlCode == CURLE_OK ?
curl_easy_perform(curlPtr) : curlCode;
curl_easy_cleanup(curlPtr);
if (curlCode != CURLE_OK)
{
LOGWRN(logger_, "Curl failure for URL `" << url << "`: " << curl_easy_strerror(curlCode) << "");
retVal.clear();
}
return retVal;
}
Downloader::InitHandle Downloader::GetInitHandle(Logger& logger)
{
// set up a mutex ASAP to ensure that weak pointer only ever references one
// shared pointer at a time
static std::mutex mutex;
std::scoped_lock lock{mutex};
static std::size_t handleNumber{0};
static std::weak_ptr<std::size_t> handleWptr{};
// attempt to upgrade weak pointer to a shared pointer ASAP, and return one if
// we get it
if (auto existingHandleSptr{handleWptr.lock()}; existingHandleSptr)
{
return existingHandleSptr;
}
// shared pointer is invalid, so make a new one
++handleNumber;
LOGINF(logger, "Initializing downloader handle #" << handleNumber);
curl_global_init(CURL_GLOBAL_ALL);
// shared pointer points at our static handle count, but instead of managing
// its memory, the deleter performs a global libcurl cleanup action
InitHandle newHandleSptr // NOSONAR
{
&handleNumber, [&logger](std::size_t const* hn)
{
LOGINF(logger, "Deinitializing downloader handle #" << *hn);
curl_global_cleanup();
}
};
handleWptr = newHandleSptr;
return newHandleSptr;
}
} // rustLaunchSite namespace end