libwreport 3.42
wreport::sys Namespace Reference

Operating system functions. More...

Data Structures

struct  Clock
 Access to clock_gettime. More...
 
class  File
 File in the file system. More...
 
class  FileDescriptor
 Common operations on file descriptors. More...
 
struct  ManagedNamedFileDescriptor
 File descriptor that gets automatically closed in the object destructor. More...
 
class  MMap
 Wraps a mmapped memory area, unmapping it on destruction. More...
 
class  NamedFileDescriptor
 File descriptor with a name. More...
 
struct  OverrideEnvironment
 Override an environment variable for the duration of this object. More...
 
struct  OverrideRlimit
 Override a soft resource limit during the lifetime of the object. More...
 
struct  Path
 Wrap a path on the file system opened with O_PATH. More...
 
class  PreserveFileTimes
 RAII mechanism to save restore file times at the end of some file operations. More...
 
class  TempBuffer
 RAII local memory buffer. More...
 
class  Tempdir
 Open a temporary directory. More...
 
class  Tempfile
 Open a temporary file. More...
 

Functions

std::filesystem::path with_suffix (const std::filesystem::path &path, const std::string &suffix)
 Return the path with suffix appended to its filename.
 
std::unique_ptr< struct stat > stat (const char *pathname)
 stat() the given file and return the struct stat with the results.
 
std::unique_ptr< struct stat > stat (const std::string &pathname)
 stat() the given file and return the struct stat with the results.
 
std::unique_ptr< struct stat > stat (const std::filesystem::path &path)
 stat() the given file and return the struct stat with the results.
 
void stat (const char *pathname, struct stat &st)
 stat() the given file filling in the given structure.
 
void stat (const std::string &pathname, struct stat &st)
 stat() the given file filling in the given structure.
 
void stat (const std::filesystem::path &path, struct stat &st)
 stat() the given file filling in the given structure.
 
bool isdir (const std::string &pathname)
 Returns true if the given pathname is a directory, else false.
 
bool isblk (const std::string &pathname)
 Same as isdir but checks for block devices.
 
bool ischr (const std::string &pathname)
 Same as isdir but checks for character devices.
 
bool isfifo (const std::string &pathname)
 Same as isdir but checks for FIFOs.
 
bool islnk (const std::string &pathname)
 Same as isdir but checks for symbolic links.
 
bool isreg (const std::string &pathname)
 Same as isdir but checks for regular files.
 
bool issock (const std::string &pathname)
 Same as isdir but checks for sockets.
 
time_t timestamp (const std::filesystem::path &file)
 File mtime.
 
time_t timestamp (const std::filesystem::path &file, time_t def)
 File mtime (or def if the file does not exist)
 
size_t size (const std::filesystem::path &file)
 File size.
 
size_t size (const std::filesystem::path &file, size_t def)
 File size (or def if the file does not exist)
 
ino_t inode (const std::filesystem::path &file)
 File inode number.
 
ino_t inode (const std::filesystem::path &file, ino_t def)
 File inode number (or 0 if the file does not exist)
 
bool access (const std::filesystem::path &s, int m)
 access() a filename
 
bool exists (const std::string &s)
 Same as access(s, F_OK);.
 
std::string getcwd ()
 Get the absolute path of the current working directory.
 
void chdir (const std::string &dir)
 Change working directory.
 
void chroot (const std::filesystem::path &dir)
 Change root directory.
 
mode_t umask (mode_t mask)
 Change umask (always succeeds and returns the previous umask)
 
std::filesystem::path abspath (const std::filesystem::path &path)
 Get the absolute path of a file.
 
std::string abspath (const std::string &pathname)
 
std::filesystem::path abspath (const char *path)
 
std::string read_file (const std::filesystem::path &file)
 Read whole file into memory. Throws exceptions on failure.
 
std::string read_file (const std::string &file)
 
std::string read_file (const char *file)
 
void write_file (const std::filesystem::path &file, const std::string &data, mode_t mode=0777)
 Write data to file, replacing existing contents if it already exists.
 
void write_file (const std::string &file, const std::string &data, mode_t mode=0777)
 
void write_file (const char *file, const std::string &data, mode_t mode=0777)
 
void write_file (const std::filesystem::path &file, const void *data, size_t size, mode_t mode=0777)
 Write data to file, replacing existing contents if it already exists.
 
void write_file (const std::string &file, const void *data, size_t size, mode_t mode=0777)
 
void write_file (const char *file, const void *data, size_t size, mode_t mode=0777)
 
void write_file_atomically (const std::filesystem::path &file, const std::string &data, mode_t mode=0777)
 Write data to file, replacing existing contents if it already exists.
 
void write_file_atomically (const std::string &file, const std::string &data, mode_t mode=0777)
 
void write_file_atomically (const char *file, const std::string &data, mode_t mode=0777)
 
void write_file_atomically (const std::filesystem::path &file, const void *data, size_t size, mode_t mode=0777)
 Write data to file, replacing existing contents if it already exists.
 
void write_file_atomically (const std::string &file, const void *data, size_t size, mode_t mode=0777)
 
bool unlink_ifexists (const char *file)
 Delete a file if it exists.
 
bool unlink_ifexists (const std::string &file)
 
bool unlink_ifexists (const std::filesystem::path &file)
 
bool rename_ifexists (const std::filesystem::path &src, const std::filesystem::path &dst)
 Move src to dst, without raising exception if src does not exist.
 
bool mkdir_ifmissing (const std::filesystem::path &path)
 Create the given directory, if it does not already exists.
 
