| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/network/jobinfo.h |
| Date: | 2025-11-09 02:35:23 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 77 | 114 | 67.5% |
| Branches: | 2 | 10 | 20.0% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | */ | ||
| 4 | |||
| 5 | #ifndef CVMFS_NETWORK_JOBINFO_H_ | ||
| 6 | #define CVMFS_NETWORK_JOBINFO_H_ | ||
| 7 | |||
| 8 | #include <poll.h> | ||
| 9 | #include <pthread.h> | ||
| 10 | #include <stdint.h> | ||
| 11 | #include <unistd.h> | ||
| 12 | |||
| 13 | #include <cstdio> | ||
| 14 | #include <map> | ||
| 15 | #include <set> | ||
| 16 | #include <string> | ||
| 17 | #include <vector> | ||
| 18 | |||
| 19 | #include "compression/compression.h" | ||
| 20 | #include "crypto/hash.h" | ||
| 21 | #include "duplex_curl.h" | ||
| 22 | #include "network/network_errors.h" | ||
| 23 | #include "network/sink.h" | ||
| 24 | #include "network/sink_file.h" | ||
| 25 | #include "network/sink_mem.h" | ||
| 26 | #include "network/sink_path.h" | ||
| 27 | #include "util/pipe.h" | ||
| 28 | #include "util/tube.h" | ||
| 29 | |||
| 30 | class InterruptCue; | ||
| 31 | |||
| 32 | namespace download { | ||
| 33 | |||
| 34 | enum DataTubeAction { | ||
| 35 | kActionStop = 0, | ||
| 36 | kActionContinue, | ||
| 37 | kActionDecompress | ||
| 38 | }; | ||
| 39 | |||
| 40 | /** | ||
| 41 | * Wrapper for the data tube to transfer data from CallbackCurlData() that is | ||
| 42 | * executed in MainDownload() Thread to Fetch() called by a fuse thread | ||
| 43 | * | ||
| 44 | * TODO(heretherebedragons): do we want to have a pool of those | ||
| 45 | * datatubeelements? | ||
| 46 | */ | ||
| 47 | struct DataTubeElement : SingleCopy { | ||
| 48 | char *data; | ||
| 49 | size_t size; | ||
| 50 | DataTubeAction action; | ||
| 51 | |||
| 52 | ✗ | explicit DataTubeElement(DataTubeAction xact) | |
| 53 | ✗ | : data(NULL), size(0), action(xact) { } | |
| 54 | DataTubeElement(char *mov_data, size_t xsize, DataTubeAction xact) | ||
| 55 | : data(mov_data), size(xsize), action(xact) { } | ||
| 56 | |||
| 57 | ✗ | ~DataTubeElement() { delete data; } | |
| 58 | }; | ||
| 59 | |||
| 60 | /** | ||
| 61 | * Contains all the information to specify a download job. | ||
| 62 | */ | ||
| 63 | class JobInfo { | ||
| 64 | private: | ||
| 65 | static atomic_int64 next_uuid; | ||
| 66 | int64_t id_; | ||
| 67 | /// Pipe used for the return value | ||
| 68 | UniquePtr<Pipe<kPipeDownloadJobsResults> > pipe_job_results; | ||
| 69 | /// Tube (bounded thread-safe queue) to transport data from CURL callback | ||
| 70 | /// to be decompressed in Fetch() instead of MainDownload() | ||
| 71 | UniquePtr<Tube<DataTubeElement> > data_tube_; | ||
| 72 | const std::string *url_; | ||
| 73 | bool compressed_; | ||
| 74 | bool probe_hosts_; | ||
| 75 | bool head_request_; | ||
| 76 | bool follow_redirects_; | ||
| 77 | bool force_nocache_; | ||
| 78 | pid_t pid_; | ||
| 79 | uid_t uid_; | ||
| 80 | gid_t gid_; | ||
| 81 | void *cred_data_; // Per-transfer credential data | ||
| 82 | InterruptCue *interrupt_cue_; | ||
| 83 | cvmfs::Sink *sink_; | ||
| 84 | const shash::Any *expected_hash_; | ||
| 85 | const std::string *extra_info_; | ||
| 86 | |||
| 87 | // Allow byte ranges to be specified. | ||
| 88 | off_t range_offset_; | ||
| 89 | off_t range_size_; | ||
| 90 | |||
| 91 | // Internal state | ||
| 92 | CURL *curl_handle_; | ||
| 93 | curl_slist *headers_; | ||
| 94 | char *info_header_; | ||
| 95 | char *tracing_header_pid_; | ||
| 96 | char *tracing_header_gid_; | ||
| 97 | char *tracing_header_uid_; | ||
| 98 | z_stream zstream_; | ||
| 99 | shash::ContextPtr hash_context_; | ||
| 100 | std::string proxy_; | ||
| 101 | std::string link_; | ||
| 102 | bool nocache_; | ||
| 103 | Failures error_code_; | ||
| 104 | int http_code_; | ||
| 105 | unsigned char num_used_proxies_; | ||
| 106 | unsigned char num_used_metalinks_; | ||
| 107 | unsigned char num_used_hosts_; | ||
| 108 | unsigned char num_retries_; | ||
| 109 | unsigned backoff_ms_; | ||
| 110 | int current_metalink_chain_index_; | ||
| 111 | int current_host_chain_index_; | ||
| 112 | |||
| 113 | // Don't fail-over proxies on download errors. default = false | ||
| 114 | bool allow_failure_; | ||
| 115 | |||
| 116 | // TODO(heretherebedragons) c++11 allows to delegate constructors (N1986) | ||
| 117 | // Replace Init() with JobInfo() that is called by the other constructors | ||
| 118 | void Init(); | ||
| 119 | |||
| 120 | public: | ||
| 121 | /** | ||
| 122 | * Sink version: downloads entire data chunk where URL u points to | ||
| 123 | */ | ||
| 124 | JobInfo(const std::string *u, const bool c, const bool ph, | ||
| 125 | const shash::Any *h, cvmfs::Sink *s); | ||
| 126 | |||
| 127 | /** | ||
| 128 | * No sink version: Only downloads header where the URL u points to | ||
| 129 | */ | ||
| 130 | JobInfo(const std::string *u, const bool ph); | ||
| 131 | |||
| 132 | 6270 | ~JobInfo() { | |
| 133 | 6270 | pipe_job_results.Destroy(); | |
| 134 | 6270 | data_tube_.Destroy(); | |
| 135 | 6270 | } | |
| 136 | |||
| 137 | ✗ | void CreatePipeJobResults() { | |
| 138 | ✗ | pipe_job_results = new Pipe<kPipeDownloadJobsResults>(); | |
| 139 | } | ||
| 140 | |||
| 141 | ✗ | bool IsValidPipeJobResults() { return pipe_job_results.IsValid(); } | |
| 142 | |||
| 143 | ✗ | void CreateDataTube() { | |
| 144 | // TODO(heretherebedragons) change to weighted queue | ||
| 145 | ✗ | data_tube_ = new Tube<DataTubeElement>(500); | |
| 146 | } | ||
| 147 | |||
| 148 | ✗ | bool IsValidDataTube() { return data_tube_.IsValid(); } | |
| 149 | |||
| 150 | /** | ||
| 151 | * Tells whether the error is because of a non-existing file. Should only | ||
| 152 | * be called if error_code is not kFailOk | ||
| 153 | */ | ||
| 154 | bool IsFileNotFound(); | ||
| 155 | |||
| 156 | 1149 | pid_t *GetPidPtr() { return &pid_; } | |
| 157 | 1149 | uid_t *GetUidPtr() { return &uid_; } | |
| 158 | 1149 | gid_t *GetGidPtr() { return &gid_; } | |
| 159 | 1149 | InterruptCue **GetInterruptCuePtr() { return &interrupt_cue_; } | |
| 160 | 12784 | z_stream *GetZstreamPtr() { return &zstream_; } | |
| 161 | Failures *GetErrorCodePtr() { return &error_code_; } | ||
| 162 | ✗ | void **GetCredDataPtr() { return &cred_data_; } | |
| 163 | ✗ | curl_slist **GetHeadersPtr() { return &headers_; } | |
| 164 | CURL **GetCurlHandle() { return &curl_handle_; } | ||
| 165 | 12855 | shash::ContextPtr *GetHashContextPtr() { return &hash_context_; } | |
| 166 | ✗ | Pipe<kPipeDownloadJobsResults> *GetPipeJobResultPtr() { | |
| 167 | ✗ | return pipe_job_results.weak_ref(); | |
| 168 | } | ||
| 169 | ✗ | Tube<DataTubeElement> *GetDataTubePtr() { return data_tube_.weak_ref(); } | |
| 170 | |||
| 171 | 20229 | const std::string *url() const { return url_; } | |
| 172 | 21099 | bool compressed() const { return compressed_; } | |
| 173 | 7227 | bool probe_hosts() const { return probe_hosts_; } | |
| 174 | 6453 | bool head_request() const { return head_request_; } | |
| 175 | 138 | bool follow_redirects() const { return follow_redirects_; } | |
| 176 | 6453 | bool force_nocache() const { return force_nocache_; } | |
| 177 | ✗ | pid_t pid() const { return pid_; } | |
| 178 | ✗ | uid_t uid() const { return uid_; } | |
| 179 | ✗ | gid_t gid() const { return gid_; } | |
| 180 | 6591 | void *cred_data() const { return cred_data_; } | |
| 181 | 548 | InterruptCue *interrupt_cue() const { return interrupt_cue_; } | |
| 182 | 100863 | cvmfs::Sink *sink() const { return sink_; } | |
| 183 | 42448 | const shash::Any *expected_hash() const { return expected_hash_; } | |
| 184 | ✗ | const std::string *extra_info() const { return extra_info_; } | |
| 185 | |||
| 186 | 6453 | off_t range_offset() const { return range_offset_; } | |
| 187 | ✗ | off_t range_size() const { return range_size_; } | |
| 188 | |||
| 189 | 26820 | CURL *curl_handle() const { return curl_handle_; } | |
| 190 | 20187 | curl_slist *headers() const { return headers_; } | |
| 191 | 6453 | char *info_header() const { return info_header_; } | |
| 192 | ✗ | char *tracing_header_pid() const { return tracing_header_pid_; } | |
| 193 | ✗ | char *tracing_header_gid() const { return tracing_header_gid_; } | |
| 194 | ✗ | char *tracing_header_uid() const { return tracing_header_uid_; } | |
| 195 | z_stream zstream() const { return zstream_; } | ||
| 196 | 21277 | shash::ContextPtr hash_context() const { return hash_context_; } | |
| 197 | 14454 | std::string proxy() const { return proxy_; } | |
| 198 | ✗ | std::string link() const { return link_; } | |
| 199 | 8419 | bool nocache() const { return nocache_; } | |
| 200 | 27558 | Failures error_code() const { return error_code_; } | |
| 201 | 1012 | int http_code() const { return http_code_; } | |
| 202 | 138 | unsigned char num_used_proxies() const { return num_used_proxies_; } | |
| 203 | ✗ | unsigned char num_used_metalinks() const { return num_used_metalinks_; } | |
| 204 | 7093 | unsigned char num_used_hosts() const { return num_used_hosts_; } | |
| 205 | 6813 | unsigned char num_retries() const { return num_retries_; } | |
| 206 | 834 | unsigned backoff_ms() const { return backoff_ms_; } | |
| 207 | 6909 | int current_metalink_chain_index() const { | |
| 208 | 6909 | return current_metalink_chain_index_; | |
| 209 | } | ||
| 210 | 92 | int current_host_chain_index() const { return current_host_chain_index_; } | |
| 211 | |||
| 212 | ✗ | bool allow_failure() const { return allow_failure_; } | |
| 213 | 23038 | int64_t id() const { return id_; } | |
| 214 | |||
| 215 | |||
| 216 | 1149 | void SetUrl(const std::string *url) { url_ = url; } | |
| 217 | 2048 | void SetCompressed(bool compressed) { compressed_ = compressed; } | |
| 218 | 899 | void SetProbeHosts(bool probe_hosts) { probe_hosts_ = probe_hosts; } | |
| 219 | void SetHeadRequest(bool head_request) { head_request_ = head_request; } | ||
| 220 | 6453 | void SetFollowRedirects(bool follow_redirects) { | |
| 221 | 6453 | follow_redirects_ = follow_redirects; | |
| 222 | 6453 | } | |
| 223 | 736 | void SetForceNocache(bool force_nocache) { force_nocache_ = force_nocache; } | |
| 224 | void SetPid(pid_t pid) { pid_ = pid; } | ||
| 225 | void SetUid(uid_t uid) { uid_ = uid; } | ||
| 226 | void SetGid(gid_t gid) { gid_ = gid; } | ||
| 227 | ✗ | void SetCredData(void *cred_data) { cred_data_ = cred_data; } | |
| 228 | 46 | void SetInterruptCue(InterruptCue *interrupt_cue) { | |
| 229 | 46 | interrupt_cue_ = interrupt_cue; | |
| 230 | 46 | } | |
| 231 | 1149 | void SetSink(cvmfs::Sink *sink) { sink_ = sink; } | |
| 232 | 1149 | void SetExpectedHash(const shash::Any *expected_hash) { | |
| 233 | 1149 | expected_hash_ = expected_hash; | |
| 234 | 1149 | } | |
| 235 | 1194 | void SetExtraInfo(const std::string *extra_info) { extra_info_ = extra_info; } | |
| 236 | |||
| 237 | 1194 | void SetRangeOffset(off_t range_offset) { range_offset_ = range_offset; } | |
| 238 | 1194 | void SetRangeSize(off_t range_size) { range_size_ = range_size; } | |
| 239 | |||
| 240 | 6453 | void SetCurlHandle(CURL *curl_handle) { curl_handle_ = curl_handle; } | |
| 241 | 12906 | void SetHeaders(curl_slist *headers) { headers_ = headers; } | |
| 242 | 6453 | void SetInfoHeader(char *info_header) { info_header_ = info_header; } | |
| 243 | ✗ | void SetTracingHeaderPid(char *tracing_header_pid) { | |
| 244 | ✗ | tracing_header_pid_ = tracing_header_pid; | |
| 245 | ✗ | }; | |
| 246 | ✗ | void SetTracingHeaderGid(char *tracing_header_gid) { | |
| 247 | ✗ | tracing_header_gid_ = tracing_header_gid; | |
| 248 | ✗ | }; | |
| 249 | ✗ | void SetTracingHeaderUid(char *tracing_header_uid) { | |
| 250 | ✗ | tracing_header_uid_ = tracing_header_uid; | |
| 251 | ✗ | }; | |
| 252 | void SetZstream(z_stream zstream) { zstream_ = zstream; } | ||
| 253 | void SetHashContext(shash::ContextPtr hash_context) { | ||
| 254 | hash_context_ = hash_context; | ||
| 255 | } | ||
| 256 | 6591 | void SetProxy(const std::string &proxy) { proxy_ = proxy; } | |
| 257 | 6453 | void SetLink(const std::string &link) { link_ = link; } | |
| 258 | 6591 | void SetNocache(bool nocache) { nocache_ = nocache; } | |
| 259 | 13546 | void SetErrorCode(Failures error_code) { error_code_ = error_code; } | |
| 260 | 7005 | void SetHttpCode(int http_code) { http_code_ = http_code; } | |
| 261 | 6499 | void SetNumUsedProxies(unsigned char num_used_proxies) { | |
| 262 | 6499 | num_used_proxies_ = num_used_proxies; | |
| 263 | 6499 | } | |
| 264 | 6453 | void SetNumUsedMetalinks(unsigned char num_used_metalinks) { | |
| 265 | 6453 | num_used_metalinks_ = num_used_metalinks; | |
| 266 | 6453 | } | |
| 267 | 6545 | void SetNumUsedHosts(unsigned char num_used_hosts) { | |
| 268 | 6545 | num_used_hosts_ = num_used_hosts; | |
| 269 | 6545 | } | |
| 270 | 6633 | void SetNumRetries(unsigned char num_retries) { num_retries_ = num_retries; } | |
| 271 | 6681 | void SetBackoffMs(unsigned backoff_ms) { backoff_ms_ = backoff_ms; } | |
| 272 | ✗ | void SetCurrentMetalinkChainIndex(int current_metalink_chain_index) { | |
| 273 | ✗ | current_metalink_chain_index_ = current_metalink_chain_index; | |
| 274 | } | ||
| 275 | 2332 | void SetCurrentHostChainIndex(int current_host_chain_index) { | |
| 276 | 2332 | current_host_chain_index_ = current_host_chain_index; | |
| 277 | 2332 | } | |
| 278 | |||
| 279 | void SetAllowFailure(bool allow_failure) { allow_failure_ = allow_failure; } | ||
| 280 | |||
| 281 | // needed for fetch.h ThreadLocalStorage | ||
| 282 |
2/4✓ Branch 2 taken 899 times.
✗ Branch 3 not taken.
✓ Branch 8 taken 899 times.
✗ Branch 9 not taken.
|
899 | JobInfo() { Init(); } |
| 283 | }; // JobInfo | ||
| 284 | |||
| 285 | } // namespace download | ||
| 286 | |||
| 287 | #endif // CVMFS_NETWORK_JOBINFO_H_ | ||
| 288 |