CernVM-FS  2.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
swissknife_assistant.cc
Go to the documentation of this file.
1 
4 #include "cvmfs_config.h"
5 #include "swissknife_assistant.h"
6 
7 #include <unistd.h>
8 
9 #include <cassert>
10 #include <cstdlib>
11 
12 #include "catalog.h"
13 #include "catalog_rw.h"
14 #include "download.h"
15 #include "history.h"
16 #include "history_sqlite.h"
17 #include "logging.h"
18 #include "manifest.h"
19 #include "util/posix.h"
20 
21 using namespace std; // NOLINT
22 
23 namespace swissknife {
24 
25 catalog::Catalog *Assistant::GetCatalog(
26  const shash::Any &catalog_hash,
27  OpenMode open_mode)
28 {
29  assert(shash::kSuffixCatalog == catalog_hash.suffix);
30  string local_path = CreateTempPath(tmp_dir_ + "/catalog", 0600);
31  assert(!local_path.empty());
32 
33  if (!FetchObject(catalog_hash, local_path)) {
34  return NULL;
35  }
36 
37  const std::string catalog_root_path = "";
38  catalog::Catalog *catalog;
39  switch (open_mode) {
40  case kOpenReadWrite:
41  catalog = catalog::WritableCatalog::AttachFreely(catalog_root_path,
42  local_path,
43  catalog_hash);
44  break;
45  case kOpenReadOnly:
46  catalog = catalog::Catalog::AttachFreely(catalog_root_path,
47  local_path,
48  catalog_hash);
49  break;
50  default:
51  abort();
52  }
53  assert(catalog != NULL);
54  catalog->TakeDatabaseFileOwnership();
55  return catalog;
56 }
57 
58 
59 history::History *Assistant::GetHistory(OpenMode open_mode) {
60  const shash::Any history_hash = manifest_->history();
62 
63  string local_path = CreateTempPath(tmp_dir_ + "/history", 0600);
64  assert(!local_path.empty());
65 
66  if (history_hash.IsNull()) {
67  history = history::SqliteHistory::Create(local_path,
69  if (NULL == history) {
70  LogCvmfs(kLogCvmfs, kLogStderr, "failed to create history database");
71  return NULL;
72  }
73  return history;
74  }
75 
76  if (!FetchObject(history_hash, local_path))
77  return NULL;
78 
79  switch (open_mode) {
80  case kOpenReadWrite:
81  history = history::SqliteHistory::OpenWritable(local_path);
82  break;
83  case kOpenReadOnly:
84  history = history::SqliteHistory::Open(local_path);
85  break;
86  default:
87  abort();
88  }
89 
90  if (history == NULL) {
91  LogCvmfs(kLogCvmfs, kLogStderr, "failed to open history database (%s)",
92  local_path.c_str());
93  unlink(local_path.c_str());
94  return NULL;
95  }
96  assert(history->fqrn() == manifest_->repository_name());
97  history->TakeDatabaseFileOwnership();
98  return history;
99 }
100 
101 
102 bool Assistant::FetchObject(const shash::Any &id, const string &local_path) {
103  assert(!id.IsNull());
104 
105  download::Failures dl_retval;
106  const std::string url = repository_url_ + "/data/" + id.MakePath();
107  download::JobInfo download_info(&url, true, false, &local_path, &id);
108  dl_retval = download_mgr_->Fetch(&download_info);
109 
110  if (dl_retval != download::kFailOk) {
111  LogCvmfs(kLogCvmfs, kLogStderr, "failed to download object '%s' (%d - %s)",
112  id.ToStringWithSuffix().c_str(),
113  dl_retval, download::Code2Ascii(dl_retval));
114  return false;
115  }
116  return true;
117 }
118 
119 } // namespace swissknife
#define LogCvmfs(source, mask,...)
Definition: logging.h:20
bool IsNull() const
Definition: hash.h:379
download::DownloadManager * download_mgr_
Definition: repository.h:140
static SqliteHistory * Open(const std::string &file_name)
const history::History * history() const
std::string CreateTempPath(const std::string &path_prefix, const int mode)
Definition: posix.cc:1059
assert((mem||(size==0))&&"Out Of Memory")
const std::string & fqrn() const
Definition: history.h:220
static SqliteHistory * OpenWritable(const std::string &file_name)
static WritableCatalog * AttachFreely(const std::string &root_path, const std::string &file, const shash::Any &catalog_hash, Catalog *parent=NULL, const bool is_not_root=false)
Definition: catalog_rw.cc:49
const char * Code2Ascii(const Failures error)
Definition: download.h:87
std::string repository_name() const
Definition: manifest.h:122
const char kSuffixCatalog
Definition: hash.h:52
void TakeDatabaseFileOwnership()
Definition: catalog.cc:479
manifest::Manifest * manifest_
Definition: repository.h:147
Failures Fetch(JobInfo *info)
Definition: download.cc:1719
shash::Any history() const
Definition: manifest.h:127
virtual void TakeDatabaseFileOwnership()=0
static Catalog * AttachFreely(const std::string &imaginary_mountpoint, const std::string &file, const shash::Any &catalog_hash, Catalog *parent=NULL, const bool is_nested=false)
Definition: catalog.cc:29
static SqliteHistory * Create(const std::string &file_name, const std::string &fqrn)
Suffix suffix
Definition: hash.h:124