OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
migrations::anonymous_namespace{MigrationMgr.cpp} Namespace Reference

Functions

bool rename_and_symlink_path (const std::filesystem::path &old_path, const std::filesystem::path &new_path)
 
bool rename_and_symlink_file (const std::filesystem::path &base_path, const std::string &dir_name, const std::string &old_file_name, const std::string &new_file_name)
 

Function Documentation

bool migrations::anonymous_namespace{MigrationMgr.cpp}::rename_and_symlink_file ( const std::filesystem::path &  base_path,
const std::string &  dir_name,
const std::string &  old_file_name,
const std::string &  new_file_name 
)

Definition at line 441 of file MigrationMgr.cpp.

References rename_and_symlink_path().

Referenced by migrations::MigrationMgr::executeRebrandMigration().

444  {
445  auto old_path = std::filesystem::canonical(base_path);
446  auto new_path = std::filesystem::canonical(base_path);
447  if (!dir_name.empty()) {
448  old_path /= dir_name;
449  new_path /= dir_name;
450  }
451  if (old_file_name.empty()) {
452  throw std::runtime_error(
453  "Unexpected error in rename_and_symlink_file: old_file_name is empty");
454  }
455  old_path /= old_file_name;
456 
457  if (new_file_name.empty()) {
458  throw std::runtime_error(
459  "Unexpected error in rename_and_symlink_file: new_file_name is empty");
460  }
461  new_path /= new_file_name;
462 
463  return rename_and_symlink_path(old_path, new_path);
464 }
bool rename_and_symlink_path(const std::filesystem::path &old_path, const std::filesystem::path &new_path)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool migrations::anonymous_namespace{MigrationMgr.cpp}::rename_and_symlink_path ( const std::filesystem::path &  old_path,
const std::filesystem::path &  new_path 
)

Definition at line 385 of file MigrationMgr.cpp.

Referenced by migrations::MigrationMgr::executeRebrandMigration(), and rename_and_symlink_file().

386  {
387  bool file_updated{false};
388  if (std::filesystem::exists(old_path)) {
389  // Skip if we have already created a symlink for the old path.
390  if (std::filesystem::is_symlink(old_path)) {
391  if (std::filesystem::read_symlink(old_path) != new_path.filename()) {
392  std::stringstream ss;
393  ss << "Rebrand migration: Encountered an unexpected symlink at path: " << old_path
394  << ". Symlink does not reference file: " << new_path.filename();
395  throw std::runtime_error(ss.str());
396  }
397  if (!std::filesystem::exists(new_path)) {
398  std::stringstream ss;
399  ss << "Rebrand migration: Encountered symlink at legacy path: " << old_path
400  << " but no corresponding file at new path: " << new_path;
401  throw std::runtime_error(ss.str());
402  }
403  } else {
404  if (std::filesystem::exists(new_path)) {
405  std::stringstream ss;
406  ss << "Rebrand migration: Encountered existing non-symlink files at the legacy "
407  "path: "
408  << old_path << " and new path: " << new_path;
409  throw std::runtime_error(ss.str());
410  }
411  std::filesystem::rename(old_path, new_path);
412  std::cout << "Rebrand migration: Renamed " << old_path << " to " << new_path
413  << std::endl;
414  file_updated = true;
415  }
416  }
417 
418  if (std::filesystem::exists(old_path)) {
419  if (!std::filesystem::is_symlink(old_path)) {
420  std::stringstream ss;
421  ss << "Rebrand migration: An unexpected error occurred. A symlink should have been "
422  "created at "
423  << old_path;
424  throw std::runtime_error(ss.str());
425  }
426  if (std::filesystem::read_symlink(old_path) != new_path.filename()) {
427  std::stringstream ss;
428  ss << "Rebrand migration: Encountered an unexpected symlink at path: " << old_path
429  << ". Symlink does not reference file: " << new_path.filename();
430  throw std::runtime_error(ss.str());
431  }
432  } else if (std::filesystem::exists(new_path)) {
433  std::filesystem::create_symlink(new_path.filename(), old_path);
434  std::cout << "Rebrand migration: Added symlink from " << old_path << " to "
435  << new_path.filename() << std::endl;
436  file_updated = true;
437  }
438  return file_updated;
439 }

+ Here is the caller graph for this function: