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