GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/network/jobinfo.h
Date: 2025-06-22 02:36:02
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 7915 ~JobInfo() {
133 7915 pipe_job_results.Destroy();
134 7915 data_tube_.Destroy();
135 7915 }
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 194 pid_t *GetPidPtr() { return &pid_; }
157 194 uid_t *GetUidPtr() { return &uid_; }
158 194 gid_t *GetGidPtr() { return &gid_; }
159 194 InterruptCue **GetInterruptCuePtr() { return &interrupt_cue_; }
160 14796 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 14769 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 25050 const std::string *url() const { return url_; }
172 26113 bool compressed() const { return compressed_; }
173 8760 bool probe_hosts() const { return probe_hosts_; }
174 8106 bool head_request() const { return head_request_; }
175 147 bool follow_redirects() const { return follow_redirects_; }
176 8106 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 8253 void *cred_data() const { return cred_data_; }
181 485 InterruptCue *interrupt_cue() const { return interrupt_cue_; }
182 128141 cvmfs::Sink *sink() const { return sink_; }
183 52095 const shash::Any *expected_hash() const { return expected_hash_; }
184 const std::string *extra_info() const { return extra_info_; }
185
186 8106 off_t range_offset() const { return range_offset_; }
187 off_t range_size() const { return range_size_; }
188
189 33351 CURL *curl_handle() const { return curl_handle_; }
190 25056 curl_slist *headers() const { return headers_; }
191 8106 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 24531 shash::ContextPtr hash_context() const { return hash_context_; }
197 17589 std::string proxy() const { return proxy_; }
198 std::string link() const { return link_; }
199 9760 bool nocache() const { return nocache_; }
200 30308 Failures error_code() const { return error_code_; }
201 1078 int http_code() const { return http_code_; }
202 147 unsigned char num_used_proxies() const { return num_used_proxies_; }
203 unsigned char num_used_metalinks() const { return num_used_metalinks_; }
204 8689 unsigned char num_used_hosts() const { return num_used_hosts_; }
205 8388 unsigned char num_retries() const { return num_retries_; }
206 642 unsigned backoff_ms() const { return backoff_ms_; }
207 8493 int current_metalink_chain_index() const {
208 8493 return current_metalink_chain_index_;
209 }
210 98 int current_host_chain_index() const { return current_host_chain_index_; }
211
212 bool allow_failure() const { return allow_failure_; }
213 27640 int64_t id() const { return id_; }
214
215
216 1228 void SetUrl(const std::string *url) { url_ = url; }
217 2115 void SetCompressed(bool compressed) { compressed_ = compressed; }
218 887 void SetProbeHosts(bool probe_hosts) { probe_hosts_ = probe_hosts; }
219 void SetHeadRequest(bool head_request) { head_request_ = head_request; }
220 8106 void SetFollowRedirects(bool follow_redirects) {
221 8106 follow_redirects_ = follow_redirects;
222 8106 }
223 784 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 49 void SetInterruptCue(InterruptCue *interrupt_cue) {
229 49 interrupt_cue_ = interrupt_cue;
230 49 }
231 1228 void SetSink(cvmfs::Sink *sink) { sink_ = sink; }
232 1228 void SetExpectedHash(const shash::Any *expected_hash) {
233 1228 expected_hash_ = expected_hash;
234 1228 }
235 1375 void SetExtraInfo(const std::string *extra_info) { extra_info_ = extra_info; }
236
237 1375 void SetRangeOffset(off_t range_offset) { range_offset_ = range_offset; }
238 1375 void SetRangeSize(off_t range_size) { range_size_ = range_size; }
239
240 8106 void SetCurlHandle(CURL *curl_handle) { curl_handle_ = curl_handle; }
241 16212 void SetHeaders(curl_slist *headers) { headers_ = headers; }
242 8106 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 8253 void SetProxy(const std::string &proxy) { proxy_ = proxy; }
257 8106 void SetLink(const std::string &link) { link_ = link; }
258 8205 void SetNocache(bool nocache) { nocache_ = nocache; }
259 16747 void SetErrorCode(Failures error_code) { error_code_ = error_code; }
260 8694 void SetHttpCode(int http_code) { http_code_ = http_code; }
261 8155 void SetNumUsedProxies(unsigned char num_used_proxies) {
262 8155 num_used_proxies_ = num_used_proxies;
263 8155 }
264 8106 void SetNumUsedMetalinks(unsigned char num_used_metalinks) {
265 8106 num_used_metalinks_ = num_used_metalinks;
266 8106 }
267 8204 void SetNumUsedHosts(unsigned char num_used_hosts) {
268 8204 num_used_hosts_ = num_used_hosts;
269 8204 }
270 8247 void SetNumRetries(unsigned char num_retries) { num_retries_ = num_retries; }
271 8247 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 3821 void SetCurrentHostChainIndex(int current_host_chain_index) {
276 3821 current_host_chain_index_ = current_host_chain_index;
277 3821 }
278
279 void SetAllowFailure(bool allow_failure) { allow_failure_ = allow_failure; }
280
281 // needed for fetch.h ThreadLocalStorage
282
2/4
✓ Branch 2 taken 887 times.
✗ Branch 3 not taken.
✓ Branch 8 taken 887 times.
✗ Branch 9 not taken.
887 JobInfo() { Init(); }
283 }; // JobInfo
284
285 } // namespace download
286
287 #endif // CVMFS_NETWORK_JOBINFO_H_
288