1
/* Copyright © 2003,2006 Roger Leigh <rleigh@debian.org>
3
* schroot is free software; you can redistribute it and/or modify it
4
* under the terms of the GNU General Public License as published by
5
* the Free Software Foundation; either version 2 of the License, or
6
* (at your option) any later version.
8
* schroot is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
* General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18
*********************************************************************/
20
#ifndef SBUILD_DIRSTREAM_H
21
#define SBUILD_DIRSTREAM_H
23
#include <sbuild/sbuild-custom-error.h>
29
#include <sys/types.h>
36
* An entry in a dirstream. It is a wrapper around the dirent
37
* structure declared in dirent.h
39
* The direntry is only valid during the lifetime of an open
40
* dirstream. Once the directory is closed, when the dirstream
41
* is destroyed, or its close() method called, the direntry can
42
* no longer be safely used. On many systems, including Linux,
43
* this does not matter, but the Single UNIX Specification makes
44
* no garuantees about this.
51
{ std::memset(&this->data, 0, sizeof(struct dirent)); }
56
* @param entry the dirent to initialise the class with.
58
direntry(const struct dirent *entry)
59
{ std::memcpy(&this->data, entry, sizeof(struct dirent)); }
62
* The copy constructor.
64
* @param orig the class to copy.
66
direntry(direntry const& orig)
67
{ memcpy(&this->data, &orig.data, sizeof(struct dirent)); }
74
* Get the dirent inode number (d_ino).
76
* @returns the inode number.
79
{ return this->data.d_ino; }
82
* Get the file type (d_type).
84
* @returns the file type.
86
unsigned char type() const
87
{ return this->data.d_type; }
90
* Get the file name (d_name).
92
* @returns a reference to a string containing the name.
94
std::string name() const
95
{ return this->data.d_name; }
100
* @returns a reference to the underlying dirent.
102
struct dirent const& dirent()
103
{ return this->data; }
106
/// The underlying dirent the class is wrapping.
111
* Access directories. This is a wrapper around the opendir(3),
112
* readdir(3) and closedir(3) functions, which are used to read a
113
* stream of "dirents" through multiple readdir() calls.
115
* dirstream calls opendir() and closedir() automatically, and
116
* represents each dirent as a dirstream::direntry. Like reading
117
* from and istream by pulling data out with the >> "extraction
118
* operator", direntries are also extracted from the dirstream with
127
DIR_OPEN, ///< Failed to open directory.
128
DIR_READ ///< Failed to read directory.
132
typedef custom_error<error_code> error;
137
* @param dir the directory to read.
139
dirstream(std::string const& dir);
142
virtual ~dirstream();
145
* Open a directory for reading. This uses the opendir(3) call to
146
* open the underlying DIR stream. Any previously open directory is
147
* closed before opening the new one. The dirstream error state is
148
* set if the open fails, and an exception will be thrown.
150
* @param dirname the directory to read.
153
void open(std::string const& dirname);
156
* Close the directory. This uses the closedir(3) call to close the
157
* underlying DIR stream. All cached data is deleted and the error
158
* state set until open() is called.
165
* Check for End Of File. Note that the end of file status is
166
* only set adter a read fails, so this should be checked after
169
* @returns true if the dirstream is empty, otherwise false.
174
* Check for errors. If there is an error, the dirstream is
175
* unusable until the next open() call.
177
* @returns true if the dirstream is in an error state, otherwise
183
* Check if the dirstream status is good.
185
* @returns true if the status is good (eof() and bad() both
191
* Check if the dirstream status is bad.
193
* @returns true if the status is bad (eof() or bad() return
200
* The overloaded extraction operator. This is used to pull
201
* direntries from a dirstream.
204
operator >> (dirstream& stream,
209
* Read dirents from the underlying DIR stream into the data
210
* deque. If the read fails, the error state will be set, and
211
* an exception will be thrown.
213
* @param quantity the number of dirents to read.
215
void read (int quantity=1);
217
/// The directory name.
220
/// The underlying DIR stream.
224
* A list of direntries represents the directory stream as a LIFO
227
std::deque<direntry> data;
232
/// End of File status.
238
#endif /* SBUILD_DIRSTREAM_H */