CernVM-FS  2.12.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 
5 #include "cvmfs_config.h"
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", dir_cache_.c_str());
41  }
42  } else {
43  copy_to_tmp_dir_ = false;
44  }
45 }
46 
48  CatalogContext *result) {
49  if (result->hash().IsNull()) {
50  result->SetHash(base_hash_);
51  }
53  result->SetMountpoint(PathString("", 0));
54 
55  return kLoadNew;
56 }
57 
59  const std::string &cache_path) {
60  std::string tmp_path;
61  FILE *fcatalog = CreateTempFile(dir_temp_ + "/catalog", 0666, "w", &tmp_path);
62  if (!fcatalog) {
63  PANIC(kLogStderr, "failed to create temp file when loading %s",
64  cache_path.c_str());
65  }
66 
67  const bool retval = CopyPath2File(cache_path, fcatalog);
68  if (!retval) {
69  unlink(tmp_path.c_str());
70  PANIC(kLogStderr, "failed to read %s", cache_path.c_str());
71  }
72  (void) fclose(fcatalog);
73 
74  return tmp_path;
75 }
76 
93  CatalogContext *ctlg_context) {
94  const shash::Any effective_hash = ctlg_context->hash();
95  assert(shash::kSuffixCatalog == effective_hash.suffix);
96  const string url = stratum0_ + "/data/" + effective_hash.MakePath();
97 
98  FILE *fcatalog;
99 
100  if (UseLocalCache()) {
101  std::string cache_path = dir_cache_ + "/"
102  + effective_hash.MakePathWithoutSuffix();
103 
104  ctlg_context->SetSqlitePath(cache_path);
105 
106  // catalog is cached in "cache_dir/" + standard cvmfs file hierarchy
107  if (FileExists(cache_path.c_str())) {
108  if (!copy_to_tmp_dir_) {
109  return kLoadNew;
110  } else { // for writable catalog create copy in dir_temp_
111  std::string tmp_path;
112 
113  tmp_path = CopyCatalogToTempFile(cache_path);
114  ctlg_context->SetSqlitePath(tmp_path);
115 
116  return kLoadNew;
117  }
118  }
119  }
120 
121  // not in local cache; just create a random tmp file for download
122  std::string tmp_path;
123  fcatalog = CreateTempFile(dir_temp_ + "/catalog", 0666, "w", &tmp_path);
124  if (!fcatalog) {
125  PANIC(kLogStderr, "failed to create temp file when loading %s",
126  url.c_str());
127  }
128  ctlg_context->SetSqlitePath(tmp_path);
129 
130  cvmfs::FileSink filesink(fcatalog);
131  download::JobInfo download_catalog(&url, true, false,
132  &effective_hash, &filesink);
133  const download::Failures retval = download_manager_->Fetch(&download_catalog);
134  fclose(fcatalog);
135 
136  if (retval != download::kFailOk) {
137  unlink(tmp_path.c_str());
138  PANIC(kLogStderr, "failed to load %s from Stratum 0 (%d - %s)",
139  url.c_str(), retval, download::Code2Ascii(retval));
140  }
141 
142  // for local cache make an atomic rename call to make the file available
143  // in the local cache
144  if (UseLocalCache()) {
145  const std::string cache_path = dir_cache_ + "/"
146  + effective_hash.MakePathWithoutSuffix();
147  rename(tmp_path.c_str(), cache_path.c_str());
148  ctlg_context->SetSqlitePath(cache_path);
149 
150  // for writable catalog make an extra copy that can be modified
151  if (copy_to_tmp_dir_) {
152  const std::string new_tmp_path = CopyCatalogToTempFile(cache_path);
153  ctlg_context->SetSqlitePath(new_tmp_path);
154  }
155  }
156 
157  return kLoadNew;
158 }
159 
160 
162  const shash::Any &catalog_hash,
163  Catalog *parent_catalog)
164 {
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:882
shash::Any hash() const
Definition: catalog_mgr.h:125
bool IsNull() const
Definition: hash.h:383
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:1005
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:791
void SetMountpoint(const PathString &mountpoint)
Definition: catalog_mgr.h:135
const char * Code2Ascii(const Failures error)
const char kSuffixCatalog
Definition: hash.h:54
void TakeDatabaseFileOwnership()
Definition: catalog.cc:479
void SetRootCtlgLocation(RootCatalogLocation root_ctlg_location)
Definition: catalog_mgr.h:140
std::string MakePathWithoutSuffix() const
Definition: hash.h:335
ShortString< kDefaultMaxPath, 0 > PathString
Definition: shortstring.h:217
Failures Fetch(JobInfo *info)
Definition: download.cc:1860
void SetHash(shash::Any hash)
Definition: catalog_mgr.h:134
Suffix suffix
Definition: hash.h:126
std::string MakePath() const
Definition: hash.h:316
void SetSqlitePath(const std::string &sqlite_path)
Definition: catalog_mgr.h:136
std::string CopyCatalogToTempFile(const std::string &cache_path)
virtual LoadReturn GetNewRootCatalogContext(CatalogContext *result)