source: trunk/debathena/third/schroot/sbuild/sbuild-mntstream.h @ 24167

Revision 24167, 5.5 KB checked in by broder, 15 years ago (diff)
Import schroot upstream into subversion.
Line 
1/* Copyright © 2003,2006-2008  Roger Leigh <rleigh@debian.org>
2 *
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 3 of the License, or
6 * (at your option) any later version.
7 *
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.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program.  If not, see
15 * <http://www.gnu.org/licenses/>.
16 *
17 *********************************************************************/
18
19#ifndef SBUILD_MNTSTREAM_H
20#define SBUILD_MNTSTREAM_H
21
22#include <sbuild/sbuild-custom-error.h>
23
24#include <iostream>
25#include <deque>
26#include <string>
27
28#include <stdio.h>
29#include <sys/types.h>
30#include <mntent.h>
31
32namespace sbuild
33{
34
35  /**
36   * Access mounts.  This is a wrapper around the setmntent(3),
37   * getmntent(3) and endmntent(3) functions, which are used to read a
38   * stream of "mntents" through multiple getmntent() calls.
39   *
40   * mntstream calls setmntent() and endmntent() automatically, and
41   * represents each mntent as a mntstream::mntentry.  Like reading
42   * from and istream by pulling data out with the >> "extraction
43   * operator", mntentries are also extracted from the mntstream with
44   * the >> operator.
45   */
46  class mntstream
47    {
48    public:
49      /// Error codes.
50      enum error_code
51        {
52          MNT_OPEN, ///< Failed to open mount file.
53          MNT_READ  ///< Failed to read mount file.
54        };
55
56      /// Exception type.
57      typedef sbuild::custom_error<error_code> error;
58
59      /**
60       * An entry in a mntstream.  It is a wrapper around the mntent
61       * structure declared in mntent.h.  Unlike a mntent pointer returned
62       * by getmntent(3), a mntentry does not become invalid when the
63       * mntstream it was extracted from is destroyed.
64       */
65      struct mntentry
66      {
67        /// The constructor.
68        mntentry ()
69        {};
70
71        /**
72         * The contructor.
73         *
74         * @param entry the mntent structure to wrap.
75         */
76        mntentry (struct mntent const&  entry);
77
78        /// Name of mounted filesystem.
79        std::string  filesystem_name;
80        /// File system path prefix.
81        std::string  directory;
82        /// Mount type.
83        std::string  type;
84        /// Mount options.
85        std::string  options;
86        /// Dump frequency (days).
87        int          dump_frequency;
88        /// Parallel fsck pass number.
89        int          fsck_pass;
90      };
91
92      /**
93       * The constructor.
94       *
95       * @param file the file to read.
96       */
97      mntstream(std::string const& file);
98
99      /// The destructor.
100      virtual ~mntstream();
101
102      /**
103       * Open a mount file for reading.  This uses the openmnt(3) call
104       * to open the underlying FILE stream.  Any previously open
105       * mount file is closed before opening the new one.  The
106       * mntstream error state is set if the open fails, and an
107       * exception will be thrown.
108       *
109       * @param file the file to read.
110       * @see close()
111       */
112      void open(std::string const& file);
113
114      /**
115       * Close the mount file.  This uses the closemnt(3) call to
116       * close the underlying FILE stream.  All cached data is deleted
117       * and the error state set until open() is called.
118       *
119       * @see open()
120       */
121      void close();
122
123      /**
124       * Check for End Of File.  Note that the end of file status is
125       * only set adter a read fails, so this should be checked after
126       * each read.
127       *
128       * @returns true if the mntstream is empty, otherwise false.
129       */
130      bool eof() const;
131
132      /**
133       * Check for errors.  If there is an error, the mntstream is
134       * unusable until the next open() call.
135       *
136       * @returns true if the mntstream is in an error state, otherwise
137       * false.
138       */
139      bool bad() const;
140
141      /**
142       * Check if the mntstream status is good.
143       *
144       * @returns true if the status is good (eof() and bad() both
145       * return false).
146       */
147      operator bool ();
148
149      /**
150       * Check if the mntstream status is bad.
151       *
152       * @returns true if the status is bad (eof() or bad() return
153       * true).
154       */
155      bool
156      operator ! ();
157
158      friend mntstream&
159      operator >> (mntstream& stream,
160                   mntentry&  entry);
161
162    private:
163      /**
164       * Read mntents from the underlying FILE stream into the data
165       * deque.  If the read fails, the error state will be set, and
166       * an exception will be thrown.
167       *
168       * @param quantity the number of mntents to read.
169       *
170       * @todo Add mntentry constructor to do automatic struct mntent
171       * to mntentry conversion.
172       */
173      void read (int quantity=1);
174
175      /// The file name.
176      std::string file;
177
178      /// The underlying FILE stream.
179      FILE *mntfile;
180
181      /**
182       * A list of mntentries represents the mount file stream as a
183       * LIFO stack.
184       */
185      std::deque<mntentry> data;
186
187      /// Error status.
188      bool error_status;
189
190      /// End of File status.
191      bool eof_status;
192  };
193
194  /**
195   * The overloaded extraction operator.  This is used to pull
196   * mntentries from a mntstream.
197   *
198   * @param stream the mntstream to get input from.
199   * @param entry the mntentry to set.
200   * @returns the mntstream.
201   */
202  mntstream&
203  operator >> (mntstream&            stream,
204               mntstream::mntentry&  entry);
205
206}
207
208#endif /* SBUILD_MNTSTREAM_H */
209
210/*
211 * Local Variables:
212 * mode:C++
213 * End:
214 */
Note: See TracBrowser for help on using the repository browser.