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

source code of boost/libs/filesystem/include/boost/filesystem/file_status.hpp