GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/network/jobinfo.h
Date: 2024-04-21 02:33:16
Exec Total Coverage
Lines: 68 101 67.3%
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.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 bool nocache_;
103 Failures error_code_;
104 int http_code_;
105 unsigned char num_used_proxies_;
106 unsigned char num_used_hosts_;
107 unsigned char num_retries_;
108 unsigned backoff_ms_;
109 unsigned int current_host_chain_index_;
110
111 // Don't fail-over proxies on download errors. default = false
112 bool allow_failure_;
113
114 // TODO(heretherebedragons) c++11 allows to delegate constructors (N1986)
115 // Replace Init() with JobInfo() that is called by the other constructors
116 void Init();
117
118 public:
119 /**
120 * Sink version: downloads entire data chunk where URL u points to
121 */
122 JobInfo(const std::string *u, const bool c, const bool ph,
123 const shash::Any *h, cvmfs::Sink *s);
124
125 /**
126 * No sink version: Only downloads header where the URL u points to
127 */
128 JobInfo(const std::string *u, const bool ph);
129
130 198 ~JobInfo() {
131 198 pipe_job_results.Destroy();
132 198 data_tube_.Destroy();
133 198 }
134
135 void CreatePipeJobResults() {
136 pipe_job_results = new Pipe<kPipeDownloadJobsResults>();
137 }
138
139 bool IsValidPipeJobResults() {
140 return pipe_job_results.IsValid();
141 }
142
143 void CreateDataTube() {
144 // TODO(heretherebedragons) change to weighted queue
145 data_tube_ = new Tube<DataTubeElement>(500);
146 }
147
148 bool IsValidDataTube() {
149 return data_tube_.IsValid();
150 }
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 4 pid_t *GetPidPtr() { return &pid_; }
159 4 uid_t *GetUidPtr() { return &uid_; }
160 4 gid_t *GetGidPtr() { return &gid_; }
161 4 InterruptCue **GetInterruptCuePtr() { return &interrupt_cue_; }
162 388 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 390 shash::ContextPtr *GetHashContextPtr() { return &hash_context_; }
168 Pipe<kPipeDownloadJobsResults> *GetPipeJobResultPtr() {
169 return pipe_job_results.weak_ref(); }
170 Tube<DataTubeElement> *GetDataTubePtr() { return data_tube_.weak_ref(); }
171
172 629 const std::string* url() const { return url_; }
173 646 bool compressed() const { return compressed_; }
174 221 bool probe_hosts() const { return probe_hosts_; }
175 203 bool head_request() const { return head_request_; }
176 3 bool follow_redirects() const { return follow_redirects_; }
177 203 bool force_nocache() const { return force_nocache_; }
178 pid_t pid() const { return pid_; }
179 uid_t uid() const { return uid_; }
180 gid_t gid() const { return gid_; }
181 206 void *cred_data() const { return cred_data_; }
182 13 InterruptCue *interrupt_cue() const { return interrupt_cue_; }
183 3168 cvmfs::Sink *sink() const { return sink_; }
184 1307 const shash::Any *expected_hash() const { return expected_hash_; }
185 const std::string *extra_info() const { return extra_info_; }
186
187 203 off_t range_offset() const { return range_offset_; }
188 off_t range_size() const { return range_size_; }
189
190 835 CURL *curl_handle() const { return curl_handle_; }
191 627 curl_slist *headers() const { return headers_; }
192 203 char *info_header() const { return info_header_; }
193 char *tracing_header_pid() const { return tracing_header_pid_; }
194 char *tracing_header_gid() const { return tracing_header_gid_; }
195 char *tracing_header_uid() const { return tracing_header_uid_; }
196 z_stream zstream() const { return zstream_; }
197 646 shash::ContextPtr hash_context() const { return hash_context_; }
198 443 std::string proxy() const { return proxy_; }
199 249 bool nocache() const { return nocache_; }
200 820 Failures error_code() const { return error_code_; }
201 22 int http_code() const { return http_code_; }
202 3 unsigned char num_used_proxies() const { return num_used_proxies_; }
203 15 unsigned char num_used_hosts() const { return num_used_hosts_; }
204 213 unsigned char num_retries() const { return num_retries_; }
205 23 unsigned backoff_ms() const { return backoff_ms_; }
206 2 unsigned int current_host_chain_index() const {
207 2 return current_host_chain_index_; }
208
209 bool allow_failure() const { return allow_failure_; }
210 583 int64_t id() const { return id_; }
211
212
213 41 void SetUrl(const std::string *url) { url_ = url; }
214 72 void SetCompressed(bool compressed) { compressed_ = compressed; }
215 31 void SetProbeHosts(bool probe_hosts) { probe_hosts_ = probe_hosts; }
216 void SetHeadRequest(bool head_request) { head_request_ = head_request; }
217 203 void SetFollowRedirects(bool follow_redirects)
218 203 { follow_redirects_ = follow_redirects; }
219 16 void SetForceNocache(bool force_nocache) { force_nocache_ = force_nocache; }
220 void SetPid(pid_t pid) { pid_ = pid; }
221 void SetUid(uid_t uid) { uid_ = uid; }
222 void SetGid(gid_t gid) { gid_ = gid; }
223 void SetCredData(void *cred_data) { cred_data_ = cred_data; }
224 1 void SetInterruptCue(InterruptCue *interrupt_cue)
225 1 { interrupt_cue_ = interrupt_cue; }
226 41 void SetSink(cvmfs::Sink *sink)
227 41 { sink_ = sink; }
228 41 void SetExpectedHash(const shash::Any *expected_hash)
229 41 { expected_hash_ = expected_hash; }
230 43 void SetExtraInfo(const std::string *extra_info)
231 43 { extra_info_ = extra_info; }
232
233 43 void SetRangeOffset(off_t range_offset) { range_offset_ = range_offset; }
234 43 void SetRangeSize(off_t range_size) { range_size_ = range_size; }
235
236 203 void SetCurlHandle(CURL *curl_handle) { curl_handle_ = curl_handle; }
237 406 void SetHeaders(curl_slist *headers) { headers_ = headers; }
238 203 void SetInfoHeader(char *info_header) { info_header_ = info_header; }
239 void SetTracingHeaderPid(char *tracing_header_pid)
240 { tracing_header_pid_ = tracing_header_pid; };
241 void SetTracingHeaderGid(char *tracing_header_gid)
242 { tracing_header_gid_ = tracing_header_gid; };
243 void SetTracingHeaderUid(char *tracing_header_uid)
244 { tracing_header_uid_ = tracing_header_uid; };
245 void SetZstream(z_stream zstream) { zstream_ = zstream; }
246 void SetHashContext(shash::ContextPtr hash_context)
247 { hash_context_ = hash_context; }
248 206 void SetProxy(const std::string &proxy) { proxy_ = proxy; }
249 206 void SetNocache(bool nocache) { nocache_ = nocache; }
250 421 void SetErrorCode(Failures error_code) { error_code_ = error_code; }
251 215 void SetHttpCode(int http_code) { http_code_ = http_code; }
252 204 void SetNumUsedProxies(unsigned char num_used_proxies)
253 204 { num_used_proxies_ = num_used_proxies; }
254 205 void SetNumUsedHosts(unsigned char num_used_hosts)
255 205 { num_used_hosts_ = num_used_hosts; }
256 208 void SetNumRetries(unsigned char num_retries) { num_retries_ = num_retries; }
257 208 void SetBackoffMs(unsigned backoff_ms) { backoff_ms_ = backoff_ms; }
258 104 void SetCurrentHostChainIndex(unsigned int current_host_chain_index)
259 104 { current_host_chain_index_ = current_host_chain_index; }
260
261 void SetAllowFailure(bool allow_failure) { allow_failure_ = allow_failure; }
262
263 // needed for fetch.h ThreadLocalStorage
264
2/4
✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 31 times.
✗ Branch 8 not taken.
31 JobInfo() { Init(); }
265 }; // JobInfo
266
267 } // namespace download
268
269 #endif // CVMFS_NETWORK_JOBINFO_H_
270