CernVM-FS  2.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
repository_diff.cc
Go to the documentation of this file.
1 
5 #include "cvmfs_config.h"
6 #include "publish/repository.h"
7 
8 #include <cassert>
9 #include <string>
10 
11 #include "catalog_counters.h"
12 #include "catalog_diff_tool.h"
13 #include "catalog_mgr_ro.h"
14 #include "file_chunk.h"
15 #include "hash.h"
16 #include "history_sqlite.h"
17 #include "publish/except.h"
18 #include "shortstring.h"
19 #include "statistics.h"
20 #include "xattr.h"
21 
22 namespace {
23 
24 static history::History::Tag GetTag(const std::string &tag_name,
26 {
27  assert(!tag_name.empty());
28 
30 
31  if (tag_name[0] == publish::Repository::kRawHashSymbol) {
32  tag.name = tag_name.substr(1);
33  tag.root_hash =
35  } else {
36  bool retval = history.GetByName(tag_name, &tag);
37  if (!retval)
38  throw publish::EPublish("unknown repository tag name: " + tag_name);
39  }
40 
41  return tag;
42 }
43 
44 
45 class DiffForwarder : public CatalogDiffTool<catalog::SimpleCatalogManager> {
46  private:
47  publish::DiffListener *listener_;
48 
49  public:
52  publish::DiffListener *listener)
53  : CatalogDiffTool<catalog::SimpleCatalogManager>(old_mgr, new_mgr)
54  , listener_(listener)
55  {
56  }
57  virtual ~DiffForwarder() {}
58 
59  virtual void ReportAddition(const PathString& path,
60  const catalog::DirectoryEntry& entry,
61  const XattrList& /* xattrs */,
62  const FileChunkList& /* chunks */)
63  {
64  listener_->OnAdd(path.ToString(), entry);
65  }
66 
67  virtual void ReportRemoval(const PathString& path,
68  const catalog::DirectoryEntry& entry)
69  {
70  listener_->OnRemove(path.ToString(), entry);
71  }
72 
73  virtual bool ReportModification(const PathString& path,
74  const catalog::DirectoryEntry& old_entry,
75  const catalog::DirectoryEntry& new_entry,
76  const XattrList& /*xattrs */,
77  const FileChunkList& /* chunks */)
78  {
79  listener_->OnModify(path.ToString(), old_entry, new_entry);
80  return true;
81  }
82 }; // class DiffForwarder
83 
84 } // anonymous namespace
85 
86 namespace publish {
87 
88 void Repository::Diff(const std::string &from, const std::string &to,
89  DiffListener *diff_listener)
90 {
91  history::History::Tag from_tag = GetTag(from, *history_);
92  history::History::Tag to_tag = GetTag(to, *history_);
93  diff_listener->OnInit(from_tag, to_tag);
94 
95  perf::Statistics stats_from;
97  from_tag.root_hash,
98  settings_.url(),
101  &stats_from,
102  true /* manage_catalog_files */);
103  mgr_from->Init();
104 
105  perf::Statistics stats_to;
107  to_tag.root_hash,
108  settings_.url(),
109  settings_.tmp_dir(),
111  &stats_to,
112  true /* manage_catalog_files */);
113  mgr_to->Init();
114 
115  catalog::Counters counters_from = mgr_from->GetRootCatalog()->GetCounters();
116  catalog::Counters counters_to = mgr_to->GetRootCatalog()->GetCounters();
117  diff_listener->OnStats(catalog::Counters::Diff(counters_from, counters_to));
118 
119  // DiffTool takes ownership of the catalog managers
120  DiffForwarder diff_forwarder(mgr_from, mgr_to, diff_listener);
121  if (!diff_forwarder.Init())
122  throw EPublish("cannot initialize difference engine");
123  diff_forwarder.Run(PathString());
124 }
125 
126 } // namespace publish
const SettingsRepository settings_
Definition: repository.h:136
const Counters & GetCounters() const
Definition: catalog.h:175
download::DownloadManager * download_mgr_
Definition: repository.h:140
std::string name
Definition: history.h:113
const history::History * history() const
static DeltaCounters Diff(const Counters &from, const Counters &to)
assert((mem||(size==0))&&"Out Of Memory")
virtual bool ReportModification(const PathString &path, const catalog::DirectoryEntry &old_entry, const catalog::DirectoryEntry &new_entry, const XattrList &, const FileChunkList &)
virtual void ReportAddition(const PathString &path, const catalog::DirectoryEntry &entry, const XattrList &, const FileChunkList &)
const char kSuffixCatalog
Definition: hash.h:52
Any MkFromHexPtr(const HexPtr hex, const char suffix)
Definition: hash.cc:83
history::SqliteHistory * history_
Definition: repository.h:148
CatalogT * GetRootCatalog() const
Definition: catalog_mgr.h:192
virtual bool GetByName(const std::string &name, Tag *tag) const =0
std::string ToString() const
Definition: shortstring.h:114
ShortString< kDefaultMaxPath, 0 > PathString
Definition: shortstring.h:190
shash::Any root_hash
Definition: history.h:114
virtual void ReportRemoval(const PathString &path, const catalog::DirectoryEntry &entry)
std::string url() const
Definition: settings.h:369
static history::History::Tag GetTag(const std::string &tag_name, const history::History &history)
void Diff(const std::string &from, const std::string &to, DiffListener *diff_listener)
std::string tmp_dir() const
Definition: settings.h:370
DiffForwarder(catalog::SimpleCatalogManager *old_mgr, catalog::SimpleCatalogManager *new_mgr, publish::DiffListener *listener)