GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/object_fetcher.h
Date: 2026-04-05 02:35:23
Exec Total Coverage
Lines: 206 235 87.7%
Branches: 122 237 51.5%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #ifndef CVMFS_OBJECT_FETCHER_H_
6 #define CVMFS_OBJECT_FETCHER_H_
7
8 #include <unistd.h>
9
10 #include <string>
11
12 #include "catalog.h"
13 #include "crypto/signature.h"
14 #include "history_sqlite.h"
15 #include "manifest.h"
16 #include "manifest_fetch.h"
17 #include "network/download.h"
18 #include "reflog.h"
19 #include "util/posix.h"
20
21 /**
22 * Trait class to define the concrete object types produced by the methods of
23 * concrete instantiations of AbstractObjectFetcher<>. For each implementation
24 * of AbstractObjectFetcher<> one needs to provide a specialisation of this
25 * trait. Note that this specialisation can be templated with the actual para-
26 * meters, hence the parameter space does not explode.
27 *
28 * See: http://stackoverflow.com/questions/6006614/
29 * c-static-polymorphism-crtp-and-using-typedefs-from-derived-classes
30 */
31 template<class ConcreteObjectFetcherT>
32 struct object_fetcher_traits;
33
34 struct ObjectFetcherFailures {
35 enum Failures {
36 kFailOk,
37 kFailNotFound,
38 kFailLocalIO,
39 kFailNetwork,
40 kFailDecompression,
41 kFailManifestNameMismatch,
42 kFailManifestSignatureMismatch,
43 kFailBadData,
44 kFailUnknown,
45
46 kFailNumEntries
47 };
48 };
49
50 60 inline const char *Code2Ascii(const ObjectFetcherFailures::Failures error) {
51 const char *texts[ObjectFetcherFailures::kFailNumEntries + 1];
52 60 texts[0] = "OK";
53 60 texts[1] = "object not found";
54 60 texts[2] = "local I/O failure";
55 60 texts[3] = "network failure";
56 60 texts[4] = "decompression failed";
57 60 texts[5] = "manifest name doesn't match";
58 60 texts[6] = "manifest signature is invalid";
59 60 texts[7] = "bad data received";
60 60 texts[8] = "no text";
61 60 return texts[error];
62 }
63
64 /**
65 * This is the default class implementing the data object fetching strategy. It
66 * is meant to be used when CVMFS specific data structures need to be downloaded
67 * from a backend storage of a repository.
68 *
69 * ObjectFetchers are supposed to be configured for one specific repository. How
70 * this is done depends on the concrete implementation of this base class. When
71 * a concrete implementation of ObjectFetcher<> needs to deal with files on the
72 * local file system it is obliged to take measures for proper cleanup of those
73 * files after usage.
74 *
75 * It abstracts all accesses to external file or HTTP resources and gathers this
76 * access logic in one central point. This also comes in handy when unit testing
77 * components that depend on downloading CVMFS data structures from a repository
78 * backend storage like CatalogTraversal<> or GarbageCollector<>.
79 */
80 template<class DerivedT>
81 class AbstractObjectFetcher : public ObjectFetcherFailures {
82 public:
83 typedef typename object_fetcher_traits<DerivedT>::CatalogTN CatalogTN;
84 typedef typename object_fetcher_traits<DerivedT>::HistoryTN HistoryTN;
85 typedef typename object_fetcher_traits<DerivedT>::ReflogTN ReflogTN;
86
87 typedef ObjectFetcherFailures::Failures Failures;
88
89 static const std::string kManifestFilename;
90 static const std::string kReflogFilename;
91
92 public:
93 /**
94 * Fetches and opens the manifest of the repository this object fetcher is
95 * configured for. Note that the user is responsible to clean up this object.
96 *
97 * @param manifest pointer to a manifest object pointer
98 * @return failure code, specifying the action's result
99 */
100 4912 Failures FetchManifest(manifest::Manifest **manifest) {
101 4912 return static_cast<DerivedT *>(this)->FetchManifest(manifest);
102 }
103
104 /**
105 * Downloads and opens (read-only) a history database. Note that the user is
106 * responsible to remove the history object after usage. The fetched SQLite
107 * database file will be unlinked automatically during the destruction of the
108 * HistoryTN object.
109 *
110 * @param history pointer to a history database object pointer
111 * @param history_hash (optional) the content hash of the history database
112 * if left blank, the latest one is downloaded
113 * @return failure code, specifying the action's result
114 */
115 2422 Failures FetchHistory(HistoryTN **history,
116 const shash::Any &history_hash = shash::Any()) {
117 // retrieve the current HEAD history hash (if nothing else given)
118
2/2
✓ Branch 1 taken 156 times.
✓ Branch 2 taken 2021 times.
2422 shash::Any effective_history_hash = (!history_hash.IsNull())
119 ? history_hash
120
1/2
✓ Branch 1 taken 2021 times.
✗ Branch 2 not taken.
2126 : GetHistoryHash();
121
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2177 times.
2422 if (effective_history_hash.IsNull()) {
122 return kFailNotFound;
123 }
124
3/4
✓ Branch 0 taken 2021 times.
✓ Branch 1 taken 156 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2021 times.
2422 assert(history_hash.suffix == shash::kSuffixHistory
125 || history_hash.IsNull());
126
127 // download the history hash
128 2422 std::string path;
129
1/2
✓ Branch 1 taken 2177 times.
✗ Branch 2 not taken.
2422 const Failures retval = Fetch(effective_history_hash, &path);
130
2/2
✓ Branch 0 taken 134 times.
✓ Branch 1 taken 2043 times.
2422 if (retval != kFailOk) {
131 204 return retval;
132 }
133
134 // open the history file
135
1/2
✓ Branch 1 taken 2043 times.
✗ Branch 2 not taken.
2218 *history = HistoryTN::Open(path);
136
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2039 times.
2218 if (NULL == *history) {
137 4 return kFailLocalIO;
138 }
139
140
1/2
✓ Branch 1 taken 2039 times.
✗ Branch 2 not taken.
2214 (*history)->TakeDatabaseFileOwnership();
141 2214 return kFailOk;
142 2422 }
143
144 /**
145 * Downloads and opens a catalog. Note that the user is responsible to remove
146 * the catalog object after usage.
147 *
148 * @param catalog_hash the content hash of the catalog object
149 * @param catalog_path the root_path the catalog is mounted on
150 * @param catalog pointer to the fetched catalog object pointer
151 * @param is_nested a hint if the catalog to be loaded is a nested one
152 * @param parent (optional) parent catalog of the requested catalog
153 * @return failure code, specifying the action's result
154 */
155 18070322 Failures FetchCatalog(const shash::Any &catalog_hash,
156 const std::string &catalog_path,
157 CatalogTN **catalog,
158 const bool is_nested = false,
159 CatalogTN *parent = NULL) {
160
2/2
✓ Branch 1 taken 49 times.
✓ Branch 2 taken 18069671 times.
18070322 assert(!catalog_hash.IsNull());
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18069671 times.
18069881 assert(catalog_hash.suffix == shash::kSuffixCatalog);
162
163 18069881 std::string path;
164
1/2
✓ Branch 1 taken 17992496 times.
✗ Branch 2 not taken.
18069930 const Failures retval = Fetch(catalog_hash, &path);
165
2/2
✓ Branch 0 taken 578 times.
✓ Branch 1 taken 17991918 times.
17992706 if (retval != kFailOk) {
166 683 return retval;
167 }
168
169
1/2
✓ Branch 1 taken 18025091 times.
✗ Branch 2 not taken.
17992023 *catalog = CatalogTN::AttachFreely(catalog_path, path, catalog_hash, parent,
170 is_nested);
171
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 18025087 times.
18025196 if (NULL == *catalog) {
172 4 return kFailLocalIO;
173 }
174
175
1/2
✓ Branch 1 taken 105 times.
✗ Branch 2 not taken.
18025192 (*catalog)->TakeDatabaseFileOwnership();
176 18023624 return kFailOk;
177 18024311 }
178
179 234 Failures FetchReflog(const shash::Any &reflog_hash, ReflogTN **reflog) {
180
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 117 times.
234 assert(!reflog_hash.IsNull());
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 117 times.
234 assert(reflog_hash.suffix == shash::kSuffixNone);
182
183 234 std::string tmp_path;
184 234 const bool decompress = false;
185 234 const bool nocache = true;
186
1/2
✓ Branch 1 taken 117 times.
✗ Branch 2 not taken.
234 Failures failure = Fetch(kReflogFilename, decompress, nocache, &tmp_path);
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 117 times.
234 if (failure != kFailOk) {
188 return failure;
189 }
190
191 // Ensure data integrity
192
1/2
✓ Branch 1 taken 117 times.
✗ Branch 2 not taken.
234 shash::Any computed_hash(reflog_hash.algorithm);
193
1/2
✓ Branch 1 taken 117 times.
✗ Branch 2 not taken.
234 ReflogTN::HashDatabase(tmp_path, &computed_hash);
194
2/4
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 78 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
234 if (computed_hash != reflog_hash) {
195 78 unlink(tmp_path.c_str());
196 78 return kFailBadData;
197 }
198
199
1/2
✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
156 *reflog = ReflogTN::Open(tmp_path);
200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
156 if (NULL == *reflog) {
201 return kFailLocalIO;
202 }
203
204
1/2
✓ Branch 1 taken 70 times.
✗ Branch 2 not taken.
156 (*reflog)->TakeDatabaseFileOwnership();
205 156 return kFailOk;
206 234 }
207
208 4912 Failures FetchManifest(UniquePtr<manifest::Manifest> *manifest) {
209 4912 manifest::Manifest *raw_manifest_ptr = NULL;
210
1/2
✓ Branch 1 taken 4702 times.
✗ Branch 2 not taken.
4912 Failures failure = FetchManifest(&raw_manifest_ptr);
211
1/2
✓ Branch 0 taken 4702 times.
✗ Branch 1 not taken.
4912 if (failure == kFailOk)
212
1/2
✓ Branch 1 taken 4702 times.
✗ Branch 2 not taken.
4912 *manifest = raw_manifest_ptr;
213 4912 return failure;
214 }
215
216 2274 Failures FetchHistory(UniquePtr<HistoryTN> *history,
217 const shash::Any &history_hash = shash::Any()) {
218 2274 HistoryTN *raw_history_ptr = NULL;
219
1/2
✓ Branch 1 taken 2099 times.
✗ Branch 2 not taken.
2274 Failures failure = FetchHistory(&raw_history_ptr, history_hash);
220
2/2
✓ Branch 0 taken 1961 times.
✓ Branch 1 taken 138 times.
2274 if (failure == kFailOk)
221
1/2
✓ Branch 1 taken 1961 times.
✗ Branch 2 not taken.
2066 *history = raw_history_ptr;
222 2274 return failure;
223 }
224
225 304 Failures FetchCatalog(const shash::Any &catalog_hash,
226 const std::string &catalog_path,
227 UniquePtr<CatalogTN> *catalog,
228 const bool is_nested = false,
229 CatalogTN *parent = NULL) {
230 304 CatalogTN *raw_catalog_ptr = NULL;
231
1/2
✓ Branch 1 taken 152 times.
✗ Branch 2 not taken.
304 Failures failure = FetchCatalog(catalog_hash, catalog_path,
232 &raw_catalog_ptr, is_nested, parent);
233
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 78 times.
304 if (failure == kFailOk)
234
1/2
✓ Branch 1 taken 74 times.
✗ Branch 2 not taken.
148 *catalog = raw_catalog_ptr;
235 304 return failure;
236 }
237
238 78 Failures FetchReflog(const shash::Any &reflog_hash,
239 UniquePtr<ReflogTN> *reflog) {
240 78 ReflogTN *raw_reflog_ptr = NULL;
241
1/2
✓ Branch 1 taken 39 times.
✗ Branch 2 not taken.
78 Failures failure = FetchReflog(reflog_hash, &raw_reflog_ptr);
242
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
78 if (failure == kFailOk)
243
1/2
✓ Branch 1 taken 39 times.
✗ Branch 2 not taken.
78 *reflog = raw_reflog_ptr;
244 78 return failure;
245 }
246
247 std::string GetUrl(const shash::Any &hash) const {
248 return static_cast<DerivedT *>(this)->GetUrl(hash);
249 }
250
251 78 bool HasHistory() {
252
1/2
✓ Branch 1 taken 39 times.
✗ Branch 2 not taken.
78 shash::Any history_hash = GetHistoryHash();
253 78 return !history_hash.IsNull();
254 }
255
256 1048 const std::string &temporary_directory() const {
257 1048 return temporary_directory_;
258 }
259
260 protected:
261 3788 explicit AbstractObjectFetcher(const std::string &temp_dir)
262 3788 : temporary_directory_(temp_dir) { }
263
264 /**
265 * Internal function used to download objects defined by the given content
266 * hash. This needs to be implemented depending on the concrete implementation
267 * of this base class.
268 *
269 * @param object_hash the content hash of the object to be downloaded
270 * @param file_path temporary file path to store the download result
271 * @return failure code (if not kFailOk, file_path is invalid)
272 */
273 18072842 Failures Fetch(const shash::Any &object_hash, std::string *file_path) {
274 18072842 return static_cast<DerivedT *>(this)->Fetch(object_hash, file_path);
275 }
276
277 234 Failures Fetch(const std::string &relative_path,
278 const bool decompress,
279 const bool nocache,
280 std::string *file_path) {
281 234 return static_cast<DerivedT *>(this)->Fetch(relative_path, decompress,
282 234 nocache, file_path);
283 }
284
285 /**
286 * Retrieves the history content hash of the HEAD history database from the
287 * repository's manifest
288 *
289 * @return the content hash of the HEAD history db or a null-hash on error
290 */
291 2200 shash::Any GetHistoryHash() {
292
1/2
✓ Branch 1 taken 2060 times.
✗ Branch 2 not taken.
2200 UniquePtr<manifest::Manifest> manifest;
293
1/2
✓ Branch 1 taken 2060 times.
✗ Branch 2 not taken.
2200 const Failures retval = FetchManifest(&manifest);
294
295
1/2
✓ Branch 1 taken 2060 times.
✗ Branch 2 not taken.
2200 if (retval != kFailOk || !manifest.IsValid()
296
3/6
✓ Branch 0 taken 2060 times.
✗ Branch 1 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 2060 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2060 times.
4400 || manifest->history().IsNull()) {
297 return shash::Any();
298 }
299
300 2200 return manifest->history();
301 2200 }
302
303 private:
304 const std::string temporary_directory_;
305 };
306
307 template<class DerivedT>
308 const std::string
309 AbstractObjectFetcher<DerivedT>::kManifestFilename = ".cvmfspublished";
310 template<class DerivedT>
311 const std::string
312 AbstractObjectFetcher<DerivedT>::kReflogFilename = ".cvmfsreflog";
313
314
315 /**
316 * This is an AbstractObjectFetcher<> accessing locally stored repository files.
317 * Note that this implementation does not take care of any repository signature
318 * verification.
319 */
320 template<class CatalogT = catalog::Catalog,
321 class HistoryT = history::SqliteHistory,
322 class ReflogT = manifest::Reflog>
323 class LocalObjectFetcher
324 : public AbstractObjectFetcher<
325 LocalObjectFetcher<CatalogT, HistoryT, ReflogT> > {
326 protected:
327 typedef LocalObjectFetcher<CatalogT, HistoryT, ReflogT> ThisTN;
328 typedef AbstractObjectFetcher<ThisTN> BaseTN;
329
330 public:
331 typedef typename BaseTN::Failures Failures;
332
333 public:
334 /**
335 * LocalObjectFetcher can reside on the stack or the heap.
336 *
337 * @param base_path the path to the repository's backend storage
338 * @param temp_dir location to store decompressed tmp data
339 */
340 120 LocalObjectFetcher(const std::string &base_path, const std::string &temp_dir)
341
1/2
✓ Branch 2 taken 120 times.
✗ Branch 3 not taken.
120 : BaseTN(temp_dir), base_path_(base_path) { }
342
343 using BaseTN::FetchManifest; // un-hiding convenience overload
344 84 Failures FetchManifest(manifest::Manifest **manifest) {
345
1/2
✓ Branch 1 taken 84 times.
✗ Branch 2 not taken.
84 const std::string path = BuildPath(BaseTN::kManifestFilename);
346
2/4
✓ Branch 1 taken 84 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 84 times.
84 if (!FileExists(path)) {
347 return BaseTN::kFailNotFound;
348 }
349
350
1/2
✓ Branch 1 taken 84 times.
✗ Branch 2 not taken.
84 *manifest = manifest::Manifest::LoadFile(path);
351
1/2
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
84 return (*manifest != NULL) ? BaseTN::kFailOk : BaseTN::kFailUnknown;
352 84 }
353
354 std::string GetUrl(const shash::Any &hash) const {
355 return "file://" + BuildPath(BuildRelativePath(hash));
356 }
357
358 156 Failures Fetch(const shash::Any &object_hash, std::string *file_path) {
359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
156 assert(file_path != NULL);
360 156 file_path->clear();
361
362
1/2
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
156 const std::string relative_path = BuildRelativePath(object_hash);
363 156 const bool decompress = true;
364 156 const bool nocache = false;
365
1/2
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
312 return Fetch(relative_path, decompress, nocache, file_path);
366 156 }
367
368
369 192 Failures Fetch(const std::string &relative_path,
370 const bool decompress,
371 const bool /* nocache */,
372 std::string *file_path) {
373
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
192 assert(file_path != NULL);
374 192 file_path->clear();
375
376 // check if the requested file object is available locally
377
1/2
✓ Branch 1 taken 192 times.
✗ Branch 2 not taken.
192 const std::string source = BuildPath(relative_path);
378
3/4
✓ Branch 1 taken 192 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 36 times.
✓ Branch 4 taken 156 times.
192 if (!FileExists(source)) {
379
1/2
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
36 LogCvmfs(kLogDownload, kLogDebug, "failed to locate file '%s'",
380 relative_path.c_str());
381 36 return BaseTN::kFailNotFound;
382 }
383
384 // create a temporary file to store the (decompressed) object file
385
3/6
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 156 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 156 times.
✗ Branch 9 not taken.
312 const std::string tmp_path = BaseTN::temporary_directory() + "/"
386 + GetFileName(relative_path);
387
1/2
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
156 FILE *f = CreateTempFile(tmp_path, 0600, "w", file_path);
388
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
156 if (NULL == f) {
389 LogCvmfs(kLogDownload, kLogStderr,
390 "failed to create temp file '%s' (errno: %d)", tmp_path.c_str(),
391 errno);
392 return BaseTN::kFailLocalIO;
393 }
394
395 // decompress or copy the requested object file
396
3/4
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 36 times.
✓ Branch 3 taken 120 times.
✗ Branch 4 not taken.
156 const bool success = (decompress) ? zlib::DecompressPath2File(source, f)
397
1/2
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
36 : CopyPath2File(source, f);
398
1/2
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
156 fclose(f);
399
400 // check the decompression success and remove the temporary file otherwise
401
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 132 times.
156 if (!success) {
402
1/2
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
24 LogCvmfs(kLogDownload, kLogDebug,
403 "failed to fetch file from '%s' "
404 "to '%s' (errno: %d)",
405 24 source.c_str(), file_path->c_str(), errno);
406 24 unlink(file_path->c_str());
407 24 file_path->clear();
408 24 return BaseTN::kFailDecompression;
409 }
410
411 132 return BaseTN::kFailOk;
412 192 }
413
414 protected:
415 276 std::string BuildPath(const std::string &relative_path) const {
416
1/2
✓ Branch 2 taken 276 times.
✗ Branch 3 not taken.
276 return base_path_ + "/" + relative_path;
417 }
418
419 156 std::string BuildRelativePath(const shash::Any &hash) const {
420
1/2
✓ Branch 2 taken 156 times.
✗ Branch 3 not taken.
156 return "data/" + hash.MakePath();
421 }
422
423 private:
424 const std::string base_path_;
425 };
426
427 template<class CatalogT, class HistoryT, class ReflogT>
428 struct object_fetcher_traits<LocalObjectFetcher<CatalogT, HistoryT, ReflogT> > {
429 typedef CatalogT CatalogTN;
430 typedef HistoryT HistoryTN;
431 typedef ReflogT ReflogTN;
432 };
433
434
435 /**
436 * This implements the AbstractObjectFetcher<> to retrieve repository objects
437 * from a remote location through HTTP. It verifies the repository's signature
438 * and the downloaded data integrity.
439 */
440 template<class CatalogT = catalog::Catalog,
441 class HistoryT = history::SqliteHistory,
442 class ReflogT = manifest::Reflog>
443 class HttpObjectFetcher : public AbstractObjectFetcher<
444 HttpObjectFetcher<CatalogT, HistoryT, ReflogT> > {
445 protected:
446 typedef HttpObjectFetcher<CatalogT, HistoryT, ReflogT> ThisTN;
447 typedef AbstractObjectFetcher<ThisTN> BaseTN;
448
449 public:
450 typedef typename BaseTN::Failures Failures;
451
452 public:
453 /**
454 * HttpObjectFetcher<> contains external DownloadManager and SignatureManager
455 * hence it essentially is a wrapper object and can be copied.
456 *
457 * @param repo_name the name of the repository to download objects from
458 * @param repo_url the URL to the repository's backend storage
459 * @param temp_dir location to store decompressed tmp data
460 * @param download_mgr pointer to the download manager to be used
461 * @param signature_mgr pointer to the signature manager to be used
462 *
463 * @return a HttpObjectFetcher<> object or NULL on error
464 */
465 230 HttpObjectFetcher(const std::string &repo_name,
466 const std::string &repo_url,
467 const std::string &temp_dir,
468 download::DownloadManager *download_mgr,
469 signature::SignatureManager *signature_mgr)
470 : BaseTN(temp_dir)
471
1/2
✓ Branch 1 taken 230 times.
✗ Branch 2 not taken.
230 , repo_url_(repo_url)
472
1/2
✓ Branch 1 taken 230 times.
✗ Branch 2 not taken.
230 , repo_name_(repo_name)
473 230 , download_manager_(download_mgr)
474 460 , signature_manager_(signature_mgr) { }
475
476 public:
477 using BaseTN::FetchManifest; // un-hiding convenience overload
478 161 Failures FetchManifest(manifest::Manifest **manifest) {
479
1/2
✓ Branch 1 taken 161 times.
✗ Branch 2 not taken.
161 const std::string url = BuildUrl(BaseTN::kManifestFilename);
480
481 // Download manifest file
482 161 struct manifest::ManifestEnsemble manifest_ensemble;
483 322 manifest::Failures retval = manifest::Fetch(repo_url_,
484
1/2
✓ Branch 1 taken 161 times.
✗ Branch 2 not taken.
161 repo_name_,
485 0,
486 NULL,
487 signature_manager_,
488 download_manager_,
489 &manifest_ensemble);
490
491 // Check if manifest was loaded correctly
492
1/4
✓ Branch 0 taken 161 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
161 switch (retval) {
493 161 case manifest::kFailOk:
494 161 break;
495
496 case manifest::kFailNameMismatch:
497 LogCvmfs(kLogDownload, kLogDebug,
498 "repository name mismatch. No name provided?");
499 return BaseTN::kFailManifestNameMismatch;
500
501 case manifest::kFailBadSignature:
502 case manifest::kFailBadCertificate:
503 case manifest::kFailBadWhitelist:
504 LogCvmfs(kLogDownload, kLogDebug,
505 "repository signature mismatch. No key(s) provided?");
506 return BaseTN::kFailManifestSignatureMismatch;
507
508 default:
509 LogCvmfs(kLogDownload, kLogDebug, "failed to load manifest (%d - %s)",
510 retval, Code2Ascii(retval));
511 return BaseTN::kFailUnknown;
512 }
513
514
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 161 times.
161 assert(retval == manifest::kFailOk);
515
2/4
✓ Branch 1 taken 161 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 161 times.
✗ Branch 5 not taken.
161 *manifest = new manifest::Manifest(*manifest_ensemble.manifest);
516
1/2
✓ Branch 0 taken 161 times.
✗ Branch 1 not taken.
161 return (*manifest != NULL) ? BaseTN::kFailOk : BaseTN::kFailUnknown;
517 161 }
518
519 std::string GetUrl(const shash::Any &hash) const {
520 return BuildUrl(BuildRelativeUrl(hash));
521 }
522
523 299 Failures Fetch(const shash::Any &object_hash, std::string *object_file) {
524
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 299 times.
299 assert(object_file != NULL);
525
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 299 times.
299 assert(!object_hash.IsNull());
526
527 299 const bool decompress = true;
528 299 const bool nocache = false;
529
1/2
✓ Branch 1 taken 299 times.
✗ Branch 2 not taken.
299 const std::string url = BuildRelativeUrl(object_hash);
530
1/2
✓ Branch 1 taken 299 times.
✗ Branch 2 not taken.
598 return Download(url, decompress, nocache, &object_hash, object_file);
531 299 }
532
533 69 Failures Fetch(const std::string &relative_path,
534 const bool decompress,
535 const bool nocache,
536 std::string *file_path) {
537 69 const shash::Any *expected_hash = NULL;
538 69 return Download(relative_path, decompress, nocache, expected_hash,
539 69 file_path);
540 }
541
542 protected:
543 529 std::string BuildUrl(const std::string &relative_path) const {
544
1/2
✓ Branch 2 taken 529 times.
✗ Branch 3 not taken.
529 return repo_url_ + "/" + relative_path;
545 }
546
547 299 std::string BuildRelativeUrl(const shash::Any &hash) const {
548
1/2
✓ Branch 2 taken 299 times.
✗ Branch 3 not taken.
299 return "data/" + hash.MakePath();
549 }
550
551 368 Failures Download(const std::string &relative_path,
552 const bool decompress,
553 const bool nocache,
554 const shash::Any *expected_hash,
555 std::string *file_path) {
556 368 file_path->clear();
557
558 // create temporary file to host the fetching result
559
3/6
✓ Branch 1 taken 368 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 368 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 368 times.
✗ Branch 9 not taken.
736 const std::string tmp_path = BaseTN::temporary_directory() + "/"
560 + GetFileName(relative_path);
561
1/2
✓ Branch 1 taken 368 times.
✗ Branch 2 not taken.
368 FILE *f = CreateTempFile(tmp_path, 0600, "w", file_path);
562
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 368 times.
368 if (NULL == f) {
563 LogCvmfs(kLogDownload, kLogStderr,
564 "failed to create temp file '%s' (errno: %d)", tmp_path.c_str(),
565 errno);
566 return BaseTN::kFailLocalIO;
567 }
568
569 // fetch and decompress the requested object
570
1/2
✓ Branch 1 taken 368 times.
✗ Branch 2 not taken.
368 const std::string url = BuildUrl(relative_path);
571 368 const bool probe_hosts = false;
572 368 cvmfs::FileSink filesink(f);
573
1/2
✓ Branch 1 taken 368 times.
✗ Branch 2 not taken.
368 download::JobInfo download_job(&url, decompress, probe_hosts, expected_hash,
574 &filesink);
575 368 download_job.SetForceNocache(nocache);
576
1/2
✓ Branch 1 taken 368 times.
✗ Branch 2 not taken.
368 download::Failures retval = download_manager_->Fetch(&download_job);
577 368 const bool success = (retval == download::kFailOk);
578
1/2
✓ Branch 1 taken 368 times.
✗ Branch 2 not taken.
368 fclose(f);
579
580 // check if download worked and remove temporary file if not
581
2/2
✓ Branch 0 taken 115 times.
✓ Branch 1 taken 253 times.
368 if (!success) {
582
1/2
✓ Branch 4 taken 115 times.
✗ Branch 5 not taken.
115 LogCvmfs(kLogDownload, kLogDebug,
583 "failed to download file "
584 "%s to '%s' (%d - %s)",
585 relative_path.c_str(), file_path->c_str(), retval,
586 Code2Ascii(retval));
587 115 unlink(file_path->c_str());
588 115 file_path->clear();
589
590 // hand out the error status
591
2/5
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 46 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 69 times.
115 switch (retval) {
592 case download::kFailLocalIO:
593 return BaseTN::kFailLocalIO;
594
595 case download::kFailBadUrl:
596 case download::kFailProxyResolve:
597 case download::kFailHostResolve:
598 case download::kFailUnsupportedProtocol:
599 LogCvmfs(kLogDownload, kLogDebug | kLogStderr,
600 "HTTP connection error %d: %s", retval, url.c_str());
601 return BaseTN::kFailNetwork;
602
603 46 case download::kFailProxyHttp:
604 case download::kFailHostHttp:
605
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 46 times.
46 if (download_job.http_code() == 404)
606 return BaseTN::kFailNotFound;
607
1/2
✓ Branch 3 taken 46 times.
✗ Branch 4 not taken.
46 LogCvmfs(kLogDownload, kLogDebug | kLogStderr,
608 "HTTP protocol error %d: %s (%d)", download_job.http_code(),
609 url.c_str(), retval);
610 46 return BaseTN::kFailNetwork;
611
612 case download::kFailBadData:
613 case download::kFailTooBig:
614 return BaseTN::kFailBadData;
615
616 69 default:
617 69 if (download::IsProxyTransferError(retval)
618
3/6
✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 69 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 69 times.
✗ Branch 6 not taken.
69 || download::IsHostTransferError(retval)) {
619
1/2
✓ Branch 3 taken 69 times.
✗ Branch 4 not taken.
69 LogCvmfs(kLogDownload, kLogDebug | kLogStderr,
620 "HTTP transfer error %d (HTTP code %d): %s", retval,
621 download_job.http_code(), url.c_str());
622 69 return BaseTN::kFailNetwork;
623 }
624 return BaseTN::kFailUnknown;
625 }
626 }
627
628
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 253 times.
253 assert(success);
629 253 return BaseTN::kFailOk;
630 368 }
631
632 private:
633 const std::string repo_url_;
634 const std::string repo_name_;
635 download::DownloadManager *download_manager_;
636 signature::SignatureManager *signature_manager_;
637 };
638
639 template<class CatalogT, class HistoryT, class ReflogT>
640 struct object_fetcher_traits<HttpObjectFetcher<CatalogT, HistoryT, ReflogT> > {
641 typedef CatalogT CatalogTN;
642 typedef HistoryT HistoryTN;
643 typedef ReflogT ReflogTN;
644 };
645
646 #endif // CVMFS_OBJECT_FETCHER_H_
647