file_status.hpp 8.3 KB

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