13#include <cosmos/BitMask.hxx>
14#include <cosmos/dso_export.h>
15#include <cosmos/utils.hxx>
35 STDOUT = STDOUT_FILENO,
36 STDERR = STDERR_FILENO,
54 WRITE_ONLY = O_WRONLY,
114 auto ret =
static_cast<mode_t
>(a) &
static_cast<mode_t
>(b);
115 return static_cast<ModeT>(ret);
129 OWNER_READ = S_IRUSR,
130 OWNER_WRITE = S_IWUSR,
131 OWNER_EXEC = S_IXUSR,
133 GROUP_READ = S_IRGRP,
134 GROUP_WRITE = S_IWGRP,
135 GROUP_EXEC = S_IXGRP,
137 OTHER_READ = S_IROTH,
138 OTHER_WRITE = S_IWOTH,
139 OTHER_EXEC = S_IXOTH,
180 explicit FileType(
const FileT raw) : m_raw{raw} {}
182 explicit FileType(
const ModeT raw) :
183 m_raw{static_cast<FileT>(raw & ModeT::MODE_T_TYPE_MASK)}
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; }
194 auto raw()
const {
return m_raw; }
201 char symbolic()
const;
203 bool operator==(
const FileType &other)
const {
204 return m_raw == other.m_raw;
207 bool operator!=(
const FileType &other)
const {
208 return !(*
this == other);
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]; }
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]; }
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]; }
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]; }
256 bool canAnyRead()
const {
257 return m_mode.anyOf({
258 FileModeBit::OWNER_READ,
259 FileModeBit::GROUP_READ,
260 FileModeBit::OTHER_READ});
263 bool canAnyWrite()
const {
264 return m_mode.anyOf({
265 FileModeBit::OWNER_WRITE,
266 FileModeBit::GROUP_WRITE,
267 FileModeBit::OTHER_WRITE});
270 bool canAnyExec()
const {
271 return m_mode.anyOf({
272 FileModeBit::OWNER_EXEC,
273 FileModeBit::GROUP_EXEC,
274 FileModeBit::OTHER_EXEC});
287 std::string symbolic()
const;
289 ModeT raw()
const {
return ModeT{m_mode.raw()}; }
291 bool operator==(
const FileMode &other)
const {
292 return m_mode == other.m_mode;
295 bool operator!=(
const FileMode &other)
const {
296 return !(*
this == other);
A typesafe bit mask representation using class enums.
Represents the mode bits portion of a ModeT.
FileMode(const ModeT raw=ModeT::NONE)
Constructs a FileMode from the given raw input.
FileModeBits m_mode
bitmask for mode bits
FileModeBits & mask()
Returns the complete bitmask object.
FileMode(const FileModeBits mask)
Constructs a FileMode from the given bitmask object.
Convenience wrapper around FileT.
FileT
File type portion as found in a ModeT.
Strong template type to wrap boolean values in a named type.
ModeT operator&(const ModeT a, const ModeT b)
Support bit masking operations on ModeT for extracting type and mode parts.
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.
@ MAX_FD
maximum file descriptor number; useful in fs::close_range().
DeviceID
A device file identification type (consists of major:minor parts).
OpenFlag
Strong enum type wrapper for file descriptor settings on top of the basic OpenMode....
@ 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.
Inode
A unique file number for a file on a block device.
ModeT
Combined file type and mode bits of a file (as found in st_mode struct stat).
@ 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.
@ SOCKET
used for generic socket options and UNIX domain sockets