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