file_stat.h 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // <sys/stat.h> C++-wrapper
  2. //
  3. // Platform: ISO C++ 98/11 - POSIX
  4. // $Id$
  5. //
  6. // (c) __vic 2012
  7. #ifndef __VIC_POSIX_FILE_STAT_H
  8. #define __VIC_POSIX_FILE_STAT_H
  9. #include<__vic/defs.h>
  10. #include<__vic/throw_errno.h>
  11. #include<sys/types.h>
  12. #include<sys/stat.h>
  13. #include<string>
  14. namespace __vic { namespace posix {
  15. //////////////////////////////////////////////////////////////////////////////
  16. struct file_stat : public ::stat
  17. {
  18. enum for_link_t { for_link };
  19. file_stat() __VIC_DEFAULT_CTR // uninitialized!
  20. typedef struct ::stat base; // Workaround for Clang's 3.5 bug
  21. file_stat(const struct ::stat &s) : base(s) {}
  22. explicit file_stat(int fd) { get(fd); }
  23. explicit file_stat(const char *path) { get(path); }
  24. explicit file_stat(const std::string &path) { get(path); }
  25. file_stat(const char *path, for_link_t) { get_for_link(path); }
  26. file_stat(const std::string &path, for_link_t) { get_for_link(path); }
  27. void get(int fd)
  28. { if(::fstat(fd, this)) throw_errno("fstat"); }
  29. void get(const char *path)
  30. { if(::stat(path, this)) throw_errno("stat"); }
  31. void get(const std::string &path)
  32. { get(path.c_str()); }
  33. void get_for_link(const char *path)
  34. { if(::lstat(path, this)) throw_errno("lstat"); }
  35. void get_for_link(const std::string &path)
  36. { get_for_link(path.c_str()); }
  37. bool try_get(int fd) { return !::fstat(fd, this); }
  38. bool try_get(const char *path) { return !::stat(path, this); }
  39. bool try_get(const std::string &path) { return try_get(path.c_str()); }
  40. bool try_get_for_link(const char *path) { return !::lstat(path, this); }
  41. bool try_get_for_link(const std::string &path)
  42. { return try_get_for_link(path.c_str()); }
  43. bool get_if_exists(const char * );
  44. bool get_if_exists(const std::string &path)
  45. { return get_if_exists(path.c_str()); }
  46. // POSIX-defined attributes
  47. dev_t dev() const { return this->st_dev; }
  48. ino_t ino() const { return this->st_ino; }
  49. mode_t mode() const { return this->st_mode; }
  50. nlink_t nlink() const { return this->st_nlink; }
  51. uid_t uid() const { return this->st_uid; }
  52. gid_t gid() const { return this->st_gid; }
  53. off_t size() const { return this->st_size; }
  54. time_t atime() const { return this->st_atime; }
  55. time_t mtime() const { return this->st_mtime; }
  56. time_t ctime() const { return this->st_ctime; }
  57. bool is_block() const { return S_ISBLK(mode()); }
  58. bool is_char() const { return S_ISCHR(mode()); }
  59. bool is_dir() const { return S_ISDIR(mode()); }
  60. bool is_fifo() const { return S_ISFIFO(mode()); }
  61. bool is_regular() const { return S_ISREG(mode()); }
  62. bool is_link() const { return S_ISLNK(mode()); }
  63. bool is_socket() const { return S_ISSOCK(mode()); }
  64. };
  65. //////////////////////////////////////////////////////////////////////////////
  66. //----------------------------------------------------------------------------
  67. inline bool is_block(const struct ::stat &s) { return S_ISBLK(s.st_mode); }
  68. inline bool is_char(const struct ::stat &s) { return S_ISCHR(s.st_mode); }
  69. inline bool is_dir(const struct ::stat &s) { return S_ISDIR(s.st_mode); }
  70. inline bool is_fifo(const struct ::stat &s) { return S_ISFIFO(s.st_mode); }
  71. inline bool is_regular(const struct ::stat &s) { return S_ISREG(s.st_mode); }
  72. inline bool is_link(const struct ::stat &s) { return S_ISLNK(s.st_mode); }
  73. inline bool is_socket(const struct ::stat &s) { return S_ISSOCK(s.st_mode); }
  74. //----------------------------------------------------------------------------
  75. inline bool is_same_file(const struct ::stat &s1, const struct ::stat &s2)
  76. {
  77. return s1.st_ino == s2.st_ino && s1.st_dev == s2.st_dev;
  78. }
  79. //----------------------------------------------------------------------------
  80. }} // namespace
  81. #endif // header guard