| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/object_fetcher.h |
| Date: | 2025-11-09 02:35:23 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 206 | 235 | 87.7% |
| Branches: | 121 | 237 | 51.1% |
| 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 | 37 | inline const char *Code2Ascii(const ObjectFetcherFailures::Failures error) { | |
| 51 | const char *texts[ObjectFetcherFailures::kFailNumEntries + 1]; | ||
| 52 | 37 | texts[0] = "OK"; | |
| 53 | 37 | texts[1] = "object not found"; | |
| 54 | 37 | texts[2] = "local I/O failure"; | |
| 55 | 37 | texts[3] = "network failure"; | |
| 56 | 37 | texts[4] = "decompression failed"; | |
| 57 | 37 | texts[5] = "manifest name doesn't match"; | |
| 58 | 37 | texts[6] = "manifest signature is invalid"; | |
| 59 | 37 | texts[7] = "bad data received"; | |
| 60 | 37 | texts[8] = "no text"; | |
| 61 | 37 | 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 | 3659 | Failures FetchManifest(manifest::Manifest **manifest) { | |
| 101 | 3659 | 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 | 2250 | 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 352 times.
✓ Branch 2 taken 1422 times.
|
2250 | shash::Any effective_history_hash = (!history_hash.IsNull()) |
| 119 | ? history_hash | ||
| 120 |
1/2✓ Branch 1 taken 1422 times.
✗ Branch 2 not taken.
|
1626 | : GetHistoryHash(); |
| 121 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1774 times.
|
2250 | if (effective_history_hash.IsNull()) { |
| 122 | ✗ | return kFailNotFound; | |
| 123 | } | ||
| 124 |
3/4✓ Branch 0 taken 1422 times.
✓ Branch 1 taken 352 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1422 times.
|
2250 | assert(history_hash.suffix == shash::kSuffixHistory |
| 125 | || history_hash.IsNull()); | ||
| 126 | |||
| 127 | // download the history hash | ||
| 128 | 2250 | std::string path; | |
| 129 |
1/2✓ Branch 1 taken 1774 times.
✗ Branch 2 not taken.
|
2250 | const Failures retval = Fetch(effective_history_hash, &path); |
| 130 |
2/2✓ Branch 0 taken 193 times.
✓ Branch 1 taken 1581 times.
|
2250 | if (retval != kFailOk) { |
| 131 | 329 | return retval; | |
| 132 | } | ||
| 133 | |||
| 134 | // open the history file | ||
| 135 |
1/2✓ Branch 1 taken 1581 times.
✗ Branch 2 not taken.
|
1921 | *history = HistoryTN::Open(path); |
| 136 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1561 times.
|
1921 | if (NULL == *history) { |
| 137 | 20 | return kFailLocalIO; | |
| 138 | } | ||
| 139 | |||
| 140 |
1/2✓ Branch 1 taken 1561 times.
✗ Branch 2 not taken.
|
1901 | (*history)->TakeDatabaseFileOwnership(); |
| 141 | 1901 | return kFailOk; | |
| 142 | 2250 | } | |
| 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 | 5565636 | 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 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 5565093 times.
|
5565636 | assert(!catalog_hash.IsNull()); |
| 161 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5565093 times.
|
5565501 | assert(catalog_hash.suffix == shash::kSuffixCatalog); |
| 162 | |||
| 163 | 5565501 | std::string path; | |
| 164 |
1/2✓ Branch 1 taken 5539983 times.
✗ Branch 2 not taken.
|
5565666 | const Failures retval = Fetch(catalog_hash, &path); |
| 165 |
2/2✓ Branch 0 taken 540 times.
✓ Branch 1 taken 5539443 times.
|
5540391 | if (retval != kFailOk) { |
| 166 | 744 | return retval; | |
| 167 | } | ||
| 168 | |||
| 169 |
1/2✓ Branch 1 taken 5550843 times.
✗ Branch 2 not taken.
|
5539647 | *catalog = CatalogTN::AttachFreely(catalog_path, path, catalog_hash, parent, |
| 170 | is_nested); | ||
| 171 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 5550823 times.
|
5551047 | if (NULL == *catalog) { |
| 172 | 20 | return kFailLocalIO; | |
| 173 | } | ||
| 174 | |||
| 175 |
1/2✓ Branch 1 taken 204 times.
✗ Branch 2 not taken.
|
5551027 | (*catalog)->TakeDatabaseFileOwnership(); |
| 176 | 5550217 | return kFailOk; | |
| 177 | 5550981 | } | |
| 178 | |||
| 179 | 528 | Failures FetchReflog(const shash::Any &reflog_hash, ReflogTN **reflog) { | |
| 180 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 264 times.
|
528 | assert(!reflog_hash.IsNull()); |
| 181 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 264 times.
|
528 | assert(reflog_hash.suffix == shash::kSuffixNone); |
| 182 | |||
| 183 | 528 | std::string tmp_path; | |
| 184 | 528 | const bool decompress = false; | |
| 185 | 528 | const bool nocache = true; | |
| 186 |
1/2✓ Branch 1 taken 264 times.
✗ Branch 2 not taken.
|
528 | Failures failure = Fetch(kReflogFilename, decompress, nocache, &tmp_path); |
| 187 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 264 times.
|
528 | if (failure != kFailOk) { |
| 188 | ✗ | return failure; | |
| 189 | } | ||
| 190 | |||
| 191 | // Ensure data integrity | ||
| 192 |
1/2✓ Branch 1 taken 264 times.
✗ Branch 2 not taken.
|
528 | shash::Any computed_hash(reflog_hash.algorithm); |
| 193 |
1/2✓ Branch 1 taken 264 times.
✗ Branch 2 not taken.
|
528 | ReflogTN::HashDatabase(tmp_path, &computed_hash); |
| 194 |
2/4✓ Branch 1 taken 88 times.
✓ Branch 2 taken 176 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
528 | if (computed_hash != reflog_hash) { |
| 195 | 176 | unlink(tmp_path.c_str()); | |
| 196 | 176 | return kFailBadData; | |
| 197 | } | ||
| 198 | |||
| 199 |
1/2✓ Branch 1 taken 176 times.
✗ Branch 2 not taken.
|
352 | *reflog = ReflogTN::Open(tmp_path); |
| 200 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 176 times.
|
352 | if (NULL == *reflog) { |
| 201 | ✗ | return kFailLocalIO; | |
| 202 | } | ||
| 203 | |||
| 204 |
1/2✓ Branch 1 taken 136 times.
✗ Branch 2 not taken.
|
352 | (*reflog)->TakeDatabaseFileOwnership(); |
| 205 | 352 | return kFailOk; | |
| 206 | 528 | } | |
| 207 | |||
| 208 | 3659 | Failures FetchManifest(UniquePtr<manifest::Manifest> *manifest) { | |
| 209 | 3659 | manifest::Manifest *raw_manifest_ptr = NULL; | |
| 210 |
1/2✓ Branch 1 taken 3251 times.
✗ Branch 2 not taken.
|
3659 | Failures failure = FetchManifest(&raw_manifest_ptr); |
| 211 |
1/2✓ Branch 0 taken 3251 times.
✗ Branch 1 not taken.
|
3659 | if (failure == kFailOk) |
| 212 |
1/2✓ Branch 1 taken 3251 times.
✗ Branch 2 not taken.
|
3659 | *manifest = raw_manifest_ptr; |
| 213 | 3659 | return failure; | |
| 214 | } | ||
| 215 | |||
| 216 | 1938 | Failures FetchHistory(UniquePtr<HistoryTN> *history, | |
| 217 | const shash::Any &history_hash = shash::Any()) { | ||
| 218 | 1938 | HistoryTN *raw_history_ptr = NULL; | |
| 219 |
1/2✓ Branch 1 taken 1598 times.
✗ Branch 2 not taken.
|
1938 | Failures failure = FetchHistory(&raw_history_ptr, history_hash); |
| 220 |
2/2✓ Branch 0 taken 1385 times.
✓ Branch 1 taken 213 times.
|
1938 | if (failure == kFailOk) |
| 221 |
1/2✓ Branch 1 taken 1385 times.
✗ Branch 2 not taken.
|
1589 | *history = raw_history_ptr; |
| 222 | 1938 | return failure; | |
| 223 | } | ||
| 224 | |||
| 225 | 664 | 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 | 664 | CatalogTN *raw_catalog_ptr = NULL; | |
| 231 |
1/2✓ Branch 1 taken 332 times.
✗ Branch 2 not taken.
|
664 | Failures failure = FetchCatalog(catalog_hash, catalog_path, |
| 232 | &raw_catalog_ptr, is_nested, parent); | ||
| 233 |
2/2✓ Branch 0 taken 156 times.
✓ Branch 1 taken 176 times.
|
664 | if (failure == kFailOk) |
| 234 |
1/2✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
|
312 | *catalog = raw_catalog_ptr; |
| 235 | 664 | return failure; | |
| 236 | } | ||
| 237 | |||
| 238 | 176 | Failures FetchReflog(const shash::Any &reflog_hash, | |
| 239 | UniquePtr<ReflogTN> *reflog) { | ||
| 240 | 176 | ReflogTN *raw_reflog_ptr = NULL; | |
| 241 |
1/2✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
|
176 | Failures failure = FetchReflog(reflog_hash, &raw_reflog_ptr); |
| 242 |
1/2✓ Branch 0 taken 88 times.
✗ Branch 1 not taken.
|
176 | if (failure == kFailOk) |
| 243 |
1/2✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
|
176 | *reflog = raw_reflog_ptr; |
| 244 | 176 | return failure; | |
| 245 | } | ||
| 246 | |||
| 247 | std::string GetUrl(const shash::Any &hash) const { | ||
| 248 | return static_cast<DerivedT *>(this)->GetUrl(hash); | ||
| 249 | } | ||
| 250 | |||
| 251 | 176 | bool HasHistory() { | |
| 252 |
1/2✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
|
176 | shash::Any history_hash = GetHistoryHash(); |
| 253 | 176 | return !history_hash.IsNull(); | |
| 254 | } | ||
| 255 | |||
| 256 | 2044 | const std::string &temporary_directory() const { | |
| 257 | 2044 | return temporary_directory_; | |
| 258 | } | ||
| 259 | |||
| 260 | protected: | ||
| 261 | 3423 | explicit AbstractObjectFetcher(const std::string &temp_dir) | |
| 262 | 3423 | : 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 | 5568021 | Failures Fetch(const shash::Any &object_hash, std::string *file_path) { | |
| 274 | 5568021 | return static_cast<DerivedT *>(this)->Fetch(object_hash, file_path); | |
| 275 | } | ||
| 276 | |||
| 277 | 528 | Failures Fetch(const std::string &relative_path, | |
| 278 | const bool decompress, | ||
| 279 | const bool nocache, | ||
| 280 | std::string *file_path) { | ||
| 281 | 528 | return static_cast<DerivedT *>(this)->Fetch(relative_path, decompress, | |
| 282 | 528 | 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 | 1782 | shash::Any GetHistoryHash() { | |
| 292 |
1/2✓ Branch 1 taken 1510 times.
✗ Branch 2 not taken.
|
1782 | UniquePtr<manifest::Manifest> manifest; |
| 293 |
1/2✓ Branch 1 taken 1510 times.
✗ Branch 2 not taken.
|
1782 | const Failures retval = FetchManifest(&manifest); |
| 294 | |||
| 295 |
1/2✓ Branch 1 taken 1510 times.
✗ Branch 2 not taken.
|
1782 | if (retval != kFailOk || !manifest.IsValid() |
| 296 |
3/6✓ Branch 0 taken 1510 times.
✗ Branch 1 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 1510 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1510 times.
|
3564 | || manifest->history().IsNull()) { |
| 297 | ✗ | return shash::Any(); | |
| 298 | } | ||
| 299 | |||
| 300 | 1782 | return manifest->history(); | |
| 301 | 1782 | } | |
| 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 | 220 | LocalObjectFetcher(const std::string &base_path, const std::string &temp_dir) | |
| 341 |
1/2✓ Branch 2 taken 220 times.
✗ Branch 3 not taken.
|
220 | : BaseTN(temp_dir), base_path_(base_path) { } |
| 342 | |||
| 343 | using BaseTN::FetchManifest; // un-hiding convenience overload | ||
| 344 | 154 | Failures FetchManifest(manifest::Manifest **manifest) { | |
| 345 |
1/2✓ Branch 1 taken 154 times.
✗ Branch 2 not taken.
|
154 | const std::string path = BuildPath(BaseTN::kManifestFilename); |
| 346 |
2/4✓ Branch 1 taken 154 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 154 times.
|
154 | if (!FileExists(path)) { |
| 347 | ✗ | return BaseTN::kFailNotFound; | |
| 348 | } | ||
| 349 | |||
| 350 |
1/2✓ Branch 1 taken 154 times.
✗ Branch 2 not taken.
|
154 | *manifest = manifest::Manifest::LoadFile(path); |
| 351 |
1/2✓ Branch 0 taken 154 times.
✗ Branch 1 not taken.
|
154 | return (*manifest != NULL) ? BaseTN::kFailOk : BaseTN::kFailUnknown; |
| 352 | 154 | } | |
| 353 | |||
| 354 | std::string GetUrl(const shash::Any &hash) const { | ||
| 355 | return "file://" + BuildPath(BuildRelativePath(hash)); | ||
| 356 | } | ||
| 357 | |||
| 358 | 286 | Failures Fetch(const shash::Any &object_hash, std::string *file_path) { | |
| 359 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 286 times.
|
286 | assert(file_path != NULL); |
| 360 | 286 | file_path->clear(); | |
| 361 | |||
| 362 |
1/2✓ Branch 1 taken 286 times.
✗ Branch 2 not taken.
|
286 | const std::string relative_path = BuildRelativePath(object_hash); |
| 363 | 286 | const bool decompress = true; | |
| 364 | 286 | const bool nocache = false; | |
| 365 |
1/2✓ Branch 1 taken 286 times.
✗ Branch 2 not taken.
|
572 | return Fetch(relative_path, decompress, nocache, file_path); |
| 366 | 286 | } | |
| 367 | |||
| 368 | |||
| 369 | 352 | 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 352 times.
|
352 | assert(file_path != NULL); |
| 374 | 352 | file_path->clear(); | |
| 375 | |||
| 376 | // check if the requested file object is available locally | ||
| 377 |
1/2✓ Branch 1 taken 352 times.
✗ Branch 2 not taken.
|
352 | const std::string source = BuildPath(relative_path); |
| 378 |
3/4✓ Branch 1 taken 352 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 66 times.
✓ Branch 4 taken 286 times.
|
352 | if (!FileExists(source)) { |
| 379 |
1/2✓ Branch 2 taken 66 times.
✗ Branch 3 not taken.
|
66 | LogCvmfs(kLogDownload, kLogDebug, "failed to locate file '%s'", |
| 380 | relative_path.c_str()); | ||
| 381 | 66 | return BaseTN::kFailNotFound; | |
| 382 | } | ||
| 383 | |||
| 384 | // create a temporary file to store the (decompressed) object file | ||
| 385 |
3/6✓ Branch 1 taken 286 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 286 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 286 times.
✗ Branch 9 not taken.
|
572 | const std::string tmp_path = BaseTN::temporary_directory() + "/" |
| 386 | + GetFileName(relative_path); | ||
| 387 |
1/2✓ Branch 1 taken 286 times.
✗ Branch 2 not taken.
|
286 | FILE *f = CreateTempFile(tmp_path, 0600, "w", file_path); |
| 388 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 286 times.
|
286 | 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 220 times.
✓ Branch 1 taken 66 times.
✓ Branch 3 taken 220 times.
✗ Branch 4 not taken.
|
286 | const bool success = (decompress) ? zlib::DecompressPath2File(source, f) |
| 397 |
1/2✓ Branch 1 taken 66 times.
✗ Branch 2 not taken.
|
66 | : CopyPath2File(source, f); |
| 398 |
1/2✓ Branch 1 taken 286 times.
✗ Branch 2 not taken.
|
286 | fclose(f); |
| 399 | |||
| 400 | // check the decompression success and remove the temporary file otherwise | ||
| 401 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 242 times.
|
286 | if (!success) { |
| 402 |
1/2✓ Branch 2 taken 44 times.
✗ Branch 3 not taken.
|
44 | LogCvmfs(kLogDownload, kLogDebug, |
| 403 | "failed to fetch file from '%s' " | ||
| 404 | "to '%s' (errno: %d)", | ||
| 405 | 44 | source.c_str(), file_path->c_str(), errno); | |
| 406 | 44 | unlink(file_path->c_str()); | |
| 407 | 44 | file_path->clear(); | |
| 408 | 44 | return BaseTN::kFailDecompression; | |
| 409 | } | ||
| 410 | |||
| 411 | 242 | return BaseTN::kFailOk; | |
| 412 | 352 | } | |
| 413 | |||
| 414 | protected: | ||
| 415 | 506 | std::string BuildPath(const std::string &relative_path) const { | |
| 416 |
1/2✓ Branch 2 taken 506 times.
✗ Branch 3 not taken.
|
506 | return base_path_ + "/" + relative_path; |
| 417 | } | ||
| 418 | |||
| 419 | 286 | std::string BuildRelativePath(const shash::Any &hash) const { | |
| 420 |
1/2✓ Branch 2 taken 286 times.
✗ Branch 3 not taken.
|
286 | 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 | 460 | 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 460 times.
✗ Branch 2 not taken.
|
460 | , repo_url_(repo_url) |
| 472 |
1/2✓ Branch 1 taken 460 times.
✗ Branch 2 not taken.
|
460 | , repo_name_(repo_name) |
| 473 | 460 | , download_manager_(download_mgr) | |
| 474 | 920 | , signature_manager_(signature_mgr) { } | |
| 475 | |||
| 476 | public: | ||
| 477 | using BaseTN::FetchManifest; // un-hiding convenience overload | ||
| 478 | 322 | Failures FetchManifest(manifest::Manifest **manifest) { | |
| 479 |
1/2✓ Branch 1 taken 322 times.
✗ Branch 2 not taken.
|
322 | const std::string url = BuildUrl(BaseTN::kManifestFilename); |
| 480 | |||
| 481 | // Download manifest file | ||
| 482 | 322 | struct manifest::ManifestEnsemble manifest_ensemble; | |
| 483 | 644 | manifest::Failures retval = manifest::Fetch(repo_url_, | |
| 484 |
1/2✓ Branch 1 taken 322 times.
✗ Branch 2 not taken.
|
322 | 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 322 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
322 | switch (retval) { |
| 493 | 322 | case manifest::kFailOk: | |
| 494 | 322 | 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 322 times.
|
322 | assert(retval == manifest::kFailOk); |
| 515 |
2/4✓ Branch 1 taken 322 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 322 times.
✗ Branch 5 not taken.
|
322 | *manifest = new manifest::Manifest(*manifest_ensemble.manifest); |
| 516 |
1/2✓ Branch 0 taken 322 times.
✗ Branch 1 not taken.
|
322 | return (*manifest != NULL) ? BaseTN::kFailOk : BaseTN::kFailUnknown; |
| 517 | 322 | } | |
| 518 | |||
| 519 | std::string GetUrl(const shash::Any &hash) const { | ||
| 520 | return BuildUrl(BuildRelativeUrl(hash)); | ||
| 521 | } | ||
| 522 | |||
| 523 | 598 | Failures Fetch(const shash::Any &object_hash, std::string *object_file) { | |
| 524 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 598 times.
|
598 | assert(object_file != NULL); |
| 525 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 598 times.
|
598 | assert(!object_hash.IsNull()); |
| 526 | |||
| 527 | 598 | const bool decompress = true; | |
| 528 | 598 | const bool nocache = false; | |
| 529 |
1/2✓ Branch 1 taken 598 times.
✗ Branch 2 not taken.
|
598 | const std::string url = BuildRelativeUrl(object_hash); |
| 530 |
1/2✓ Branch 1 taken 598 times.
✗ Branch 2 not taken.
|
1196 | return Download(url, decompress, nocache, &object_hash, object_file); |
| 531 | 598 | } | |
| 532 | |||
| 533 | 138 | Failures Fetch(const std::string &relative_path, | |
| 534 | const bool decompress, | ||
| 535 | const bool nocache, | ||
| 536 | std::string *file_path) { | ||
| 537 | 138 | const shash::Any *expected_hash = NULL; | |
| 538 | 138 | return Download(relative_path, decompress, nocache, expected_hash, | |
| 539 | 138 | file_path); | |
| 540 | } | ||
| 541 | |||
| 542 | protected: | ||
| 543 | 1058 | std::string BuildUrl(const std::string &relative_path) const { | |
| 544 |
1/2✓ Branch 2 taken 1058 times.
✗ Branch 3 not taken.
|
1058 | return repo_url_ + "/" + relative_path; |
| 545 | } | ||
| 546 | |||
| 547 | 598 | std::string BuildRelativeUrl(const shash::Any &hash) const { | |
| 548 |
1/2✓ Branch 2 taken 598 times.
✗ Branch 3 not taken.
|
598 | return "data/" + hash.MakePath(); |
| 549 | } | ||
| 550 | |||
| 551 | 736 | 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 | 736 | file_path->clear(); | |
| 557 | |||
| 558 | // create temporary file to host the fetching result | ||
| 559 |
3/6✓ Branch 1 taken 736 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 736 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 736 times.
✗ Branch 9 not taken.
|
1472 | const std::string tmp_path = BaseTN::temporary_directory() + "/" |
| 560 | + GetFileName(relative_path); | ||
| 561 |
1/2✓ Branch 1 taken 736 times.
✗ Branch 2 not taken.
|
736 | FILE *f = CreateTempFile(tmp_path, 0600, "w", file_path); |
| 562 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 736 times.
|
736 | 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 736 times.
✗ Branch 2 not taken.
|
736 | const std::string url = BuildUrl(relative_path); |
| 571 | 736 | const bool probe_hosts = false; | |
| 572 | 736 | cvmfs::FileSink filesink(f); | |
| 573 |
1/2✓ Branch 1 taken 736 times.
✗ Branch 2 not taken.
|
736 | download::JobInfo download_job(&url, decompress, probe_hosts, expected_hash, |
| 574 | &filesink); | ||
| 575 | 736 | download_job.SetForceNocache(nocache); | |
| 576 |
1/2✓ Branch 1 taken 736 times.
✗ Branch 2 not taken.
|
736 | download::Failures retval = download_manager_->Fetch(&download_job); |
| 577 | 736 | const bool success = (retval == download::kFailOk); | |
| 578 |
1/2✓ Branch 1 taken 736 times.
✗ Branch 2 not taken.
|
736 | fclose(f); |
| 579 | |||
| 580 | // check if download worked and remove temporary file if not | ||
| 581 |
2/2✓ Branch 0 taken 230 times.
✓ Branch 1 taken 506 times.
|
736 | if (!success) { |
| 582 |
1/2✓ Branch 4 taken 230 times.
✗ Branch 5 not taken.
|
230 | 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 | 230 | unlink(file_path->c_str()); | |
| 588 | 230 | file_path->clear(); | |
| 589 | |||
| 590 | // hand out the error status | ||
| 591 |
2/5✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 92 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 138 times.
|
230 | 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 | 92 | case download::kFailProxyHttp: | |
| 604 | case download::kFailHostHttp: | ||
| 605 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 92 times.
|
92 | if (download_job.http_code() == 404) |
| 606 | ✗ | return BaseTN::kFailNotFound; | |
| 607 |
1/2✓ Branch 3 taken 92 times.
✗ Branch 4 not taken.
|
92 | LogCvmfs(kLogDownload, kLogDebug | kLogStderr, |
| 608 | "HTTP protocol error %d: %s (%d)", download_job.http_code(), | ||
| 609 | url.c_str(), retval); | ||
| 610 | 92 | return BaseTN::kFailNetwork; | |
| 611 | |||
| 612 | ✗ | case download::kFailBadData: | |
| 613 | case download::kFailTooBig: | ||
| 614 | ✗ | return BaseTN::kFailBadData; | |
| 615 | |||
| 616 | 138 | default: | |
| 617 | 138 | if (download::IsProxyTransferError(retval) | |
| 618 |
3/6✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 138 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 138 times.
✗ Branch 6 not taken.
|
138 | || download::IsHostTransferError(retval)) { |
| 619 |
1/2✓ Branch 3 taken 138 times.
✗ Branch 4 not taken.
|
138 | LogCvmfs(kLogDownload, kLogDebug | kLogStderr, |
| 620 | "HTTP transfer error %d (HTTP code %d): %s", retval, | ||
| 621 | download_job.http_code(), url.c_str()); | ||
| 622 | 138 | return BaseTN::kFailNetwork; | |
| 623 | } | ||
| 624 | ✗ | return BaseTN::kFailUnknown; | |
| 625 | } | ||
| 626 | } | ||
| 627 | |||
| 628 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 506 times.
|
506 | assert(success); |
| 629 | 506 | return BaseTN::kFailOk; | |
| 630 | 736 | } | |
| 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 |