bool makedirs (const std::filesystem::path &path)
 Create all the component of the given directory, including the directory itself.
 
std::filesystem::path which (const std::string &name)
 Compute the absolute path of an executable.
 
void unlink (const std::filesystem::path &pathname)
 Delete the file using unlink()
 
void rmdir (const std::filesystem::path &pathname)
 Remove the directory using rmdir(2)
 
void rmtree (const std::filesystem::path &pathname)
 Delete the directory pathname and all its contents.
 
bool rmtree_ifexists (const std::filesystem::path &pathname)
 Delete the directory pathname and all its contents.
 
bool rmtree_ifexists (const std::string &pathname)
 
bool rmtree_ifexists (const char *pathname)
 
void rename (const std::string &src_pathname, const std::string &dst_pathname)
 Rename src_pathname into dst_pathname.
 
void touch (const std::filesystem::path &pathname, time_t ts)
 Set mtime and atime for the file.
 
bool touch_ifexists (const std::filesystem::path &pathname, time_t ts)
 Set mtime and atime for the file.
 
void clock_gettime (::clockid_t clk_id, ::timespec &ts)
 Call clock_gettime, raising an exception if it fails.
 
unsigned long long timesec_elapsed (const ::timespec &begin, const ::timespec &until)
 Return the time elapsed between two timesec structures, in nanoseconds.
 
void getrlimit (int resource, ::rlimit &rlim)
 rlimit wrappers
 
void setrlimit (int resource, const ::rlimit &rlim)
 Call setrlimit, raising an exception if it fails.
 
void breakpoint ()
 Set a breakpoint at this code location.
 

Detailed Description

Operating system functions.

Author
Enrico Zini enric.nosp@m.o@en.nosp@m.ricoz.nosp@m.ini..nosp@m.org

Copyright (C) 2007–2018 Enrico Zini enric.nosp@m.o@de.nosp@m.bian..nosp@m.org

Function Documentation

◆ getrlimit()

void wreport::sys::getrlimit ( int resource,
::rlimit & rlim )

rlimit wrappers

Call getrlimit, raising an exception if it fails

◆ isdir()

bool wreport::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.

◆ makedirs()

bool wreport::sys::makedirs ( const std::filesystem::path & path)

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

Returns
true if the directory was created, false if it already existed.

◆ mkdir_ifmissing()

bool wreport::sys::mkdir_ifmissing ( const std::filesystem::path & path)

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

It will throw an exception if the given pathname already exists but is not a directory.

Returns
true if the directory was created, false if it already existed.

◆ rename()

void wreport::sys::rename ( const std::string & src_pathname,
const std::string & dst_pathname )

Rename src_pathname into dst_pathname.

This is just a wrapper to the rename(2) system call: source and destination must be on the same file system.

◆ rename_ifexists()

bool wreport::sys::rename_ifexists ( const std::filesystem::path & src,
const std::filesystem::path & 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

◆ rmtree_ifexists()

bool wreport::sys::rmtree_ifexists ( const std::filesystem::path & pathname)

Delete the directory pathname and all its contents.

If the directory does not exist, it returns false, else true.

◆ stat() [1/6]

std::unique_ptr< struct stat > wreport::sys::stat ( const char * pathname)

stat() the given file and return the struct stat with the results.

If the file does not exist, return NULL. Raises exceptions in case of errors.

Referenced by wreport::sys::FileDescriptor::close(), wreport::sys::Path::fstatat_ifexists(), wreport::sys::Path::lstatat(), wreport::sys::Path::lstatat_ifexists(), wreport::sys::Path::openat_ifexists(), stat(), stat(), and stat().

◆ stat() [2/6]

void wreport::sys::stat ( const char * 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().

◆ stat() [3/6]

std::unique_ptr< struct stat > wreport::sys::stat ( const std::filesystem::path & path)

stat() the given file and return the struct stat with the results.

If the file does not exist, return NULL. Raises exceptions in case of errors.

◆ stat() [4/6]

void wreport::sys::stat ( const std::filesystem::path & path,
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().

◆ stat() [5/6]

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

stat() the given file and return the struct stat with the results.

If the file does not exist, return NULL. Raises exceptions in case of errors.

◆ stat() [6/6]

void wreport::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().

◆ touch()

void wreport::sys::touch ( const std::filesystem::path & pathname,
time_t ts )

Set mtime and atime for the file.

Throws an error if the file does not exist.

◆ touch_ifexists()

bool wreport::sys::touch_ifexists ( const std::filesystem::path & pathname,
time_t ts )

Set mtime and atime for the file.

Returns false if the file does not exist.

◆ unlink_ifexists()

bool wreport::sys::unlink_ifexists ( const char * 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

◆ which()

std::filesystem::path wreport::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.

◆ with_suffix()

std::filesystem::path wreport::sys::with_suffix ( const std::filesystem::path & path,
const std::string & suffix )

Return the path with suffix appended to its filename.

path needs to have a filename, otherwise the function will throw std::invalid_argument

◆ write_file() [1/2]

void wreport::sys::write_file ( const std::filesystem::path & 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.

◆ write_file() [2/2]

void wreport::sys::write_file ( const std::filesystem::path & file,
const void * data,
size_t size,
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 size().

◆ write_file_atomically() [1/2]

void wreport::sys::write_file_atomically ( const std::filesystem::path & 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.

◆ write_file_atomically() [2/2]

void wreport::sys::write_file_atomically ( const std::filesystem::path & file,
const void * data,
size_t size,
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 size().