libcosmos
Linux C++ System Programming Library
All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
filesystem.hxx
Go to the documentation of this file.
1#pragma once
2
3// Linux
4#include <linux/close_range.h>
5
6// C++
7#include <optional>
8#include <string>
9#include <string_view>
10#include <utility>
11
12// cosmos
13#include <cosmos/SysString.hxx>
14#include <cosmos/dso_export.h>
15#include <cosmos/error/errno.hxx>
16#include <cosmos/fs/DirFD.hxx>
17#include <cosmos/fs/FileDescriptor.hxx>
18#include <cosmos/fs/types.hxx>
19#include <cosmos/types.hxx>
20
29namespace cosmos::fs {
30
32
41COSMOS_API FileDescriptor open(
42 const SysString path, const OpenMode mode,
43 const OpenFlags flags, const std::optional<FileMode> fmode = {});
44
46
62COSMOS_API FileDescriptor open_at(
63 const DirFD dir_fd, const SysString path,
64 const OpenMode mode, const OpenFlags flags,
65 const std::optional<FileMode> fmode = {});
66
67enum class CloseRangeFlag : unsigned int {
69 CLOEXEC = CLOSE_RANGE_CLOEXEC,
71 UNSHARE = CLOSE_RANGE_UNSHARE
72};
73
76
78
94COSMOS_API void close_range(const FileNum first,
95 const FileNum last = FileNum::MAX_FD,
96 const CloseRangeFlags flags = CloseRangeFlags{});
97
99
127COSMOS_API std::pair<FileDescriptor, std::string> make_tempfile(
128 const SysString _template, const OpenFlags flags = OpenFlags{OpenFlag::CLOEXEC});
129
131
146COSMOS_API std::string make_tempdir(const SysString _template);
147
149
170COSMOS_API void make_fifo(const SysString path, const FileMode mode);
171
173
181COSMOS_API void make_fifo_at(const DirFD dir_fd, const SysString path,
182 const FileMode mode);
183
185
213COSMOS_API FileMode set_umask(const FileMode mode);
214
216
235COSMOS_API bool exists_file(const SysString path);
236
238
248COSMOS_API void unlink_file(const SysString path);
249
251
262COSMOS_API void unlink_file_at(const DirFD dir_fd, const SysString path);
263
265
268COSMOS_API void change_dir(const SysString path);
269
271
275COSMOS_API std::string get_working_dir();
276
278
290COSMOS_API std::optional<std::string> which(const std::string_view exec_base) noexcept;
291
293
301COSMOS_API void make_dir(const SysString path, const FileMode mode);
302
304
310COSMOS_API void make_dir_at(const DirFD dir_fd, const SysString path, const FileMode mode);
311
313
317COSMOS_API void remove_dir(const SysString path);
318
320
326COSMOS_API void remove_dir_at(const DirFD dir_fd, const SysString path);
327
329
342COSMOS_API Errno make_all_dirs(const SysString path, const FileMode mode);
343
345
356COSMOS_API void remove_tree(const SysString path);
357
359
377COSMOS_API void change_mode(const SysString path, const FileMode mode);
378
380
389COSMOS_API void change_mode(const FileDescriptor fd, const FileMode mode);
390
392
405COSMOS_API void change_owner(const SysString path, const UserID uid, const GroupID gid = GroupID::INVALID);
406
408
417COSMOS_API void change_owner(const FileDescriptor fd, const UserID uid, const GroupID gid = GroupID::INVALID);
418
420
432COSMOS_API void change_owner(const SysString path, const SysString user,
433 const SysString group = {});
434
436
441COSMOS_API void change_owner(const FileDescriptor fd, const SysString user,
442 const SysString group = {});
443
445inline void change_group(const FileDescriptor fd, const GroupID id) {
446 change_owner(fd, UserID::INVALID, id);
447}
448
450inline void change_group(const FileDescriptor fd, const SysString group) {
451 change_owner(fd, {}, group);
452}
453
455inline void change_group(const SysString path, const GroupID id) {
456 change_owner(path, UserID::INVALID, id);
457}
458
460inline void change_group(const SysString path, const SysString group) {
461 change_owner(path, {}, group);
462}
463
465
474COSMOS_API void change_owner_nofollow(const SysString path, const UserID uid,
475 const GroupID gid = GroupID::INVALID);
476
478
481COSMOS_API void change_owner_nofollow(const SysString path, const SysString user,
482 const SysString group = {});
483
485
506COSMOS_API void make_symlink(const SysString target, const SysString path);
507
509
513COSMOS_API void make_symlink_at(const SysString target, const DirFD dir_fd,
514 const SysString path);
515
517
526COSMOS_API std::string read_symlink(const SysString path);
527
529
535COSMOS_API std::string read_symlink_at(const DirFD dir_fd, const SysString path);
536
538
556COSMOS_API void link(const SysString old_path, const SysString new_path);
557
559
572COSMOS_API void linkat(const DirFD old_dir, const SysString old_path,
573 const DirFD new_dir, const SysString new_path,
574 const FollowSymlinks follow_old = FollowSymlinks{false});
575
577
597COSMOS_API void linkat_fd(const FileDescriptor fd, const DirFD new_dir,
598 const SysString new_path);
599
601
606COSMOS_API void linkat_proc_fd(const FileDescriptor fd, const DirFD new_dir,
607 const SysString new_path);
608
610
618COSMOS_API void truncate(const FileDescriptor fd, off_t length);
619
621
626COSMOS_API void truncate(const SysString path, off_t length);
627
631 FileDescriptor out;
632 size_t len;
633 std::optional<off_t> off_in;
634 std::optional<off_t> off_out;
635};
636
638
669COSMOS_API size_t copy_file_range(CopyFileRangeParameters &pars);
670
672
677COSMOS_API size_t copy_file_range(
678 const FileDescriptor fd_in, const FileDescriptor fd_out, const size_t len);
679
681enum class AccessCheck : int {
682 READ_OK = R_OK,
683 WRITE_OK = W_OK,
684 EXEC_OK = X_OK
685};
686
687using AccessChecks = BitMask<AccessCheck>;
688
690
712COSMOS_API void check_access(const SysString path, const AccessChecks checks = {});
713
715enum class AccessFlag : int {
716 EFFECTIVE_CREDS = AT_EACCESS,
717 NO_FOLLOW = AT_SYMLINK_NOFOLLOW
718};
719
720using AccessFlags = BitMask<AccessFlag>;
721
723
734COSMOS_API void check_access_at(const DirFD dir_fd, const SysString path,
735 const AccessChecks checks = {}, const AccessFlags flags = {});
736
738
742COSMOS_API void check_access_fd(const FileDescriptor fd, const AccessChecks check = {},
743 const AccessFlags flags = {});
744
746enum class LockOperation : int {
747 LOCK_SHARED = LOCK_SH,
748 LOCK_EXCLUSIVE = LOCK_EX,
749 UNLOCK = LOCK_UN
750};
751
752enum class LockFlag : int {
753 LOCK_NONBLOCK = LOCK_NB
754};
755
758
760
790COSMOS_API void flock(const FileDescriptor fd, const LockOperation operation, const LockFlags flags = {});
791
792} // end ns
A typesafe bit mask representation using class enums.
Definition BitMask.hxx:19
A specialized FileDescriptor for directory objects.
Definition DirFD.hxx:17
Thin Wrapper around OS file descriptors.
AccessCheck
Different access checks that can be performed in check_access().
@ EXEC_OK
Execution is allowed.
@ WRITE_OK
Write access is allowed.
@ READ_OK
Read access is allowed.
@ LOCK_NONBLOCK
don't block if a lock cannot be placed, throw ApiError with Errno::WOULD_BLOCK instead.
LockOperation
Flags used with flock().
@ UNLOCK
remove an existing lock (regardless of shared or exclusive)
@ LOCK_SHARED
place a shared lock of which multiple may exist at the same time (for reading)
@ LOCK_EXCLUSIVE
place an exclusive lock of which only one may exist at the same time (for writing)
@ CLOEXEC
Instead of closing, mark all matching file descriptors as CLOEXEC.
@ UNSHARE
Unshare specified file descriptors before closing to avoid race conditions with other threads.
void change_group(const FileDescriptor fd, const GroupID id)
Convenience wrapper of change_owner() to change only the group of a file.
AccessFlag
Extra flags that influence the behaviour of check_access_at(), and check_access_fd().
@ NO_FOLLOW
don't resolve symlinks in path but check access to the link itself.
@ EFFECTIVE_CREDS
use the caller's effective UID and GID for the access check.
FileNum
Primitive file descriptor.
Definition types.hxx:32
Wrapper type around a C-style string for use with system APIs.
Definition SysString.hxx:33
set of parameters for copy_file_range().