libept
Classes | Functions
ept::sys Namespace Reference

Classes

class  File
 open(2) file descriptors More...
 
class  FileDescriptor
 Common operations on file descriptors. More...
 
class  MMap
 Wraps a mmapped memory area, unmapping it on destruction. More...
 
class  NamedFileDescriptor
 File descriptor with a name. More...
 
struct  Path
 Wrap a path on the file system opened with O_PATH. More...
 

Functions

std::unique_ptr< struct stat > stat (const std::string &pathname)
 stat() the given file and return the struct stat with the results. More...
 
void stat (const std::string &pathname, struct stat &st)
 stat() the given file filling in the given structure. More...
 
bool isdir (const std::string &pathname)
 Returns true if the given pathname is a directory, else false. More...
 
bool isblk (const std::string &pathname)
 Same as isdir but checks for block devices. More...
 
bool ischr (const std::string &pathname)
 Same as isdir but checks for character devices. More...
 
bool isfifo (const std::string &pathname)
 Same as isdir but checks for FIFOs. More...
 
bool islnk (const std::string &pathname)
 Same as isdir but checks for symbolic links. More...
 
bool isreg (const std::string &pathname)
 Same as isdir but checks for regular files. More...
 
bool issock (const std::string &pathname)
 Same as isdir but checks for sockets. More...
 
time_t timestamp (const std::string &file)
 File mtime. More...
 
time_t timestamp (const std::string &file, time_t def)
 File mtime (or def if the file does not exist) More...
 
size_t size (const std::string &file)
 File size. More...
 
size_t size (const std::string &file, size_t def)
 File size (or def if the file does not exist) More...
 
ino_t inode (const std::string &file)
 File inode number. More...
 
ino_t inode (const std::string &file, ino_t def)
 File inode number (or 0 if the file does not exist) More...
 
bool access (const std::string &s, int m)
 access() a filename More...
 
bool exists (const std::string &s)
 Same as access(s, F_OK);. More...
 
std::string getcwd ()
 Get the absolute path of the current working directory. More...
 
std::string abspath (const std::string &pathname)
 Get the absolute path of a file. More...
 
std::string read_file (const std::string &file)
 Read whole file into memory. Throws exceptions on failure. More...
 
void write_file (const std::string &file, const std::string &data, mode_t mode=0777)
 Write data to file, replacing existing contents if it already exists. More...
 
void write_file_atomically (const std::string &file, const std::string &data, mode_t mode=0777)
 Write data to file, replacing existing contents if it already exists. More...
 
bool unlink_ifexists (const std::string &file)
 Delete a file if it exists. More...
 
bool rename_ifexists (const std::string &src, const std::string &dst)
 Move src to dst, without raising exception if src does not exist. More...
 
void mkdir_ifmissing (const char *pathname, mode_t mode=0777)
 Create the given directory, if it does not already exists. More...
 
void mkdir_ifmissing (const std::string &pathname, mode_t mode)
 
void makedirs (const std::string &pathname, mode_t=0777)
 Create all the component of the given directory, including the directory itself. More...
 
std::string which (const std::string &name)
 Compute the absolute path of an executable. More...
 
void unlink (const std::string &pathname)
 Delete the file using unlink() More...
 
void rmdir (const std::string &pathname)
 Remove the directory using rmdir(2) More...
 
void rmtree (const std::string &pathname)
 Delete the directory pathname and all its contents. More...
 

Function Documentation

◆ abspath()

std::string ept::sys::abspath ( const std::string &  pathname)

Get the absolute path of a file.

References getcwd(), ept::str::joinpath(), and ept::str::normpath().

Referenced by which().

◆ access()

bool ept::sys::access ( const std::string &  s,
int  m 
)

access() a filename

Referenced by exists(), and which().

◆ exists()

bool ept::sys::exists ( const std::string &  file)

◆ getcwd()

std::string ept::sys::getcwd ( )

Get the absolute path of the current working directory.

References size().

Referenced by abspath().

◆ inode() [1/2]

ino_t ept::sys::inode ( const std::string &  file)

File inode number.

References stat().

◆ inode() [2/2]

ino_t ept::sys::inode ( const std::string &  file,
ino_t  def 
)

File inode number (or 0 if the file does not exist)

References stat().

◆ isblk()

bool ept::sys::isblk ( const std::string &  pathname)

Same as isdir but checks for block devices.

References common_stat_body.

Referenced by ept::sys::Path::iterator::operator->().

◆ ischr()

bool ept::sys::ischr ( const std::string &  pathname)

Same as isdir but checks for character devices.

References common_stat_body.

Referenced by ept::sys::Path::iterator::operator->().

◆ isdir()

bool ept::sys::isdir ( const std::string &  pathname)

Returns true if the given pathname is a directory, else false.

It also returns false if the pathname does not exist.

References common_stat_body.

Referenced by ept::sys::Path::iterator::operator->().

