OmniSciDB  a987f07e93
 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 263 of file MigrationMgr.cpp.

References rename_and_symlink_path().

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

266  {
267  auto old_path = std::filesystem::canonical(base_path);
268  auto new_path = std::filesystem::canonical(base_path);
269  if (!dir_name.empty()) {
270  old_path /= dir_name;
271  new_path /= dir_name;
272  }
273  if (old_file_name.empty()) {
274  throw std::runtime_error(
275  "Unexpected error in rename_and_symlink_file: old_file_name is empty");
276  }
277  old_path /= old_file_name;
278 
279  if (new_file_name.empty()) {
280  throw std::runtime_error(
281  "Unexpected error in rename_and_symlink_file: new_file_name is empty");
282  }
283  new_path /= new_file_name;
284 
285  return rename_and_symlink_path(old_path, new_path);
286 }
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 207 of file MigrationMgr.cpp.

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

208  {
209  bool file_updated{false};
210  if (std::filesystem::exists(old_path)) {
211  // Skip if we have already created a symlink for the old path.
212  if (std::filesystem::is_symlink(old_path)) {
213  if (std::filesystem::read_symlink(old_path) != new_path.filename()) {
214  std::stringstream ss;
215  ss << "Rebrand migration: Encountered an unexpected symlink at path: " << old_path
216  << ". Symlink does not reference file: " << new_path.filename();
217  throw std::runtime_error(ss.str());
218  }
219  if (!std::filesystem::exists(new_path)) {
220  std::stringstream ss;
221  ss << "Rebrand migration: Encountered symlink at legacy path: " << old_path
222  << " but no corresponding file at new path: " << new_path;
223  throw std::runtime_error(ss.str());
224  }
225  } else {
226  if (std::filesystem::exists(new_path)) {
227  std::stringstream ss;
228  ss << "Rebrand migration: Encountered existing non-symlink files at the legacy "
229  "path: "
230  << old_path << " and new path: " << new_path;
231  throw std::runtime_error(ss.str());
232  }
233  std::filesystem::rename(old_path, new_path);
234  std::cout << "Rebrand migration: Renamed " << old_path << " to " << new_path
235  << std::endl;
236  file_updated = true;
237  }
238  }
239 
240  if (std::filesystem::exists(old_path)) {
241  if (!std::filesystem::is_symlink(old_path)) {
242  std::stringstream ss;
243  ss << "Rebrand migration: An unexpected error occurred. A symlink should have been "
244  "created at "
245  << old_path;
246  throw std::runtime_error(ss.str());
247  }
248  if (std::filesystem::read_symlink(old_path) != new_path.filename()) {
249  std::stringstream ss;
250  ss << "Rebrand migration: Encountered an unexpected symlink at path: " << old_path
251  << ". Symlink does not reference file: " << new_path.filename();
252  throw std::runtime_error(ss.str());
253  }
254  } else if (std::filesystem::exists(new_path)) {
255  std::filesystem::create_symlink(new_path.filename(), old_path);
256  std::cout << "Rebrand migration: Added symlink from " << old_path << " to "
257  << new_path.filename() << std::endl;
258  file_updated = true;
259  }
260  return file_updated;
261 }

+ Here is the caller graph for this function: