cppfs  1.2.0.5b71c2c98fb9
Cross-platform C++ file system library supporting multiple backends (Local-FS, SSH)
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
cppfs::FileHandle Class Reference

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...
 
FileHandleoperator= (const FileHandle &fileHandle)
 Copy operator. More...
 
FileHandleoperator= (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< TreereadTree (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< AbstractFileHandleBackendm_backend
 Backend implementation (can be null) More...
 

Detailed Description

Handle for a file or directory.

Remarks
A file handle is used to access a file or directory. It can be obtained either by calling open() on a FileSystem object, or by iterating over directories using the FileSystemIterator.

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.

Member Typedef Documentation

using cppfs::FileHandle::VisitFunc = std::function<bool(FileHandle &)>

Constructor & Destructor Documentation

cppfs::FileHandle::FileHandle ( )

Constructor.

cppfs::FileHandle::FileHandle ( std::unique_ptr< AbstractFileHandleBackend > &&  backend)

Constructor.

Parameters
[in]backendConcrete file handle backend
cppfs::FileHandle::FileHandle ( const FileHandle fileHandle)

Copy constructor.

Parameters
[in]fileHandleSource handle
cppfs::FileHandle::FileHandle ( FileHandle &&  fileHandle)

Move constructor.

Parameters
[in]fileHandleSource handle
virtual cppfs::FileHandle::~FileHandle ( )
virtual

Destructor.

Member Function Documentation

FileHandle& cppfs::FileHandle::operator= ( const FileHandle fileHandle)

Copy operator.

Parameters
[in]fileHandleSource handle
FileHandle& cppfs::FileHandle::operator= ( FileHandle &&  fileHandle)

Move operator.

Parameters
[in]fileHandleSource handle
std::string cppfs::FileHandle::path ( ) const

Get path.

Returns
Path to file or directory
std::string cppfs::FileHandle::fileName ( ) const

Get filename.

Returns
Filename
void cppfs::FileHandle::updateFileInfo ( )

Update file information.

Remarks
Must reload and update the file information. It will for example be called after a file has been created, copied, or removed to ensure that the file information returned by the handle is correct.
bool cppfs::FileHandle::exists ( ) const

Check if file or directory exists.

Returns
'true' if it exists, else 'false'
bool cppfs::FileHandle::isFile ( ) const

Check if item is a file.

Returns
'true' if it is a file, else 'false'
bool cppfs::FileHandle::isDirectory ( ) const

Check if item is a directory.

Returns
'true' if it is a directory, else 'false'
bool cppfs::FileHandle::isSymbolicLink ( ) const

Check if item is a symbolic link.

Returns
'true' if it is a symbolic link, else 'false'
std::vector<std::string> cppfs::FileHandle::listFiles ( ) const

List files in directory.

Returns
List of files, empty list if this is not a valid directory
void cppfs::FileHandle::traverse ( VisitFunc  funcFileEntry)

Traverse directory tree with callback functions.

Parameters
[in]funcFileEntryFunction that is call on each file entry (files and directories)
void cppfs::FileHandle::traverse ( VisitFunc  funcFile,
VisitFunc  funcDirectory 
)

Traverse directory tree with callback functions.

Parameters
[in]funcFileFunction that is call on each file
[in]funcDirectoryFunction that is call on each directory
void cppfs::FileHandle::traverse ( FileVisitor visitor)

Traverse directory tree with a visitor.

Parameters
[in]visitorVisitor 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.

Parameters
[in]pathFile path for the root element
[in]includeHashCompute SHA1 hash of each file? (slow, as each will must be read entirely)
Returns
File tree, nullptr if this file does not exist
FileIterator cppfs::FileHandle::begin ( ) const

Get iterator that points to the first directory entry.

Returns
Iterator
FileIterator cppfs::FileHandle::end ( ) const

Get iterator that points behind the last directory entry.

Returns
Iterator
unsigned int cppfs::FileHandle::size ( ) const

Get file size.

Returns
Size if handle points to a file, else 0
unsigned int cppfs::FileHandle::accessTime ( ) const

Get time of last access.

Returns
Time stamp
unsigned int cppfs::FileHandle::modificationTime ( ) const

Get time of last modification.

Returns
Time stamp
unsigned int cppfs::FileHandle::userId ( ) const

Get ID of owning user.

Returns
User ID
void cppfs::FileHandle::setUserId ( unsigned int  uid)

Set owning user.

Parameters
[in]uidUser ID
unsigned int cppfs::FileHandle::groupId ( ) const

Get ID of owning group.

Returns
Group ID
void cppfs::FileHandle::setGroupId ( unsigned int  gid)

Set owning group.

Parameters
[in]gidGroup ID
unsigned long cppfs::FileHandle::permissions ( ) const

Get file permissions.

Returns
File permissions
void cppfs::FileHandle::setPermissions ( unsigned long  permissions)

Set file permissions.

Parameters
[in]permissionsFile permissions
std::string cppfs::FileHandle::sha1 ( ) const

Compute sha1 hash for file.

Returns
SHA1 hash, "" on error
std::string cppfs::FileHandle::base64 ( ) const

Get base64 encoded file content.

Returns
Base64 encoded file, "" on error
FileHandle cppfs::FileHandle::parentDirectory ( ) const

Get handle to the parent directory.

Returns
File handle
FileHandle cppfs::FileHandle::open ( const std::string &  path) const

Open file using a relative path from the current file/directory.

Parameters
[in]pathRelative path
Returns
File handle
bool cppfs::FileHandle::createDirectory ( )

Create directory.

Returns
'true' if successful, else 'false'
bool cppfs::FileHandle::removeDirectory ( )

Remove directory.

Returns
'true' if successful, else 'false'
Remarks
Only works if the directory exists and is empty. To remove an entire directory tree, see removeDirectoryRec.
void cppfs::FileHandle::copyDirectoryRec ( FileHandle dstDir)

Copy directory recursively.

Parameters
[in]dstDirDestination directory
Remarks
Destination directory points to the actual directory that is to be created, not its parent!

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.

Parameters
[in]destDestination file or directory
Returns
'true' if successful, else 'false'
bool cppfs::FileHandle::move ( FileHandle dest)

Move file.

Parameters
[in]destDestination file or directory
Returns
'true' if successful, else 'false'
bool cppfs::FileHandle::createLink ( FileHandle dest)

Create hard link.

Parameters
[in]destDestination file or directory
Returns
'true' if successful, else 'false'
bool cppfs::FileHandle::createSymbolicLink ( FileHandle dest)

Create symbolic link.

Parameters
[in]destDestination file or directory
Returns
'true' if successful, else 'false'
bool cppfs::FileHandle::rename ( const std::string &  filename)

Rename file or directory.

Parameters
[in]filenameFile name
Returns
'true' if successful, else 'false'
bool cppfs::FileHandle::remove ( )

Remove file.

Returns
'true' if successful, else 'false'
Remarks
Does only work if the handle points to a valid file, not a directory.
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.

Parameters
[in]modeOpening mode flags
Returns
Input stream, null on error
Remarks
The created stream object has to be destroyed be the caller.
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.

Parameters
[in]modeOpening mode flags
Returns
Output stream, null on error
Remarks
The created stream object has to be destroyed be the caller.
std::string cppfs::FileHandle::readFile ( ) const

Read file to string.

Returns
File content, "" on error
bool cppfs::FileHandle::writeFile ( const std::string &  content)

Write string to file.

Parameters
[in]contentFile content
Returns
'true' on success, else 'false'
bool cppfs::FileHandle::writeFileBase64 ( const std::string &  base64)

Write file content from base64 encoded string.

Parameters
[in]base64File content in base64 encoding
Returns
'true' on success, else 'false'
bool cppfs::FileHandle::genericCopy ( FileHandle dest)
protected

Copy file by stream copy.

Parameters
[in]destDestination file or directory
Returns
'true' if successful, else 'false'
bool cppfs::FileHandle::genericMove ( FileHandle dest)
protected

Move file by stream copy and delete.

Parameters
[in]destDestination file or directory
Returns
'true' if successful, else 'false'

Member Data Documentation

std::unique_ptr<AbstractFileHandleBackend> cppfs::FileHandle::m_backend
protected

Backend implementation (can be null)


The documentation for this class was generated from the following file: