cppfs
1.2.0.5b71c2c98fb9
Cross-platform C++ file system library supporting multiple backends (Local-FS, SSH)
|
Handle for a file or directory. More...
#include <cppfs/include/cppfs/FileHandle.h>
Public Types | |
using | VisitFunc = std::function< bool(FileHandle &)> |
Public Member Functions | |
FileHandle () | |
Constructor. More... | |
FileHandle (std::unique_ptr< AbstractFileHandleBackend > &&backend) | |
Constructor. More... | |
FileHandle (const FileHandle &fileHandle) | |
Copy constructor. More... | |
FileHandle (FileHandle &&fileHandle) | |
Move constructor. More... | |
virtual | ~FileHandle () |
Destructor. More... | |
FileHandle & | operator= (const FileHandle &fileHandle) |
Copy operator. More... | |
FileHandle & | operator= (FileHandle &&fileHandle) |
Move operator. More... | |
std::string | path () const |
Get path. More... | |
std::string | fileName () const |
Get filename. More... | |
void | updateFileInfo () |
Update file information. More... | |
bool | exists () const |
Check if file or directory exists. More... | |
bool | isFile () const |
Check if item is a file. More... | |
bool | isDirectory () const |
Check if item is a directory. More... | |
bool | isSymbolicLink () const |
Check if item is a symbolic link. More... | |
std::vector< std::string > | listFiles () const |
List files in directory. More... | |
void | traverse (VisitFunc funcFileEntry) |
Traverse directory tree with callback functions. More... | |
void | traverse (VisitFunc funcFile, VisitFunc funcDirectory) |
Traverse directory tree with callback functions. More... | |
void | traverse (FileVisitor &visitor) |
Traverse directory tree with a visitor. More... | |
std::unique_ptr< Tree > | readTree (const std::string &path="", bool includeHash=false) const |
Read directory tree. More... | |
FileIterator | begin () const |
Get iterator that points to the first directory entry. More... | |
FileIterator | end () const |
Get iterator that points behind the last directory entry. More... | |
unsigned int | size () const |
Get file size. More... | |
unsigned int | accessTime () const |
Get time of last access. More... | |
unsigned int | modificationTime () const |
Get time of last modification. More... | |
unsigned int | userId () const |
Get ID of owning user. More... | |
void | setUserId (unsigned int uid) |
Set owning user. More... | |
unsigned int | groupId () const |
Get ID of owning group. More... | |
void | setGroupId (unsigned int gid) |
Set owning group. More... | |
unsigned long | permissions () const |
Get file permissions. More... | |
void | setPermissions (unsigned long permissions) |
Set file permissions. More... | |
std::string | sha1 () const |
Compute sha1 hash for file. More... | |
std::string | base64 () const |
Get base64 encoded file content. More... | |
FileHandle | parentDirectory () const |
Get handle to the parent directory. More... | |
FileHandle | open (const std::string &path) const |
Open file using a relative path from the current file/directory. More... | |
bool | createDirectory () |
Create directory. More... | |
bool | removeDirectory () |
Remove directory. More... | |
void | copyDirectoryRec (FileHandle &dstDir) |
Copy directory recursively. More... | |
void | removeDirectoryRec () |
Remove directory recursively. More... | |
bool | copy (FileHandle &dest) |
Copy file. More... | |
bool | move (FileHandle &dest) |
Move file. More... | |
bool | createLink (FileHandle &dest) |
Create hard link. More... | |
bool | createSymbolicLink (FileHandle &dest) |
Create symbolic link. More... | |
bool | rename (const std::string &filename) |
Rename file or directory. More... | |
bool | remove () |
Remove file. More... | |
std::unique_ptr< std::istream > | createInputStream (std::ios_base::openmode mode=std::ios_base::in) const |
Create input stream to read from the file. More... | |
std::unique_ptr< std::ostream > | createOutputStream (std::ios_base::openmode mode=std::ios_base::out) |
Create output stream to write to the file. More... | |
std::string | readFile () const |
Read file to string. More... | |
bool | writeFile (const std::string &content) |
Write string to file. More... | |
bool | writeFileBase64 (const std::string &base64) |
Write file content from base64 encoded string. More... | |
Protected Member Functions | |
bool | genericCopy (FileHandle &dest) |
Copy file by stream copy. More... | |
bool | genericMove (FileHandle &dest) |
Move file by stream copy and delete. More... | |
Protected Attributes | |
std::unique_ptr< AbstractFileHandleBackend > | m_backend |
Backend implementation (can be null) More... | |
Handle for a file or directory.
If the handle points to a valid file or directory, exists() returns true, otherwise false. The type of the entry can be determined by the methods isFile() and isDirectory().
File handles can be copied or moved. Overhead for these functions are limited, so for example, file or directories will not be opened automatically.
using cppfs::FileHandle::VisitFunc = std::function<bool(FileHandle &)> |
cppfs::FileHandle::FileHandle | ( | ) |
Constructor.
cppfs::FileHandle::FileHandle | ( | std::unique_ptr< AbstractFileHandleBackend > && | backend | ) |
Constructor.
[in] | backend | Concrete file handle backend |
cppfs::FileHandle::FileHandle | ( | const FileHandle & | fileHandle | ) |
Copy constructor.
[in] | fileHandle | Source handle |
cppfs::FileHandle::FileHandle | ( | FileHandle && | fileHandle | ) |
Move constructor.
[in] | fileHandle | Source handle |
|
virtual |
Destructor.
FileHandle& cppfs::FileHandle::operator= | ( | const FileHandle & | fileHandle | ) |
Copy operator.
[in] | fileHandle | Source handle |
FileHandle& cppfs::FileHandle::operator= | ( | FileHandle && | fileHandle | ) |
Move operator.
[in] | fileHandle | Source handle |
std::string cppfs::FileHandle::path | ( | ) | const |
Get path.
std::string cppfs::FileHandle::fileName | ( | ) | const |
Get filename.
void cppfs::FileHandle::updateFileInfo | ( | ) |
Update file information.
bool cppfs::FileHandle::exists | ( | ) | const |
Check if file or directory exists.
bool cppfs::FileHandle::isFile | ( | ) | const |
Check if item is a file.
bool cppfs::FileHandle::isDirectory | ( | ) | const |
Check if item is a directory.
bool cppfs::FileHandle::isSymbolicLink | ( | ) | const |
Check if item is a symbolic link.
std::vector<std::string> cppfs::FileHandle::listFiles | ( | ) | const |
List files in directory.
void cppfs::FileHandle::traverse | ( | VisitFunc | funcFileEntry | ) |
Traverse directory tree with callback functions.
[in] | funcFileEntry | Function that is call on each file entry (files and directories) |
Traverse directory tree with callback functions.
[in] | funcFile | Function that is call on each file |
[in] | funcDirectory | Function that is call on each directory |
void cppfs::FileHandle::traverse | ( | FileVisitor & | visitor | ) |
Traverse directory tree with a visitor.
[in] | visitor | Visitor that is invoked for each entry in the directory tree |
std::unique_ptr<Tree> cppfs::FileHandle::readTree | ( | const std::string & | path = "" , |
bool | includeHash = false |
||
) | const |
Read directory tree.
[in] | path | File path for the root element |
[in] | includeHash | Compute SHA1 hash of each file? (slow, as each will must be read entirely) |
FileIterator cppfs::FileHandle::begin | ( | ) | const |
Get iterator that points to the first directory entry.
FileIterator cppfs::FileHandle::end | ( | ) | const |
Get iterator that points behind the last directory entry.
unsigned int cppfs::FileHandle::size | ( | ) | const |
Get file size.
unsigned int cppfs::FileHandle::accessTime | ( | ) | const |
Get time of last access.
unsigned int cppfs::FileHandle::modificationTime | ( | ) | const |
Get time of last modification.
unsigned int cppfs::FileHandle::userId | ( | ) | const |
Get ID of owning user.
void cppfs::FileHandle::setUserId | ( | unsigned int | uid | ) |
Set owning user.
[in] | uid | User ID |
unsigned int cppfs::FileHandle::groupId | ( | ) | const |
Get ID of owning group.
void cppfs::FileHandle::setGroupId | ( | unsigned int | gid | ) |
Set owning group.
[in] | gid | Group ID |
unsigned long cppfs::FileHandle::permissions | ( | ) | const |
Get file permissions.
void cppfs::FileHandle::setPermissions | ( | unsigned long | permissions | ) |
Set file permissions.
[in] | permissions | File permissions |
std::string cppfs::FileHandle::sha1 | ( | ) | const |
Compute sha1 hash for file.
std::string cppfs::FileHandle::base64 | ( | ) | const |
Get base64 encoded file content.
FileHandle cppfs::FileHandle::parentDirectory | ( | ) | const |
Get handle to the parent directory.
FileHandle cppfs::FileHandle::open | ( | const std::string & | path | ) | const |
Open file using a relative path from the current file/directory.
[in] | path | Relative path |
bool cppfs::FileHandle::createDirectory | ( | ) |
Create directory.
bool cppfs::FileHandle::removeDirectory | ( | ) |
Remove directory.
void cppfs::FileHandle::copyDirectoryRec | ( | FileHandle & | dstDir | ) |
Copy directory recursively.
[in] | dstDir | Destination directory |
Example: FileHandle dir = fs::open("/projects/project1"); dir.copyDirectory(fs::open("/backup/projects/project1"))
void cppfs::FileHandle::removeDirectoryRec | ( | ) |
Remove directory recursively.
bool cppfs::FileHandle::copy | ( | FileHandle & | dest | ) |
Copy file.
[in] | dest | Destination file or directory |
bool cppfs::FileHandle::move | ( | FileHandle & | dest | ) |
Move file.
[in] | dest | Destination file or directory |
bool cppfs::FileHandle::createLink | ( | FileHandle & | dest | ) |
Create hard link.
[in] | dest | Destination file or directory |
bool cppfs::FileHandle::createSymbolicLink | ( | FileHandle & | dest | ) |
Create symbolic link.
[in] | dest | Destination file or directory |
bool cppfs::FileHandle::rename | ( | const std::string & | filename | ) |
Rename file or directory.
[in] | filename | File name |
bool cppfs::FileHandle::remove | ( | ) |
Remove file.
std::unique_ptr<std::istream> cppfs::FileHandle::createInputStream | ( | std::ios_base::openmode | mode = std::ios_base::in | ) | const |
Create input stream to read from the file.
[in] | mode | Opening mode flags |
std::unique_ptr<std::ostream> cppfs::FileHandle::createOutputStream | ( | std::ios_base::openmode | mode = std::ios_base::out | ) |
Create output stream to write to the file.
[in] | mode | Opening mode flags |
std::string cppfs::FileHandle::readFile | ( | ) | const |
Read file to string.
bool cppfs::FileHandle::writeFile | ( | const std::string & | content | ) |
Write string to file.
[in] | content | File content |
bool cppfs::FileHandle::writeFileBase64 | ( | const std::string & | base64 | ) |
Write file content from base64 encoded string.
[in] | base64 | File content in base64 encoding |
|
protected |
Copy file by stream copy.
[in] | dest | Destination file or directory |
|
protected |
Move file by stream copy and delete.
[in] | dest | Destination file or directory |
|
protected |
Backend implementation (can be null)