OmniSciDB  4201147b46
 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 209 of file MigrationMgr.cpp.

References rename_and_symlink_path().

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

212  {
213  auto old_path = std::filesystem::canonical(base_path);
214  auto new_path = std::filesystem::canonical(base_path);
215  if (!dir_name.empty()) {
216  old_path /= dir_name;
217  new_path /= dir_name;
218  }
219  if (old_file_name.empty()) {
220  throw std::runtime_error(
221  "Unexpected error in rename_and_symlink_file: old_file_name is empty");
222  }
223  old_path /= old_file_name;
224 
225  if (new_file_name.empty()) {
226  throw std::runtime_error(
227  "Unexpected error in rename_and_symlink_file: new_file_name is empty");
228  }
229  new_path /= new_file_name;
230 
231  return rename_and_symlink_path(old_path, new_path);
232 }
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 153 of file MigrationMgr.cpp.

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

154  {
155  bool file_updated{false};
156  if (std::filesystem::exists(old_path)) {
157  // Skip if we have already created a symlink for the old path.
158  if (std::filesystem::is_symlink(old_path)) {
159  if (std::filesystem::read_symlink(old_path) != new_path.filename()) {
160  std::stringstream ss;
161  ss << "Rebrand migration: Encountered an unexpected symlink at path: " << old_path
162  << ". Symlink does not reference file: " << new_path.filename();
163  throw std::runtime_error(ss.str());
164  }
165  if (!std::filesystem::exists(new_path)) {
166  std::stringstream ss;
167  ss << "Rebrand migration: Encountered symlink at legacy path: " << old_path
168  << " but no corresponding file at new path: " << new_path;
169  throw std::runtime_error(ss.str());
170  }
171  } else {
172  if (std::filesystem::exists(new_path)) {
173  std::stringstream ss;
174  ss << "Rebrand migration: Encountered existing non-symlink files at the legacy "
175  "path: "
176  << old_path << " and new path: " << new_path;
177  throw std::runtime_error(ss.str());
178  }
179  std::filesystem::rename(old_path, new_path);
180  std::cout << "Rebrand migration: Renamed " << old_path << " to " << new_path
181  << std::endl;
182  file_updated = true;
183  }
184  }
185 
186  if (std::filesystem::exists(old_path)) {
187  if (!std::filesystem::is_symlink(old_path)) {
188  std::stringstream ss;
189  ss << "Rebrand migration: An unexpected error occurred. A symlink should have been "
190  "created at "
191  << old_path;
192  throw std::runtime_error(ss.str());
193  }
194  if (std::filesystem::read_symlink(old_path) != new_path.filename()) {
195  std::stringstream ss;
196  ss << "Rebrand migration: Encountered an unexpected symlink at path: " << old_path
197  << ". Symlink does not reference file: " << new_path.filename();
198  throw std::runtime_error(ss.str());
199  }
200  } else if (std::filesystem::exists(new_path)) {
201  std::filesystem::create_symlink(new_path.filename(), old_path);
202  std::cout << "Rebrand migration: Added symlink from " << old_path << " to "
203  << new_path.filename() << std::endl;
204  file_updated = true;
205  }
206  return file_updated;
207 }

+ Here is the caller graph for this function: