SPH
Classes | Enumerations | Functions
FileSystem Namespace Reference

Classes

class  ScopedWorkingDirectory
 
class  DirectoryIterator
 Iterator allowing to enumerate files and subdirectories in given directory. More...
 
class  DirectoryAdapter
 Object providing begin and end directory iterator for given directory path. More...
 
class  FileLock
 Locks a file. More...
 

Enumerations

enum class  PathType { FILE , DIRECTORY , SYMLINK , OTHER }
 
enum class  CreateDirectoryFlag { ALLOW_EXISTING = 1 << 0 }
 
enum class  RemovePathFlag { RECURSIVE = 1 << 1 }
 

Functions

std::string readFile (const Path &path)
 Reads the whole file into the string. More...
 
bool pathExists (const Path &path)
 Checks if a file or directory exists (or more precisely, if a file or directory is accessible). More...
 
Size fileSize (const Path &path)
 Returns the size of a file. More...
 
bool isPathWritable (const Path &path)
 Checks whether the given file is writable. More...
 
Expected< PathgetHomeDirectory ()
 Returns the home directory of the current user. More...
 
Path getAbsolutePath (const Path &relativePath)
 Returns the absolute path to the file. More...
 
Expected< PathTypepathType (const Path &path)
 Returns the type of the given path, or error message if the function fails. More...
 
Outcome createDirectory (const Path &path, const Flags< CreateDirectoryFlag > flags=CreateDirectoryFlag::ALLOW_EXISTING)
 Creates a directory with given path. Creates all parent directories as well. More...
 
Outcome removePath (const Path &path, const Flags< RemovePathFlag > flags=EMPTY_FLAGS)
 
Outcome copyFile (const Path &from, const Path &to)
 Copies a file on given path to a different path. More...
 
Outcome copyDirectory (const Path &from, const Path &to)
 Copies a directory (and all files and subdirectories it contains) to a different path. More...
 
void setWorkingDirectory (const Path &path)
 Changes the current working directory. More...
 
DirectoryAdapter iterateDirectory (const Path &directory)
 
Array< PathgetFilesInDirectory (const Path &directory)
 Alternatitve to iterateDirectory, returning all files in directory in an array. More...
 
bool isFileLocked (const Path &path)
 

Enumeration Type Documentation

◆ CreateDirectoryFlag

Enumerator
ALLOW_EXISTING 

If the named directory already exists, function returns SUCCESS instead of error message.

Definition at line 50 of file FileSystem.h.

◆ PathType

enum FileSystem::PathType
strong
Enumerator
FILE 

Regular file.

DIRECTORY 

Directory.

SYMLINK 

Symbolic link.

OTHER 

Pipe, socket, ...

Definition at line 39 of file FileSystem.h.

◆ RemovePathFlag

Enumerator
RECURSIVE 

Removes also all subdirectories. If not used, removing non-empty directory will return an error. Option has no effect for files.

Definition at line 64 of file FileSystem.h.

Function Documentation

◆ copyDirectory()

Outcome FileSystem::copyDirectory ( const Path from,
const Path to 
)

Copies a directory (and all files and subdirectories it contains) to a different path.

The original directory is not changed. If there is already a directory on target path, it is overriden. All parent directories of target path are created, if necessary.

Definition at line 247 of file FileSystem.cpp.

◆ copyFile()

Outcome FileSystem::copyFile ( const Path from,
const Path to 
)

Copies a file on given path to a different path.

The original file is not changed. If there is already a file on target path, the file is overriden. All parent directories of target path are created, if necessary.

Parameters
fromSource file to be copied.
toTarget path where the copy is saved.
Returns
SUCCESS if the file has been copied, or error message.

Definition at line 219 of file FileSystem.cpp.

◆ createDirectory()

Outcome FileSystem::createDirectory ( const Path path,
const Flags< CreateDirectoryFlag flags = CreateDirectoryFlag::ALLOW_EXISTING 
)

Creates a directory with given path. Creates all parent directories as well.

If empty path is passed, the function returns SUCCESS without actually doing anything, it is therefore possible to use constructs like

createDirectory(file.parentPath());
Outcome createDirectory(const Path &path, const Flags< CreateDirectoryFlag > flags=CreateDirectoryFlag::ALLOW_EXISTING)
Creates a directory with given path. Creates all parent directories as well.
Definition: FileSystem.cpp:119

even with relative paths.

Definition at line 119 of file FileSystem.cpp.

◆ fileSize()

Size FileSystem::fileSize ( const Path path)

Returns the size of a file.

The file must exist and be accessible, checked by assert.

Definition at line 29 of file FileSystem.cpp.

◆ getAbsolutePath()

Path FileSystem::getAbsolutePath ( const Path relativePath)

Returns the absolute path to the file.

Function also resolves all symlinks in the path.

Definition at line 48 of file FileSystem.cpp.

◆ getFilesInDirectory()

Array< Path > FileSystem::getFilesInDirectory ( const Path directory)

Alternatitve to iterateDirectory, returning all files in directory in an array.

Returns relative paths with respect to the given parent directory.

Definition at line 342 of file FileSystem.cpp.

◆ getHomeDirectory()

Expected< Path > FileSystem::getHomeDirectory ( )

Returns the home directory of the current user.

Definition at line 39 of file FileSystem.cpp.

◆ isFileLocked()

bool FileSystem::isFileLocked ( const Path path)

◆ isPathWritable()

bool FileSystem::isPathWritable ( const Path path)

Checks whether the given file is writable.

Definition at line 35 of file FileSystem.cpp.

◆ iterateDirectory()

FileSystem::DirectoryAdapter FileSystem::iterateDirectory ( const Path directory)

Syntactic suggar, function simply returning the DirectoryAdapter for given path.

Usage:

for (Path path : iterateDirectory(parentDir) {
foo(parentDir / path);
}
Object representing a path on a filesystem.
Definition: Path.h:17
DirectoryAdapter iterateDirectory(const Path &directory)
Definition: FileSystem.cpp:338

Definition at line 338 of file FileSystem.cpp.

◆ pathExists()

bool FileSystem::pathExists ( const Path path)

Checks if a file or directory exists (or more precisely, if a file or directory is accessible).

Definition at line 21 of file FileSystem.cpp.

◆ pathType()

Expected< FileSystem::PathType > FileSystem::pathType ( const Path path)

Returns the type of the given path, or error message if the function fails.

Todo:
possibly get the actual error, like in other functions

Definition at line 54 of file FileSystem.cpp.

◆ readFile()

NAMESPACE_SPH_BEGIN std::string FileSystem::readFile ( const Path path)

Reads the whole file into the string.

Returns an empty string if the file does not exist or cannot be opened for reading.

Parameters
pathFile to read

Definition at line 14 of file FileSystem.cpp.

◆ removePath()

Outcome FileSystem::removePath ( const Path path,
const Flags< RemovePathFlag flags = EMPTY_FLAGS 
)

Removes a file or a directory at given path.

Parameters
pathPath to a file or directory to remove.
flagsOptional parameters of the function, see RemovePathFlag enum.
Returns
SUCCESS if the path has been removed, or error message.

Definition at line 137 of file FileSystem.cpp.

◆ setWorkingDirectory()

void FileSystem::setWorkingDirectory ( const Path path)

Changes the current working directory.

Definition at line 273 of file FileSystem.cpp.