move_file_replace.cpp 620 B

12345678910111213141516171819202122232425
  1. //
  2. // $Id$
  3. //
  4. #include<__vic/fs.h>
  5. #include<__vic/throw_errno.h>
  6. #include<cstdio>
  7. #include<cerrno>
  8. namespace __vic {
  9. //----------------------------------------------------------------------------
  10. void move_file_replace(const char *src_path, const char *dest_path)
  11. {
  12. if(std::rename(src_path, dest_path) == 0) return;
  13. int err = errno;
  14. if(err != EXDEV) throw_errno("rename", err);
  15. // src and dest are not on the same filesystem
  16. copy_file(src_path, dest_path, true);
  17. remove_file_if_exists(src_path);
  18. }
  19. //----------------------------------------------------------------------------
  20. } // namespace