file_status.hpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // boost/filesystem/file_status.hpp --------------------------------------------------//
  2. // Copyright Beman Dawes 2002-2009
  3. // Copyright Jan Langer 2002
  4. // Copyright Dietmar Kuehl 2001
  5. // Copyright Vladimir Prus 2002
  6. // Copyright Andrey Semashev 2019
  7. // Distributed under the Boost Software License, Version 1.0.
  8. // See http://www.boost.org/LICENSE_1_0.txt
  9. // Library home page: http://www.boost.org/libs/filesystem
  10. //--------------------------------------------------------------------------------------//
  11. #ifndef BOOST_FILESYSTEM_FILE_STATUS_HPP
  12. #define BOOST_FILESYSTEM_FILE_STATUS_HPP
  13. #include <boost/filesystem/config.hpp>
  14. #include <boost/detail/bitmask.hpp>
  15. #include <boost/filesystem/detail/header.hpp> // must be the last #include
  16. //--------------------------------------------------------------------------------------//
  17. namespace boost {
  18. namespace filesystem {
  19. //--------------------------------------------------------------------------------------//
  20. // file_type //
  21. //--------------------------------------------------------------------------------------//
  22. enum file_type
  23. {
  24. status_error,
  25. #ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  26. status_unknown = status_error,
  27. #endif
  28. file_not_found,
  29. regular_file,
  30. directory_file,
  31. // the following may not apply to some operating systems or file systems
  32. symlink_file,
  33. block_file,
  34. character_file,
  35. fifo_file,
  36. socket_file,
  37. reparse_file, // Windows: FILE_ATTRIBUTE_REPARSE_POINT that is not a symlink
  38. type_unknown // file does exist, but isn't one of the above types or
  39. // we don't have strong enough permission to find its type
  40. };
  41. //--------------------------------------------------------------------------------------//
  42. // perms //
  43. //--------------------------------------------------------------------------------------//
  44. enum perms
  45. {
  46. no_perms = 0, // file_not_found is no_perms rather than perms_not_known
  47. // POSIX equivalent macros given in comments.
  48. // Values are from POSIX and are given in octal per the POSIX standard.
  49. // permission bits
  50. owner_read = 0400, // S_IRUSR, Read permission, owner
  51. owner_write = 0200, // S_IWUSR, Write permission, owner
  52. owner_exe = 0100, // S_IXUSR, Execute/search permission, owner
  53. owner_all = 0700, // S_IRWXU, Read, write, execute/search by owner
  54. group_read = 040, // S_IRGRP, Read permission, group
  55. group_write = 020, // S_IWGRP, Write permission, group
  56. group_exe = 010, // S_IXGRP, Execute/search permission, group
  57. group_all = 070, // S_IRWXG, Read, write, execute/search by group
  58. others_read = 04, // S_IROTH, Read permission, others
  59. others_write = 02, // S_IWOTH, Write permission, others
  60. others_exe = 01, // S_IXOTH, Execute/search permission, others
  61. others_all = 07, // S_IRWXO, Read, write, execute/search by others
  62. all_all = 0777, // owner_all|group_all|others_all
  63. // other POSIX bits
  64. set_uid_on_exe = 04000, // S_ISUID, Set-user-ID on execution
  65. set_gid_on_exe = 02000, // S_ISGID, Set-group-ID on execution
  66. sticky_bit = 01000, // S_ISVTX,
  67. // (POSIX XSI) On directories, restricted deletion flag
  68. // (V7) 'sticky bit': save swapped text even after use
  69. // (SunOS) On non-directories: don't cache this file
  70. // (SVID-v4.2) On directories: restricted deletion flag
  71. // Also see http://en.wikipedia.org/wiki/Sticky_bit
  72. perms_mask = 07777, // all_all|set_uid_on_exe|set_gid_on_exe|sticky_bit
  73. perms_not_known = 0xFFFF, // present when directory_entry cache not loaded
  74. // options for permissions() function
  75. add_perms = 0x1000, // adds the given permission bits to the current bits
  76. remove_perms = 0x2000, // removes the given permission bits from the current bits;
  77. // choose add_perms or remove_perms, not both; if neither add_perms
  78. // nor remove_perms is given, replace the current bits with
  79. // the given bits.
  80. symlink_perms = 0x4000, // on POSIX, don't resolve symlinks; implied on Windows
  81. // BOOST_BITMASK op~ casts to int32_least_t, producing invalid enum values
  82. _detail_extend_perms_32_1 = 0x7fffffff,
  83. _detail_extend_perms_32_2 = -0x7fffffff - 1
  84. };
  85. BOOST_BITMASK(perms)
  86. //--------------------------------------------------------------------------------------//
  87. // file_status //
  88. //--------------------------------------------------------------------------------------//
  89. class file_status
  90. {
  91. public:
  92. BOOST_CONSTEXPR file_status() BOOST_NOEXCEPT :
  93. m_value(status_error),
  94. m_perms(perms_not_known)
  95. {
  96. }
  97. explicit BOOST_CONSTEXPR file_status(file_type v) BOOST_NOEXCEPT :
  98. m_value(v),
  99. m_perms(perms_not_known)
  100. {
  101. }
  102. BOOST_CONSTEXPR file_status(file_type v, perms prms) BOOST_NOEXCEPT :
  103. m_value(v),
  104. m_perms(prms)
  105. {
  106. }
  107. // As of October 2015 the interaction between noexcept and =default is so troublesome
  108. // for VC++, GCC, and probably other compilers, that =default is not used with noexcept
  109. // functions. GCC is not even consistent for the same release on different platforms.
  110. BOOST_CONSTEXPR file_status(file_status const& rhs) BOOST_NOEXCEPT :
  111. m_value(rhs.m_value),
  112. m_perms(rhs.m_perms)
  113. {
  114. }
  115. BOOST_CXX14_CONSTEXPR file_status& operator=(file_status const& rhs) BOOST_NOEXCEPT
  116. {
  117. m_value = rhs.m_value;
  118. m_perms = rhs.m_perms;
  119. return *this;
  120. }
  121. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  122. // Note: std::move is not constexpr in C++11, that's why we're not using it here
  123. BOOST_CONSTEXPR file_status(file_status&& rhs) BOOST_NOEXCEPT :
  124. m_value(static_cast< file_type&& >(rhs.m_value)),
  125. m_perms(static_cast< perms&& >(rhs.m_perms))
  126. {
  127. }
  128. BOOST_CXX14_CONSTEXPR file_status& operator=(file_status&& rhs) BOOST_NOEXCEPT
  129. {
  130. m_value = static_cast< file_type&& >(rhs.m_value);
  131. m_perms = static_cast< perms&& >(rhs.m_perms);
  132. return *this;
  133. }
  134. #endif
  135. // observers
  136. BOOST_CONSTEXPR file_type type() const BOOST_NOEXCEPT { return m_value; }
  137. BOOST_CONSTEXPR perms permissions() const BOOST_NOEXCEPT { return m_perms; }
  138. // modifiers
  139. BOOST_CXX14_CONSTEXPR void type(file_type v) BOOST_NOEXCEPT { m_value = v; }
  140. BOOST_CXX14_CONSTEXPR void permissions(perms prms) BOOST_NOEXCEPT { m_perms = prms; }
  141. BOOST_CONSTEXPR bool operator==(file_status const& rhs) const BOOST_NOEXCEPT
  142. {
  143. return type() == rhs.type() && permissions() == rhs.permissions();
  144. }
  145. BOOST_CONSTEXPR bool operator!=(file_status const& rhs) const BOOST_NOEXCEPT
  146. {
  147. return !(*this == rhs);
  148. }
  149. private:
  150. file_type m_value;
  151. perms m_perms;
  152. };
  153. inline BOOST_CONSTEXPR bool type_present(file_status f) BOOST_NOEXCEPT
  154. {
  155. return f.type() != filesystem::status_error;
  156. }
  157. inline BOOST_CONSTEXPR bool permissions_present(file_status f) BOOST_NOEXCEPT
  158. {
  159. return f.permissions() != filesystem::perms_not_known;
  160. }
  161. inline BOOST_CONSTEXPR bool status_known(file_status f) BOOST_NOEXCEPT
  162. {
  163. return filesystem::type_present(f) && filesystem::permissions_present(f);
  164. }
  165. inline BOOST_CONSTEXPR bool exists(file_status f) BOOST_NOEXCEPT
  166. {
  167. return f.type() != filesystem::status_error && f.type() != filesystem::file_not_found;
  168. }
  169. inline BOOST_CONSTEXPR bool is_regular_file(file_status f) BOOST_NOEXCEPT
  170. {
  171. return f.type() == filesystem::regular_file;
  172. }
  173. inline BOOST_CONSTEXPR bool is_directory(file_status f) BOOST_NOEXCEPT
  174. {
  175. return f.type() == filesystem::directory_file;
  176. }
  177. inline BOOST_CONSTEXPR bool is_symlink(file_status f) BOOST_NOEXCEPT
  178. {
  179. return f.type() == filesystem::symlink_file;
  180. }
  181. inline BOOST_CONSTEXPR bool is_block_file(file_status f) BOOST_NOEXCEPT
  182. {
  183. return f.type() == filesystem::block_file;
  184. }
  185. inline BOOST_CONSTEXPR bool is_character_file(file_status f) BOOST_NOEXCEPT
  186. {
  187. return f.type() == filesystem::character_file;
  188. }
  189. inline BOOST_CONSTEXPR bool is_fifo(file_status f) BOOST_NOEXCEPT
  190. {
  191. return f.type() == filesystem::fifo_file;
  192. }
  193. inline BOOST_CONSTEXPR bool is_socket(file_status f) BOOST_NOEXCEPT
  194. {
  195. return f.type() == filesystem::socket_file;
  196. }
  197. inline BOOST_CONSTEXPR bool is_reparse_file(file_status f) BOOST_NOEXCEPT
  198. {
  199. return f.type() == filesystem::reparse_file;
  200. }
  201. inline BOOST_CONSTEXPR bool is_other(file_status f) BOOST_NOEXCEPT
  202. {
  203. return filesystem::exists(f) && !filesystem::is_regular_file(f) && !filesystem::is_directory(f) && !filesystem::is_symlink(f);
  204. }
  205. #ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  206. BOOST_FILESYSTEM_DETAIL_DEPRECATED("Use is_regular_file() instead")
  207. inline bool is_regular(file_status f) BOOST_NOEXCEPT
  208. {
  209. return filesystem::is_regular_file(f);
  210. }
  211. #endif
  212. } // namespace filesystem
  213. } // namespace boost
  214. #include <boost/filesystem/detail/footer.hpp> // pops abi_prefix.hpp pragmas
  215. #endif // BOOST_FILESYSTEM_FILE_STATUS_HPP