path_exists.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //
  2. // $Id$
  3. //
  4. #include<__vic/fs.h>
  5. #include<__vic/posix/file_stat.h>
  6. #include<__vic/posix/error.h>
  7. #include<__vic/string_buffer.h>
  8. #include<__vic/throw_errno.h>
  9. #include<cerrno>
  10. namespace __vic {
  11. namespace {
  12. //----------------------------------------------------------------------------
  13. bool get_stat(const char *path, posix::file_stat &s, const char *what)
  14. {
  15. if(s.try_get(path)) return true;
  16. int err = errno;
  17. switch(err)
  18. {
  19. __VIC_CASE_ENOENT
  20. case ENOTDIR:
  21. return false; // path doesn't exist
  22. }
  23. throw_errno(__vic::msg(64) <<
  24. "Can't stat " << what << " \"" << path << '"', err);
  25. }
  26. //----------------------------------------------------------------------------
  27. } // namespace
  28. //----------------------------------------------------------------------------
  29. bool path_exists(const char *path)
  30. {
  31. posix::file_stat s;
  32. return get_stat(path,s,"path");
  33. }
  34. //----------------------------------------------------------------------------
  35. bool file_exists(const char *path)
  36. {
  37. posix::file_stat s;
  38. return get_stat(path,s,"file") && s.is_regular();
  39. }
  40. //----------------------------------------------------------------------------
  41. bool dir_exists(const char *path)
  42. {
  43. posix::file_stat s;
  44. return get_stat(path,s,"dir") && s.is_dir();
  45. }
  46. //----------------------------------------------------------------------------
  47. } // namespace