1 |
|
|
/** |
2 |
|
|
* This file is part of the CernVM File System |
3 |
|
|
* |
4 |
|
|
* The WritableCatalog class is derived from Catalog. It is used by the |
5 |
|
|
* WritableCatalogManager on the server side. |
6 |
|
|
* |
7 |
|
|
* The main functionality is in: |
8 |
|
|
* - AddEntry |
9 |
|
|
* - UpdateEntry |
10 |
|
|
* - RemoveEntry |
11 |
|
|
* |
12 |
|
|
* Catalogs not thread safe. |
13 |
|
|
*/ |
14 |
|
|
|
15 |
|
|
#ifndef CVMFS_CATALOG_RW_H_ |
16 |
|
|
#define CVMFS_CATALOG_RW_H_ |
17 |
|
|
|
18 |
|
|
#include <stdint.h> |
19 |
|
|
|
20 |
|
|
#include <string> |
21 |
|
|
#include <vector> |
22 |
|
|
|
23 |
|
|
#include "catalog.h" |
24 |
|
|
#include "util/posix.h" |
25 |
|
|
|
26 |
|
|
class XattrList; |
27 |
|
|
|
28 |
|
|
namespace swissknife { |
29 |
|
|
class CommandMigrate; |
30 |
|
|
} |
31 |
|
|
|
32 |
|
|
namespace catalog { |
33 |
|
|
|
34 |
|
|
class WritableCatalogManager; |
35 |
|
|
|
36 |
|
|
class WritableCatalog : public Catalog { |
37 |
|
|
friend class WritableCatalogManager; |
38 |
|
|
friend class swissknife::CommandMigrate; // needed for catalog migrations |
39 |
|
|
friend class VirtualCatalog; // needed for /.cvmfs creation |
40 |
|
|
|
41 |
|
|
public: |
42 |
|
|
WritableCatalog(const std::string &path, |
43 |
|
|
const shash::Any &catalog_hash, |
44 |
|
|
Catalog *parent, |
45 |
|
|
const bool is_not_root = false); |
46 |
|
|
virtual ~WritableCatalog(); |
47 |
|
|
|
48 |
|
|
static WritableCatalog *AttachFreely(const std::string &root_path, |
49 |
|
|
const std::string &file, |
50 |
|
|
const shash::Any &catalog_hash, |
51 |
|
|
Catalog *parent = NULL, |
52 |
|
|
const bool is_not_root = false); |
53 |
|
|
|
54 |
|
|
void Transaction(); |
55 |
|
|
void Commit(); |
56 |
|
|
|
57 |
|
15 |
inline bool IsDirty() const { return dirty_; } |
58 |
|
105 |
inline bool IsWritable() const { return true; } |
59 |
|
|
uint32_t GetMaxLinkId() const; |
60 |
|
|
|
61 |
|
|
void AddEntry(const DirectoryEntry &entry, |
62 |
|
|
const XattrList &xattr, |
63 |
|
|
const std::string &entry_path, |
64 |
|
|
const std::string &parent_path); |
65 |
|
|
void TouchEntry(const DirectoryEntryBase &entry, const shash::Md5 &path_hash); |
66 |
|
|
inline void TouchEntry( |
67 |
|
|
const DirectoryEntryBase &entry, |
68 |
|
|
const std::string &path) |
69 |
|
|
{ |
70 |
|
|
TouchEntry(entry, shash::Md5(shash::AsciiPtr(path))); |
71 |
|
|
} |
72 |
|
|
void RemoveEntry(const std::string &entry_path); |
73 |
|
|
void IncLinkcount(const std::string &path_within_group, const int delta); |
74 |
|
|
void AddFileChunk(const std::string &entry_path, const FileChunk &chunk); |
75 |
|
|
void RemoveFileChunks(const std::string &entry_path); |
76 |
|
|
|
77 |
|
|
// Creation and removal of catalogs |
78 |
|
|
void Partition(WritableCatalog *new_nested_catalog); |
79 |
|
|
void MergeIntoParent(); |
80 |
|
|
void RemoveFromParent(); |
81 |
|
|
|
82 |
|
|
// Nested catalog references |
83 |
|
|
void InsertNestedCatalog(const std::string &mountpoint, |
84 |
|
|
Catalog *attached_reference, |
85 |
|
|
const shash::Any content_hash, |
86 |
|
|
const uint64_t size); |
87 |
|
|
void InsertBindMountpoint(const std::string &mountpoint, |
88 |
|
|
const shash::Any content_hash, |
89 |
|
|
const uint64_t size); |
90 |
|
|
void UpdateNestedCatalog(const std::string &path, |
91 |
|
|
const shash::Any &hash, |
92 |
|
|
const uint64_t size, |
93 |
|
|
const DeltaCounters &child_counters); |
94 |
|
|
void RemoveNestedCatalog(const std::string &mountpoint, |
95 |
|
|
Catalog **attached_reference); |
96 |
|
|
void RemoveBindMountpoint(const std::string &mountpoint); |
97 |
|
|
|
98 |
|
|
void UpdateLastModified(); |
99 |
|
|
void IncrementRevision(); |
100 |
|
|
void SetRevision(const uint64_t new_revision); |
101 |
|
|
void SetBranch(const std::string &branch_name); |
102 |
|
|
void SetPreviousRevision(const shash::Any &hash); |
103 |
|
|
void SetTTL(const uint64_t new_ttl); |
104 |
|
|
bool SetVOMSAuthz(const std::string &voms_authz); |
105 |
|
|
|
106 |
|
|
protected: |
107 |
|
|
static const double kMaximalFreePageRatio; // = 0.2 |
108 |
|
|
static const double kMaximalRowIdWasteRatio; // = 0.25; |
109 |
|
|
|
110 |
|
39 |
CatalogDatabase::OpenMode DatabaseOpenMode() const { |
111 |
|
39 |
return CatalogDatabase::kOpenReadWrite; |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
void UpdateEntry(const DirectoryEntry &entry, const shash::Md5 &path_hash); |
115 |
|
50 |
inline void UpdateEntry( |
116 |
|
|
const DirectoryEntry &entry, |
117 |
|
|
const std::string &path) |
118 |
|
|
{ |
119 |
|
50 |
UpdateEntry(entry, shash::Md5(shash::AsciiPtr(path))); |
120 |
|
50 |
} |
121 |
|
|
|
122 |
|
22 |
inline void AddEntry( |
123 |
|
|
const DirectoryEntry &entry, |
124 |
|
|
const XattrList &xattrs, |
125 |
|
|
const std::string &path) |
126 |
|
|
{ |
127 |
|
22 |
AddEntry(entry, xattrs, path, GetParentPath(path)); |
128 |
|
22 |
} |
129 |
|
|
|
130 |
|
|
void InitPreparedStatements(); |
131 |
|
|
void FinalizePreparedStatements(); |
132 |
|
|
|
133 |
|
14 |
inline WritableCatalog* GetWritableParent() const { |
134 |
|
14 |
Catalog *parent = this->parent(); |
135 |
✗✓ |
14 |
assert(parent->IsWritable()); |
136 |
|
14 |
return static_cast<WritableCatalog *>(parent); |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
int dirty_children() const { return atomic_read32(&dirty_children_); } |
140 |
|
15 |
void set_dirty_children(const int count) { |
141 |
|
15 |
atomic_write32(&dirty_children_, count); |
142 |
|
15 |
} |
143 |
|
7 |
int DecrementDirtyChildren() { |
144 |
|
7 |
return atomic_xadd32(&dirty_children_, -1) - 1; |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
private: |
148 |
|
|
SqlDirentInsert *sql_insert_; |
149 |
|
|
SqlDirentUnlink *sql_unlink_; |
150 |
|
|
SqlDirentTouch *sql_touch_; |
151 |
|
|
SqlDirentUpdate *sql_update_; |
152 |
|
|
SqlChunkInsert *sql_chunk_insert_; |
153 |
|
|
SqlChunksRemove *sql_chunks_remove_; |
154 |
|
|
SqlChunksCount *sql_chunks_count_; |
155 |
|
|
SqlMaxHardlinkGroup *sql_max_link_id_; |
156 |
|
|
SqlIncLinkcount *sql_inc_linkcount_; |
157 |
|
|
|
158 |
|
|
bool dirty_; /**< Indicates if the catalog has been changed */ |
159 |
|
|
|
160 |
|
|
DeltaCounters delta_counters_; |
161 |
|
|
|
162 |
|
|
// parallel commit state |
163 |
|
|
mutable atomic_int32 dirty_children_; |
164 |
|
|
|
165 |
|
267 |
inline void SetDirty() { |
166 |
✓✓ |
267 |
if (!dirty_) |
167 |
|
31 |
Transaction(); |
168 |
|
267 |
dirty_ = true; |
169 |
|
267 |
} |
170 |
|
|
|
171 |
|
|
// Helpers for nested catalog creation and removal |
172 |
|
|
void MakeTransitionPoint(const std::string &mountpoint); |
173 |
|
|
void MakeNestedRoot(); |
174 |
|
7 |
inline void MoveToNested( |
175 |
|
|
const std::string dir_structure_root, |
176 |
|
|
WritableCatalog *new_nested_catalog, |
177 |
|
|
std::vector<std::string> *grand_child_mountpoints) |
178 |
|
|
{ |
179 |
|
|
MoveToNestedRecursively(dir_structure_root, |
180 |
|
|
new_nested_catalog, |
181 |
|
7 |
grand_child_mountpoints); |
182 |
|
7 |
} |
183 |
|
|
void MoveToNestedRecursively( |
184 |
|
|
const std::string dir_structure_root, |
185 |
|
|
WritableCatalog *new_nested_catalog, |
186 |
|
|
std::vector<std::string> *grand_child_mountpoints); |
187 |
|
|
void MoveCatalogsToNested(const std::vector<std::string> &nested_catalogs, |
188 |
|
|
WritableCatalog *new_nested_catalog); |
189 |
|
|
void MoveFileChunksToNested(const std::string &full_path, |
190 |
|
|
const shash::Algorithms algorithm, |
191 |
|
|
WritableCatalog *new_nested_catalog); |
192 |
|
|
|
193 |
|
|
void CopyToParent(); |
194 |
|
|
void CopyCatalogsToParent(); |
195 |
|
|
|
196 |
|
|
void UpdateCounters(); |
197 |
|
|
void VacuumDatabaseIfNecessary(); |
198 |
|
|
}; // class WritableCatalog |
199 |
|
|
|
200 |
|
|
typedef std::vector<WritableCatalog *> WritableCatalogList; |
201 |
|
|
|
202 |
|
|
} // namespace catalog |
203 |
|
|
|
204 |
|
|
#endif // CVMFS_CATALOG_RW_H_ |