wibble 1.1
fs.h
Go to the documentation of this file.
1#ifndef WIBBLE_SYS_DIRECTORY_H
2#define WIBBLE_SYS_DIRECTORY_H
3
4#include <string>
5#include <iosfwd>
6#include <memory> // auto_ptr
7#include <sys/types.h> // mode_t
8#include <sys/stat.h> // struct stat
9#include <unistd.h> // access
10
11struct dirent;
12
13namespace wibble {
14namespace sys {
15namespace fs {
16
22std::auto_ptr<struct stat> stat(const std::string& pathname);
23
28void stat(const std::string& pathname, struct stat& st);
29
31bool access(const std::string& s, int m);
32
34bool exists(const std::string& s);
35
39std::string abspath(const std::string& pathname);
40
41// Create a temporary directory based on a template.
42std::string mkdtemp( std::string templ );
43
47void mkdirIfMissing(const std::string& dir, mode_t mode = 0777);
48
51void mkpath(const std::string& dir);
52
55void mkFilePath(const std::string& file);
56
58std::string readFile(const std::string &file);
59
67std::string readFile(std::istream& file, const std::string& filename);
68
70void writeFile(const std::string &file, const std::string &data);
71
78void writeFileAtomically(const std::string &file, const std::string &data);
79
87std::string findExecutable(const std::string& name);
88
94bool deleteIfExists(const std::string& file);
95
97void renameIfExists(const std::string& src, const std::string& dst);
98
100void unlink(const std::string& fname);
101
103void rmdir(const std::string& dirname);
104
106void rmtree(const std::string& dir);
107
113bool isdir(const std::string& pathname);
114
116bool isblk(const std::string& pathname);
117
119bool ischr(const std::string& pathname);
120
122bool isfifo(const std::string& pathname);
123
125bool islnk(const std::string& pathname);
126
128bool isreg(const std::string& pathname);
129
131bool issock(const std::string& pathname);
132
134time_t timestamp(const std::string& file);
135
137time_t timestamp(const std::string& file, time_t def);
138
140size_t size(const std::string& file);
141
143size_t size(const std::string& file, size_t def);
144
146ino_t inode(const std::string& file);
147
149ino_t inode(const std::string& file, ino_t def);
150
151
154{
155protected:
157 std::string m_path;
158
159public:
161 {
163 const Directory* dir;
165 void* dirp;
167 struct dirent* direntbuf;
168
169 public:
170 // Create an end iterator
172 // Create a begin iterator
174 // Cleanup properly
176
180
183
185 std::string operator*() const;
186
187 bool operator==(const const_iterator& iter) const;
188 bool operator!=(const const_iterator& iter) const;
189
191 bool isdir() const;
192
194 bool isblk() const;
195
197 bool ischr() const;
198
200 bool isfifo() const;
201
203 bool islnk() const;
204
206 bool isreg() const;
207
209 bool issock() const;
210 };
211
212 Directory(const std::string& path);
214
216 const std::string& path() const { return m_path; }
217
219 bool exists() const;
220
223
226};
227
228}
229}
230}
231
232// vim:set ts=4 sw=4:
233#endif
bool operator==(const const_iterator &iter) const
const_iterator(const const_iterator &i)
auto_ptr style copy semantics
bool operator!=(const const_iterator &iter) const
const_iterator & operator=(const const_iterator &i)
const_iterator & operator++()
Move to the next directory entry.
Nicely wrap access to directories.
Definition fs.h:154
bool exists() const
Check if the directory exists.
const_iterator begin() const
Begin iterator.
const_iterator end() const
End iterator.
const std::string & path() const
Pathname of the directory.
Definition fs.h:216
std::string m_path
Directory pathname.
Definition fs.h:157
Directory(const std::string &path)
size_t size(const std::string &file)
File size.
Definition fs.cpp:287
std::string mkdtemp(std::string templ)
bool isreg(const std::string &pathname)
Same as isdir but checks for regular files.
bool isblk(const std::string &pathname)
Same as isdir but checks for block devices.
void mkdirIfMissing(const std::string &dir, mode_t mode=0777)
Create the given directory, if it does not already exists.
bool deleteIfExists(const std::string &file)
Delete a file if it exists.
Definition fs.cpp:244
void rmtree(const std::string &dir)
Delete the directory dir and all its content.
void mkpath(const std::string &dir)
Create all the component of the given directory, including the directory itself.
bool access(const std::string &s, int m)
access() a filename
void rmdir(const std::string &dirname)
Remove the directory using rmdir(2)
Definition fs.cpp:268
std::string abspath(const std::string &pathname)
Get the absolute path of a file.
bool issock(const std::string &pathname)
Same as isdir but checks for sockets.
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
ino_t inode(const std::string &file)
File inode number.
Definition fs.cpp:300
bool exists(const std::string &s)
Same as access(s, F_OK);.
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
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
bool islnk(const std::string &pathname)
Same as isdir but checks for symbolic links.
std::auto_ptr< struct stat > stat(const std::string &pathname)
stat() the given file and return the struct stat with the results.
std::string findExecutable(const std::string &name)
Compute the absolute path of an executable.
Definition fs.cpp:223
bool ischr(const std::string &pathname)
Same as isdir but checks for character devices.
time_t timestamp(const std::string &file)
File mtime.
Definition fs.cpp:274
std::string readFile(const std::string &file)
Read whole file into memory. Throws exceptions on failure.
Definition fs.cpp:164
bool isfifo(const std::string &pathname)
Same as isdir but checks for FIFOs.
void unlink(const std::string &fname)
Delete the file.
Definition fs.cpp:262
bool isdir(const std::string &pathname)
Returns true if the given pathname is a directory, else false.
void mkFilePath(const std::string &file)
Ensure that the path to the given file exists, creating it if it does not.
Definition amorph.h:17
Definition amorph.h:30