permissions.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. namespace winapi {
  31. struct BOOST_LOG_MAY_ALIAS _SECURITY_ATTRIBUTES;
  32. }
  33. #endif
  34. namespace interprocess {
  35. class permissions;
  36. } // namespace interprocess
  37. BOOST_LOG_OPEN_NAMESPACE
  38. /*!
  39. * \brief Access permissions wrapper.
  40. *
  41. * On Windows platforms, it represents a pointer to \c SECURITY_ATTRIBUTES. The user is responsible
  42. * for allocating and reclaiming resources associated with the pointer, \c permissions instance does
  43. * not own them.
  44. *
  45. * On POSIX platforms, it represents a \c mode_t value.
  46. */
  47. class permissions
  48. {
  49. public:
  50. #if defined(BOOST_LOG_DOXYGEN_PASS)
  51. //! The type of security permissions, specific to the operating system
  52. typedef implementation_defined native_type;
  53. #elif defined(BOOST_WINDOWS)
  54. typedef ::_SECURITY_ATTRIBUTES* native_type;
  55. #else
  56. // Equivalent to POSIX mode_t
  57. typedef unsigned int native_type;
  58. #endif
  59. #if !defined(BOOST_LOG_DOXYGEN_PASS)
  60. private:
  61. native_type m_perms;
  62. #endif
  63. public:
  64. /*!
  65. * Default constructor. The method constructs an object that represents
  66. * a null \c SECURITY_ATTRIBUTES pointer on Windows platforms, and a
  67. * \c mode_t value \c 0644 on POSIX platforms.
  68. */
  69. permissions() BOOST_NOEXCEPT
  70. {
  71. set_default();
  72. }
  73. /*!
  74. * Copy constructor.
  75. */
  76. permissions(permissions const& that) BOOST_NOEXCEPT : m_perms(that.m_perms)
  77. {
  78. }
  79. /*!
  80. * Copy assignment.
  81. */
  82. permissions& operator=(permissions const& that) BOOST_NOEXCEPT
  83. {
  84. m_perms = that.m_perms;
  85. return *this;
  86. }
  87. /*!
  88. * Initializing constructor.
  89. */
  90. permissions(native_type perms) BOOST_NOEXCEPT : m_perms(perms)
  91. {
  92. }
  93. #ifdef BOOST_WINDOWS
  94. permissions(boost::winapi::_SECURITY_ATTRIBUTES* perms) BOOST_NOEXCEPT : m_perms(reinterpret_cast< native_type >(perms))
  95. {
  96. }
  97. #endif
  98. /*!
  99. * Initializing constructor.
  100. */
  101. BOOST_LOG_API permissions(boost::interprocess::permissions const& perms) BOOST_NOEXCEPT;
  102. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  103. /*!
  104. * Move constructor.
  105. */
  106. permissions(permissions&& that) BOOST_NOEXCEPT : m_perms(that.m_perms)
  107. {
  108. that.set_default();
  109. }
  110. /*!
  111. * Move assignment.
  112. */
  113. permissions& operator=(permissions&& that) BOOST_NOEXCEPT
  114. {
  115. m_perms = that.m_perms;
  116. that.set_default();
  117. return *this;
  118. }
  119. #endif
  120. /*!
  121. * Sets permissions from the OS-specific permissions.
  122. */
  123. void set_native(native_type perms) BOOST_NOEXCEPT
  124. {
  125. m_perms = perms;
  126. }
  127. /*!
  128. * Returns the underlying OS-specific permissions.
  129. */
  130. native_type get_native() const BOOST_NOEXCEPT
  131. {
  132. return m_perms;
  133. }
  134. /*!
  135. * Sets the default permissions, which are equivalent to \c NULL \c SECURITY_ATTRIBUTES
  136. * on Windows and \c 0644 on POSIX platforms.
  137. */
  138. void set_default() BOOST_NOEXCEPT
  139. {
  140. #if defined(BOOST_WINDOWS)
  141. m_perms = 0;
  142. #else
  143. m_perms = 0644;
  144. #endif
  145. }
  146. /*!
  147. * Sets unrestricted permissions, which are equivalent to \c SECURITY_ATTRIBUTES with \c NULL DACL
  148. * on Windows and \c 0666 on POSIX platforms.
  149. */
  150. void set_unrestricted()
  151. {
  152. #if defined(BOOST_WINDOWS)
  153. m_perms = get_unrestricted_security_attributes();
  154. #else
  155. m_perms = 0666;
  156. #endif
  157. }
  158. /*!
  159. * The method swaps the object with \a that.
  160. *
  161. * \param that The other object to swap with.
  162. */
  163. void swap(permissions& that) BOOST_NOEXCEPT
  164. {
  165. native_type perms = m_perms;
  166. m_perms = that.m_perms;
  167. that.m_perms = perms;
  168. }
  169. //! Swaps the two \c permissions objects.
  170. friend void swap(permissions& a, permissions& b) BOOST_NOEXCEPT
  171. {
  172. a.swap(b);
  173. }
  174. #if !defined(BOOST_LOG_DOXYGEN_PASS) && defined(BOOST_WINDOWS)
  175. private:
  176. static BOOST_LOG_API native_type get_unrestricted_security_attributes();
  177. #endif
  178. };
  179. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  180. } // namespace boost
  181. #include <boost/log/detail/footer.hpp>
  182. #endif // BOOST_LOG_UTILITY_PERMISSIONS_HPP_INCLUDED_