BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdls_filepermissions.h
Go to the documentation of this file.
1/// @file bdls_filepermissions.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdls_filepermissions.h -*-C++-*-
8#ifndef INCLUDED_BDLS_FILEPERMISSIONS
9#define INCLUDED_BDLS_FILEPERMISSIONS
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdls_filepermissions bdls_filepermissions
15/// @brief Enumerate the file-system permission bits used across `bdl`.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdls
19/// @{
20/// @addtogroup bdls_filepermissions
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdls_filepermissions-purpose"> Purpose</a>
25/// * <a href="#bdls_filepermissions-classes"> Classes </a>
26/// * <a href="#bdls_filepermissions-description"> Description </a>
27/// * <a href="#bdls_filepermissions-enumerators"> Enumerators </a>
28/// * <a href="#bdls_filepermissions-usage"> Usage </a>
29/// * <a href="#bdls_filepermissions-example-1-composing-and-validating-permissions"> Example 1: Composing and Validating Permissions </a>
30///
31/// # Purpose {#bdls_filepermissions-purpose}
32/// Enumerate the file-system permission bits used across `bdl`.
33///
34/// # Classes {#bdls_filepermissions-classes}
35///
36/// - bdls::FilePermissions: namespace for a `std::filesystem::perms`-style enum
37///
38/// @see bdls_filesystemutil, balb_pipecontrolchannel
39///
40/// # Description {#bdls_filepermissions-description}
41/// This component provides a `struct`, `bdls::FilePermissions`,
42/// that scopes a bit-mask `enum`, `Enum`, modelled on `std::filesystem::perms`
43/// [https://en.cppreference.com/w/cpp/filesystem/perms]. The enumerators
44/// describe the standard Unix file-system permission bits (owner, group, and
45/// others read/write/execute triples, plus the "set uid", "set gid", and
46/// sticky bits) with numeric values that match the traditional Unix octal
47/// representation. This component also provides two static predicates for
48/// validating an `int` bit mask against the set of defined bits.
49///
50/// This component is intended to be used by other components that accept a
51/// permission bit mask as an `int` argument. Taking an `int` (rather than
52/// the enumeration type) lets callers pass either a familiar octal literal
53/// (for example `0666`) or a bitwise-OR of `bdls::FilePermissions` enumerators.
54/// The consuming component is expected to validate its argument by calling
55/// one of the `isValid*` predicates.
56///
57/// ## Enumerators {#bdls_filepermissions-enumerators}
58///
59///
60/// The `Enum` values are:
61/// @code
62/// Enumerator Value Meaning
63/// ----------------- -------- ----------------------------------------------
64/// k_NONE 0 No permissions.
65/// k_OWNER_READ 0400 Owner has read permission.
66/// k_OWNER_WRITE 0200 Owner has write permission.
67/// k_OWNER_EXEC 0100 Owner has execute permission.
68/// k_OWNER_ALL 0700 Owner has read, write, and execute permission.
69/// k_GROUP_READ 0040 Group has read permission.
70/// k_GROUP_WRITE 0020 Group has write permission.
71/// k_GROUP_EXEC 0010 Group has execute permission.
72/// k_GROUP_ALL 0070 Group has read, write, and execute permission.
73/// k_OTHERS_READ 0004 Others have read permission.
74/// k_OTHERS_WRITE 0002 Others have write permission.
75/// k_OTHERS_EXEC 0001 Others have execute permission.
76/// k_OTHERS_ALL 0007 Others have read, write, and execute perm.
77/// k_ALL 0777 Owner, group, and others all have full perms.
78/// k_SET_UID 04000 Set-user-ID on execution.
79/// k_SET_GID 02000 Set-group-ID on execution.
80/// k_STICKY_BIT 01000 Sticky bit.
81/// k_MASK 07777 Union of every defined bit.
82/// @endcode
83///
84/// ## Usage {#bdls_filepermissions-usage}
85///
86///
87/// This section illustrates intended use of this component.
88///
89/// ### Example 1: Composing and Validating Permissions {#bdls_filepermissions-example-1-composing-and-validating-permissions}
90///
91///
92/// A component (for instance, `balb::PipeControlChannel`) may accept an `int`
93/// permission bit mask. A caller can build such a mask by OR-ing together
94/// enumerators of `bdls::FilePermissions`. Suppose we want a file readable
95/// and writable by the owner and readable by the owning group:
96/// @code
97/// int perms = bdls::FilePermissions::k_OWNER_READ |
98/// bdls::FilePermissions::k_OWNER_WRITE |
99/// bdls::FilePermissions::k_GROUP_READ;
100/// assert(0640 == perms);
101/// @endcode
102/// The consuming component may validate the mask before using it. To reject
103/// a mask that contains any bit outside the traditional 9-bit rwx set (that
104/// is, to reject `k_SET_UID`, `k_SET_GID`, and `k_STICKY_BIT`), use
105/// `isValidBaseBits`:
106/// @code
107/// assert( bdls::FilePermissions::isValidBaseBits(0640));
108/// assert(!bdls::FilePermissions::isValidBaseBits(0640 |
109/// bdls::FilePermissions::k_SET_UID));
110/// @endcode
111/// To accept any combination of defined bits, use `isValid`:
112/// @code
113/// assert( bdls::FilePermissions::isValid(0640 |
114/// bdls::FilePermissions::k_SET_UID));
115/// assert(!bdls::FilePermissions::isValid(1 << 15));
116/// @endcode
117/// @}
118/** @} */
119/** @} */
120
121/** @addtogroup bdl
122 * @{
123 */
124/** @addtogroup bdls
125 * @{
126 */
127/** @addtogroup bdls_filepermissions
128 * @{
129 */
130
131#include <bdlscm_version.h>
132
133#include <bsl_iosfwd.h>
134
135
136namespace bdls {
137
138 // ======================
139 // struct FilePermissions
140 // ======================
141
142/// This `struct` provides a namespace for a `std::filesystem::perms`-style
143/// enumeration of file-system permission bits, and a pair of predicates
144/// for validating an `int` bit mask against that enumeration.
145///
146/// See @ref bdls_filepermissions
148
149 // TYPES
150
151 /// File-system permission bits, in the conventional Unix octal layout.
152 /// Values may be combined with the bitwise-OR operator. See the
153 /// component-level documentation for the meaning of each enumerator.
180
181 // CLASS METHODS
182
183 /// Return `true` if the specified `permissions` bit mask consists only
184 /// of the "base" nine rwx bits (owner/group/others read/write/execute, i.e., bits in `k_ALL`), and `false` otherwise.
185 ///
186 /// \note Note that this
187 /// predicate is stricter than `isValid`: `k_SET_UID`, `k_SET_GID`, and
188 /// `k_STICKY_BIT` are rejected.
189 static bool isValidBaseBits(int permissions);
190
191 /// Return `true` if the specified `permissions` bit mask consists only
192 /// of bits defined in `Enum` (i.e., bits in `k_MASK`), and `false`
193 /// otherwise. A negative value always returns `false`.
194 static bool isValid(int permissions);
195
196 /// Write the octal numeric representation of the specified permission bit
197 /// mask `value` to the specified output `stream`, and return a reference
198 /// to `stream`. Optionally specify an initial indentation `level`, whose
199 /// absolute value is incremented recursively for nested objects. If
200 /// `level` is specified, optionally specify `spacesPerLevel`, whose
201 /// absolute value indicates the number of spaces per indentation level for
202 /// this and all of its nested objects. If `level` is negative, suppress
203 /// indentation of the first line. If `spacesPerLevel` is negative, format
204 /// the entire output on one line, suppressing all but the initial
205 /// indentation (as governed by `level`). The `value` is written in octal
206 /// notation *without* the customary leading `0` prefix (for example, the
207 /// mask `0640` is written as `640`).
208 static bsl::ostream& print(bsl::ostream& stream,
210 int level = 0,
211 int spacesPerLevel = 4);
212};
213
214// FREE OPERATORS
215
216/// Write the octal numeric representation of the specified permission bit mask
217/// `value` to the specified output `stream` in a single-line format, and
218/// return a reference to `stream`. The `value` is written in octal notation
219/// *without* the customary leading `0` prefix (for example, the mask `0640` is written as `640`).
220///
221/// \note Note that this method has the same behavior as
222/// @code
223/// bdls::FilePermissions::print(stream, value, 0, -1);
224/// @endcode
225bsl::ostream& operator<<(bsl::ostream& stream,
227
228// ============================================================================
229// INLINE DEFINITIONS
230// ============================================================================
231
232 // ----------------------
233 // struct FilePermissions
234 // ----------------------
235
236// CLASS METHODS
237inline
239{
240 return 0 <= permissions
241 && 0 == (permissions & ~static_cast<int>(k_ALL));
242}
243
244inline
245bool FilePermissions::isValid(int permissions)
246{
247 return 0 <= permissions
248 && 0 == (permissions & ~static_cast<int>(k_MASK));
249}
250
251} // close package namespace
252
253// FREE OPERATORS
254inline
255bsl::ostream& bdls::operator<<(bsl::ostream& stream,
256 FilePermissions::Enum value)
257{
258 return FilePermissions::print(stream, value, 0, -1);
259}
260
261
262
263#endif
264
265// ----------------------------------------------------------------------------
266// Copyright 2026 Bloomberg Finance L.P.
267//
268// Licensed under the Apache License, Version 2.0 (the "License");
269// you may not use this file except in compliance with the License.
270// You may obtain a copy of the License at
271//
272// http://www.apache.org/licenses/LICENSE-2.0
273//
274// Unless required by applicable law or agreed to in writing, software
275// distributed under the License is distributed on an "AS IS" BASIS,
276// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
277// See the License for the specific language governing permissions and
278// limitations under the License.
279// ----------------------------- END-OF-FILE ----------------------------------
280
281/** @} */
282/** @} */
283/** @} */
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdls_fdstreambuf.h:412
bsl::ostream & operator<<(bsl::ostream &stream, FilePermissions::Enum value)
Definition bdls_filepermissions.h:147
Enum
Definition bdls_filepermissions.h:154
@ k_GROUP_EXEC
Definition bdls_filepermissions.h:164
@ k_STICKY_BIT
Definition bdls_filepermissions.h:176
@ k_OWNER_ALL
Definition bdls_filepermissions.h:160
@ k_GROUP_ALL
Definition bdls_filepermissions.h:165
@ k_MASK
Definition bdls_filepermissions.h:178
@ k_SET_UID
Definition bdls_filepermissions.h:174
@ k_OWNER_EXEC
Definition bdls_filepermissions.h:159
@ k_OTHERS_ALL
Definition bdls_filepermissions.h:170
@ k_GROUP_READ
Definition bdls_filepermissions.h:162
@ k_OWNER_READ
Definition bdls_filepermissions.h:157
@ k_OTHERS_WRITE
Definition bdls_filepermissions.h:168
@ k_ALL
Definition bdls_filepermissions.h:172
@ k_OWNER_WRITE
Definition bdls_filepermissions.h:158
@ k_OTHERS_EXEC
Definition bdls_filepermissions.h:169
@ k_NONE
Definition bdls_filepermissions.h:155
@ k_SET_GID
Definition bdls_filepermissions.h:175
@ k_OTHERS_READ
Definition bdls_filepermissions.h:167
@ k_GROUP_WRITE
Definition bdls_filepermissions.h:163
static bsl::ostream & print(bsl::ostream &stream, FilePermissions::Enum value, int level=0, int spacesPerLevel=4)
static bool isValidBaseBits(int permissions)
Definition bdls_filepermissions.h:238
static bool isValid(int permissions)
Definition bdls_filepermissions.h:245