CernVM-FS  2.13.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
catalog_mgr_ro.cc
Go to the documentation of this file.
1 
6 #include "catalog_mgr_ro.h"
7 
9 #include "network/download.h"
10 #include "util/exception.h"
11 #include "util/posix.h"
12 
13 using namespace std; // NOLINT
14 
15 namespace catalog {
16 
17 SimpleCatalogManager::SimpleCatalogManager(
18  const shash::Any &base_hash,
19  const std::string &stratum0,
20  const std::string &dir_temp,
21  download::DownloadManager *download_manager,
22  perf::Statistics *statistics,
23  const bool manage_catalog_files,
24  const std::string &dir_cache,
25  const bool copy_to_tmp_dir)
26  : AbstractCatalogManager<Catalog>(statistics)
27  , dir_cache_(dir_cache)
28  , copy_to_tmp_dir_(copy_to_tmp_dir)
29  , base_hash_(base_hash)
30  , stratum0_(stratum0)
31  , dir_temp_(dir_temp)
32  , download_manager_(download_manager)
33  , manage_catalog_files_(manage_catalog_files) {
34  if (!dir_cache.empty()) {
35  const bool success = MakeCacheDirectories(dir_cache_, 0755);
36 
37  if (!success) {
39  "Failure during creation of local cache directory for server. "
40  "Local cache directory: %s",
41  dir_cache_.c_str());
42  }
43  } else {
44  copy_to_tmp_dir_ = false;
45  }
46 }
47 
49  CatalogContext *result) {
50  if (result->hash().IsNull()) {
51  result->SetHash(base_hash_);
52  }
54  result->SetMountpoint(PathString("", 0));
55 
56  return kLoadNew;
57 }
58 
60  const std::string &cache_path) {
61  std::string tmp_path;
62  FILE *fcatalog = CreateTempFile(dir_temp_ + "/catalog", 0666, "w", &tmp_path);
63  if (!fcatalog) {
64  PANIC(kLogStderr, "failed to create temp file when loading %s",
65  cache_path.c_str());
66  }
67 
68  const bool retval = CopyPath2File(cache_path, fcatalog);
69  if (!retval) {
70  unlink(tmp_path.c_str());
71  PANIC(kLogStderr, "failed to read %s", cache_path.c_str());
72  }
73  (void)fclose(fcatalog);
74 
75  return tmp_path;
76 }
77 
94  CatalogContext *ctlg_context) {
95  const shash::Any effective_hash = ctlg_context->hash();
96  assert(shash::kSuffixCatalog == effective_hash.suffix);
97  const string url = stratum0_ + "/data/" + effective_hash.MakePath();
98 
99  FILE *fcatalog;
100 
101  if (UseLocalCache()) {
102  std::string cache_path = dir_cache_ + "/"
103  + effective_hash.MakePathWithoutSuffix();
104 
105  ctlg_context->SetSqlitePath(cache_path);
106 
107  // catalog is cached in "cache_dir/" + standard cvmfs file hierarchy
108  if (FileExists(cache_path.c_str())) {
109  if (!copy_to_tmp_dir_) {
110  return kLoadNew;
111  } else { // for writable catalog create copy in dir_temp_
112  std::string tmp_path;
113 
114  tmp_path = CopyCatalogToTempFile(cache_path);
115  ctlg_context->SetSqlitePath(tmp_path);
116 
117  return kLoadNew;
118  }
119  }
120  }
121 
122  // not in local cache; just create a random tmp file for download
123  std::string tmp_path;
124  fcatalog = CreateTempFile(dir_temp_ + "/catalog", 0666, "w", &tmp_path);
125  if (!fcatalog) {
126  PANIC(kLogStderr, "failed to create temp file when loading %s",
127  url.c_str());
128  }
129  ctlg_context->SetSqlitePath(tmp_path);
130 
131  cvmfs::FileSink filesink(fcatalog);
132  download::JobInfo download_catalog(&url, true, false, &effective_hash,
133  &filesink);
134  const download::Failures retval = download_manager_->Fetch(&download_catalog);
135  fclose(fcatalog);
136 
137  if (retval != download::kFailOk) {
138  unlink(tmp_path.c_str());
139  PANIC(kLogStderr, "failed to load %s from Stratum 0 (%d - %s)", url.c_str(),
140  retval, download::Code2Ascii(retval));
141  }
142 
143  // for local cache make an atomic rename call to make the file available
144  // in the local cache
145  if (UseLocalCache()) {
146  const std::string cache_path = dir_cache_ + "/"
147  + effective_hash.MakePathWithoutSuffix();
148  rename(tmp_path.c_str(), cache_path.c_str());
149  ctlg_context->SetSqlitePath(cache_path);
150 
151  // for writable catalog make an extra copy that can be modified
152  if (copy_to_tmp_dir_) {
153  const std::string new_tmp_path = CopyCatalogToTempFile(cache_path);
154  ctlg_context->SetSqlitePath(new_tmp_path);
155  }
156  }
157 
158  return kLoadNew;
159 }
160 
161 
163  const shash::Any &catalog_hash,
164  Catalog *parent_catalog) {
165  Catalog *new_catalog = new Catalog(mountpoint, catalog_hash, parent_catalog);
166  if (manage_catalog_files_) {
167  new_catalog->TakeDatabaseFileOwnership();
168  }
169 
170  return new_catalog;
171 }
172 
173 } // namespace catalog
bool MakeCacheDirectories(const std::string &path, const mode_t mode)
Definition: posix.cc:890
shash::Any hash() const
Definition: catalog_mgr.h:124
bool IsNull() const
Definition: hash.h:371
download::DownloadManager * download_manager_
#define PANIC(...)
Definition: exception.h:29
FILE * CreateTempFile(const std::string &path_prefix, const int mode, const char *open_flags, std::string *final_path)
Definition: posix.cc:1013
virtual LoadReturn LoadCatalogByHash(CatalogContext *ctlg_context)
assert((mem||(size==0))&&"Out Of Memory")
bool CopyPath2File(const std::string &src, FILE *fdest)
Definition: compression.cc:46
virtual Catalog * CreateCatalog(const PathString &mountpoint, const shash::Any &catalog_hash, Catalog *parent_catalog)
bool FileExists(const std::string &path)
Definition: posix.cc:803
void SetMountpoint(const PathString &mountpoint)
Definition: catalog_mgr.h:134
const char * Code2Ascii(const Failures error)
const char kSuffixCatalog
Definition: hash.h:54
void TakeDatabaseFileOwnership()
Definition: catalog.cc:464
void SetRootCtlgLocation(RootCatalogLocation root_ctlg_location)
Definition: catalog_mgr.h:141
std::string MakePathWithoutSuffix() const
Definition: hash.h:323
ShortString< kDefaultMaxPath, 0 > PathString
Definition: shortstring.h:213
Failures Fetch(JobInfo *info)
Definition: download.cc:1982
void SetHash(shash::Any hash)
Definition: catalog_mgr.h:133
Suffix suffix
Definition: hash.h:123
std::string MakePath() const
Definition: hash.h:306
void SetSqlitePath(const std::string &sqlite_path)
Definition: catalog_mgr.h:135
std::string CopyCatalogToTempFile(const std::string &cache_path)
virtual LoadReturn GetNewRootCatalogContext(CatalogContext *result)