lfilesystem
0.0.1
C++ filesystem library
|
The base class for any kind of object on the filesystem. More...
#include <lfilesystem_FilesystemEntry.h>
Public Types | |
Typedefs | |
using | PermOptions = std::filesystem::perm_options |
Options bitfield for setting permissions. More... | |
using | CopyOptions = std::filesystem::copy_options |
Options bitfield for copying. More... | |
using | Time = std::filesystem::file_time_type |
A time point used for filesystem time. More... | |
Public Member Functions | |
FilesystemEntry (const FilesystemEntry &)=default | |
FilesystemEntry (FilesystemEntry &&)=default | |
virtual | ~FilesystemEntry ()=default |
Destructor. More... | |
bool | copyFrom (const FilesystemEntry &source, CopyOptions options=CopyOptions::update_existing) const noexcept |
bool | copyFrom (const Path &source, CopyOptions options=CopyOptions::update_existing) const noexcept |
Overwriting. More... | |
FilesystemEntry & | operator= (const FilesystemEntry &)=default |
FilesystemEntry & | operator= (FilesystemEntry &&)=default |
bool | revealToUserInFileBrowser () const |
Uses the system's native file browser to display this file to the user. More... | |
Constructors | |
FilesystemEntry ()=default | |
Creates a FilesystemEntry with an empty path. More... | |
FilesystemEntry (const Path &pathToUse) | |
Creates a FilesystemEntry referring to the specified path. More... | |
Path assignment | |
FilesystemEntry & | operator= (const Path &newPath) |
Assigns the path this filesystem entry represents. More... | |
FilesystemEntry & | operator= (const std::string_view &newPath) |
Assigns the path this filesystem entry represents. More... | |
FilesystemEntry & | assignPath (const Path &newPath) noexcept |
Assigns the path this filesystem entry represents. More... | |
Equality comparison | |
bool | operator== (const FilesystemEntry &other) const noexcept |
Compares this filesystem entry's absolute path with the other one's for equality. More... | |
bool | operator== (const Path &other) const noexcept |
Compares this filesystem entry's absolute path with the other one's for equality. More... | |
bool | operator!= (const FilesystemEntry &other) const noexcept |
Compares this filesystem entry's absolute path with the other one's for equality. More... | |
bool | operator!= (const Path &other) const noexcept |
Compares this filesystem entry's absolute path with the other one's for equality. More... | |
Lexicographic comparison | |
bool | operator< (const FilesystemEntry &other) const noexcept |
Lexicographically compares this filesystem entry's absolute path with another path. More... | |
bool | operator> (const FilesystemEntry &other) const noexcept |
Lexicographically compares this filesystem entry's absolute path with another path. More... | |
bool | operator< (const Path &other) const noexcept |
Lexicographically compares this filesystem entry's absolute path with another path. More... | |
bool | operator> (const Path &other) const noexcept |
Lexicographically compares this filesystem entry's absolute path with another path. More... | |
Path manipulation | |
FilesystemEntry | operator/ (const std::string_view &subpathName) const |
Returns a new FilesystemEntry referring to a subpath of this one. More... | |
FilesystemEntry & | operator/= (const std::string_view &subpathName) |
Assigns this object's path to a subpath of the current path. More... | |
FilesystemEntry & | changeName (const std::string_view &newName) |
Changes the last section of the path (the filename). More... | |
bool | makeAbsoluteRelativeTo (const Path &basePath) noexcept |
If the path this object holds is already absolute, this function does nothing and returns false. More... | |
bool | makeAbsoluteRelativeToCWD () noexcept |
Similar to makeAbsoluteRelativeTo() , but uses the current working directory as the base path. More... | |
Path queries | |
Path | getPath (bool makePreferred=false) const noexcept |
Returns the current path held by this object. More... | |
Path | getAbsolutePath (bool makePreferred=false) const noexcept |
Returns the full, absolute path of this filesystem entry. More... | |
operator Path () const noexcept | |
Converts this filesystem entry to the path it holds. More... | |
std::string | getName () const noexcept |
Returns the last section of the path (the filename). More... | |
Directory | getDirectory () const |
Returns the directory containing this filesystem entry. More... | |
Directory | getParentDirectory () const |
Returns the parent directory of this filesystem entry. More... | |
bool | isBelow (const Directory &directory, std::size_t depthLimit=50) const |
Returns true if this filesystem entry is in a subdirectory of the given directory – ie, if that directory is a parent or grandparent of this filepath. More... | |
bool | isAbsolutePath () const noexcept |
Returns true if the path this object holds is an absolute path. More... | |
bool | isRelativePath () const noexcept |
Returns true if the path this object holds is a relative path. More... | |
bool | isValid () const noexcept |
Returns true if this object holds a valid filesystem path. More... | |
bool | isHidden () const |
Returns true if this path is a hidden file or directory. More... | |
FilesystemEntry | getSibling (const std::string_view &siblingName) const |
Returns a filesystem entry with the given name in the same directory as this one. More... | |
Filesystem queries | |
virtual bool | isFile () const noexcept |
Returns true if this filesystem entry refers to a file. More... | |
virtual bool | isDirectory () const noexcept |
Returns true if this filesystem entry refers to a directory. More... | |
virtual bool | isSymLink () const noexcept |
Returns true if this filesystem entry refers to a symbolic link. More... | |
bool | exists () const noexcept |
Returns true if the filesystem object this object refers to exists on disk. More... | |
operator bool () const noexcept | |
Returns true if this filesystem object exists. More... | |
virtual std::uintmax_t | sizeInBytes () const |
Returns the size, in bytes, of the filesystem entry referred to by this object. More... | |
Time | getLastModificationTime () const noexcept |
Returns the last modification time of the filesystem entry. More... | |
std::optional< Volume > | getVolume () const noexcept |
Returns a Volume object representing the logical filesystem volume that this object exists on. More... | |
Type conversion | |
std::optional< File > | getFileObject () const noexcept |
If this object refers to a file, constructs a File object referring to the same file. More... | |
std::optional< Directory > | getDirectoryObject () const noexcept |
If this object refers to a directory, constructs a Directory object referring to the same directory. More... | |
std::optional< SymLink > | getSymLinkObject () const noexcept |
If this object refers to a symbolic link, constructs a SymLink object referring to the same link. More... | |
Filesystem actions | |
virtual bool | createIfDoesntExist () const noexcept |
Creates the filesystem entry this object refers to, if it doesn't already exist. More... | |
bool | deleteIfExists () const noexcept |
Deletes the filesystem entry this object refers to, if it exists. More... | |
bool | moveToTrash () noexcept |
Attempts to move this filesystem object to the system's trash folder. More... | |
void | touch () const |
If the filesystem entry this object refers do doesn't exist, this function creates it. More... | |
bool | touch_noCreate () const |
The same as touch() , except this function does not create the filesystem entry if it didn't already exist. More... | |
bool | rename (const Path &newPath) noexcept |
Renames this filesystem object to a new path. More... | |
Permissions | |
bool | setPermissions (FSPerms permissions, PermOptions options=PermOptions::replace) const noexcept |
Sets the permissions for the filesystem entry. More... | |
Permissions | getPermissions () const |
Returns the current permissions of the filesystem entry this object refers to. More... | |
Creating copies | |
bool | copyTo (const Path &dest, CopyOptions options=CopyOptions::update_existing) const noexcept |
Creates a copy of this filesystem entry at a new location on disk. More... | |
bool | copyTo (const FilesystemEntry &dest, CopyOptions options=CopyOptions::update_existing) const noexcept |
Creates a copy of this filesystem entry at a new location on disk. More... | |
std::optional< FilesystemEntry > | copyToDirectory (const Path &destDirectory, CopyOptions options=CopyOptions::update_existing) const noexcept |
Creates a copy of this filesystem object, with the same filename, in a different directory. More... | |
Related Functions | |
(Note that these are not member functions.) | |
std::ostream & | operator<< (std::ostream &os, const FilesystemEntry &value) |
Writes a FilesystemEntry's path to the output stream. More... | |
The base class for any kind of object on the filesystem.
This class is essentially a wrapper around std::filesystem::path
, with a more object-oriented design. This class holds only the path, and so is relatively trivial to construct or copy (think of it like a string).
This class normalizes paths passed to it using the normalizePath()
function.
The kinds of filesystem objects Limes defines are File, Directory, and SymLink, but custom file-like object types can be created as well. To satisfy this interface, a custom type must have and be able to logically manipulate a std::filesystem::path
.
This base class contains various semantic operations on and queries of the path, some basic disk operations, and provides functions for converting any FilesystemEntry
object to higher-level Directory
, File
, or SymLink
objects.
For example:
getNonexistentSibling()
getCreationTime(), getLastAccessTime()
tests for getModificationTime(), getCreationTime(), getLastAccessTime()
Definition at line 79 of file lfilesystem_FilesystemEntry.h.
using limes::files::FilesystemEntry::CopyOptions = std::filesystem::copy_options |
Options bitfield for copying.
Definition at line 89 of file lfilesystem_FilesystemEntry.h.
using limes::files::FilesystemEntry::PermOptions = std::filesystem::perm_options |
Options bitfield for setting permissions.
Definition at line 86 of file lfilesystem_FilesystemEntry.h.
using limes::files::FilesystemEntry::Time = std::filesystem::file_time_type |
A time point used for filesystem time.
Definition at line 92 of file lfilesystem_FilesystemEntry.h.
|
default |
Creates a FilesystemEntry with an empty path.
After creating a FilesystemEntry with this default constructor, calling isValid()
on it will return false. You can use the assignPath()
function to assign this object to a path after it has been constructed.
|
explicit |
Creates a FilesystemEntry referring to the specified path.
pathToUse | The path to this filesystem entry. This can be an absolute or relative path, and it is not an error if the passed path doesn't actually exist on disk. The path will be normalized using the normalizePath() function. |
|
virtualdefault |
Destructor.
|
noexcept |
Assigns the path this filesystem entry represents.
rename()
function. The path will be normalized using the normalizePath()
function.FilesystemEntry& limes::files::FilesystemEntry::changeName | ( | const std::string_view & | newName | ) |
Changes the last section of the path (the filename).
For example:
rename()
function.
|
noexcept |
Overwriting.
Overwrites this filesystem entry by copying from another location.
|
noexcept |
Creates a copy of this filesystem entry at a new location on disk.
|
noexcept |
Creates a copy of this filesystem entry at a new location on disk.
|
noexcept |
Creates a copy of this filesystem object, with the same filename, in a different directory.
If the destDirectory
is the same directory that this object is in, this function will make no changes on disk and return a nullopt
.
nullopt
if copying was not successful.
|
virtualnoexcept |
Creates the filesystem entry this object refers to, if it doesn't already exist.
Reimplemented in limes::files::Directory.
|
noexcept |
Deletes the filesystem entry this object refers to, if it exists.
|
noexcept |
Returns true if the filesystem object this object refers to exists on disk.
|
noexcept |
Returns the full, absolute path of this filesystem entry.
The returned path will be normalized using the normalizePath() function.
makePreferred | If true, this converts any directory separators present in the path to the platform's preferred directory separator. See dirSeparator() |
Directory limes::files::FilesystemEntry::getDirectory | ( | ) | const |
Returns the directory containing this filesystem entry.
If this FilesystemEntry is a directory, its own path is returned; otherwise, the path of the directory containing this file is returned.
For example:
|
noexcept |
If this object refers to a directory, constructs a Directory object referring to the same directory.
Otherwise, returns a nullopt
.
|
noexcept |
If this object refers to a file, constructs a File object referring to the same file.
Otherwise, returns a nullopt
.
|
noexcept |
Returns the last modification time of the filesystem entry.
|
noexcept |
Returns the last section of the path (the filename).
Directory limes::files::FilesystemEntry::getParentDirectory | ( | ) | const |
Returns the parent directory of this filesystem entry.
If this FilesystemEntry is a directory, this returns its parent directory; otherwise, this returns the parent directory of the directory containing this file or symlink.
For example:
|
noexcept |
Returns the current path held by this object.
If this object was constructed with or assigned to a relative path, this function will return that relative path.
The returned path will be normalized using the normalizePath() function.
makePreferred | If true, this converts any directory separators present in the path to the platform's preferred directory separator. See dirSeparator() |
Permissions limes::files::FilesystemEntry::getPermissions | ( | ) | const |
Returns the current permissions of the filesystem entry this object refers to.
FilesystemEntry limes::files::FilesystemEntry::getSibling | ( | const std::string_view & | siblingName | ) | const |
Returns a filesystem entry with the given name in the same directory as this one.
|
noexcept |
If this object refers to a symbolic link, constructs a SymLink object referring to the same link.
Otherwise, returns a nullopt
.
|
noexcept |
Returns a Volume object representing the logical filesystem volume that this object exists on.
Returns a nullopt
if the volume for this path cannot be computed correctly, or if this FilesystemEntry holds an invalid path.
|
noexcept |
Returns true if the path this object holds is an absolute path.
Note that this function does not always return true if isRelativePath()
returns false – if isValid()
returns false, then both isAbsolutePath()
and isRelativePath()
will always return false.
bool limes::files::FilesystemEntry::isBelow | ( | const Directory & | directory, |
std::size_t | depthLimit = 50 |
||
) | const |
Returns true if this filesystem entry is in a subdirectory of the given directory
– ie, if that directory is a parent or grandparent of this filepath.
directory | The directory that may be a parent of this path. |
depthLimit | Specifies the number of parent paths of this entry that will be checked for equivalence to directory . Setting this to 1 will only allow this function to return true if directory is the directory this entry is in (its direct parent); 2 would return true if directory is this path's directory or the directory's parent directory, etc. |
|
virtualnoexcept |
Returns true if this filesystem entry refers to a directory.
Reimplemented in limes::files::SymLink, limes::files::File, and limes::files::Directory.
|
virtualnoexcept |
Returns true if this filesystem entry refers to a file.
If this path is not an existing directory or symlink, this class will assume that the path refers to a nonexistent file, because a file is assumed to be the "default" type of filesystem object.
Reimplemented in limes::files::SymLink, limes::files::File, and limes::files::Directory.
bool limes::files::FilesystemEntry::isHidden | ( | ) | const |
Returns true if this path is a hidden file or directory.
On Linux and iOS, this returns true if the filename begins with a dot (.) character. On Windows and MacOS, the actual OS filesystem APIs are queried.
|
noexcept |
Returns true if the path this object holds is a relative path.
Note that this function does not always return true if isAbsolutePath()
returns false – if isValid()
returns false, then both isAbsolutePath()
and isRelativePath()
will always return false.
|
virtualnoexcept |
Returns true if this filesystem entry refers to a symbolic link.
Reimplemented in limes::files::SymLink, limes::files::File, and limes::files::Directory.
|
noexcept |
Returns true if this object holds a valid filesystem path.
|
noexcept |
If the path this object holds is already absolute, this function does nothing and returns false.
Otherwise, this function assigns the path to basePath/currentPath
(prepending the basePath
to the previous path this object was already assigned to).
basePath
is not an absolute path, this function always returns false and does not alter the path held by this object.For example:
|
noexcept |
Similar to makeAbsoluteRelativeTo()
, but uses the current working directory as the base path.
|
noexcept |
Attempts to move this filesystem object to the system's trash folder.
|
noexcept |
|
noexcept |
Converts this filesystem entry to the path it holds.
|
noexcept |
Compares this filesystem entry's absolute path with the other one's for equality.
Note that this is a path comparison, and does not compare the contents of files. This comparison attempts to respect the case sensitivity of the underlying filesystem.
|
noexcept |
Compares this filesystem entry's absolute path with the other one's for equality.
Note that this is a path comparison, and does not compare the contents of files. This comparison attempts to respect the case sensitivity of the underlying filesystem.
FilesystemEntry limes::files::FilesystemEntry::operator/ | ( | const std::string_view & | subpathName | ) | const |
Returns a new FilesystemEntry referring to a subpath of this one.
For example:
rename()
function.FilesystemEntry& limes::files::FilesystemEntry::operator/= | ( | const std::string_view & | subpathName | ) |
Assigns this object's path to a subpath of the current path.
For example:
rename()
function.
|
noexcept |
Lexicographically compares this filesystem entry's absolute path with another path.
Directories will always be sorted before other types of filesystem objects; other than this, std::filesystem::path
's built-in lexicographic sort is used.
|
noexcept |
Lexicographically compares this filesystem entry's absolute path with another path.
Directories will always be sorted before other types of filesystem objects; other than this, std::filesystem::path
's built-in lexicographic sort is used.
FilesystemEntry& limes::files::FilesystemEntry::operator= | ( | const Path & | newPath | ) |
Assigns the path this filesystem entry represents.
rename()
function. The path will be normalized using the normalizePath()
function.FilesystemEntry& limes::files::FilesystemEntry::operator= | ( | const std::string_view & | newPath | ) |
Assigns the path this filesystem entry represents.
rename()
function. The path will be normalized using the normalizePath()
function.
|
noexcept |
Compares this filesystem entry's absolute path with the other one's for equality.
Note that this is a path comparison, and does not compare the contents of files. This comparison attempts to respect the case sensitivity of the underlying filesystem.
|
noexcept |
Compares this filesystem entry's absolute path with the other one's for equality.
Note that this is a path comparison, and does not compare the contents of files. This comparison attempts to respect the case sensitivity of the underlying filesystem.
|
noexcept |
Lexicographically compares this filesystem entry's absolute path with another path.
Directories will always be sorted before other types of filesystem objects; other than this, std::filesystem::path
's built-in lexicographic sort is used.
|
noexcept |
Lexicographically compares this filesystem entry's absolute path with another path.
Directories will always be sorted before other types of filesystem objects; other than this, std::filesystem::path
's built-in lexicographic sort is used.
|
noexcept |
Renames this filesystem object to a new path.
The filesystem object is moved on disk, and this object stores the new path.
To manipulate the path this object holds without writing the changes to disk, see assignPath()
, changeName()
, etc.
bool limes::files::FilesystemEntry::revealToUserInFileBrowser | ( | ) | const |
Uses the system's native file browser to display this file to the user.
This will open a Finder window on Mac, a file explorer window on Windows, etc.
This function returns immediately.
|
noexcept |
Sets the permissions for the filesystem entry.
|
virtual |
Returns the size, in bytes, of the filesystem entry referred to by this object.
If the filesystem entry doesn't exist, this will return 0. For directories, this returns the cumulative size of all the directory's contents, recursively. For symbolic links, this returns the size of the link itself, not what it points to.
Reimplemented in limes::files::Directory.
void limes::files::FilesystemEntry::touch | ( | ) | const |
If the filesystem entry this object refers do doesn't exist, this function creates it.
If it did already exist, this function updates its modification time to the current system time.
bool limes::files::FilesystemEntry::touch_noCreate | ( | ) | const |