copy_file_if_exists.cpp 783 B

1234567891011121314151617181920212223242526272829303132
  1. //
  2. // $Id$
  3. //
  4. #include<__vic/fs.h>
  5. #include<__vic/posix/error.h>
  6. #include<__vic/throw_errno.h>
  7. #include<__vic/posix/file.h>
  8. namespace __vic {
  9. void write_fd_to_file(int , const char * , bool );
  10. //----------------------------------------------------------------------------
  11. bool copy_file_if_exists(
  12. const char *src_path, const char *dest_path, bool replace)
  13. {
  14. posix::file src(src_path, O_RDONLY);
  15. if(!src.is_open())
  16. {
  17. int err = errno;
  18. if(posix::is_ENOENT(err)) return false; // input file doesn't exist
  19. throw_errno("Cannot open input file", err);
  20. }
  21. write_fd_to_file(src.handle(), dest_path, replace);
  22. src.close();
  23. return true;
  24. }
  25. //----------------------------------------------------------------------------
  26. } // namespace