permissions.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright Lingxi Li 2015.
  3. * Copyright Andrey Semashev 2015.
  4. * Distributed under the Boost Software License, Version 1.0.
  5. * (See accompanying file LICENSE_1_0.txt or copy at
  6. * http://www.boost.org/LICENSE_1_0.txt)
  7. */
  8. /*!
  9. * \file permissions.hpp
  10. * \author Lingxi Li
  11. * \author Andrey Semashev
  12. * \date 14.10.2015
  13. *
  14. * The header contains an abstraction wrapper for security permissions.
  15. */
  16. #ifndef BOOST_LOG_UTILITY_PERMISSIONS_HPP_INCLUDED_
  17. #define BOOST_LOG_UTILITY_PERMISSIONS_HPP_INCLUDED_
  18. #include <boost/log/detail/config.hpp>
  19. #include <boost/log/detail/header.hpp>
  20. #ifdef BOOST_HAS_PRAGMA_ONCE
  21. #pragma once
  22. #endif
  23. #ifdef BOOST_WINDOWS
  24. extern "C" {
  25. struct _SECURITY_ATTRIBUTES;
  26. }
  27. #endif // BOOST_WINDOWS
  28. namespace boost {
  29. #ifdef BOOST_WINDOWS
  30. #if defined(BOOST_GCC) && BOOST_GCC >= 40600
  31. #pragma GCC diagnostic push
  32. // type attributes ignored after type is already defined
  33. #pragma GCC diagnostic ignored "-Wattributes"
  34. #endif
  35. namespace winapi {
  36. struct BOOST_LOG_MAY_ALIAS _SECURITY_ATTRIBUTES;
  37. }
  38. #if defined(BOOST_GCC) && BOOST_GCC >= 40600
  39. #pragma GCC diagnostic pop
  40. #endif
  41. #endif
  42. namespace interprocess {
  43. class permissions;
  44. } // namespace interprocess
  45. BOOST_LOG_OPEN_NAMESPACE
  46. /*!
  47. * \brief Access permissions wrapper.
  48. *
  49. * On Windows platforms, it represents a pointer to \c SECURITY_ATTRIBUTES. The user is responsible
  50. * for allocating and reclaiming resources associated with the pointer, \c permissions instance does
  51. * not own them.
  52. *
  53. * On POSIX platforms, it represents a \c mode_t value.
  54. */
  55. class permissions
  56. {
  57. public:
  58. #if defined(BOOST_LOG_DOXYGEN_PASS)
  59. //! The type of security permissions, specific to the operating system
  60. typedef implementation_defined native_type;
  61. #elif defined(BOOST_WINDOWS)
  62. typedef ::_SECURITY_ATTRIBUTES* native_type;
  63. #else
  64. // Equivalent to POSIX mode_t
  65. typedef unsigned int native_type;
  66. #endif
  67. #if !defined(BOOST_LOG_DOXYGEN_PASS)
  68. private:
  69. native_type m_perms;
  70. #endif
  71. public:
  72. /*!
  73. * Default constructor. The method constructs an object that represents
  74. * a null \c SECURITY_ATTRIBUTES pointer on Windows platforms, and a
  75. * \c mode_t value \c 0644 on POSIX platforms.
  76. */
  77. permissions() BOOST_NOEXCEPT
  78. {
  79. set_default();
  80. }
  81. /*!
  82. * Copy constructor.
  83. */
  84. permissions(permissions const& that) BOOST_NOEXCEPT : m_perms(that.m_perms)
  85. {
  86. }
  87. /*!
  88. * Copy assignment.
  89. */
  90. permissions& operator=(permissions const& that) BOOST_NOEXCEPT
  91. {
  92. m_perms = that.m_perms;
  93. return *this;
  94. }
  95. /*!
  96. * Initializing constructor.
  97. */
  98. permissions(native_type perms) BOOST_NOEXCEPT : m_perms(perms)
  99. {
  100. }
  101. #ifdef BOOST_WINDOWS
  102. permissions(boost::winapi::_SECURITY_ATTRIBUTES* perms) BOOST_NOEXCEPT : m_perms(reinterpret_cast< native_type >(perms))
  103. {
  104. }
  105. #endif
  106. /*!
  107. * Initializing constructor.
  108. */
  109. BOOST_LOG_API permissions(boost::interprocess::permissions const& perms) BOOST_NOEXCEPT;
  110. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  111. /*!
  112. * Move constructor.
  113. */
  114. permissions(permissions&& that) BOOST_NOEXCEPT : m_perms(that.m_perms)
  115. {
  116. that.set_default();
  117. }
  118. /*!
  119. * Move assignment.
  120. */
  121. permissions& operator=(permissions&& that) BOOST_NOEXCEPT
  122. {
  123. m_perms = that.m_perms;
  124. that.set_default();
  125. return *this;
  126. }
  127. #endif
  128. /*!
  129. * Sets permissions from the OS-specific permissions.
  130. */
  131. void set_native(native_type perms) BOOST_NOEXCEPT
  132. {
  133. m_perms = perms;
  134. }
  135. /*!
  136. * Returns the underlying OS-specific permissions.
  137. */
  138. native_type get_native() const BOOST_NOEXCEPT
  139. {
  140. return m_perms;
  141. }
  142. /*!
  143. * Sets the default permissions, which are equivalent to \c NULL \c SECURITY_ATTRIBUTES
  144. * on Windows and \c 0644 on POSIX platforms.
  145. */
  146. void set_default() BOOST_NOEXCEPT
  147. {
  148. #if defined(BOOST_WINDOWS)
  149. m_perms = 0;
  150. #else
  151. m_perms = 0644;
  152. #endif
  153. }
  154. /*!
  155. * Sets unrestricted permissions, which are equivalent to \c SECURITY_ATTRIBUTES with \c NULL DACL
  156. * on Windows and \c 0666 on POSIX platforms.
  157. */
  158. void set_unrestricted()
  159. {
  160. #if defined(BOOST_WINDOWS)
  161. m_perms = get_unrestricted_security_attributes();
  162. #else
  163. m_perms = 0666;
  164. #endif
  165. }
  166. /*!
  167. * The method swaps the object with \a that.
  168. *
  169. * \param that The other object to swap with.
  170. */
  171. void swap(permissions& that) BOOST_NOEXCEPT
  172. {
  173. native_type perms = m_perms;
  174. m_perms = that.m_perms;
  175. that.m_perms = perms;
  176. }
  177. //! Swaps the two \c permissions objects.
  178. friend void swap(permissions& a, permissions& b) BOOST_NOEXCEPT
  179. {
  180. a.swap(b);
  181. }
  182. #if !defined(BOOST_LOG_DOXYGEN_PASS) && defined(BOOST_WINDOWS)
  183. private:
  184. static BOOST_LOG_API native_type get_unrestricted_security_attributes();
  185. #endif
  186. };
  187. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  188. } // namespace boost
  189. #include <boost/log/detail/footer.hpp>
  190. #endif // BOOST_LOG_UTILITY_PERMISSIONS_HPP_INCLUDED_