25#ifndef JAU_FILE_UTIL_HPP_
26#define JAU_FILE_UTIL_HPP_
36 #include <sys/types.h>
53 bool chdir(
const std::string& path)
noexcept;
60 std::string
absolute(
const std::string_view& relpath)
noexcept;
72 std::string
dirname(
const std::string_view& path)
noexcept;
82 std::string
basename(
const std::string_view& path)
noexcept;
85 bool isAbsolute(
const std::string_view& path)
noexcept;
93 std::string basename_;
96 struct backed_string_view {
98 std::string_view view;
100 backed_string_view() noexcept
101 : backing(), view(backing) {}
103 backed_string_view(
const std::string& backing_,
const std::string_view& view_) noexcept
105 view(backing_.size() > 0 ? ((std::string_view)backing).substr(view_.data() - backing_.data(), view_.size()) : view_) {}
107 backed_string_view(
const std::string_view& view_) noexcept
108 : backing(), view(view_) {}
111 backed_string_view(
const backed_string_view& o) noexcept
112 : backing(o.backing),
113 view( o.is_backed() ? ((std::string_view)backing).substr(o.view.data() - o.backing.data(), o.view.size()) : o.view)
117 backed_string_view(
const backed_string_view& o)
noexcept =
delete;
121 backed_string_view(backed_string_view&& o) noexcept
122 : backing( std::move(o.backing) ),
123 view( std::move(o.view) )
125 fprintf(stderr,
"backed_string_view move_ctor %s\n",
to_string(
true).c_str());
129 backed_string_view(backed_string_view&& o)
noexcept =
delete;
132 bool is_backed()
const noexcept {
return backing.size() > 0; }
134 void backup()
noexcept {
135 backing = std::string(view);
138 void backup(
const std::string& orig)
noexcept {
142 void backup(
const std::string_view& orig)
noexcept {
143 backing = std::string(orig);
146 void backup_and_append(
const std::string& orig,
const std::string& appendix)
noexcept {
148 backing.append(appendix);
151 void backup_and_append(
const std::string_view& orig,
const std::string& appendix)
noexcept {
152 backing = std::string(orig);
153 backing.append(appendix);
157 std::string
to_string(
const bool destailed =
false)
const noexcept {
159 return "[backing '" + backing +
"', view '" + std::string(view) +
"']";
161 return std::string(view);
164 static std::unique_ptr<backed_string_view> reduce(
const std::string_view& path_)
noexcept;
166 dir_item(std::unique_ptr<backed_string_view> cleanpath)
noexcept;
179 dir_item(
const std::string_view& path_)
noexcept;
193 dir_item(std::string dirname__, std::string basename__)
noexcept;
196 const std::string&
dirname() const noexcept {
return dirname_; }
199 const std::string&
basename() const noexcept {
return basename_; }
204 std::string
path() const noexcept;
209 bool empty() const noexcept {
return empty_; }
212 return dirname_ == rhs.dirname_ && basename_ == rhs.basename_;
216 return !(*
this == rhs);
289 sock = 0b00000000000000000001000000000000,
291 blk = 0b00000000000000000010000000000000,
293 chr = 0b00000000000000000100000000000000,
295 fifo = 0b00000000000000001000000000000000,
297 dir = 0b00000000000000010000000000000000,
299 file = 0b00000000000000100000000000000000,
301 link = 0b00000000000001000000000000000000,
303 no_access = 0b00100000000000000000000000000000,
307 type_mask = 0b01100000000001111111000000000000,
310 return static_cast<uint32_t
>(rhs);
313 return static_cast<fmode_t>(~number(rhs));
340 return !(lhs == rhs);
343 return bits == (mask & bits);
351 std::string
to_string(
const fmode_t mask,
const bool show_rwx =
false) noexcept;
415 type = 0b0000000000000001,
417 mode = 0b0000000000000010,
418 nlink = 0b0000000000000100,
419 uid = 0b0000000000001000,
420 gid = 0b0000000000010000,
421 atime = 0b0000000000100000,
422 mtime = 0b0000000001000000,
423 ctime = 0b0000000010000000,
424 ino = 0b0000000100000000,
425 size = 0b0000001000000000,
426 blocks = 0b0000010000000000,
427 btime = 0b0000100000000000,
428 fd = 0b0001000000000000
439 std::shared_ptr<std::string> link_target_path_;
440 std::shared_ptr<file_stats> link_target_;
458 ctor_cookie(
const uint16_t recursion_level_) { rec_level = recursion_level_; }
466 file_stats(const ctor_cookie& cc,
int dirfd, const
dir_item& item, const
bool dirfd_is_item_dirname) noexcept;
501 file_stats(const
int dirfd, const
dir_item& item, const
bool dirfd_is_item_dirname = true) noexcept;
519 const
dir_item& item() const noexcept {
return item_; }
530 std::string
path() const noexcept {
return item_.
path(); }
539 const std::shared_ptr<std::string>&
link_target_path() const noexcept {
return link_target_path_; }
550 const std::shared_ptr<file_stats>&
link_target() const noexcept {
return link_target_; }
562 const file_stats* final_target(
size_t* link_count =
nullptr) const noexcept;
565 bool has(const field_t fields) const noexcept;
568 constexpr
field_t fields() const noexcept {
return has_fields_; }
584 int fd() const noexcept {
return fd_; }
597 uint64_t
size() const noexcept {
return size_; }
609 constexpr int errno_res() const noexcept {
return errno_res_; }
612 constexpr bool ok() const noexcept {
return 0 == errno_res_; }
621 constexpr bool has_fd() const noexcept {
return 0 <= fd_; }
653 return !(*
this == rhs);
662 return static_cast<uint32_t
>(rhs);
692 return !(lhs == rhs);
695 return bits == (mask & bits);
823 return static_cast<uint16_t
>(rhs);
853 return !(lhs == rhs);
856 return bit == (mask & bit);
905 return static_cast<uint16_t
>(rhs);
935 return !(lhs == rhs);
938 return bit == (mask & bit);
1012 bool compare(const file_stats& source1, const file_stats& source2, const
bool verbose = false) noexcept;
1022 bool compare(const
std::
string& source1, const
std::
string& source2, const
bool verbose = false) noexcept;
1075 return static_cast<uint16_t
>(rhs);
1105 return !(lhs == rhs);
1108 return bit == (mask & bit);
1167 bool rename(
const std::string& oldpath,
const std::string& newpath)
noexcept;
1172 void sync() noexcept;
1180 : mounted(true), target(
std::
move(target_)), loop_device_id(loop_device_id_) {}
1183 : mounted(false), target(), loop_device_id(-1) {}
1258 return !(lhs == rhs);
1261 return bit == (mask & bit);
1298 mount_ctx
mount_image(
const std::string& image_path,
const std::string& target,
const std::string& fs_type,
1299 const mountflags_t flags,
const std::string& fs_options =
"");
1320 mount_ctx
mount(
const std::string& source,
const std::string& target,
const std::string& fs_type,
1321 const mountflags_t flags,
const std::string& fs_options =
"");
1372 return !(lhs == rhs);
1375 return bit == (mask & bit);
Representing a directory item split into dirname() and basename().
std::string path() const noexcept
Returns a full unix path representation combining dirname() and basename().
const std::string & basename() const noexcept
Return the basename, shall not be empty nor contain a dirname.
bool operator!=(const dir_item &rhs) const noexcept
bool empty() const noexcept
Returns true if bot, dirname() and basename() refer to .
std::string to_string() const noexcept
Returns a comprehensive string representation of this item.
dir_item() noexcept
Empty item w/ .
bool operator==(const dir_item &rhs) const noexcept
const std::string & dirname() const noexcept
Returns the dirname, shall not be empty and denotes .
Platform agnostic representation of POSIX ::lstat() and ::stat() for a given pathname.
constexpr bool is_char() const noexcept
Returns true if entity is a character device, might be in combination with is_link().
int fd() const noexcept
Returns the file descriptor if has_fd(), otherwise -1 for no file descriptor.
constexpr bool exists() const noexcept
Returns true if entity does not exist, exclusive bit.
fmode_t type_mode() const noexcept
Returns the type bit portion of fmode_t, i.e.
constexpr bool has_fd() const noexcept
Returns true if entity has a file descriptor.
const fraction_timespec & mtime() const noexcept
Returns the last modification time of this element since Unix Epoch.
constexpr bool is_fifo() const noexcept
Returns true if entity is a fifo/pipe, might be in combination with is_link().
constexpr bool is_link() const noexcept
Returns true if entity is a symbolic link, might be in combination with is_file(),...
constexpr bool is_file() const noexcept
Returns true if entity is a file, might be in combination with is_link().
const fraction_timespec & ctime() const noexcept
Returns the last status change time of this element since Unix Epoch.
gid_t gid() const noexcept
Returns the group id, owning the element.
constexpr bool is_dir() const noexcept
Returns true if entity is a directory, might be in combination with is_link().
field_t
Field identifier which bit-mask indicates field_t fields.
uint64_t size() const noexcept
Returns the size in bytes of this element if is_file(), otherwise zero.
constexpr bool is_socket() const noexcept
Returns true if entity is a socket, might be in combination with is_link().
const fraction_timespec & btime() const noexcept
Returns the birth time of this element since Unix Epoch, i.e.
constexpr bool has_access() const noexcept
Returns true if entity gives no access to user, exclusive bit.
constexpr bool ok() const noexcept
Returns true if no error occurred.
const std::shared_ptr< std::string > & link_target_path() const noexcept
Returns the stored link-target path this symbolic-link points to if instance is a symbolic-link,...
fmode_t mode() const noexcept
Returns the fmode_t, file type and mode.
const std::shared_ptr< file_stats > & link_target() const noexcept
Returns the link-target this symbolic-link points to if instance is a symbolic-link,...
uid_t uid() const noexcept
Returns the user id, owning the element.
constexpr int errno_res() const noexcept
Returns the errno value occurred to produce this instance, or zero for no error.
bool operator!=(const file_stats &rhs) const noexcept
fmode_t prot_mode() const noexcept
Returns the POSIX protection bit portion of fmode_t, i.e.
const fraction_timespec & atime() const noexcept
Returns the last access time of this element since Unix Epoch.
constexpr bool is_block() const noexcept
Returns true if entity is a block device, might be in combination with is_link().
std::string path() const noexcept
Returns the unix path representation.
Class template jau::function is a general-purpose static-polymorphic function wrapper.
bool mkdir(const std::string &path, const fmode_t mode=jau::fs::fmode_t::def_dir_prot, const bool verbose=false) noexcept
Create directory.
uint64_t mountflags_t
Generic flag bit values for mount() flags.
std::string to_named_fd(const int fd) noexcept
Returns platform dependent named file descriptor of given file descriptor, if supported.
mount_ctx mount(const std::string &source, const std::string &target, const std::string &fs_type, const mountflags_t flags, const std::string &fs_options="")
Attach the filesystem named in source to target using the given filesystem source directly.
mount_ctx mount_image(const std::string &image_path, const std::string &target, const std::string &fs_type, const mountflags_t flags, const std::string &fs_options="")
Attach the filesystem image named in image_path to target using an intermediate platform specific fil...
constexpr fmode_t operator&(const fmode_t lhs, const fmode_t rhs) noexcept
constexpr fmode_t operator|(const fmode_t lhs, const fmode_t rhs) noexcept
bool copy(const std::string &source_path, const std::string &dest_path, const copy_options copts=copy_options::none) noexcept
Copy the given source_path to dest_path using copy_options.
fmode_t
Generic file type and POSIX protection mode bits as used in file_stats, touch(), mkdir() etc.
constexpr fmode_t operator^(const fmode_t lhs, const fmode_t rhs) noexcept
std::string basename(const std::string_view &path) noexcept
Return stripped leading directory components from given path separated by /.
mountflags_linux
Flag bit values for mount() flags under GNU/Linux.
std::string get_cwd() noexcept
Return the current working directory or empty on failure.
constexpr uint32_t number(const fmode_t rhs) noexcept
bool compare(const file_stats &source1, const file_stats &source2, const bool verbose=false) noexcept
Compare the bytes of both files, denoted by source1 and source2.
bool visit(const std::string &path, const traverse_options topts, const path_visitor &visitor, std::vector< int > *dirfds=nullptr) noexcept
Visit element(s) of a given path, see traverse_options for detailed settings.
std::string absolute(const std::string_view &relpath) noexcept
Returns the absolute path of given relpath if existing, otherwise an empty string.
int umountflags_t
Generic flag bit values for umount() flags.
constexpr fmode_t & operator|=(fmode_t &lhs, const fmode_t rhs) noexcept
std::string dirname(const std::string_view &path) noexcept
Return stripped last component from given path separated by /, excluding the trailing separator /.
copy_options
Filesystem copy options used to copy() path elements.
bool get_dir_content(const std::string &path, const consume_dir_item &digest) noexcept
Returns a list of directory elements excluding .
bool touch(const std::string &path, const jau::fraction_timespec &atime, const jau::fraction_timespec &mtime, const fmode_t mode=jau::fs::fmode_t::def_file_prot) noexcept
Touch the file with given atime and mtime and create file if not existing yet.
traverse_options
Filesystem traverse options used to visit() path elements.
bool rename(const std::string &oldpath, const std::string &newpath) noexcept
Rename oldpath to newpath using POSIX rename(), with the following combinations.
constexpr fmode_t & operator&=(fmode_t &lhs, const fmode_t rhs) noexcept
bool remove(const std::string &path, const traverse_options topts=traverse_options::none) noexcept
Remove the given path.
void sync() noexcept
Synchronizes filesystems, i.e.
int from_named_fd(const std::string &named_fd) noexcept
Returns the file descriptor from the given named file descriptor.
constexpr bool is_set(const fmode_t mask, const fmode_t bits) noexcept
bool umount(const mount_ctx &context, const umountflags_t flags)
Detach the given mount_ctx context
constexpr fmode_t & operator^=(fmode_t &lhs, const fmode_t rhs) noexcept
umountflags_linux
Flag bit values for umount() flags under GNU/Linux.
constexpr fmode_t operator~(const fmode_t rhs) noexcept
bool chdir(const std::string &path) noexcept
Change working directory.
bool isAbsolute(const std::string_view &path) noexcept
Returns true if first character is / or - in case of Windows - \\.
traverse_event
Filesystem traverse event used to call path_visitor for path elements from visit().
constexpr ::mode_t posix_protection_bits(const fmode_t mask) noexcept
Returns the POSIX protection bits: rwx_all | set_uid | set_gid | sticky, i.e.
jau::function< bool(traverse_event, const file_stats &, size_t)> path_visitor
path_visitor jau::FunctionDef definition
@ exec_usr
Protection bit: POSIX S_IXUSR.
@ exec_grp
Protection bit: POSIX S_IXGRP.
@ def_file_prot
Default file protection bit: Safe default: POSIX S_IRUSR | S_IWUSR | S_IRGRP or read_usr | write_usr ...
@ write_grp
Protection bit: POSIX S_IWGRP.
@ no_access
Type: Entity gives no access to user, exclusive bit.
@ set_uid
Protection bit: POSIX S_ISUID.
@ write_usr
Protection bit: POSIX S_IWUSR.
@ ugs_set
Protection bit: POSIX S_ISUID | S_ISGID | S_ISVTX.
@ link
Type: Entity is a symbolic link, might be in combination with file or dir, fifo, chr,...
@ sock
Type: Entity is a socket, might be in combination with link.
@ chr
Type: Entity is a character device, might be in combination with link.
@ rwx_oth
Protection bit: POSIX S_IRWXO.
@ sticky
Protection bit: POSIX S_ISVTX.
@ rwx_usr
Protection bit: POSIX S_IRWXU.
@ read_usr
Protection bit: POSIX S_IRUSR.
@ dir
Type: Entity is a directory, might be in combination with link.
@ rwx_all
Protection bit: POSIX S_IRWXU | S_IRWXG | S_IRWXO or rwx_usr | rwx_grp | rwx_oth.
@ file
Type: Entity is a file, might be in combination with link.
@ protection_mask
12 bit protection bit mask 07777 for rwx_all | set_uid | set_gid | sticky .
@ blk
Type: Entity is a block device, might be in combination with link.
@ read_grp
Protection bit: POSIX S_IRGRP.
@ read_oth
Protection bit: POSIX S_IROTH.
@ not_existing
Type: Entity does not exist, exclusive bit.
@ rwx_grp
Protection bit: POSIX S_IRWXG.
@ fifo
Type: Entity is a fifo/pipe, might be in combination with link.
@ write_oth
Protection bit: POSIX S_IWOTH.
@ set_gid
Protection bit: POSIX S_ISGID.
@ def_dir_prot
Default directory protection bit: Safe default: POSIX S_IRWXU | S_IRGRP | S_IXGRP or rwx_usr | read_g...
@ type_mask
Type mask for sock | blk | chr | fifo | dir | file | link | no_access | not_existing.
@ exec_oth
Protection bit: POSIX S_IXOTH.
@ ignore_symlink_errors
Ignore errors from erroneous symlinks, e.g.
@ overwrite
Overwrite existing destination files.
@ preserve_all
Preserve uid and gid if allowed and access- and modification-timestamps, i.e.
@ into_existing_dir
Copy source dir content into an already existing destination directory as if destination directory di...
@ lexicographical_order
Traverse through elements in lexicographical order.
@ verbose
Enable verbosity mode, potentially used by a path_visitor implementation like remove().
@ follow_symlinks
Traverse through symbolic linked directories if traverse_options::recursive is set,...
@ recursive
Traverse through directories, i.e.
@ dir_symlink
Visiting a symbolic-link to a directory which is not followed, i.e.
@ dir_non_recursive
Visiting a directory non-recursive, i.e.
@ dir_check_entry
Visiting a directory on entry, see traverse_options::dir_check_entry.
@ dir_entry
Visiting a directory on entry, see traverse_options::dir_entry.
@ symlink
Visiting a symbolic-link, either to a file or a non-existing entity.
@ dir_exit
Visiting a directory on exit, see traverse_options::dir_exit.
@ file_symlink
Visiting a symlink to a file, i.e.
Author: Sven Gothel sgothel@jausoft.com Copyright (c) 2022 Gothel Software e.K.
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Timespec structure using int64_t for its components in analogy to struct timespec_t on 64-bit platfor...
mount_ctx(std::string target_, const int loop_device_id_)