CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sync_union_tarball.cc
Go to the documentation of this file.
1 
5 #define __STDC_FORMAT_MACROS
6 
7 #include "sync_union_tarball.h"
8 
9 #include <pthread.h>
10 #include <unistd.h>
11 
12 #include <cassert>
13 #include <cstdio>
14 #include <list>
15 #include <set>
16 #include <string>
17 #include <vector>
18 
19 #include "duplex_libarchive.h"
20 #include "sync_item.h"
21 #include "sync_item_dummy.h"
22 #include "sync_item_tar.h"
23 #include "sync_mediator.h"
24 #include "sync_union.h"
25 #include "util/concurrency.h"
26 #include "util/exception.h"
27 #include "util/fs_traversal.h"
28 #include "util/posix.h"
29 #include "util/smalloc.h"
30 
31 namespace publish {
32 
34  const std::string &rdonly_path,
35  const std::string &tarball_path,
36  const std::string &base_directory,
37  const std::string &to_delete,
38  const bool create_catalog_on_root)
39  : SyncUnion(mediator, rdonly_path, "", ""),
40  src(NULL),
41  tarball_path_(tarball_path),
42  base_directory_(base_directory),
43  to_delete_(to_delete),
44  create_catalog_on_root_(create_catalog_on_root),
45  read_archive_signal_(new Signal) {}
46 
48 
50  bool result;
51 
52  // We are just deleting entity from the repo
53  if (tarball_path_ == "") {
54  assert(NULL == src);
55  return SyncUnion::Initialize();
56  }
57 
58  src = archive_read_new();
59  assert(ARCHIVE_OK == archive_read_support_format_tar(src));
60  assert(ARCHIVE_OK == archive_read_support_format_empty(src));
61 
62  if (tarball_path_ == "-") {
63  result = archive_read_open_filename(src, NULL, kBlockSize);
64  } else {
65  std::string tarball_absolute_path = GetAbsolutePath(tarball_path_);
66  result = archive_read_open_filename(src, tarball_absolute_path.c_str(),
67  kBlockSize);
68  }
69 
70  if (result != ARCHIVE_OK) {
71  LogCvmfs(kLogUnionFs, kLogStderr, "Impossible to open the archive: %s",
72  archive_error_string(src));
73  return false;
74  }
75 
76  return SyncUnion::Initialize();
77 }
78 
79 /*
80  * Libarchive is not thread aware, so we need to make sure that before
81  * to read/"open" the next header in the archive the content of the
82  *
83  * present header is been consumed completely.
84  * Different thread read/"open" the header from the one that consumes
85  * it so we opted for a Signal that is backed by a conditional variable.
86  * We wait for the signal just before to read the header.
87  * Then when we have done with the header the Signal is fired.
88  * The Signal can be fired inside the main loop if we don't need to read
89  * data, or when the IngestionSource get closed, which means that we are
90  * not reading data anymore from there.
91  * This whole process is not necessary for directories since we don't
92  * actually need to read data from them.
93  *
94  * It may be needed to add a catalog as a root of the archive.
95  * A possible way to do it is by creating an virtual `.cvmfscatalog` file and
96  * push it into the usual pipeline.
97  * This operation must be done only once, and it seems like a good idea to do
98  * it at the first iteration of the loop, hence this logic is managed by the
99  * `first_iteration` boolean flag.
100  */
103  assert(this->IsInitialized());
104 
105  /*
106  * As first step we eliminate the requested directories.
107  */
108  if (to_delete_ != "") {
109  vector<std::string> to_eliminate_vec = SplitString(to_delete_, ':');
110 
111  for (vector<string>::iterator s = to_eliminate_vec.begin();
112  s != to_eliminate_vec.end(); ++s) {
113  std::string parent_path;
114  std::string filename;
115  SplitPath(*s, &parent_path, &filename);
116  if (parent_path == ".") parent_path = "";
117  SharedPtr<SyncItem> sync_entry =
118  CreateSyncItem(parent_path, filename, kItemDir);
119  mediator_->Remove(sync_entry);
120  }
121  }
122 
123  // we are simply deleting entity from the repo
124  if (NULL == src) return;
125 
126  struct archive_entry *entry = archive_entry_new();
127  while (true) {
128  // Get the lock, wait if lock is not available yet
130 
131  int result = archive_read_next_header2(src, entry);
132 
133  switch (result) {
134  case ARCHIVE_FATAL: {
135  PANIC(kLogStderr, "Fatal error in reading the archive.\n%s\n",
136  archive_error_string(src));
137  break; // Only exit point with error
138  }
139 
140  case ARCHIVE_RETRY: {
142  "Error in reading the header, retrying.\n%s\n",
143  archive_error_string(src));
144  continue;
145  break;
146  }
147 
148  case ARCHIVE_EOF: {
149  if (create_catalog_on_root_ && (base_directory_ != "/")) {
150  CreateDirectories(base_directory_); // necessary for empty archives
153  ProcessFile(catalog);
155  }
156  for (set<string>::iterator dir = to_create_catalog_dirs_.begin();
157  dir != to_create_catalog_dirs_.end(); ++dir) {
158  assert(dirs_.find(*dir) != dirs_.end());
159  SharedPtr<SyncItem> to_mark = dirs_[*dir];
160  assert(to_mark->IsDirectory());
161  to_mark->SetCatalogMarker();
162  to_mark->MakePlaceholderDirectory();
163  ProcessDirectory(to_mark);
164  }
165  return; // Only successful exit point
166  break;
167  }
168 
169  case ARCHIVE_WARN: {
171  "Warning in uncompression reading, going on.\n %s",
172  archive_error_string(src));
173  // We actually want this to enter the ARCHIVE_OK case
174  }
175 
176  case ARCHIVE_OK: {
177  ProcessArchiveEntry(entry);
178  break;
179  }
180 
181  default: {
182  // We should never enter in this branch, but just for safeness we prefer
183  // to abort in case we hit a case we don't how to manage.
184  PANIC(kLogStderr, "Enter in unknown state. Aborting.\nError: %s\n",
185  result, archive_error_string(src));
186  }
187  }
188  }
189 }
190 
191 void SyncUnionTarball::ProcessArchiveEntry(struct archive_entry *entry) {
192  std::string archive_file_path(archive_entry_pathname(entry));
193  archive_file_path = SanitizePath(archive_file_path);
194 
195  std::string complete_path =
196  base_directory_ != "/"
197  ? MakeCanonicalPath(base_directory_ + "/" + archive_file_path)
198  : MakeCanonicalPath(archive_file_path);
199 
200  std::string parent_path;
201  std::string filename;
202  SplitPath(complete_path, &parent_path, &filename);
203  if (parent_path == ".") parent_path.clear();
204 
205  CreateDirectories(parent_path);
206 
208  parent_path, filename, src, entry, read_archive_signal_, this));
209 
210  if (NULL != archive_entry_hardlink(entry)) {
211  const std::string hardlink_name(
212  SanitizePath(archive_entry_hardlink(entry)));
213  const std::string hardlink = base_directory_ != "/"
214  ? base_directory_ + "/" + hardlink_name
215  : hardlink_name;
216 
217  if (hardlinks_.find(hardlink) != hardlinks_.end()) {
218  hardlinks_.find(hardlink)->second.push_back(complete_path);
219  } else {
220  std::list<std::string> to_hardlink;
221  to_hardlink.push_back(complete_path);
222  hardlinks_[hardlink] = to_hardlink;
223  }
224  if (filename == ".cvmfscatalog") {
225  // the file is created in the PostUpload phase
226  to_create_catalog_dirs_.insert(parent_path);
227  }
229  return;
230  }
231 
232  if (sync_entry->IsDirectory()) {
233  if (know_directories_.find(complete_path) != know_directories_.end()) {
234  sync_entry->MakePlaceholderDirectory();
235  }
236  ProcessUnmaterializedDirectory(sync_entry);
237  dirs_[complete_path] = sync_entry;
238  know_directories_.insert(complete_path);
239 
240  read_archive_signal_->Wakeup(); // We don't need to read data and we
241  // can read the next header
242 
243  } else if (sync_entry->IsRegularFile()) {
244  // inside the process pipeline we will wake up the signal
245  ProcessFile(sync_entry);
246  if (filename == ".cvmfscatalog") {
247  to_create_catalog_dirs_.insert(parent_path);
248  }
249 
250  } else if (sync_entry->IsSymlink() || sync_entry->IsFifo() ||
251  sync_entry->IsSocket() || sync_entry->IsCharacterDevice() ||
252  sync_entry->IsBlockDevice()) {
253  // we avoid to add an entity called as a catalog marker if it is not a
254  // regular file
255  if (filename != ".cvmfscatalog") {
256  ProcessFile(sync_entry);
257  } else {
259  "Found entity called as a catalog marker '%s' that however is "
260  "not a regular file, abort",
261  complete_path.c_str());
262  }
263 
264  // here we don't need to read data from the tar file so we can wake up
265  // immediately the signal
267 
268  } else {
269  PANIC(kLogStderr, "Fatal error found unexpected file: \n%s\n",
270  filename.c_str());
271  // if for any reason this code path change and we don't abort anymore,
272  // remember to wakeup the signal, otherwise we will be stuck in a deadlock
273  //
274  // read_archive_signal_->Wakeup();
275  }
276 }
277 
278 std::string SyncUnionTarball::SanitizePath(const std::string &path) {
279  if (path.length() >= 2) {
280  if (path[0] == '.' && path[1] == '/') {
281  return path.substr(2);
282  }
283  }
284  if (path.length() >= 1) {
285  if (path[0] == '/') {
286  return path.substr(1);
287  }
288  }
289  return path;
290 }
291 
293  std::map<const std::string, std::list<std::string> >::iterator hardlink;
294  for (hardlink = hardlinks_.begin(); hardlink != hardlinks_.end();
295  ++hardlink) {
296  std::list<std::string>::iterator entry;
297  for (entry = hardlink->second.begin(); entry != hardlink->second.end();
298  ++entry) {
299  mediator_->Clone(*entry, hardlink->first);
300  }
301  }
302 }
303 
305  SharedPtr<SyncItem> entry) const {
306  return entry->filename();
307 }
308 
310  return false;
311 }
312 
314  return false;
315 }
316 
317 /* Tar files are not necessarily traversed in order from root to leave.
318  * So it may happens that we are expanding the file `/a/b/c.txt` without
319  * having created yet the directory `/a/b/`.
320  * In order to overcome this limitation the following function create dummy
321  * directories that can be used as placeholder and that they will be overwritten
322  * as soon as the real directory is found in the tarball
323  */
324 void SyncUnionTarball::CreateDirectories(const std::string &target) {
325  if (know_directories_.find(target) != know_directories_.end()) return;
326  if (target == ".") return;
327 
328  std::string dirname = "";
329  std::string filename = "";
330  SplitPath(target, &dirname, &filename);
331  CreateDirectories(dirname);
332 
333  if (dirname == ".") dirname = "";
335  new SyncItemDummyDir(dirname, filename, this, kItemDir));
336 
338  dirs_[target] = dummy;
339  know_directories_.insert(target);
340 }
341 
342 } // namespace publish
#define LogCvmfs(source, mask,...)
Definition: logging.h:25
std::map< const std::string, std::list< std::string > > hardlinks_
virtual bool ProcessUnmaterializedDirectory(SharedPtr< SyncItem > entry)
Definition: sync_union.cc:92
bool IsOpaqueDirectory(SharedPtr< SyncItem > directory) const
std::map< std::string, SharedPtr< SyncItem > > dirs_
const std::string base_directory_
#define PANIC(...)
Definition: exception.h:29
virtual bool Initialize()
Definition: sync_union.cc:24
void Wakeup()
Definition: concurrency.cc:59
assert((mem||(size==0))&&"Out Of Memory")
std::string UnwindWhiteoutFilename(SharedPtr< SyncItem > entry) const
void CreateDirectories(const std::string &target)
SharedPtr< SyncItem > CreateSyncItem(const std::string &relative_parent_path, const std::string &filename, const SyncItemType entry_type) const
Definition: sync_union.cc:30
const std::string tarball_path_
std::set< std::string > to_create_catalog_dirs_
std::string GetAbsolutePath(const std::string &path)
Definition: posix.cc:160
void SplitPath(const std::string &path, std::string *dirname, std::string *filename)
Definition: posix.cc:113
AbstractSyncMediator * mediator_
Definition: sync_union.h:147
vector< string > SplitString(const string &str, char delim)
Definition: string.cc:290
bool IsInitialized() const
Definition: sync_union.h:139
std::string SanitizePath(const std::string &path)
const std::string to_delete_
entity to delete before to extract the tar
void ProcessArchiveEntry(struct archive_entry *entry)
void ProcessFile(SharedPtr< SyncItem > entry)
Definition: sync_union.cc:116
void Wait()
Definition: concurrency.cc:49
virtual void Clone(const std::string from, const std::string to)=0
virtual void Remove(SharedPtr< SyncItem > entry)=0
virtual bool ProcessDirectory(const std::string &parent_dir, const std::string &dir_name)
static const size_t kBlockSize
bool IsWhiteoutEntry(SharedPtr< SyncItem > entry) const
std::set< std::string > know_directories_
directory that we know already exist
std::string MakeCanonicalPath(const std::string &path)
Definition: posix.cc:98
SyncUnionTarball(AbstractSyncMediator *mediator, const std::string &rdonly_path, const std::string &tarball_path, const std::string &base_directory, const std::string &to_delete, const bool create_catalog_on_root)