wibble::sys::fs Namespace Reference

## Classes

class | Directory
| Nicely wrap access to directories. More...
struct | Lockfile
| RAII lock file. More...

## Functions

std::string | readFile (const std::string &file)
| Read whole file into memory. Throws exceptions on failure. More...
std::string | readFile (std::istream &file, const std::string &filename)
| Read the entire contents of a file into a string. More...
void | writeFile (const std::string &file, const std::string &data)
| Write data to file, replacing existing contents if it already exists. More...
void | writeFileAtomically (const std::string &file, const std::string &data)
| Write data to file, replacing existing contents if it already exists. More...
std::string | findExecutable (const std::string &name)
| Compute the absolute path of an executable. More...
bool | deleteIfExists (const std::string &file)
| Delete a file if it exists. More...
void | renameIfExists (const std::string &src, const std::string &dst)
| Move src to dst, without raising exception if src does not exist. More...
void | unlink (const std::string &fname)
| Delete the file. More...
void | rmdir (const std::string &dirname)
| Remove the directory using rmdir(2) 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...
std::auto_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 | 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 | abspath (const std::string &pathname)
| Get the absolute path of a file. More...
std::string | mkdtemp (std::string templ)
void | mkdirIfMissing (const std::string &dir, mode_t mode=0777)
| Create the given directory, if it does not already exists. More...
void | mkpath (const std::string &dir)
| Create all the component of the given directory, including the directory itself. More...
void | mkFilePath (const std::string &file)
| Ensure that the path to the given file exists, creating it if it does not. More...
void | rmtree (const std::string &dir)
| Delete the directory dir and all its content. 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...

## Function Documentation

## abspath()

std::string wibble::sys::fs::abspath | ( | const std::string & | pathname | ) |

Get the absolute path of a file.

Referenced by findExecutable().

## access()

bool wibble::sys::fs::access | ( | const std::string & | s,
|  | int | m
| ) |  |

access() a filename

Referenced by TestFs::_mkdirIfMissing(), TestFs::_mkFilePath(), TestFs::_mkPath2(), findExecutable(), and TestException::system().

## deleteIfExists()

bool wibble::sys::fs::deleteIfExists | ( | 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().

Referenced by TestFs::_deleteIfExists().

## exists()

bool wibble::sys::fs::exists | ( | const std::string & | s | ) |

Same as access(s, F_OK);.

Referenced by wibble::tests::TestFileExists::check(), wibble::tests::test_assert_file_exists(), and wibble::tests::test_assert_not_file_exists().

## findExecutable()

std::string wibble::sys::fs::findExecutable | ( | 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(), wibble::str::Split::begin(), wibble::str::Split::end(), and wibble::str::joinpath().

## inode() [1/2]

ino_t wibble::sys::fs::inode | ( | const std::string & | file | ) |

File inode number.

References stat().

## inode() [2/2]

ino_t wibble::sys::fs::inode | ( | const std::string & | file,
|  | ino_t | def
| ) |  |

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

References stat().

## isblk()

bool wibble::sys::fs::isblk | ( | const std::string & | pathname | ) |

Same as isdir but checks for block devices.

## ischr()

bool wibble::sys::fs::ischr | ( | const std::string & | pathname | ) |

Same as isdir but checks for character devices.

## isdir()

bool wibble::sys::fs::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.

Referenced by TestFs::_isdir().

## isfifo()

bool wibble::sys::fs::isfifo | ( | const std::string & | pathname | ) |

Same as isdir but checks for FIFOs.

## islnk()

bool wibble::sys::fs::islnk | ( | const std::string & | pathname | ) |

Same as isdir but checks for symbolic links.

## isreg()

bool wibble::sys::fs::isreg | ( | const std::string & | pathname | ) |

Same as isdir but checks for regular files.

## issock()

bool wibble::sys::fs::issock | ( | const std::string & | pathname | ) |

Same as isdir but checks for sockets.

## mkdirIfMissing()

void wibble::sys::fs::mkdirIfMissing | ( | const std::string & | dir,
|  | 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 TestFs::_mkdirIfMissing().

## mkdtemp()

std::string wibble::sys::fs::mkdtemp | ( | std::string | templ | ) |

## mkFilePath()

void wibble::sys::fs::mkFilePath | ( | const std::string & | file | ) |

Ensure that the path to the given file exists, creating it if it does not.

The file itself will not get created.

Referenced by TestFs::_mkFilePath().

## mkpath()

void wibble::sys::fs::mkpath | ( | const std::string & | dir | ) |

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

Referenced by TestFs::_mkPath(), and TestFs::_mkPath2().

## readFile() [1/2]

std::string wibble::sys::fs::readFile | ( | const std::string & | file | ) |

Read whole file into memory. Throws exceptions on failure.

References wibble::list::end().

Referenced by TestFs::writeFileAtomically().

## readFile() [2/2]

std::string wibble::sys::fs::readFile | ( | std::istream & | file,
|  | const std::string & | filename
| ) |  |

Read the entire contents of a file into a string.

Parameters

  filename | name or description of the stream we are reading. Used only for error messages.

## renameIfExists()

void wibble::sys::fs::renameIfExists | ( | const std::string & | src,
|  | const std::string & | dst
| ) |  |

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

## rmdir()

void wibble::sys::fs::rmdir | ( | const std::string & | dirname | ) |

Remove the directory using rmdir(2)

References wibble::str::dirname().

## rmtree()

void wibble::sys::fs::rmtree | ( | const std::string & | dir | ) |

Delete the directory dir and all its content.

## size() [1/2]

size_t wibble::sys::fs::size | ( | const std::string & | file | ) |

File size.

References stat().

Referenced by wibble::net::http::Request::read_buf(), wibble::exception::Regexp::Regexp(), and wibble::net::http::Request::send_date_header().

## size() [2/2]

size_t wibble::sys::fs::size | ( | const std::string & | file,
|  | size_t | def
| ) |  |

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

References stat().

## stat() [1/2]

std::auto_ptr<struct stat> wibble::sys::fs::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.

Referenced by inode(), size(), and timestamp().

## stat() [2/2]

void wibble::sys::fs::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.

## timestamp() [1/2]

time_t wibble::sys::fs::timestamp | ( | const std::string & | file | ) |

File mtime.

References stat().

Referenced by TestFs::timestamp().

## timestamp() [2/2]

time_t wibble::sys::fs::timestamp | ( | const std::string & | file,
|  | time_t | def
| ) |  |

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

References stat().

## unlink()

void wibble::sys::fs::unlink | ( | const std::string & | fname | ) |

Delete the file.

Referenced by deleteIfExists(), and TestFs::timestamp().

## writeFile()

void wibble::sys::fs::writeFile | ( | const std::string & | file,
|  | const std::string & | data
| ) |  |

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

Referenced by TestFs::timestamp().

## writeFileAtomically()

void wibble::sys::fs::writeFileAtomically | ( | const std::string & | file,
|  | const std::string & | data
| ) |  |

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

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

References wibble::str::fmtf().

Referenced by TestFs::writeFileAtomically().