file_status.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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_FILESYSTEM3_FILE_STATUS_HPP
  12. #define BOOST_FILESYSTEM3_FILE_STATUS_HPP
  13. #include <boost/config.hpp>
  14. # if defined( BOOST_NO_STD_WSTRING )
  15. # error Configuration not supported: Boost.Filesystem V3 and later requires std::wstring support
  16. # endif
  17. #include <boost/filesystem/config.hpp>
  18. #include <boost/detail/bitmask.hpp>
  19. #include <boost/config/abi_prefix.hpp> // must be the last #include
  20. //--------------------------------------------------------------------------------------//
  21. namespace boost {
  22. namespace filesystem {
  23. //--------------------------------------------------------------------------------------//
  24. // file_type //
  25. //--------------------------------------------------------------------------------------//
  26. enum file_type
  27. {
  28. status_error,
  29. # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  30. status_unknown = status_error,
  31. # endif
  32. file_not_found,
  33. regular_file,
  34. directory_file,
  35. // the following may not apply to some operating systems or file systems
  36. symlink_file,
  37. block_file,
  38. character_file,
  39. fifo_file,
  40. socket_file,
  41. reparse_file, // Windows: FILE_ATTRIBUTE_REPARSE_POINT that is not a symlink
  42. type_unknown, // file does exist, but isn't one of the above types or
  43. // we don't have strong enough permission to find its type
  44. _detail_directory_symlink // internal use only; never exposed to users
  45. };
  46. //--------------------------------------------------------------------------------------//
  47. // perms //
  48. //--------------------------------------------------------------------------------------//
  49. enum perms
  50. {
  51. no_perms = 0, // file_not_found is no_perms rather than perms_not_known
  52. // POSIX equivalent macros given in comments.
  53. // Values are from POSIX and are given in octal per the POSIX standard.
  54. // permission bits
  55. owner_read = 0400, // S_IRUSR, Read permission, owner
  56. owner_write = 0200, // S_IWUSR, Write permission, owner
  57. owner_exe = 0100, // S_IXUSR, Execute/search permission, owner
  58. owner_all = 0700, // S_IRWXU, Read, write, execute/search by owner
  59. group_read = 040, // S_IRGRP, Read permission, group
  60. group_write = 020, // S_IWGRP, Write permission, group
  61. group_exe = 010, // S_IXGRP, Execute/search permission, group
  62. group_all = 070, // S_IRWXG, Read, write, execute/search by group
  63. others_read = 04, // S_IROTH, Read permission, others
  64. others_write = 02, // S_IWOTH, Write permission, others
  65. others_exe = 01, // S_IXOTH, Execute/search permission, others
  66. others_all = 07, // S_IRWXO, Read, write, execute/search by others
  67. all_all = 0777, // owner_all|group_all|others_all
  68. // other POSIX bits
  69. set_uid_on_exe = 04000, // S_ISUID, Set-user-ID on execution
  70. set_gid_on_exe = 02000, // S_ISGID, Set-group-ID on execution
  71. sticky_bit = 01000, // S_ISVTX,
  72. // (POSIX XSI) On directories, restricted deletion flag
  73. // (V7) 'sticky bit': save swapped text even after use
  74. // (SunOS) On non-directories: don't cache this file
  75. // (SVID-v4.2) On directories: restricted deletion flag
  76. // Also see http://en.wikipedia.org/wiki/Sticky_bit
  77. perms_mask = 07777, // all_all|set_uid_on_exe|set_gid_on_exe|sticky_bit
  78. perms_not_known = 0xFFFF, // present when directory_entry cache not loaded
  79. // options for permissions() function
  80. add_perms = 0x1000, // adds the given permission bits to the current bits
  81. remove_perms = 0x2000, // removes the given permission bits from the current bits;
  82. // choose add_perms or remove_perms, not both; if neither add_perms
  83. // nor remove_perms is given, replace the current bits with
  84. // the given bits.
  85. symlink_perms = 0x4000, // on POSIX, don't resolve symlinks; implied on Windows
  86. // BOOST_BITMASK op~ casts to int32_least_t, producing invalid enum values
  87. _detail_extend_perms_32_1 = 0x7fffffff,
  88. _detail_extend_perms_32_2 = -0x7fffffff-1
  89. };
  90. BOOST_BITMASK(perms)
  91. //--------------------------------------------------------------------------------------//
  92. // file_status //
  93. //--------------------------------------------------------------------------------------//
  94. class file_status
  95. {
  96. public:
  97. BOOST_CONSTEXPR file_status() BOOST_NOEXCEPT :
  98. m_value(status_error), m_perms(perms_not_known)
  99. {
  100. }
  101. explicit BOOST_CONSTEXPR file_status(file_type v) BOOST_NOEXCEPT :
  102. m_value(v), m_perms(perms_not_known)
  103. {
  104. }
  105. BOOST_CONSTEXPR file_status(file_type v, perms prms) BOOST_NOEXCEPT :
  106. m_value(v), m_perms(prms)
  107. {
  108. }
  109. // As of October 2015 the interaction between noexcept and =default is so troublesome
  110. // for VC++, GCC, and probably other compilers, that =default is not used with noexcept
  111. // functions. GCC is not even consistent for the same release on different platforms.
  112. BOOST_CONSTEXPR file_status(const file_status& rhs) BOOST_NOEXCEPT :
  113. m_value(rhs.m_value), m_perms(rhs.m_perms)
  114. {
  115. }
  116. BOOST_CXX14_CONSTEXPR file_status& operator=(const file_status& rhs) BOOST_NOEXCEPT
  117. {
  118. m_value = rhs.m_value;
  119. m_perms = rhs.m_perms;
  120. return *this;
  121. }
  122. # if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  123. // Note: std::move is not constexpr in C++11, that's why we're not using it here
  124. BOOST_CONSTEXPR file_status(file_status&& rhs) BOOST_NOEXCEPT :
  125. m_value(static_cast< file_type&& >(rhs.m_value)), m_perms(static_cast< enum 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< enum 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==(const file_status& rhs) const BOOST_NOEXCEPT
  142. {
  143. return type() == rhs.type() && permissions() == rhs.permissions();
  144. }
  145. BOOST_CONSTEXPR bool operator!=(const file_status& rhs) const BOOST_NOEXCEPT
  146. {
  147. return !(*this == rhs);
  148. }
  149. private:
  150. file_type m_value;
  151. enum perms m_perms;
  152. };
  153. inline BOOST_CONSTEXPR bool type_present(file_status f) BOOST_NOEXCEPT
  154. {
  155. return f.type() != status_error;
  156. }
  157. inline BOOST_CONSTEXPR bool permissions_present(file_status f) BOOST_NOEXCEPT
  158. {
  159. return f.permissions() != 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() != status_error && f.type() != file_not_found;
  168. }
  169. inline BOOST_CONSTEXPR bool is_regular_file(file_status f) BOOST_NOEXCEPT
  170. {
  171. return f.type() == regular_file;
  172. }
  173. inline BOOST_CONSTEXPR bool is_directory(file_status f) BOOST_NOEXCEPT
  174. {
  175. return f.type() == directory_file;
  176. }
  177. inline BOOST_CONSTEXPR bool is_symlink(file_status f) BOOST_NOEXCEPT
  178. {
  179. return f.type() == symlink_file;
  180. }
  181. inline BOOST_CONSTEXPR bool is_other(file_status f) BOOST_NOEXCEPT
  182. {
  183. return filesystem::exists(f) && !filesystem::is_regular_file(f)
  184. && !filesystem::is_directory(f) && !filesystem::is_symlink(f);
  185. }
  186. # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  187. inline bool is_regular(file_status f) BOOST_NOEXCEPT { return filesystem::is_regular_file(f); }
  188. # endif
  189. } // namespace filesystem
  190. } // namespace boost
  191. #include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
  192. #endif // BOOST_FILESYSTEM3_FILE_STATUS_HPP