libcosmos
Linux C++ System Programming Library
All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
types.hxx
Go to the documentation of this file.
1#pragma once
2
3// Linux
4#include <fcntl.h>
5#include <sys/stat.h>
6#include <unistd.h>
7
8// C++
9#include <iosfwd>
10#include <string>
11
12// cosmos
13#include <cosmos/BitMask.hxx>
14#include <cosmos/dso_export.h>
15#include <cosmos/utils.hxx>
16
23namespace cosmos {
24
27
30
32enum class FileNum : int {
33 INVALID = -1,
34 STDIN = STDIN_FILENO,
35 STDOUT = STDOUT_FILENO,
36 STDERR = STDERR_FILENO,
38 AT_CWD = AT_FDCWD,
40 MAX_FD = ~int(0)
41};
42
44enum class Inode : ino_t {
45};
46
48enum class DeviceID : dev_t {
49};
50
52enum class OpenMode : int {
53 READ_ONLY = O_RDONLY,
54 WRITE_ONLY = O_WRONLY,
55 READ_WRITE = O_RDWR
56};
57
59enum class OpenFlag : int {
61 APPEND = O_APPEND,
63 ASYNC = O_ASYNC,
65 CLOEXEC = O_CLOEXEC,
67 CREATE = O_CREAT,
69 DIRECT = O_DIRECT,
71 DIRECTORY = O_DIRECTORY,
73 DSYNC = O_DSYNC,
75 EXCLUSIVE = O_EXCL,
77 NOATIME = O_NOATIME,
79 NO_CONTROLLING_TTY = O_NOCTTY,
81 NOFOLLOW = O_NOFOLLOW,
83 NONBLOCK = O_NONBLOCK,
86 PATH = O_PATH,
88 SYNC = O_SYNC,
90 TMPFILE = O_TMPFILE,
92 TRUNCATE = O_TRUNC
93};
94
97
99
106enum class ModeT : mode_t {
107 NONE = 0,
108 MODE_T_TYPE_MASK = S_IFMT,
109 MODE_T_MODE_MASK = ~static_cast<mode_t>(S_IFMT)
110};
111
113inline ModeT operator&(const ModeT a, const ModeT b) {
114 auto ret = static_cast<mode_t>(a) & static_cast<mode_t>(b);
115 return static_cast<ModeT>(ret);
116}
117
119
125enum class FileModeBit : mode_t {
126 SETUID = S_ISUID, // set user-id bit
127 SETGID = S_ISGID, // set group-id bit
128 STICKY = S_ISVTX, // only has a meaning for directory, typically set on /tmp
129 OWNER_READ = S_IRUSR,
130 OWNER_WRITE = S_IWUSR,
131 OWNER_EXEC = S_IXUSR,
132 OWNER_ALL = S_IRWXU,
133 GROUP_READ = S_IRGRP,
134 GROUP_WRITE = S_IWGRP,
135 GROUP_EXEC = S_IXGRP,
136 GROUP_ALL = S_IRWXG,
137 OTHER_READ = S_IROTH,
138 OTHER_WRITE = S_IWOTH,
139 OTHER_EXEC = S_IXOTH,
140 OTHER_ALL = S_IRWXO
141};
142
145
147
151class COSMOS_API FileType {
152public: // types
153
155
168 enum FileT : mode_t {
169 NONE = 0,
170 SOCKET = S_IFSOCK,
171 LINK = S_IFLNK,
172 REGULAR = S_IFREG,
173 BLOCKDEV = S_IFBLK,
174 DIRECTORY = S_IFDIR,
175 CHARDEV = S_IFCHR,
176 FIFO = S_IFIFO
177 };
178
179public:
180 explicit FileType(const FileT raw) : m_raw{raw} {}
181
182 explicit FileType(const ModeT raw) :
183 m_raw{static_cast<FileT>(raw & ModeT::MODE_T_TYPE_MASK)}
184 {}
185
186 bool isRegular() const { return m_raw == REGULAR; }
187 bool isDirectory() const { return m_raw == DIRECTORY; }
188 bool isCharDev() const { return m_raw == CHARDEV; }
189 bool isBlockDev() const { return m_raw == BLOCKDEV; }
190 bool isFIFO() const { return m_raw == FIFO; }
191 bool isLink() const { return m_raw == LINK; }
192 bool isSocket() const { return m_raw == SOCKET; }
193
194 auto raw() const { return m_raw; }
195
197
201 char symbolic() const;
202
203 bool operator==(const FileType &other) const {
204 return m_raw == other.m_raw;
205 }
206
207 bool operator!=(const FileType &other) const {
208 return !(*this == other);
209 }
210
211protected: // data
212
213 FileT m_raw;
214};
215
217
221class COSMOS_API FileMode {
222public:
224 explicit FileMode(const FileModeBits mask) :
225 m_mode{mask}
226 {}
227
229
236 FileMode(const ModeT raw = ModeT::NONE) :
237 m_mode{static_cast<FileModeBit>(raw & ModeT::MODE_T_MODE_MASK)}
238 {}
239
240 bool isSetUID() const { return m_mode[FileModeBit::SETUID]; }
241 bool isSetGID() const { return m_mode[FileModeBit::SETGID]; }
242 bool isSticky() const { return m_mode[FileModeBit::STICKY]; }
243
244 bool canOwnerRead() const { return m_mode[FileModeBit::OWNER_READ]; }
245 bool canOwnerWrite() const { return m_mode[FileModeBit::OWNER_WRITE]; }
246 bool canOwnerExec() const { return m_mode[FileModeBit::OWNER_EXEC]; }
247
248 bool canGroupRead() const { return m_mode[FileModeBit::GROUP_READ]; }
249 bool canGroupWrite() const { return m_mode[FileModeBit::GROUP_WRITE]; }
250 bool canGroupExec() const { return m_mode[FileModeBit::GROUP_EXEC]; }
251
252 bool canOthersRead() const { return m_mode[FileModeBit::OTHER_READ]; }
253 bool canOthersWrite() const { return m_mode[FileModeBit::OTHER_WRITE]; }
254 bool canOthersExec() const { return m_mode[FileModeBit::OTHER_EXEC]; }
255
256 bool canAnyRead() const {
257 return m_mode.anyOf({
258 FileModeBit::OWNER_READ,
259 FileModeBit::GROUP_READ,
260 FileModeBit::OTHER_READ});
261 }
262
263 bool canAnyWrite() const {
264 return m_mode.anyOf({
265 FileModeBit::OWNER_WRITE,
266 FileModeBit::GROUP_WRITE,
267 FileModeBit::OTHER_WRITE});
268 }
269
270 bool canAnyExec() const {
271 return m_mode.anyOf({
272 FileModeBit::OWNER_EXEC,
273 FileModeBit::GROUP_EXEC,
274 FileModeBit::OTHER_EXEC});
275 }
276
278 FileModeBits& mask() { return m_mode; }
279 const FileModeBits& mask() const { return m_mode; }
280
282
287 std::string symbolic() const;
288
289 ModeT raw() const { return ModeT{m_mode.raw()}; }
290
291 bool operator==(const FileMode &other) const {
292 return m_mode == other.m_mode;
293 }
294
295 bool operator!=(const FileMode &other) const {
296 return !(*this == other);
297 }
298
299protected: // data
300
303};
304
305} // end ns
306
308COSMOS_API std::ostream& operator<<(std::ostream &o, const cosmos::FileMode mode);
310COSMOS_API std::ostream& operator<<(std::ostream &o, const cosmos::FileType type);
312COSMOS_API std::ostream& operator<<(std::ostream &o, const cosmos::OpenFlags flags);
A typesafe bit mask representation using class enums.
Definition BitMask.hxx:19
Represents the mode bits portion of a ModeT.
Definition types.hxx:221
FileMode(const ModeT raw=ModeT::NONE)
Constructs a FileMode from the given raw input.
Definition types.hxx:236
FileModeBits m_mode
bitmask for mode bits
Definition types.hxx:302
FileModeBits & mask()
Returns the complete bitmask object.
Definition types.hxx:278
FileMode(const FileModeBits mask)
Constructs a FileMode from the given bitmask object.
Definition types.hxx:224
Convenience wrapper around FileT.
Definition types.hxx:151
FileT
File type portion as found in a ModeT.
Definition types.hxx:168
Strong template type to wrap boolean values in a named type.
Definition utils.hxx:50
ModeT operator&(const ModeT a, const ModeT b)
Support bit masking operations on ModeT for extracting type and mode parts.
Definition types.hxx:113
COSMOS_API std::ostream & operator<<(std::ostream &o, const cosmos::FileMode mode)
Outputs a friendly version of the FileMode information onto the stream.
FileNum
Primitive file descriptor.
Definition types.hxx:32
@ MAX_FD
maximum file descriptor number; useful in fs::close_range().
DeviceID
A device file identification type (consists of major:minor parts).
Definition types.hxx:48
OpenFlag
Strong enum type wrapper for file descriptor settings on top of the basic OpenMode....
Definition types.hxx:59
@ TRUNCATE
If write access was requested and is allowed then an already existing file object is truncated to zer...
@ DSYNC
Use synchronous write operation, after write() returns everything should be written to disk.
@ SYNC
Similar to DSYNC, see man page.
@ DIRECTORY
Require the path to refer to a directory.
@ CREATE
Create the file if it doesn't exist (file mode required as well).
@ NOFOLLOW
Don't follow symlinks in the final path component.
@ APPEND
Writes will always happen at the end of the file.
@ NO_CONTROLLING_TTY
If the file refers to a terminal, don't make it the controlling terminal of the calling process.
@ DIRECT
Bypass Kernel side caching.
@ NOATIME
Don't update the access time of the file if certain preconditions are fulfilled.
@ NONBLOCK
Attempt to open the file in non-blocking mode causing I/O operations not to block.
@ TMPFILE
Attempt to create an unnamed temporary file, path needs to specify the directory where to create it.
@ ASYNC
Enable signal driven I/O for certain file types.
@ EXCLUSIVE
Use this in conjunction with CREATE to make sure the file gets newly created.
FileModeBit
Bitmask values for file mode bits.
Definition types.hxx:125
Inode
A unique file number for a file on a block device.
Definition types.hxx:44
ModeT
Combined file type and mode bits of a file (as found in st_mode struct stat).
Definition types.hxx:106
@ MODE_T_MODE_MASK
masks all mode bits
@ MODE_T_TYPE_MASK
masks all type bits
OpenMode
Strong enum type wrapper for the basic open() mode flag.
Definition types.hxx:52
@ SOCKET
used for generic socket options and UNIX domain sockets