GCC Code Coverage Report


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