25#ifndef JAU_FILE_UTIL_HPP_
26#define JAU_FILE_UTIL_HPP_
38 #include <sys/types.h>
57 bool chdir(
const std::string& path)
noexcept;
64 std::string
absolute(
const std::string_view& relpath)
noexcept;
76 std::string
dirname(
const std::string_view& path)
noexcept;
86 std::string
basename(
const std::string_view& path)
noexcept;
89 bool isAbsolute(
const std::string_view& path)
noexcept;
92 bool exists(
const std::string& path,
bool verbose_on_error=
false)
noexcept;
101 std::string
lookup_asset_dir(
const char* exe_path,
const char* asset_file,
const char* asset_install_subdir)
noexcept;
108 std::string dirname_;
109 std::string basename_;
112 struct backed_string_view {
114 std::string_view view;
116 backed_string_view() noexcept
117 : backing(), view(backing) {}
119 backed_string_view(
const std::string& backing_,
const std::string_view& view_) noexcept
121 view(backing_.size() > 0 ? ((std::string_view)backing).substr(view_.data() - backing_.data(), view_.size()) : view_) {}
123 backed_string_view(
const std::string_view& view_) noexcept
124 : backing(), view(view_) {}
127 backed_string_view(
const backed_string_view& o) noexcept
128 : backing(o.backing),
129 view( o.is_backed() ? ((std::string_view)backing).substr(o.view.data() - o.backing.data(), o.view.size()) : o.view)
133 backed_string_view(
const backed_string_view& o)
noexcept =
delete;
137 backed_string_view(backed_string_view&& o) noexcept
138 : backing( std::move(o.backing) ),
139 view( std::move(o.view) )
141 fprintf(stderr,
"backed_string_view move_ctor %s\n",
to_string(
true).c_str());
145 backed_string_view(backed_string_view&& o)
noexcept =
delete;
148 bool is_backed()
const noexcept {
return backing.size() > 0; }
150 void backup()
noexcept {
151 backing = std::string(view);
154 void backup(
const std::string& orig)
noexcept {
158 void backup(
const std::string_view& orig)
noexcept {
159 backing = std::string(orig);
162 void backup_and_append(
const std::string& orig,
const std::string& appendix)
noexcept {
164 backing.append(appendix);
167 void backup_and_append(
const std::string_view& orig,
const std::string& appendix)
noexcept {
168 backing = std::string(orig);
169 backing.append(appendix);
173 std::string
to_string(
const bool destailed =
false)
const noexcept {
175 return "[backing '" + backing +
"', view '" + std::string(view) +
"']";
177 return std::string(view);
180 static std::unique_ptr<backed_string_view> reduce(
const std::string_view& path_)
noexcept;
182 dir_item(std::unique_ptr<backed_string_view> cleanpath)
noexcept;
195 dir_item(
const std::string_view& path_)
noexcept;
209 dir_item(std::string dirname__, std::string basename__)
noexcept;
212 const std::string&
dirname() const noexcept {
return dirname_; }
215 const std::string&
basename() const noexcept {
return basename_; }
220 std::string
path() const noexcept;
225 bool empty() const noexcept {
return empty_; }
228 return dirname_ == rhs.dirname_ && basename_ == rhs.basename_;
232 return !(*
this == rhs);
305 sock = 0b00000000000000000001000000000000,
307 blk = 0b00000000000000000010000000000000,
309 chr = 0b00000000000000000100000000000000,
311 fifo = 0b00000000000000001000000000000000,
313 dir = 0b00000000000000010000000000000000,
315 file = 0b00000000000000100000000000000000,
317 link = 0b00000000000001000000000000000000,
410 fd = 0b0001000000000000
421 std::shared_ptr<std::string> link_target_path_;
422 std::shared_ptr<file_stats> link_target_;
440 ctor_cookie(
const uint16_t recursion_level_) { rec_level = recursion_level_; }
448 file_stats(const ctor_cookie& cc,
int dirfd, const
dir_item& item, const
bool dirfd_is_item_dirname) noexcept;
467 file_stats(const
int dirfd, const
std::
string& path) noexcept;
483 file_stats(const
int dirfd, const
dir_item& item, const
bool dirfd_is_item_dirname = true) noexcept;
512 std::string
path() const noexcept {
return item_.path(); }
521 const std::shared_ptr<std::string>&
link_target_path() const noexcept {
return link_target_path_; }
532 const std::shared_ptr<file_stats>&
link_target() const noexcept {
return link_target_; }
544 const file_stats* final_target(
size_t* link_count =
nullptr) const noexcept;
547 bool has(const field_t fields) const noexcept;
566 int fd() const noexcept {
return fd_; }
579 uint64_t
size() const noexcept {
return size_; }
591 constexpr int errno_res() const noexcept {
return errno_res_; }
594 constexpr bool ok() const noexcept {
return 0 == errno_res_; }
603 constexpr bool has_fd() const noexcept {
return 0 <= fd_; }
635 return !(*
this == rhs);
643 JAU_MAKE_BITFIELD_ENUM_STRING2(
file_stats::field_t, field_t,
type, mode, nlink, uid, gid, atime, mtime, ctime, ino, size, blocks, btime);
896 bool compare(const
std::
string& source1, const
std::
string& source2, const
bool verbose = false) noexcept;
1005 bool rename(
const std::string& oldpath,
const std::string& newpath)
noexcept;
1010 void sync() noexcept;
1065 JAU_MAKE_BITFIELD_ENUM_STRING(
mountflags_linux,
rdonly,
nosuid,
nodev,
noexec,
synchronous,
remount,
mandlock,
dirsync,
noatime,
1066 nodiratime,
bind,
move,
rec,
silent,
posixacl,
unbindable,
private_,
slave,
shared,
relatime,
1091 mount_ctx mount_image(
const std::string& image_path,
const std::string& target,
const std::string& fs_type,
1092 const mountflags_t flags,
const std::string& fs_options =
"");
1113 mount_ctx mount(
const std::string& source,
const std::string& target,
const std::string& fs_type,
1114 const mountflags_t flags,
const std::string& fs_options =
"");
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().
const dir_item & item() const noexcept
Returns the dir_item.
field_t
Field identifier which bit-mask indicates field_t fields.
@ type
File type mode bits.
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.
file_stats() noexcept
Instantiate an empty file_stats with fmode_t::not_existing set.
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 field_t fields() const noexcept
Returns the retrieved field_t fields.
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.
#define JAU_MAKE_BITFIELD_ENUM_STRING(type,...)
constexpr bool is_set(const E mask, const E bits) noexcept
bool exists(const std::string &path, bool verbose_on_error=false) noexcept
Returns true if path exists and is accessible.
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 lookup_asset_dir(const char *exe_path, const char *asset_file, const char *asset_install_subdir) noexcept
Returns located asset directory if found, otherwise an empty string.
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...
std::string to_string(const fmode_t mask, const bool show_rwx) noexcept
Return the string representation of fmode_t.
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.
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.
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.
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.
jau::function< void(const dir_item &)> consume_dir_item
void consume_dir_item(const dir_item& item)
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.
bool umount(const mount_ctx &context, const umountflags_t flags)
Detach the given mount_ctx context
umountflags_linux
Flag bit values for umount() flags under GNU/Linux.
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
JAU_MAKE_BITFIELD_ENUM_STRING2(file_stats::field_t, field_t, type, mode, nlink, uid, gid, atime, mtime, ctime, ino, size, blocks, btime)
@ 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 Gothel Software e.K.
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_)
bool operator==(const Addr48Bit &lhs, const Addr48Bit &rhs) noexcept