CernVM-FS  2.13.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
swissknife_assistant.cc
Go to the documentation of this file.
1 
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 "history.h"
15 #include "history_sqlite.h"
16 #include "manifest.h"
17 #include "network/download.h"
18 #include "util/logging.h"
19 #include "util/posix.h"
20 
21 using namespace std; // NOLINT
22 
23 namespace swissknife {
24 
25 catalog::Catalog *Assistant::GetCatalog(const shash::Any &catalog_hash,
26  OpenMode open_mode) {
27  assert(shash::kSuffixCatalog == catalog_hash.suffix);
28  const string local_path = CreateTempPath(tmp_dir_ + "/catalog", 0600);
29  assert(!local_path.empty());
30 
31  if (!FetchObject(catalog_hash, local_path)) {
32  return NULL;
33  }
34 
35  const std::string catalog_root_path = "";
36  catalog::Catalog *catalog;
37  switch (open_mode) {
38  case kOpenReadWrite:
40  catalog_root_path, local_path, catalog_hash);
41  break;
42  case kOpenReadOnly:
44  catalog_root_path, local_path, catalog_hash);
45  break;
46  default:
47  abort();
48  }
49  assert(catalog != NULL);
50  catalog->TakeDatabaseFileOwnership();
51  return catalog;
52 }
53 
54 
55 history::History *Assistant::GetHistory(OpenMode open_mode) {
56  const shash::Any history_hash = manifest_->history();
58 
59  const string local_path = CreateTempPath(tmp_dir_ + "/history", 0600);
60  assert(!local_path.empty());
61 
62  if (history_hash.IsNull()) {
63  history = history::SqliteHistory::Create(local_path,
65  if (NULL == history) {
66  LogCvmfs(kLogCvmfs, kLogStderr, "failed to create history database");
67  return NULL;
68  }
69  return history;
70  }
71 
72  if (!FetchObject(history_hash, local_path))
73  return NULL;
74 
75  switch (open_mode) {
76  case kOpenReadWrite:
77  history = history::SqliteHistory::OpenWritable(local_path);
78  break;
79  case kOpenReadOnly:
80  history = history::SqliteHistory::Open(local_path);
81  break;
82  default:
83  abort();
84  }
85 
86  if (history == NULL) {
87  LogCvmfs(kLogCvmfs, kLogStderr, "failed to open history database (%s)",
88  local_path.c_str());
89  unlink(local_path.c_str());
90  return NULL;
91  }
92  assert(history->fqrn() == manifest_->repository_name());
93  history->TakeDatabaseFileOwnership();
94  return history;
95 }
96 
97 
98 bool Assistant::FetchObject(const shash::Any &id, const string &local_path) {
99  assert(!id.IsNull());
100 
101  download::Failures dl_retval;
102  const std::string url = repository_url_ + "/data/" + id.MakePath();
103 
104  cvmfs::PathSink pathsink(local_path);
105  download::JobInfo download_info(&url, true, false, &id, &pathsink);
106  dl_retval = download_mgr_->Fetch(&download_info);
107 
108  if (dl_retval != download::kFailOk) {
109  LogCvmfs(kLogCvmfs, kLogStderr, "failed to download object '%s' (%d - %s)",
110  id.ToStringWithSuffix().c_str(), dl_retval,
111  download::Code2Ascii(dl_retval));
112  return false;
113  }
114  return true;
115 }
116 
117 } // namespace swissknife
bool IsNull() const
Definition: hash.h:371
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:1042
assert((mem||(size==0))&&"Out Of Memory")
const std::string & fqrn() const
Definition: history.h:197
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)
std::string repository_name() const
Definition: manifest.h:123
const char kSuffixCatalog
Definition: hash.h:54
void TakeDatabaseFileOwnership()
Definition: catalog.cc:464
manifest::Manifest * manifest_
Definition: repository.h:147
Failures Fetch(JobInfo *info)
Definition: download.cc:1984
shash::Any history() const
Definition: manifest.h:128
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:123
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)
Definition: logging.cc:545