fs.h
Go to the documentation of this file.
#ifndef WIBBLE_SYS_DIRECTORY_H
#define WIBBLE_SYS_DIRECTORY_H
#include <string>
#include <iosfwd>
#include <memory> // auto_ptr
#include <sys/types.h> // mode_t
#include <sys/stat.h> // struct stat
#include <unistd.h> // access
struct dirent;
namespace wibble {
namespace sys {
namespace fs {
std::auto_ptr<struct stat> stat(const std::string& pathname);
void stat(const std::string& pathname, struct stat& st);
bool access(const std::string& s, int m);
bool exists(const std::string& s);
std::string abspath(const std::string& pathname);
// Create a temporary directory based on a template.
std::string mkdtemp( std::string templ );
void mkdirIfMissing(const std::string& dir, mode_t mode = 0777);
void mkpath(const std::string& dir);
void mkFilePath(const std::string& file);
std::string readFile(const std::string &file);
std::string readFile(std::istream& file, const std::string& filename);
void writeFile(const std::string &file, const std::string &data);
void writeFileAtomically(const std::string &file, const std::string &data);
std::string findExecutable(const std::string& name);
bool deleteIfExists(const std::string& file);
void renameIfExists(const std::string& src, const std::string& dst);
void unlink(const std::string& fname);
void rmdir(const std::string& dirname);
void rmtree(const std::string& dir);
bool isdir(const std::string& pathname);
bool isblk(const std::string& pathname);
bool ischr(const std::string& pathname);
bool isfifo(const std::string& pathname);
bool islnk(const std::string& pathname);
bool isreg(const std::string& pathname);
bool issock(const std::string& pathname);
time_t timestamp(const std::string& file);
time_t timestamp(const std::string& file, time_t def);
size_t size(const std::string& file);
size_t size(const std::string& file, size_t def);
ino_t inode(const std::string& file);
ino_t inode(const std::string& file, ino_t def);
class Directory
{
protected:
std::string m_path;
public:
class const_iterator
{
const Directory* dir;
void* dirp;
struct dirent* direntbuf;
public:
// Create an end iterator
const_iterator();
// Create a begin iterator
const_iterator(const Directory& dir);
// Cleanup properly
~const_iterator();
const_iterator(const const_iterator& i);
const_iterator& operator=(const const_iterator& i);
const_iterator& operator++();
std::string operator*() const;
bool operator==(const const_iterator& iter) const;
bool operator!=(const const_iterator& iter) const;
bool isdir() const;
bool isblk() const;
bool ischr() const;
bool isfifo() const;
bool islnk() const;
bool isreg() const;
bool issock() const;
};
Directory(const std::string& path);
~Directory();
const std::string& path() const { return m_path; }
bool exists() const;
const_iterator begin() const;
const_iterator end() const;
};
}
}
}
// vim:set ts=4 sw=4:
#endif
wibble::sys::fs::Directory::const_iterator
Definition: fs.h:161
wibble::sys::fs::Directory::const_iterator::operator==
bool operator==(const const_iterator &iter) const
wibble::sys::fs::Directory::const_iterator::islnk
bool islnk() const
wibble::sys::fs::Directory::const_iterator::operator=
const_iterator & operator=(const const_iterator &i)
wibble::sys::fs::Directory::const_iterator::operator*
std::string operator*() const
wibble::sys::fs::Directory::const_iterator::const_iterator
const_iterator(const const_iterator &i)
auto_ptr style copy semantics
wibble::sys::fs::Directory::const_iterator::issock
bool issock() const
wibble::sys::fs::Directory::const_iterator::isblk
bool isblk() const
wibble::sys::fs::Directory::const_iterator::operator!=
bool operator!=(const const_iterator &iter) const
wibble::sys::fs::Directory::const_iterator::isdir
bool isdir() const
wibble::sys::fs::Directory::const_iterator::isreg
bool isreg() const
wibble::sys::fs::Directory::const_iterator::const_iterator
const_iterator(const Directory &dir)
wibble::sys::fs::Directory::const_iterator::isfifo
bool isfifo() const
wibble::sys::fs::Directory::const_iterator::~const_iterator
~const_iterator()
wibble::sys::fs::Directory::const_iterator::const_iterator
const_iterator()
wibble::sys::fs::Directory::const_iterator::operator++
const_iterator & operator++()
Move to the next directory entry.
wibble::sys::fs::Directory::const_iterator::ischr
bool ischr() const
wibble::sys::fs::Directory
Nicely wrap access to directories.
Definition: fs.h:154
wibble::sys::fs::Directory::exists
bool exists() const
Check if the directory exists.
wibble::sys::fs::Directory::begin
const_iterator begin() const
Begin iterator.
wibble::sys::fs::Directory::path
const std::string & path() const
Pathname of the directory.
Definition: fs.h:216
wibble::sys::fs::Directory::end
const_iterator end() const
End iterator.
wibble::sys::fs::Directory::m_path
std::string m_path
Directory pathname.
Definition: fs.h:157
wibble::sys::fs::Directory::Directory
Directory(const std::string &path)
wibble::sys::fs::Directory::~Directory
~Directory()
wibble::str::dirname
std::string dirname(const std::string &pathname)
Given a pathname, return the directory name without the file name.
Definition: string.h:134
wibble::sys::fs::size
size_t size(const std::string &file)
File size.
Definition: fs.cpp:287
wibble::sys::fs::mkdtemp
std::string mkdtemp(std::string templ)
wibble::sys::fs::isreg
bool isreg(const std::string &pathname)
Same as isdir but checks for regular files.
wibble::sys::fs::isblk
bool isblk(const std::string &pathname)
Same as isdir but checks for block devices.
wibble::sys::fs::mkdirIfMissing
void mkdirIfMissing(const std::string &dir, mode_t mode=0777)
Create the given directory, if it does not already exists.
wibble::sys::fs::deleteIfExists
bool deleteIfExists(const std::string &file)
Delete a file if it exists.
Definition: fs.cpp:244
wibble::sys::fs::rmtree
void rmtree(const std::string &dir)
Delete the directory dir and all its content.
wibble::sys::fs::mkpath
void mkpath(const std::string &dir)
Create all the component of the given directory, including the directory itself.
wibble::sys::fs::access
bool access(const std::string &s, int m)
access() a filename
wibble::sys::fs::rmdir
void rmdir(const std::string &dirname)
Remove the directory using rmdir(2)
Definition: fs.cpp:268
wibble::sys::fs::abspath
std::string abspath(const std::string &pathname)
Get the absolute path of a file.
wibble::sys::fs::stat
std::auto_ptr< struct stat > stat(const std::string &pathname)
stat() the given file and return the struct stat with the results.
wibble::sys::fs::issock
bool issock(const std::string &pathname)
Same as isdir but checks for sockets.
wibble::sys::fs::writeFile
void writeFile(const std::string &file, const std::string &data)
Write data to file, replacing existing contents if it already exists.
Definition: fs.cpp:198
wibble::sys::fs::inode
ino_t inode(const std::string &file)
File inode number.
Definition: fs.cpp:300
wibble::sys::fs::exists
bool exists(const std::string &s)
Same as access(s, F_OK);.
wibble::sys::fs::writeFileAtomically
void writeFileAtomically(const std::string &file, const std::string &data)
Write data to file, replacing existing contents if it already exists.
Definition: fs.cpp:206
wibble::sys::fs::renameIfExists
void renameIfExists(const std::string &src, const std::string &dst)
Move src to dst, without raising exception if src does not exist.
Definition: fs.cpp:255
wibble::sys::fs::islnk
bool islnk(const std::string &pathname)
Same as isdir but checks for symbolic links.
wibble::sys::fs::findExecutable
std::string findExecutable(const std::string &name)
Compute the absolute path of an executable.
Definition: fs.cpp:223
wibble::sys::fs::ischr
bool ischr(const std::string &pathname)
Same as isdir but checks for character devices.
wibble::sys::fs::timestamp
time_t timestamp(const std::string &file)
File mtime.
Definition: fs.cpp:274
wibble::sys::fs::readFile
std::string readFile(const std::string &file)
Read whole file into memory. Throws exceptions on failure.
Definition: fs.cpp:164
wibble::sys::fs::isfifo
bool isfifo(const std::string &pathname)
Same as isdir but checks for FIFOs.
wibble::sys::fs::unlink
void unlink(const std::string &fname)
Delete the file.
Definition: fs.cpp:262
wibble::sys::fs::isdir
bool isdir(const std::string &pathname)
Returns true if the given pathname is a directory, else false.
wibble::sys::fs::mkFilePath
void mkFilePath(const std::string &file)
Ensure that the path to the given file exists, creating it if it does not.
wibble
Definition: amorph.h:17