Namespaces | Functions
File System

A light-weight file system library. More...

Namespaces

 file_system
 A light-weight file system library based on POSIX.
 

Functions

std::string file_system::get_parent_path (const std::string &path)
 Wraps the dirname function to get the pathname of the parent directory. More...
 
std::string file_system::get_file_name (const std::string &file_path)
 Given a path to a file returns the file name. More...
 
std::string file_system::get_absolute_path (const std::string &rel_path)
 Get the absolute path, resolving symlinks. More...
 
void file_system::create_directory (const std::string &dir, double wait_time=1, size_t num_tries=40)
 Creates a directory, including any parents that don't exist. If the directory exists create_directory does nothing. More...
 
bool file_system::check_if_dir_exists (const std::string &dir)
 Returns true if the directory exists. More...
 
bool file_system::check_if_file_exists (const std::string &file)
 Returns true if the regular file or link to the regular file exists. More...
 
bool file_system::is_file (const std::string &path)
 Returns true if the path points to a regular file or a link to a regular file. More...
 
size_t file_system::file_size (const std::string &file)
 Returns the file size in bytes. More...
 
std::string file_system::cwd ()
 Returns the current working directory, resolving symlinks.
 
std::vector< std::stringfile_system::ls (const std::string &dir_name="./")
 Gets a list of files in a directory. More...
 
void file_system::rm (const std::string &path, bool recursive)
 Deletes a file or directory. More...
 

Detailed Description

A light-weight file system library.

Function Documentation

◆ check_if_dir_exists()

bool file_system::check_if_dir_exists ( const std::string dir)

Returns true if the directory exists.

Returns: true if the directory exists

◆ check_if_file_exists()

bool file_system::check_if_file_exists ( const std::string file)

Returns true if the regular file or link to the regular file exists.

Note
See the stat(2) documentation, e.g. at http://man7.org/linux/man-pages/man2/stat.2.html for details.

Returns: true if the file exists

◆ create_directory()

void file_system::create_directory ( const std::string dir,
double  wait_time = 1,
size_t  num_tries = 40 
)

Creates a directory, including any parents that don't exist. If the directory exists create_directory does nothing.

Requires: permissions to create dir on the filesystem

Effects: creates the directory dir on the filesystem

Parameters
dirthe path where to create the directory
wait_timetime to wait in seconds between failures
num_triesnumber of attempts to create directory (for slow filesystems)

◆ file_size()

size_t file_system::file_size ( const std::string file)

Returns the file size in bytes.

Requires: file is a valid file on the filesystem

Returns: size of file in bytes

◆ get_absolute_path()

std::string file_system::get_absolute_path ( const std::string rel_path)

Get the absolute path, resolving symlinks.

Requires: rel_path is a valid path on the filesystem

Returns: the absolute path

◆ get_file_name()

std::string file_system::get_file_name ( const std::string file_path)

Given a path to a file returns the file name.

Example

CHECK("dummy.txt"s ==
file_system::get_file_name("/test/path/to/dir/dummy.txt"));
CHECK(".dummy.txt"s ==
file_system::get_file_name("/test/path/to/dir/.dummy.txt"));
CHECK("dummy.txt"s == file_system::get_file_name("./dummy.txt"));
CHECK("dummy.txt"s == file_system::get_file_name("../dummy.txt"));
CHECK(".dummy.txt"s == file_system::get_file_name(".dummy.txt"));
CHECK("dummy.txt"s == file_system::get_file_name("dummy.txt"));
CHECK(".dummy"s == file_system::get_file_name(".dummy"));

Requires: file_path is a valid path on the filesystem

Returns: the file name

◆ get_parent_path()

std::string file_system::get_parent_path ( const std::string path)

Wraps the dirname function to get the pathname of the parent directory.

See the opengroup documentation: http://pubs.opengroup.org/onlinepubs/9699919799/functions/dirname.html

Example

CHECK("/test/path/to/dir"s ==
file_system::get_parent_path("/test/path/to/dir/dummy.txt"));
CHECK("/test/path/to"s == file_system::get_parent_path("/test/path/to/dir/"));
CHECK("/"s == file_system::get_parent_path("/"));
CHECK("path/to/dir"s ==
file_system::get_parent_path("path/to/dir/dummy.txt"));
CHECK("/usr"s == file_system::get_parent_path("/usr/lib/"));
CHECK("/"s == file_system::get_parent_path("/usr"));
CHECK("."s == file_system::get_parent_path("usr"));
CHECK("."s == file_system::get_parent_path(".."));
CHECK("."s == file_system::get_parent_path(""));

Requires: path is a valid path on the filesystem

Returns: the path to the parent directory

◆ is_file()

bool file_system::is_file ( const std::string path)

Returns true if the path points to a regular file or a link to a regular file.

Note
See the stat(2) documentation, e.g. at http://man7.org/linux/man-pages/man2/stat.2.html for details.

Requires: path is a valid path on the filesystem

Returns: true if file is a file, not a directory

◆ ls()

std::vector< std::string > file_system::ls ( const std::string dir_name = "./")

Gets a list of files in a directory.

Returns: vector of all files and directories inside dir_name

◆ rm()

void file_system::rm ( const std::string path,
bool  recursive 
)

Deletes a file or directory.

Requires: path be a valid path on the filesystem

Effects: deletes path from the filesystem, if recursive is true then behaves like rm -r, otherwise like rm but will delete an empty directory