GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/network/jobinfo.h
Date: 2026-04-05 02:35:23
Exec Total Coverage
Lines: 77 113 68.1%
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 *path_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 4884 ~JobInfo() {
133 4884 pipe_job_results.Destroy();
134 4884 data_tube_.Destroy();
135 4884 }
136
137 static bool EscapeUrlChar(unsigned char input, char output[3]);
138
139 void CreatePipeJobResults() {
140 pipe_job_results = new Pipe<kPipeDownloadJobsResults>();
141 }
142
143 bool IsValidPipeJobResults() { return pipe_job_results.IsValid(); }
144
145 void CreateDataTube() {
146 // TODO(heretherebedragons) change to weighted queue
147 data_tube_ = new Tube<DataTubeElement>(500);
148 }
149
150 bool IsValidDataTube() { return data_tube_.IsValid(); }
151
152 /**
153 * Tells whether the error is because of a non-existing file. Should only
154 * be called if error_code is not kFailOk
155 */
156 bool IsFileNotFound();
157
158 852 pid_t *GetPidPtr() { return &pid_; }
159 852 uid_t *GetUidPtr() { return &uid_; }
160 852 gid_t *GetGidPtr() { return &gid_; }
161 852 InterruptCue **GetInterruptCuePtr() { return &interrupt_cue_; }
162 7890 z_stream *GetZstreamPtr() { return &zstream_; }
163 Failures *GetErrorCodePtr() { return &error_code_; }
164 void **GetCredDataPtr() { return &cred_data_; }
165 curl_slist **GetHeadersPtr() { return &headers_; }
166 CURL **GetCurlHandle() { return &curl_handle_; }
167 7827 shash::ContextPtr *GetHashContextPtr() { return &hash_context_; }
168 Pipe<kPipeDownloadJobsResults> *GetPipeJobResultPtr() {
169 return pipe_job_results.weak_ref();
170 }
171 Tube<DataTubeElement> *GetDataTubePtr() { return data_tube_.weak_ref(); }
172
173 15197 const std::string *url() const { return url_; }
174 16582 bool compressed() const { return compressed_; }
175 5407 bool probe_hosts() const { return probe_hosts_; }
176 4883 bool head_request() const { return head_request_; }
177 147 bool follow_redirects() const { return follow_redirects_; }
178 4883 bool force_nocache() const { return force_nocache_; }
179 pid_t pid() const { return pid_; }
180 uid_t uid() const { return uid_; }
181 gid_t gid() const { return gid_; }
182 5030 void *cred_data() const { return cred_data_; }
183 367 InterruptCue *interrupt_cue() const { return interrupt_cue_; }
184 80426 cvmfs::Sink *sink() const { return sink_; }
185 31613 const shash::Any *expected_hash() const { return expected_hash_; }
186 const std::string *path_info() const { return path_info_; }
187
188 4883 off_t range_offset() const { return range_offset_; }
189 off_t range_size() const { return range_size_; }
190
191 20230 CURL *curl_handle() const { return curl_handle_; }
192 15054 curl_slist *headers() const { return headers_; }
193 4883 char *info_header() const { return info_header_; }
194 char *tracing_header_pid() const { return tracing_header_pid_; }
195 char *tracing_header_gid() const { return tracing_header_gid_; }
196 char *tracing_header_uid() const { return tracing_header_uid_; }
197 z_stream zstream() const { return zstream_; }
198 13091 shash::ContextPtr hash_context() const { return hash_context_; }
199 10843 std::string proxy() const { return proxy_; }
200 std::string link() const { return link_; }
201 6006 bool nocache() const { return nocache_; }
202 17869 Failures error_code() const { return error_code_; }
203 896 int http_code() const { return http_code_; }
204 147 unsigned char num_used_proxies() const { return num_used_proxies_; }
205 unsigned char num_used_metalinks() const { return num_used_metalinks_; }
206 5348 unsigned char num_used_hosts() const { return num_used_hosts_; }
207 5073 unsigned char num_retries() const { return num_retries_; }
208 257 unsigned backoff_ms() const { return backoff_ms_; }
209 5152 int current_metalink_chain_index() const {
210 5152 return current_metalink_chain_index_;
211 }
212 98 int current_host_chain_index() const { return current_host_chain_index_; }
213
214 bool allow_failure() const { return allow_failure_; }
215 19379 int64_t id() const { return id_; }
216
217 std::string GetInfoHeaderContents(const std::string &templ);
218 852 void SetUrl(const std::string *url) { url_ = url; }
219 1603 void SetCompressed(bool compressed) { compressed_ = compressed; }
220 751 void SetProbeHosts(bool probe_hosts) { probe_hosts_ = probe_hosts; }
221 void SetHeadRequest(bool head_request) { head_request_ = head_request; }
222 4883 void SetFollowRedirects(bool follow_redirects) {
223 4883 follow_redirects_ = follow_redirects;
224 4883 }
225 368 void SetForceNocache(bool force_nocache) { force_nocache_ = force_nocache; }
226 void SetCredData(void *cred_data) { cred_data_ = cred_data; }
227 49 void SetInterruptCue(InterruptCue *interrupt_cue) {
228 49 interrupt_cue_ = interrupt_cue;
229 49 }
230 852 void SetSink(cvmfs::Sink *sink) { sink_ = sink; }
231 852 void SetExpectedHash(const shash::Any *expected_hash) {
232 852 expected_hash_ = expected_hash;
233 852 }
234 999 void SetPathInfo(const std::string *path_info) { path_info_ = path_info; }
235
236 999 void SetRangeOffset(off_t range_offset) { range_offset_ = range_offset; }
237 999 void SetRangeSize(off_t range_size) { range_size_ = range_size; }
238
239 4883 void SetCurlHandle(CURL *curl_handle) { curl_handle_ = curl_handle; }
240 9766 void SetHeaders(curl_slist *headers) { headers_ = headers; }
241 4883 void SetInfoHeader(char *info_header) { info_header_ = info_header; }
242 void SetTracingHeaderPid(char *tracing_header_pid) {
243 tracing_header_pid_ = tracing_header_pid;
244 };
245 void SetTracingHeaderGid(char *tracing_header_gid) {
246 tracing_header_gid_ = tracing_header_gid;
247 };
248 void SetTracingHeaderUid(char *tracing_header_uid) {
249 tracing_header_uid_ = tracing_header_uid;
250 };
251 void SetZstream(z_stream zstream) { zstream_ = zstream; }
252 void SetHashContext(shash::ContextPtr hash_context) {
253 hash_context_ = hash_context;
254 }
255 5030 void SetProxy(const std::string &proxy) { proxy_ = proxy; }
256 4883 void SetLink(const std::string &link) { link_ = link; }
257 4949 void SetNocache(bool nocache) { nocache_ = nocache; }
258 10150 void SetErrorCode(Failures error_code) { error_code_ = error_code; }
259 5471 void SetHttpCode(int http_code) { http_code_ = http_code; }
260 4932 void SetNumUsedProxies(unsigned char num_used_proxies) {
261 4932 num_used_proxies_ = num_used_proxies;
262 4932 }
263 4883 void SetNumUsedMetalinks(unsigned char num_used_metalinks) {
264 4883 num_used_metalinks_ = num_used_metalinks;
265 4883 }
266 4981 void SetNumUsedHosts(unsigned char num_used_hosts) {
267 4981 num_used_hosts_ = num_used_hosts;
268 4981 }
269 4939 void SetNumRetries(unsigned char num_retries) { num_retries_ = num_retries; }
270 4949 void SetBackoffMs(unsigned backoff_ms) { backoff_ms_ = backoff_ms; }
271 void SetCurrentMetalinkChainIndex(int current_metalink_chain_index) {
272 current_metalink_chain_index_ = current_metalink_chain_index;
273 }
274 2826 void SetCurrentHostChainIndex(int current_host_chain_index) {
275 2826 current_host_chain_index_ = current_host_chain_index;
276 2826 }
277
278 void SetAllowFailure(bool allow_failure) { allow_failure_ = allow_failure; }
279
280 // needed for fetch.h ThreadLocalStorage
281
2/4
✓ Branch 2 taken 751 times.
✗ Branch 3 not taken.
✓ Branch 8 taken 751 times.
✗ Branch 9 not taken.
751 JobInfo() { Init(); }
282 }; // JobInfo
283
284 } // namespace download
285
286 #endif // CVMFS_NETWORK_JOBINFO_H_
287