◆ isfifo()

bool ept::sys::isfifo ( const std::string &  pathname)

Same as isdir but checks for FIFOs.

References common_stat_body.

Referenced by ept::sys::Path::iterator::operator->().

◆ islnk()

bool ept::sys::islnk ( const std::string &  pathname)

Same as isdir but checks for symbolic links.

References common_stat_body.

Referenced by ept::sys::Path::iterator::operator->().

◆ isreg()

bool ept::sys::isreg ( const std::string &  pathname)

Same as isdir but checks for regular files.

References common_stat_body.

Referenced by ept::sys::Path::iterator::operator->().

◆ issock()

bool ept::sys::issock ( const std::string &  pathname)

Same as isdir but checks for sockets.

References common_stat_body.

Referenced by ept::sys::Path::iterator::operator->().

◆ makedirs()

void ept::sys::makedirs ( const std::string &  pathname,
mode_t  = 0777 
)

Create all the component of the given directory, including the directory itself.

References ept::str::dirname(), and mkdir_ifmissing().

◆ mkdir_ifmissing() [1/2]

void ept::sys::mkdir_ifmissing ( const char *  pathname,
mode_t  mode = 0777 
)

Create the given directory, if it does not already exists.

It will complain if the given pathname already exists but is not a directory.

Referenced by makedirs().

◆ mkdir_ifmissing() [2/2]

void ept::sys::mkdir_ifmissing ( const std::string &  pathname,
mode_t  mode 
)

◆ read_file()

std::string ept::sys::read_file ( const std::string &  file)

Read whole file into memory. Throws exceptions on failure.

References ept::sys::FileDescriptor::fstat(), ept::sys::FileDescriptor::mmap(), and stat().

◆ rename_ifexists()

bool ept::sys::rename_ifexists ( const std::string &  src,
const std::string &  dst 
)

Move src to dst, without raising exception if src does not exist.

Returns
true if the file was renamed, false if it did not exist

◆ rmdir()

void ept::sys::rmdir ( const std::string &  pathname)

Remove the directory using rmdir(2)

Referenced by ept::sys::Path::rmtree().

◆ rmtree()

void ept::sys::rmtree ( const std::string &  pathname)

Delete the directory pathname and all its contents.

References ept::sys::Path::rmtree().

Referenced by ept::sys::Path::iterator::operator->().

◆ size() [1/2]

size_t ept::sys::size ( const std::string &  file)

◆ size() [2/2]

size_t ept::sys::size ( const std::string &  file,
size_t  def 
)

File size (or def if the file does not exist)

References stat().

◆ stat() [1/2]

std::unique_ptr< struct stat > ept::sys::stat ( const std::string &  pathname)

◆ stat() [2/2]

void ept::sys::stat ( const std::string &  pathname,
struct stat &  st 
)

stat() the given file filling in the given structure.

Raises exceptions in case of errors, including if the file does not exist.

References stat().

◆ timestamp() [1/2]

time_t ept::sys::timestamp ( const std::string &  file)

◆ timestamp() [2/2]

time_t ept::sys::timestamp ( const std::string &  file,
time_t  def 
)

File mtime (or def if the file does not exist)

References stat().

◆ unlink()

void ept::sys::unlink ( const std::string &  pathname)

Delete the file using unlink()

Referenced by unlink_ifexists().

◆ unlink_ifexists()

bool ept::sys::unlink_ifexists ( const std::string &  file)

Delete a file if it exists.

If it does not exist, do nothing.

Returns
true if the file was deleted, false if it did not exist

References unlink().

◆ which()

std::string ept::sys::which ( const std::string &  name)

Compute the absolute path of an executable.

If name is specified as a partial path, it ensures it is made absolute. If name is not specified as a path, it looks for the executable in $PATH and return its absolute pathname.

References abspath(), access(), ept::str::joinpath(), and ept::sys::NamedFileDescriptor::name().

◆ write_file()

void ept::sys::write_file ( const std::string &  file,
const std::string &  data,
mode_t  mode = 0777 
)

Write data to file, replacing existing contents if it already exists.

New files are created with the given permission mode, honoring umask. Permissions of existing files do not change.

References ept::sys::FileDescriptor::close(), and ept::sys::FileDescriptor::write_all().

◆ write_file_atomically()

void ept::sys::write_file_atomically ( const std::string &  file,
const std::string &  data,
mode_t  mode = 0777 
)

Write data to file, replacing existing contents if it already exists.

Files are created with the given permission mode, honoring umask. If the file already exists, its mode is ignored.

Data is written to a temporary file, then moved to its final destination, to ensure an atomic operation.

References ept::sys::FileDescriptor::close(), ept::sys::FileDescriptor::fchmod(), ept::sys::File::mkstemp(), ept::sys::NamedFileDescriptor::name(), and ept::sys::FileDescriptor::write_all().

Referenced by ept::debtags::Vocabulary::write().