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

Revision 24167, 5.6 KB checked in by broder, 15 years ago (diff)
Import schroot upstream into subversion.
Line 
1/* Copyright © 2005-2009  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_RUN_PARTS_H
20#define SBUILD_RUN_PARTS_H
21
22#include <sbuild/sbuild-custom-error.h>
23#include <sbuild/sbuild-environment.h>
24#include <sbuild/sbuild-types.h>
25
26#include <set>
27#include <string>
28
29#include <sys/types.h>
30#include <sys/stat.h>
31
32namespace sbuild
33{
34
35  /**
36   * Run all scripts or programs within a directory.
37   */
38  class run_parts
39  {
40  public:
41    /// Error codes.
42    enum error_code
43      {
44        CHILD_FORK, ///< Failed to fork child.
45        CHILD_WAIT, ///< Wait for child failed.
46        EXEC,       ///< Failed to execute.
47        PIPE,       ///< Failed to create pipe.
48        DUP,        ///< Failed to duplicate file descriptor.
49        POLL,       ///< Failed to poll file descriptor.
50        READ        ///< Failed to read file descriptor.
51      };
52
53    /// Exception type.
54    typedef custom_error<error_code> error;
55
56    /**
57     *  The constructor.
58     *
59     * @param directory the directory to run scripts from.
60     * @param lsb_mode use Linux Standard Base filename requirements.
61     * If true, the following patterns are permitted: LANANA
62     * ("^[a-z0-9]+$"), LSB ("^_?([a-z0-9_.]+-)+[a-z0-9]+$"), and
63     * Debian cron ("^[a-z0-9][a-z0-9-]*$").  Debian dpkg conffile
64     * backups are not permitted ("dpkg-(old|dist|new|tmp)$").  If
65     * false, the traditional run-parts pattern is used
66     * ("^[a-zA-Z0-9_-]$").
67     * @param abort_on_error stop executing scripts if one returns an error.
68     * @param umask the umask to set when running scripts.
69     */
70    run_parts (std::string const& directory,
71               bool               lsb_mode = true,
72               bool               abort_on_error = true,
73               mode_t             umask = 022);
74
75    /// The destructor.
76    ~run_parts ();
77
78    /**
79     * Get the verbosity level.
80     *
81     * @returns true if verbose, otherwise false.
82     */
83    bool
84    get_verbose () const;
85
86    /**
87     * Set the verbosity level.
88     *
89     * @param verbose true to be verbose, otherwise false.
90     */
91    void
92    set_verbose (bool verbose);
93
94    /**
95     * Get the script execution order.
96     *
97     * @returns true if executing in reverse, otherwise false.
98     */
99    bool
100    get_reverse () const;
101
102    /**
103     * Set the script execution order.
104     *
105     * @param reverse true to execute in reverse, otherwise false.
106     */
107    void
108    set_reverse (bool reverse);
109
110    /**
111     * Run all scripts in the specified directory.  If abort_on_error
112     * is true, execution will stop at the first script to fail.
113     *
114     * @param command the command to run.
115     * @param env the environment to use.
116     * @returns the exit status of the scripts.  This will be 0 on
117     * success, or the exit status of the last failing script.
118     */
119    int
120    run(string_list const& command,
121        environment const& env);
122
123    /**
124     * Output the environment to an ostream.
125     *
126     * @param stream the stream to output to.
127     * @param rhs the environment to output.
128     * @returns the stream.
129     */
130    template <class charT, class traits>
131    friend
132    std::basic_ostream<charT,traits>&
133    operator << (std::basic_ostream<charT,traits>& stream,
134                 run_parts const&                  rhs)
135    {
136      if (!rhs.reverse)
137        {
138          for (program_set::const_iterator pos = rhs.programs.begin();
139               pos != rhs.programs.end();
140               ++pos)
141            stream << *pos << '\n';
142        }
143      else
144        {
145          for (program_set::const_reverse_iterator pos = rhs.programs.rbegin();
146               pos != rhs.programs.rend();
147               ++pos)
148            stream << *pos << '\n';
149        }
150      return stream;
151    }
152
153  private:
154    /**
155     * Run the command specified by file (an absolute pathname), using
156     * command and env as the argv and environment, respectively.
157     *
158     * @param file the program to execute.
159     * @param command the arguments to pass to the executable.
160     * @param env the environment.
161     * @returns the return value of the execve system call on failure.
162     */
163    int
164    run_child(std::string const& file,
165              string_list const& command,
166              environment const& env);
167
168    /**
169     * Wait for a child process to complete, and check its exit status.
170     *
171     * An error will be thrown on failure.
172     *
173     * @param pid the pid to wait for.
174     * @param child_status the place to store the child exit status.
175     */
176    void
177    wait_for_child (pid_t pid,
178                    int&  child_status);
179
180    /// A sorted set of filenames to use.
181    typedef std::set<std::string> program_set;
182
183    /// The LSB mode for allowed filenames.
184    bool        lsb_mode;
185    /// Whether to abort on script execution error.
186    bool        abort_on_error;
187    /// The umask to run scripts with.
188    mode_t      umask;
189    /// Verbose logging.
190    bool        verbose;
191    /// Execute scripts in reverse order.
192    bool        reverse;
193    /// The directory to run scripts from.
194    std::string directory;
195    /// The list of scripts to run.
196    program_set programs;
197  };
198
199}
200
201#endif /* SBUILD_RUN_PARTS_H */
202
203/*
204 * Local Variables:
205 * mode:C++
206 * End:
207 */
Note: See TracBrowser for help on using the repository browser.