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